diff --git a/src/declarativeimports/core/colorscope.cpp b/src/declarativeimports/core/colorscope.cpp index 87ee45c17..f6cefcce0 100644 --- a/src/declarativeimports/core/colorscope.cpp +++ b/src/declarativeimports/core/colorscope.cpp @@ -1,201 +1,215 @@ /*************************************************************************** * Copyright 2011 Marco Martin * * Copyright 2011 Artur Duque de Souza * * Copyright 2013 Sebastian Kügler * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "colorscope.h" #include #include #include QHash ColorScope::s_attachedScopes = QHash(); ColorScope::ColorScope(QQuickItem *parent, QObject *parentObject) : QQuickItem(parent), m_inherit(false), m_group(Plasma::Theme::NormalColorGroup), m_parent(parentObject) { connect(&m_theme, &Plasma::Theme::themeChanged, this, &ColorScope::colorsChanged); + connect(this, &ColorScope::colorGroupChanged, this, &ColorScope::colorsChanged); QQuickItem *parentItem = qobject_cast(parentObject); if (parentItem) { - connect(parentItem, &QQuickItem::parentChanged, this, &ColorScope::colorGroupChanged); - connect(parentItem, &QQuickItem::parentChanged, this, &ColorScope::colorsChanged); + connect(parentItem, &QQuickItem::parentChanged, this, &ColorScope::checkColorGroupChanged); + } else if (m_parent) { + m_parent->installEventFilter(this); } } ColorScope::~ColorScope() { s_attachedScopes.remove(m_parent); } ColorScope *ColorScope::qmlAttachedProperties(QObject *object) { const auto cs = s_attachedScopes.value(object); if (cs) { return cs; } ColorScope *s = new ColorScope(0, object); s_attachedScopes[object] = s; - + s->m_inherit = true; s->setParent(object); + s->checkColorGroupChanged(); - s->m_inherit = true; return s; } -ColorScope *ColorScope::findParentScope() const +bool ColorScope::eventFilter(QObject* watched, QEvent* event) +{ + Q_ASSERT(watched == m_parent && !qobject_cast(watched)); + if (event->type() == QEvent::ParentChange) { + checkColorGroupChanged(); + } + return QQuickItem::eventFilter(watched, event); +} + +void ColorScope::setParentScope(ColorScope* parentScope) +{ + if (parentScope == m_parentScope) + return; + + if (m_parentScope) { + disconnect(m_parentScope.data(), &ColorScope::colorGroupChanged, + this, &ColorScope::checkColorGroupChanged); + } + + m_parentScope = parentScope; + + if (parentScope) { + connect(parentScope, &ColorScope::colorGroupChanged, + this, &ColorScope::checkColorGroupChanged); + } +} + +ColorScope *ColorScope::findParentScope() { QObject *p = 0; if (m_parent) { QQuickItem *gp = qobject_cast(m_parent); if (gp) { p = gp->parentItem(); } else { p = m_parent->parent(); } } if (!p || !m_parent) { - if (m_parentScope) { - disconnect(m_parentScope.data(), &ColorScope::colorGroupChanged, - this, &ColorScope::colorGroupChanged); - disconnect(m_parentScope.data(), &ColorScope::colorsChanged, - this, &ColorScope::colorsChanged); - } + setParentScope(nullptr); return 0; } ColorScope *c = qobject_cast(p); if (!c) { c = qmlAttachedProperties(p); } - 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); - } - if (c) { - connect(c, &ColorScope::colorGroupChanged, - this, &ColorScope::colorGroupChanged); - connect(c, &ColorScope::colorsChanged, - this, &ColorScope::colorsChanged); - } - //HACK - const_cast(this)->m_parentScope = c; - } - + setParentScope(c); return m_parentScope; } void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group) { if (m_group == group) { return; } m_group = group; - emit colorGroupChanged(); - emit colorsChanged(); + checkColorGroupChanged(); } Plasma::Theme::ColorGroup ColorScope::colorGroup() const { - if (m_inherit) { - ColorScope *s = findParentScope(); - if (s) { - return s->colorGroup(); - } - } - return m_group; + return m_actualGroup; } QColor ColorScope::textColor() const { return m_theme.color(Plasma::Theme::TextColor, colorGroup()); } QColor ColorScope::highlightColor() const { return m_theme.color(Plasma::Theme::HighlightColor, colorGroup()); } QColor ColorScope::highlightedTextColor() const { return m_theme.color(Plasma::Theme::HighlightedTextColor, colorGroup()); } QColor ColorScope::backgroundColor() const { return m_theme.color(Plasma::Theme::BackgroundColor, colorGroup()); } QColor ColorScope::positiveTextColor() const { return m_theme.color(Plasma::Theme::PositiveTextColor, colorGroup()); } QColor ColorScope::neutralTextColor() const { return m_theme.color(Plasma::Theme::NeutralTextColor, colorGroup()); } QColor ColorScope::negativeTextColor() const { return m_theme.color(Plasma::Theme::NegativeTextColor, colorGroup()); } bool ColorScope::inherit() const { return m_inherit; } void ColorScope::setInherit(bool inherit) { if (m_inherit == inherit) { return; } m_inherit = inherit; emit inheritChanged(); - emit colorsChanged(); + checkColorGroupChanged(); } 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(); + checkColorGroupChanged(); } } QQuickItem::itemChange(change, value); } +void ColorScope::checkColorGroupChanged() +{ + const auto last = m_actualGroup; + if (m_inherit) { + findParentScope(); + m_actualGroup = m_parentScope ? m_parentScope->colorGroup() : m_group; + } else { + m_actualGroup = m_group; + } + + if (m_actualGroup != last) { + Q_EMIT colorGroupChanged(); + } +} #include "moc_colorscope.cpp" diff --git a/src/declarativeimports/core/colorscope.h b/src/declarativeimports/core/colorscope.h index bb1517ca7..88e6a9762 100644 --- a/src/declarativeimports/core/colorscope.h +++ b/src/declarativeimports/core/colorscope.h @@ -1,132 +1,138 @@ /*************************************************************************** * Copyright 2014 Marco Martin * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef COLORSCOPE_H #define COLORSCOPE_H #include #include #include #include #include class QQuickItem; /** * @class ColorScope * * @short Sets the colour scheme to be used by all child items * * It is exposed as org.kde.plasma.core.ColorScope */ class ColorScope : public QQuickItem { Q_OBJECT /** * Specifies the color group to use for this ColorScope */ Q_PROPERTY(Plasma::Theme::ColorGroup colorGroup READ colorGroup WRITE setColorGroup NOTIFY colorGroupChanged) /** * The main foreground color within this colorscope */ Q_PROPERTY(QColor textColor READ textColor NOTIFY colorsChanged) /** * The highlight color within this colorscope */ Q_PROPERTY(QColor highlightColor READ highlightColor NOTIFY colorsChanged) /** * The highlighted text color within this colorscope */ Q_PROPERTY(QColor highlightedTextColor READ highlightedTextColor NOTIFY colorsChanged) /** * The background color that should be used within this colorscope */ Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY colorsChanged) /** * Color of foreground objects with a "positive message" connotation (usually green) */ Q_PROPERTY(QColor positiveTextColor READ positiveTextColor NOTIFY colorsChanged) /** * Color of foreground objects with a "neutral message" connotation (usually yellow) */ Q_PROPERTY(QColor neutralTextColor READ neutralTextColor NOTIFY colorsChanged) /** * Color of foreground objects with a "negative message" connotation (usually red) */ Q_PROPERTY(QColor negativeTextColor READ negativeTextColor NOTIFY colorsChanged) /** * true if the scope inherits from its parent scope * @since 5.39 */ Q_PROPERTY(bool inherit READ inherit WRITE setInherit NOTIFY inheritChanged) public: /// @cond INTERNAL_DOCS ColorScope(QQuickItem *parent = 0, QObject *parentObject = 0); ~ColorScope(); void setColorGroup(Plasma::Theme::ColorGroup group); Plasma::Theme::ColorGroup colorGroup() const; QColor textColor() const; QColor highlightColor() const; QColor highlightedTextColor() const; QColor backgroundColor() const; QColor positiveTextColor() const; QColor neutralTextColor() const; QColor negativeTextColor() const; bool inherit() const; void setInherit(bool inherit); ////NEEDED BY QML TO CREATE ATTACHED PROPERTIES static ColorScope *qmlAttachedProperties(QObject *object); /// @endcond - ColorScope *findParentScope() const; + ColorScope *findParentScope(); void itemChange(ItemChange change, const ItemChangeData &value) Q_DECL_OVERRIDE; + bool eventFilter(QObject * watched, QEvent * event) override; + Q_SIGNALS: void colorGroupChanged(); void colorsChanged(); void inheritChanged(); private: + void checkColorGroupChanged(); + void setParentScope(ColorScope * parentScope); + bool m_inherit; Plasma::Theme m_theme; Plasma::Theme::ColorGroup m_group; QPointer m_parentScope; - QObject *m_parent; + QObject *const m_parent; + Plasma::Theme::ColorGroup m_actualGroup; static QHash s_attachedScopes; }; QML_DECLARE_TYPEINFO(ColorScope, QML_HAS_ATTACHED_PROPERTIES) #endif