1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickmaterialstyle_p.h"
38
39#include <QtCore/qdebug.h>
40#include <QtCore/qsettings.h>
41#include <QtQml/qqmlinfo.h>
42#include <QtQuickControls2/private/qquickstyle_p.h>
43
44QT_BEGIN_NAMESPACE
45
46static const QRgb colors[][14] = {
47 // Red
48 {
49 0xFFFFEBEE, // Shade50
50 0xFFFFCDD2, // Shade100
51 0xFFEF9A9A, // Shade200
52 0xFFE57373, // Shade300
53 0xFFEF5350, // Shade400
54 0xFFF44336, // Shade500
55 0xFFE53935, // Shade600
56 0xFFD32F2F, // Shade700
57 0xFFC62828, // Shade800
58 0xFFB71C1C, // Shade900
59 0xFFFF8A80, // ShadeA100
60 0xFFFF5252, // ShadeA200
61 0xFFFF1744, // ShadeA400
62 0xFFD50000 // ShadeA700
63 },
64 // Pink
65 {
66 0xFFFCE4EC, // Shade50
67 0xFFF8BBD0, // Shade100
68 0xFFF48FB1, // Shade200
69 0xFFF06292, // Shade300
70 0xFFEC407A, // Shade400
71 0xFFE91E63, // Shade500
72 0xFFD81B60, // Shade600
73 0xFFC2185B, // Shade700
74 0xFFAD1457, // Shade800
75 0xFF880E4F, // Shade900
76 0xFFFF80AB, // ShadeA100
77 0xFFFF4081, // ShadeA200
78 0xFFF50057, // ShadeA400
79 0xFFC51162 // ShadeA700
80 },
81 // Purple
82 {
83 0xFFF3E5F5, // Shade50
84 0xFFE1BEE7, // Shade100
85 0xFFCE93D8, // Shade200
86 0xFFBA68C8, // Shade300
87 0xFFAB47BC, // Shade400
88 0xFF9C27B0, // Shade500
89 0xFF8E24AA, // Shade600
90 0xFF7B1FA2, // Shade700
91 0xFF6A1B9A, // Shade800
92 0xFF4A148C, // Shade900
93 0xFFEA80FC, // ShadeA100
94 0xFFE040FB, // ShadeA200
95 0xFFD500F9, // ShadeA400
96 0xFFAA00FF // ShadeA700
97 },
98 // DeepPurple
99 {
100 0xFFEDE7F6, // Shade50
101 0xFFD1C4E9, // Shade100
102 0xFFB39DDB, // Shade200
103 0xFF9575CD, // Shade300
104 0xFF7E57C2, // Shade400
105 0xFF673AB7, // Shade500
106 0xFF5E35B1, // Shade600
107 0xFF512DA8, // Shade700
108 0xFF4527A0, // Shade800
109 0xFF311B92, // Shade900
110 0xFFB388FF, // ShadeA100
111 0xFF7C4DFF, // ShadeA200
112 0xFF651FFF, // ShadeA400
113 0xFF6200EA // ShadeA700
114 },
115 // Indigo
116 {
117 0xFFE8EAF6, // Shade50
118 0xFFC5CAE9, // Shade100
119 0xFF9FA8DA, // Shade200
120 0xFF7986CB, // Shade300
121 0xFF5C6BC0, // Shade400
122 0xFF3F51B5, // Shade500
123 0xFF3949AB, // Shade600
124 0xFF303F9F, // Shade700
125 0xFF283593, // Shade800
126 0xFF1A237E, // Shade900
127 0xFF8C9EFF, // ShadeA100
128 0xFF536DFE, // ShadeA200
129 0xFF3D5AFE, // ShadeA400
130 0xFF304FFE // ShadeA700
131 },
132 // Blue
133 {
134 0xFFE3F2FD, // Shade50
135 0xFFBBDEFB, // Shade100
136 0xFF90CAF9, // Shade200
137 0xFF64B5F6, // Shade300
138 0xFF42A5F5, // Shade400
139 0xFF2196F3, // Shade500
140 0xFF1E88E5, // Shade600
141 0xFF1976D2, // Shade700
142 0xFF1565C0, // Shade800
143 0xFF0D47A1, // Shade900
144 0xFF82B1FF, // ShadeA100
145 0xFF448AFF, // ShadeA200
146 0xFF2979FF, // ShadeA400
147 0xFF2962FF // ShadeA700
148 },
149 // LightBlue
150 {
151 0xFFE1F5FE, // Shade50
152 0xFFB3E5FC, // Shade100
153 0xFF81D4FA, // Shade200
154 0xFF4FC3F7, // Shade300
155 0xFF29B6F6, // Shade400
156 0xFF03A9F4, // Shade500
157 0xFF039BE5, // Shade600
158 0xFF0288D1, // Shade700
159 0xFF0277BD, // Shade800
160 0xFF01579B, // Shade900
161 0xFF80D8FF, // ShadeA100
162 0xFF40C4FF, // ShadeA200
163 0xFF00B0FF, // ShadeA400
164 0xFF0091EA // ShadeA700
165 },
166 // Cyan
167 {
168 0xFFE0F7FA, // Shade50
169 0xFFB2EBF2, // Shade100
170 0xFF80DEEA, // Shade200
171 0xFF4DD0E1, // Shade300
172 0xFF26C6DA, // Shade400
173 0xFF00BCD4, // Shade500
174 0xFF00ACC1, // Shade600
175 0xFF0097A7, // Shade700
176 0xFF00838F, // Shade800
177 0xFF006064, // Shade900
178 0xFF84FFFF, // ShadeA100
179 0xFF18FFFF, // ShadeA200
180 0xFF00E5FF, // ShadeA400
181 0xFF00B8D4 // ShadeA700
182 },
183 // Teal
184 {
185 0xFFE0F2F1, // Shade50
186 0xFFB2DFDB, // Shade100
187 0xFF80CBC4, // Shade200
188 0xFF4DB6AC, // Shade300
189 0xFF26A69A, // Shade400
190 0xFF009688, // Shade500
191 0xFF00897B, // Shade600
192 0xFF00796B, // Shade700
193 0xFF00695C, // Shade800
194 0xFF004D40, // Shade900
195 0xFFA7FFEB, // ShadeA100
196 0xFF64FFDA, // ShadeA200
197 0xFF1DE9B6, // ShadeA400
198 0xFF00BFA5 // ShadeA700
199 },
200 // Green
201 {
202 0xFFE8F5E9, // Shade50
203 0xFFC8E6C9, // Shade100
204 0xFFA5D6A7, // Shade200
205 0xFF81C784, // Shade300
206 0xFF66BB6A, // Shade400
207 0xFF4CAF50, // Shade500
208 0xFF43A047, // Shade600
209 0xFF388E3C, // Shade700
210 0xFF2E7D32, // Shade800
211 0xFF1B5E20, // Shade900
212 0xFFB9F6CA, // ShadeA100
213 0xFF69F0AE, // ShadeA200
214 0xFF00E676, // ShadeA400
215 0xFF00C853 // ShadeA700
216 },
217 // LightGreen
218 {
219 0xFFF1F8E9, // Shade50
220 0xFFDCEDC8, // Shade100
221 0xFFC5E1A5, // Shade200
222 0xFFAED581, // Shade300
223 0xFF9CCC65, // Shade400
224 0xFF8BC34A, // Shade500
225 0xFF7CB342, // Shade600
226 0xFF689F38, // Shade700
227 0xFF558B2F, // Shade800
228 0xFF33691E, // Shade900
229 0xFFCCFF90, // ShadeA100
230 0xFFB2FF59, // ShadeA200
231 0xFF76FF03, // ShadeA400
232 0xFF64DD17 // ShadeA700
233 },
234 // Lime
235 {
236 0xFFF9FBE7, // Shade50
237 0xFFF0F4C3, // Shade100
238 0xFFE6EE9C, // Shade200
239 0xFFDCE775, // Shade300
240 0xFFD4E157, // Shade400
241 0xFFCDDC39, // Shade500
242 0xFFC0CA33, // Shade600
243 0xFFAFB42B, // Shade700
244 0xFF9E9D24, // Shade800
245 0xFF827717, // Shade900
246 0xFFF4FF81, // ShadeA100
247 0xFFEEFF41, // ShadeA200
248 0xFFC6FF00, // ShadeA400
249 0xFFAEEA00 // ShadeA700
250 },
251 // Yellow
252 {
253 0xFFFFFDE7, // Shade50
254 0xFFFFF9C4, // Shade100
255 0xFFFFF59D, // Shade200
256 0xFFFFF176, // Shade300
257 0xFFFFEE58, // Shade400
258 0xFFFFEB3B, // Shade500
259 0xFFFDD835, // Shade600
260 0xFFFBC02D, // Shade700
261 0xFFF9A825, // Shade800
262 0xFFF57F17, // Shade900
263 0xFFFFFF8D, // ShadeA100
264 0xFFFFFF00, // ShadeA200
265 0xFFFFEA00, // ShadeA400
266 0xFFFFD600 // ShadeA700
267 },
268 // Amber
269 {
270 0xFFFFF8E1, // Shade50
271 0xFFFFECB3, // Shade100
272 0xFFFFE082, // Shade200
273 0xFFFFD54F, // Shade300
274 0xFFFFCA28, // Shade400
275 0xFFFFC107, // Shade500
276 0xFFFFB300, // Shade600
277 0xFFFFA000, // Shade700
278 0xFFFF8F00, // Shade800
279 0xFFFF6F00, // Shade900
280 0xFFFFE57F, // ShadeA100
281 0xFFFFD740, // ShadeA200
282 0xFFFFC400, // ShadeA400
283 0xFFFFAB00 // ShadeA700
284 },
285 // Orange
286 {
287 0xFFFFF3E0, // Shade50
288 0xFFFFE0B2, // Shade100
289 0xFFFFCC80, // Shade200
290 0xFFFFB74D, // Shade300
291 0xFFFFA726, // Shade400
292 0xFFFF9800, // Shade500
293 0xFFFB8C00, // Shade600
294 0xFFF57C00, // Shade700
295 0xFFEF6C00, // Shade800
296 0xFFE65100, // Shade900
297 0xFFFFD180, // ShadeA100
298 0xFFFFAB40, // ShadeA200
299 0xFFFF9100, // ShadeA400
300 0xFFFF6D00 // ShadeA700
301 },
302 // DeepOrange
303 {
304 0xFFFBE9E7, // Shade50
305 0xFFFFCCBC, // Shade100
306 0xFFFFAB91, // Shade200
307 0xFFFF8A65, // Shade300
308 0xFFFF7043, // Shade400
309 0xFFFF5722, // Shade500
310 0xFFF4511E, // Shade600
311 0xFFE64A19, // Shade700
312 0xFFD84315, // Shade800
313 0xFFBF360C, // Shade900
314 0xFFFF9E80, // ShadeA100
315 0xFFFF6E40, // ShadeA200
316 0xFFFF3D00, // ShadeA400
317 0xFFDD2C00 // ShadeA700
318 },
319 // Brown
320 {
321 0xFFEFEBE9, // Shade50
322 0xFFD7CCC8, // Shade100
323 0xFFBCAAA4, // Shade200
324 0xFFA1887F, // Shade300
325 0xFF8D6E63, // Shade400
326 0xFF795548, // Shade500
327 0xFF6D4C41, // Shade600
328 0xFF5D4037, // Shade700
329 0xFF4E342E, // Shade800
330 0xFF3E2723, // Shade900
331 0xFF000000, // ShadeA100
332 0xFF000000, // ShadeA200
333 0xFF000000, // ShadeA400
334 0xFF000000 // ShadeA700
335 },
336 // Grey
337 {
338 0xFFFAFAFA, // Shade50
339 0xFFF5F5F5, // Shade100
340 0xFFEEEEEE, // Shade200
341 0xFFE0E0E0, // Shade300
342 0xFFBDBDBD, // Shade400
343 0xFF9E9E9E, // Shade500
344 0xFF757575, // Shade600
345 0xFF616161, // Shade700
346 0xFF424242, // Shade800
347 0xFF212121, // Shade900
348 0xFF000000, // ShadeA100
349 0xFF000000, // ShadeA200
350 0xFF000000, // ShadeA400
351 0xFF000000 // ShadeA700
352 },
353 // BlueGrey
354 {
355 0xFFECEFF1, // Shade50
356 0xFFCFD8DC, // Shade100
357 0xFFB0BEC5, // Shade200
358 0xFF90A4AE, // Shade300
359 0xFF78909C, // Shade400
360 0xFF607D8B, // Shade500
361 0xFF546E7A, // Shade600
362 0xFF455A64, // Shade700
363 0xFF37474F, // Shade800
364 0xFF263238, // Shade900
365 0xFF000000, // ShadeA100
366 0xFF000000, // ShadeA200
367 0xFF000000, // ShadeA400
368 0xFF000000 // ShadeA700
369 }
370};
371
372// If no value was inherited from a parent or explicitly set, the "global" values are used.
373// The initial, default values of the globals are hard-coded here, but the environment
374// variables and .conf file override them if specified.
375static QQuickMaterialStyle::Theme globalTheme = QQuickMaterialStyle::Light;
376static uint globalPrimary = QQuickMaterialStyle::Indigo;
377static uint globalAccent = QQuickMaterialStyle::Pink;
378static uint globalForeground = 0xDD000000; // primaryTextColorLight
379static uint globalBackground = 0xFFFAFAFA; // backgroundColorLight
380// These represent whether a global foreground/background was set.
381// Each style's m_hasForeground/m_hasBackground are initialized to these values.
382static bool hasGlobalForeground = false;
383static bool hasGlobalBackground = false;
384// These represent whether or not the global color value was specified as one of the
385// values that QColor accepts, as opposed to one of the pre-defined colors like Red.
386static bool globalPrimaryCustom = false;
387static bool globalAccentCustom = false;
388static bool globalForegroundCustom = true;
389static bool globalBackgroundCustom = true;
390// This is global because:
391// 1) The theme needs access to it to determine font sizes.
392// 2) There can only be one variant used for the whole application.
393static QQuickMaterialStyle::Variant globalVariant = QQuickMaterialStyle::Normal;
394
395static const QRgb backgroundColorLight = 0xFFFAFAFA;
396static const QRgb backgroundColorDark = 0xFF303030;
397static const QRgb dialogColorLight = 0xFFFFFFFF;
398static const QRgb dialogColorDark = 0xFF424242;
399static const QRgb primaryTextColorLight = 0xDD000000;
400static const QRgb primaryTextColorDark = 0xFFFFFFFF;
401static const QRgb secondaryTextColorLight = 0x89000000;
402static const QRgb secondaryTextColorDark = 0xB2FFFFFF;
403static const QRgb hintTextColorLight = 0x60000000;
404static const QRgb hintTextColorDark = 0x4CFFFFFF;
405static const QRgb dividerColorLight = 0x1E000000;
406static const QRgb dividerColorDark = 0x1EFFFFFF;
407static const QRgb iconColorLight = 0x89000000;
408static const QRgb iconColorDark = 0xFFFFFFFF;
409static const QRgb iconDisabledColorLight = 0x42000000;
410static const QRgb iconDisabledColorDark = 0x4CFFFFFF;
411static const QRgb raisedButtonColorLight = 0xFFD6D7D7;
412static const QRgb raisedButtonColorDark = 0x3FCCCCCC;
413static const QRgb raisedButtonDisabledColorLight = dividerColorLight;
414static const QRgb raisedButtonDisabledColorDark = dividerColorDark;
415static const QRgb frameColorLight = hintTextColorLight;
416static const QRgb frameColorDark = hintTextColorDark;
417static const QRgb switchUncheckedTrackColorLight = 0x42000000;
418static const QRgb switchUncheckedTrackColorDark = 0x4CFFFFFF;
419static const QRgb switchDisabledTrackColorLight = 0x1E000000;
420static const QRgb switchDisabledTrackColorDark = 0x19FFFFFF;
421static const QRgb rippleColorLight = 0x10000000;
422static const QRgb rippleColorDark = 0x20FFFFFF;
423static const QRgb spinBoxDisabledIconColorLight = 0xFFCCCCCC;
424static const QRgb spinBoxDisabledIconColorDark = 0xFF666666;
425
426static QQuickMaterialStyle::Theme effectiveTheme(QQuickMaterialStyle::Theme theme)
427{
428 if (theme == QQuickMaterialStyle::System)
429 theme = QQuickStylePrivate::isDarkSystemTheme() ? QQuickMaterialStyle::Dark : QQuickMaterialStyle::Light;
430 return theme;
431}
432
433QQuickMaterialStyle::QQuickMaterialStyle(QObject *parent) : QQuickAttachedObject(parent),
434 m_customPrimary(globalPrimaryCustom),
435 m_customAccent(globalAccentCustom),
436 m_customForeground(globalForegroundCustom),
437 m_customBackground(globalBackgroundCustom),
438 m_hasForeground(hasGlobalForeground),
439 m_hasBackground(hasGlobalBackground),
440 m_theme(globalTheme),
441 m_primary(globalPrimary),
442 m_accent(globalAccent),
443 m_foreground(globalForeground),
444 m_background(globalBackground)
445{
446 QQuickAttachedObject::init();
447}
448
449QQuickMaterialStyle *QQuickMaterialStyle::qmlAttachedProperties(QObject *object)
450{
451 return new QQuickMaterialStyle(object);
452}
453
454QQuickMaterialStyle::Theme QQuickMaterialStyle::theme() const
455{
456 return m_theme;
457}
458
459void QQuickMaterialStyle::setTheme(Theme theme)
460{
461 if (theme == System)
462 theme = QQuickStylePrivate::isDarkSystemTheme() ? Dark : Light;
463
464 m_explicitTheme = true;
465 if (m_theme == theme)
466 return;
467
468 m_theme = theme;
469 propagateTheme();
470 emit themeChanged();
471 emit paletteChanged();
472 if (!m_customAccent)
473 emit accentChanged();
474 if (!m_hasBackground)
475 emit backgroundChanged();
476 if (!m_hasForeground)
477 emit foregroundChanged();
478}
479
480void QQuickMaterialStyle::inheritTheme(Theme theme)
481{
482 if (m_explicitTheme || m_theme == theme)
483 return;
484
485 m_theme = theme;
486 propagateTheme();
487 emit themeChanged();
488 emit paletteChanged();
489 if (!m_customAccent)
490 emit accentChanged();
491 if (!m_hasBackground)
492 emit backgroundChanged();
493 if (!m_hasForeground)
494 emit foregroundChanged();
495}
496
497void QQuickMaterialStyle::propagateTheme()
498{
499 const auto styles = attachedChildren();
500 for (QQuickAttachedObject *child : styles) {
501 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(child);
502 if (material)
503 material->inheritTheme(m_theme);
504 }
505}
506
507void QQuickMaterialStyle::resetTheme()
508{
509 if (!m_explicitTheme)
510 return;
511
512 m_explicitTheme = false;
513 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(attachedParent());
514 inheritTheme(material ? material->theme() : globalTheme);
515}
516
517QVariant QQuickMaterialStyle::primary() const
518{
519 return primaryColor();
520}
521
522void QQuickMaterialStyle::setPrimary(const QVariant &var)
523{
524 QRgb primary = 0;
525 bool custom = false;
526 if (!variantToRgba(var, "primary", &primary, &custom))
527 return;
528
529 m_explicitPrimary = true;
530 if (m_primary == primary)
531 return;
532
533 m_customPrimary = custom;
534 m_primary = primary;
535 propagatePrimary();
536 emit primaryChanged();
537 emit paletteChanged();
538}
539
540void QQuickMaterialStyle::inheritPrimary(uint primary, bool custom)
541{
542 if (m_explicitPrimary || m_primary == primary)
543 return;
544
545 m_customPrimary = custom;
546 m_primary = primary;
547 propagatePrimary();
548 emit primaryChanged();
549 emit paletteChanged();
550}
551
552void QQuickMaterialStyle::propagatePrimary()
553{
554 const auto styles = attachedChildren();
555 for (QQuickAttachedObject *child : styles) {
556 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(child);
557 if (material)
558 material->inheritPrimary(m_primary, m_customPrimary);
559 }
560}
561
562void QQuickMaterialStyle::resetPrimary()
563{
564 if (!m_explicitPrimary)
565 return;
566
567 m_customPrimary = false;
568 m_explicitPrimary = false;
569 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(attachedParent());
570 if (material)
571 inheritPrimary(material->m_primary, material->m_customPrimary);
572 else
573 inheritPrimary(globalPrimary, false);
574}
575
576QVariant QQuickMaterialStyle::accent() const
577{
578 return accentColor();
579}
580
581void QQuickMaterialStyle::setAccent(const QVariant &var)
582{
583 QRgb accent = 0;
584 bool custom = false;
585 if (!variantToRgba(var, "accent", &accent, &custom))
586 return;
587
588 m_explicitAccent = true;
589 if (m_accent == accent)
590 return;
591
592 m_customAccent = custom;
593 m_accent = accent;
594 propagateAccent();
595 emit accentChanged();
596 emit paletteChanged();
597}
598
599void QQuickMaterialStyle::inheritAccent(uint accent, bool custom)
600{
601 if (m_explicitAccent || m_accent == accent)
602 return;
603
604 m_customAccent = custom;
605 m_accent = accent;
606 propagateAccent();
607 emit accentChanged();
608 emit paletteChanged();
609}
610
611void QQuickMaterialStyle::propagateAccent()
612{
613 const auto styles = attachedChildren();
614 for (QQuickAttachedObject *child : styles) {
615 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(child);
616 if (material)
617 material->inheritAccent(m_accent, m_customAccent);
618 }
619}
620
621void QQuickMaterialStyle::resetAccent()
622{
623 if (!m_explicitAccent)
624 return;
625
626 m_customAccent = false;
627 m_explicitAccent = false;
628 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(attachedParent());
629 if (material)
630 inheritAccent(material->m_accent, material->m_customAccent);
631 else
632 inheritAccent(globalAccent, false);
633}
634
635QVariant QQuickMaterialStyle::foreground() const
636{
637 if (!m_hasForeground)
638 return QColor::fromRgba(m_theme == Light ? primaryTextColorLight : primaryTextColorDark);
639 if (m_customForeground)
640 return QColor::fromRgba(m_foreground);
641 if (m_foreground > BlueGrey)
642 return QColor();
643 return QColor::fromRgba(colors[m_foreground][Shade500]);
644}
645
646void QQuickMaterialStyle::setForeground(const QVariant &var)
647{
648 QRgb foreground = 0;
649 bool custom = false;
650 if (!variantToRgba(var, "foreground", &foreground, &custom))
651 return;
652
653 m_hasForeground = true;
654 m_explicitForeground = true;
655 if (m_foreground == foreground)
656 return;
657
658 m_customForeground = custom;
659 m_foreground = foreground;
660 propagateForeground();
661 emit foregroundChanged();
662}
663
664void QQuickMaterialStyle::inheritForeground(uint foreground, bool custom, bool has)
665{
666 if (m_explicitForeground || m_foreground == foreground)
667 return;
668
669 m_hasForeground = has;
670 m_customForeground = custom;
671 m_foreground = foreground;
672 propagateForeground();
673 emit foregroundChanged();
674}
675
676void QQuickMaterialStyle::propagateForeground()
677{
678 const auto styles = attachedChildren();
679 for (QQuickAttachedObject *child : styles) {
680 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(child);
681 if (material)
682 material->inheritForeground(m_foreground, m_customForeground, m_hasForeground);
683 }
684}
685
686void QQuickMaterialStyle::resetForeground()
687{
688 if (!m_explicitForeground)
689 return;
690
691 m_hasForeground = false;
692 m_customForeground = false;
693 m_explicitForeground = false;
694 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(attachedParent());
695 inheritForeground(material ? material->m_foreground : globalForeground, true, material ? material->m_hasForeground : false);
696}
697
698QVariant QQuickMaterialStyle::background() const
699{
700 return backgroundColor();
701}
702
703void QQuickMaterialStyle::setBackground(const QVariant &var)
704{
705 QRgb background = 0;
706 bool custom = false;
707 if (!variantToRgba(var, "background", &background, &custom))
708 return;
709
710 m_hasBackground = true;
711 m_explicitBackground = true;
712 if (m_background == background)
713 return;
714
715 m_customBackground = custom;
716 m_background = background;
717 propagateBackground();
718 emit backgroundChanged();
719 emit paletteChanged();
720}
721
722void QQuickMaterialStyle::inheritBackground(uint background, bool custom, bool has)
723{
724 if (m_explicitBackground || m_background == background)
725 return;
726
727 m_hasBackground = has;
728 m_customBackground = custom;
729 m_background = background;
730 propagateBackground();
731 emit backgroundChanged();
732 emit paletteChanged();
733}
734
735void QQuickMaterialStyle::propagateBackground()
736{
737 const auto styles = attachedChildren();
738 for (QQuickAttachedObject *child : styles) {
739 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(child);
740 if (material)
741 material->inheritBackground(m_background, m_customBackground, m_hasBackground);
742 }
743}
744
745void QQuickMaterialStyle::resetBackground()
746{
747 if (!m_explicitBackground)
748 return;
749
750 m_hasBackground = false;
751 m_customBackground = false;
752 m_explicitBackground = false;
753 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(attachedParent());
754 inheritBackground(material ? material->m_background : globalBackground, true, material ? material->m_hasBackground : false);
755}
756
757int QQuickMaterialStyle::elevation() const
758{
759 return m_elevation;
760}
761
762void QQuickMaterialStyle::setElevation(int elevation)
763{
764 if (m_elevation == elevation)
765 return;
766
767 m_elevation = elevation;
768 emit elevationChanged();
769}
770
771void QQuickMaterialStyle::resetElevation()
772{
773 setElevation(0);
774}
775
776QColor QQuickMaterialStyle::primaryColor() const
777{
778 if (m_customPrimary)
779 return QColor::fromRgba(m_primary);
780 if (m_primary > BlueGrey)
781 return QColor();
782 return colors[m_primary][Shade500];
783}
784
785QColor QQuickMaterialStyle::accentColor(Shade shade) const
786{
787 if (m_customAccent)
788 return shade == themeShade() ? QColor::fromRgba(m_accent)
789 : this->shade(QColor::fromRgba(m_accent), shade);
790 if (m_accent > BlueGrey)
791 return QColor();
792 return colors[m_accent][shade];
793}
794
795QColor QQuickMaterialStyle::accentColor() const
796{
797 return accentColor(themeShade());
798}
799
800QColor QQuickMaterialStyle::backgroundColor(Shade shade) const
801{
802 if (!m_hasBackground)
803 return QColor::fromRgba(m_theme == Light ? backgroundColorLight : backgroundColorDark);
804 if (m_customBackground)
805 return shade == themeShade() ? QColor::fromRgba(m_background)
806 : this->shade(QColor::fromRgba(m_background), shade);
807 if (m_background > BlueGrey)
808 return QColor();
809 return colors[m_background][shade];
810}
811
812QColor QQuickMaterialStyle::backgroundColor() const
813{
814 return backgroundColor(themeShade());
815}
816
817QColor QQuickMaterialStyle::primaryTextColor() const
818{
819 return QColor::fromRgba(m_theme == Light ? primaryTextColorLight : primaryTextColorDark);
820}
821
822QColor QQuickMaterialStyle::primaryHighlightedTextColor() const
823{
824 if (m_explicitForeground)
825 return primaryTextColor();
826 return QColor::fromRgba(primaryTextColorDark);
827}
828
829QColor QQuickMaterialStyle::secondaryTextColor() const
830{
831 return QColor::fromRgba(m_theme == Light ? secondaryTextColorLight : secondaryTextColorDark);
832}
833
834QColor QQuickMaterialStyle::hintTextColor() const
835{
836 return QColor::fromRgba(m_theme == Light ? hintTextColorLight : hintTextColorDark);
837}
838
839QColor QQuickMaterialStyle::textSelectionColor() const
840{
841 QColor color = accentColor();
842 color.setAlphaF(0.4);
843 return color;
844}
845
846QColor QQuickMaterialStyle::dropShadowColor() const
847{
848 return QColor::fromRgba(0x40000000);
849}
850
851QColor QQuickMaterialStyle::dividerColor() const
852{
853 return QColor::fromRgba(m_theme == Light ? dividerColorLight : dividerColorDark);
854}
855
856QColor QQuickMaterialStyle::iconColor() const
857{
858 return QColor::fromRgba(m_theme == Light ? iconColorLight : iconColorDark);
859}
860
861QColor QQuickMaterialStyle::iconDisabledColor() const
862{
863 return QColor::fromRgba(m_theme == Light ? iconDisabledColorLight : iconDisabledColorDark);
864}
865
866QColor QQuickMaterialStyle::buttonColor(bool highlighted) const
867{
868 Shade shade = themeShade();
869
870 QColor color = Qt::transparent;
871
872 if (m_explicitBackground) {
873 color = backgroundColor(shade);
874 } else if (highlighted) {
875 color = accentColor(shade);
876 } else if (elevation() > 0) {
877 color = QColor::fromRgba(m_theme == Light ? raisedButtonColorLight
878 : raisedButtonColorDark);
879 }
880
881 return color;
882}
883
884QColor QQuickMaterialStyle::buttonColor() const
885{
886 return buttonColor(false);
887}
888
889QColor QQuickMaterialStyle::buttonDisabledColor() const
890{
891 if (elevation() > 0) {
892 return QColor::fromRgba(m_theme == Light ? raisedButtonDisabledColorLight
893 : raisedButtonDisabledColorDark);
894 } else {
895 return Qt::transparent;
896 }
897}
898
899QColor QQuickMaterialStyle::highlightedButtonColor() const
900{
901 return buttonColor(true);
902}
903
904QColor QQuickMaterialStyle::frameColor() const
905{
906 return QColor::fromRgba(m_theme == Light ? frameColorLight : frameColorDark);
907}
908
909QColor QQuickMaterialStyle::rippleColor() const
910{
911 return QColor::fromRgba(m_theme == Light ? rippleColorLight : rippleColorDark);
912}
913
914QColor QQuickMaterialStyle::highlightedRippleColor() const
915{
916 QColor pressColor = accentColor();
917 pressColor.setAlpha(m_theme == Light ? 30 : 50);
918 return pressColor;
919}
920
921QColor QQuickMaterialStyle::switchUncheckedTrackColor() const
922{
923 return QColor::fromRgba(m_theme == Light ? switchUncheckedTrackColorLight : switchUncheckedTrackColorDark);
924}
925
926QColor QQuickMaterialStyle::switchCheckedTrackColor() const
927{
928 QColor trackColor(accentColor());
929 trackColor.setAlphaF(0.5);
930 return trackColor;
931}
932
933QColor QQuickMaterialStyle::switchUncheckedHandleColor() const
934{
935 return m_theme == Light ? color(Grey, Shade50) : color(Grey, Shade400);
936}
937
938QColor QQuickMaterialStyle::switchCheckedHandleColor() const
939{
940 return m_theme == Light ? accentColor() : shade(accentColor(), Shade200);
941}
942
943QColor QQuickMaterialStyle::switchDisabledTrackColor() const
944{
945 return QColor::fromRgba(m_theme == Light ? switchDisabledTrackColorLight : switchDisabledTrackColorDark);
946}
947
948QColor QQuickMaterialStyle::switchDisabledHandleColor() const
949{
950 return m_theme == Light ? color(Grey, Shade400) : color(Grey, Shade800);
951}
952
953QColor QQuickMaterialStyle::scrollBarColor() const
954{
955 return QColor::fromRgba(m_theme == Light ? 0x40000000 : 0x40FFFFFF);
956}
957
958QColor QQuickMaterialStyle::scrollBarHoveredColor() const
959{
960 return QColor::fromRgba(m_theme == Light ? 0x60000000 : 0x60FFFFFF);
961}
962
963QColor QQuickMaterialStyle::scrollBarPressedColor() const
964{
965 return QColor::fromRgba(m_theme == Light ? 0x80000000 : 0x80FFFFFF);
966}
967
968QColor QQuickMaterialStyle::dialogColor() const
969{
970 if (m_hasBackground)
971 return backgroundColor();
972 return QColor::fromRgba(m_theme == Light ? dialogColorLight : dialogColorDark);
973}
974
975QColor QQuickMaterialStyle::backgroundDimColor() const
976{
977 return QColor::fromRgba(m_theme == Light ? 0x99303030 : 0x99fafafa);
978}
979
980QColor QQuickMaterialStyle::listHighlightColor() const
981{
982 return QColor::fromRgba(m_theme == Light ? 0x1e000000 : 0x1effffff);
983}
984
985QColor QQuickMaterialStyle::tooltipColor() const
986{
987 if (m_explicitBackground)
988 return backgroundColor();
989 return color(Grey, Shade700);
990}
991
992QColor QQuickMaterialStyle::toolBarColor() const
993{
994 if (m_explicitBackground)
995 return backgroundColor();
996 return primaryColor();
997}
998
999QColor QQuickMaterialStyle::toolTextColor() const
1000{
1001 if (m_hasForeground || m_customPrimary)
1002 return primaryTextColor();
1003
1004 switch (m_primary) {
1005 case Red:
1006 case Pink:
1007 case Purple:
1008 case DeepPurple:
1009 case Indigo:
1010 case Blue:
1011 case Teal:
1012 case DeepOrange:
1013 case Brown:
1014 case BlueGrey:
1015 return QColor::fromRgba(primaryTextColorDark);
1016
1017 case LightBlue:
1018 case Cyan:
1019 case Green:
1020 case LightGreen:
1021 case Lime:
1022 case Yellow:
1023 case Amber:
1024 case Orange:
1025 case Grey:
1026 return QColor::fromRgba(primaryTextColorLight);
1027
1028 default:
1029 break;
1030 }
1031
1032 return primaryTextColor();
1033}
1034
1035QColor QQuickMaterialStyle::spinBoxDisabledIconColor() const
1036{
1037 return QColor::fromRgba(m_theme == Light ? spinBoxDisabledIconColorLight : spinBoxDisabledIconColorDark);
1038}
1039
1040QColor QQuickMaterialStyle::color(QQuickMaterialStyle::Color color, QQuickMaterialStyle::Shade shade) const
1041{
1042 int count = sizeof(colors) / sizeof(colors[0]);
1043 if (color < 0 || color >= count)
1044 return QColor();
1045
1046 count = sizeof(colors[0]) / sizeof(colors[0][0]);
1047 if (shade < 0 || shade >= count)
1048 return QColor();
1049
1050 return colors[color][shade];
1051}
1052
1053static QColor lighterShade(const QColor &color, qreal amount)
1054{
1055 QColor hsl = color.toHsl();
1056 hsl.setHslF(hsl.hueF(), hsl.saturationF(), qBound<qreal>(0.0, hsl.lightnessF() + amount, 1.0), color.alphaF());
1057 return hsl.convertTo(color.spec());
1058}
1059
1060static QColor darkerShade(const QColor &color, qreal amount)
1061{
1062 QColor hsl = color.toHsl();
1063 hsl.setHslF(hsl.hueF(), hsl.saturationF(), qBound<qreal>(0.0, hsl.lightnessF() - amount, 1.0), color.alphaF());
1064 return hsl.convertTo(color.spec());
1065}
1066
1067QQuickMaterialStyle::Shade QQuickMaterialStyle::themeShade() const
1068{
1069 return m_theme == Light ? Shade500 : Shade200;
1070}
1071
1072/*
1073 * The following lightness values originate from the Material Design Color Generator project.
1074 *
1075 * The MIT License (MIT)
1076 *
1077 * Copyright (c) 2015 mbitson
1078 *
1079 * Permission is hereby granted, free of charge, to any person obtaining a copy
1080 * of this software and associated documentation files (the "Software"), to deal
1081 * in the Software without restriction, including without limitation the rights
1082 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1083 * copies of the Software, and to permit persons to whom the Software is
1084 * furnished to do so, subject to the following conditions:
1085 *
1086 * The above copyright notice and this permission notice shall be included in all
1087 * copies or substantial portions of the Software.
1088 *
1089 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1090 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1091 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1092 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1093 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1094 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1095 * SOFTWARE.
1096 */
1097
1098// Returns the same color, if shade == themeShade()
1099QColor QQuickMaterialStyle::shade(const QColor &color, Shade shade) const
1100{
1101 switch (shade) {
1102 case Shade50:
1103 return lighterShade(color, m_theme == Light ? 0.52 : 0.26);
1104 case Shade100:
1105 return lighterShade(color, m_theme == Light ? 0.37 : 0.11);
1106 case Shade200:
1107 return m_theme == Light ? lighterShade(color, 0.26) : color;
1108 case Shade300:
1109 return m_theme == Light ? lighterShade(color, 0.12) : darkerShade(color, 0.14);
1110 case Shade400:
1111 return m_theme == Light ? lighterShade(color, 0.06) : darkerShade(color, 0.20);
1112 case Shade500:
1113 return m_theme == Light ? color : darkerShade(color, 0.26);
1114 case Shade600:
1115 return darkerShade(color, m_theme == Light ? 0.06 : 0.32);
1116 case Shade700:
1117 return darkerShade(color, m_theme == Light ? 0.12 : 0.38);
1118 case Shade800:
1119 return darkerShade(color, m_theme == Light ? 0.18 : 0.44);
1120 case Shade900:
1121 return darkerShade(color, m_theme == Light ? 0.24 : 0.50);
1122 case ShadeA100:
1123 return lighterShade(color, m_theme == Light ? 0.54 : 0.28);
1124 case ShadeA200:
1125 return lighterShade(color, m_theme == Light ? 0.37 : 0.11);
1126 case ShadeA400:
1127 return m_theme == Light ? lighterShade(color, 0.06) : darkerShade(color, 0.20);
1128 case ShadeA700:
1129 return darkerShade(color, m_theme == Light ? 0.12 : 0.38);
1130 default:
1131 Q_UNREACHABLE();
1132 return QColor();
1133 }
1134}
1135
1136int QQuickMaterialStyle::touchTarget() const
1137{
1138 // https://material.io/guidelines/components/buttons.html#buttons-style
1139 return globalVariant == Dense ? 44 : 48;
1140}
1141
1142int QQuickMaterialStyle::buttonHeight() const
1143{
1144 // https://material.io/guidelines/components/buttons.html#buttons-style
1145 return globalVariant == Dense ? 32 : 36;
1146}
1147
1148int QQuickMaterialStyle::delegateHeight() const
1149{
1150 // https://material.io/guidelines/components/lists.html#lists-specs
1151 return globalVariant == Dense ? 40 : 48;
1152}
1153
1154int QQuickMaterialStyle::dialogButtonBoxHeight() const
1155{
1156 return globalVariant == Dense ? 48 : 52;
1157}
1158
1159int QQuickMaterialStyle::frameVerticalPadding() const
1160{
1161 return globalVariant == Dense ? 8 : 12;
1162}
1163
1164int QQuickMaterialStyle::menuItemHeight() const
1165{
1166 // https://material.io/guidelines/components/menus.html#menus-simple-menus
1167 return globalVariant == Dense ? 32 : 48;
1168}
1169
1170int QQuickMaterialStyle::menuItemVerticalPadding() const
1171{
1172 return globalVariant == Dense ? 8 : 12;
1173}
1174
1175int QQuickMaterialStyle::switchDelegateVerticalPadding() const
1176{
1177 // SwitchDelegate's indicator is much larger than the others due to the shadow,
1178 // so we must reduce its padding to ensure its implicitHeight is 40 when dense.
1179 return globalVariant == Dense ? 4 : 8;
1180}
1181
1182int QQuickMaterialStyle::tooltipHeight() const
1183{
1184 // https://material.io/guidelines/components/tooltips.html
1185 return globalVariant == Dense ? 22 : 32;
1186}
1187
1188QQuickMaterialStyle::Variant QQuickMaterialStyle::variant()
1189{
1190 return globalVariant;
1191}
1192
1193template <typename Enum>
1194static Enum toEnumValue(const QByteArray &value, bool *ok)
1195{
1196 QMetaEnum enumeration = QMetaEnum::fromType<Enum>();
1197 return static_cast<Enum>(enumeration.keyToValue(value, ok));
1198}
1199
1200static QByteArray resolveSetting(const QByteArray &env, const QSharedPointer<QSettings> &settings, const QString &name)
1201{
1202 QByteArray value = qgetenv(env);
1203#if QT_CONFIG(settings)
1204 if (value.isNull() && !settings.isNull())
1205 value = settings->value(name).toByteArray();
1206#endif
1207 return value;
1208}
1209
1210void QQuickMaterialStyle::initGlobals()
1211{
1212 QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(QStringLiteral("Material"));
1213
1214 bool ok = false;
1215 QByteArray themeValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_THEME", settings, QStringLiteral("Theme"));
1216 Theme themeEnum = toEnumValue<Theme>(themeValue, &ok);
1217 if (ok)
1218 globalTheme = effectiveTheme(themeEnum);
1219 else if (!themeValue.isEmpty())
1220 qWarning().nospace().noquote() << "Material: unknown theme value: " << themeValue;
1221
1222 QByteArray variantValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_VARIANT", settings, QStringLiteral("Variant"));
1223 Variant variantEnum = toEnumValue<Variant>(variantValue, &ok);
1224 if (ok)
1225 globalVariant = variantEnum;
1226 else if (!variantValue.isEmpty())
1227 qWarning().nospace().noquote() << "Material: unknown variant value: " << variantValue;
1228
1229 QByteArray primaryValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_PRIMARY", settings, QStringLiteral("Primary"));
1230 Color primaryEnum = toEnumValue<Color>(primaryValue, &ok);
1231 if (ok) {
1232 globalPrimaryCustom = false;
1233 globalPrimary = primaryEnum;
1234 } else {
1235 QColor color(primaryValue.constData());
1236 if (color.isValid()) {
1237 globalPrimaryCustom = true;
1238 globalPrimary = color.rgba();
1239 } else if (!primaryValue.isEmpty()) {
1240 qWarning().nospace().noquote() << "Material: unknown primary value: " << primaryValue;
1241 }
1242 }
1243
1244 QByteArray accentValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_ACCENT", settings, QStringLiteral("Accent"));
1245 Color accentEnum = toEnumValue<Color>(accentValue, &ok);
1246 if (ok) {
1247 globalAccentCustom = false;
1248 globalAccent = accentEnum;
1249 } else if (!accentValue.isEmpty()) {
1250 QColor color(accentValue.constData());
1251 if (color.isValid()) {
1252 globalAccentCustom = true;
1253 globalAccent = color.rgba();
1254 } else {
1255 qWarning().nospace().noquote() << "Material: unknown accent value: " << accentValue;
1256 }
1257 }
1258
1259 QByteArray foregroundValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_FOREGROUND", settings, QStringLiteral("Foreground"));
1260 Color foregroundEnum = toEnumValue<Color>(foregroundValue, &ok);
1261 if (ok) {
1262 globalForegroundCustom = false;
1263 globalForeground = foregroundEnum;
1264 hasGlobalForeground = true;
1265 } else if (!foregroundValue.isEmpty()) {
1266 QColor color(foregroundValue.constData());
1267 if (color.isValid()) {
1268 globalForegroundCustom = true;
1269 globalForeground = color.rgba();
1270 hasGlobalForeground = true;
1271 } else {
1272 qWarning().nospace().noquote() << "Material: unknown foreground value: " << foregroundValue;
1273 }
1274 }
1275
1276 QByteArray backgroundValue = resolveSetting("QT_QUICK_CONTROLS_MATERIAL_BACKGROUND", settings, QStringLiteral("Background"));
1277 Color backgroundEnum = toEnumValue<Color>(backgroundValue, &ok);
1278 if (ok) {
1279 globalBackgroundCustom = false;
1280 globalBackground = backgroundEnum;
1281 hasGlobalBackground = true;
1282 } else if (!backgroundValue.isEmpty()) {
1283 QColor color(backgroundValue.constData());
1284 if (color.isValid()) {
1285 globalBackgroundCustom = true;
1286 globalBackground = color.rgba();
1287 hasGlobalBackground = true;
1288 } else {
1289 qWarning().nospace().noquote() << "Material: unknown background value: " << backgroundValue;
1290 }
1291 }
1292}
1293
1294void QQuickMaterialStyle::attachedParentChange(QQuickAttachedObject *newParent, QQuickAttachedObject *oldParent)
1295{
1296 Q_UNUSED(oldParent);
1297 QQuickMaterialStyle *material = qobject_cast<QQuickMaterialStyle *>(newParent);
1298 if (material) {
1299 inheritPrimary(material->m_primary, material->m_customPrimary);
1300 inheritAccent(material->m_accent, material->m_customAccent);
1301 inheritForeground(material->m_foreground, material->m_customForeground, material->m_hasForeground);
1302 inheritBackground(material->m_background, material->m_customBackground, material->m_hasBackground);
1303 inheritTheme(material->theme());
1304 }
1305}
1306
1307bool QQuickMaterialStyle::variantToRgba(const QVariant &var, const char *name, QRgb *rgba, bool *custom) const
1308{
1309 *custom = false;
1310 if (var.type() == QVariant::Int) {
1311 int val = var.toInt();
1312 if (val > BlueGrey) {
1313 qmlWarning(parent()) << "unknown Material." << name << " value: " << val;
1314 return false;
1315 }
1316 *rgba = val;
1317 } else {
1318 int val = QMetaEnum::fromType<Color>().keyToValue(var.toByteArray());
1319 if (val != -1) {
1320 *rgba = val;
1321 } else {
1322 QColor color(var.toString());
1323 if (!color.isValid()) {
1324 qmlWarning(parent()) << "unknown Material." << name << " value: " << var.toString();
1325 return false;
1326 }
1327 *custom = true;
1328 *rgba = color.rgba();
1329 }
1330 }
1331 return true;
1332}
1333
1334QT_END_NAMESPACE
1335