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 // used for tab-bar tabs
88 struct Tab
89 {
90 int index = -1;
91 friend bool operator==(const Tab &l, const Tab &r) noexcept
92 { return l.index == r.index; }
93 };
94 std::variant<std::monostate, ItemViewCell, Action, Tab> id;
95
96 bool isValid() const noexcept
97 { return widget != nullptr && !std::holds_alternative<std::monostate>(id); }
98 friend bool operator==(const SubElementKey &l, const SubElementKey &r) noexcept
99 { return l.widget == r.widget && l.id == r.id; }
100 friend size_t qHash(const SubElementKey &k, size_t seed = 0) noexcept
101 {
102 if (auto *c = std::get_if<ItemViewCell>(&k.id))
103 return qHashMulti(seed, k.widget, 1, c->model, c->internalId, c->row, c->column);
104 if (auto *a = std::get_if<Action>(&k.id))
105 return qHashMulti(seed, k.widget, 2, a->action);
106 if (auto *t = std::get_if<Tab>(&k.id))
107 return qHashMulti(seed, k.widget, 3, t->index);
108 return qHashMulti(seed, k.widget, 0);
109 }
110 };
111
112 // Key for the metrics cache
113 struct MetricsCacheKey
114 {
115 QQStyleKitReader::ControlType type;
116 QQSK::State state;
117
118 friend bool operator==(MetricsCacheKey lhs, MetricsCacheKey rhs) noexcept
119 { return lhs.type == rhs.type && lhs.state == rhs.state; }
120 friend size_t qHash(MetricsCacheKey key, size_t seed = 0) noexcept
121 { return qHashMulti(seed, key.type, key.state); }
122 };
123
124 // Stores the resolved properties for a control in a specific state.
125 // Used by painters to read properties when drawing.
126 // The reader stays internal and we expose only the property accessors
127 // to avoid mutating the reader and reconfigure resolution mid-paint.
128 struct QQStyleKitResolved
129 {
130 const QQStyleKitReader *reader = nullptr;
131 const ControlMetrics *metrics = nullptr;
132 const QWidget *widget = nullptr;
133
134 bool isValid() const { return reader && metrics; }
135 const QQStyleKitTextProperties *text() const;
136 const QQStyleKitDelegateProperties *background() const;
137 const QQStyleKitHandleProperties *handle() const;
138 const QQStyleKitIndicatorWithSubTypes *indicator() const;
139 QFont font() const;
140 };
141 // Stores the resolved properties for layout purposes.
142 // Accessors read static values via the shared reader (transitions disabled),
143 // so layout never depends on animation state.
144 struct QQStyleKitLayoutResolved
145 {
146 const ControlMetrics *metrics = nullptr;
147 const QQStyleKitControlProperties *staticProps = nullptr;
148 QFont staticFont;
149
150 bool isValid() const { return metrics; }
151 const QQStyleKitTextProperties *text() const;
152 const QQStyleKitDelegateProperties *background() const;
153 const QQStyleKitHandleProperties *handle() const;
154 const QQStyleKitIndicatorWithSubTypes *indicator() const;
155 QFont font() const { return staticFont; }
156 };
157
158 // Copied from qstylesheetstyle_p.h
159 template <typename T>
160 struct Tampered {
161 T oldWidgetValue;
162 decltype(std::declval<T>().resolveMask()) resolveMask;
163
164 // only call this function on an rvalue *this (it mangles oldWidgetValue)
165 T reverted(T current) && {
166 oldWidgetValue.setResolveMask(oldWidgetValue.resolveMask() & resolveMask);
167 current.setResolveMask(current.resolveMask() & ~resolveMask);
168 current.resolve(oldWidgetValue);
169 current.setResolveMask(current.resolveMask() | oldWidgetValue.resolveMask());
170 return current;
171 }
172 };
173
174private:
175 QStyleKitStylePrivate();
176
177 bool loadStyle();
178 void updateStyle();
179
180 QQStyleKitStyle *ensureDefaultStyle();
181 QQStyleKitStyle *effectiveStyle() const;
182
183 void unsetStyleFont(QWidget *widget);
184 void setStyleFont(QWidget *widget, const QFont &styleFont);
185 void refreshStyleFont(QWidget *widget);
186
187 void unsetStylePalette(QWidget *widget);
188 void setStylePalette(QWidget *widget, const QPalette &stylePalette) const;
189 void refreshStylePalette(QWidget *widget);
190
191 QQStyleKitReader *readerForWidget(const QWidget *widget) const;
192 void cleanupWidgetReader(const QWidget *widget) const;
193 void onWidgetDestroyed(QObject *w) const;
194
195 QQStyleKitReader *ensureSharedReader() const;
196 QQStyleKitReader *ensureSubElementReader() const;
197
198 static SubElementKey subElementKeyForOption(const QStyleOption *opt, const QWidget *widget);
199 QQStyleKitReader *startSubElementTransition(const SubElementKey &key,
200 QQStyleKitReader::ControlType type,
201 QQSK::State fromState, QQSK::State toState,
202 QQuickTransition *transition) const;
203 void cleanupSubElements(const QWidget *widget) const;
204 void clearAllSubElements() const;
205
206 QQSK::State resolvedStateFor(QQStyleKitReader::ControlType type, QStyle::State state,
207 const QWidget *widget = nullptr) const;
208
209 QQStyleKitResolved resolve(const QWidget *w, QQStyleKitReader::ControlType type, QStyle::State state) const;
210 QQStyleKitResolved resolveSubElement(const QWidget *w, const QStyleOption *opt,
211 QQStyleKitReader::ControlType type, QStyle::State state,
212 bool track = true) const;
213
214 QQStyleKitLayoutResolved resolveLayout(QQStyleKitReader::ControlType type,
215 QStyle::State state) const;
216
217 const ControlMetrics &metricsFor(QQStyleKitReader::ControlType type, QQSK::State state) const;
218 void clearMetricsCache();
219
220 ControlMetrics metricsForReader(QQStyleKitReader *reader) const;
221
222 void drawControlIndicator(const QQStyleKitDelegateProperties *indicator, const QRectF &rect, QPainter *p) const;
223 void drawControlText(const QQStyleKitTextProperties *textProps, const QFont &font,
224 const QRect &rect, const QString &text, uint textFlags,
225 QPainter *p,
226 Qt::Alignment defaultAlignment = Qt::AlignHCenter | Qt::AlignVCenter) const;
227 void drawStyledItemRect(const QQStyleKitDelegateProperties *, const QRectF &rect, QPainter *p) const;
228 void drawStyledItemContents(const QQStyleKitDelegateProperties *, const QRectF &rect, QPainter *p) const;
229 void drawStyledItemImage(const QQStyleKitImageProperties *image, const QRectF &rect,
230 qreal opacity, QPainter *p) const;
231
232 QRect getAlignedRectInContainer(const QRect &container, const QSize &indicatorSize,
233 uint alignment, const QMargins &padding,
234 const QMargins &indicatorMargins) const;
235
236 QQmlEngine *qmlEngine = nullptr;
237 QQStyleKitStyle *style = nullptr;
238 QQStyleKitStyle *defaultStyle = nullptr;
239 // Shared reader for static metric reads
240 mutable QQStyleKitReader *sharedReader = nullptr;
241 // Per-widget readers
242 // Separate per-widget readers are used in order to support transitions,
243 // as each reader provides interpolated property values during these transitions.
244 mutable QHash<const QWidget *, QQStyleKitReader *> widgetReaders;
245 // Shared reader for static (ie: not currently animating) sub-element reads
246 mutable QQStyleKitReader *subElementReader = nullptr;
247 // Last transitioned-to state for the sub-element
248 mutable QHash<SubElementKey, QQSK::State> subElementStates;
249 // Transient readers, one per animating sub-element
250 mutable QHash<SubElementKey, QQStyleKitReader *> subElementAnimationReaders;
251 static constexpr int kMaxSubElementAnimationReaders = 64;
252 static constexpr int kMaxSubElementStates = 1024;
253 // Cache of resolved metrics per control type and state
254 mutable QHash<MetricsCacheKey, ControlMetrics> metricsCache;
257 // Widgets whose automatic background fill we disabled
258 QSet<const QWidget *> bgFillDisabledWidgets;
259 QString stylePath;
260};
261
262QT_END_NAMESPACE
263
264#endif // QSTYLEKITSTYLE_P_H
\inmodule QtLabsStyleKit
Combined button and popup list for selecting options.
static QWidget * paintTarget(const QWidget *widget)
static bool applyDelegateTransform(QPainter *painter, const QQStyleKitDelegateProperties *element, const QRectF &box)
static QWidget * managedViewport(QWidget *widget)
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 qreal resolvedHeight(const QQStyleKitDelegateProperties *element, qreal availableH)
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
friend bool operator==(const Tab &l, const Tab &r) noexcept