add shadows to dialogs

This commit is contained in:
Marco Martin 2013-08-29 15:11:07 +02:00
parent 81f4ab704b
commit 5ecaf1d08b
5 changed files with 554 additions and 0 deletions

View File

@ -9,11 +9,19 @@ INCLUDE_DIRECTORIES(
${KDECLARATIVE_INCLUDE_DIR}
)
add_definitions(-DHAVE_X11=${X11_FOUND})
if(X11_FOUND)
# FIXME: remove when upstream CMakeLists is fixed
find_package(XCB REQUIRED)
endif()
set(corebindings_SRCS
corebindingsplugin.cpp
theme.cpp
datamodel.cpp
datasource.cpp
dialogshadows.cpp
runnermodel.cpp
svgitem.cpp
framesvgitem.cpp
@ -37,6 +45,11 @@ target_link_libraries(corebindingsplugin
KF5::KWindowSystem
plasma)
if(X11_FOUND)
target_link_libraries(corebindingsplugin ${X11_LIBRARIES} ${XCB_XCB_LIBRARIES} )
target_link_libraries(corebindingsplugin Qt5::X11Extras)
endif(X11_FOUND)
install(TARGETS corebindingsplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/plasma/core)
install(FILES qmldir ToolTip.qml DESTINATION ${QML_INSTALL_DIR}/org/kde/plasma/core)

View File

@ -20,6 +20,7 @@
#include "dialog.h"
#include "framesvgitem.h"
#include "dialogshadows_p.h"
#include <QApplication>
#include <QQuickItem>
@ -83,10 +84,12 @@ DialogProxy::DialogProxy(QQuickItem *parent)
m_frameSvgItem = new Plasma::FrameSvgItem(contentItem());
m_frameSvgItem->setImagePath("dialogs/background");
//m_frameSvgItem->setImagePath("widgets/background"); // larger borders, for testing those
DialogShadows::self()->addWindow(this, m_frameSvgItem->enabledBorders());
}
DialogProxy::~DialogProxy()
{
DialogShadows::self()->removeWindow(this);
}
QQuickItem *DialogProxy::mainItem() const
@ -184,6 +187,7 @@ void DialogProxy::setVisible(const bool visible)
}
raise();
}
DialogShadows::self()->addWindow(this, m_frameSvgItem->enabledBorders());
QQuickWindow::setVisible(visible);
}
@ -433,5 +437,11 @@ void DialogProxy::focusOutEvent(QFocusEvent *ev)
QQuickWindow::focusOutEvent(ev);
}
void DialogProxy::showEvent(QShowEvent *event)
{
DialogShadows::self()->addWindow(this, m_frameSvgItem->enabledBorders());
QQuickWindow::showEvent(event);
}
#include "dialog.moc"

View File

@ -148,6 +148,7 @@ protected:
void resizeEvent(QResizeEvent *re);
void focusInEvent(QFocusEvent *ev);
void focusOutEvent(QFocusEvent *ev);
void showEvent(QShowEvent *event);
QTimer *m_syncTimer;
Plasma::Types::Location m_location;

View File

