From e8a560faee30b2c24015453e33d72f16c9290be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=8D=9A=E6=96=87?= Date: Wed, 24 May 2017 10:56:43 +0800 Subject: [PATCH] draw preview window content at floating window Change-Id: I9d8e16463b3f84de331a0da76f3126ef659cc488 --- frame/item/components/_previewcontainer.cpp | 5 +++-- frame/item/components/appsnapshot.cpp | 10 ++++++--- frame/item/components/appsnapshot.h | 4 ++++ frame/item/components/floatingpreview.cpp | 25 ++++++++++++++++++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/frame/item/components/_previewcontainer.cpp b/frame/item/components/_previewcontainer.cpp index 2d8fc75df..15bca9c65 100644 --- a/frame/item/components/_previewcontainer.cpp +++ b/frame/item/components/_previewcontainer.cpp @@ -4,8 +4,8 @@ #include #include -#define SPACING 5 -#define MARGIN 5 +#define SPACING 0 +#define MARGIN 0 _PreviewContainer::_PreviewContainer(QWidget *parent) : QWidget(parent), @@ -49,6 +49,7 @@ void _PreviewContainer::setWindowInfos(const WindowDict &infos) { if (!m_snapshots.contains(it.key())) appendSnapWidget(it.key()); + m_snapshots[it.key()]->setWindowTitle(it.value()); } adjustSize(); diff --git a/frame/item/components/appsnapshot.cpp b/frame/item/components/appsnapshot.cpp index 92ccf7e72..cd458a1d2 100644 --- a/frame/item/components/appsnapshot.cpp +++ b/frame/item/components/appsnapshot.cpp @@ -42,6 +42,11 @@ void AppSnapshot::closeWindow() const XFlush(display); } +void AppSnapshot::setWindowTitle(const QString &title) +{ + m_title = title; +} + void AppSnapshot::fetchSnapshot() { if (!isVisible()) @@ -105,7 +110,7 @@ void AppSnapshot::paintEvent(QPaintEvent *e) if (m_snapshot.isNull()) return; - const QRect r = rect(); + const QRect r = rect().marginsRemoved(QMargins(8, 8, 8, 8)); // draw image // const QPoint offset = r.center() - ir.center(); @@ -113,8 +118,7 @@ void AppSnapshot::paintEvent(QPaintEvent *e) // painter.fillRect(offset.x(), offset.y(), ir.width(), ir.height(), Qt::white); // painter.drawImage(offset.x(), offset.y(), m_snapshot); // painter.fillRect(r, Qt::white); - const QSize s = size(); - const QImage im = m_snapshot.scaled(s, Qt::KeepAspectRatio, Qt::SmoothTransformation); + const QImage im = m_snapshot.scaled(r.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); const QRect ir = im.rect(); const QPoint offset = r.center() - ir.center(); painter.drawImage(offset.x(), offset.y(), im); diff --git a/frame/item/components/appsnapshot.h b/frame/item/components/appsnapshot.h index d5164d534..3335889f4 100644 --- a/frame/item/components/appsnapshot.h +++ b/frame/item/components/appsnapshot.h @@ -13,6 +13,8 @@ public: explicit AppSnapshot(const WId wid, QWidget *parent = 0); WId wid() const { return m_wid; } + const QImage snapshot() const { return m_snapshot; } + const QString title() const { return m_title; } signals: void entered(const WId wid) const; @@ -20,6 +22,7 @@ signals: public slots: void closeWindow() const; + void setWindowTitle(const QString &title); private slots: void fetchSnapshot(); @@ -33,6 +36,7 @@ private: private: const WId m_wid; + QString m_title; QImage m_snapshot; QTimer *m_fetchSnapshotTimer; diff --git a/frame/item/components/floatingpreview.cpp b/frame/item/components/floatingpreview.cpp index f850a0fa5..a868975d4 100644 --- a/frame/item/components/floatingpreview.cpp +++ b/frame/item/components/floatingpreview.cpp @@ -42,9 +42,32 @@ void FloatingPreview::paintEvent(QPaintEvent *e) { QWidget::paintEvent(e); + if (m_tracked.isNull()) + return; + QPainter painter(this); - painter.fillRect(rect(), Qt::red); + const QRect r = rect().marginsRemoved(QMargins(8, 8, 8, 8)); + const QImage snapshot = m_tracked->snapshot(); + const QImage im = snapshot.scaled(r.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); + const QRect ir = im.rect(); + const QPoint offset = r.center() - ir.center(); + painter.fillRect(r, Qt::black); + painter.drawImage(offset.x(), offset.y(), im); + + // bottom black background + QRect bgr = r; + bgr.setTop(bgr.bottom() - 25); + painter.fillRect(bgr, QColor(0, 0, 0, 255 * 0.3)); + + // bottom title + painter.drawText(bgr, Qt::AlignCenter, m_tracked->title()); + + // draw border + const QRect br = r.marginsAdded(QMargins(1, 1, 1, 1)); + painter.setBrush(Qt::transparent); + painter.setRenderHint(QPainter::Antialiasing); + painter.drawRoundedRect(br, 3, 3); } void FloatingPreview::mouseReleaseEvent(QMouseEvent *e)