Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickstyleplugin.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#include "qquickstyle.h"
8
9#include <QtCore/private/qfileselector_p.h>
10#include <QtCore/qloggingcategory.h>
11#include <QtGui/qstylehints.h>
12#include <QtGui/qguiapplication.h>
13#include <QtQml/qqmlengine.h>
14#include <QtQml/qqmlfile.h>
15#include <QtQml/private/qqmlmetatype_p.h>
16#include <QtQuickTemplates2/private/qquicktheme_p_p.h>
17
19
20Q_STATIC_LOGGING_CATEGORY(lcStylePlugin, "qt.quick.controls.styleplugin")
21
22class QQuickThemeChangeObserver : public QObject {
23 Q_OBJECT
24public:
25 explicit QQuickThemeChangeObserver()
26 {
27 QStyleHints *styleHints = QGuiApplication::styleHints();
28 moveToThread(styleHints->thread());
29 styleHints->installEventFilter(this);
30 }
31
32Q_SIGNALS:
33 void paletteOrThemeChanged();
34
35protected:
36 bool eventFilter(QObject *object, QEvent *event) override {
37 Q_UNUSED(object);
38 if (event->type() == QEvent::ApplicationPaletteChange || event->type() == QEvent::ThemeChange)
39 emit paletteOrThemeChanged();
40 return false;
41 }
42};
43
44// The custom destructor ensures that all posted events gets processed before the event filter is deleted.
45void QQuickStylePlugin::ObserverDeleter::operator()(QQuickThemeChangeObserver *observer)
46{
47 observer->disconnect();
48 observer->deleteLater();
49}
50
51QQuickStylePlugin::QQuickStylePlugin(QObject *parent)
52 : QQmlExtensionPlugin(parent)
53{
54}
55
56QQuickStylePlugin::~QQuickStylePlugin()
57{
58}
59
60void QQuickStylePlugin::registerTypes(const char *uri)
61{
62 qCDebug(lcStylePlugin).nospace() << "registerTypes called with uri " << uri << "; plugin name is " << name();
63
64 const QTypeRevision latestControlsRevision = QQmlMetaType::latestModuleVersion(QLatin1String("QtQuick.Controls"));
65 // Use the private function because we don't want to cause resolve() to be called,
66 // as the logic that assigns a default style if one wasn't set would interfere with compile-time style selection.
67 QString styleName = QQuickStylePrivate::style();
68 if (!latestControlsRevision.isValid() && styleName.isEmpty()) {
69 // The user hasn't imported QtQuick.Controls, nor set a style via the runtime methods.
70 qCDebug(lcStylePlugin).nospace() << uri << " imported before QtQuick.Controls; using compile-time style selection";
71 QQuickStyle::setStyle(name());
72 styleName = name();
73 }
74
75 // Even if this style plugin isn't for the style set by the user,
76 // we still want to create the theme object, because that function
77 // is also responsible for reading values from qtquickcontrols2.conf.
78 // So, even if a style doesn't have a QQuickTheme, it can still have
79 // values set for (e.g. fonts and palettes) in qtquickcontrols2.conf.
80 const QString effectiveCurrentStyleName = QQuickStylePrivate::effectiveStyleName(styleName);
81 auto theme = QQuickTheme::instance();
82 if (!theme) {
83 qCDebug(lcStylePlugin) << "creating theme";
84 theme = createTheme(effectiveCurrentStyleName);
85 }
86
87 // The primary fallback is the style set by the user. We need to check for that here
88 // so that we can ensure that fallback styles' themes are initialized (QTBUG-117403)
89 // without also allowing the Basic style to be initialized, as it is a secondary fallback
90 // for every built-in style (and only built-in styles can be fallbacks).
91 const bool thisPluginBelongsToCurrentStyle = name() == effectiveCurrentStyleName;
92 const bool isPrimaryFallback = name() == QQuickStylePrivate::fallbackStyle();
93 if (!thisPluginBelongsToCurrentStyle && !isPrimaryFallback) {
94 qCDebug(lcStylePlugin).nospace() << "this style plugin does not belong to the current ("
95 << effectiveCurrentStyleName << ") or fallback (" << QQuickStylePrivate::fallbackStyle()
96 << ") style; not calling initializeTheme()";
97 return;
98 }
99
100 if (thisPluginBelongsToCurrentStyle) {
101 qCDebug(lcStylePlugin).nospace() << "this style plugin belongs to the current style "
102 << effectiveCurrentStyleName << "; calling initializeTheme()";
103 } else {
104 qCDebug(lcStylePlugin).nospace() << "this style plugin belongs to the fallback style "
105 << QQuickStylePrivate::fallbackStyle() << "; calling initializeTheme()";
106 }
107 initializeTheme(theme);
108
109 themeChangeObserver.reset(new QQuickThemeChangeObserver);
110 connect(themeChangeObserver.get(), &QQuickThemeChangeObserver::paletteOrThemeChanged,
111 this, &QQuickStylePlugin::updateTheme);
112
113 if (!isPrimaryFallback && !styleName.isEmpty())
114 QFileSelectorPrivate::addStatics(QStringList() << styleName);
115}
116
117void QQuickStylePlugin::unregisterTypes()
118{
119 qCDebug(lcStylePlugin) << "unregisterTypes called; plugin name is" << name();
120 if (!QQuickThemePrivate::instance)
121 return;
122
123 themeChangeObserver.reset();
124
125 const bool isPrimaryFallback = name() == QQuickStylePrivate::fallbackStyle();
126 const QString styleName = QQuickStylePrivate::style();
127 if (!isPrimaryFallback && !styleName.isEmpty())
128 QFileSelectorPrivate::removeStatics(QStringList() << styleName);
129
130 // Not every style has a plugin - some styles are QML-only. So, we clean this
131 // stuff up when the first style plugin is unregistered rather than when the
132 // plugin for the current style is unregistered.
133 QQuickThemePrivate::instance.reset();
134 QQuickStylePrivate::reset();
135}
136
137/*!
138 \internal
139
140 Responsible for setting the font and palette settings that were specified in the
141 qtquickcontrols2.conf file.
142
143 Style-specific settings (e.g. Variant=Dense) are read in the constructor of the
144 appropriate style plugin (e.g. QtQuickControls2MaterialStylePlugin).
145
146 Implicit style-specific font and palette values are assigned in the relevant theme
147 (e.g. QQuickMaterialTheme).
148*/
149QQuickTheme *QQuickStylePlugin::createTheme(const QString &name)
150{
151 qCDebug(lcStylePlugin) << "creating QQuickTheme instance to be initialized by style-specific theme of" << name;
152
153 QQuickTheme *theme = new QQuickTheme;
154#if QT_CONFIG(settings)
155 QQuickThemePrivate *p = QQuickThemePrivate::get(theme);
156 QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(name);
157 if (settings) {
158 p->defaultFont.reset(QQuickStylePrivate::readFont(settings));
159 // Set the default font as the System scope, because that's what
160 // QQuickControlPrivate::parentFont() uses as its fallback if no
161 // parent item has a font explicitly set. QQuickControlPrivate::parentFont()
162 // is used as the starting point for font inheritance/resolution.
163 // The same goes for palettes below.
164 theme->setFont(QQuickTheme::System, *p->defaultFont);
165
166 p->defaultPalette.reset(QQuickStylePrivate::readPalette(settings));
167 theme->setPalette(QQuickTheme::System, *p->defaultPalette);
168 }
169#endif
170 QQuickThemePrivate::instance.reset(theme);
171 return theme;
172}
173
174QT_END_NAMESPACE
175
176#include "qquickstyleplugin.moc"
177#include "moc_qquickstyleplugin_p.cpp"
Combined button and popup list for selecting options.