Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqstylekitpropertyresolver.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
11
12#include <QtQuickTemplates2/private/qquickcontrol_p.h>
13#include <QtCore/QScopedValueRollback>
14
16
17bool QQStyleKitPropertyResolver::s_styleWarningsIssued = false;
18bool QQStyleKitPropertyResolver::s_isReadingProperty = false;
19QQSK::State QQStyleKitPropertyResolver::s_cachedState = QQSK::StateFlag::Unspecified;
20QVarLengthArray<QQSK::StateFlag, 10> QQStyleKitPropertyResolver::s_cachedStateList;
21
22const QList<QQStyleKitExtendableControlType> QQStyleKitPropertyResolver::baseTypesForType(
23 QQStyleKitExtendableControlType exactType)
24{
25 /* By default, the base types should mirror the class hierarchy in Qt Quick Controls.
26 * However, to make it possible to style a base type without having to "undo" it again
27 * in a sub type, we choose to diverge in som cases:
28 *
29 * ItemDelegate — Normally used as a menu item in a ComboBox or as an item in a ListView.
30 * Although it behaves similarly to a button, it is typically styled very differently
31 * (e.g., without borders, drop shadows, gradients, etc.). For that reason, it falls
32 * back to Control rather than AbstractButton.
33 *
34 * MenuBarItem — Although it is technically a button, it is typically styled very differently
35 * from other buttons (e.g., without borders, drop shadows, gradients, etc.). For that
36 * reason, it falls back to Control rather than AbstractButton.
37 *
38 * MenuItem — In Qt Quick Controls, MenuItem inherits from AbstractButton. Although it
39 * behaves similarly to a button, it is typically styled very differently
40 * (e.g., without borders, drop shadows, gradients, etc.). For that reason, it falls
41 * back to Control rather than AbstractButton.
42 */
43 switch (exactType) {
44 case QQStyleKitReader::ApplicationWindow: {
45 static QList<QQStyleKitExtendableControlType> t =
46 { QQStyleKitReader::ApplicationWindow };
47 return t; }
48 case QQStyleKitReader::Button:
49 case QQStyleKitReader::DelayButton:
50 case QQStyleKitReader::FlatButton:
51 case QQStyleKitReader::ToolButton:
52 case QQStyleKitReader::TabButton:
53 case QQStyleKitReader::RadioButton:
54 case QQStyleKitReader::RoundButton:
55 case QQStyleKitReader::CheckBox:
56 case QQStyleKitReader::SwitchControl: {
57 static QList<QQStyleKitExtendableControlType> t =
58 { QQStyleKitReader::AbstractButton, QQStyleKitReader::Control };
59 return t; }
60 case QQStyleKitReader::CheckDelegate:
61 case QQStyleKitReader::RadioDelegate:
62 case QQStyleKitReader::SwipeDelegate:
63 case QQStyleKitReader::SwitchDelegate:
64 case QQStyleKitReader::TableViewDelegate:
65 case QQStyleKitReader::TreeViewDelegate: {
66 static QList<QQStyleKitExtendableControlType> t =
67 { QQStyleKitReader::ItemDelegate, QQStyleKitReader::Control };
68 return t; }
69 case QQStyleKitReader::HeaderViewDelegate: {
70 static QList<QQStyleKitExtendableControlType> t =
71 { QQStyleKitReader::TableViewDelegate, QQStyleKitReader::ItemDelegate, QQStyleKitReader::Control };
72 return t;
73 }
74 case QQStyleKitReader::Dialog:
75 case QQStyleKitReader::Drawer:
76 case QQStyleKitReader::Menu:
77 case QQStyleKitReader::ToolTip: {
78 static QList<QQStyleKitExtendableControlType> t =
79 { QQStyleKitReader::Popup, QQStyleKitReader::Control };
80 return t; }
81 case QQStyleKitReader::Page:
82 case QQStyleKitReader::Frame:
83 case QQStyleKitReader::ToolBar: {
84 static QList<QQStyleKitExtendableControlType> t =
85 { QQStyleKitReader::Pane, QQStyleKitReader::Control };
86 return t; }
87 case QQStyleKitReader::DialogButtonBox:
88 case QQStyleKitReader::MenuBar:
89 case QQStyleKitReader::TabBar: {
90 static QList<QQStyleKitExtendableControlType> t =
91 { QQStyleKitReader::Container, QQStyleKitReader::Control };
92 return t; }
93 case QQStyleKitReader::GroupBox: {
94 static QList<QQStyleKitExtendableControlType> t =
95 { QQStyleKitReader::Frame, QQStyleKitReader::Pane, QQStyleKitReader::Control };
96 return t;
97 }
98 case QQStyleKitReader::SplitView: {
99 static QList<QQStyleKitExtendableControlType> t =
100 { QQStyleKitReader::Container, QQStyleKitReader::Control };
101 return t; }
102 case QQStyleKitReader::TextField:
103 case QQStyleKitReader::TextArea: {
104 static QList<QQStyleKitExtendableControlType> t =
105 { QQStyleKitReader::TextInput, QQStyleKitReader::Control };
106 return t; }
107 default: {
108 static QList<QQStyleKitExtendableControlType> t =
109 { QQStyleKitReader::Control };
110 return t; }
111 }
112
113 Q_UNREACHABLE();
114 return {};
115}
116
117void QQStyleKitPropertyResolver::cacheReaderState(QQSK::State state)
118{
119 Q_ASSERT(state != QQSK::StateFlag::Unspecified);
120 if (state == s_cachedState)
121 return;
122
123 s_cachedState = state;
124
125 /* Note: The order in which we add the states below matters.
126 * The reason is that the s_cachedStateList that we build is used by QQStyleKitPropertyResolver
127 * later to generate all the different state combinations that should be tested when
128 * searching for a property. And the states added first to the list will "win" if the
129 * same property is set in several of the states. */
130 s_cachedStateList.clear();
131 if (state.testFlag(QQSK::StateFlag::Pressed))
132 s_cachedStateList.append(QQSK::StateFlag::Pressed);
133 if (state.testFlag(QQSK::StateFlag::Hovered))
134 s_cachedStateList.append(QQSK::StateFlag::Hovered);
135 if (state.testFlag(QQSK::StateFlag::Highlighted))
136 s_cachedStateList.append(QQSK::StateFlag::Highlighted);
137 if (state.testFlag(QQSK::StateFlag::Focused))
138 s_cachedStateList.append(QQSK::StateFlag::Focused);
139 if (state.testFlag(QQSK::StateFlag::Checked))
140 s_cachedStateList.append(QQSK::StateFlag::Checked);
141 if (state.testFlag(QQSK::StateFlag::Vertical))
142 s_cachedStateList.append(QQSK::StateFlag::Vertical);
143 if (state.testFlag(QQSK::StateFlag::Disabled))
144 s_cachedStateList.append(QQSK::StateFlag::Disabled);
145}
146
147void QQStyleKitPropertyResolver::addVariationToReader(
148 QQStyleKitReader *styleReader,
149 QQStyleKitStyleAndThemeBase *styleOrTheme,
150 QQStyleKitVariation *variation)
151{
152 /* Add the variation to the StyleReader's list of effective variations.
153 * A variation may appear more than once in a type variation list or in
154 * an instance variation list, but it only needs to be added once - the
155 * first in the list will shadow the other ones anyway. */
156 if (!styleReader->m_effectiveVariations.contains(variation))
157 styleReader->m_effectiveVariations.append(variation);
158
159 /* We also record in which Style or Theme the variation was found, so that
160 * it only takes effect for that specific Style or Theme during property
161 * propagation. Note that the same type variation can be used from both the
162 * Style and the Theme (its ID can be added to several variation lists). */
163 if (!variation->m_usageContext.contains(styleOrTheme))
164 variation->m_usageContext.append(styleOrTheme);
165}
166
167void QQStyleKitPropertyResolver::addTypeVariationsToReader(
168 QQStyleKitReader *styleReader,
169 QQStyleKitStyleAndThemeBase *styleOrTheme,
170 const AttachedVariationList &attachedVariations)
171{
172 const QQStyleKitExtendableControlType styleReaderType = styleReader->controlType();
173 const auto styleReaderBaseType = baseTypesForType(styleReaderType);
174
175 static PropertyPathIds ids;
176 if (ids.property.property() == QQSK::Property::NoProperty) {
177 /* ids is made static, since the 'variations' path will be the same for all
178 * StyleKitControls. Also, since subtypes are only possible for delegates,
179 * and 'variations' is a control property, we can exclude subtypes. */
180 ids.property = QQStyleKitPropertyPath(styleReader, QQSK::Property::Variations, QQStyleKitPropertyPath::Flag::ExcludeSubtype);
181 ids.alternative = QQStyleKitPropertyPath(styleReader, QQSK::Property::NoProperty, QQStyleKitPropertyPath::Flag::ExcludeSubtype);
182 ids.subTypeProperty = QQStyleKitPropertyPath();
183 ids.subTypeAlternative = QQStyleKitPropertyPath();
184 }
185
186 for (const QQStyleKitVariationAttached *attached : attachedVariations) {
187 const auto parentType = attached->controlType();
188 const auto parentBaseTypes = baseTypesForType(parentType);
189
190 /* Search for the 'variations' property set on the parentType, or any of its base
191 * types, using normal propagation and fallback logic. Unlike when resolving other style
192 * properties, we limit the search to the style or theme we're processing, since it matters which
193 * style or theme a variation belongs when they're used to resolve other style properties later. */
194 const QVariant typeVariationsVariant = readPropertyInRelevantControls(styleOrTheme, ids, parentType, parentBaseTypes);
195 if (!typeVariationsVariant.isValid())
196 continue;
197
198 const auto typeVariations = *qvariant_cast<QList<QQStyleKitVariation *> *>(typeVariationsVariant);
199
200 for (QQStyleKitVariation *variation : typeVariations) {
201 /* Inside each type variation, check if the control type that styleReader represents has
202 * been defined. If so, it means that the variation _might_ affect it, and should therefore
203 * be added to the style readers list of effective variations. */
204 if (!variation) {
205 /* The variation will be nullptr if non-QQStyleKitVariation elements
206 * are added to the 'variations' list from QML (such as strings). */
207 continue;
208 }
209
210 if (variation->getControlStyle(styleReaderType)) {
211 addVariationToReader(styleReader, styleOrTheme, variation);
212 } else {
213 for (int type : styleReaderBaseType) {
214 if (variation->getControlStyle(type))
215 addVariationToReader(styleReader, styleOrTheme, variation);
216 }
217 }
218 }
219 }
220}
221
222void QQStyleKitPropertyResolver::addInstanceVariationsToReader(
223 QQStyleKitReader *styleReader,
224 QQStyleKitStyleAndThemeBase *styleOrTheme,
225 const AttachedVariationList &attachedVariations)
226{
227 /* Add the variations set from the application to the list of effective variations
228 * in the styleReader. But, to speed up property look-up later on, we only add the
229 * variations that has the potential to affect the control type, or its base types,
230 * that the styleReader represents. The variations that are closest to styleReader
231 * in the hierarchy will be added first and take precendence over the ones added last. */
232 const QQStyleKitExtendableControlType styleReaderType = styleReader->controlType();
233 const auto styleReaderBaseTypes = baseTypesForType(styleReaderType);
234
235 for (const QQStyleKitVariationAttached *attached : attachedVariations) {
236 for (const QString &instanceVariationName : attached->variations()) {
237 for (QQStyleKitVariation *variation : styleOrTheme->m_styleVariations) {
238 if (variation->name() != instanceVariationName)
239 continue;
240
241 /* Invariant: we found a variation in the given Style or a Theme with a name that matches
242 * a name in the attached variation list. Check if the found variation contains the
243 * type, or the subtypes, of the style reader. If not, it doesn't affect it and can
244 * therefore be skipped. */
245 if (variation->getControlStyle(styleReaderType)) {
246 addVariationToReader(styleReader, styleOrTheme, variation);
247 } else {
248 for (int baseType : styleReaderBaseTypes) {
249 if (variation->getControlStyle(baseType))
250 addVariationToReader(styleReader, styleOrTheme, variation);
251 }
252 }
253 }
254 }
255 }
256}
257
258void QQStyleKitPropertyResolver::rebuildVariationsForReader(
259 QQStyleKitReader *styleReader, QQStyleKitStyle *style)
260{
261 /* Traverse up the parent chain of \a styleReader, and for each parent, look for an
262 * instance of QQStyleKitVariationAttached. And for each attached object, check if it
263 * has variations that can potentially affect the style reader. If so, add the
264 * variations to the style readers list of effective variations.
265 * A QQStyleKitVariationAttached can specify both Instance Variations and Type Variations.
266 * The former should affect all descendant StyleKitReaders of the parent, while the
267 * latter should only affect descendant StyleKitReaders of a specific type. */
268 Q_ASSERT(styleReader->m_effectiveVariationsDirty);
269 styleReader->m_effectiveVariationsDirty = false;
270 styleReader->m_effectiveVariations.clear();
271
272 if (!style->m_hasVariations)
273 return;
274
275 /* Walk up the parent chain and collect all attached StyleVariation objects that
276 * may affect this StyleReader. Their variation lists affect instance variations,
277 * and their control type may affect type variations. */
278 AttachedVariationList attachedVariations;
279 for (QObject *current = styleReader; current; current = current->parent()) {
280 if (const QObject *attachedObject = qmlAttachedPropertiesObject<QQStyleKitVariation>(current, false)) {
281 const auto *attached = static_cast<const QQStyleKitVariationAttached *>(attachedObject);
282 attachedVariations.append(attached);
283 }
284 }
285
286 if (attachedVariations.isEmpty())
287 return;
288
289 for (QQStyleKitStyle *current = style; current; current = current->fallbackStyle()) {
290 if (QQStyleKitTheme *theme = current->theme()) {
291 addInstanceVariationsToReader(styleReader, theme, attachedVariations);
292 addTypeVariationsToReader(styleReader, theme, attachedVariations);
293 }
294 addInstanceVariationsToReader(styleReader, current, attachedVariations);
295 addTypeVariationsToReader(styleReader, current, attachedVariations);
296 }
297}
298
299template <class T>
300QVariant QQStyleKitPropertyResolver::readPropertyInStorageForState(
301 const QQStyleKitPropertyPath main, const QQStyleKitPropertyPath alternative,
302 const T *storageProvider, QQSK::State state)
303{
304 /* If either the main property or its alternative is set in the storage,
305 * we’ve found the best match and can return the value to the application.
306 * The reason we support an alternative property is that some properties can be
307 * specified in more than one way. For example, 'topLeftRadius' can be set either
308 * directly ('topLeftRadius', the main property) or indirectly via 'radius'
309 * (the alternative). Whichever one is encountered first in the propagation chain
310 * takes precedence.
311 * This means that when resolving 'topLeftRadius' for a Button, if 'radius' is set
312 * on the Button and 'topLeftRadius' is set on AbstractButton, then 'radius' will
313 * override 'topLeftRadius' and be used as the final value. */
314 Q_ASSERT(qlonglong(state) <= qlonglong(QQSK::StateFlag::MAX_STATE));
315
316 const PropertyStorageId propertyKey = main.createStorageId(state);
317
318 if (Q_UNLIKELY(QQStyleKitDebug::enabled()))
319 QQStyleKitDebug::trace(main, storageProvider, state, propertyKey);
320
321 const QVariant propertyValue = storageProvider->readStyleProperty(propertyKey);
322 if (propertyValue.isValid()) {
323 if (Q_UNLIKELY(QQStyleKitDebug::enabled()))
324 QQStyleKitDebug::notifyPropertyRead(main, storageProvider, state, propertyValue);
325 return propertyValue;
326 }
327
328 const PropertyStorageId altPropertyKey = alternative.createStorageId(state);
329
330 if (Q_UNLIKELY(QQStyleKitDebug::enabled()))
331 QQStyleKitDebug::trace(alternative, storageProvider, state, altPropertyKey);
332
333 const QVariant altValue = storageProvider->readStyleProperty(altPropertyKey);
334 if (altValue.isValid()) {
335 if (Q_UNLIKELY(QQStyleKitDebug::enabled()))
336 QQStyleKitDebug::notifyPropertyRead(main, storageProvider, state, altValue);
337 return altValue;
338 }
339
340 return {};
341}
342
343template <class INDICES_CONTAINER>
344QVariant QQStyleKitPropertyResolver::readPropertyInControlForStates(
345 const QQStyleKitPropertyPath main, const QQStyleKitPropertyPath alternative,
346 const QQStyleKitControl *control, INDICES_CONTAINER &stateListIndices,
347 int startIndex, int recursionLevel)
348{
349 for (int i = startIndex; i < s_cachedStateList.length(); ++i) {
350 /* stateListIndices is a helper list to track which index in the state list
351 * each recursion level is currently processing. The first recursion level
352 * will iterate through all of the states. The second recursion level will
353 * only the iterate through the states that comes after the state at the
354 * previous recursion. And so on recursively. The end result will be a list of
355 * state combinations (depth first) where no state is repeated more than
356 * once. And for each combination, we check if the storage has a value assigned
357 * for the given property for the given state combination. */
358 stateListIndices[recursionLevel] = i;
359 const QQSK::StateFlag stateFlag = s_cachedStateList[i];
360
361 /* Optimization: check if the control stores values for any properties for
362 * the state we're processing. Otherwise, skip the state. */
363 if (!control->m_writtenStates.testFlag(stateFlag))
364 continue;
365
366 /* Optimization: check if the style/theme/variation stores a value for the
367 * property in the state we're processing. Otherwise, skip the state. */
368 const QQStyleKitControls *controls = control->controls();
369 const QQSK::State statesAffectingProperty = controls->m_writtenPropertyPaths[main.pathId()];
370 if (!statesAffectingProperty.testFlag(stateFlag)) {
371 if (alternative.property() == QQSK::Property::NoProperty) {
372 continue;
373 } else {
374 const QQSK::State statesAffectingAlternative = controls->m_writtenPropertyPaths[alternative.pathId()];
375 if (!statesAffectingAlternative.testFlag(stateFlag))
376 continue;
377 }
378 }
379
380 if (recursionLevel < s_cachedStateList.length() - 1) {
381 // Continue the recursion towards the longest possible nested state
382 const QVariant value = readPropertyInControlForStates(
383 main, alternative, control, stateListIndices, i + 1, recursionLevel + 1);
384 if (value.isValid())
385 return value;
386 }
387
388 // Check the current combination
389 QQSK::State storageState = QQSK::StateFlag::Unspecified;
390 for (int j = 0; j <= recursionLevel; ++j)
391 storageState.setFlag(s_cachedStateList[stateListIndices[j]]);
392 const QVariant value = readPropertyInStorageForState(main, alternative, control, storageState);
393 if (value.isValid())
394 return value;
395 }
396
397 return {};
398}
399
400QVariant QQStyleKitPropertyResolver::readPropertyInControl(
401 const PropertyPathIds &ids, const QQStyleKitControl *control)
402{
403 /* Find the most specific state combination (based on the state of the reader) that
404 * has a value set for the property in the contol. In case several state combinations
405 * could be found, the order of the states in the stateList decides the priority.
406 * If we're reading a property in a subtype, try all state combinations in the subtype
407 * first, before trying all the state combinations in the super type. */
408 QVarLengthArray<int, 10> stateListIndices(s_cachedStateList.length());
409
410 if (ids.subTypeProperty.property() != QQSK::Property::NoProperty) {
411 if (s_cachedState != QQSK::StateFlag::Normal) {
412 QVariant value = readPropertyInControlForStates(
413 ids.subTypeProperty, ids.subTypeAlternative, control, stateListIndices, 0, 0);
414 if (value.isValid())
415 return value;
416 }
417
418 if (control->m_writtenStates.testFlag(QQSK::StateFlag::Normal)) {
419 const QVariant value = readPropertyInStorageForState(
420 ids.subTypeProperty, ids.subTypeAlternative, control, QQSK::StateFlag::Normal);
421 if (value.isValid())
422 return value;
423 }
424 }
425
426 if (s_cachedState != QQSK::StateFlag::Normal) {
427 const QVariant value = readPropertyInControlForStates(
428 ids.property, ids.alternative, control, stateListIndices, 0, 0);
429 if (value.isValid())
430 return value;
431 }
432
433 /* The normal state is the propagation fall back for all state combinations.
434 * If the normal state has the property set, it'll return a valid QVariant,
435 * which will cause the propagation to stop. Otherwise we'll return an invalid
436 * variant which will cause the search to continue. */
437 if (control->m_writtenStates.testFlag(QQSK::StateFlag::Normal))
438 return readPropertyInStorageForState(ids.property, ids.alternative, control, QQSK::StateFlag::Normal);
439
440 return {};
441}
442
443QVariant QQStyleKitPropertyResolver::readPropertyInRelevantControls(
444 const QQStyleKitControls *controls, const PropertyPathIds &ids,
445 const QQStyleKitExtendableControlType exactType,
446 const QList<QQStyleKitExtendableControlType> baseTypes)
447{
448 if (!controls)
449 return {};
450
451 /* Optimization: check if the style/theme/variation stores a value for
452 * the property, regardless of state. Otherwise we can just return. */
453 while (true) {
454 const auto writtenProperties = controls->m_writtenPropertyPaths;
455 if (writtenProperties.contains(ids.property.pathId()))
456 break;
457 const bool hasAlternative = ids.alternative.property() != QQSK::Property::NoProperty;
458 if (hasAlternative && writtenProperties.contains(ids.alternative.pathId()))
459 break;
460 if (ids.subTypeProperty.property() == QQSK::Property::NoProperty)
461 return {};
462 if (writtenProperties.contains(ids.subTypeProperty.pathId()))
463 break;
464 if (hasAlternative && writtenProperties.contains(ids.subTypeAlternative.pathId()))
465 break;
466 return {};
467 }
468
469 if (const QQStyleKitControl *control = controls->getControlStyle(exactType)) {
470 const QVariant value = readPropertyInControl(ids, control);
471 if (value.isValid())
472 return value;
473 }
474
475 for (const int type : baseTypes) {
476 if (const QQStyleKitControl *control = controls->getControlStyle(type)) {
477 const QVariant value = readPropertyInControl(ids, control);
478 if (value.isValid())
479 return value;
480 }
481 }
482
483 return {};
484}
485
486QVariant QQStyleKitPropertyResolver::readPropertyInVariations(
487 const QList<QPointer<QQStyleKitVariation>> &variations,
488 const QQStyleKitStyleAndThemeBase *styleOrTheme,
489 const PropertyPathIds &ids,
490 const QQStyleKitExtendableControlType exactType,
491 const QList<QQStyleKitExtendableControlType> baseTypes)
492{
493 bool foundAtLeastOneVariation = false;
494 for (const QPointer<QQStyleKitVariation> &variation : variations) {
495 if (!variation)
496 continue;
497 if (!variation->m_usageContext.contains(styleOrTheme)) {
498 if (foundAtLeastOneVariation) {
499 /* Optimization: We have found at least one effective variation in the list, but this
500 * one has a different usage context. This means that the remaining variations will also
501 * have a different usage context, since the variations were added to the list sorted on context.
502 * So we can end the iteration. */
503 break;
504 }
505 /* The variations list is sorted on usage context. And the first variation in the list with
506 * the correct usage context has yet to be found. So continue to the next variation. */
507 continue;
508 }
509 foundAtLeastOneVariation = true;
510 const QVariant value = readPropertyInRelevantControls(variation, ids, exactType, baseTypes);
511 if (value.isValid())
512 return value;
513 }
514 return {};
515}
516
517QVariant QQStyleKitPropertyResolver::readPropertyInStyle(
518 QQStyleKitStyle *style, const PropertyPathIds &ids, QQStyleKitReader *styleReader)
519{
520 /* Sync the palette of the style with the palette of the current reader. Note
521 * that this can cause palette bindings in the style to change, which will
522 * result in calls to writeStyleProperty(). */
523 style->syncFromQPalette(styleReader->effectivePalette());
524
525 /* Cache the state of the style reader to avoid rebuilding the same helper
526 * structures on subsequent reads. In practice, a single style reader
527 * typically processes many properties in sequence rather than just one. */
528 cacheReaderState(styleReader->controlState());
529
530 if (styleReader->m_effectiveVariationsDirty)
531 rebuildVariationsForReader(styleReader, style);
532
533 const QQStyleKitExtendableControlType exactType = styleReader->controlType();
534 const QList<QQStyleKitExtendableControlType> baseTypes = baseTypesForType(exactType);
535
536 QVariant value;
537
538 while (true) {
539 value = readPropertyInVariations(styleReader->m_effectiveVariations, style->theme(), ids, exactType, baseTypes);
540 if (value.isValid())
541 break;
542
543 value = readPropertyInRelevantControls(style->theme(), ids, exactType, baseTypes);
544 if (value.isValid())
545 break;
546
547 value = readPropertyInVariations(styleReader->m_effectiveVariations, style, ids, exactType, baseTypes);
548 if (value.isValid())
549 break;
550
551 value = readPropertyInRelevantControls(style, ids, exactType, baseTypes);
552 if (value.isValid())
553 break;
554
555 if (auto *fallbackStyle = style->fallbackStyle()) {
556 /* Recurse into the fallback style, and search for the property there. If not
557 * found, and the fallback style has a fallback style, the recursion continues. */
558 value = readPropertyInStyle(fallbackStyle, ids, styleReader);
559 if (value.isValid())
560 break;
561 }
562
563 break;
564 }
565
566 if (Q_UNLIKELY(QQStyleKitDebug::enabled())) {
567 if (!value.isValid())
568 QQStyleKitDebug::notifyPropertyNotResolved(ids.property);
569 }
570
571 return value;
572}
573
574QVariant QQStyleKitPropertyResolver::readStyleProperty(
575 const QQStyleKitPropertyGroup *group,
576 const QQSK::Property property,
577 const QQSK::Property alternative)
578{
579 const QQStyleKitControlProperties *controlProperties = group->controlProperties();
580 const QQSK::PropertyPathFlags pathFlags = group->pathFlags();
581 const QQSK::Subclass subclass = controlProperties->subclass();
582
583 if (subclass != QQSK::Subclass::QQStyleKitReader) {
584 /* Due to propagation, we must know the reader’s state in order to resolve the value
585 * of a given property. For example, the background delegate’s color may follow a
586 * completely different lookup path—and therefore produce a different result—depending
587 * on whether the reader is a Slider or a Button, and whether it is hovered, pressed, etc.
588 * For this reason, when a property is not read via a QQStyleKitReader, propagation and
589 * fall back logic cannot be supported.
590 * However, to still allow static (i.e., non-propagating) bindings between properties
591 * _inside_ a Style, we fall back to return the value set directly on the accessed control. */
592 Q_ASSERT(subclass == QQSK::Subclass::QQStyleKitState);
593 const QQStyleKitControlState *controlState = controlProperties->asQQStyleKitState();
594 const QQStyleKitControl *control = controlState->control();
595 const PropertyStorageId key = QQStyleKitPropertyPath(group, property).createStorageId(controlState->nestedState());
596 return control->readStyleProperty(key);
597 }
598
599 QQStyleKitStyle *style = controlProperties->style();
600 if (!style || !style->loaded()) {
601 /* No style has been set yet, or the style is still loading. The application should set
602 * a style early (e.g. at the top of main.qml) before creating any controls, to avoid
603 * premature property reads that end up here. However, some controls like the menu bar
604 * are instantiated very early and may try to read properties before a style is available. */
605 return {};
606 }
607
608 QQStyleKitDebug::groupBeingRead = group;
609 QQStyleKitReader *styleReader = controlProperties->asQQStyleKitReader();
610
611 if (s_isReadingProperty) {
612 if (!s_styleWarningsIssued) {
613 s_styleWarningsIssued = true;
614 qmlWarning(styleReader) << "The style property '" << property << "' was read "
615 << "before finishing the read of another style property. "
616 << "This is likely to cause a style glitch.";
617 }
618 }
619 QScopedValueRollback rollback(s_isReadingProperty, true);
620
621 PropertyPathIds ids;
622 ids.property = QQStyleKitPropertyPath(group, property, QQStyleKitPropertyPath::Flag::ExcludeSubtype);
623 ids.alternative = QQStyleKitPropertyPath(group, alternative, QQStyleKitPropertyPath::Flag::ExcludeSubtype);
624 const bool insideSubType = pathFlags &
625 (QQSK::PropertyPathFlag::DelegateSubtype1 | QQSK::PropertyPathFlag::DelegateSubtype2);
626
627 if (insideSubType) {
628 ids.subTypeProperty = QQStyleKitPropertyPath(group, property);
629 ids.subTypeAlternative = QQStyleKitPropertyPath(group, alternative);
630 } else {
631 ids.subTypeProperty = QQStyleKitPropertyPath();
632 ids.subTypeAlternative = QQStyleKitPropertyPath();
633 }
634
635 if (!pathFlags.testFlag(QQSK::PropertyPathFlag::Global)) {
636 /* A style reader can have a storage that contains local property overrides (that is,
637 * interpolated values from an ongoing transition). When searching for a property, we
638 * therefore need to check this storage first. The exception is if the property was read
639 * inside the 'global' group, which means that we should read the values directly
640 * from the style. */
641 if (insideSubType) {
642 const QVariant value = readPropertyInStorageForState(
643 ids.subTypeProperty, ids.subTypeAlternative, styleReader, QQSK::StateFlag::Normal);
644 if (value.isValid())
645 return value;
646 }
647 const QVariant value = readPropertyInStorageForState(
648 ids.property, ids.alternative, styleReader, QQSK::StateFlag::Normal);
649 if (value.isValid())
650 return value;
651 }
652
653 return readPropertyInStyle(style, ids, styleReader);
654}
655
656bool QQStyleKitPropertyResolver::writeStyleProperty(
657 const QQStyleKitPropertyGroup *group,
658 const QQSK::Property property,
659 const QVariant &value)
660{
661 // While readStyleProperty() takes propagation into account, writeStyleProperty() doesn't.
662 // Instead it writes \a value directly to the storage that the group belongs to.
663 Q_ASSERT(group);
664 const QQStyleKitControlProperties *controlProperties = group->controlProperties();
665 const QQSK::PropertyPathFlags pathFlags = group->pathFlags();
666 const QQSK::Subclass subclass = controlProperties->subclass();
667 const QQStyleKitPropertyPath propertyPath(group, property);
668
669 if (pathFlags.testFlag(QQSK::PropertyPathFlag::Global)) {
670 qmlWarning(controlProperties) << "Properties inside 'global' are read-only!";
671 return false;
672 }
673
674 if (subclass == QQSK::Subclass::QQStyleKitReader) {
675 // This is a write to a StyleKitReader, probably from an ongoing transition
676 QQStyleKitReader *reader = controlProperties->asQQStyleKitReader();
677 const PropertyStorageId key = propertyPath.createStorageId(QQSK::StateFlag::Normal);
678 const QVariant currentValue = reader->readStyleProperty(key);
679 const bool valueChanged = currentValue != value;
680 if (valueChanged) {
681 reader->writeStyleProperty(key, value);
682 QQStyleKitDebug::notifyPropertyWrite(group, property, reader, QQSK::StateFlag::Normal, key, value);
683 }
684 return valueChanged;
685 }
686
687 if (subclass == QQSK::Subclass::QQStyleKitState) {
688 // This is a write to a control inside the StyleKit style
689 const QQStyleKitControlState *controlState = controlProperties->asQQStyleKitState();
690 QQStyleKitControl *control = controlState->control();
691 const QQSK::State nestedState = controlState->nestedState();
692 const PropertyStorageId key = propertyPath.createStorageId(nestedState);
693 const QVariant currentValue = control->readStyleProperty(key);
694 const bool valueChanged = currentValue != value;
695 if (valueChanged) {
696 /* Optimization: Allow a control to track which states it stores property values for.
697 * When later reading a property via propagation, we can skip that control entirely
698 * if it has no stored values corresponding to the state requested by the StyleKitReader. */
699 control->m_writtenStates |= nestedState;
700 /* Optimization: Track which properties a style, theme, or variation defines
701 * values for, and for which states. When reading a property later, we can skip
702 * the style/theme/variation entirely if it has no stored values for it, or if
703 * none match the state requested by the StyleKitReader. */
704 QQStyleKitControls *controls = control->controls();
705 const QQSK::State alreadyWrittenStates = controls->m_writtenPropertyPaths[propertyPath.pathId()];
706 controls->m_writtenPropertyPaths[propertyPath.pathId()] = alreadyWrittenStates | nestedState;
707
708 control->writeStyleProperty(key, value);
709 QQStyleKitDebug::notifyPropertyWrite(group, property, control, nestedState, key, value);
710 }
711 return valueChanged;
712 }
713
714 Q_UNREACHABLE();
715 return false;
716}
717
718bool QQStyleKitPropertyResolver::hasLocalStyleProperty(
719 const QQStyleKitPropertyGroup *group,
720 const QQSK::Property property)
721{
722 Q_ASSERT(group);
723 const QQStyleKitControlProperties *controlProperties = group->controlProperties();
724 const QQSK::PropertyPathFlags pathFlags = group->pathFlags();
725 const QQStyleKitPropertyPath propertyPath(group, property);
726 const QQSK::Subclass subclass = controlProperties->subclass();
727
728 if (pathFlags.testFlag(QQSK::PropertyPathFlag::Global))
729 return false;
730
731 if (subclass == QQSK::Subclass::QQStyleKitReader) {
732 const PropertyStorageId key = propertyPath.createStorageId(QQSK::StateFlag::Normal);
733 return controlProperties->asQQStyleKitReader()->readStyleProperty(key).isValid();
734 }
735
736 if (subclass == QQSK::Subclass::QQStyleKitState) {
737 const QQStyleKitControlState *controlState = controlProperties->asQQStyleKitState();
738 const QQStyleKitControl *control = controlState->control();
739 const PropertyStorageId key = propertyPath.createStorageId(controlState->nestedState());
740 return control->readStyleProperty(key).isValid();
741 }
742
743 Q_UNREACHABLE();
744 return false;
745}
746
747QT_END_NAMESPACE
748
749#include "moc_qqstylekitpropertyresolver_p.cpp"
QQStyleKitControlProperties * controlProperties() const
Combined button and popup list for selecting options.