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 "qquickmaterialtheme_p.h" |
5 | #include "qquickmaterialstyle_p.h" |
6 | |
7 | #include <QtGui/qpa/qplatformdialoghelper.h> |
8 | #include <QtGui/qfont.h> |
9 | #include <QtGui/qfontdatabase.h> |
10 | #include <QtQuickTemplates2/private/qquicktheme_p.h> |
11 | #include <QtCore/qmutex.h> |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | struct QQuickMaterialThemePrivate |
15 | { |
16 | static inline void addSystemStyle(QPointer<QQuickMaterialStyle> style); |
17 | static inline void removeSystemStyle(QPointer<QQuickMaterialStyle> style); |
18 | static inline void updateSystemStyles(); |
19 | |
20 | static inline std::vector<QPointer<QQuickMaterialStyle>> systemStyles = {}; |
21 | static inline QMutex mutex; |
22 | }; |
23 | |
24 | void QQuickMaterialThemePrivate::addSystemStyle(QPointer<QQuickMaterialStyle> style) |
25 | { |
26 | QMutexLocker locker{&mutex}; |
27 | auto it = std::find(first: systemStyles.begin(), last: systemStyles.end(), val: style); |
28 | if (it == systemStyles.end()) |
29 | systemStyles.push_back(x: style); |
30 | } |
31 | |
32 | void QQuickMaterialThemePrivate::removeSystemStyle(QPointer<QQuickMaterialStyle> style) |
33 | { |
34 | QMutexLocker locker{&mutex}; |
35 | auto it = std::find(first: systemStyles.begin(), last: systemStyles.end(), val: style); |
36 | if (it != systemStyles.end()) |
37 | systemStyles.erase(position: it); |
38 | } |
39 | |
40 | void QQuickMaterialThemePrivate::updateSystemStyles() |
41 | { |
42 | QMutexLocker locker{&mutex}; |
43 | for (auto it = systemStyles.begin(); it != systemStyles.end(); ) { |
44 | if (it->isNull()) { |
45 | it = systemStyles.erase(position: it); |
46 | } else { |
47 | (*it)->setTheme(QQuickMaterialStyle::System); |
48 | ++it; |
49 | } |
50 | } |
51 | } |
52 | |
53 | void QQuickMaterialTheme::initialize(QQuickTheme *theme) |
54 | { |
55 | QFont systemFont; |
56 | QFont buttonFont; |
57 | QFont toolTipFont; |
58 | QFont itemViewFont; |
59 | QFont listViewFont; |
60 | QFont ; |
61 | QFont editorFont; |
62 | |
63 | auto defaultFontFamily = QLatin1String("Roboto" ); |
64 | if (!QFontDatabase::hasFamily(family: defaultFontFamily)) { |
65 | defaultFontFamily = QLatin1String("Noto" ); // fallback |
66 | if (!QFontDatabase::hasFamily(family: defaultFontFamily)) |
67 | defaultFontFamily = {}; |
68 | } |
69 | |
70 | if (!defaultFontFamily.isEmpty()) { |
71 | const QStringList families{defaultFontFamily}; |
72 | systemFont.setFamilies(families); |
73 | buttonFont.setFamilies(families); |
74 | toolTipFont.setFamilies(families); |
75 | itemViewFont.setFamilies(families); |
76 | listViewFont.setFamilies(families); |
77 | menuItemFont.setFamilies(families); |
78 | editorFont.setFamilies(families); |
79 | } |
80 | |
81 | const bool dense = QQuickMaterialStyle::variant() == QQuickMaterialStyle::Dense; |
82 | systemFont.setPixelSize(dense ? 13 : 14); |
83 | theme->setFont(scope: QQuickTheme::System, font: systemFont); |
84 | |
85 | // https://material.io/guidelines/components/buttons.html#buttons-style |
86 | buttonFont.setPixelSize(dense ? 13 : 14); |
87 | buttonFont.setWeight(QFont::Medium); |
88 | theme->setFont(scope: QQuickTheme::Button, font: buttonFont); |
89 | theme->setFont(scope: QQuickTheme::TabBar, font: buttonFont); |
90 | theme->setFont(scope: QQuickTheme::ToolBar, font: buttonFont); |
91 | |
92 | // https://material.io/guidelines/components/tooltips.html |
93 | toolTipFont.setPixelSize(dense ? 10 : 14); |
94 | toolTipFont.setWeight(QFont::Medium); |
95 | theme->setFont(scope: QQuickTheme::ToolTip, font: toolTipFont); |
96 | |
97 | itemViewFont.setPixelSize(dense ? 13 : 14); |
98 | itemViewFont.setWeight(QFont::Medium); |
99 | theme->setFont(scope: QQuickTheme::ItemView, font: itemViewFont); |
100 | |
101 | // https://material.io/guidelines/components/lists.html#lists-specs |
102 | listViewFont.setPixelSize(dense ? 13 : 16); |
103 | theme->setFont(scope: QQuickTheme::ListView, font: listViewFont); |
104 | |
105 | menuItemFont.setPixelSize(dense ? 13 : 16); |
106 | theme->setFont(scope: QQuickTheme::Menu, font: menuItemFont); |
107 | theme->setFont(scope: QQuickTheme::MenuBar, font: menuItemFont); |
108 | theme->setFont(scope: QQuickTheme::ComboBox, font: menuItemFont); |
109 | |
110 | editorFont.setPixelSize(dense ? 13 : 16); |
111 | theme->setFont(scope: QQuickTheme::TextArea, font: editorFont); |
112 | theme->setFont(scope: QQuickTheme::TextField, font: editorFont); |
113 | theme->setFont(scope: QQuickTheme::SpinBox, font: editorFont); |
114 | } |
115 | |
116 | void QQuickMaterialTheme::registerSystemStyle(QQuickMaterialStyle *style) |
117 | { |
118 | QQuickMaterialThemePrivate::addSystemStyle(style: QPointer{style}); |
119 | } |
120 | |
121 | void QQuickMaterialTheme::unregisterSystemStyle(QQuickMaterialStyle *style) |
122 | { |
123 | QQuickMaterialThemePrivate::removeSystemStyle(style: QPointer{style}); |
124 | } |
125 | |
126 | void QQuickMaterialTheme::updateTheme() |
127 | { |
128 | QQuickMaterialThemePrivate::updateSystemStyles(); |
129 | } |
130 | |
131 | QT_END_NAMESPACE |
132 | |