From ecbd619566c2664034fb100598de68e2cba8e35a Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 19 Mar 2015 11:20:01 +0100 Subject: [PATCH] make PlasmaCore.ColorScope work better as advertised Since attached properties cannot ever change during the lifetime of the object, a colorscope needs to be created for each obect that asks for the attached property. now each instance if (internal) has m_inherit (only those created as an attached property), will search for the property of the proper ancestor. Not super efficient, but the proeprties should be actually asked for rarely enough. doesn't look much nice, but seems to work pretty reliably and the only way to. Change-Id: I735da5551dbb8c2641be88d245f998d00ed977ac REVIEW:122988 --- src/declarativeimports/core/colorscope.cpp | 104 ++++++++++++++++--- src/declarativeimports/core/colorscope.h | 9 +- src/declarativeimports/core/framesvgitem.cpp | 16 +++ src/declarativeimports/core/framesvgitem.h | 13 +++ 4 files changed, 126 insertions(+), 16 deletions(-) diff --git a/src/declarativeimports/core/colorscope.cpp b/src/declarativeimports/core/colorscope.cpp index cca0d2fed..92dcb3e74 100644 --- a/src/declarativeimports/core/colorscope.cpp +++ b/src/declarativeimports/core/colorscope.cpp @@ -25,11 +25,9 @@ #include -ColorScope *ColorScope::s_colorScope = 0; - - ColorScope::ColorScope(QQuickItem *parent) : QQuickItem(parent), + m_inherit(false), m_group(Plasma::Theme::NormalColorGroup) { connect(&m_theme, &Plasma::Theme::themeChanged, this, &ColorScope::colorsChanged); @@ -42,30 +40,59 @@ ColorScope::~ColorScope() ColorScope *ColorScope::qmlAttachedProperties(QObject *object) { - QObject *p = object; + QQuickItem *qp = qobject_cast(object); + ColorScope *s = new ColorScope(qp); + if (!qp) { + s->setParent(object); + } + s->m_inherit = true; + return s; +} + +ColorScope *ColorScope::findParentScope() const +{ + QObject *p = parentItem(); + if (!p) { + p = parent(); + } + while (p) { ColorScope *c = qobject_cast(p); if (c) { - return c; + if (c != m_parentScope) { + if (m_parentScope) { + disconnect(m_parentScope.data(), &ColorScope::colorGroupChanged, + this, &ColorScope::colorGroupChanged); + disconnect(m_parentScope.data(), &ColorScope::colorsChanged, + this, &ColorScope::colorsChanged); + } + connect(c, &ColorScope::colorGroupChanged, + this, &ColorScope::colorGroupChanged); + connect(c, &ColorScope::colorsChanged, + this, &ColorScope::colorsChanged); + //HACK + const_cast(this)->m_parentScope = c; + } + return m_parentScope; } - //this will be parent() for qobjects, parentItem for QQuickItems - QQuickItem *item = qobject_cast(p); - if (item) { - p = item->parentItem(); + + if (QQuickItem *qi = qobject_cast(p)) { + p = qi->parentItem(); } else { p = p->parent(); } } - if (!s_colorScope) { - s_colorScope = new ColorScope; + if (m_parentScope) { + disconnect(m_parentScope.data(), &ColorScope::colorGroupChanged, + this, &ColorScope::colorGroupChanged); + disconnect(m_parentScope.data(), &ColorScope::colorsChanged, + this, &ColorScope::colorsChanged); } - - return s_colorScope; + const_cast(this)->m_parentScope.clear(); + return 0; } - - void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group) { if (m_group == group) { @@ -80,22 +107,69 @@ void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group) Plasma::Theme::ColorGroup ColorScope::colorGroup() const { + if (m_inherit) { + ColorScope *s = findParentScope(); + if (s) { + return s->colorGroup(); + } + } return m_group; } QColor ColorScope::textColor() const { + if (m_inherit) { + ColorScope *s = findParentScope(); + if (s) { + return s->textColor(); + } + } return m_theme.color(Plasma::Theme::TextColor, m_group); } QColor ColorScope::highlightColor() const { + if (m_inherit) { + ColorScope *s = findParentScope(); + if (s) { + return s->highlightColor(); + } + } return m_theme.color(Plasma::Theme::HighlightColor, m_group); } QColor ColorScope::backgroundColor() const { + if (m_inherit) { + ColorScope *s = findParentScope(); + if (s) { + return s->backgroundColor(); + } + } return m_theme.color(Plasma::Theme::BackgroundColor, m_group); } +void ColorScope::itemChange(ItemChange change, const ItemChangeData &value) +{ + if (change == QQuickItem::ItemSceneChange) { + //we have a window: create the representations if needed + if (value.window) { + emit colorGroupChanged(); + emit colorsChanged(); + } + } + + QQuickItem::itemChange(change, value); +} + +bool ColorScope::event(QEvent *event) +{ + if (event->type() == QEvent::ParentChange) { + emit colorGroupChanged(); + emit colorsChanged(); + } + + return QQuickItem::event(event); +} + #include "moc_colorscope.cpp" diff --git a/src/declarativeimports/core/colorscope.h b/src/declarativeimports/core/colorscope.h index fa75b9daa..93b330352 100644 --- a/src/declarativeimports/core/colorscope.h +++ b/src/declarativeimports/core/colorscope.h @@ -76,14 +76,21 @@ public: /// @endcond + ColorScope *findParentScope() const; + void itemChange(ItemChange change, const ItemChangeData &value); + +protected: + bool event(QEvent *event); + Q_SIGNALS: void colorGroupChanged(); void colorsChanged(); private: + bool m_inherit; Plasma::Theme m_theme; Plasma::Theme::ColorGroup m_group; - static ColorScope *s_colorScope; + QPointer m_parentScope; }; QML_DECLARE_TYPEINFO(ColorScope, QML_HAS_ATTACHED_PROPERTIES) diff --git a/src/declarativeimports/core/framesvgitem.cpp b/src/declarativeimports/core/framesvgitem.cpp index e2a078903..ba7e37d91 100644 --- a/src/declarativeimports/core/framesvgitem.cpp +++ b/src/declarativeimports/core/framesvgitem.cpp @@ -348,6 +348,22 @@ FrameSvgItemMargins *FrameSvgItem::fixedMargins() const return m_fixedMargins; } +void FrameSvgItem::setColorGroup(Plasma::Theme::ColorGroup group) +{ + if (m_frameSvg->colorGroup() == group) { + return; + } + + m_frameSvg->setColorGroup(group); + + emit colorGroupChanged(); +} + +Plasma::Theme::ColorGroup FrameSvgItem::colorGroup() const +{ + return m_frameSvg->colorGroup(); +} + bool FrameSvgItem::fromCurrentTheme() const { return m_frameSvg->fromCurrentTheme(); diff --git a/src/declarativeimports/core/framesvgitem.h b/src/declarativeimports/core/framesvgitem.h index 20486c712..d6681ad93 100644 --- a/src/declarativeimports/core/framesvgitem.h +++ b/src/declarativeimports/core/framesvgitem.h @@ -149,6 +149,15 @@ class FrameSvgItem : public QQuickItem */ Q_PROPERTY(bool fromCurrentTheme READ fromCurrentTheme NOTIFY fromCurrentThemeChanged) + /** + * Set a color group for the FrameSvgItem. + * if the Svg uses stylesheets and has elements + * that are eithe TextColor or BackgroundColor class, + * make them use ButtonTextColor/ButtonBackgroundColor + * or ViewTextColor/ViewBackgroundColor, ComplementaryTextColor etc. + */ + Q_PROPERTY(Plasma::Theme::ColorGroup colorGroup READ colorGroup WRITE setColorGroup NOTIFY colorGroupChanged) + public: /** * @return true if the svg has the necessary elements with the given prefix @@ -173,6 +182,9 @@ public: FrameSvgItemMargins *margins() const; FrameSvgItemMargins *fixedMargins() const; + void setColorGroup(Plasma::Theme::ColorGroup group); + Plasma::Theme::ColorGroup colorGroup() const; + bool fromCurrentTheme() const; void geometryChanged(const QRectF &newGeometry, @@ -197,6 +209,7 @@ Q_SIGNALS: void prefixChanged(); void enabledBordersChanged(); void fromCurrentThemeChanged(); + void colorGroupChanged(); private Q_SLOTS: void doUpdate();