1 | // Copyright (C) 2020 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 "qquickstyle.h" |
5 | #include "qquickstyle_p.h" |
6 | #include "qquickstyleplugin_p.h" |
7 | |
8 | #include <QtCore/private/qfileselector_p.h> |
9 | #include <QtCore/qloggingcategory.h> |
10 | #include <QtGui/qstylehints.h> |
11 | #include <QtGui/qguiapplication.h> |
12 | #include <QtQml/qqmlengine.h> |
13 | #include <QtQml/qqmlfile.h> |
14 | #include <QtQml/private/qqmlmetatype_p.h> |
15 | #include <QtQuickTemplates2/private/qquicktheme_p_p.h> |
16 | |
17 | QT_BEGIN_NAMESPACE |
18 | |
19 | Q_LOGGING_CATEGORY(lcStylePlugin, "qt.quick.controls.styleplugin" ) |
20 | |
21 | QQuickStylePlugin::QQuickStylePlugin(QObject *parent) |
22 | : QQmlExtensionPlugin(parent) |
23 | { |
24 | } |
25 | |
26 | QQuickStylePlugin::~QQuickStylePlugin() |
27 | { |
28 | } |
29 | |
30 | void QQuickStylePlugin::registerTypes(const char *uri) |
31 | { |
32 | qCDebug(lcStylePlugin).nospace() << "registerTypes called with uri " << uri << "; plugin name is " << name(); |
33 | |
34 | const QTypeRevision latestControlsRevision = QQmlMetaType::latestModuleVersion(uri: QLatin1String("QtQuick.Controls" )); |
35 | // Use the private function because we don't want to cause resolve() to be called, |
36 | // as the logic that assigns a default style if one wasn't set would interfere with compile-time style selection. |
37 | QString styleName = QQuickStylePrivate::style(); |
38 | if (!latestControlsRevision.isValid() && styleName.isEmpty()) { |
39 | // The user hasn't imported QtQuick.Controls, nor set a style via the runtime methods. |
40 | qCDebug(lcStylePlugin).nospace() << uri << " imported before QtQuick.Controls; using compile-time style selection" ; |
41 | QQuickStyle::setStyle(name()); |
42 | styleName = name(); |
43 | } |
44 | |
45 | // Even if this style plugin isn't for the style set by the user, |
46 | // we still want to create the theme object, because that function |
47 | // is also responsible for reading values from qtquickcontrols2.conf. |
48 | // So, even if a style doesn't have a QQuickTheme, it can still have |
49 | // values set for (e.g. fonts and palettes) in qtquickcontrols2.conf. |
50 | const QString effectiveCurrentStyleName = QQuickStylePrivate::effectiveStyleName(styleName); |
51 | auto theme = QQuickTheme::instance(); |
52 | if (!theme) { |
53 | qCDebug(lcStylePlugin) << "creating theme" ; |
54 | theme = createTheme(name: effectiveCurrentStyleName); |
55 | } |
56 | |
57 | // The primary fallback is the style set by the user. We need to check for that here |
58 | // so that we can ensure that fallback styles' themes are initialized (QTBUG-117403) |
59 | // without also allowing the Basic style to be initialized, as it is a secondary fallback |
60 | // for every built-in style (and only built-in styles can be fallbacks). |
61 | const bool thisPluginBelongsToCurrentStyle = name() == effectiveCurrentStyleName; |
62 | const bool isPrimaryFallback = name() == QQuickStylePrivate::fallbackStyle(); |
63 | if (!thisPluginBelongsToCurrentStyle && !isPrimaryFallback) { |
64 | qCDebug(lcStylePlugin).nospace() << "this style plugin does not belong to the current (" |
65 | << effectiveCurrentStyleName << ") or fallback (" << QQuickStylePrivate::fallbackStyle() |
66 | << ") style; not calling initializeTheme()" ; |
67 | return; |
68 | } |
69 | |
70 | if (thisPluginBelongsToCurrentStyle) { |
71 | qCDebug(lcStylePlugin).nospace() << "this style plugin belongs to the current style " |
72 | << effectiveCurrentStyleName << "; calling initializeTheme()" ; |
73 | } else { |
74 | qCDebug(lcStylePlugin).nospace() << "this style plugin belongs to the fallback style " |
75 | << QQuickStylePrivate::fallbackStyle() << "; calling initializeTheme()" ; |
76 | } |
77 | initializeTheme(theme); |
78 | connect(sender: QGuiApplication::styleHints(), signal: &QStyleHints::colorSchemeChanged, |
79 | context: this, slot: &QQuickStylePlugin::updateTheme); |
80 | |
81 | if (!isPrimaryFallback && !styleName.isEmpty()) |
82 | QFileSelectorPrivate::addStatics(QStringList() << styleName); |
83 | } |
84 | |
85 | void QQuickStylePlugin::unregisterTypes() |
86 | { |
87 | qCDebug(lcStylePlugin) << "unregisterTypes called; plugin name is" << name(); |
88 | if (!QQuickThemePrivate::instance) |
89 | return; |
90 | |
91 | disconnect(sender: QGuiApplication::styleHints(), signal: &QStyleHints::colorSchemeChanged, |
92 | receiver: this, slot: &QQuickStylePlugin::updateTheme); |
93 | |
94 | // Not every style has a plugin - some styles are QML-only. So, we clean this |
95 | // stuff up when the first style plugin is unregistered rather than when the |
96 | // plugin for the current style is unregistered. |
97 | QQuickThemePrivate::instance.reset(); |
98 | QQuickStylePrivate::reset(); |
99 | } |
100 | |
101 | /*! |
102 | \internal |
103 | |
104 | Responsible for setting the font and palette settings that were specified in the |
105 | qtquickcontrols2.conf file. |
106 | |
107 | Style-specific settings (e.g. Variant=Dense) are read in the constructor of the |
108 | appropriate style plugin (e.g. QtQuickControls2MaterialStylePlugin). |
109 | |
110 | Implicit style-specific font and palette values are assigned in the relevant theme |
111 | (e.g. QQuickMaterialTheme). |
112 | */ |
113 | QQuickTheme *QQuickStylePlugin::createTheme(const QString &name) |
114 | { |
115 | qCDebug(lcStylePlugin) << "creating QQuickTheme instance to be initialized by style-specific theme of" << name; |
116 | |
117 | QQuickTheme *theme = new QQuickTheme; |
118 | #if QT_CONFIG(settings) |
119 | QQuickThemePrivate *p = QQuickThemePrivate::get(theme); |
120 | QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(name); |
121 | if (settings) { |
122 | p->defaultFont.reset(other: QQuickStylePrivate::readFont(settings)); |
123 | // Set the default font as the System scope, because that's what |
124 | // QQuickControlPrivate::parentFont() uses as its fallback if no |
125 | // parent item has a font explicitly set. QQuickControlPrivate::parentFont() |
126 | // is used as the starting point for font inheritance/resolution. |
127 | // The same goes for palettes below. |
128 | theme->setFont(scope: QQuickTheme::System, font: *p->defaultFont); |
129 | |
130 | p->defaultPalette.reset(other: QQuickStylePrivate::readPalette(settings)); |
131 | theme->setPalette(scope: QQuickTheme::System, palette: *p->defaultPalette); |
132 | } |
133 | #endif |
134 | QQuickThemePrivate::instance.reset(p: theme); |
135 | return theme; |
136 | } |
137 | |
138 | QT_END_NAMESPACE |
139 | |
140 | #include "moc_qquickstyleplugin_p.cpp" |
141 | |