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