Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
6#include <QtCore/qdebug.h>
7#if QT_CONFIG(settings)
8#include <QtCore/qsettings.h>
9#endif
10#include <QtQml/qqmlinfo.h>
11#include <QtQuickControls2/private/qquickstyle_p.h>
12
14
16{
17 static const QRgb colors[] = {
18 0xFFFFFFFF, // SystemAltHighColor
19 0x33FFFFFF, // SystemAltLowColor
20 0x99FFFFFF, // SystemAltMediumColor
21 0xCCFFFFFF, // SystemAltMediumHighColor
22 0x66FFFFFF, // SystemAltMediumLowColor
23 0xFF000000, // SystemBaseHighColor
24 0x33000000, // SystemBaseLowColor
25 0x99000000, // SystemBaseMediumColor
26 0xCC000000, // SystemBaseMediumHighColor
27 0x66000000, // SystemBaseMediumLowColor
28 0xFF171717, // SystemChromeAltLowColor
29 0xFF000000, // SystemChromeBlackHighColor
30 0x33000000, // SystemChromeBlackLowColor
31 0x66000000, // SystemChromeBlackMediumLowColor
32 0xCC000000, // SystemChromeBlackMediumColor
33 0xFFCCCCCC, // SystemChromeDisabledHighColor
34 0xFF7A7A7A, // SystemChromeDisabledLowColor
35 0xFFCCCCCC, // SystemChromeHighColor
36 0xFFF2F2F2, // SystemChromeLowColor
37 0xFFE6E6E6, // SystemChromeMediumColor
38 0xFFF2F2F2, // SystemChromeMediumLowColor
39 0xFFFFFFFF, // SystemChromeWhiteColor
40 0x19000000, // SystemListLowColor
41 0x33000000 // SystemListMediumColor
42 };
43 return colors[role];
44}
45
47{
48 static const QRgb colors[] = {
49 0xFF000000, // SystemAltHighColor
50 0x33000000, // SystemAltLowColor
51 0x99000000, // SystemAltMediumColor
52 0xCC000000, // SystemAltMediumHighColor
53 0x66000000, // SystemAltMediumLowColor
54 0xFFFFFFFF, // SystemBaseHighColor
55 0x33FFFFFF, // SystemBaseLowColor
56 0x99FFFFFF, // SystemBaseMediumColor
57 0xCCFFFFFF, // SystemBaseMediumHighColor
58 0x66FFFFFF, // SystemBaseMediumLowColor
59 0xFFF2F2F2, // SystemChromeAltLowColor
60 0xFF000000, // SystemChromeBlackHighColor
61 0x33000000, // SystemChromeBlackLowColor
62 0x66000000, // SystemChromeBlackMediumLowColor
63 0xCC000000, // SystemChromeBlackMediumColor
64 0xFF333333, // SystemChromeDisabledHighColor
65 0xFF858585, // SystemChromeDisabledLowColor
66 0xFF767676, // SystemChromeHighColor
67 0xFF171717, // SystemChromeLowColor
68 0xFF1F1F1F, // SystemChromeMediumColor
69 0xFF2B2B2B, // SystemChromeMediumLowColor
70 0xFFFFFFFF, // SystemChromeWhiteColor
71 0x19FFFFFF, // SystemListLowColor
72 0x33FFFFFF // SystemListMediumColor
73 };
74 return colors[role];
75}
76
78{
79 static const QRgb colors[] = {
80 0xFFA4C400, // Lime
81 0xFF60A917, // Green
82 0xFF008A00, // Emerald
83 0xFF00ABA9, // Teal
84 0xFF1BA1E2, // Cyan
85 0xFF3E65FF, // Cobalt
86 0xFF6A00FF, // Indigo
87 0xFFAA00FF, // Violet
88 0xFFF472D0, // Pink
89 0xFFD80073, // Magenta
90 0xFFA20025, // Crimson
91 0xFFE51400, // Red
92 0xFFFA6800, // Orange
93 0xFFF0A30A, // Amber
94 0xFFE3C800, // Yellow
95 0xFF825A2C, // Brown
96 0xFF6D8764, // Olive
97 0xFF647687, // Steel
98 0xFF76608A, // Mauve
99 0xFF87794E // Taupe
100 };
101 return colors[accent];
102}
103
110
111// If no value was inherited from a parent or explicitly set, the "global" values are used.
112// The initial, default values of the globals are hard-coded here, but the environment
113// variables and .conf file override them if specified.
118// These represent whether a global foreground/background was set.
119// Each style's m_hasForeground/m_hasBackground are initialized to these values.
120static bool HasGlobalForeground = false;
121static bool HasGlobalBackground = false;
122
124 m_hasForeground(HasGlobalForeground), m_hasBackground(HasGlobalBackground), m_theme(GlobalTheme),
125 m_accent(GlobalAccent), m_foreground(GlobalForeground), m_background(GlobalBackground)
126{
127 initialize();
128}
129
134
136{
137 return m_theme;
138}
139
141{
143 m_explicitTheme = true;
144 if (m_theme == theme)
145 return;
146
147 m_theme = theme;
153}
154
156{
157 if (m_explicitTheme || m_theme == theme)
158 return;
159
160 m_theme = theme;
166}
167
169{
170 const auto styles = attachedChildren();
172 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
173 if (universal)
174 universal->inheritTheme(m_theme);
175 }
176}
177
179{
180 if (!m_explicitTheme)
181 return;
182
183 m_explicitTheme = false;
184 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
185 inheritTheme(universal ? universal->theme() : GlobalTheme);
186}
187
189{
190 return QColor::fromRgba(m_accent);
191}
192
194{
195 QRgb accent = 0;
196 if (!variantToRgba(var, "accent", &accent))
197 return;
198
199 m_explicitAccent = true;
200 if (m_accent == accent)
201 return;
202
203 m_accent = accent;
206}
207
209{
210 if (m_explicitAccent || m_accent == accent)
211 return;
212
213 m_accent = accent;
216}
217
219{
220 const auto styles = attachedChildren();
222 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
223 if (universal)
224 universal->inheritAccent(m_accent);
225 }
226}
227
229{
230 if (!m_explicitAccent)
231 return;
232
233 m_explicitAccent = false;
234 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
235 inheritAccent(universal ? universal->m_accent : GlobalAccent);
236}
237
239{
240 if (m_hasForeground)
241 return QColor::fromRgba(m_foreground);
242 return baseHighColor();
243}
244
246{
247 QRgb foreground = 0;
248 if (!variantToRgba(var, "foreground", &foreground))
249 return;
250
251 m_hasForeground = true;
252 m_explicitForeground = true;
253 if (m_foreground == foreground)
254 return;
255
256 m_foreground = foreground;
259}
260
262{
263 if (m_explicitForeground || m_foreground == foreground)
264 return;
265
266 m_hasForeground = has;
267 m_foreground = foreground;
270}
271
273{
274 const auto styles = attachedChildren();
276 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
277 if (universal)
278 universal->inheritForeground(m_foreground, m_hasForeground);
279 }
280}
281
283{
284 if (!m_explicitForeground)
285 return;
286
287 m_hasForeground = false;
288 m_explicitForeground = false;
289 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
290 inheritForeground(universal ? universal->m_foreground : GlobalForeground, universal ? universal->m_hasForeground : false);
291}
292
294{
295 if (m_hasBackground)
296 return QColor::fromRgba(m_background);
297 return altHighColor();
298}
299
301{
302 QRgb background = 0;
303 if (!variantToRgba(var, "background", &background))
304 return;
305
306 m_hasBackground = true;
307 m_explicitBackground = true;
308 if (m_background == background)
309 return;
310
311 m_background = background;
314}
315
317{
318 if (m_explicitBackground || m_background == background)
319 return;
320
321 m_hasBackground = has;
322 m_background = background;
325}
326
328{
329 const auto styles = attachedChildren();
331 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(child);
332 if (universal)
333 universal->inheritBackground(m_background, m_hasBackground);
334 }
335}
336
338{
339 if (!m_explicitBackground)
340 return;
341
342 m_hasBackground = false;
343 m_explicitBackground = false;
344 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(attachedParent());
345 inheritBackground(universal ? universal->m_background : GlobalBackground, universal ? universal->m_hasBackground : false);
346}
347
352
357
362
367
372
377
382
387
392
397
402
407
412
417
422
427
432
437
442
447
452
457
462
467
472
477
479{
480 Q_UNUSED(oldParent);
481 QQuickUniversalStyle *universal = qobject_cast<QQuickUniversalStyle *>(newParent);
482 if (universal) {
483 inheritTheme(universal->theme());
484 inheritAccent(universal->m_accent);
485 inheritForeground(universal->m_foreground, universal->m_hasForeground);
486 inheritBackground(universal->m_background, universal->m_hasBackground);
487 }
488}
489
490template <typename Enum>
491static Enum toEnumValue(const QByteArray &value, bool *ok)
492{
493 QMetaEnum enumeration = QMetaEnum::fromType<Enum>();
494 return static_cast<Enum>(enumeration.keyToValue(value, ok));
495}
496
497static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer<QSettings> &settings, const QString &name)
498{
499 QByteArray value = qgetenv(env);
500#if QT_CONFIG(settings)
501 if (value.isNull() && !settings.isNull())
503#endif
504 return value;
505}
506
508{
509 QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(QStringLiteral("Universal"));
510
511 bool ok = false;
512 QByteArray themeValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_THEME", settings, QStringLiteral("Theme"));
513 Theme themeEnum = toEnumValue<Theme>(themeValue, &ok);
514 if (ok)
516 else if (!themeValue.isEmpty())
517 qWarning().nospace().noquote() << "Universal: unknown theme value: " << themeValue;
518
519 QByteArray accentValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_ACCENT", settings, QStringLiteral("Accent"));
520 Color accentEnum = toEnumValue<Color>(accentValue, &ok);
521 if (ok) {
523 } else if (!accentValue.isEmpty()) {
524 QColor color = QColor::fromString(accentValue);
525 if (color.isValid())
526 GlobalAccent = color.rgba();
527 else
528 qWarning().nospace().noquote() << "Universal: unknown accent value: " << accentValue;
529 }
530
531 QByteArray foregroundValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_FOREGROUND", settings, QStringLiteral("Foreground"));
532 Color foregroundEnum = toEnumValue<Color>(foregroundValue, &ok);
533 if (ok) {
535 HasGlobalForeground = true;
536 } else if (!foregroundValue.isEmpty()) {
537 QColor color = QColor::fromString(foregroundValue);
538 if (color.isValid()) {
539 GlobalForeground = color.rgba();
540 HasGlobalForeground = true;
541 } else {
542 qWarning().nospace().noquote() << "Universal: unknown foreground value: " << foregroundValue;
543 }
544 }
545
546 QByteArray backgroundValue = resolveSetting("QT_QUICK_CONTROLS_UNIVERSAL_BACKGROUND", settings, QStringLiteral("Background"));
547 Color backgroundEnum = toEnumValue<Color>(backgroundValue, &ok);
548 if (ok) {
550 HasGlobalBackground = true;
551 } else if (!backgroundValue.isEmpty()) {
552 QColor color = QColor::fromString(backgroundValue);
553 if (color.isValid()) {
554 GlobalBackground = color.rgba();
555 HasGlobalBackground = true;
556 } else {
557 qWarning().nospace().noquote() << "Universal: unknown background value: " << backgroundValue;
558 }
559 }
560}
561
562bool QQuickUniversalStyle::variantToRgba(const QVariant &var, const char *name, QRgb *rgba) const
563{
564 if (var.metaType().id() == QMetaType::Int) {
565 int val = var.toInt();
566 if (val < Lime || val > Taupe) {
567 qmlWarning(parent()) << "unknown Universal." << name << " value: " << val;
568 return false;
569 }
570 *rgba = qquickuniversal_accent_color(static_cast<Color>(val));
571 } else {
572 int val = QMetaEnum::fromType<Color>().keyToValue(var.toByteArray());
573 if (val != -1) {
574 *rgba = qquickuniversal_accent_color(static_cast<Color>(val));
575 } else {
577 if (!color.isValid()) {
578 qmlWarning(parent()) << "unknown Universal." << name << " value: " << var.toString();
579 return false;
580 }
581 *rgba = color.rgba();
582 }
583 }
584 return true;
585}
586
588
589#include "moc_qquickuniversalstyle_p.cpp"
\inmodule QtCore
Definition qbytearray.h:57
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static QColor fromRgba(QRgb rgba) noexcept
Static convenience function that returns a QColor constructed from the given QRgb value rgba.
Definition qcolor.cpp:2385
static QColor fromString(QAnyStringView name) noexcept
Definition qcolor.cpp:980
\inmodule QtCore
int keyToValue(const char *key, bool *ok=nullptr) const
Returns the integer value of the given enumeration key, or -1 if key is not defined.
int id(int=0) const
Definition qmetatype.h:475
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
The QQuickAttachedPropertyPropagator class provides a way to propagate attached properties.
QQuickAttachedPropertyPropagator * attachedParent() const
This function returns the attached parent of this attached object.
QList< QQuickAttachedPropertyPropagator * > attachedChildren() const
This function returns the attached children of this attached object.
void initialize()
Finds and sets the attached parent for this attached object, and then does the same for its children.
static QSharedPointer< QSettings > settings(const QString &group=QString())
static bool isDarkSystemTheme()
void setForeground(const QVariant &foreground)
Q_INVOKABLE QColor color(Color color) const
void inheritBackground(QRgb background, bool has)
void setAccent(const QVariant &accent)
void inheritForeground(QRgb foreground, bool has)
QColor systemColor(SystemColor role) const
void inheritAccent(QRgb accent)
static QQuickUniversalStyle * qmlAttachedProperties(QObject *object)
QQuickUniversalStyle(QObject *parent=nullptr)
void attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent) override
This function is called whenever the attached parent of this QQuickAttachedPropertyPropagator changes...
void setBackground(const QVariant &background)
QVariant value(QAnyStringView key, const QVariant &defaultValue) const
Returns the value for setting key.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has userType() \l QMetaType::QByteArray or \l QMet...
QMetaType metaType() const
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:166
GLuint color
[2]
GLuint name
GLuint GLfloat * val
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer< QSettings > &settings, const QString &name)
static QT_BEGIN_NAMESPACE const QRgb colors[][14]
static QQuickUniversalStyle::Theme GlobalTheme
static bool HasGlobalForeground
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 QRgb qquickuniversal_dark_color(QQuickUniversalStyle::SystemColor role)
static QRgb GlobalForeground
static bool HasGlobalBackground
static QQuickUniversalStyle::Theme qquickuniversal_effective_theme(QQuickUniversalStyle::Theme theme)
static QRgb qquickuniversal_accent_color(QQuickUniversalStyle::Color accent)
static QRgb GlobalAccent
static QRgb GlobalBackground
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
#define QStringLiteral(str)
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
#define emit
#define Q_UNUSED(x)
QSettings settings("MySoft", "Star Runner")
[0]
QLayoutItem * child
[0]