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 Templates 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 "qquicktheme_p.h" 38 #include "qquicktheme_p_p.h" 39 40 #include <QtGui/qpa/qplatformtheme.h> 41 #include <QtGui/private/qguiapplication_p.h> 42 43 QT_BEGIN_NAMESPACE 44 45 QScopedPointer<QQuickTheme> QQuickThemePrivate::instance; 46 cleanup_instance()47static void cleanup_instance() 48 { 49 QQuickThemePrivate::instance.reset(); 50 } 51 install_instance_cleanuper()52static void install_instance_cleanuper() 53 { 54 qAddPostRoutine(cleanup_instance); 55 } 56 Q_COREAPP_STARTUP_FUNCTION(install_instance_cleanuper)57Q_COREAPP_STARTUP_FUNCTION(install_instance_cleanuper) 58 59 static QPlatformTheme::Font platformFont(QQuickTheme::Scope scope) 60 { 61 switch (scope) { 62 case QQuickTheme::Button: return QPlatformTheme::PushButtonFont; 63 case QQuickTheme::CheckBox: return QPlatformTheme::CheckBoxFont; 64 case QQuickTheme::ComboBox: return QPlatformTheme::ComboMenuItemFont; 65 case QQuickTheme::GroupBox: return QPlatformTheme::GroupBoxTitleFont; 66 case QQuickTheme::ItemView: return QPlatformTheme::ItemViewFont; 67 case QQuickTheme::Label: return QPlatformTheme::LabelFont; 68 case QQuickTheme::ListView: return QPlatformTheme::ListViewFont; 69 case QQuickTheme::Menu: return QPlatformTheme::MenuFont; 70 case QQuickTheme::MenuBar: return QPlatformTheme::MenuBarFont; 71 case QQuickTheme::RadioButton: return QPlatformTheme::RadioButtonFont; 72 case QQuickTheme::SpinBox: return QPlatformTheme::EditorFont; 73 case QQuickTheme::Switch: return QPlatformTheme::CheckBoxFont; 74 case QQuickTheme::TabBar: return QPlatformTheme::TabButtonFont; 75 case QQuickTheme::TextArea: return QPlatformTheme::EditorFont; 76 case QQuickTheme::TextField: return QPlatformTheme::EditorFont; 77 case QQuickTheme::ToolBar: return QPlatformTheme::ToolButtonFont; 78 case QQuickTheme::ToolTip: return QPlatformTheme::TipLabelFont; 79 case QQuickTheme::Tumbler: return QPlatformTheme::ItemViewFont; 80 default: return QPlatformTheme::SystemFont; 81 } 82 } 83 platformPalette(QQuickTheme::Scope scope)84static QPlatformTheme::Palette platformPalette(QQuickTheme::Scope scope) 85 { 86 switch (scope) { 87 case QQuickTheme::Button: return QPlatformTheme::ButtonPalette; 88 case QQuickTheme::CheckBox: return QPlatformTheme::CheckBoxPalette; 89 case QQuickTheme::ComboBox: return QPlatformTheme::ComboBoxPalette; 90 case QQuickTheme::GroupBox: return QPlatformTheme::GroupBoxPalette; 91 case QQuickTheme::ItemView: return QPlatformTheme::ItemViewPalette; 92 case QQuickTheme::Label: return QPlatformTheme::LabelPalette; 93 case QQuickTheme::ListView: return QPlatformTheme::ItemViewPalette; 94 case QQuickTheme::Menu: return QPlatformTheme::MenuPalette; 95 case QQuickTheme::MenuBar: return QPlatformTheme::MenuBarPalette; 96 case QQuickTheme::RadioButton: return QPlatformTheme::RadioButtonPalette; 97 case QQuickTheme::SpinBox: return QPlatformTheme::TextLineEditPalette; 98 case QQuickTheme::Switch: return QPlatformTheme::CheckBoxPalette; 99 case QQuickTheme::TabBar: return QPlatformTheme::TabBarPalette; 100 case QQuickTheme::TextArea: return QPlatformTheme::TextEditPalette; 101 case QQuickTheme::TextField: return QPlatformTheme::TextLineEditPalette; 102 case QQuickTheme::ToolBar: return QPlatformTheme::ToolButtonPalette; 103 case QQuickTheme::ToolTip: return QPlatformTheme::ToolTipPalette; 104 case QQuickTheme::Tumbler: return QPlatformTheme::ItemViewPalette; 105 default: return QPlatformTheme::SystemPalette; 106 } 107 } 108 QQuickTheme()109QQuickTheme::QQuickTheme() 110 : d_ptr(new QQuickThemePrivate) 111 { 112 } 113 ~QQuickTheme()114QQuickTheme::~QQuickTheme() 115 { 116 } 117 instance()118QQuickTheme *QQuickTheme::instance() 119 { 120 return QQuickThemePrivate::instance.data(); 121 } 122 font(Scope scope)123QFont QQuickTheme::font(Scope scope) 124 { 125 const QFont *font = nullptr; 126 if (QQuickTheme *theme = instance()) 127 font = QQuickThemePrivate::get(theme)->fonts[scope].data(); 128 else if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) 129 font = theme->font(platformFont(scope)); 130 131 if (font) { 132 QFont f = *font; 133 if (scope == System) 134 f.resolve(0); 135 return f; 136 } 137 138 if (scope != System) 139 return QQuickTheme::font(System); 140 141 return QFont(); 142 } 143 palette(Scope scope)144QPalette QQuickTheme::palette(Scope scope) 145 { 146 const QPalette *palette = nullptr; 147 if (QQuickTheme *theme = instance()) 148 palette = QQuickThemePrivate::get(theme)->palettes[scope].data(); 149 else if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) 150 palette = theme->palette(platformPalette(scope)); 151 152 if (palette) { 153 QPalette f = *palette; 154 if (scope == System) 155 f.resolve(0); 156 return f; 157 } 158 159 if (scope != System) 160 return QQuickTheme::palette(System); 161 162 return QPalette(); 163 } 164 setFont(Scope scope,const QFont & font)165void QQuickTheme::setFont(Scope scope, const QFont &font) 166 { 167 Q_D(QQuickTheme); 168 d->fonts[scope] = QSharedPointer<QFont>::create(d->defaultFont ? d->defaultFont->resolve(font) : font); 169 // See comment in QQuickControlPrivate::inheritFont 170 d->fonts[scope]->setFamilies(QStringList()); 171 } 172 setPalette(Scope scope,const QPalette & palette)173void QQuickTheme::setPalette(Scope scope, const QPalette &palette) 174 { 175 Q_D(QQuickTheme); 176 d->palettes[scope] = QSharedPointer<QPalette>::create(d->defaultPalette ? d->defaultPalette->resolve(palette) : palette); 177 } 178 179 QT_END_NAMESPACE 180