1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qqstylekit_p.h"
#include "qqstylekitstyle_p.h"
#include "qqstylekittheme_p.h"
#include "qqstylekitcustomtheme_p.h"
#include "qqstylekitcontrolproperties_p.h"
#include "qqstylekitpropertyresolver_p.h"
#include <QtQuickTemplates2/private/qquickdeferredexecute_p_p.h>
#include <QtQml/private/qqmllist_p.h>
#include <QtGui/QGuiApplication>
#include <QtGui/QStyleHints>
QT_BEGIN_NAMESPACE
static const QString kSystem = "System"_L1;
static const QString kLight = "Light"_L1;
static const QString kDark = "Dark"_L1;
QQStyleKitStyle::QQStyleKitStyle(QObject *parent)
: QQStyleKitStyleAndThemeBase(parent)
, m_themeName(kSystem)
{
}
QQStyleKitStyle::~QQStyleKitStyle()
{
if (m_theme)
m_theme->deleteLater();
}
QQuickPalette *QQStyleKitStyle::palette() const
{
if (!m_palette) {
static QQuickPalette placeholder;
return &placeholder;
}
return const_cast<QQStyleKitStyle *>(this)->m_palette.get();
}
QQmlComponent *QQStyleKitStyle::light() const
{
return m_light;
}
QQStyleKitStyle *QQStyleKitStyle::fallbackStyle() const
{
if (!m_fallbackStyle) {
auto *self = const_cast<QQStyleKitStyle *>(this);
self->executeFallbackStyle();
}
return m_fallbackStyle;
}
void QQStyleKitStyle::setFallbackStyle(QQStyleKitStyle *fallbackStyle)
{
if (m_fallbackStyle == fallbackStyle)
return;
m_fallbackStyle = fallbackStyle;
emit fallbackStyleChanged();
if (fonts())
fonts()->setFallbackFont(m_fallbackStyle ? m_fallbackStyle->fonts() : nullptr);
if (m_theme && m_theme->fonts())
m_theme->fonts()->setFallbackFont(fonts());
}
void QQStyleKitStyle::setLight(QQmlComponent *lightTheme)
{
if (m_light == lightTheme)
return;
m_light = lightTheme;
emit lightChanged();
}
QQmlComponent *QQStyleKitStyle::dark() const
{
return m_dark;
}
void QQStyleKitStyle::setDark(QQmlComponent *darkTheme)
{
if (m_dark == darkTheme)
return;
m_dark = darkTheme;
emit darkChanged();
}
QQStyleKitTheme *QQStyleKitStyle::theme() const
{
return m_theme;
}
QList<QObject *> QQStyleKitStyle::customThemesAsList()
{
QList<QObject *> list;
for (auto *customTheme : customThemes())
list.append(customTheme);
return list;
}
QList<QQStyleKitCustomTheme *> QQStyleKitStyle::customThemes() const
{
QList<QQStyleKitCustomTheme *> list;
for (auto *obj : children()) {
if (auto *customTheme = qobject_cast<QQStyleKitCustomTheme *>(obj))
list.append(customTheme);
}
return list;
}
void QQStyleKitStyle::parseThemes()
{
m_themeNames = QStringList({kSystem, kLight, kDark});
for (auto *customTheme : customThemes()) {
const QString name = customTheme->name();
if (name.isEmpty())
continue;
m_themeNames << name;
m_customThemeNames << name;
}
emit themeNamesChanged();
emit customThemeNamesChanged();
}
QString QQStyleKitStyle::themeName() const
{
return m_themeName;
}
QStringList QQStyleKitStyle::themeNames() const
{
return m_themeNames;
}
QStringList QQStyleKitStyle::customThemeNames() const
{
return m_customThemeNames;
}
void QQStyleKitStyle::setThemeName(const QString &themeName)
{
if (m_themeName == themeName)
return;
m_themeName = themeName;
recreateTheme();
emit themeNameChanged();
}
void QQStyleKitStyle::recreateTheme()
{
QString effectiveThemeName;
QQmlComponent *effectiveThemeComponent = nullptr;
if (QString::compare(m_themeName, kSystem, Qt::CaseInsensitive) == 0) {
const auto scheme = QGuiApplication::styleHints()->colorScheme();
if (scheme == Qt::ColorScheme::Light) {
effectiveThemeName = kLight;
effectiveThemeComponent = m_light;
}
else if (scheme == Qt::ColorScheme::Dark) {
effectiveThemeName =kDark;
effectiveThemeComponent = m_dark;
}
} else if (QString::compare(m_themeName, kLight, Qt::CaseInsensitive) == 0) {
effectiveThemeName = kLight;
effectiveThemeComponent = m_light;
} else if (QString::compare(m_themeName,kDark, Qt::CaseInsensitive) == 0) {
effectiveThemeName =kDark;
effectiveThemeComponent = m_dark;
} else if (!m_themeName.isEmpty()){
for (auto *customTheme : customThemes()) {
if (QString::compare(m_themeName, customTheme->name(), Qt::CaseInsensitive) == 0) {
effectiveThemeName = customTheme->name();
effectiveThemeComponent = customTheme->theme();
break;
}
}
if (effectiveThemeName.isEmpty())
qmlWarning(this) << "No theme found with name:" << m_themeName;
else if (!effectiveThemeComponent)
qmlWarning(this) << "Custom theme '" << effectiveThemeName << "' has no theme component set";
}
if (m_effectiveThemeName == effectiveThemeName) {
// Switching theme name from e.g "System" to "Light" might not
// actually change the currently effective theme.
emit themeNameChanged();
return;
}
if (m_theme) {
m_theme->deleteLater();
m_theme = nullptr;
}
m_currentThemeComponent = effectiveThemeComponent;
if (effectiveThemeComponent) {
if (effectiveThemeComponent->status() != QQmlComponent::Ready) {
qmlWarning(this) << "failed to create theme '" << effectiveThemeName << "': " << effectiveThemeComponent->errorString();
} else {
/* The 'createThemeInsideStyle' JS function is a work-around since we haven't found
* a way to instantiate a Theme inside the context of a Style from c++. Doing so is
* needed in order to allow custom style properties to be added as children of a
* Style, and at the same time, be able to access them from within a Theme. For
* this to work, the Style also needs to set 'pragma ComponentBehavior: Bound'. */
QVariant themeAsVariant;
QMetaObject::invokeMethod(this, "createThemeInsideStyle", Qt::DirectConnection,
qReturnArg(themeAsVariant), QVariant::fromValue(effectiveThemeComponent));
m_theme = qvariant_cast<QQStyleKitTheme *>(themeAsVariant);
if (!m_theme || !effectiveThemeComponent->errorString().isEmpty()) {
qmlWarning(this) << "failed to create theme '" << effectiveThemeName << "': " << effectiveThemeComponent->errorString();
} else {
m_theme->setParent(this);
}
}
}
if (!m_theme) {
// We always require a theme, even if it's empty
m_theme = new QQStyleKitTheme(this);
m_theme->setObjectName("<empty theme>"_L1);
m_theme->m_completed = true;
} else {
m_theme->setParent(this);
}
if (m_theme && m_theme->fonts())
m_theme->fonts()->setFallbackFont(fonts());
if (this == current()) {
m_theme->updateQuickTheme();
if (m_theme->fonts())
m_theme->fonts()->setFallbackFont(fonts());
if (fonts())
fonts()->setFallbackFont(m_fallbackStyle ? m_fallbackStyle->fonts() : nullptr);
QQStyleKitReader::resetAll();
}
emit themeChanged();
}
QQStyleKitStyle* QQStyleKitStyle::current()
{
return QQStyleKit::qmlAttachedProperties()->style();
}
QFont QQStyleKitStyle::fontForReader(QQStyleKitReader *reader) const
{
switch (reader->type()) {
case QQStyleKitReader::ControlType::Control:
return m_theme->fonts()->systemFont();
case QQStyleKitReader::ControlType::AbstractButton:
case QQStyleKitReader::ControlType::Button:
case QQStyleKitReader::ControlType::FlatButton: {
return m_theme->fonts()->buttonFont();
}
case QQStyleKitReader::ControlType::CheckBox:
return m_theme->fonts()->checkBoxFont();
case QQStyleKitReader::ControlType::ComboBox:
return m_theme->fonts()->comboBoxFont();
case QQStyleKitReader::ControlType::RadioButton:
return m_theme->fonts()->radioButtonFont();
case QQStyleKitReader::ControlType::SpinBox:
return m_theme->fonts()->spinBoxFont();
case QQStyleKitReader::ControlType::SwitchControl:
return m_theme->fonts()->switchControlFont();
case QQStyleKitReader::ControlType::TextInput:
case QQStyleKitReader::ControlType::TextField:
return m_theme->fonts()->textFieldFont();
case QQStyleKitReader::ControlType::TextArea:
return m_theme->fonts()->textAreaFont();
case QQStyleKitReader::ControlType::ItemDelegate:
return m_theme->fonts()->itemViewFont();
default:
return m_theme->fonts()->systemFont();
}
}
bool QQStyleKitStyle::loaded() const
{
/* Before both the style and theme has completed loading
* we return false. This can be used to avoid unnecessary
* property reads when we anyway have to do a full update
* in the end. */
if (!m_completed)
return false;
if (!m_theme || !m_theme->m_completed)
return false;
return true;
}
void QQStyleKitStyle::executeFallbackStyle(bool complete)
{
if (m_fallbackStyle.wasExecuted())
return;
const QString name = "fallbackStyle"_L1;
if (!m_fallbackStyle || complete)
quickBeginDeferred(this, name, m_fallbackStyle);
if (complete)
quickCompleteDeferred(this, name, m_fallbackStyle);
}
void QQStyleKitStyle::setPalette(QQuickPalette *palette)
{
if (m_palette && palette && m_palette->toQPalette() == palette->toQPalette())
return;
m_palette = palette;
QScopedValueRollback rollback(m_isUpdatingPalette, true);
emit paletteChanged();
}
void QQStyleKitStyle::componentComplete()
{
QQStyleKitControls::componentComplete();
/* It's important to set m_completed before creating the theme, otherwise
* styleAndThemeFinishedLoading() will still be false, which will e.g cause
* property reads to return early from QQStyleKitPropertyResolver */
m_completed = true;
executeFallbackStyle(true);
parseThemes();
recreateTheme();
}
QT_END_NAMESPACE
#include "moc_qqstylekitstyle_p.cpp"
|