Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtquickcontrols2plugin.cpp
Go to the documentation of this file.
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 <QtCore/private/qfileselector_p.h>
5#include <QtCore/qloggingcategory.h>
6#include <QtQml/qqmlengine.h>
7#include <QtQml/qqmlextensionplugin.h>
8#include <QtQuickTemplates2/private/qquicktheme_p_p.h>
9#include <QtQuickControls2/private/qquickstyle_p.h>
10#include <QtQuickControls2/private/qquickstyleplugin_p.h>
11#include <QtQuickControls2/qquickstyle.h>
12#include <QtQuickControls2/qtquickcontrols2global.h>
13
15
17
18Q_LOGGING_CATEGORY(lcQtQuickControls2Plugin, "qt.quick.controls.qtquickcontrols2plugin")
19
21{
24
25public:
26 QtQuickControls2Plugin(QObject *parent = nullptr);
28
29 void registerTypes(const char *uri) override;
30 void unregisterTypes() override;
31
32private:
33 // We store these because the style plugins can be unregistered before
34 // QtQuickControls2Plugin, and since QQuickStylePlugin calls QQuickStylePrivate::reset(),
35 // the style information can be lost when it comes time to call qmlUnregisterModuleImport().
36 // It also avoids unnecessarily resolving the style after resetting it just to get the style
37 // name in unregisterTypes().
38 bool customStyle = false;
39 QString registeredStyleUri;
40 QString registeredFallbackStyleUri;
41 QString rawFallbackStyleName;
42};
43
44static const char *qtQuickControlsUri = "QtQuick.Controls";
45
47{
48 const QString style = QQuickStyle::name();
50 // The style set is a built-in style.
51 const QString styleName = QQuickStylePrivate::effectiveStyleName(style);
52 return QString::fromLatin1("QtQuick.Controls.%1").arg(styleName);
53 }
54
55 // This is a custom style, so just use the name as the import uri.
56 QString styleName = style;
57 if (styleName.startsWith(QLatin1String(":/")))
58 styleName.remove(0, 2);
59 return styleName;
60}
61
63{
64 // The fallback style must be a built-in style, so we don't need to check for custom styles here.
65 const QString fallbackStyle = QQuickStylePrivate::fallbackStyle();
66 const QString fallbackStyleName = QQuickStylePrivate::effectiveStyleName(fallbackStyle);
67 return QString::fromLatin1("QtQuick.Controls.%1").arg(fallbackStyleName);
68}
69
75
77{
78 // Intentionally empty: we use register/unregisterTypes() to do
79 // initialization and cleanup, as plugins are not unloaded on macOS.
80}
81
135{
136 qCDebug(lcQtQuickControls2Plugin) << "registerTypes() called with uri" << uri;
137
138 // It's OK that the style is resolved more than once; some accessors like name() cause it to be called, for example.
140
141 // The fallback style that was set via env var/.conf/C++.
142 rawFallbackStyleName = QQuickStylePrivate::fallbackStyle();
143 // The style that was set via env var/.conf/C++, or Basic if none was set.
145 // The effective fallback style: rawFallbackStyleName, or Basic if empty.
146 const QString fallbackStyleName = QQuickStylePrivate::effectiveStyleName(rawFallbackStyleName);
147 qCDebug(lcQtQuickControls2Plugin) << "style:" << QQuickStyle::name() << "effective style:" << styleName
148 << "fallback style:" << rawFallbackStyleName << "effective fallback style:" << fallbackStyleName;
149
150 customStyle = QQuickStylePrivate::isCustomStyle();
151 // The URI of the current style. For built-in styles, the style name is appended to "QtQuick.Controls.".
152 // For custom styles that are embedded in resources, we need to remove the ":/" prefix.
153 registeredStyleUri = ::styleUri();
154
155 // If the style is Basic, we don't need to register the fallback because the Basic style
156 // provides all controls. Also, if we didn't return early here, we can get an infinite import loop
157 // when the style is set to Basic.
158 if (styleName != fallbackStyleName && styleName != QLatin1String("Basic")) {
159 // If no specific fallback is given, the fallback is of lower precedence than recursive
160 // imports of the main style (i.e. IMPORTS in a style's CMakeLists.txt).
161 // If a specific fallback is given, it is of higher precedence.
162
163 QString parentModule;
164 QString fallbackModule;
165
166 // The fallback style has to be a built-in style, so it will become "QtQuick.Controls.<fallback>".
167 registeredFallbackStyleUri = ::fallbackStyleUri();
168
169 if (!rawFallbackStyleName.isEmpty()) {
170 parentModule = qtQuickControlsUri;
171 fallbackModule = registeredFallbackStyleUri;
172 } else if (customStyle) {
173 // Since we don't know the versioning scheme of custom styles, but we want the
174 // version of QtQuick.Controls to be propagated, we need to do our own indirection.
175 // QtQuick.Controls.IndirectBasic indirectly imports QtQuick.Controls.Basic
176 Q_ASSERT(registeredFallbackStyleUri == QLatin1String("QtQuick.Controls.Basic"));
177 parentModule = qtQuickControlsUri;
178 fallbackModule = QLatin1String("QtQuick.Controls.IndirectBasic");
179 } else {
180 parentModule = registeredStyleUri;
181 fallbackModule = registeredFallbackStyleUri;
182 }
183
184 qCDebug(lcQtQuickControls2Plugin)
185 << "calling qmlRegisterModuleImport() to register fallback style with"
186 << " uri \"" << parentModule << "\" moduleMajor" << QQmlModuleImportModuleAny
187 << "import" << fallbackModule << "importMajor" << QQmlModuleImportAuto;
188 // Whenever parentModule is imported, registeredFallbackStyleUri will be imported too.
189 // The fallback style must be a built-in style, so we match the version number.
190 qmlRegisterModuleImport(parentModule.toUtf8().constData(), QQmlModuleImportModuleAny,
191 fallbackModule.toUtf8().constData(),
193 }
194
195 // If the user imports QtQuick.Controls 2.15, and they're using the Material style, we should import version 2.15.
196 // However, if they import QtQuick.Controls 2.15, but are using a custom style, we want to use the latest version
197 // number of their style.
198 const int importMajor = customStyle ? QQmlModuleImportLatest : QQmlModuleImportAuto;
199 qCDebug(lcQtQuickControls2Plugin).nospace()
200 << "calling qmlRegisterModuleImport() to register primary style with"
201 << " uri \"" << qtQuickControlsUri << "\" moduleMajor " << importMajor
202 << " import " << registeredStyleUri << " importMajor " << importMajor;
203 // When QtQuick.Controls is imported, the selected style will be imported too.
205 registeredStyleUri.toUtf8().constData(), importMajor);
206
207 if (customStyle)
209}
210
212{
213 qCDebug(lcQtQuickControls2Plugin) << "unregisterTypes() called";
214
215 const int importMajor = customStyle ? QQmlModuleImportLatest : QQmlModuleImportAuto;
216 qCDebug(lcQtQuickControls2Plugin).nospace()
217 << "calling qmlUnregisterModuleImport() to unregister primary style with"
218 << " uri \"" << qtQuickControlsUri << "\" moduleMajor " << importMajor
219 << " import " << registeredStyleUri << " importMajor " << importMajor;
221 registeredStyleUri.toUtf8().constData(), importMajor);
222
223 if (!registeredFallbackStyleUri.isEmpty()) {
224 QString parentModule;
225 QString fallbackModule;
226
227 if (!rawFallbackStyleName.isEmpty()) {
228 parentModule = qtQuickControlsUri;
229 fallbackModule = registeredFallbackStyleUri;
230 rawFallbackStyleName.clear();
231 } else if (customStyle) {
232 parentModule = qtQuickControlsUri;
233 fallbackModule = QLatin1String("QtQuick.Controls.IndirectBasic");
234 } else {
235 parentModule = registeredStyleUri;
236 fallbackModule = registeredFallbackStyleUri;
237 }
238
239 qCDebug(lcQtQuickControls2Plugin)
240 << "calling qmlUnregisterModuleImport() to unregister fallback style with"
241 << " uri \"" << parentModule << "\" moduleMajor" << QQmlModuleImportModuleAny
242 << "import" << fallbackModule << "importMajor" << QQmlModuleImportAuto;
243 qmlUnregisterModuleImport(parentModule.toUtf8().constData(), QQmlModuleImportModuleAny,
244 fallbackModule.toUtf8().constData(),
246
247 registeredFallbackStyleUri.clear();
248 }
249
250 customStyle = false;
251 registeredStyleUri.clear();
252}
253
255
256#include "qtquickcontrols2plugin.moc"
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
static void addStatics(const QStringList &)
\inmodule QtCore
Definition qobject.h:103
The QQmlExtensionPlugin class provides an abstract base for custom QML extension plugins with custom ...
static QString effectiveStyleName(const QString &styleName)
static bool isCustomStyle()
static void init()
static QString fallbackStyle()
static QString name()
Returns the name of the application style.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5455
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
QString & remove(qsizetype i, qsizetype len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition qstring.cpp:3466
QByteArray toUtf8() const &
Definition qstring.h:634
QtQuickControls2Plugin(QObject *parent=nullptr)
void registerTypes(const char *uri) override
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
Q_QML_EXPORT void qmlRegisterModuleImport(const char *uri, int moduleMajor, const char *import, int importMajor=QQmlModuleImportLatest, int importMinor=QQmlModuleImportLatest)
@ QQmlModuleImportLatest
Definition qqml.h:649
@ QQmlModuleImportModuleAny
Definition qqml.h:648
@ QQmlModuleImportAuto
Definition qqml.h:650
Q_QML_EXPORT void qmlUnregisterModuleImport(const char *uri, int moduleMajor, const char *import, int importMajor=QQmlModuleImportLatest, int importMinor=QQmlModuleImportLatest)
#define QQmlExtensionInterface_iid
static const QQmlModuleRegistration registration("QtQml", qml_register_types_QtQml)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define Q_OBJECT
#define Q_PLUGIN_METADATA(x)
#define Q_UNUSED(x)
QT_BEGIN_NAMESPACE Q_QUICKCONTROLS2_EXPORT void qml_register_types_QtQuick_Controls()
QString fallbackStyleUri()
static const char * qtQuickControlsUri
QString styleUri()
#define Q_GHS_KEEP_REFERENCE(S)