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