From 660082707fcd8802096b779be67700c38b08ef56 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Fri, 9 Oct 2009 14:40:03 +0000 Subject: [PATCH] move the windows thumbnail code in WindowEffects, still to be decided if windowsizes(geometries?) will be in KWindowsystem svn path=/trunk/KDE/kdelibs/; revision=1033125 --- private/windowpreview.cpp | 76 +++++-------------------------- private/windowpreview_p.h | 1 - windoweffects.cpp | 94 +++++++++++++++++++++++++++++++++++++++ windoweffects.h | 34 ++++++++++++++ 4 files changed, 138 insertions(+), 67 deletions(-) diff --git a/private/windowpreview.cpp b/private/windowpreview.cpp index 8477fe8e6..ee0e1f586 100644 --- a/private/windowpreview.cpp +++ b/private/windowpreview.cpp @@ -28,6 +28,7 @@ #include #include +#include #ifdef Q_WS_X11 #include @@ -38,27 +39,6 @@ namespace Plasma { -bool WindowPreview::previewsAvailable() // static -{ - if (!KWindowSystem::compositingActive()) { - return false; - } -#ifdef Q_WS_X11 - // hackish way to find out if KWin has the effect enabled, - // TODO provide proper support - Display *dpy = QX11Info::display(); - Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False); - int cnt; - Atom *list = XListProperties(dpy, DefaultRootWindow(dpy), &cnt); - if (list != NULL) { - bool ret = (qFind(list, list + cnt, atom) != list + cnt); - XFree(list); - return ret; - } -#endif - return false; -} - WindowPreview::WindowPreview(QWidget *parent) : QWidget(parent) { @@ -69,7 +49,7 @@ WindowPreview::WindowPreview(QWidget *parent) void WindowPreview::setWindowIds(const QList wids) { - if (!previewsAvailable()) { + if (!WindowEffects::isEffectAvailable(WindowEffects::WindowPreview)) { setMinimumSize(0,0); setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); ids.clear(); @@ -83,7 +63,7 @@ void WindowPreview::setWindowIds(const QList wids) ids = wids.mid(0, 4); } - readWindowSizes(); + windowSizes = WindowEffects::windowSizes(ids); QSize s(sizeHint()); if (s.isValid()) { setFixedSize(sizeHint()); @@ -102,7 +82,7 @@ QSize WindowPreview::sizeHint() const } if (!windowSizes.size() == 0) { - readWindowSizes(); + windowSizes = WindowEffects::windowSizes(ids); } int maxHeight = 0; @@ -126,22 +106,6 @@ QSize WindowPreview::sizeHint() const return s; } -void WindowPreview::readWindowSizes() const -{ - windowSizes.clear(); - foreach (WId id, ids) { -#ifdef Q_WS_X11 - if (id > 0) { - KWindowInfo info = KWindowSystem::windowInfo(id, NET::WMGeometry|NET::WMFrameExtents); - windowSizes.append(info.frameGeometry().size()); - } else { - windowSizes.append(QSize()); - } -#else - windowSizes.append(QSize()); -#endif - } -} bool WindowPreview::isEmpty() const { @@ -156,20 +120,17 @@ bool WindowPreview::isEmpty() const void WindowPreview::setInfo() { -#ifdef Q_WS_X11 - Display *dpy = QX11Info::display(); - Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False); if (isEmpty()) { - XDeleteProperty(dpy, parentWidget()->winId(), atom); + WindowEffects::showWindowThumbnails(parentWidget()->winId()); return; } if (windowSizes.size() == 0) { - readWindowSizes(); + windowSizes = WindowEffects::windowSizes(ids); } if (windowSizes.size() == 0) { - XDeleteProperty(dpy, parentWidget()->winId(), atom); + WindowEffects::showWindowThumbnails(parentWidget()->winId()); return; } @@ -195,30 +156,13 @@ void WindowPreview::setInfo() x += s.width() + WINDOW_MARGIN; } - QVarLengthArray data(1 + (6 * numWindows)); - data[0] = numWindows; - - for (int i = 0; i < numWindows; ++i) { - const int start = (i * 6) + 1; - const QRect thumbnailRect = m_thumbnailRects[i]; - - data[start] = 5; - data[start+1] = ids[i]; - data[start+2] = thumbnailRect.x(); - data[start+3] = thumbnailRect.y(); - data[start+4] = thumbnailRect.width(); - data[start+5] = thumbnailRect.height(); - } - - XChangeProperty(dpy, parentWidget()->winId(), atom, atom, 32, PropModeReplace, - reinterpret_cast(data.data()), data.size()); -#endif + WindowEffects::showWindowThumbnails(parentWidget()->winId(), ids, m_thumbnailRects); } void WindowPreview::paintEvent(QPaintEvent *e) { Q_UNUSED(e) -#ifdef Q_WS_X11 + QPainter painter(this); qreal left, top, right, bottom; @@ -229,7 +173,7 @@ void WindowPreview::paintEvent(QPaintEvent *e) m_background->resizeFrame(r.size()+QSize(left+right, top+bottom)); m_background->paintFrame(&painter, r.topLeft()-pos()-QPoint(left,top)); } -#endif + } void WindowPreview::mousePressEvent(QMouseEvent *event) diff --git a/private/windowpreview_p.h b/private/windowpreview_p.h index e8622927b..331db673d 100644 --- a/private/windowpreview_p.h +++ b/private/windowpreview_p.h @@ -58,7 +58,6 @@ protected: void mousePressEvent(QMouseEvent *event); private: - void readWindowSizes() const; QList ids; mutable QList windowSizes; diff --git a/windoweffects.cpp b/windoweffects.cpp index c79b2d33a..d763f8c7f 100644 --- a/windoweffects.cpp +++ b/windoweffects.cpp @@ -34,6 +34,41 @@ namespace Plasma namespace WindowEffects { +//FIXME: check if this works for any atom? +bool isEffectAvailable(Effect effect) +{ + if (!KWindowSystem::compositingActive()) { + return false; + } +#ifdef Q_WS_X11 + QString effectName; + + switch (effect) { + case Slide: + effectName = "_KDE_SLIDE"; + break; + case WindowPreview: + effectName = "_KDE_WINDOW_PREVIEW"; + break; + default: + return false; + } + + // hackish way to find out if KWin has the effect enabled, + // TODO provide proper support + Display *dpy = QX11Info::display(); + Atom atom = XInternAtom(dpy, effectName.toLatin1(), False); + int cnt; + Atom *list = XListProperties(dpy, DefaultRootWindow(dpy), &cnt); + if (list != NULL) { + bool ret = (qFind(list, list + cnt, atom) != list + cnt); + XFree(list); + return ret; + } +#endif + return false; +} + void slideWindow(WId id, Plasma::Location location, int offset) { #ifdef Q_WS_X11 @@ -104,6 +139,65 @@ void slideWindow(QWidget *widget, Plasma::Location location) #endif } +QList windowSizes(const QList &ids) +{ + QList windowSizes; + foreach (WId id, ids) { +#ifdef Q_WS_X11 + if (id > 0) { + KWindowInfo info = KWindowSystem::windowInfo(id, NET::WMGeometry|NET::WMFrameExtents); + windowSizes.append(info.frameGeometry().size()); + } else { + windowSizes.append(QSize()); + } +#else + windowSizes.append(QSize()); +#endif + } + return windowSizes; +} + +void showWindowThumbnails(WId parent, const QList &windows, const QList &rects) +{ + if (windows.size() != rects.size()) { + return; + } +#ifdef Q_WS_X11 + Display *dpy = QX11Info::display(); + Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False); + if (windows.isEmpty()) { + XDeleteProperty(dpy, parent, atom); + return; + } + + int numWindows = windows.size(); + + QVarLengthArray data(1 + (6 * numWindows)); + data[0] = numWindows; + + QList::const_iterator windowsIt; + QList::const_iterator rectsIt = rects.constBegin(); + int i = 0; + for (windowsIt = windows.constBegin(); windowsIt != windows.constEnd(); ++windowsIt) { + + const int start = (i * 6) + 1; + const QRect thumbnailRect = (*rectsIt); + + data[start] = 5; + data[start+1] = (*windowsIt); + data[start+2] = thumbnailRect.x(); + data[start+3] = thumbnailRect.y(); + data[start+4] = thumbnailRect.width(); + data[start+5] = thumbnailRect.height(); + ++rectsIt; + ++i; + } + + XChangeProperty(dpy, parent, atom, atom, 32, PropModeReplace, + reinterpret_cast(data.data()), data.size()); +#endif +} + } } diff --git a/windoweffects.h b/windoweffects.h index 8c3918fb5..f1c9fc7e8 100644 --- a/windoweffects.h +++ b/windoweffects.h @@ -35,6 +35,19 @@ namespace Plasma */ namespace WindowEffects { + enum Effect { + Slide = 1, + WindowPreview = 2 + }; + + /** + * @return if an atom property is available + * + * @param effect the effect we want to check + * @since 4.4 + */ + PLASMA_EXPORT bool isEffectAvailable(Effect effect); + /** * Mark a window as sliding from screen edge * @@ -56,6 +69,27 @@ namespace WindowEffects * @since 4.4 */ PLASMA_EXPORT void slideWindow(QWidget *widget, Plasma::Location location); + + /** + * @return dimension of all the windows passed as parameter + * + * @param ids all the windows we want the size + * @since 4.4 + */ + PLASMA_EXPORT QList windowSizes(const QList &ids); + + /** + * Paint inside the window parent the thumbnails of the windows list in + * the respective rectangles of the rects list + * + * @param parent window where we should paint + * @param windows windows we want a thumbnail of. + * If it is empty any thumbnail will be deleted + * @param rects rectangles in parent coordinates where to paint the window thumbnails. + * If it is empty any thumbnail will be deleted + * @since 4.4 + */ + PLASMA_EXPORT void showWindowThumbnails(WId parent, const QList &windows = QList(), const QList &rects = QList()); } } // namespace Plasma