Changeset View
Changeset View
Standalone View
Standalone View
src/declarativeimports/core/colorscope.cpp
Show All 19 Lines | |||||
20 | ***************************************************************************/ | 20 | ***************************************************************************/ | ||
21 | 21 | | |||
22 | #include "colorscope.h" | 22 | #include "colorscope.h" | ||
23 | 23 | | |||
24 | #include <QQmlContext> | 24 | #include <QQmlContext> | ||
25 | #include <QQmlEngine> | 25 | #include <QQmlEngine> | ||
26 | #include <QColor> | 26 | #include <QColor> | ||
27 | 27 | | |||
28 | #include <PlasmaQuick/AppletQuickItem> | ||||
29 | | ||||
28 | QHash<QObject *, ColorScope *> ColorScope::s_attachedScopes = QHash<QObject *, ColorScope *>(); | 30 | QHash<QObject *, ColorScope *> ColorScope::s_attachedScopes = QHash<QObject *, ColorScope *>(); | ||
29 | 31 | | |||
30 | QWeakPointer<Plasma::Theme> ColorScope::s_theme; | 32 | QWeakPointer<Plasma::Theme> ColorScope::s_theme; | ||
31 | 33 | | |||
32 | ColorScope::ColorScope(QQuickItem *parent, QObject *parentObject) | 34 | ColorScope::ColorScope(QQuickItem *parent, QObject *parentObject) | ||
33 | : QQuickItem(parent), | 35 | : QQuickItem(parent), | ||
34 | m_inherit(false), | 36 | m_inherit(false), | ||
35 | m_group(Plasma::Theme::NormalColorGroup), | 37 | m_group(Plasma::Theme::NormalColorGroup), | ||
36 | m_parent(parentObject), | 38 | m_parent(parentObject), | ||
37 | m_actualGroup(Plasma::Theme::NormalColorGroup) | 39 | m_actualGroup(Plasma::Theme::NormalColorGroup) | ||
38 | { | 40 | { | ||
39 | m_theme = s_theme.toStrongRef(); | 41 | m_theme = s_theme.toStrongRef(); | ||
40 | if (!m_theme) { | 42 | if (!m_theme) { | ||
41 | QSharedPointer<Plasma::Theme> themePtr(new Plasma::Theme); | 43 | QSharedPointer<Plasma::Theme> themePtr(new Plasma::Theme); | ||
42 | s_theme = themePtr; | 44 | s_theme = themePtr; | ||
43 | m_theme = s_theme.toStrongRef(); | 45 | m_theme = s_theme.toStrongRef(); | ||
44 | } | 46 | } | ||
45 | 47 | | |||
46 | connect(m_theme.data(), &Plasma::Theme::themeChanged, this, &ColorScope::colorsChanged); | 48 | connect(m_theme.data(), &Plasma::Theme::themeChanged, this, &ColorScope::colorsChanged); | ||
47 | 49 | | |||
48 | connect(this, &ColorScope::colorGroupChanged, this, &ColorScope::colorsChanged); | 50 | connect(this, &ColorScope::colorGroupChanged, this, &ColorScope::colorsChanged); | ||
49 | 51 | | |||
50 | QQuickItem *parentItem = qobject_cast<QQuickItem *>(parentObject); | 52 | if (parentObject && qobject_cast<QQuickItem *>(parentObject)) { | ||
51 | if (parentItem) { | 53 | connect(static_cast<QQuickItem *>(parentObject), &QQuickItem::parentChanged, | ||
52 | connect(parentItem, &QQuickItem::parentChanged, this, &ColorScope::checkColorGroupChanged); | 54 | this, &ColorScope::checkColorGroupChanged); | ||
53 | } else if (m_parent) { | 55 | } else if (parent) { | ||
54 | m_parent->installEventFilter(this); | 56 | connect(parent, &QQuickItem::parentChanged, | ||
57 | this, &ColorScope::checkColorGroupChanged); | ||||
davidedmundson: It' be better to split this colour change stuff.
It's effectively an unrelated bugfix/cleanup | |||||
55 | } | 58 | } | ||
56 | } | 59 | } | ||
57 | 60 | | |||
58 | ColorScope::~ColorScope() | 61 | ColorScope::~ColorScope() | ||
59 | { | 62 | { | ||
60 | s_attachedScopes.remove(m_parent); | 63 | s_attachedScopes.remove(m_parent); | ||
61 | } | 64 | } | ||
62 | 65 | | |||
63 | ColorScope *ColorScope::qmlAttachedProperties(QObject *object) | 66 | ColorScope *ColorScope::qmlAttachedProperties(QObject *object) | ||
64 | { | 67 | { | ||
65 | const auto cs = s_attachedScopes.value(object); | 68 | const auto cs = s_attachedScopes.value(object); | ||
66 | if (cs) { | 69 | if (cs) { | ||
67 | return cs; | 70 | return cs; | ||
68 | } | 71 | } | ||
69 | 72 | | |||
70 | ColorScope *s = new ColorScope(nullptr, object); | 73 | ColorScope *s = new ColorScope(nullptr, object); | ||
71 | s_attachedScopes[object] = s; | 74 | s_attachedScopes[object] = s; | ||
72 | s->m_inherit = true; | 75 | s->m_inherit = true; | ||
73 | s->setParent(object); | 76 | s->setParent(object); | ||
74 | s->checkColorGroupChanged(); | 77 | s->checkColorGroupChanged(); | ||
75 | 78 | | |||
76 | return s; | 79 | return s; | ||
77 | } | 80 | } | ||
78 | 81 | | |||
79 | bool ColorScope::eventFilter(QObject* watched, QEvent* event) | | |||
80 | { | | |||
81 | Q_ASSERT(watched == m_parent && !qobject_cast<QQuickItem *>(watched)); | | |||
82 | if (event->type() == QEvent::ParentChange) { | | |||
83 | checkColorGroupChanged(); | | |||
84 | } | | |||
85 | return QQuickItem::eventFilter(watched, event); | | |||
86 | } | | |||
87 | | ||||
88 | void ColorScope::setParentScope(ColorScope* parentScope) | 82 | void ColorScope::setParentScope(ColorScope* parentScope) | ||
89 | { | 83 | { | ||
90 | if (parentScope == m_parentScope) | 84 | if (parentScope == m_parentScope) | ||
91 | return; | 85 | return; | ||
92 | 86 | | |||
93 | if (m_parentScope) { | 87 | if (m_parentScope) { | ||
94 | disconnect(m_parentScope.data(), &ColorScope::colorGroupChanged, | 88 | disconnect(m_parentScope.data(), &ColorScope::colorGroupChanged, | ||
95 | this, &ColorScope::checkColorGroupChanged); | 89 | this, &ColorScope::checkColorGroupChanged); | ||
96 | } | 90 | } | ||
97 | 91 | | |||
98 | m_parentScope = parentScope; | 92 | m_parentScope = parentScope; | ||
99 | 93 | | |||
100 | if (parentScope) { | 94 | if (parentScope) { | ||
101 | connect(parentScope, &ColorScope::colorGroupChanged, | 95 | connect(parentScope, &ColorScope::colorGroupChanged, | ||
102 | this, &ColorScope::checkColorGroupChanged); | 96 | this, &ColorScope::checkColorGroupChanged); | ||
103 | } | 97 | } | ||
104 | } | 98 | } | ||
105 | 99 | | |||
106 | ColorScope *ColorScope::findParentScope() | 100 | ColorScope *ColorScope::findParentScope() | ||
107 | { | 101 | { | ||
108 | QObject *p = nullptr; | 102 | QQuickItem *candidate = qobject_cast<QQuickItem *>(parentItem()); | ||
109 | if (m_parent) { | 103 | if (!candidate) { | ||
110 | QQuickItem *gp = qobject_cast<QQuickItem *>(m_parent); | 104 | candidate = qobject_cast<QQuickItem *>(parent()); | ||
111 | if (gp) { | 105 | } | ||
112 | p = gp->parentItem(); | 106 | while (candidate) { | ||
113 | } else { | 107 | candidate = candidate->parentItem(); | ||
114 | p = m_parent->parent(); | 108 | ColorScope *s = qobject_cast<ColorScope *>(candidate); | ||
115 | } | 109 | if (!s) { | ||
110 | // Make sure AppletInterface always has a ColorScope | ||||
davidedmundson: typo | |||||
111 | s = static_cast<ColorScope *>(qmlAttachedPropertiesObject<ColorScope>(candidate, qobject_cast<PlasmaQuick::AppletQuickItem *>(candidate))); | ||||
116 | } | 112 | } | ||
117 | 113 | | |||
118 | if (!p || !m_parent) { | 114 | if (s) { | ||
119 | setParentScope(nullptr); | 115 | setParentScope(s); | ||
120 | return nullptr; | 116 | return s; | ||
121 | } | 117 | } | ||
122 | | ||||
123 | ColorScope *c = qobject_cast<ColorScope *>(p); | | |||
124 | if (!c) { | | |||
125 | c = qmlAttachedProperties(p); | | |||
126 | } | 118 | } | ||
127 | 119 | return nullptr; | |||
128 | setParentScope(c); | | |||
129 | | ||||
130 | return m_parentScope; | | |||
131 | } | 120 | } | ||
132 | 121 | | |||
133 | void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group) | 122 | void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group) | ||
134 | { | 123 | { | ||
135 | if (m_group == group) { | 124 | if (m_group == group) { | ||
136 | return; | 125 | return; | ||
137 | } | 126 | } | ||
138 | 127 | | |||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |
It' be better to split this colour change stuff.
It's effectively an unrelated bugfix/cleanup