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
qqstylekitreader.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
4#include <QtQuick/private/qquickpalette_p.h>
5#include <QtQuick/private/qquickwindow_p.h>
6#include <QtQuick/private/qquickstategroup_p.h>
7#include <QtQuick/private/qquickpropertychanges_p.h>
8
9#include "qqstylekit_p.h"
13
15
16/*!
17 \qmltype StyleReader
18 \inqmlmodule Qt.labs.StyleKit
19 \inherits ControlStyleProperties
20 \brief Reads properties from the active Style for a specific control.
21
22 StyleReader is the bridge between a control in \l {Qt Quick Controls}
23 and the \l {StyleKit::style}{active style}. It exposes all the style
24 properties — \l {ControlStyleProperties::background}{backgrounds},
25 \l {ControlStyleProperties::indicator}{indicators},
26 \l {ControlStyleProperties::handle}{handles},
27 \l {ControlStyleProperties::text}{text},
28 \l {ControlStyleProperties::padding}{padding},
29 and more — that a control, and its delegates, should bind to.
30 All built-in \c StyleKit controls use a StyleReader
31 internally for this purpose.
32
33 The state properties — \l hovered, \l pressed, \l focused, \l checked,
34 \l highlighted, etc. — tell StyleReader which state the control is in.
35 It uses these to resolve and read the correct property values from
36 the \l Style, taking \l {Theme}{Themes}, \l {StyleVariation}{StyleVariations},
37 \l {StylableControls}{fallback types}, and property propagation
38 into account.
39
40 When implementing a \l CustomControl, you can follow the same approach as
41 the built-in controls:
42
43 \snippet StyleReaderSnippets.qml custom control
44
45 \note The style properties inherited from \l ControlStyleProperties do not map
46 directly to the \l Style. Instead, they reflect a cache of potentially interpolated
47 values during an active \l {ControlStyle::}{transition}. Writing to them will \e not
48 update the corresponding properties in the \l Style and may interfere with ongoing
49 transitions. Write directly to the \l Style when you need to change a style property.
50
51 \labs
52
53 \sa CustomControl, Style, {StyleVariation::controlType}{StyleVariation.controlType},
54 {StyleKit Property Resolution}
55*/
56
57/*!
58 \qmlproperty enumeration StyleReader::controlType
59
60 Identifies which control type in the \l Style this reader reads
61 properties from. This can either be set to one of the predefined values
62 below, or to a \l {CustomControl::controlType}{custom control type} defined in the
63 \l Style.
64
65 \value StyleReader.BusyIndicator \l {BusyIndicator}
66 \value StyleReader.Button \l {Button}
67 \value StyleReader.CheckBox \l {CheckBox}
68 \value StyleReader.CheckDelegate \l {CheckDelegate}
69 \value StyleReader.ComboBox \l {ComboBox}
70 \value StyleReader.Container \l {Container}
71 \value StyleReader.DayOfWeekRow \l {DayOfWeekRow}
72 \value StyleReader.DelayButton \l {DelayButton}
73 \value StyleReader.Dial \l {Dial}
74 \value StyleReader.Dialog \l {Dialog}
75 \value StyleReader.DialogButtonBox \l {DialogButtonBox}
76 \value StyleReader.Drawer \l {Drawer}
77 \value StyleReader.Frame \l {Frame}
78 \value StyleReader.GroupBox \l {GroupBox}
79 \value StyleReader.HeaderViewDelegate \l {HorizontalHeaderViewDelegate} and \l {VerticalHeaderViewDelegate}
80 \value StyleReader.ItemDelegate \l {ItemDelegate}
81 \value StyleReader.Label \l {Label}
82 \value StyleReader.Menu \l {Menu}
83 \value StyleReader.MenuBar \l {MenuBar}
84 \value StyleReader.MenuBarItem \l {MenuBarItem}
85 \value StyleReader.MenuItem \l {MenuItem}
86 \value StyleReader.MenuSeparator \l {MenuSeparator}
87 \value StyleReader.MonthGrid \l {MonthGrid}
88 \value StyleReader.Page \l {Page}
89 \value StyleReader.PageIndicator \l {PageIndicator}
90 \value StyleReader.Pane \l {Pane}
91 \value StyleReader.Popup \l {Popup}
92 \value StyleReader.ProgressBar \l {ProgressBar}
93 \value StyleReader.RadioButton \l {RadioButton}
94 \value StyleReader.RadioDelegate \l {RadioDelegate}
95 \value StyleReader.RoundButton \l {RoundButton}
96 \value StyleReader.ScrollBar \l {ScrollBar}
97 \value StyleReader.ScrollView \l {ScrollView}
98 \value StyleReader.SearchField \l {SearchField}
99 \value StyleReader.Slider \l {Slider}
100 \value StyleReader.SpinBox \l {SpinBox}
101 \value StyleReader.SplitView \l {SplitView}
102 \value StyleReader.SwipeDelegate \l {SwipeDelegate}
103 \value StyleReader.Switch \l {Switch}
104 \value StyleReader.SwitchDelegate \l {SwitchDelegate}
105 \value StyleReader.TabBar \l {TabBar}
106 \value StyleReader.TabButton \l {TabButton}
107 \value StyleReader.TableViewDelegate \l {TableViewDelegate}
108 \value StyleReader.TextArea \l {TextArea}
109 \value StyleReader.TextField \l {TextField}
110 \value StyleReader.ToolBar \l {ToolBar}
111 \value StyleReader.ToolButton \l {ToolButton}
112 \value StyleReader.ToolSeparator \l {ToolSeparator}
113 \value StyleReader.ToolTip \l {ToolTip}
114 \value StyleReader.TreeViewDelegate \l {TreeViewDelegate}
115 \value StyleReader.Tumbler \l {Tumbler}
116 \value StyleReader.WeekNumberColumn \l {WeekNumberColumn}
117
118 \sa {StyleVariation::controlType}{StyleVariation.controlType}
119*/
120
121/*!
122 \qmlproperty bool StyleReader::enabled
123
124 Whether the control is enabled.
125
126 Bind this to the control's \l {Item::enabled}{enabled} property.
127
128 The default value is \c true.
129
130 \sa {ControlStateStyle::disabled}{ControlStateStyle.disabled}
131*/
132
133/*!
134 \qmlproperty bool StyleReader::hovered
135
136 Whether the control is hovered.
137
138 Bind this to the control's \l {Control::hovered}{hovered} property.
139
140 The default value is \c false.
141
142 \sa {ControlStateStyle::hovered}{ControlStateStyle.hovered}
143*/
144
145/*!
146 \qmlproperty bool StyleReader::pressed
147
148 Whether the control is pressed.
149
150 Bind this to the control's \l {AbstractButton::pressed}{pressed} property.
151
152 The default value is \c false.
153
154 \sa {ControlStateStyle::pressed}{ControlStateStyle.pressed}
155*/
156
157/*!
158 \qmlproperty bool StyleReader::focused
159
160 Whether the control has active focus.
161
162 Bind this to the control's \l {Item::activeFocus}{activeFocus} property.
163
164 The default value is \c false.
165
166 \sa {ControlStateStyle::focused}{ControlStateStyle.focused}
167*/
168
169/*!
170 \qmlproperty bool StyleReader::checked
171
172 Whether the control is checked.
173
174 Bind this to the control's \l {AbstractButton::checked}{checked} property.
175
176 The default value is \c false.
177
178 \sa {ControlStateStyle::checked}{ControlStateStyle.checked}
179*/
180
181/*!
182 \qmlproperty bool StyleReader::highlighted
183
184 Whether the control is highlighted.
185
186 Bind this to the control's \l {Button::highlighted}{highlighted} property.
187
188 The default value is \c false.
189
190 \sa {ControlStateStyle::highlighted}{ControlStateStyle.highlighted}
191*/
192
193/*!
194 \qmlproperty bool StyleReader::vertical
195
196 Whether the control is oriented vertically.
197
198 Bind this to the control's \l {Slider::orientation}{orientation}.
199
200 The default value is \c false.
201
202 \sa {ControlStateStyle::vertical}{ControlStateStyle.vertical}
203*/
204
205/*!
206 \qmlproperty palette StyleReader::palette
207
208 The palette of the control. StyleKit uses this to resolve color properties
209 that bind the \l {Style::}{palette} in the \l Style.
210
211 Bind this to the control's \l {Control::palette}{palette} property.
212
213 \sa {Style::palette}{Style.palette}
214*/
215
216/*!
217 \qmlproperty font StyleReader::font
218 \readonly
219
220 The effective font for this control type, as defined by the
221 \l {AbstractStyle::fonts}{style}. This also takes into account any font
222 overrides set in the \l {ControlStyle::}{text} properties of the style.
223
224 Bind the control's \l {Control::font}{font} property to this property.
225
226 \note Unlike properties such as \l hovered, \l pressed and \l palette — which
227 is forwarded from the control to the StyleReader — \l font is an output. Bind the
228 control's \l {Control::font}{font} property to this value, not the other way around.
229*/
230
231/*!
232 \qmlproperty ControlStyleProperties StyleReader::global
233 \readonly
234
235 Provides direct access to the style properties, bypassing any ongoing
236 \l {ControlStyle::}{transition}.
237
238 While a state transition is ongoing, style properties read from a StyleReader
239 may return interpolated values. By prepending \c global to the property path,
240 you bypass the transition and get the end-state values immediately.
241
242 For example, when transitioning from \l hovered to \l pressed, \c {background.color}
243 may return an interpolated value between the \l {DelegateStyle::}{color} in the
244 \l {ControlStateStyle::}{hovered} state and the color in the
245 \l {ControlStateStyle::}{pressed} state. \c {global.background.color}, on the
246 other hand, returns the color in the pressed state directly.
247
248 \sa {StyleKit::transitionsEnabled}{transitionsEnabled}
249*/
250
251using namespace Qt::StringLiterals;
252
253static constexpr QLatin1StringView kAlternate1 = "A1"_L1;
254static constexpr QLatin1StringView kAlternate2 = "A2"_L1;
255
256static quint64 textFontOverridesSignature(const QQStyleKitTextProperties *t)
257{
258 if (!t)
259 return 0;
260
261 quint64 sig = 0;
262
263 // bit 0: bold defined, bit 1: bold value
264 if (t->isDefined(QQSK::Property::Bold)) {
265 sig |= (quint64(1) << 0);
266 if (t->styleProperty<bool>(QQSK::Property::Bold))
267 sig |= (quint64(1) << 1);
268 }
269 // bit 2: italic defined, bit 3: italic value
270 if (t->isDefined(QQSK::Property::Italic)) {
271 sig |= (quint64(1) << 2);
272 if (t->styleProperty<bool>(QQSK::Property::Italic))
273 sig |= (quint64(1) << 3);
274 }
275 // bit 4: pointSize defined, bits 5..: quantized pointSize
276 if (t->isDefined(QQSK::Property::PointSize)) {
277 sig |= (quint64(1) << 4);
278 const qreal ps = t->styleProperty<qreal>(QQSK::Property::PointSize);
279 // 64-bit signature: 5 bits used, 59 bits available for pointSize
280 constexpr int payloadBits = 64 - 5;
281 const qint64 maxQ = (quint64(1) << payloadBits) - 1;
282 const quint64 q = quint64(qBound<qint64>(0, qRound64(ps * 64.0), maxQ));
283 sig |= (q << 5);
284 }
285 return sig;
286}
287
288QList<QQStyleKitReader *> QQStyleKitReader::s_allReaders;
289QQStyleKitReader::PropertyChangesComponents QQStyleKitReader::s_propertyChangesComponents;
290
291QQStyleKitReader::QQStyleKitReader(QObject *parent)
292 : QQStyleKitControlProperties(QQSK::PropertyGroup::Control, parent)
293 , m_dontEmitChangedSignals(false)
294 , m_effectiveVariationsDirty(true)
295 , m_transitionsEnabled(true)
296 , m_completed(false)
297 , m_global(QQStyleKitControlProperties(QQSK::PropertyGroup::GlobalFlag, this))
298{
299}
300
301QQStyleKitReader::~QQStyleKitReader()
302{
303 s_allReaders.removeOne(this);
304 if (m_explicitStyle)
305 m_explicitStyle->m_explicitReaderCount--;
306}
307
308QQuickStateGroup *QQStyleKitReader::stateGroup()
309{
310 if (m_stateGroup)
311 return m_stateGroup;
312
313 /* Lazy create a QQuickStateGroup as soon as we have delegates
314 * that needs to be "tracked". That is, the user of this StyleKitReader
315 * has read one or more properties, so we need to check and emit changes for
316 * those properties whenever our state changes. */
317 const auto *stylePtr = style();
318 Q_ASSERT(stylePtr);
319
320 m_stateGroup = new QQuickStateGroup(this);
321
322 // Add two states that we can alternate between
323 auto statesProp = m_stateGroup->statesProperty();
324 QQuickState *alternate1 = new QQuickState(m_stateGroup);
325 QQuickState *alternate2 = new QQuickState(m_stateGroup);
326 alternate1->setName(kAlternate1);
327 alternate2->setName(kAlternate2);
328 m_stateGroup->statesProperty().append(&statesProp, alternate1);
329 m_stateGroup->statesProperty().append(&statesProp, alternate2);
330
331 QQmlComponent *controlComp = createControlChangesComponent();
332 instantiatePropertyChanges(controlComp);
333
334 return m_stateGroup;
335}
336
337QQmlComponent *QQStyleKitReader::createControlChangesComponent() const
338{
339 QQmlEngine *engine = qmlEngine(style());
340 auto key = PropertyChangesComponents::key_type{engine, u"control"_s};
341 if (auto r = s_propertyChangesComponents.value(key, nullptr))
342 return r.data();
343
344 const QString qmlControlCode = QString::fromUtf8(R"(
345 import QtQuick
346 PropertyChanges {
347 spacing: global.spacing
348 padding: global.padding
349 leftPadding: global.leftPadding
350 rightPadding: global.rightPadding
351 topPadding: global.topPadding
352 bottomPadding: global.bottomPadding
353 text.color: global.text.color
354 text.alignment: global.text.alignment
355 text.bold: global.text.bold
356 text.italic: global.text.italic
357 text.pointSize: global.text.pointSize
358 text.padding: global.text.padding
359 text.leftPadding: global.text.leftPadding
360 text.rightPadding: global.text.rightPadding
361 text.topPadding: global.text.topPadding
362 text.bottomPadding: global.text.bottomPadding
363 }
364 )");
365
366 // TODO: cache propertyName to component!
367 QQmlComponent *component = new QQmlComponent(engine, engine);
368 component->setData(qmlControlCode.toUtf8(), QUrl());
369 Q_ASSERT_X(!component->isError(), __FUNCTION__, component->errorString().toUtf8().constData());
370 s_propertyChangesComponents.insert(key, component);
371 QObject::connect(engine, &QObject::destroyed, engine, [key = std::move(key)] {
372 s_propertyChangesComponents.remove(key);
373 });
374 return component;
375}
376
377QQmlComponent *QQStyleKitReader::createDelegateChangesComponent(const QString &delegateName) const
378{
379 QQmlEngine *engine = qmlEngine(style());
380 auto key = PropertyChangesComponents::key_type{engine, delegateName};
381 if (auto r = s_propertyChangesComponents.value(key, nullptr))
382 return r.data();
383
384 static const QString qmlTemplateCode = QString::fromUtf8(R"(
385 import QtQuick
386 PropertyChanges { $ {
387 width: global.$.width
388 height: global.$.height
389 visible: global.$.visible
390 color: global.$.color
391 gradient: global.$.gradient
392 radius: global.$.radius
393 topLeftRadius: global.$.topLeftRadius
394 topRightRadius: global.$.topRightRadius
395 bottomLeftRadius: global.$.bottomLeftRadius
396 bottomRightRadius: global.$.bottomRightRadius
397 margins: global.$.margins
398 alignment: global.$.alignment
399 leftMargin: global.$.leftMargin
400 rightMargin: global.$.rightMargin
401 topMargin: global.$.topMargin
402 bottomMargin: global.$.bottomMargin
403 scale: global.$.scale
404 rotation: global.$.rotation
405 opacity: global.$.opacity
406 border.color: global.$.border.color
407 border.width: global.$.border.width
408 shadow.color: global.$.shadow.color
409 shadow.scale: global.$.shadow.scale
410 shadow.blur: global.$.shadow.blur
411 shadow.visible: global.$.shadow.visible
412 shadow.opacity: global.$.shadow.opacity
413 shadow.verticalOffset: global.$.shadow.verticalOffset
414 shadow.horizontalOffset: global.$.shadow.horizontalOffset
415 shadow.delegate: global.$.shadow.delegate
416 image.source: global.$.image.source
417 image.color: global.$.image.color
418 image.fillMode: global.$.image.fillMode
419 delegate: global.$.delegate
420 data: global.$.data
421 }}
422 )");
423
424 QString substitutedCode = qmlTemplateCode;
425 substitutedCode.replace('$'_L1, delegateName);
426 QQmlComponent *component = new QQmlComponent(engine, engine);
427 component->setData(substitutedCode.toUtf8(), QUrl());
428 Q_ASSERT_X(!component->isError(), __FUNCTION__, component->errorString().toUtf8().constData());
429 s_propertyChangesComponents.insert(key, component);
430 QObject::connect(engine, &QObject::destroyed, engine, [key = std::move(key)] {
431 s_propertyChangesComponents.remove(key);
432 });
433 return component;
434}
435
436void QQStyleKitReader::instantiatePropertyChanges(QQmlComponent *comp)
437{
438 QObject *obj = comp->create(qmlContext(this));
439 auto *propertyChanges = qobject_cast<QQuickPropertyChanges *>(obj);
440 Q_ASSERT(propertyChanges);
441
442 // setter for the "target" property is called setObject
443 propertyChanges->setObject(this);
444 /* set "explicit" to true, meaning that the StyleProperties shouldn't
445 * create bindings, but do one-off assignments. Bindings are not needed
446 * here since it's the state changes of this StyleKitReader that
447 * drives the property changes. The properties cannot change outside of
448 * a state change (or, if they do, it will trigger a full update equal
449 * to a theme change) */
450 propertyChanges->setIsExplicit(true);
451 /* We don't need to ever restore the properties back to default, since
452 * the group state will never be reset back to an empty string. This will
453 * hopefully avoid the generation of restore structures inside the StateGroup. */
454 propertyChanges->setRestoreEntryValues(false);
455
456 /* Add the new PropertyChanges object to both states, A1 and A2 */
457 for (QQuickState *state : stateGroup()->states()) {
458 auto changesProp = state->changes();
459 changesProp.append(&changesProp, propertyChanges);
460 }
461}
462
463void QQStyleKitReader::maybeTrackDelegates()
464{
465 forEachUsedDelegate(
466 [this](QQStyleKitDelegateProperties *delegate, QQSK::Delegate type, const QString &delegatePath){
467 if (m_trackedDelegates.testFlag(type)) {
468 // We're already tracking the delegate. So nothing needs to be done.
469 return;
470 }
471 if (!delegate->visible()) {
472 /* As an optimization, if the delegate is hidden, we don't track it. Most
473 * controls set background.visible to false, for example. If this, for
474 * whatever reason, is not wanted, set opacity to 0 instead. */
475 return;
476 }
477 /* Invariant: The application has read one or more properties for the given delegate
478 * from the Style, but we don't yet have a PropertyChanges object that can track
479 * changes to it (and run transitions). So we create one now. By lazy creating them this
480 * way, we avoid creating PropertyChanges for all the different delegates that a control
481 * _may_ have. Instead, we only track changes for the delegates it actually uses. */
482 m_trackedDelegates.setFlag(type);
483 QQmlComponent *comp = createDelegateChangesComponent(delegatePath);
484 instantiatePropertyChanges(comp);
485 });
486}
487
488void QQStyleKitReader::updateControl(bool applyStateChange)
489{
490 const QQStyleKitStyle *currentStyle = style();
491 if (!m_completed || !currentStyle || !currentStyle->loaded())
492 return;
493
494 if (applyStateChange) {
495 /* Alternate between two states to trigger a state change. The state group
496 * will, upon changing state, take care of reading the updated property values,
497 * compare them against the current ones in the local storage, and emit changes
498 * (possibly using a transition) if changed. Since the new state might change the
499 * transition, we need to update it first before we do the state change, so that
500 * it takes effect.
501 * If we have skipped tracking some delegates because they are hidden, we need to
502 * check again if this is still the case for the current state. Otherwise, we now
503 * need to track them. Untracked delegates are not backed by PropertyChanges objects,
504 * and hence, will not update when we do a state swap below.
505 * Note that the first time this function is called after start-up, none of the
506 * delegates are yet tracked, and therefore will be created now. */
507
508 maybeTrackDelegates();
509
510 auto transitionProp = stateGroup()->transitionsProperty();
511 const int transitionCountInStateGroup = transitionProp.count(&transitionProp);
512 const bool enabled = m_transitionsEnabled && QQStyleKit::qmlAttachedProperties()->transitionsEnabled();
513 QQuickTransition *transitionInStyle = enabled ? transition() : nullptr;
514 QQuickTransition *transitionInStateGroup =
515 transitionCountInStateGroup > 0 ? transitionProp.at(&transitionProp, 0) : nullptr;
516 if (transitionInStyle != transitionInStateGroup) {
517 transitionProp.clear(&transitionProp);
518 if (transitionInStyle)
519 transitionProp.append(&transitionProp, transitionInStyle);
520 }
521
522 switch (m_alternateState) {
523 case AlternateState::Alternate1:
524 m_alternateState = AlternateState::Alternate2;
525 stateGroup()->setState(kAlternate2);
526 break;
527 case AlternateState::Alternate2:
528 m_alternateState = AlternateState::Alternate1;
529 stateGroup()->setState(kAlternate1);
530 break;
531 default:
532 Q_UNREACHABLE();
533 }
534 } else {
535 // Since no state change occurs, the property values in
536 // the local storage no longer reflect this StyleReader's
537 // current state. Clear it to avoid returning stale values
538 // from subsequent property reads.
539 clearLocalStorage();
540 }
541
542 auto textOverrideSig = textFontOverridesSignature(global()->text());
543 if (m_lastTextFontOverridesSignature != textOverrideSig)
544 m_fontDirty = true;
545 m_lastTextFontOverridesSignature = textOverrideSig;
546 rebuildEffectiveFont();
547}
549void QQStyleKitReader::resetReadersForStyle(const QQStyleKitStyle *style)
550{
551 if (style != QQStyleKitStyle::current() && !style->hasExplicitReaders()) {
552 /* Optimization: A QML runtime can only have one style that is current, and all
553 * the StyleReaders read from it, unless they have been given an explicit style.
554 * So if this style is neither, no StyleReader can be reading from it. */
555 return;
556 }
558 for (QQStyleKitReader *reader : s_allReaders) {
559 if (reader->style() == style) {
560 reader->m_effectiveVariationsDirty = true;
561 reader->m_fontDirty = true;
562 reader->clearLocalStorage();
563 reader->rebuildEffectivePalette();
564 reader->rebuildEffectiveFont();
565 reader->emitChangedForAllStyleProperties(EmitFlag::AllProperties);
566 }
567 }
568}
569
570void QQStyleKitReader::populateLocalStorage()
571{
572 if (!m_completed) {
573 /* The local storage is used for implementing transitions and to ensure that we
574 * only emit changed signals for properties that actually change during a state
575 * change. During start-up, however, all properties are considered dirty, so we
576 * can delay populating the storage until the first time we get a state change
577 * after completed, to improve start-up performance. */
578 return;
579 }
580
581 if (!m_storage.isEmpty())
582 return;
583 const auto *stylePtr = style();
584 if (!stylePtr || !stylePtr->loaded())
585 return;
586
587 /* The local storage is empty, which is typically the case after an
588 * operation that should perform without a transition, such as a theme
589 * change or a change to a property value in the Style itself.
590 * Doing a transition in that case is unwanted and slow (since the
591 * operation typically affect all controls), so we short-cut the process by
592 * emitting changed signals directly for all the properties instead.
593 * Since that will render the local storage out-of-sync, we clear it at the
594 * same time to signal that it's 'dirty'. Which is why we need to sync it back
595 * up again now.
596 * Syncing the local storage before changing state (even if the values that
597 * end up in the storage should be exactly the same as those already
598 * showing), has the upshot that we can compare after the state change which
599 * properties has changed, which will limit the amount of changed signals we
600 * then need to emit. */
601 m_dontEmitChangedSignals = true;
602 updateControl();
603 m_dontEmitChangedSignals = false;
604}
605
606void QQStyleKitReader::clearLocalStorage()
607{
608 /* Clear all the local property overrides that has been set on this reader. Such
609 * overrides are typically interpolated values set by a transition during a state
610 * change. By clearing them, the controls will end up reading the property values
611 * directly from the Style instead. */
612 m_storage.clear();
613}
614
615QQSK::State QQStyleKitReader::controlState() const
616{
617 QQSK::State effectiveState = m_state;
618
619 if (!enabled()) {
620 // Some states are not valid if the control is disabled
621 effectiveState &= ~(QQSK::StateFlag::Pressed |
622 QQSK::StateFlag::Hovered |
623 QQSK::StateFlag::Highlighted |
624 QQSK::StateFlag::Focused |
625 QQSK::StateFlag::Hovered);
626 }
627
628 if (effectiveState == QQSK::StateFlag::Unspecified)
629 effectiveState.setFlag(QQSK::StateFlag::Normal);
630
631 return effectiveState;
632}
634QVariant QQStyleKitReader::readStyleProperty(PropertyStorageId key) const
635{
636 return m_storage.value(key);
637}
639void QQStyleKitReader::writeStyleProperty(PropertyStorageId key, const QVariant &value)
640{
641 m_storage.insert(key, value);
642}
643
644bool QQStyleKitReader::dontEmitChangedSignals() const
645{
646 return m_dontEmitChangedSignals;
647}
648
649QQStyleKitExtendableControlType QQStyleKitReader::controlType() const
650{
651 return m_type;
652}
653
654void QQStyleKitReader::setControlType(QQStyleKitExtendableControlType type)
655{
656 if (m_type == type)
657 return;
658
659 m_type = type;
660 populateLocalStorage();
661 emit controlTypeChanged();
662 updateControl();
663}
664
665#ifdef QT_DEBUG
666QQStyleKitReader::ControlType QQStyleKitReader::typeAsControlType() const
667{
668 /* Note: m_type is of type int to support extending the list
669 * of possible types from the Style itself. This function
670 * is here to for debugging purposes */
671 return ControlType(m_type);
672}
673#endif
674
675bool QQStyleKitReader::hovered() const
676{
677 return m_state.testFlag(QQSK::StateFlag::Hovered);
678}
679
680void QQStyleKitReader::setHovered(bool hovered)
682 if (hovered == QQStyleKitReader::hovered())
683 return;
684
685 populateLocalStorage();
686 m_state.setFlag(QQSK::StateFlag::Hovered, hovered);
687 emit hoveredChanged();
688 updateControl();
689}
690
691bool QQStyleKitReader::enabled() const
692{
693 return !m_state.testFlag(QQSK::StateFlag::Disabled);
694}
695
696void QQStyleKitReader::setEnabled(bool enabled)
698 if (enabled == QQStyleKitReader::enabled())
699 return;
700
701 populateLocalStorage();
702 m_state.setFlag(QQSK::StateFlag::Disabled, !enabled);
703 emit enabledChanged();
704 updateControl();
705}
706
707bool QQStyleKitReader::focused() const
708{
709 return m_state.testFlag(QQSK::StateFlag::Focused);
710}
711
712void QQStyleKitReader::setFocused(bool focused)
714 if (focused == QQStyleKitReader::focused())
715 return;
716
717 populateLocalStorage();
718 m_state.setFlag(QQSK::StateFlag::Focused, focused);
719 emit focusedChanged();
720 updateControl();
721}
722
723bool QQStyleKitReader::checked() const
724{
725 return m_state.testFlag(QQSK::StateFlag::Checked);
726}
727
728void QQStyleKitReader::setChecked(bool checked)
730 if (checked == QQStyleKitReader::checked())
731 return;
732
733 populateLocalStorage();
734 m_state.setFlag(QQSK::StateFlag::Checked, checked);
735 emit checkedChanged();
736 updateControl();
737}
738
739bool QQStyleKitReader::pressed() const
740{
741 return m_state.testFlag(QQSK::StateFlag::Pressed);
742}
743
744void QQStyleKitReader::setPressed(bool pressed)
745{
746 if (pressed == QQStyleKitReader::pressed())
747 return;
748
749 populateLocalStorage();
750 m_state.setFlag(QQSK::StateFlag::Pressed, pressed);
751 emit pressedChanged();
752 updateControl();
753}
755bool QQStyleKitReader::vertical() const
756{
757 return m_state.testFlag(QQSK::StateFlag::Vertical);
758}
759
760void QQStyleKitReader::setVertical(bool vertical)
761{
762 if (vertical == QQStyleKitReader::vertical())
763 return;
765 populateLocalStorage();
766 m_state.setFlag(QQSK::StateFlag::Vertical, vertical);
767 emit verticalChanged();
768 updateControl();
769}
770
771bool QQStyleKitReader::highlighted() const
772{
773 return m_state.testFlag(QQSK::StateFlag::Highlighted);
774}
775
776void QQStyleKitReader::setHighlighted(bool highlighted)
777{
778 if (highlighted == QQStyleKitReader::highlighted())
779 return;
780
781 populateLocalStorage();
782 m_state.setFlag(QQSK::StateFlag::Highlighted, highlighted);
783 emit highlightedChanged();
784 updateControl();
785}
786
787void QQStyleKitReader::setControlTypeAndState(QQStyleKitExtendableControlType controlType, QQSK::State flags,
788 bool applyStateChange)
789{
790 // Apply type and states in one shot to avoid calling
791 // populateLocalStorage() + updateControl() once per individual setter
792 const bool typeChanged = m_type != controlType;
793 const QQSK::State stateChanged = m_state ^ flags;
794
795 if (!typeChanged && !stateChanged)
796 return;
797
798 if (applyStateChange)
799 populateLocalStorage();
800
801 m_type = controlType;
802 m_state = flags;
803
804 updateControl(applyStateChange);
805}
806
807QObject *QQStyleKitReader::target() const
808{
809 return m_target.data();
810}
811
812void QQStyleKitReader::setTarget(QObject *target)
813{
814 /* A reader can optionally set a target object which represents
815 the current object that is being styled, ie: using this StyleReader
816 to read properties from the style. It is needed so that the widget style using
817 the StyleReader is able to post StyleAnimationUpdate events to the correct
818 target during a transition. */
819 m_target = target;
820}
821
822void QQStyleKitReader::setCompleted(bool completed)
823{
824 const QQStyleKitStyle *currentStyle = style();
825 if (currentStyle && currentStyle->loaded()) {
826 clearLocalStorage();
827 rebuildEffectiveFont();
828 rebuildEffectivePalette();
829
830 /* Some style properties were read (e.g. by a Qt Quick Control) before this
831 * StyleReader was completed. Those reads were premature: the values depend not
832 * only on the style and theme, but also on the StyleReader's own state (such as
833 * its controlType, checked state, and so on), which may not have been fully
834 * initialized at that time. Now that all of it is in place, emit change signals
835 * for all properties to force the bindings to re-read them. */
836 emitChangedForAllStyleProperties(EmitFlag::AllProperties);
837 }
838
839 // From now on, we need to be notified when the style or theme changes
840 s_allReaders.append(this);
841
842 m_completed = completed;
843}
844
845bool QQStyleKitReader::transitionsEnabled() const
846{
847 return m_transitionsEnabled;
848}
849
850void QQStyleKitReader::setTransitionsEnabled(bool enabled)
851{
852 if (m_transitionsEnabled == enabled)
853 return;
854 m_transitionsEnabled = enabled;
856
857QQStyleKitStyle *QQStyleKitReader::explicitStyle() const
858{
859 return m_explicitStyle.data();
860}
861
862void QQStyleKitReader::setExplicitStyle(QQStyleKitStyle *style)
863{
864 if (m_explicitStyle == style)
865 return;
866
867 if (m_explicitStyle)
868 m_explicitStyle->m_explicitReaderCount--;
869
870 m_explicitStyle = style;
871
872 if (m_explicitStyle)
873 m_explicitStyle->m_explicitReaderCount++;
874
875 if (!m_explicitStyle || !m_explicitStyle->loaded()) {
876 clearLocalStorage();
877 return;
878 }
879
880 m_effectiveVariationsDirty = true;
881 m_fontDirty = true;
882 clearLocalStorage();
883 rebuildEffectivePalette();
884 rebuildEffectiveFont();
885 emitChangedForAllStyleProperties(EmitFlag::AllProperties);
886}
887
888QQuickPalette *QQStyleKitReader::palette() const
889{
890 return m_palette.data();
892
893void QQStyleKitReader::setPalette(QQuickPalette *palette)
894{
895 if (m_palette == palette)
896 return;
897
898 if (m_palette)
899 QObject::disconnect(m_palette, nullptr, this, nullptr);
900
901 m_palette = palette;
902 emit paletteChanged();
903
904 if (m_palette) {
905 // changed signal will be triggered when any role changes
906 QObject::connect(m_palette, &QQuickPalette::changed,
907 this, &QQStyleKitReader::onPaletteChanged);
908 }
909
910 onPaletteChanged();
911}
912
913QPalette QQStyleKitReader::effectivePalette() const
914{
915 return m_effectivePalette;
916}
917
918void QQStyleKitReader::onPaletteChanged()
919{
920 const QQStyleKitStyle *currentStyle = style();
921 if (!m_completed || !currentStyle || !currentStyle->loaded())
922 return;
923
924 if (rebuildEffectivePalette()) {
925 clearLocalStorage();
926 emitChangedForAllStyleProperties(EmitFlag::Colors);
927 }
928}
930bool QQStyleKitReader::rebuildEffectivePalette()
931{
932 auto mergedPalette = style()->paletteForControlType(this->controlType());
933 const auto stylePaletteResolveMask = mergedPalette.resolveMask();
934 if (m_palette) {
935 // The control palette takes precedence over the style palette
936 const auto controlPalette = m_palette->toQPalette();
937 mergedPalette = controlPalette.resolve(mergedPalette);
938 // Explicitly set the resolve mask to make sure it is not lost during the resolve operation
939 // when the control palette has a resolveMask of 0
940 mergedPalette.setResolveMask(stylePaletteResolveMask | controlPalette.resolveMask());
941 }
942 if (m_effectivePalette == mergedPalette)
943 return false;
944
945 m_effectivePalette = mergedPalette;
946 return true;
947}
948
949QFont QQStyleKitReader::font() const
950{
951 return m_font;
952}
953
954bool QQStyleKitReader::rebuildEffectiveFont()
955{
956 const QQStyleKitStyle *currentStyle = style();
957 if (!currentStyle || !currentStyle->loaded())
958 return false;
959
960 if (!m_fontDirty)
961 return false;
962
963 QFont font = currentStyle->fontForControlType(controlType());
964 const QQStyleKitTextProperties *textProps = global()->text();
965 if (textProps) {
966 if (textProps->isDefined(QQSK::Property::Bold))
967 font.setBold(textProps->bold());
968 if (textProps->isDefined(QQSK::Property::Italic))
969 font.setItalic(textProps->italic());
970 if (textProps->isDefined(QQSK::Property::PointSize))
971 font.setPointSizeF(textProps->pointSize());
972 }
973
974 if (m_font == font)
975 return false;
976
977 m_font = font;
978 emit fontChanged();
979 return true;
980}
981
982QQStyleKitControlProperties *QQStyleKitReader::global() const
983{
984 return &const_cast<QQStyleKitReader *>(this)->m_global;
985}
986
987void QQStyleKitReader::componentComplete()
988{
989 setCompleted(true);
990}
991
992QT_END_NAMESPACE
993
994#include "moc_qqstylekitreader_p.cpp"
Combined button and popup list for selecting options.
static quint64 textFontOverridesSignature(const QQStyleKitTextProperties *t)
static constexpr QLatin1StringView kAlternate1
static constexpr QLatin1StringView kAlternate2