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>
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
122
123
124
125
126
127
128
129
130
131
134
135
136
137
138
139
140
141
142
143
146
147
148
149
150
151
152
153
154
155
158
159
160
161
162
163
164
165
166
167
170
171
172
173
174
175
176
177
178
179
182
183
184
185
186
187
188
189
190
191
194
195
196
197
198
199
200
201
202
203
206
207
208
209
210
211
212
213
214
217
218
219
220
221
222
223
224
225
226
227
228
229
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
251using namespace Qt::StringLiterals;
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);
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);
276 if (t->isDefined(QQSK::Property::PointSize)) {
277 sig |= (quint64(1) << 4);
278 const qreal ps = t->styleProperty<qreal>(QQSK::Property::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));
288QList<QQStyleKitReader *> QQStyleKitReader::s_allReaders;
289QQStyleKitReader::PropertyChangesComponents QQStyleKitReader::s_propertyChangesComponents;
291QQStyleKitReader::QQStyleKitReader(QObject *parent)
292 : QQStyleKitControlProperties(QQSK::PropertyGroup::Control, parent)
293 , m_dontEmitChangedSignals(
false)
294 , m_effectiveVariationsDirty(
true)
295 , m_transitionsEnabled(
true)
297 , m_global(QQStyleKitControlProperties(QQSK::PropertyGroup::GlobalFlag,
this))
301QQStyleKitReader::~QQStyleKitReader()
303 s_allReaders.removeOne(
this);
305 m_explicitStyle->m_explicitReaderCount--;
308QQuickStateGroup *QQStyleKitReader::stateGroup()
314
315
316
317 const auto *stylePtr = style();
320 m_stateGroup =
new QQuickStateGroup(
this);
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);
331 QQmlComponent *controlComp = createControlChangesComponent();
332 instantiatePropertyChanges(controlComp);
337QQmlComponent *QQStyleKitReader::createControlChangesComponent()
const
339 QQmlEngine *engine = qmlEngine(style());
340 auto key = PropertyChangesComponents::key_type{engine, u"control"_s};
341 if (
auto r = s_propertyChangesComponents.value(key,
nullptr))
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 )");
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);
377QQmlComponent *QQStyleKitReader::createDelegateChangesComponent(
const QString &delegateName)
const
379 QQmlEngine *engine = qmlEngine(style());
380 auto key = PropertyChangesComponents::key_type{engine, delegateName};
381 if (
auto r = s_propertyChangesComponents.value(key,
nullptr))
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 )");
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);
436void QQStyleKitReader::instantiatePropertyChanges(QQmlComponent *comp)
438 QObject *obj = comp->create(qmlContext(
this));
439 auto *propertyChanges = qobject_cast<QQuickPropertyChanges *>(obj);
440 Q_ASSERT(propertyChanges);
443 propertyChanges->setObject(
this);
445
446
447
448
449
450 propertyChanges->setIsExplicit(
true);
452
453
454 propertyChanges->setRestoreEntryValues(
false);
457 for (QQuickState *state : stateGroup()->states()) {
458 auto changesProp = state->changes();
459 changesProp.append(&changesProp, propertyChanges);
463void QQStyleKitReader::maybeTrackDelegates()
466 [
this](QQStyleKitDelegateProperties *delegate, QQSK::Delegate type,
const QString &delegatePath){
467 if (m_trackedDelegates.testFlag(type)) {
471 if (!delegate->visible()) {
473
474
478
479
480
481
482 m_trackedDelegates.setFlag(type);
483 QQmlComponent *comp = createDelegateChangesComponent(delegatePath);
484 instantiatePropertyChanges(comp);
488void QQStyleKitReader::updateControl(
bool applyStateChange)
490 const QQStyleKitStyle *currentStyle = style();
491 if (!m_completed || !currentStyle || !currentStyle->loaded())
494 if (applyStateChange) {
496
497
498
499
500
501
502
503
504
505
506
508 maybeTrackDelegates();
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);
522 switch (m_alternateState) {
523 case AlternateState::Alternate1:
524 m_alternateState = AlternateState::Alternate2;
525 stateGroup()->setState(kAlternate2);
527 case AlternateState::Alternate2:
528 m_alternateState = AlternateState::Alternate1;
529 stateGroup()->setState(kAlternate1);
542 auto textOverrideSig = textFontOverridesSignature(global()->text());
543 if (m_lastTextFontOverridesSignature != textOverrideSig)
545 m_lastTextFontOverridesSignature = textOverrideSig;
546 rebuildEffectiveFont();
549void QQStyleKitReader::resetReadersForStyle(
const QQStyleKitStyle *style)
551 if (style != QQStyleKitStyle::current() && !style->hasExplicitReaders()) {
553
554
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);
570void QQStyleKitReader::populateLocalStorage()
574
575
581 if (!m_storage.isEmpty())
583 const auto *stylePtr = style();
584 if (!stylePtr || !stylePtr->loaded())
588
589
590
595
597
598
599
600
601 m_dontEmitChangedSignals =
true;
603 m_dontEmitChangedSignals =
false;
606void QQStyleKitReader::clearLocalStorage()
609
610
611
615QQSK::State QQStyleKitReader::controlState()
const
617 QQSK::State effectiveState = m_state;
621 effectiveState &= ~(QQSK::StateFlag::Pressed |
622 QQSK::StateFlag::Hovered |
623 QQSK::StateFlag::Highlighted |
624 QQSK::StateFlag::Focused |
625 QQSK::StateFlag::Hovered);
628 if (effectiveState == QQSK::StateFlag::Unspecified)
629 effectiveState.setFlag(QQSK::StateFlag::Normal);
631 return effectiveState;
634QVariant QQStyleKitReader::readStyleProperty(PropertyStorageId key)
const
636 return m_storage.value(key);
639void QQStyleKitReader::writeStyleProperty(PropertyStorageId key,
const QVariant &value)
641 m_storage.insert(key, value);
644bool QQStyleKitReader::dontEmitChangedSignals()
const
646 return m_dontEmitChangedSignals;
649QQStyleKitExtendableControlType QQStyleKitReader::controlType()
const
654void QQStyleKitReader::setControlType(QQStyleKitExtendableControlType type)
660 populateLocalStorage();
661 emit controlTypeChanged();
666QQStyleKitReader::ControlType QQStyleKitReader::typeAsControlType()
const
669
671 return ControlType(m_type);
675bool QQStyleKitReader::hovered()
const
677 return m_state.testFlag(QQSK::StateFlag::Hovered);
680void QQStyleKitReader::setHovered(
bool hovered)
682 if (hovered == QQStyleKitReader::hovered())
685 populateLocalStorage();
686 m_state.setFlag(QQSK::StateFlag::Hovered, hovered);
687 emit hoveredChanged();
691bool QQStyleKitReader::enabled()
const
693 return !m_state.testFlag(QQSK::StateFlag::Disabled);
696void QQStyleKitReader::setEnabled(
bool enabled)
698 if (enabled == QQStyleKitReader::enabled())
701 populateLocalStorage();
702 m_state.setFlag(QQSK::StateFlag::Disabled, !enabled);
703 emit enabledChanged();
707bool QQStyleKitReader::focused()
const
709 return m_state.testFlag(QQSK::StateFlag::Focused);
712void QQStyleKitReader::setFocused(
bool focused)
714 if (focused == QQStyleKitReader::focused())
717 populateLocalStorage();
718 m_state.setFlag(QQSK::StateFlag::Focused, focused);
719 emit focusedChanged();
723bool QQStyleKitReader::checked()
const
725 return m_state.testFlag(QQSK::StateFlag::Checked);
728void QQStyleKitReader::setChecked(
bool checked)
730 if (checked == QQStyleKitReader::checked())
733 populateLocalStorage();
734 m_state.setFlag(QQSK::StateFlag::Checked, checked);
735 emit checkedChanged();
739bool QQStyleKitReader::pressed()
const
741 return m_state.testFlag(QQSK::StateFlag::Pressed);
744void QQStyleKitReader::setPressed(
bool pressed)
746 if (pressed == QQStyleKitReader::pressed())
749 populateLocalStorage();
750 m_state.setFlag(QQSK::StateFlag::Pressed, pressed);
751 emit pressedChanged();
755bool QQStyleKitReader::vertical()
const
757 return m_state.testFlag(QQSK::StateFlag::Vertical);
760void QQStyleKitReader::setVertical(
bool vertical)
762 if (vertical == QQStyleKitReader::vertical())
765 populateLocalStorage();
766 m_state.setFlag(QQSK::StateFlag::Vertical, vertical);
767 emit verticalChanged();
771bool QQStyleKitReader::highlighted()
const
773 return m_state.testFlag(QQSK::StateFlag::Highlighted);
776void QQStyleKitReader::setHighlighted(
bool highlighted)
778 if (highlighted == QQStyleKitReader::highlighted())
781 populateLocalStorage();
782 m_state.setFlag(QQSK::StateFlag::Highlighted, highlighted);
783 emit highlightedChanged();
787void QQStyleKitReader::setControlTypeAndState(QQStyleKitExtendableControlType controlType, QQSK::State flags,
788 bool applyStateChange)
792 const bool typeChanged = m_type != controlType;
793 const QQSK::State stateChanged = m_state ^ flags;
795 if (!typeChanged && !stateChanged)
798 if (applyStateChange)
799 populateLocalStorage();
801 m_type = controlType;
804 updateControl(applyStateChange);
807QObject *QQStyleKitReader::target()
const
809 return m_target.data();
812void QQStyleKitReader::setTarget(QObject *target)
815
816
817
818
822void QQStyleKitReader::setCompleted(
bool completed)
824 const QQStyleKitStyle *currentStyle = style();
825 if (currentStyle && currentStyle->loaded()) {
827 rebuildEffectiveFont();
828 rebuildEffectivePalette();
834
836 emitChangedForAllStyleProperties(EmitFlag::AllProperties);
840 s_allReaders.append(
this);
842 m_completed = completed;
845bool QQStyleKitReader::transitionsEnabled()
const
847 return m_transitionsEnabled;
850void QQStyleKitReader::setTransitionsEnabled(
bool enabled)
852 if (m_transitionsEnabled == enabled)
854 m_transitionsEnabled = enabled;
857QQStyleKitStyle *QQStyleKitReader::explicitStyle()
const
859 return m_explicitStyle.data();
862void QQStyleKitReader::setExplicitStyle(QQStyleKitStyle *style)
864 if (m_explicitStyle == style)
868 m_explicitStyle->m_explicitReaderCount--;
870 m_explicitStyle = style;
873 m_explicitStyle->m_explicitReaderCount++;
875 if (!m_explicitStyle || !m_explicitStyle->loaded()) {
880 m_effectiveVariationsDirty =
true;
883 rebuildEffectivePalette();
884 rebuildEffectiveFont();
885 emitChangedForAllStyleProperties(EmitFlag::AllProperties);
888QQuickPalette *QQStyleKitReader::palette()
const
890 return m_palette.data();
893void QQStyleKitReader::setPalette(QQuickPalette *palette)
895 if (m_palette == palette)
899 QObject::disconnect(m_palette,
nullptr,
this,
nullptr);
902 emit paletteChanged();
906 QObject::connect(m_palette, &QQuickPalette::changed,
907 this, &QQStyleKitReader::onPaletteChanged);
913QPalette QQStyleKitReader::effectivePalette()
const
915 return m_effectivePalette;
918void QQStyleKitReader::onPaletteChanged()
920 const QQStyleKitStyle *currentStyle = style();
921 if (!m_completed || !currentStyle || !currentStyle->loaded())
924 if (rebuildEffectivePalette()) {
926 emitChangedForAllStyleProperties(EmitFlag::Colors);
930bool QQStyleKitReader::rebuildEffectivePalette()
932 auto mergedPalette = style()->paletteForControlType(
this->controlType());
933 const auto stylePaletteResolveMask = mergedPalette.resolveMask();
936 const auto controlPalette = m_palette->toQPalette();
937 mergedPalette = controlPalette.resolve(mergedPalette);
940 mergedPalette.setResolveMask(stylePaletteResolveMask | controlPalette.resolveMask());
942 if (m_effectivePalette == mergedPalette)
945 m_effectivePalette = mergedPalette;
949QFont QQStyleKitReader::font()
const
954bool QQStyleKitReader::rebuildEffectiveFont()
956 const QQStyleKitStyle *currentStyle = style();
957 if (!currentStyle || !currentStyle->loaded())
963 QFont font = currentStyle->fontForControlType(controlType());
964 const QQStyleKitTextProperties *textProps = global()->text();
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());
982QQStyleKitControlProperties *QQStyleKitReader::global()
const
984 return &
const_cast<QQStyleKitReader *>(
this)->m_global;
987void QQStyleKitReader::componentComplete()
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