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