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
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 if (fb->isSet(scope)) {
137 if (auto *p = (fb->*getter)())
138 result = p->toQPalette().resolve(result);
139 }
140 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
141 if (auto *sys = fb->system())
142 result = sys->toQPalette().resolve(result);
143 }
144 }
145
146 return result;
147 };
148
149 auto setResolved = [&](QQuickPalette* (QQStyleKitPalette::*getter)() const,
150 QQuickTheme::Scope scope)
151 {
152 m_effectivePalettes[int(scope)] = resolveFromPaletteChain(scope, getter);
153 };
154
155 setResolved(&QQStyleKitPalette::system, QQuickTheme::System);
156 setResolved(&QQStyleKitPalette::button, QQuickTheme::Button);
157 setResolved(&QQStyleKitPalette::checkBox, QQuickTheme::CheckBox);
158 setResolved(&QQStyleKitPalette::comboBox, QQuickTheme::ComboBox);
159 setResolved(&QQStyleKitPalette::groupBox, QQuickTheme::GroupBox);
160 setResolved(&QQStyleKitPalette::itemDelegate, QQuickTheme::ItemView);
161 setResolved(&QQStyleKitPalette::label, QQuickTheme::Label);
162 setResolved(&QQStyleKitPalette::radioButton, QQuickTheme::RadioButton);
163 setResolved(&QQStyleKitPalette::spinBox, QQuickTheme::SpinBox);
164 setResolved(&QQStyleKitPalette::tabBar, QQuickTheme::TabBar);
165 setResolved(&QQStyleKitPalette::textArea, QQuickTheme::TextArea);
166 setResolved(&QQStyleKitPalette::textField, QQuickTheme::TextField);
167 setResolved(&QQStyleKitPalette::toolBar, QQuickTheme::ToolBar);
168}
169
170void QQStyleKitTheme::updateThemeFonts()
171{
172 const QQStyleKitFont *fonts = this->fonts();
173 if (!fonts)
174 return;
175
176 // Collect font fallback chain
177 QVector<const QQStyleKitFont *> fbChain;
178 for (auto *fb = fonts; fb; fb = fb->fallbackFont())
179 fbChain.append(fb);
180
181 auto resolveFromFontChain = [&](QQuickTheme::Scope scope) -> QFont
182 {
183 QFont result;
184
185 // Apply from farthest fallback -> local
186 for (int i = fbChain.size() - 1; i >= 0; --i) {
187 const QQStyleKitFont *fb = fbChain[i];
188 if (fb->isSet(scope)) {
189 result = fb->fontForScope(scope).resolve(result);
190 }
191 if (scope != QQuickTheme::System && fb->isSet(QQuickTheme::System)) {
192 result = fb->fontForScope(QQuickTheme::System).resolve(result);
193 }
194 }
195
196 return result;
197 };
198
199 for (int i = 0; i < NScopes; ++i) {
200 const QQuickTheme::Scope scope = static_cast<QQuickTheme::Scope>(i);
201 m_effectiveFonts[i] = resolveFromFontChain(scope);
202 }
203}
204
206{
208 m_completed = true;
209}
210
211QT_END_NAMESPACE
212
213#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