diff --git a/.gitignore b/.gitignore index 301af0f0b..37cc3aecb 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ *.a build/ -*.pro.user +*.pro.user* diff --git a/dde-dock/Widgets/windowpreview.cpp b/dde-dock/Widgets/windowpreview.cpp new file mode 100644 index 000000000..e50c23c01 --- /dev/null +++ b/dde-dock/Widgets/windowpreview.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "windowpreview.h" + +WindowPreview::WindowPreview(WId sourceWindow, QWidget *parent) + : QWidget(parent), + m_sourceWindow(sourceWindow), + m_cache(NULL), + m_timer(new QTimer(this)) +{ + m_timer->setInterval(500); + m_timer->setSingleShot(false); + m_timer->start(); + connect(m_timer, &QTimer::timeout, [=]{ this->updateCache(); this->repaint(); }); +} + +WindowPreview::~WindowPreview() +{ + clearCache(); +} + +void WindowPreview::paintEvent(QPaintEvent * event) +{ + if (m_cache) { + QPainter painter(this); + + QRect rect = m_cache->rect(); + rect.moveCenter(event->rect().center()); + + painter.drawImage(rect, *m_cache); + painter.end(); + } +} + +void WindowPreview::clearCache() +{ + if (m_cache) { + delete m_cache; + m_cache = NULL; + } +} + +void WindowPreview::updateCache() +{ + clearCache(); + + Display *dpy = QX11Info::display(); + + XWindowAttributes watts; + Status status = XGetWindowAttributes(dpy, m_sourceWindow, &watts); + + if (status != 0) { + XImage *image = XGetImage(dpy, m_sourceWindow, + watts.x, watts.y, watts.width, watts.height, + AllPlanes, ZPixmap); + if (image) { + QImage cache(watts.width, watts.height, QImage::Format_RGB32); + + for (int y = 0; y < watts.height; y++) { + for (int x = 0; x < watts.width; x++) { + u_long pixel = XGetPixel(image, x, y); + + cache.setPixel(x, y, pixel); + } + } + + cache = cache.scaledToWidth(width(), Qt::SmoothTransformation); + + m_cache = new QImage(cache); + } + } +} diff --git a/dde-dock/Widgets/windowpreview.h b/dde-dock/Widgets/windowpreview.h new file mode 100644 index 000000000..c8abc1127 --- /dev/null +++ b/dde-dock/Widgets/windowpreview.h @@ -0,0 +1,30 @@ +#ifndef WINDOWPREVIEW_H +#define WINDOWPREVIEW_H + +#include +#include + +class QImage; +class QTimer; + +class WindowPreview : public QWidget +{ + Q_OBJECT + +public: + WindowPreview(WId sourceWindow, QWidget *parent = 0); + ~WindowPreview(); + +protected: + void paintEvent(QPaintEvent * event) Q_DECL_OVERRIDE; + +private: + WId m_sourceWindow; + QImage *m_cache; + QTimer *m_timer; + + void clearCache(); + void updateCache(); +}; + +#endif // WINDOWPREVIEW_H diff --git a/dde-dock/dde-dock.pro b/dde-dock/dde-dock.pro index 97f66628e..1abe9839f 100644 --- a/dde-dock/dde-dock.pro +++ b/dde-dock/dde-dock.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui +QT += core gui x11extras greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -22,7 +22,8 @@ SOURCES += main.cpp\ Widgets/dockview.cpp \ Widgets/dockitemdelegate.cpp \ Widgets/appitem.cpp \ - Widgets/docklayout.cpp + Widgets/docklayout.cpp \ + Widgets/windowpreview.cpp HEADERS += mainwidget.h \ Panel/panel.h \ @@ -33,10 +34,11 @@ HEADERS += mainwidget.h \ Widgets/dockview.h \ Widgets/dockitemdelegate.h \ Widgets/appitem.h \ - Widgets/docklayout.h + Widgets/docklayout.h \ + Widgets/windowpreview.h RESOURCES += \ images.qrc -PKGCONFIG += gtk+-2.0 +PKGCONFIG += gtk+-2.0 x11 CONFIG += c++11 link_pkgconfig