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 Example}.
44
45 \labs
46
47 \sa Style, CustomTheme
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::FlatButton:
56 return QQuickTheme::Button;
57 case QQStyleKitReader::ControlType::CheckBox:
58 return QQuickTheme::CheckBox;
59 case QQStyleKitReader::ControlType::ComboBox:
60 return QQuickTheme::ComboBox;
61 case QQStyleKitReader::ControlType::GroupBox:
62 return QQuickTheme::GroupBox;
63 case QQStyleKitReader::ControlType::ItemDelegate:
64 return QQuickTheme::ItemView;
65 case QQStyleKitReader::ControlType::Label:
66 return QQuickTheme::Label;
67 case QQStyleKitReader::ControlType::RadioButton:
68 return QQuickTheme::RadioButton;
69 case QQStyleKitReader::ControlType::SpinBox:
70 return QQuickTheme::SpinBox;
71 case QQStyleKitReader::ControlType::SwitchControl:
72 return QQuickTheme::Switch;
73 case QQStyleKitReader::ControlType::TabBar:
74 case QQStyleKitReader::ControlType::TabButton:
75 return QQuickTheme::TabBar;
76 case QQStyleKitReader::ControlType::TextArea:
77 return QQuickTheme::TextArea;
78 case QQStyleKitReader::ControlType::TextInput:
79 case QQStyleKitReader::ControlType::TextField:
80 return QQuickTheme::TextField;
81 case QQStyleKitReader::ControlType::ToolBar:
82 case QQStyleKitReader::ControlType::ToolButton:
83 case QQStyleKitReader::ControlType::ToolSeparator:
84 return QQuickTheme::ToolBar;
85 default:
86 return QQuickTheme::System;
87 }
88 Q_UNREACHABLE();
89}
90
91QQStyleKitTheme::QQStyleKitTheme(QObject *parent)
93{
94}
95
96QQStyleKitStyle *QQStyleKitTheme::style() const
97{
98 QObject *parentObj = parent();
99 if (!parentObj)
100 return nullptr;
101 Q_ASSERT(qobject_cast<QQStyleKitStyle *>(parentObj));
102 return static_cast<QQStyleKitStyle *>(parentObj);
103}
104
105QPalette QQStyleKitTheme::paletteForControlType(QQStyleKitExtendableControlType type) const
106{
107 const QQuickTheme::Scope scope = scopeForType(type);
108 return m_effectivePalettes[int(scope)];
109}
110
111QFont QQStyleKitTheme::fontForControlType(QQStyleKitExtendableControlType type) const
112{
113 const QQuickTheme::Scope scope = scopeForType(type);
114 return m_effectiveFonts[int(scope)];
115}
116
117void QQStyleKitTheme::updateThemePalettes()
118{
119 const QQStyleKitPalette *pals = palettes();
120 if (!pals)
121 return;
122
123 // Collect palette fallback chain
124 QVector<const QQStyleKitPalette *> fbChain;
125 for (auto *fb = pals; fb; fb = fb->fallbackPalette())
126 fbChain.append(fb);
127
128 auto resolveFromPaletteChain = [&](QQuickTheme::Scope scope,
129 QQuickPalette* (QQStyleKitPalette::*getter)() const) -> QPalette
130 {
131 QPalette result;
132
133 // Apply from farthest fallback -> local
134 for (int i = fbChain.size() - 1; i >= 0; --i) {
135 const QQStyleKitPalette *fb = fbChain[i];
136 // Apply system palette first as a base, and override with scope-specific palette
137 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
138 if (auto *sys = fb->system())
139 result = sys->toQPalette().resolve(result);
140 }
141 if (fb->isSet(scope)) {
142 if (auto *p = (fb->*getter)())
143 result = p->toQPalette().resolve(result);
144 }
145 }
146
147 return result;
148 };
149
150 auto setResolved = [&](QQuickPalette* (QQStyleKitPalette::*getter)() const,
151 QQuickTheme::Scope scope)
152 {
153 m_effectivePalettes[int(scope)] = resolveFromPaletteChain(scope, getter);
154 };
155
156 setResolved(&QQStyleKitPalette::system, QQuickTheme::System);
157 setResolved(&QQStyleKitPalette::button, QQuickTheme::Button);
158 setResolved(&QQStyleKitPalette::checkBox, QQuickTheme::CheckBox);
159 setResolved(&QQStyleKitPalette::comboBox, QQuickTheme::ComboBox);
160 setResolved(&QQStyleKitPalette::groupBox, QQuickTheme::GroupBox);
161 setResolved(&QQStyleKitPalette::itemDelegate, QQuickTheme::ItemView);
162 setResolved(&QQStyleKitPalette::label, QQuickTheme::Label);
163 setResolved(&QQStyleKitPalette::radioButton, QQuickTheme::RadioButton);
164 setResolved(&QQStyleKitPalette::spinBox, QQuickTheme::SpinBox);
165 setResolved(&QQStyleKitPalette::tabBar, QQuickTheme::TabBar);
166 setResolved(&QQStyleKitPalette::textArea, QQuickTheme::TextArea);
167 setResolved(&QQStyleKitPalette::textField, QQuickTheme::TextField);
168 setResolved(&QQStyleKitPalette::toolBar, QQuickTheme::ToolBar);
169}
170
171void QQStyleKitTheme::updateThemeFonts()
172{
173 const QQStyleKitFont *fonts = this->fonts();
174 if (!fonts)
175 return;
176
177 // Collect font fallback chain
178 QVector<const QQStyleKitFont *> fbChain;
179 for (auto *fb = fonts; fb; fb = fb->fallbackFont())
180 fbChain.append(fb);
181
182 auto resolveFromFontChain = [&](QQuickTheme::Scope scope) -> QFont
183 {
184 QFont result;
185
186 // Apply from farthest fallback -> local
187 for (int i = fbChain.size() - 1; i >= 0; --i) {
188 const QQStyleKitFont *fb = fbChain[i];
189 // Apply system font first as a base, and override with scope-specific font
190 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
191 result = fb->fontForScope(QQuickTheme::System).resolve(result);
192 }
193 if (fb->isSet(scope)) {
194 result = fb->fontForScope(scope).resolve(result);
195 }
196 }
197
198 return result;
199 };
200
201 for (int i = 0; i < NScopes; ++i) {
202 const QQuickTheme::Scope scope = static_cast<QQuickTheme::Scope>(i);
203 m_effectiveFonts[i] = resolveFromFontChain(scope);
204 }
205}
206
208{
210 m_completed = true;
211}
212
213QT_END_NAMESPACE
214
215#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