@ -0,0 +1,478 @@
/*
* Copyright 2011 by Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "dialogshadows_p.h"
#include <QWindow>
#include <QPainter>
#ifdef HAVE_X11
#include <QX11Info>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <fixx11h.h>
#endif
#include <qdebug.h>
#include <kglobal.h>
class DialogShadows::Private
{
public:
Private(DialogShadows *shadows)
: q(shadows)
#if HAVE_X11
,_connection( 0x0 ),
_gc( 0x0 )
#endif
{
}
~Private()
{
// Do not call clearPixmaps() from here: it creates new QPixmap(),
// which causes a crash when application is stopping.
freeX11Pixmaps();
}
void freeX11Pixmaps();
void clearPixmaps();
void setupPixmaps();
Qt::HANDLE createPixmap(const QPixmap& source);
void initPixmap(const QString &element);
QPixmap initEmptyPixmap(const QSize &size);
void updateShadow(const QWindow *window, Plasma::FrameSvg::EnabledBorders);
void clearShadow(const QWindow *window);
void updateShadows();
void windowDestroyed(QObject *deletedObject);
void setupData(Plasma::FrameSvg::EnabledBorders enabledBorders);
DialogShadows *q;
QList<QPixmap> m_shadowPixmaps;
QPixmap m_emptyCornerPix;
QPixmap m_emptyCornerLeftPix;
QPixmap m_emptyCornerTopPix;
QPixmap m_emptyCornerRightPix;
QPixmap m_emptyCornerBottomPix;
QPixmap m_emptyVerticalPix;
QPixmap m_emptyHorizontalPix;
//! xcb connection
xcb_connection_t* _connection;
//! graphical context
xcb_gcontext_t _gc;
QHash<Plasma::FrameSvg::EnabledBorders, QVector<unsigned long> > data;
QHash<const QWindow *, Plasma::FrameSvg::EnabledBorders> m_windows;
};
class DialogShadowsSingleton
{
public:
DialogShadowsSingleton()
{
}
DialogShadows self;
};
K_GLOBAL_STATIC(DialogShadowsSingleton, privateDialogShadowsSelf)
DialogShadows::DialogShadows(QObject *parent, const QString &prefix)
: Plasma::Svg(parent),
d(new Private(this))
{
setImagePath(prefix);
connect(this, SIGNAL(repaintNeeded()), this, SLOT(updateShadows()));
}
DialogShadows::~DialogShadows()
{
delete d;
}
DialogShadows *DialogShadows::self()
{
return &privateDialogShadowsSelf->self;
}
void DialogShadows::addWindow(const QWindow *window, Plasma::FrameSvg::EnabledBorders enabledBorders)
{
if (!window) {
return;
}
d->m_windows[window] = enabledBorders;
d->updateShadow(window, enabledBorders);
connect(window, SIGNAL(destroyed(QObject*)),
this, SLOT(windowDestroyed(QObject*)), Qt::UniqueConnection);
}
void DialogShadows::removeWindow(const QWindow *window)
{
if (!d->m_windows.contains(window)) {
return;
}
d->m_windows.remove(window);
disconnect(window, 0, this, 0);
d->clearShadow(window);
if (d->m_windows.isEmpty()) {
d->clearPixmaps();
}
}
void DialogShadows::Private::windowDestroyed(QObject *deletedObject)
{
m_windows.remove(static_cast<QWindow *>(deletedObject));
if (m_windows.isEmpty()) {
clearPixmaps();
}
}
void DialogShadows::Private::updateShadows()
{
setupPixmaps();
QHash<const QWindow *, Plasma::FrameSvg::EnabledBorders>::const_iterator i;
for (i = m_windows.constBegin(); i != m_windows.constEnd(); ++i) {
updateShadow(i.key(), i.value());
}
}
Qt::HANDLE DialogShadows::Private::createPixmap(const QPixmap& source)
{
// do nothing for invalid pixmaps
if( source.isNull() ) return 0;
/*
in some cases, pixmap handle is invalid. This is the case notably
when Qt uses to RasterEngine. In this case, we create an X11 Pixmap
explicitly and draw the source pixmap on it.
*/
#if HAVE_X11
// check connection
if( !_connection ) _connection = QX11Info::connection();
const int width( source.width() );
const int height( source.height() );
// create X11 pixmap
Pixmap pixmap = XCreatePixmap( QX11Info::display(), QX11Info::appRootWindow(), width, height, 32 );
// check gc
if( !_gc )
{
_gc = xcb_generate_id( _connection );
xcb_create_gc( _connection, _gc, pixmap, 0, 0x0 );
}
// // create explicitly shared QPixmap from it
// QPixmap dest( QPixmap::fromX11Pixmap( pixmap, QPixmap::ExplicitlyShared ) );
//
// // create surface for pixmap
// {
// QPainter painter( &dest );
// painter.setCompositionMode( QPainter::CompositionMode_Source );
// painter.drawPixmap( 0, 0, source );
// }
//
//
// return pixmap;
QImage image( source.toImage() );
xcb_put_image(
_connection, XCB_IMAGE_FORMAT_Z_PIXMAP, pixmap, _gc,
image.width(), image.height(), 0, 0,
0, 32,
image.byteCount(), image.constBits());
return (Qt::HANDLE)pixmap;
#else
return 0;
#endif
}
void DialogShadows::Private::initPixmap(const QString &element)
{
m_shadowPixmaps << q->pixmap(element);
}
QPixmap DialogShadows::Private::initEmptyPixmap(const QSize &size)
{
#ifdef HAVE_X11
QPixmap tempEmptyPix(size);
if (!size.isEmpty()) {
tempEmptyPix.fill(Qt::transparent);
}
return tempEmptyPix;
#else
return QPixmap();
#endif
}
void DialogShadows::Private::setupPixmaps()
{
clearPixmaps();
initPixmap("shadow-top");
initPixmap("shadow-topright");
initPixmap("shadow-right");
initPixmap("shadow-bottomright");
initPixmap("shadow-bottom");
initPixmap("shadow-bottomleft");
initPixmap("shadow-left");
initPixmap("shadow-topleft");
m_emptyCornerPix = initEmptyPixmap(QSize(1,1));
m_emptyCornerLeftPix = initEmptyPixmap(QSize(q->elementSize("shadow-topleft").width(), 1));
m_emptyCornerTopPix = initEmptyPixmap(QSize(1, q->elementSize("shadow-topleft").height()));
m_emptyCornerRightPix = initEmptyPixmap(QSize(q->elementSize("shadow-bottomright").width(), 1));
m_emptyCornerBottomPix = initEmptyPixmap(QSize(1, q->elementSize("shadow-bottomright").height()));
m_emptyVerticalPix = initEmptyPixmap(QSize(1, q->elementSize("shadow-left").height()));
m_emptyHorizontalPix = initEmptyPixmap(QSize(q->elementSize("shadow-top").width(), 1));
}
void DialogShadows::Private::setupData(Plasma::FrameSvg::EnabledBorders enabledBorders)
{
#ifdef HAVE_X11
//shadow-top
if (enabledBorders & Plasma::FrameSvg::TopBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[0]));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyHorizontalPix));
}
//shadow-topright
if (enabledBorders & Plasma::FrameSvg::TopBorder &&
enabledBorders & Plasma::FrameSvg::RightBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[1]));
} else if (enabledBorders & Plasma::FrameSvg::TopBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerTopPix));
} else if (enabledBorders & Plasma::FrameSvg::RightBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerRightPix));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerPix));
}
//shadow-right
if (enabledBorders & Plasma::FrameSvg::RightBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[2]));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyVerticalPix));
}
//shadow-bottomright
if (enabledBorders & Plasma::FrameSvg::BottomBorder &&
enabledBorders & Plasma::FrameSvg::RightBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[3]));
} else if (enabledBorders & Plasma::FrameSvg::BottomBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerBottomPix));
} else if (enabledBorders & Plasma::FrameSvg::RightBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerRightPix));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerPix));
}
//shadow-bottom
if (enabledBorders & Plasma::FrameSvg::BottomBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[4]));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyHorizontalPix));
}
//shadow-bottomleft
if (enabledBorders & Plasma::FrameSvg::BottomBorder &&
enabledBorders & Plasma::FrameSvg::LeftBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[5]));
} else if (enabledBorders & Plasma::FrameSvg::BottomBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerBottomPix));
} else if (enabledBorders & Plasma::FrameSvg::LeftBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerLeftPix));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerPix));
}
//shadow-left
if (enabledBorders & Plasma::FrameSvg::LeftBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[6]));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyVerticalPix));
}
//shadow-topleft
if (enabledBorders & Plasma::FrameSvg::TopBorder &&
enabledBorders & Plasma::FrameSvg::LeftBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_shadowPixmaps[7]));
} else if (enabledBorders & Plasma::FrameSvg::TopBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerTopPix));
} else if (enabledBorders & Plasma::FrameSvg::LeftBorder) {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerLeftPix));
} else {
data[enabledBorders] << reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerPix));
}
#endif
int left, top, right, bottom = 0;
QSize marginHint;
if (enabledBorders & Plasma::FrameSvg::TopBorder) {
marginHint = q->elementSize("shadow-hint-top-margin");
if (marginHint.isValid()) {
top = marginHint.height();
} else {
top = m_shadowPixmaps[0].height(); // top
}
} else {
top = 1;
}
if (enabledBorders & Plasma::FrameSvg::RightBorder) {
marginHint = q->elementSize("shadow-hint-right-margin");
if (marginHint.isValid()) {
right = marginHint.width();
} else {
right = m_shadowPixmaps[2].width(); // right
}
} else {
right = 1;
}
if (enabledBorders & Plasma::FrameSvg::BottomBorder) {
marginHint = q->elementSize("shadow-hint-bottom-margin");
if (marginHint.isValid()) {
bottom = marginHint.height();
} else {
bottom = m_shadowPixmaps[4].height(); // bottom
}
} else {
bottom = 1;
}
if (enabledBorders & Plasma::FrameSvg::LeftBorder) {
marginHint = q->elementSize("shadow-hint-left-margin");
if (marginHint.isValid()) {
left = marginHint.width();
} else {
left = m_shadowPixmaps[6].width(); // left
}
} else {
left = 1;
}
data[enabledBorders] << top << right << bottom << left;
}
void DialogShadows::Private::freeX11Pixmaps()
{
#ifdef HAVE_X11
foreach (const QPixmap &pixmap, m_shadowPixmaps) {
if (!QX11Info::display()) {
return;
}
if (!pixmap.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(pixmap)));
}
}
if (!m_emptyCornerPix.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerPix)));
}
if (!m_emptyCornerBottomPix.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerBottomPix)));
}
if (!m_emptyCornerLeftPix.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerLeftPix)));
}
if (!m_emptyCornerRightPix.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerRightPix)));
}
if (!m_emptyCornerTopPix.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(m_emptyCornerTopPix)));
}
if (!m_emptyVerticalPix.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(m_emptyVerticalPix)));
}
if (!m_emptyHorizontalPix.isNull()) {
XFreePixmap(QX11Info::display(), reinterpret_cast<unsigned long>(createPixmap(m_emptyHorizontalPix)));
}
#endif
}
void DialogShadows::Private::clearPixmaps()
{
#ifdef HAVE_X11
freeX11Pixmaps();
m_emptyCornerPix = QPixmap();
m_emptyCornerBottomPix = QPixmap();
m_emptyCornerLeftPix = QPixmap();
m_emptyCornerRightPix = QPixmap();
m_emptyCornerTopPix = QPixmap();
m_emptyVerticalPix = QPixmap();
m_emptyHorizontalPix = QPixmap();
#endif
m_shadowPixmaps.clear();
data.clear();
}
void DialogShadows::Private::updateShadow(const QWindow *window, Plasma::FrameSvg::EnabledBorders enabledBorders)
{
#ifdef HAVE_X11
if (m_shadowPixmaps.isEmpty()) {
setupPixmaps();
}
if (!data.contains(enabledBorders)) {
setupData(enabledBorders);
}
Display *dpy = QX11Info::display();
Atom atom = XInternAtom(dpy, "_KDE_NET_WM_SHADOW", False);
qDebug() << "going to set the shadow of" << window->winId() << "to" << data;
XChangeProperty(dpy, window->winId(), atom, XA_CARDINAL, 32, PropModeReplace,
reinterpret_cast<const unsigned char *>(data[enabledBorders].constData()), data[enabledBorders].size());
#endif
}
void DialogShadows::Private::clearShadow(const QWindow *window)
{
#ifdef HAVE_X11
Display *dpy = QX11Info::display();
Atom atom = XInternAtom(dpy, "_KDE_NET_WM_SHADOW", False);
XDeleteProperty(dpy, window->winId(), atom);
#endif
}
bool DialogShadows::enabled() const
{
return hasElement("shadow-left");
}
#include "moc_dialogshadows_p.cpp"

View File

@ -0,0 +1,52 @@
/*
* Copyright 2011 by Aaron Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef PLASMA_DIALOGSHADOWS_H
#define PLASMA_DIALOGSHADOWS_H
#include <QSet>
#include "plasma/framesvg.h"
#include "plasma/svg.h"
class DialogShadows : public Plasma::Svg
{
Q_OBJECT
public:
explicit DialogShadows(QObject *parent = 0, const QString &prefix = "dialogs/background");
~DialogShadows();
static DialogShadows *self();
void addWindow(const QWindow *window, Plasma::FrameSvg::EnabledBorders enabledBorders = Plasma::FrameSvg::AllBorders);
void removeWindow(const QWindow *window);
bool enabled() const;
private:
class Private;
Private * const d;
Q_PRIVATE_SLOT(d, void updateShadows())
Q_PRIVATE_SLOT(d, void windowDestroyed(QObject *deletedObject))
};
#endif