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
qqstylekittheme.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
4#include <QtQuickTemplates2/private/qquicktheme_p.h>
5#include <QtQuick/private/qquickpalette_p.h>
6#include <QtGui/private/qguiapplication_p.h>
7#include <QtGui/qpa/qplatformtheme.h>
8
11
13
14/*!
15 \qmltype Theme
16 \inqmlmodule Qt.labs.StyleKit
17 \inherits AbstractStyle
18 \brief Defines color and style overrides for a color scheme.
19
20 A Theme overrides properties defined in a \l Style to provide a
21 distinct visual appearance for a particular color scheme. A style
22 typically configures structural properties such as implicit size,
23 padding, and radii, while a theme specifies colors, shadows, and
24 other visual attributes. However, this is not a restriction — a
25 theme can override any property that a style can set.
26
27 A style can provide a \l {Style::light}{light} and a
28 \l {Style::dark}{dark} theme. When \l {Style::themeName}{themeName}
29 is set to \c "System" (the default), the active theme follows the
30 \l {QStyleHints::colorScheme}{operating system's color scheme}.
31 You can also create additional themes using \l CustomTheme and
32 \l {Style::themeName}{switch between them} at runtime.
33
34 Properties that are not set in the theme fall back to those defined
35 in the \l Style.
36
37 The following example defines light, dark, and high-contrast
38 themes, each providing different colors for the controls:
39
40 \snippet ThemeSnippets.qml themes
41
42 For a complete example showing themes in action, see the
43 \l {StyleKit Quick Controls Example}.
44
45 \labs
46
47 \sa Style, CustomTheme, {StyleKit Property Resolution}
48*/
49
50static QQuickTheme::Scope scopeForType(QQStyleKitExtendableControlType type)
51{
52 switch (type) {
53 case QQStyleKitReader::ControlType::AbstractButton:
54 case QQStyleKitReader::ControlType::Button:
55 case QQStyleKitReader::ControlType::DelayButton:
56 case QQStyleKitReader::ControlType::FlatButton:
57 case QQStyleKitReader::ControlType::RoundButton:
58 return QQuickTheme::Button;
59 case QQStyleKitReader::ControlType::CheckBox:
60 return QQuickTheme::CheckBox;
61 case QQStyleKitReader::ControlType::CheckDelegate:
62 case QQStyleKitReader::ControlType::RadioDelegate:
63 case QQStyleKitReader::ControlType::SwipeDelegate:
64 case QQStyleKitReader::ControlType::SwitchDelegate:
65 return QQuickTheme::ListView;
66 case QQStyleKitReader::ControlType::ComboBox:
67 return QQuickTheme::ComboBox;
68 case QQStyleKitReader::ControlType::GroupBox:
69 return QQuickTheme::GroupBox;
70 case QQStyleKitReader::ControlType::HeaderViewDelegate:
71 case QQStyleKitReader::ControlType::ItemDelegate:
72 case QQStyleKitReader::ControlType::TableViewDelegate:
73 case QQStyleKitReader::ControlType::TreeViewDelegate:
74 return QQuickTheme::ItemView;
75 case QQStyleKitReader::ControlType::Label:
76 return QQuickTheme::Label;
77 case QQStyleKitReader::ControlType::MenuBar:
78 case QQStyleKitReader::ControlType::MenuBarItem:
79 return QQuickTheme::MenuBar;
80 case QQStyleKitReader::ControlType::Menu:
81 case QQStyleKitReader::ControlType::MenuItem:
82 case QQStyleKitReader::ControlType::MenuSeparator:
83 return QQuickTheme::Menu;
84 case QQStyleKitReader::ControlType::RadioButton:
85 return QQuickTheme::RadioButton;
86 case QQStyleKitReader::ControlType::SpinBox:
87 return QQuickTheme::SpinBox;
88 case QQStyleKitReader::ControlType::SwitchControl:
89 return QQuickTheme::Switch;
90 case QQStyleKitReader::ControlType::TabBar:
91 case QQStyleKitReader::ControlType::TabButton:
92 return QQuickTheme::TabBar;
93 case QQStyleKitReader::ControlType::TextArea:
94 return QQuickTheme::TextArea;
95 case QQStyleKitReader::ControlType::TextInput:
96 case QQStyleKitReader::ControlType::TextField:
97 return QQuickTheme::TextField;
98 case QQStyleKitReader::ControlType::ToolBar:
99 case QQStyleKitReader::ControlType::ToolButton:
100 case QQStyleKitReader::ControlType::ToolSeparator:
101 return QQuickTheme::ToolBar;
102 case QQStyleKitReader::ControlType::ToolTip:
103 return QQuickTheme::ToolTip;
104 case QQStyleKitReader::ControlType::Tumbler:
105 return QQuickTheme::Tumbler;
106 default:
107 return QQuickTheme::System;
108 }
109 Q_UNREACHABLE();
110}
111
112QQStyleKitTheme::QQStyleKitTheme(QObject *parent)
114{
115}
116
117QQStyleKitStyle *QQStyleKitTheme::style() const
118{
119 QObject *parentObj = parent();
120 if (!parentObj)
121 return nullptr;
122 Q_ASSERT(qobject_cast<QQStyleKitStyle *>(parentObj));
123 return static_cast<QQStyleKitStyle *>(parentObj);
124}
125
126QPalette QQStyleKitTheme::paletteForControlType(QQStyleKitExtendableControlType type) const
127{
128 const QQuickTheme::Scope scope = scopeForType(type);
129 return m_effectivePalettes[int(scope)];
130}
131
132QFont QQStyleKitTheme::fontForControlType(QQStyleKitExtendableControlType type) const
133{
134 const QQuickTheme::Scope scope = scopeForType(type);
135 return m_effectiveFonts[int(scope)];
136}
137
138void QQStyleKitTheme::updateThemePalettes()
139{
140 const QQStyleKitPalette *pals = palettes();
141 if (!pals)
142 return;
143
144 // Collect palette fallback chain
145 QVector<const QQStyleKitPalette *> fbChain;
146 for (auto *fb = pals; fb; fb = fb->fallbackPalette())
147 fbChain.append(fb);
148
149 auto resolveFromPaletteChain = [&](QQuickTheme::Scope scope,
150 QQuickPalette* (QQStyleKitPalette::*getter)() const) -> QPalette
151 {
152 QPalette result;
153
154 // Apply from farthest fallback -> local
155 for (int i = fbChain.size() - 1; i >= 0; --i) {
156 const QQStyleKitPalette *fb = fbChain[i];
157 // Apply system palette first as a base, and override with scope-specific palette
158 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
159 if (auto *sys = fb->system())
160 result = sys->toQPalette().resolve(result);
161 }
162 if (fb->isSet(scope)) {
163 if (auto *p = (fb->*getter)())
164 result = p->toQPalette().resolve(result);
165 }
166 }
167
168 return result;
169 };
170
171 auto setResolved = [&](QQuickPalette* (QQStyleKitPalette::*getter)() const,
172 QQuickTheme::Scope scope)
173 {
174 m_effectivePalettes[int(scope)] = resolveFromPaletteChain(scope, getter);
175 };
176
177 setResolved(&QQStyleKitPalette::system, QQuickTheme::System);
178 setResolved(&QQStyleKitPalette::button, QQuickTheme::Button);
179 setResolved(&QQStyleKitPalette::checkBox, QQuickTheme::CheckBox);
180 setResolved(&QQStyleKitPalette::comboBox, QQuickTheme::ComboBox);
181 setResolved(&QQStyleKitPalette::groupBox, QQuickTheme::GroupBox);
182 setResolved(&QQStyleKitPalette::itemDelegate, QQuickTheme::ItemView);
183 setResolved(&QQStyleKitPalette::label, QQuickTheme::Label);
184 setResolved(&QQStyleKitPalette::radioButton, QQuickTheme::RadioButton);
185 setResolved(&QQStyleKitPalette::spinBox, QQuickTheme::SpinBox);
186 setResolved(&QQStyleKitPalette::tabBar, QQuickTheme::TabBar);
187 setResolved(&QQStyleKitPalette::textArea, QQuickTheme::TextArea);
188 setResolved(&QQStyleKitPalette::textField, QQuickTheme::TextField);
189 setResolved(&QQStyleKitPalette::toolBar, QQuickTheme::ToolBar);
190}
191
192void QQStyleKitTheme::updateThemeFonts()
193{
194 const QQStyleKitFont *fonts = this->fonts();
195 if (!fonts)
196 return;
197
198 // Collect font fallback chain
199 QVector<const QQStyleKitFont *> fbChain;
200 for (auto *fb = fonts; fb; fb = fb->fallbackFont())
201 fbChain.append(fb);
202
203 // QFont::resolve() keeps only the left-hand operand's resolve mask.
204 // Restore the mask explicitly, or every attribute contributed by an
205 // earlier layer of the chain is dropped again by font resolution.
206 auto merge = [](const QFont &a, const QFont &b) {
207 QFont merged = a.resolve(b);
208 merged.setResolveMask(b.resolveMask() | a.resolveMask());
209 return merged;
210 };
211
212 auto resolveFromFontChain = [&](QQuickTheme::Scope scope) -> QFont
213 {
214 QFont result;
215
216 // Apply from farthest fallback -> local
217 for (int i = fbChain.size() - 1; i >= 0; --i) {
218 const QQStyleKitFont *fb = fbChain[i];
219 // Apply system font first as a base, and override with scope-specific font
220 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
221 result = merge(fb->fontForScope(QQuickTheme::System), result);
222 }
223 if (fb->isSet(scope)) {
224 result = merge(fb->fontForScope(scope), result);
225 }
226 }
227
228 return result;
229 };
230
231 for (int i = 0; i < NScopes; ++i) {
232 const QQuickTheme::Scope scope = static_cast<QQuickTheme::Scope>(i);
233 m_effectiveFonts[i] = resolveFromFontChain(scope);
234 }
235}
236
238{
240 m_completed = true;
241}
242
243QT_END_NAMESPACE
244
245#include "moc_qqstylekittheme_p.cpp"
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
QQuickPalette * groupBox() const
QQuickPalette * checkBox() const
QQuickPalette * toolBar() const
QQuickPalette * textField() const
QQuickPalette * spinBox() const
QQuickPalette * system() const
QQuickPalette * comboBox() const
QQuickPalette * itemDelegate() const
QQuickPalette * button() const
QQuickPalette * radioButton() const
QQuickPalette * textArea() const
QQuickPalette * tabBar() const
QQuickPalette * label() const
QQStyleKitPalette * palettes()
QQStyleKitFont * fonts()
QQStyleKitStyle * style() const
QFont fontForControlType(QQStyleKitExtendableControlType type) const
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
QPalette paletteForControlType(QQStyleKitExtendableControlType type) const
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QQuickTheme::Scope scopeForType(QQStyleKitExtendableControlType type)
\qmltype Theme \inqmlmodule Qt.labs.StyleKit \inherits AbstractStyle