Changeset View
Changeset View
Standalone View
Standalone View
src/declarativeimports/core/colorscope.cpp
Show All 22 Lines | |||||
23 | 23 | | |||
24 | #include <QQmlContext> | 24 | #include <QQmlContext> | ||
25 | #include <QQmlEngine> | 25 | #include <QQmlEngine> | ||
26 | #include <QColor> | 26 | #include <QColor> | ||
27 | #include <QQuickWindow> | 27 | #include <QQuickWindow> | ||
28 | 28 | | |||
29 | #include <PlasmaQuick/AppletQuickItem> | 29 | #include <PlasmaQuick/AppletQuickItem> | ||
30 | 30 | | |||
31 | QHash<QObject *, ColorScope *> ColorScope::s_attachedScopes = QHash<QObject *, ColorScope *>(); | 31 | QHash<QObject *, ColorScopeAttached *> ColorScopeAttached::s_attachedScopes = QHash<QObject *, ColorScopeAttached *>(); | ||
32 | 32 | | |||
33 | QWeakPointer<Plasma::Theme> ColorScope::s_theme; | 33 | QWeakPointer<Plasma::Theme> ColorScopeAttached::s_theme; | ||
34 | 34 | | |||
35 | ColorScope::ColorScope(QQuickItem *parent, QObject *parentObject) | 35 | ColorScopeAttached::ColorScopeAttached(QObject *parent) | ||
36 | : QQuickItem(parent), | 36 | : QObject(parent), | ||
37 | m_inherit(false), | 37 | m_inherit(false), | ||
38 | m_group(Plasma::Theme::NormalColorGroup), | 38 | m_group(Plasma::Theme::NormalColorGroup), | ||
39 | m_parent(parentObject), | 39 | m_parent(parent), | ||
40 | m_actualGroup(Plasma::Theme::NormalColorGroup) | 40 | m_actualGroup(Plasma::Theme::NormalColorGroup) | ||
41 | { | 41 | { | ||
42 | m_theme = s_theme.toStrongRef(); | 42 | m_theme = s_theme.toStrongRef(); | ||
43 | if (!m_theme) { | 43 | if (!m_theme) { | ||
44 | QSharedPointer<Plasma::Theme> themePtr(new Plasma::Theme); | 44 | QSharedPointer<Plasma::Theme> themePtr(new Plasma::Theme); | ||
45 | s_theme = themePtr; | 45 | s_theme = themePtr; | ||
46 | m_theme = s_theme.toStrongRef(); | 46 | m_theme = s_theme.toStrongRef(); | ||
47 | } | 47 | } | ||
48 | 48 | | |||
49 | connect(m_theme.data(), &Plasma::Theme::themeChanged, this, &ColorScope::colorsChanged); | 49 | connect(m_theme.data(), &Plasma::Theme::themeChanged, this, &ColorScopeAttached::colorsChanged); | ||
apol: let's make this a proper method? it's used twice here and a normal method should be quicker to… | |||||
50 | 50 | | |||
51 | connect(this, &ColorScope::colorGroupChanged, this, &ColorScope::colorsChanged); | 51 | connect(this, &ColorScopeAttached::colorGroupChanged, this, &ColorScopeAttached::colorsChanged); | ||
52 | 52 | | |||
53 | if (parentObject && qobject_cast<QQuickItem *>(parentObject)) { | 53 | QQuickItem *parentItem = qobject_cast<QQuickItem *>(m_parent); | ||
54 | connect(static_cast<QQuickItem *>(parentObject), &QQuickItem::windowChanged, | | |||
55 | this, [this]() { | | |||
56 | findParentScope(); | | |||
57 | checkColorGroupChanged(); | | |||
58 | }); | | |||
59 | 54 | | |||
60 | connect(static_cast<QQuickItem *>(parentObject), &QQuickItem::parentChanged, | 55 | auto scopeChange = [this] () { | ||
61 | this, [this]() { | | |||
62 | findParentScope(); | 56 | findParentScope(); | ||
63 | checkColorGroupChanged(); | 57 | checkColorGroupChanged(); | ||
64 | }); | 58 | }; | ||
65 | } else if (parent) { | 59 | | ||
66 | connect(parent, &QQuickItem::parentChanged, | 60 | if (parentItem) { | ||
67 | this, &ColorScope::checkColorGroupChanged); | 61 | connect(parentItem, &QQuickItem::windowChanged, | ||
62 | this, scopeChange); | ||||
63 | | ||||
64 | connect(parentItem, &QQuickItem::parentChanged, | ||||
65 | this, scopeChange); | ||||
68 | } | 66 | } | ||
67 | findParentScope(); | ||||
69 | } | 68 | } | ||
70 | 69 | | |||
71 | ColorScope::~ColorScope() | 70 | ColorScopeAttached::~ColorScopeAttached() | ||
72 | { | 71 | { | ||
73 | m_deleting = true; | 72 | m_deleting = true; | ||
74 | s_attachedScopes.remove(m_parent); | 73 | s_attachedScopes.remove(m_parent); | ||
75 | } | 74 | } | ||
76 | 75 | | |||
77 | ColorScope *ColorScope::qmlAttachedProperties(QObject *object) | 76 | void ColorScopeAttached::setParentScope(ColorScopeAttached* parentScope) | ||
78 | { | 77 | { | ||
79 | const auto cs = s_attachedScopes.value(object); | 78 | if (parentScope == m_parentScope) | ||
80 | if (cs) { | 79 | return; | ||
81 | return cs; | 80 | | ||
81 | if (m_parentScope) { | ||||
82 | disconnect(m_parentScope.data(), &ColorScopeAttached::colorGroupChanged, | ||||
83 | this, &ColorScopeAttached::checkColorGroupChanged); | ||||
82 | } | 84 | } | ||
83 | 85 | | |||
84 | ColorScope *s = new ColorScope(nullptr, object); | 86 | m_parentScope = parentScope; | ||
85 | s_attachedScopes[object] = s; | | |||
86 | s->m_inherit = true; | | |||
87 | s->setParent(object); | | |||
88 | s->checkColorGroupChanged(); | | |||
89 | 87 | | |||
90 | return s; | 88 | if (parentScope) { | ||
89 | connect(parentScope, &ColorScopeAttached::colorGroupChanged, | ||||
90 | this, &ColorScopeAttached::checkColorGroupChanged); | ||||
91 | } | ||||
91 | } | 92 | } | ||
92 | 93 | | |||
93 | void ColorScope::setParentScope(ColorScope* parentScope) | 94 | void ColorScopeAttached::setColorGroup(Plasma::Theme::ColorGroup group) | ||
94 | { | 95 | { | ||
95 | if (parentScope == m_parentScope) | 96 | if (m_group == group) { | ||
96 | return; | 97 | return; | ||
98 | } | ||||
97 | 99 | | |||
98 | if (m_parentScope) { | 100 | m_group = group; | ||
99 | disconnect(m_parentScope.data(), &ColorScope::colorGroupChanged, | 101 | | ||
100 | this, &ColorScope::checkColorGroupChanged); | 102 | checkColorGroupChanged(); | ||
101 | } | 103 | } | ||
102 | 104 | | |||
103 | m_parentScope = parentScope; | 105 | Plasma::Theme::ColorGroup ColorScopeAttached::colorGroup() const | ||
106 | { | ||||
107 | return m_actualGroup; | ||||
108 | } | ||||
104 | 109 | | |||
105 | if (parentScope) { | 110 | QColor ColorScopeAttached::textColor() const | ||
106 | connect(parentScope, &ColorScope::colorGroupChanged, | 111 | { | ||
107 | this, &ColorScope::checkColorGroupChanged); | 112 | return m_theme->color(Plasma::Theme::TextColor, colorGroup()); | ||
113 | } | ||||
114 | | ||||
115 | QColor ColorScopeAttached::highlightColor() const | ||||
116 | { | ||||
117 | return m_theme->color(Plasma::Theme::HighlightColor, colorGroup()); | ||||
118 | } | ||||
119 | | ||||
120 | QColor ColorScopeAttached::highlightedTextColor() const | ||||
121 | { | ||||
122 | return m_theme->color(Plasma::Theme::HighlightedTextColor, colorGroup()); | ||||
123 | } | ||||
124 | | ||||
125 | QColor ColorScopeAttached::backgroundColor() const | ||||
126 | { | ||||
127 | return m_theme->color(Plasma::Theme::BackgroundColor, colorGroup()); | ||||
128 | } | ||||
129 | | ||||
130 | QColor ColorScopeAttached::positiveTextColor() const | ||||
131 | { | ||||
132 | return m_theme->color(Plasma::Theme::PositiveTextColor, colorGroup()); | ||||
133 | } | ||||
134 | | ||||
135 | QColor ColorScopeAttached::neutralTextColor() const | ||||
136 | { | ||||
137 | return m_theme->color(Plasma::Theme::NeutralTextColor, colorGroup()); | ||||
138 | } | ||||
139 | | ||||
140 | QColor ColorScopeAttached::negativeTextColor() const | ||||
141 | { | ||||
142 | return m_theme->color(Plasma::Theme::NegativeTextColor, colorGroup()); | ||||
143 | } | ||||
144 | | ||||
145 | QColor ColorScopeAttached::disabledTextColor() const | ||||
146 | { | ||||
147 | return m_theme->color(Plasma::Theme::DisabledTextColor, colorGroup()); | ||||
148 | } | ||||
149 | | ||||
150 | bool ColorScopeAttached::inherit() const | ||||
151 | { | ||||
152 | return m_inherit; | ||||
153 | } | ||||
154 | | ||||
155 | void ColorScopeAttached::setInherit(bool inherit) | ||||
156 | { | ||||
157 | if (m_inherit == inherit) { | ||||
158 | return; | ||||
108 | } | 159 | } | ||
160 | m_inherit = inherit; | ||||
161 | emit inheritChanged(); | ||||
162 | checkColorGroupChanged(); | ||||
109 | } | 163 | } | ||
110 | 164 | | |||
111 | ColorScope *ColorScope::findParentScope() | 165 | void ColorScopeAttached::checkColorGroupChanged() | ||
112 | { | 166 | { | ||
113 | QObject *candidate = parentItem(); | 167 | const auto last = m_actualGroup; | ||
114 | if (!candidate) { | 168 | if (m_inherit) { | ||
115 | candidate = parent(); | 169 | findParentScope(); | ||
170 | m_actualGroup = m_parentScope ? m_parentScope->colorGroup() : m_group; | ||||
171 | } else { | ||||
172 | m_actualGroup = m_group; | ||||
173 | } | ||||
174 | | ||||
175 | if (m_actualGroup != last) { | ||||
176 | Q_EMIT colorGroupChanged(); | ||||
177 | } | ||||
116 | } | 178 | } | ||
117 | 179 | | |||
180 | ColorScopeAttached *ColorScopeAttached::findParentScope() | ||||
181 | { | ||||
182 | QObject *candidate = parent(); | ||||
183 | | ||||
118 | while (candidate) { | 184 | while (candidate) { | ||
119 | auto *quickCandidate = qobject_cast<QQuickItem *>(candidate); | 185 | auto *quickCandidate = qobject_cast<QQuickItem *>(candidate); | ||
120 | if (quickCandidate && quickCandidate->parentItem()) { | 186 | if (quickCandidate && quickCandidate->parentItem()) { | ||
121 | candidate = quickCandidate->parentItem(); | 187 | candidate = quickCandidate->parentItem(); | ||
122 | } else { | 188 | } else { | ||
123 | candidate = candidate->parent(); | 189 | candidate = candidate->parent(); | ||
124 | } | 190 | } | ||
125 | 191 | | |||
126 | ColorScope *s = qobject_cast<ColorScope *>(candidate); | 192 | // Make sure AppletInterface always has a ColorScopeAttached | ||
127 | if (!s) { | 193 | ColorScopeAttached *s = static_cast<ColorScopeAttached *>(qmlAttachedPropertiesObject<ColorScope>(candidate, qobject_cast<PlasmaQuick::AppletQuickItem *>(candidate))); | ||
128 | // Make sure AppletInterface always has a ColorScope | 194 | | ||
129 | s = static_cast<ColorScope *>(qmlAttachedPropertiesObject<ColorScope>(candidate, qobject_cast<PlasmaQuick::AppletQuickItem *>(candidate))); | | |||
130 | } | | |||
131 | if (s && !s->m_deleting) { | 195 | if (s && !s->m_deleting) { | ||
132 | setParentScope(s); | 196 | setParentScope(s); | ||
133 | return s; | 197 | return s; | ||
134 | } | 198 | } | ||
135 | } | 199 | } | ||
136 | 200 | | |||
137 | return nullptr; | 201 | return nullptr; | ||
138 | } | 202 | } | ||
139 | 203 | | |||
140 | void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group) | 204 | | ||
205 | ///////////////////////////////////////////////////// | ||||
206 | | ||||
207 | | ||||
208 | ColorScope::ColorScope(QQuickItem *parent) | ||||
209 | : QQuickItem(parent) | ||||
141 | { | 210 | { | ||
142 | if (m_group == group) { | 211 | m_ownAttached = qobject_cast<ColorScopeAttached *>(qmlAttachedPropertiesObject<ColorScope>(this, true)); | ||
143 | return; | 212 | | ||
213 | connect(m_ownAttached, &ColorScopeAttached::colorGroupChanged, | ||||
214 | this, &ColorScope::colorGroupChanged); | ||||
215 | connect(m_ownAttached, &ColorScopeAttached::colorsChanged, | ||||
216 | this, &ColorScope::colorsChanged); | ||||
217 | connect(m_ownAttached, &ColorScopeAttached::inheritChanged, | ||||
218 | this, &ColorScope::inheritChanged); | ||||
144 | } | 219 | } | ||
145 | 220 | | |||
146 | m_group = group; | 221 | ColorScope::~ColorScope() | ||
222 | {} | ||||
147 | 223 | | |||
148 | checkColorGroupChanged(); | 224 | void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group) | ||
225 | { | ||||
226 | m_ownAttached->setColorGroup(group); | ||||
149 | } | 227 | } | ||
150 | 228 | | |||
151 | Plasma::Theme::ColorGroup ColorScope::colorGroup() const | 229 | Plasma::Theme::ColorGroup ColorScope::colorGroup() const | ||
152 | { | 230 | { | ||
153 | return m_actualGroup; | 231 | return m_ownAttached->colorGroup(); | ||
154 | } | 232 | } | ||
155 | 233 | | |||
156 | QColor ColorScope::textColor() const | 234 | QColor ColorScope::textColor() const | ||
157 | { | 235 | { | ||
158 | return m_theme->color(Plasma::Theme::TextColor, colorGroup()); | 236 | return m_ownAttached->textColor(); | ||
159 | } | 237 | } | ||
160 | 238 | | |||
161 | QColor ColorScope::highlightColor() const | 239 | QColor ColorScope::highlightColor() const | ||
162 | { | 240 | { | ||
163 | return m_theme->color(Plasma::Theme::HighlightColor, colorGroup()); | 241 | return m_ownAttached->highlightColor(); | ||
164 | } | 242 | } | ||
165 | 243 | | |||
166 | QColor ColorScope::highlightedTextColor() const | 244 | QColor ColorScope::highlightedTextColor() const | ||
167 | { | 245 | { | ||
168 | return m_theme->color(Plasma::Theme::HighlightedTextColor, colorGroup()); | 246 | return m_ownAttached->highlightedTextColor(); | ||
169 | } | 247 | } | ||
170 | 248 | | |||
171 | QColor ColorScope::backgroundColor() const | 249 | QColor ColorScope::backgroundColor() const | ||
172 | { | 250 | { | ||
173 | return m_theme->color(Plasma::Theme::BackgroundColor, colorGroup()); | 251 | return m_ownAttached->backgroundColor(); | ||
174 | } | 252 | } | ||
175 | 253 | | |||
176 | QColor ColorScope::positiveTextColor() const | 254 | QColor ColorScope::positiveTextColor() const | ||
177 | { | 255 | { | ||
178 | return m_theme->color(Plasma::Theme::PositiveTextColor, colorGroup()); | 256 | return m_ownAttached->positiveTextColor(); | ||
179 | } | 257 | } | ||
180 | 258 | | |||
181 | QColor ColorScope::neutralTextColor() const | 259 | QColor ColorScope::neutralTextColor() const | ||
182 | { | 260 | { | ||
183 | return m_theme->color(Plasma::Theme::NeutralTextColor, colorGroup()); | 261 | return m_ownAttached->neutralTextColor(); | ||
184 | } | 262 | } | ||
185 | 263 | | |||
186 | QColor ColorScope::negativeTextColor() const | 264 | QColor ColorScope::negativeTextColor() const | ||
187 | { | 265 | { | ||
188 | return m_theme->color(Plasma::Theme::NegativeTextColor, colorGroup()); | 266 | return m_ownAttached->negativeTextColor(); | ||
189 | } | 267 | } | ||
190 | 268 | | |||
191 | QColor ColorScope::disabledTextColor() const | 269 | QColor ColorScope::disabledTextColor() const | ||
192 | { | 270 | { | ||
193 | return m_theme->color(Plasma::Theme::DisabledTextColor, colorGroup()); | 271 | return m_ownAttached->disabledTextColor(); | ||
194 | } | 272 | } | ||
195 | 273 | | |||
196 | bool ColorScope::inherit() const | 274 | bool ColorScope::inherit() const | ||
197 | { | 275 | { | ||
198 | return m_inherit; | 276 | return m_ownAttached->inherit(); | ||
199 | } | 277 | } | ||
200 | 278 | | |||
201 | void ColorScope::setInherit(bool inherit) | 279 | void ColorScope::setInherit(bool inherit) | ||
202 | { | 280 | { | ||
203 | if (m_inherit == inherit) { | 281 | m_ownAttached->setInherit(inherit); | ||
204 | return; | | |||
205 | } | | |||
206 | m_inherit = inherit; | | |||
207 | emit inheritChanged(); | | |||
208 | checkColorGroupChanged(); | | |||
209 | } | 282 | } | ||
210 | 283 | | |||
211 | void ColorScope::itemChange(ItemChange change, const ItemChangeData &value) | 284 | void ColorScope::itemChange(ItemChange change, const ItemChangeData &value) | ||
212 | { | 285 | { | ||
213 | if (change == QQuickItem::ItemSceneChange) { | 286 | if (change == QQuickItem::ItemSceneChange) { | ||
214 | //we have a window: create the representations if needed | 287 | //we have a window: create the representations if needed | ||
215 | if (value.window) { | 288 | if (value.window) { | ||
216 | findParentScope(); | 289 | m_ownAttached->findParentScope(); | ||
217 | checkColorGroupChanged(); | 290 | m_ownAttached->checkColorGroupChanged(); | ||
218 | } | 291 | } | ||
219 | } | 292 | } | ||
220 | 293 | | |||
221 | QQuickItem::itemChange(change, value); | 294 | QQuickItem::itemChange(change, value); | ||
222 | } | 295 | } | ||
223 | 296 | | |||
224 | void ColorScope::checkColorGroupChanged() | 297 | ColorScopeAttached *ColorScope::qmlAttachedProperties(QObject *object) | ||
225 | { | 298 | { | ||
226 | const auto last = m_actualGroup; | 299 | const auto cs = ColorScopeAttached::s_attachedScopes.value(object); | ||
227 | if (m_inherit) { | 300 | if (cs) { | ||
228 | findParentScope(); | 301 | return cs; | ||
229 | m_actualGroup = m_parentScope ? m_parentScope->colorGroup() : m_group; | | |||
230 | } else { | | |||
231 | m_actualGroup = m_group; | | |||
232 | } | 302 | } | ||
233 | 303 | | |||
234 | if (m_actualGroup != last) { | 304 | ColorScopeAttached *s = new ColorScopeAttached(object); | ||
235 | Q_EMIT colorGroupChanged(); | 305 | ColorScopeAttached::s_attachedScopes[object] = s; | ||
236 | } | 306 | s->m_inherit = true; | ||
307 | s->setParent(object); | ||||
308 | s->checkColorGroupChanged(); | ||||
309 | | ||||
310 | return s; | ||||
237 | } | 311 | } | ||
238 | 312 | | |||
239 | #include "moc_colorscope.cpp" | 313 | #include "moc_colorscope.cpp" |
let's make this a proper method? it's used twice here and a normal method should be quicker to use and easier on the reader.