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
4#include "qquicktheme_p.h"
5#include "qquicktheme_p_p.h"
6
7#include <QtGui/qpa/qplatformtheme.h>
8#include <QtGui/private/qguiapplication_p.h>
9
10QT_BEGIN_NAMESPACE
11
12std::unique_ptr<QQuickTheme> QQuickThemePrivate::instance;
13
14static void cleanup_instance()
15{
16 QQuickThemePrivate::instance.reset();
17}
18
19static void install_instance_cleanuper()
20{
21 qAddPostRoutine(cleanup_instance);
22}
23
24Q_COREAPP_STARTUP_FUNCTION(install_instance_cleanuper)
25
26static QPlatformTheme::Font platformFont(QQuickTheme::Scope scope)
27{
28 switch (scope) {
29 case QQuickTheme::Button: return QPlatformTheme::PushButtonFont;
30 case QQuickTheme::CheckBox: return QPlatformTheme::CheckBoxFont;
31 case QQuickTheme::ComboBox: return QPlatformTheme::ComboMenuItemFont;
32 case QQuickTheme::GroupBox: return QPlatformTheme::GroupBoxTitleFont;
33 case QQuickTheme::ItemView: return QPlatformTheme::ItemViewFont;
34 case QQuickTheme::Label: return QPlatformTheme::LabelFont;
35 case QQuickTheme::ListView: return QPlatformTheme::ListViewFont;
36 case QQuickTheme::Menu: return QPlatformTheme::MenuFont;
37 case QQuickTheme::MenuBar: return QPlatformTheme::MenuBarFont;
38 case QQuickTheme::RadioButton: return QPlatformTheme::RadioButtonFont;
39 case QQuickTheme::SpinBox: return QPlatformTheme::EditorFont;
40 case QQuickTheme::Switch: return QPlatformTheme::CheckBoxFont;
41 case QQuickTheme::TabBar: return QPlatformTheme::TabButtonFont;
42 case QQuickTheme::TextArea: return QPlatformTheme::EditorFont;
43 case QQuickTheme::TextField: return QPlatformTheme::EditorFont;
44 case QQuickTheme::ToolBar: return QPlatformTheme::ToolButtonFont;
45 case QQuickTheme::ToolTip: return QPlatformTheme::TipLabelFont;
46 case QQuickTheme::Tumbler: return QPlatformTheme::ItemViewFont;
47 default: return QPlatformTheme::SystemFont;
48 }
49}
50
51static QPlatformTheme::Palette platformPalette(QQuickTheme::Scope scope)
52{
53 switch (scope) {
54 case QQuickTheme::Button: return QPlatformTheme::ButtonPalette;
55 case QQuickTheme::CheckBox: return QPlatformTheme::CheckBoxPalette;
56 case QQuickTheme::ComboBox: return QPlatformTheme::ComboBoxPalette;
57 case QQuickTheme::GroupBox: return QPlatformTheme::GroupBoxPalette;
58 case QQuickTheme::ItemView: return QPlatformTheme::ItemViewPalette;
59 case QQuickTheme::Label: return QPlatformTheme::LabelPalette;
60 case QQuickTheme::ListView: return QPlatformTheme::ItemViewPalette;
61 case QQuickTheme::Menu: return QPlatformTheme::MenuPalette;
62 case QQuickTheme::MenuBar: return QPlatformTheme::MenuBarPalette;
63 case QQuickTheme::RadioButton: return QPlatformTheme::RadioButtonPalette;
64 case QQuickTheme::SpinBox: return QPlatformTheme::TextLineEditPalette;
65 case QQuickTheme::Switch: return QPlatformTheme::CheckBoxPalette;
66 case QQuickTheme::TabBar: return QPlatformTheme::TabBarPalette;
67 case QQuickTheme::TextArea: return QPlatformTheme::TextEditPalette;
68 case QQuickTheme::TextField: return QPlatformTheme::TextLineEditPalette;
69 case QQuickTheme::ToolBar: return QPlatformTheme::ToolButtonPalette;
70 case QQuickTheme::ToolTip: return QPlatformTheme::ToolTipPalette;
71 case QQuickTheme::Tumbler: return QPlatformTheme::ItemViewPalette;
72 default: return QPlatformTheme::SystemPalette;
73 }
74}
75
76QQuickTheme::QQuickTheme()
77 : d_ptr(new QQuickThemePrivate)
78{
79}
80
81QQuickTheme::~QQuickTheme()
82{
83}
84
85QQuickTheme *QQuickTheme::instance()
86{
87 return QQuickThemePrivate::instance.get();
88}
89
90QFont QQuickTheme::font(Scope scope)
91{
92 const QFont *font = nullptr;
93 if (QQuickTheme *theme = instance())
94 font = QQuickThemePrivate::get(theme)->fonts[scope].data();
95 else if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
96 font = theme->font(type: platformFont(scope));
97
98 if (font) {
99 QFont f = *font;
100 if (scope == System)
101 f.setResolveMask(0);
102 return f;
103 }
104
105 if (scope != System)
106 return QQuickTheme::font(scope: System);
107
108 return QFont();
109}
110
111QPalette QQuickTheme::palette(Scope scope)
112{
113 const QPalette *palette = nullptr;
114
115 if (auto theme = instance()) {
116 if (theme->usePlatformPalette()) {
117 if (auto platformTheme = QGuiApplicationPrivate::platformTheme()) {
118 palette = platformTheme->palette(type: platformPalette(scope));
119 // In case, if palettes are provided through configuration file
120 // (qtquickcontrols2.conf), then respect configuration palette
121 // and resolve it with platform palette
122 if (palette) {
123 QQuickThemePrivate *p = QQuickThemePrivate::get(theme);
124 if (p->defaultPalette && p->defaultPalette->resolveMask() != 0) {
125 QPalette defPalette = *p->defaultPalette;
126 defPalette.resolve(other: *palette);
127 if (scope == System)
128 defPalette.setResolveMask(0);
129 return defPalette;
130 }
131 }
132 }
133 } else {
134 palette = QQuickThemePrivate::get(theme)->palettes[scope].data();
135 }
136 }
137
138 if (palette) {
139 QPalette f = *palette;
140 if (scope == System)
141 f.setResolveMask(0);
142 return f;
143 }
144
145 if (scope != System)
146 return QQuickTheme::palette(scope: System);
147
148 return QPalette();
149}
150
151void QQuickTheme::setFont(Scope scope, const QFont &font)
152{
153 Q_D(QQuickTheme);
154 d->fonts[scope] = QSharedPointer<QFont>::create(arguments: d->defaultFont ? d->defaultFont->resolve(font) : font);
155}
156
157void QQuickTheme::setPalette(Scope scope, const QPalette &palette)
158{
159 Q_D(QQuickTheme);
160 d->palettes[scope] = QSharedPointer<QPalette>::create(arguments: d->defaultPalette ? d->defaultPalette->resolve(other: palette) : palette);
161}
162
163QT_END_NAMESPACE
164

source code of qtdeclarative/src/quicktemplates/qquicktheme.cpp