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