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
qqstylekitcontrol.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
7
9
10int QQStyleKitControlAttached::s_variationCount = 0;
11
12QQStyleKitControl::QQStyleKitControl(QObject *parent)
14{
15}
16
18{
19 const bool isWrite = subclass() == QQSK::Subclass::QQStyleKitState;
20 if (isWrite) {
21 if (!QQStyleKitPropertyResolver::hasLocalStyleProperty(this, QQSK::Property::Variations)) {
22 /* We need to handle both the setter and the getter in the getter, since QML doesn't
23 * use a separate setter for QQmlListProperty. This means that we need to initialize
24 * the storage with an empty QList, since we need to be prepared for the QML engine
25 * inserting elements into it behind our back. A negative side-effect of this logic
26 * is that a simple read of the property for a QQStyleKitState will also accidentally
27 * populate the local storage of that QQStylKitState, and thereby affect propagation.
28 * Note: since Variations is not a part of QQStyleKitProperties, it's not possible,
29 * and there is also no point, in emitting a changed signal globally, since a
30 * QQuickStyleKitReader inherits QQStyleKitProperties and not QQStyleKitControl,
31 * and as such, doesn't have a 'variations' property. */
32 auto *newList = new QList<QQStyleKitVariation *>();
33 setStyleProperty(QQSK::Property::Variations, newList);
34 }
35 }
36
37 /* Read the property, taking propagation into account. Note that since QQmlListProperty takes
38 * a pointer to a QList as argument, we need to store the list as a pointer as well */
39 const QVariant variant = QQStyleKitPropertyResolver::readStyleProperty(this, QQSK::Property::Variations);
40 if (!variant.isValid()) {
41 // Return a read-only list. Trying to change this list from the app has no effect.
42 static auto *emptyList = new QList<QQStyleKitVariation *>();
43 return QQmlListProperty<QQStyleKitVariation>(this, emptyList);
44 }
45
46 const auto value = qvariant_cast<QList<QQStyleKitVariation *> *>(variant);
47 return QQmlListProperty<QQStyleKitVariation>(this, value);
48}
49
50QQStyleKitControlAttached *QQStyleKitControl::qmlAttachedProperties(QObject *object)
51{
52 return new QQStyleKitControlAttached(object);
53}
54
55QVariant QQStyleKitControl::readStyleProperty(PropertyStorageId key) const
56{
57 return m_storage.value(key);
58}
59
60void QQStyleKitControl::writeStyleProperty(PropertyStorageId key, const QVariant &value)
61{
62 m_storage.insert(key, value);
63}
64
66{
67 Q_ASSERT(qobject_cast<QQStyleKitControls *>(parent()));
68 return static_cast<QQStyleKitControls *>(parent());
69}
70
71QQStyleKitExtendableControlType QQStyleKitControl::controlType() const
72{
73 const auto &controlsMap = controls()->m_controls;
74 Q_ASSERT(std::find(controlsMap.begin(), controlsMap.end(), this) != controlsMap.end());
75 return controlsMap.key(const_cast<QQStyleKitControl *>(this));
76}
77
78QQStyleKitControlAttached::QQStyleKitControlAttached(QObject *parent)
79 : QObject(parent)
80{
81}
82
84{
85 return m_variations;
86}
87
88void QQStyleKitControlAttached::setVariations(const QStringList &variations)
89{
90 if (m_variations == variations)
91 return;
92
93 /* As an optimization, we count the number of variations set from the application.
94 * That way, if s_variationCount == 1, for example, and we found a variation while
95 * resolving the effective variations for a specific QQStyleReader, we can stop the search. */
96 s_variationCount++;
97
98 m_variations = variations;
99 emit variationsChanged();
100}
101
103{
104 return m_controlType;
105}
106
107void QQStyleKitControlAttached::setControlType(QQStyleKitExtendableControlType type)
108{
109 if (m_controlType == type)
110 return;
111
112 m_controlType = type;
113 emit controlTypeChanged();
114}
115
116QT_END_NAMESPACE
117
118#include "moc_qqstylekitcontrol_p.cpp"
void setControlType(QQStyleKitExtendableControlType type)
QStringList variations() const
void setVariations(const QStringList &variations)
QQStyleKitExtendableControlType controlType()
QQmlListProperty< QQStyleKitVariation > variations()
Combined button and popup list for selecting options.