Merge branch 'dde-dock' of gitcafe.com:Hualet/dde-workspace-2015 into dde-dock

This commit is contained in:
杨万青 2015-06-25 23:15:22 +08:00
commit 6a1866d0dd
4 changed files with 119 additions and 5 deletions

2
.gitignore vendored
View File

@ -13,4 +13,4 @@
*.a
build/
*.pro.user
*.pro.user*

View File

@ -0,0 +1,82 @@
#include <QImage>
#include <QApplication>
#include <QtX11Extras/QX11Info>
#include <QPaintEvent>
#include <QPainter>
#include <QTimer>
#include <QDebug>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#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);
}
}
XDestroyImage(image);
QImage cacheScaled = cache.scaledToWidth(width(), Qt::SmoothTransformation);
m_cache = new QImage(cacheScaled);
}
}
}

View File

@ -0,0 +1,30 @@
#ifndef WINDOWPREVIEW_H
#define WINDOWPREVIEW_H
#include <QWidget>
#include <QWindow>
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

View File

@ -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