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
qquickuniversalstyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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
7
8#include <QtCore/qdebug.h>
9#if QT_CONFIG(settings)
10#include <QtCore/qsettings.h>
11#endif
12#include <QtQml/qqmlinfo.h>
13#include <QtQuickControls2/private/qquickstyle_p.h>
14
16
17static QRgb qquickuniversal_light_color(QQuickUniversalStyle::SystemColor role)
18{
19 static const QRgb colors[] = {
20 0xFFFFFFFF, // SystemAltHighColor
21 0x33FFFFFF, // SystemAltLowColor
22 0x99FFFFFF, // SystemAltMediumColor
23 0xCCFFFFFF, // SystemAltMediumHighColor
24 0x66FFFFFF, // SystemAltMediumLowColor
25 0xFF000000, // SystemBaseHighColor
26 0x33000000, // SystemBaseLowColor
27 0x99000000, // SystemBaseMediumColor
28 0xCC000000, // SystemBaseMediumHighColor
29 0x66000000, // SystemBaseMediumLowColor
30 0xFF171717, // SystemChromeAltLowColor
31 0xFF000000, // SystemChromeBlackHighColor
32 0x33000000, // SystemChromeBlackLowColor
33 0x66000000, // SystemChromeBlackMediumLowColor
34 0xCC000000, // SystemChromeBlackMediumColor
35 0xFFCCCCCC, // SystemChromeDisabledHighColor
36 0xFF7A7A7A, // SystemChromeDisabledLowColor
37 0xFFCCCCCC, // SystemChromeHighColor
38 0xFFF2F2F2, // SystemChromeLowColor
39 0xFFE6E6E6, // SystemChromeMediumColor
40 0xFFF2F2F2, // SystemChromeMediumLowColor
41 0xFFFFFFFF, // SystemChromeWhiteColor
42 0x19000000, // SystemListLowColor
43 0x33000000 // SystemListMediumColor
44 };
45 return colors[role];
46}
47
48static QRgb qquickuniversal_dark_color(QQuickUniversalStyle::SystemColor role)
49{
50 static const QRgb colors[] = {
51 0xFF000000, // SystemAltHighColor
52 0x33000000, // SystemAltLowColor
53 0x99000000, // SystemAltMediumColor
54 0xCC000000, // SystemAltMediumHighColor
55 0x66000000, // SystemAltMediumLowColor
56 0xFFFFFFFF, // SystemBaseHighColor
57 0x33FFFFFF, // SystemBaseLowColor
58 0x99FFFFFF, // SystemBaseMediumColor
59 0xCCFFFFFF, // SystemBaseMediumHighColor
60 0x66FFFFFF, // SystemBaseMediumLowColor
61 0xFFF2F2F2, // SystemChromeAltLowColor
62 0xFF000000, // SystemChromeBlackHighColor
63 0x33000000, // SystemChromeBlackLowColor
64 0x66000000, // SystemChromeBlackMediumLowColor
65 0xCC000000, // SystemChromeBlackMediumColor
66 0xFF333333, // SystemChromeDisabledHighColor
67 0xFF858585, // SystemChromeDisabledLowColor
68 0xFF767676, // SystemChromeHighColor
69 0xFF171717, // SystemChromeLowColor
70 0xFF1F1F1F, // SystemChromeMediumColor
71 0xFF2B2B2B, // SystemChromeMediumLowColor
72 0xFFFFFFFF, // SystemChromeWhiteColor
73 0x19FFFFFF, // SystemListLowColor
74 0x33FFFFFF // SystemListMediumColor
75 };
76 return colors[role];
77}
78
79static QRgb qquickuniversal_accent_color(QQuickUniversalStyle::Color accent)
80{
81 static const QRgb colors[] = {
82 0xFFA4C400, // Lime
83 0xFF60A917, // Green
84 0xFF008A00, // Emerald
85 0xFF00ABA9, // Teal
86 0xFF1BA1E2, // Cyan
87 0xFF3E65FF, // Cobalt
88 0xFF6A00FF, // Indigo
89 0xFFAA00FF, // Violet
90 0xFFF472D0, // Pink
91 0xFFD80073, // Magenta
92 0xFFA20025, // Crimson
93 0xFFE51400, // Red
94 0xFFFA6800, // Orange
95 0xFFF0A30A, // Amber
96 0xFFE3C800, // Yellow
97 0xFF825A2C, // Brown
98 0xFF6D8764, // Olive
99 0xFF647687, // Steel
100 0xFF76608A, // Mauve
101 0xFF87794E // Taupe
102 };
103 return colors[accent];
104}
105
106static QQuickUniversalStyle::Theme qquickuniversal_effective_theme(QQuickUniversalStyle::Theme theme)
107{
108 if (theme == QQuickUniversalStyle::System)
109 theme = QQuickStylePrivate::isDarkSystemTheme() ? QQuickUniversalStyle::Dark : QQuickUniversalStyle::Light;
110 return theme;
111}
112
113// If no value was inherited from a parent or explicitly set, the "global" values are used.
114// The initial, default values of the globals are hard-coded here, but the environment
115// variables and .conf file override them if specified.
116static QQuickUniversalStyle::Theme GlobalTheme = QQuickUniversalStyle::Light;
117static QRgb GlobalAccent = qquickuniversal_accent_color(QQuickUniversalStyle::Cobalt);
118static QRgb GlobalForeground = qquickuniversal_light_color(QQuickUniversalStyle::BaseHigh);
119static QRgb GlobalBackground = qquickuniversal_light_color(QQuickUniversalStyle::AltHigh);
120// These represent whether a global foreground/background was set.
121// Each style's m_hasForeground/m_hasBackground are initialized to these values.
122static bool HasGlobalForeground = false;
123static bool HasGlobalBackground = false;
124
125QQuickUniversalStyle::QQuickUniversalStyle(QObject *parent)
126 : QQuickAttachedPropertyPropagator(parent)
127 , m_hasForeground(HasGlobalForeground)
128 , m_hasBackground(HasGlobalBackground)
129 , m_usingSystemTheme(GlobalTheme == System)
130 , m_theme(qquickuniversal_effective_theme(GlobalTheme))
131 , m_accent(GlobalAccent)
132 , m_foreground(GlobalForeground)
133 , m_background(GlobalBackground)
134{
135 initialize();
136}
137
138QQuickUniversalStyle *QQuickUniversalStyle::qmlAttachedProperties(QObject *object)
139{
140 return new QQuickUniversalStyle(object);
141}
142
143QQuickUniversalStyle::Theme QQuickUniversalStyle::theme() const
144{
145 return m_theme;
146}
147
148void QQuickUniversalStyle::setTheme(Theme theme)
149{
150 m_explicitTheme = true;
151
152 // If theme is System: m_theme is set to the system's theme (Dark/Light)
153 // and m_usingSystemTheme is set to true.
154 // If theme is Dark/Light: m_theme is set to the input theme (Dark/Light)
155 // and m_usingSystemTheme is set to false.
156 const bool systemThemeChanged = (m_usingSystemTheme != (theme == System));
157 const Theme effectiveTheme = qquickuniversal_effective_theme(theme);
158 // Check m_theme and m_usingSystemTheme have changed.
159 if ((m_theme == effectiveTheme) && !systemThemeChanged)
160 return;
161
162 m_theme = effectiveTheme;
163 m_usingSystemTheme = (theme == System);
164 if (systemThemeChanged) {
165 if (m_usingSystemTheme)
166 QQuickUniversalTheme::registerSystemStyle(this);
167 else
168 QQuickUniversalTheme::unregisterSystemStyle(this);
169 }
170
171 propagateTheme();
172 emit themeChanged();
173 emit paletteChanged();
174 emit foregroundChanged();
175 emit backgroundChanged();
176}
177
178void QQuickUniversalStyle::inheritTheme(Theme theme)
179{
180 const bool systemThemeChanged = (m_usingSystemTheme != (theme == System));
181 const Theme effectiveTheme = qquickuniversal_effective_theme(theme);
182 const bool hasThemeChanged = systemThemeChanged || (m_theme != effectiveTheme);
183 if (m_explicitTheme || !hasThemeChanged)
184 return;
185
186 m_theme = effectiveTheme;
187 m_usingSystemTheme = (theme == System);
188
189 propagateTheme();
190 emit themeChanged();
191 emit paletteChanged();
192 emit foregroundChanged();
193 emit backgroundChanged();
194}
195
196void QQuickUniversalStyle::propagateTheme()
197{
198 const auto styles = attachedChildren();
199 for (QQuickAttachedPropertyPropagator *child : styles) {
200 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
201 if (universal) {
202 // m_theme is the effective theme; either Dark or Light.
203 // m_usingSystemTheme indicates whether the theme is set by
204 // the system (true) or manually (false).
205 universal->inheritTheme(m_theme);
206 }
207 }
208}
209
210void QQuickUniversalStyle::resetTheme()
211{
212 if (!m_explicitTheme)
213 return;
214
215 m_explicitTheme = false;
216 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
217 inheritTheme(universal ? universal->theme() : GlobalTheme);
218}
219
220QVariant QQuickUniversalStyle::accent() const
221{
222 return QColor::fromRgba(m_accent);
223}
224
225void QQuickUniversalStyle::setAccent(const QVariant &var)
226{
227 QRgb accent = 0;
228 if (!variantToRgba(var, "accent", &accent))
229 return;
230
231 m_explicitAccent = true;
232 if (m_accent == accent)
233 return;
234
235 m_accent = accent;
236 propagateAccent();
237 emit accentChanged();
238}
239
240void QQuickUniversalStyle::inheritAccent(QRgb accent)
241{
242 if (m_explicitAccent || m_accent == accent)
243 return;
244
245 m_accent = accent;
246 propagateAccent();
247 emit accentChanged();
248}
249
250void QQuickUniversalStyle::propagateAccent()
251{
252 const auto styles = attachedChildren();
253 for (QQuickAttachedPropertyPropagator *child : styles) {
254 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
255 if (universal)
256 universal->inheritAccent(m_accent);
257 }
258}
259
260void QQuickUniversalStyle::resetAccent()
261{
262 if (!m_explicitAccent)
263 return;
264
265 m_explicitAccent = false;
266 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
267 inheritAccent(universal ? universal->m_accent : GlobalAccent);
268}
269
270QVariant QQuickUniversalStyle::foreground() const
271{
272 if (m_hasForeground)
273 return QColor::fromRgba(m_foreground);
274 return baseHighColor();
275}
276
277void QQuickUniversalStyle::setForeground(const QVariant &var)
278{
279 QRgb foreground = 0;
280 if (!variantToRgba(var, "foreground", &foreground))
281 return;
282
283 m_hasForeground = true;
284 m_explicitForeground = true;
285 if (m_foreground == foreground)
286 return;
287
288 m_foreground = foreground;
289 propagateForeground();
290 emit foregroundChanged();
291}
292
293void QQuickUniversalStyle::inheritForeground(QRgb foreground, bool has)
294{
295 if (m_explicitForeground || m_foreground == foreground)
296 return;
297
298 m_hasForeground = has;
299 m_foreground = foreground;
300 propagateForeground();
301 emit foregroundChanged();
302}
303
304void QQuickUniversalStyle::propagateForeground()
305{
306 const auto styles = attachedChildren();
307 for (QQuickAttachedPropertyPropagator *child : styles) {
308 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
309 if (universal)
310 universal->inheritForeground(m_foreground, m_hasForeground);
311 }
312}
313
314void QQuickUniversalStyle::resetForeground()
315{
316 if (!m_explicitForeground)
317 return;
318
319 m_hasForeground = false;
320 m_explicitForeground = false;
321 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
322 inheritForeground(universal ? universal->m_foreground : GlobalForeground, universal ? universal->m_hasForeground : false);
323}
324
325QVariant QQuickUniversalStyle::background() const
326{
327 if (m_hasBackground)
328 return QColor::fromRgba(m_background);
329 return altHighColor();
330}
331
332void QQuickUniversalStyle::setBackground(const QVariant &var)
333{
334 QRgb background = 0;
335 if (!variantToRgba(var, "background", &background))
336 return;
337
338 m_hasBackground = true;
339 m_explicitBackground = true;
340 if (m_background == background)
341 return;
342
343 m_background = background;
344 propagateBackground();
345 emit backgroundChanged();
346}
347
348void QQuickUniversalStyle::inheritBackground(QRgb background, bool has)
349{
350 if (m_explicitBackground || m_background == background)
351 return;
352
353 m_hasBackground = has;
354 m_background = background;
355 propagateBackground();
356 emit backgroundChanged();
357}
358
359void QQuickUniversalStyle::propagateBackground()
360{
361 const auto styles = attachedChildren();
362 for (QQuickAttachedPropertyPropagator *child : styles) {
363 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
364 if (universal)
365 universal->inheritBackground(m_background, m_hasBackground);
366 }
367}
368
369void QQuickUniversalStyle::resetBackground()
370{
371 if (!m_explicitBackground)
372 return;
373
374 m_hasBackground = false;
375 m_explicitBackground = false;
376 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
377 inheritBackground(universal ? universal->m_background : GlobalBackground, universal ? universal->m_hasBackground : false);
378}
379
380QColor QQuickUniversalStyle::color(Color color) const
381{
382 return qquickuniversal_accent_color(color);
383}
384
385QColor QQuickUniversalStyle::altHighColor() const
386{
387 return systemColor(AltHigh);
388}
389
390QColor QQuickUniversalStyle::altLowColor() const
391{
392 return systemColor(AltLow);
393}
394
395QColor QQuickUniversalStyle::altMediumColor() const
396{
397 return systemColor(AltMedium);
398}
399
400QColor QQuickUniversalStyle::altMediumHighColor() const
401{
402 return systemColor(AltMediumHigh);
403}
404
405QColor QQuickUniversalStyle::altMediumLowColor() const
406{
407 return systemColor(AltMediumLow);
408}
409
410QColor QQuickUniversalStyle::baseHighColor() const
411{
412 return systemColor(BaseHigh);
413}
414
415QColor QQuickUniversalStyle::baseLowColor() const
416{
417 return systemColor(BaseLow);
418}
419
420QColor QQuickUniversalStyle::baseMediumColor() const
421{
422 return systemColor(BaseMedium);
423}
424
425QColor QQuickUniversalStyle::baseMediumHighColor() const
426{
427 return systemColor(BaseMediumHigh);
428}
429
430QColor QQuickUniversalStyle::baseMediumLowColor() const
431{
432 return systemColor(BaseMediumLow);
433}
434
435QColor QQuickUniversalStyle::chromeAltLowColor() const
436{
437 return systemColor(ChromeAltLow);
438}
439
440QColor QQuickUniversalStyle::chromeBlackHighColor() const
441{
442 return systemColor(ChromeBlackHigh);
443}
444
445QColor QQuickUniversalStyle::chromeBlackLowColor() const
446{
447 return systemColor(ChromeBlackLow);
448}
449
450QColor QQuickUniversalStyle::chromeBlackMediumLowColor() const
451{
452 return systemColor(ChromeBlackMediumLow);
453}
454
455QColor QQuickUniversalStyle::chromeBlackMediumColor() const
456{
457 return systemColor(ChromeBlackMedium);
458}
459
460QColor QQuickUniversalStyle::chromeDisabledHighColor() const
461{
462 return systemColor(ChromeDisabledHigh);
463}
464
465QColor QQuickUniversalStyle::chromeDisabledLowColor() const
466{
467 return systemColor(ChromeDisabledLow);
468}
469
470QColor QQuickUniversalStyle::chromeHighColor() const
471{
472 return systemColor(ChromeHigh);
473}
474
475QColor QQuickUniversalStyle::chromeLowColor() const
476{
477 return systemColor(ChromeLow);
478}
479
480QColor QQuickUniversalStyle::chromeMediumColor() const
481{
482 return systemColor(ChromeMedium);
483}
484
485QColor QQuickUniversalStyle::chromeMediumLowColor() const
486{
487 return systemColor(ChromeMediumLow);
488}
489
490QColor QQuickUniversalStyle::chromeWhiteColor() const
491{
492 return systemColor(ChromeWhite);
493}
494
495QColor QQuickUniversalStyle::listLowColor() const
496{
497 return systemColor(ListLow);
498}
499
500QColor QQuickUniversalStyle::listMediumColor() const
501{
502 return systemColor(ListMedium);
503}
504
505QColor QQuickUniversalStyle::systemColor(SystemColor role) const
506{
507 return QColor::fromRgba(m_theme == QQuickUniversalStyle::Dark ? qquickuniversal_dark_color(role) : qquickuniversal_light_color(role));
508}
509
510void QQuickUniversalStyle::attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent)
511{
512 Q_UNUSED(oldParent);
513 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(newParent);
514 if (universal) {
515 inheritTheme(universal->theme());
516 inheritAccent(universal->m_accent);
517 inheritForeground(universal->m_foreground, universal->m_hasForeground);
518 inheritBackground(universal->m_background, universal->m_hasBackground);
519 }
520}
521
522template <typename Enum>
523static Enum toEnumValue(const QByteArray &value, bool *ok)
524{
525 QMetaEnum enumeration = QMetaEnum::fromType<Enum>();
526 return static_cast<Enum>(enumeration.keyToValue(value, ok));
527}
528
529static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer<QSettings> &settings, const QString &name)
530{
531 QByteArray value = qgetenv(env);
532#if QT_CONFIG(settings)
533 if (value.isNull() && !settings.isNull())
534 value = settings->value(name).toByteArray();
535#endif
536 return value;
537}
538
539void QQuickUniversalStyle::initGlobals()
540{
541 QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(QStringLiteral("Universal"));
542
543 bool ok = false;
544 QByteArray themeValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_THEME", settings, QStringLiteral("Theme"));
545 Theme themeEnum = toEnumValue<Theme>(themeValue, &ok);
546 if (ok)
547 GlobalTheme = themeEnum;
548 else if (!themeValue.isEmpty())
549 qWarning().nospace().noquote() << "Universal: unknown theme value: " << themeValue;
550
551 QByteArray accentValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_ACCENT", settings, QStringLiteral("Accent"));
552 Color accentEnum = toEnumValue<Color>(accentValue, &ok);
553 if (ok) {
554 GlobalAccent = qquickuniversal_accent_color(accentEnum);
555 } else if (!accentValue.isEmpty()) {
556 QColor color = QColor::fromString(accentValue);
557 if (color.isValid())
558 GlobalAccent = color.rgba();
559 else
560 qWarning().nospace().noquote() << "Universal: unknown accent value: " << accentValue;
561 }
562
563 QByteArray foregroundValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_FOREGROUND", settings, QStringLiteral("Foreground"));
564 Color foregroundEnum = toEnumValue<Color>(foregroundValue, &ok);
565 if (ok) {
566 GlobalForeground = qquickuniversal_accent_color(foregroundEnum);
567 HasGlobalForeground = true;
568 } else if (!foregroundValue.isEmpty()) {
569 QColor color = QColor::fromString(foregroundValue);
570 if (color.isValid()) {
571 GlobalForeground = color.rgba();
572 HasGlobalForeground = true;
573 } else {
574 qWarning().nospace().noquote() << "Universal: unknown foreground value: " << foregroundValue;
575 }
576 }
577
578 QByteArray backgroundValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_BACKGROUND", settings, QStringLiteral("Background"));
579 Color backgroundEnum = toEnumValue<Color>(backgroundValue, &ok);
580 if (ok) {
581 GlobalBackground = qquickuniversal_accent_color(backgroundEnum);
582 HasGlobalBackground = true;
583 } else if (!backgroundValue.isEmpty()) {
584 QColor color = QColor::fromString(backgroundValue);
585 if (color.isValid()) {
586 GlobalBackground = color.rgba();
587 HasGlobalBackground = true;
588 } else {
589 qWarning().nospace().noquote() << "Universal: unknown background value: " << backgroundValue;
590 }
591 }
592}
593
594bool QQuickUniversalStyle::variantToRgba(const QVariant &var, const char *name, QRgb *rgba) const
595{
596 if (var.metaType().id() == QMetaType::Int) {
597 int val = var.toInt();
598 if (val < Lime || val > Taupe) {
599 qmlWarning(parent()) << "unknown Universal." << name << " value: " << val;
600 return false;
601 }
602 *rgba = qquickuniversal_accent_color(static_cast<Color>(val));
603 } else {
604 int val = QMetaEnum::fromType<Color>().keyToValue(var.toByteArray());
605 if (val != -1) {
606 *rgba = qquickuniversal_accent_color(static_cast<Color>(val));
607 } else {
608 QColor color = QColor::fromString(var.toString());
609 if (!color.isValid()) {
610 qmlWarning(parent()) << "unknown Universal." << name << " value: " << var.toString();
611 return false;
612 }
613 *rgba = color.rgba();
614 }
615 }
616 return true;
617}
618
619QT_END_NAMESPACE
620
621#include "moc_qquickuniversalstyle_p.cpp"
static QRgb GlobalAccent
static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer< QSettings > &settings, const QString &name)
static Enum toEnumValue(const QByteArray &value, bool *ok)
static QT_BEGIN_NAMESPACE QRgb qquickuniversal_light_color(QQuickUniversalStyle::SystemColor role)
static bool HasGlobalForeground
static QQuickUniversalStyle::Theme GlobalTheme
static QRgb qquickuniversal_dark_color(QQuickUniversalStyle::SystemColor role)
static QQuickUniversalStyle::Theme qquickuniversal_effective_theme(QQuickUniversalStyle::Theme theme)
static QRgb GlobalForeground
static QRgb GlobalBackground
static QRgb qquickuniversal_accent_color(QQuickUniversalStyle::Color accent)
static bool HasGlobalBackground