mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 09:23:03 +00:00
add WindowPreview widget
This commit is contained in:
parent
6d1f869fd8
commit
dd11cf57d1
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,4 +13,4 @@
|
|||||||
*.a
|
*.a
|
||||||
|
|
||||||
build/
|
build/
|
||||||
*.pro.user
|
*.pro.user*
|
||||||
|
81
dde-dock/Widgets/windowpreview.cpp
Normal file
81
dde-dock/Widgets/windowpreview.cpp
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
#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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cache = cache.scaledToWidth(width(), Qt::SmoothTransformation);
|
||||||
|
|
||||||
|
m_cache = new QImage(cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
dde-dock/Widgets/windowpreview.h
Normal file
30
dde-dock/Widgets/windowpreview.h
Normal 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
|
@ -4,7 +4,7 @@
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------
|
#-------------------------------------------------
|
||||||
|
|
||||||
QT += core gui
|
QT += core gui x11extras
|
||||||
|
|
||||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||||
|
|
||||||
@ -22,7 +22,8 @@ SOURCES += main.cpp\
|
|||||||
Widgets/dockview.cpp \
|
Widgets/dockview.cpp \
|
||||||
Widgets/dockitemdelegate.cpp \
|
Widgets/dockitemdelegate.cpp \
|
||||||
Widgets/appitem.cpp \
|
Widgets/appitem.cpp \
|
||||||
Widgets/docklayout.cpp
|
Widgets/docklayout.cpp \
|
||||||
|
Widgets/windowpreview.cpp
|
||||||
|
|
||||||
HEADERS += mainwidget.h \
|
HEADERS += mainwidget.h \
|
||||||
Panel/panel.h \
|
Panel/panel.h \
|
||||||
@ -33,10 +34,11 @@ HEADERS += mainwidget.h \
|
|||||||
Widgets/dockview.h \
|
Widgets/dockview.h \
|
||||||
Widgets/dockitemdelegate.h \
|
Widgets/dockitemdelegate.h \
|
||||||
Widgets/appitem.h \
|
Widgets/appitem.h \
|
||||||
Widgets/docklayout.h
|
Widgets/docklayout.h \
|
||||||
|
Widgets/windowpreview.h
|
||||||
|
|
||||||
RESOURCES += \
|
RESOURCES += \
|
||||||
images.qrc
|
images.qrc
|
||||||
|
|
||||||
PKGCONFIG += gtk+-2.0
|
PKGCONFIG += gtk+-2.0 x11
|
||||||
CONFIG += c++11 link_pkgconfig
|
CONFIG += c++11 link_pkgconfig
|
||||||
|
Loading…
x
Reference in New Issue
Block a user