From 027a7a16469d05ae44056c8215e8049a77f7d651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Mon, 1 Dec 2014 09:40:24 +0100 Subject: [PATCH] [kdecoration2] Decoration::shadow returns QSharedPointer Adjust all components to use the QSharedPointer. Aurorae obviously has to create a QSharedPointer, Shadow holds a QSharedPointer for the DecorationShadow (advantage: is kept when the Decoration is destroyed), and the KCM needs to add a property on PreviewItem to get access to the Shadow. It's no longer a Q_PROPERTY on Decoration and we cannot re-add it as a dynamic property (cannot be read from QML side). --- clients/aurorae/src/aurorae.cpp | 10 ++---- .../declarative-plugin/previewitem.cpp | 36 ++++++++++++++----- .../declarative-plugin/previewitem.h | 4 +++ kcmkwin/kwindecoration/qml/main.qml | 16 ++++----- shadow.h | 2 +- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/clients/aurorae/src/aurorae.cpp b/clients/aurorae/src/aurorae.cpp index 6c14324822..47376a2e5e 100644 --- a/clients/aurorae/src/aurorae.cpp +++ b/clients/aurorae/src/aurorae.cpp @@ -347,7 +347,7 @@ void Decoration::init() } else { // create a dummy shadow for the configuration interface if (m_padding) { - KDecoration2::DecorationShadow *s = new KDecoration2::DecorationShadow(this); + auto s = QSharedPointer::create(); s->setPadding(*m_padding); s->setInnerShadowRect(QRect(m_padding->left(), m_padding->top(), 1, 1)); setShadow(s); @@ -394,20 +394,16 @@ void Decoration::paint(QPainter *painter, const QRect &repaintRegion) (m_padding->left() > 0 || m_padding->top() > 0 || m_padding->right() > 0 || m_padding->bottom() > 0) && !client().data()->isMaximized()) { r = r.adjusted(m_padding->left(), m_padding->top(), -m_padding->right(), -m_padding->bottom()); - KDecoration2::DecorationShadow *s = new KDecoration2::DecorationShadow(this); + auto s = QSharedPointer::create(); s->setShadow(m_buffer); s->setPadding(*m_padding); s->setInnerShadowRect(QRect(m_padding->left(), m_padding->top(), m_buffer.width() - m_padding->left() - m_padding->right(), m_buffer.height() - m_padding->top() - m_padding->bottom())); - auto oldShadow = shadow(); setShadow(s); - if (oldShadow) { - delete oldShadow.data(); - } } else { - setShadow(QPointer()); + setShadow(QSharedPointer()); } painter->drawImage(rect(), m_buffer, r); } diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewitem.cpp b/kcmkwin/kwindecoration/declarative-plugin/previewitem.cpp index 9e26fa0646..ac981748d2 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/previewitem.cpp +++ b/kcmkwin/kwindecoration/declarative-plugin/previewitem.cpp @@ -126,6 +126,7 @@ void PreviewItem::setDecoration(Decoration *deco) } } ); + connect(m_decoration, &KDecoration2::Decoration::shadowChanged, this, &PreviewItem::shadowChanged); emit decorationChanged(m_decoration); } @@ -165,7 +166,7 @@ void PreviewItem::paint(QPainter *painter) void PreviewItem::paintShadow(QPainter *painter, int &paddingLeft, int &paddingRight, int &paddingTop, int &paddingBottom) { - const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow(); + const auto &shadow = ((const Decoration*)(m_decoration))->shadow(); if (!shadow) { return; } @@ -208,7 +209,8 @@ void PreviewItem::paintShadow(QPainter *painter, int &paddingLeft, int &paddingR void PreviewItem::mouseDoubleClickEvent(QMouseEvent *event) { - if (const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow()) { + const auto &shadow = m_decoration->shadow(); + if (shadow) { QMouseEvent e(event->type(), event->localPos() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), event->button(), @@ -222,7 +224,8 @@ void PreviewItem::mouseDoubleClickEvent(QMouseEvent *event) void PreviewItem::mousePressEvent(QMouseEvent *event) { - if (const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow()) { + const auto &shadow = m_decoration->shadow(); + if (shadow) { QMouseEvent e(event->type(), event->localPos() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), event->button(), @@ -236,7 +239,8 @@ void PreviewItem::mousePressEvent(QMouseEvent *event) void PreviewItem::mouseReleaseEvent(QMouseEvent *event) { - if (const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow()) { + const auto &shadow = m_decoration->shadow(); + if (shadow) { QMouseEvent e(event->type(), event->localPos() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), event->button(), @@ -250,7 +254,8 @@ void PreviewItem::mouseReleaseEvent(QMouseEvent *event) void PreviewItem::mouseMoveEvent(QMouseEvent *event) { - if (const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow()) { + const auto &shadow = m_decoration->shadow(); + if (shadow) { QMouseEvent e(event->type(), event->localPos() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), event->button(), @@ -264,7 +269,8 @@ void PreviewItem::mouseMoveEvent(QMouseEvent *event) void PreviewItem::hoverEnterEvent(QHoverEvent *event) { - if (const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow()) { + const auto &shadow = m_decoration->shadow(); + if (shadow) { QHoverEvent e(event->type(), event->posF() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), event->oldPosF() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), @@ -277,7 +283,8 @@ void PreviewItem::hoverEnterEvent(QHoverEvent *event) void PreviewItem::hoverLeaveEvent(QHoverEvent *event) { - if (const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow()) { + const auto &shadow = m_decoration->shadow(); + if (shadow) { QHoverEvent e(event->type(), event->posF() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), event->oldPosF() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), @@ -290,7 +297,8 @@ void PreviewItem::hoverLeaveEvent(QHoverEvent *event) void PreviewItem::hoverMoveEvent(QHoverEvent *event) { - if (const DecorationShadow *shadow = ((const Decoration*)(m_decoration))->shadow()) { + const auto &shadow = m_decoration->shadow(); + if (shadow) { QHoverEvent e(event->type(), event->posF() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), event->oldPosF() - QPointF(shadow->paddingLeft(), shadow->paddingTop()), @@ -370,5 +378,17 @@ void PreviewItem::syncSize() m_client->setHeight(height() - m_decoration->borderTop() - m_decoration->borderBottom() - heightOffset); } +DecorationShadow *PreviewItem::shadow() const +{ + if (!m_decoration) { + return nullptr; + } + const auto &s = m_decoration->shadow(); + if (!s) { + return nullptr; + } + return s.data(); +} + } } diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewitem.h b/kcmkwin/kwindecoration/declarative-plugin/previewitem.h index c210314956..e052751ef5 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/previewitem.h +++ b/kcmkwin/kwindecoration/declarative-plugin/previewitem.h @@ -26,6 +26,7 @@ namespace KDecoration2 { class Decoration; +class DecorationShadow; class DecorationSettings; namespace Preview @@ -41,6 +42,7 @@ class PreviewItem : public QQuickPaintedItem Q_PROPERTY(KDecoration2::Preview::PreviewBridge *bridge READ bridge WRITE setBridge NOTIFY bridgeChanged) Q_PROPERTY(KDecoration2::Preview::Settings *settings READ settings WRITE setSettings NOTIFY settingsChanged) Q_PROPERTY(KDecoration2::Preview::PreviewClient *client READ client) + Q_PROPERTY(KDecoration2::DecorationShadow *shadow READ shadow NOTIFY shadowChanged) Q_PROPERTY(QColor windowColor READ windowColor WRITE setWindowColor NOTIFY windowColorChanged) Q_PROPERTY(bool drawBackground READ isDrawingBackground WRITE setDrawingBackground NOTIFY drawingBackgroundChanged) public: @@ -64,6 +66,7 @@ public: void setSettings(Settings *settings); PreviewClient *client(); + DecorationShadow *shadow() const; Q_SIGNALS: void decorationChanged(KDecoration2::Decoration *deco); @@ -71,6 +74,7 @@ Q_SIGNALS: void drawingBackgroundChanged(bool); void bridgeChanged(); void settingsChanged(); + void shadowChanged(); protected: void mouseDoubleClickEvent(QMouseEvent *event) override; diff --git a/kcmkwin/kwindecoration/qml/main.qml b/kcmkwin/kwindecoration/qml/main.qml index 0eb3faba10..692d576886 100644 --- a/kcmkwin/kwindecoration/qml/main.qml +++ b/kcmkwin/kwindecoration/qml/main.qml @@ -54,10 +54,10 @@ ScrollView { Component.onCompleted: { client.caption = Qt.binding(function() { return model["display"]; }); client.active = false; - anchors.leftMargin = Qt.binding(function() { return 40 - (inactivePreview.decoration.shadow ? inactivePreview.decoration.shadow.paddingLeft : 0);}); - anchors.rightMargin = Qt.binding(function() { return 10 - (inactivePreview.decoration.shadow ? inactivePreview.decoration.shadow.paddingRight : 0);}); - anchors.topMargin = Qt.binding(function() { return 10 - (inactivePreview.decoration.shadow ? inactivePreview.decoration.shadow.paddingTop : 0);}); - anchors.bottomMargin = Qt.binding(function() { return 40 - (inactivePreview.decoration.shadow ? inactivePreview.decoration.shadow.paddingBottom : 0);}); + anchors.leftMargin = Qt.binding(function() { return 40 - (inactivePreview.shadow ? inactivePreview.shadow.paddingLeft : 0);}); + anchors.rightMargin = Qt.binding(function() { return 10 - (inactivePreview.shadow ? inactivePreview.shadow.paddingRight : 0);}); + anchors.topMargin = Qt.binding(function() { return 10 - (inactivePreview.shadow ? inactivePreview.shadow.paddingTop : 0);}); + anchors.bottomMargin = Qt.binding(function() { return 40 - (inactivePreview.shadow ? inactivePreview.shadow.paddingBottom : 0);}); } } KDecoration.Decoration { @@ -68,10 +68,10 @@ ScrollView { Component.onCompleted: { client.caption = Qt.binding(function() { return model["display"]; }); client.active = true; - anchors.leftMargin = Qt.binding(function() { return 10 - (activePreview.decoration.shadow ? activePreview.decoration.shadow.paddingLeft : 0);}); - anchors.rightMargin = Qt.binding(function() { return 40 - (activePreview.decoration.shadow ? activePreview.decoration.shadow.paddingRight : 0);}); - anchors.topMargin = Qt.binding(function() { return 40 - (activePreview.decoration.shadow ? activePreview.decoration.shadow.paddingTop : 0);}); - anchors.bottomMargin = Qt.binding(function() { return 10 - (activePreview.decoration.shadow ? activePreview.decoration.shadow.paddingBottom : 0);}); + anchors.leftMargin = Qt.binding(function() { return 10 - (activePreview.shadow ? activePreview.shadow.paddingLeft : 0);}); + anchors.rightMargin = Qt.binding(function() { return 40 - (activePreview.shadow ? activePreview.shadow.paddingRight : 0);}); + anchors.topMargin = Qt.binding(function() { return 40 - (activePreview.shadow ? activePreview.shadow.paddingTop : 0);}); + anchors.bottomMargin = Qt.binding(function() { return 10 - (activePreview.shadow ? activePreview.shadow.paddingBottom : 0);}); } } MouseArea { diff --git a/shadow.h b/shadow.h index 4f7169f06b..beb14b499a 100644 --- a/shadow.h +++ b/shadow.h @@ -170,7 +170,7 @@ private: QRegion m_shadowRegion; QSize m_cachedSize; // Decoration based shadows - QPointer m_decorationShadow; + QSharedPointer m_decorationShadow; }; }