aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickcontrols/qtquickcontrols2plugin.cpp
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2022-11-18 15:15:16 +0800
committerMitch Curtis <mitch.curtis@qt.io>2022-12-01 10:26:20 +0800
commit4bd87b903b355b53e3105ba1ae7c154c4e55cdaf (patch)
treecc2edb597f0d5871302eb86e9dda78217384a5aa /src/quickcontrols/qtquickcontrols2plugin.cpp
parent786e1748d4469c135a922a221024f3f9c421c0de (diff)
Remove "2" from Qt Quick Controls directories
Qt Quick Controls 2 was named that way because it was a follow-up to Qt Quick Controls 1.x. Now that Qt Quick Controls 1 is no longer supported, we don't need to have "2" in the name. Work on this was already started for the documentation in 1abdfe5d5a052f2298b7bf657513dfa7e0c66a56. By doing this renaming a few weeks before feature freeze, it won't affect the release but still results in as little time possible spent manually fixing conflicts in cherry-picks from non-LTS releases as a result of the renaming. This patch does the following: - Renames directories. - Adapts CMakeLists.txt and other files to account for the new paths. A follow-up patch will handle documentation. It does not touch library names or other user-facing stuff, as that will have to be done in Qt 7. Task-number: QTBUG-95413 Change-Id: I170d8db19033ee71e495ff0c5c1a517a41ed7634 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quickcontrols/qtquickcontrols2plugin.cpp')
-rw-r--r--src/quickcontrols/qtquickcontrols2plugin.cpp140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/quickcontrols/qtquickcontrols2plugin.cpp b/src/quickcontrols/qtquickcontrols2plugin.cpp
new file mode 100644
index 0000000000..8e30e8428b
--- /dev/null
+++ b/src/quickcontrols/qtquickcontrols2plugin.cpp
@@ -0,0 +1,140 @@
+// Copyright (C) 2017 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#include <QtCore/private/qfileselector_p.h>
+#include <QtCore/qloggingcategory.h>
+#include <QtQml/qqmlengine.h>
+#include <QtQml/qqmlextensionplugin.h>
+#include <QtQuickTemplates2/private/qquicktheme_p_p.h>
+#include <QtQuickControls2/private/qquickstyle_p.h>
+#include <QtQuickControls2/private/qquickstyleplugin_p.h>
+#include <QtQuickControls2/qquickstyle.h>
+#include <QtQuickControls2/qtquickcontrols2global.h>
+
+QT_BEGIN_NAMESPACE
+
+Q_GHS_KEEP_REFERENCE(qml_register_types_QtQuick_Controls);
+
+Q_LOGGING_CATEGORY(lcQtQuickControls2Plugin, "qt.quick.controls.qtquickcontrols2plugin")
+
+class QtQuickControls2Plugin : public QQmlExtensionPlugin
+{
+ Q_OBJECT
+ Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
+
+public:
+ QtQuickControls2Plugin(QObject *parent = nullptr);
+ ~QtQuickControls2Plugin();
+
+ void registerTypes(const char *uri) override;
+ void unregisterTypes() override;
+
+private:
+ // We store these because the style plugins can be unregistered before
+ // QtQuickControls2Plugin, and since QQuickStylePlugin calls QQuickStylePrivate::reset(),
+ // the style information can be lost when it comes time to call qmlUnregisterModuleImport().
+ // It also avoids unnecessarily resolving the style after resetting it just to get the style
+ // name in unregisterTypes().
+ bool customStyle = false;
+ QString registeredStyleUri;
+ QString registeredFallbackStyleUri;
+};
+
+static const char *qtQuickControlsUri = "QtQuick.Controls";
+
+QString styleUri()
+{
+ const QString style = QQuickStyle::name();
+ if (!QQuickStylePrivate::isCustomStyle()) {
+ // The style set is a built-in style.
+ const QString styleName = QQuickStylePrivate::effectiveStyleName(style);
+ return QString::fromLatin1("QtQuick.Controls.%1").arg(styleName);
+ }
+
+ // This is a custom style, so just use the name as the import uri.
+ QString styleName = style;
+ if (styleName.startsWith(QLatin1String(":/")))
+ styleName.remove(0, 2);
+ return styleName;
+}
+
+QString fallbackStyleUri()
+{
+ // The fallback style must be a built-in style, so we don't need to check for custom styles here.
+ const QString fallbackStyle = QQuickStylePrivate::fallbackStyle();
+ const QString fallbackStyleName = QQuickStylePrivate::effectiveStyleName(fallbackStyle);
+ return QString::fromLatin1("QtQuick.Controls.%1").arg(fallbackStyleName);
+}
+
+QtQuickControls2Plugin::QtQuickControls2Plugin(QObject *parent) : QQmlExtensionPlugin(parent)
+{
+ volatile auto registration = &qml_register_types_QtQuick_Controls;
+ Q_UNUSED(registration);
+}
+
+QtQuickControls2Plugin::~QtQuickControls2Plugin()
+{
+ // Intentionally empty: we use register/unregisterTypes() to do
+ // initialization and cleanup, as plugins are not unloaded on macOS.
+}
+
+void QtQuickControls2Plugin::registerTypes(const char *uri)
+{
+ qCDebug(lcQtQuickControls2Plugin) << "registerTypes() called with uri" << uri;
+
+ // It's OK that the style is resolved more than once; some accessors like name() cause it to be called, for example.
+ QQuickStylePrivate::init();
+
+ const QString styleName = QQuickStylePrivate::effectiveStyleName(QQuickStyle::name());
+ const QString fallbackStyleName = QQuickStylePrivate::effectiveStyleName(QQuickStylePrivate::fallbackStyle());
+ qCDebug(lcQtQuickControls2Plugin) << "style:" << QQuickStyle::name() << "effective style:" << styleName
+ << "fallback style:" << QQuickStylePrivate::fallbackStyle() << "effective fallback style:" << fallbackStyleName;
+
+ // If the style is Basic, we don't need to register the fallback because the Basic style
+ // provides all controls. Also, if we didn't return early here, we can get an infinite import loop
+ // when the style is set to Basic.
+ if (styleName != fallbackStyleName && styleName != QLatin1String("Basic")) {
+ registeredFallbackStyleUri = ::fallbackStyleUri();
+ qCDebug(lcQtQuickControls2Plugin) << "calling qmlRegisterModuleImport() to register fallback style with"
+ << " uri \"" << qtQuickControlsUri << "\" moduleMajor" << QQmlModuleImportModuleAny
+ << "import" << registeredFallbackStyleUri << "importMajor" << QQmlModuleImportAuto;
+ // The fallback style must be a built-in style, so we match the version number.
+ qmlRegisterModuleImport(qtQuickControlsUri, QQmlModuleImportModuleAny, registeredFallbackStyleUri.toUtf8().constData(),
+ QQmlModuleImportAuto, QQmlModuleImportAuto);
+ }
+
+ // If the user imports QtQuick.Controls 2.15, and they're using the Material style, we should import version 2.15.
+ // However, if they import QtQuick.Controls 2.15, but are using a custom style, we want to use the latest version
+ // number of their style.
+ customStyle = QQuickStylePrivate::isCustomStyle();
+ registeredStyleUri = ::styleUri();
+ const int importMajor = !customStyle ? QQmlModuleImportAuto : QQmlModuleImportLatest;
+ qCDebug(lcQtQuickControls2Plugin).nospace() << "calling qmlRegisterModuleImport() to register primary style with"
+ << " uri \"" << qtQuickControlsUri << "\" moduleMajor " << importMajor
+ << " import " << registeredStyleUri << " importMajor " << importMajor;
+ qmlRegisterModuleImport(qtQuickControlsUri, QQmlModuleImportModuleAny, registeredStyleUri.toUtf8().constData(), importMajor);
+
+ if (customStyle)
+ QFileSelectorPrivate::addStatics(QStringList() << styleName);
+}
+
+void QtQuickControls2Plugin::unregisterTypes()
+{
+ qCDebug(lcQtQuickControls2Plugin) << "unregisterTypes() called";
+
+ if (!registeredFallbackStyleUri.isEmpty()) {
+ // We registered a fallback style, so now we need to unregister it.
+ qmlUnregisterModuleImport(qtQuickControlsUri, QQmlModuleImportModuleAny, registeredFallbackStyleUri.toUtf8().constData(),
+ QQmlModuleImportAuto, QQmlModuleImportAuto);
+ registeredFallbackStyleUri.clear();
+ }
+
+ const int importMajor = !customStyle ? QQmlModuleImportAuto : QQmlModuleImportLatest;
+ qmlUnregisterModuleImport(qtQuickControlsUri, QQmlModuleImportModuleAny, registeredStyleUri.toUtf8().constData(), importMajor);
+ customStyle = false;
+ registeredStyleUri.clear();
+}
+
+QT_END_NAMESPACE
+
+#include "qtquickcontrols2plugin.moc"