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
qstylekitstyle_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 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// Qt-Security score:significant reason:default
4
5#ifndef QSTYLEKITSTYLE_P_H
6#define QSTYLEKITSTYLE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qset.h>
20#include <QtWidgets/private/qtwidgetsglobal_p.h>
21#include <QtWidgets/qcommonstyle.h>
22#include <QtWidgets/private/qcommonstyle_p.h>
23#include <QtQml/qqmlengine.h>
24#include "qstylekitstyle.h"
25#include <QtLabsStyleKit/private/qqstylekitreader_p.h>
26
27#include <variant>
28
29QT_BEGIN_NAMESPACE
30
31class QAction;
32class QQuickTransition;
33class QQStyleKitDelegateProperties;
35class QQStyleKitImageProperties;
38class QQStyleKitReader;
39class QQStyleKitStyle;
41{
42 Q_DECLARE_PUBLIC(QStyleKitStyle)
43
44 // Stores all the metrics needed to draw and size a control.
45 // Cached per-control type and state so that we don't have to do multiple
46 // lookups on the reader for each property when drawing
47 struct ControlMetrics
48 {
59 int spacing;
60 };
61 // Key to identify a sub-element inside a widget: item-view cells,
62 // a menu/menu-bar item, etc.
63 // Needed for animating sub-elements
64 struct SubElementKey
65 {
66 const QWidget *widget = nullptr;
67 // used for item-view cells
69 {
70 const void *model = nullptr;
72 int row = -1;
73 int column = -1;
74 friend bool operator==(const ItemViewCell &l, const ItemViewCell &r) noexcept
75 {
76 return l.model == r.model && l.internalId == r.internalId
77 && l.row == r.row && l.column == r.column;
78 }
79 };
80 // used for menu/menu-bar items
81 struct Action
82 {
83 const QAction *action = nullptr;
84 friend bool operator==(const Action &l, const Action &r) noexcept
85 { return l.action == r.action; }
86 };
87 std::variant<std::monostate, ItemViewCell, Action> id;
88
89 bool isValid() const noexcept
90 { return widget != nullptr && !std::holds_alternative<std::monostate>(id); }
91 friend bool operator==(const SubElementKey &l, const SubElementKey &r) noexcept
92 { return l.widget == r.widget && l.id == r.id; }
93 friend size_t qHash(const SubElementKey &k, size_t seed = 0) noexcept
94 {
95 if (auto *c = std::get_if<ItemViewCell>(&k.id))
96 return qHashMulti(seed, k.widget, 1, c->model, c->internalId, c->row, c->column);
97 if (auto *a = std::get_if<Action>(&k.id))
98 return qHashMulti(seed, k.widget, 2, a->action);
99 return qHashMulti(seed, k.widget, 0);
100 }
101 };
102
103 // Key for the metrics cache
104 struct MetricsCacheKey
105 {
106 QQStyleKitReader::ControlType type;
107 QQSK::State state;
108
109 friend bool operator==(MetricsCacheKey lhs, MetricsCacheKey rhs) noexcept
110 { return lhs.type == rhs.type && lhs.state == rhs.state; }
111 friend size_t qHash(MetricsCacheKey key, size_t seed = 0) noexcept
112 { return qHashMulti(seed, key.type, key.state); }
113 };
114
115 // Stores the resolved properties for a control in a specific state.
116 // Used by painters to read properties when drawing.
117 // The reader stays internal and we expose only the property accessors
118 // to avoid mutating the reader and reconfigure resolution mid-paint.
119 struct QQStyleKitResolved
120 {
121 const QQStyleKitReader *reader = nullptr;
122 const ControlMetrics *metrics = nullptr;
123 const QWidget *widget = nullptr;
124
125 bool isValid() const { return reader && metrics; }
126 const QQStyleKitTextProperties *text() const;
127 const QQStyleKitDelegateProperties *background() const;
128 const QQStyleKitHandleProperties *handle() const;
129 const QQStyleKitIndicatorWithSubTypes *indicator() const;
130 QFont font() const;
131 };
132 // Stores the resolved properties for layout purposes.
133 // Accessors read static values via the shared reader (transitions disabled),
134 // so layout never depends on animation state.
135 struct QQStyleKitLayoutResolved
136 {
137 const ControlMetrics *metrics = nullptr;
138 const QQStyleKitControlProperties *staticProps = nullptr;
139 QFont staticFont;
140
141 bool isValid() const { return metrics; }
142 const QQStyleKitTextProperties *text() const;
143 const QQStyleKitDelegateProperties *background() const;
144 const QQStyleKitHandleProperties *handle() const;
145 const QQStyleKitIndicatorWithSubTypes *indicator() const;
146 QFont font() const { return staticFont; }
147 };
148
149 // Copied from qstylesheetstyle_p.h
150 template <typename T>
151 struct Tampered {
152 T oldWidgetValue;
153 decltype(std::declval<T>().resolveMask()) resolveMask;
154
155 // only call this function on an rvalue *this (it mangles oldWidgetValue)
156 T reverted(T current) && {
157 oldWidgetValue.setResolveMask(oldWidgetValue.resolveMask() & resolveMask);
158 current.setResolveMask(current.resolveMask() & ~resolveMask);
159 current.resolve(oldWidgetValue);
160 current.setResolveMask(current.resolveMask() | oldWidgetValue.resolveMask());
161 return current;
162 }
163 };
164
165private:
166 QStyleKitStylePrivate();
167
168 bool loadStyle();
169 void updateStyle();
170
171 QQStyleKitStyle *ensureDefaultStyle();
172 QQStyleKitStyle *effectiveStyle() const;
173
174 void unsetStyleFont(QWidget *widget);
175 void setStyleFont(QWidget *widget, const QFont &styleFont);
176 void refreshStyleFont(QWidget *widget);
177
178 void unsetStylePalette(QWidget *widget);
179 void setStylePalette(QWidget *widget, const QPalette &stylePalette) const;
180 void refreshStylePalette(QWidget *widget);
181
182 QQStyleKitReader *readerForWidget(const QWidget *widget) const;
183 void cleanupWidgetReader(const QWidget *widget) const;
184 void onWidgetDestroyed(QObject *w) const;
185
186 QQStyleKitReader *ensureSharedReader() const;
187 QQStyleKitReader *ensureSubElementReader() const;
188
189 static SubElementKey subElementKeyForOption(const QStyleOption *opt, const QWidget *widget);
190 QQStyleKitReader *startSubElementTransition(const SubElementKey &key,
191 QQStyleKitReader::ControlType type,
192 QQSK::State fromState, QQSK::State toState,
193 QQuickTransition *transition) const;
194 void cleanupSubElements(const QWidget *widget) const;
195 void clearAllSubElements() const;
196
197 QQSK::State resolvedStateFor(QQStyleKitReader::ControlType type, QStyle::State state,
198 const QWidget *widget = nullptr) const;
199
200 QQStyleKitResolved resolve(const QWidget *w, QQStyleKitReader::ControlType type, QStyle::State state) const;
201 QQStyleKitResolved resolveSubElement(const QWidget *w, const QStyleOption *opt,
202 QQStyleKitReader::ControlType type, QStyle::State state,
203 bool track = true) const;
204
205 QQStyleKitLayoutResolved resolveLayout(QQStyleKitReader::ControlType type,
206 QStyle::State state) const;
207
208 const ControlMetrics &metricsFor(QQStyleKitReader::ControlType type, QQSK::State state) const;
209 void clearMetricsCache();
210
211 ControlMetrics metricsForReader(QQStyleKitReader *reader) const;
212
213 void drawControlIndicator(const QQStyleKitDelegateProperties *indicator, const QRectF &rect, QPainter *p) const;
214 void drawControlText(const QQStyleKitTextProperties *textProps, const QFont &font,
215 const QRect &rect, const QString &text, uint textFlags,
216 QPainter *p,
217 Qt::Alignment defaultAlignment = Qt::AlignHCenter | Qt::AlignVCenter) const;
218 void drawStyledItemRect(const QQStyleKitDelegateProperties *, const QRectF &rect, QPainter *p) const;
219 void drawBackground(const QQStyleKitDelegateProperties *, const QRect &rect, QPainter *p) const;
220 void drawStyledItemImage(const QQStyleKitImageProperties *image, const QRectF &rect,
221 qreal opacity, QPainter *p) const;
222
223 QRect getAlignedRectInContainer(const QRect &container, const QSize &indicatorSize,
224 uint alignment, const QMargins &padding,
225 const QMargins &indicatorMargins) const;
226
227 QQmlEngine *qmlEngine = nullptr;
228 QQStyleKitStyle *style = nullptr;
229 QQStyleKitStyle *defaultStyle = nullptr;
230 // Shared reader for static metric reads
231 mutable QQStyleKitReader *sharedReader = nullptr;
232 // Per-widget readers
233 // Separate per-widget readers are used in order to support transitions,
234 // as each reader provides interpolated property values during these transitions.
235 mutable QHash<const QWidget *, QQStyleKitReader *> widgetReaders;
236 // Shared reader for static (ie: not currently animating) sub-element reads
237 mutable QQStyleKitReader *subElementReader = nullptr;
238 // Last transitioned-to state for the sub-element
239 mutable QHash<SubElementKey, QQSK::State> subElementStates;
240 // Transient readers, one per animating sub-element
241 mutable QHash<SubElementKey, QQStyleKitReader *> subElementAnimationReaders;
242 static constexpr int kMaxSubElementAnimationReaders = 64;
243 static constexpr int kMaxSubElementStates = 1024;
244 // Cache of resolved metrics per control type and state
245 mutable QHash<MetricsCacheKey, ControlMetrics> metricsCache;
248 // Widgets whose automatic background fill we disabled
249 QSet<const QWidget *> bgFillDisabledWidgets;
250 QString stylePath;
251};
252
253QT_END_NAMESPACE
254
255#endif // QSTYLEKITSTYLE_P_H
\inmodule QtLabsStyleKit
Combined button and popup list for selecting options.
static QWidget * managedViewport(QWidget *widget)
static qreal resolvedHeight(const QQStyleKitDelegateProperties *element, qreal avilableH)
static QMargins elementMargins(const QQStyleKitDelegateProperties *element)
static uint resolvedAlignment(uint raw, Qt::Alignment hDefault, Qt::Alignment vDefault)
static qreal resolvedWidth(const QQStyleKitDelegateProperties *element, qreal availableW)
static bool isSelfPaintingWidget(const QWidget *widget)
static QUrl urlFromStylePath(const QString &filePath)
static const QWidget * containerWidget(const QWidget *w)
friend bool operator==(const Action &l, const Action &r) noexcept
friend bool operator==(const ItemViewCell &l, const ItemViewCell &r) noexcept