diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2022-02-07 13:53:49 +0100 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2022-02-14 12:38:10 +0100 |
commit | 2ea887cca0d31e98ef72ea3de868c7ae86276a81 (patch) | |
tree | 2d7fa8dc80a120ef09df49614e5c7a03cdfcb7d6 /src/qml | |
parent | 0f0987c16011b3485ed99660e8c83e3d75d01fd0 (diff) |
qmldir: Allow for specifying default imports
Adds the option to specify default optional imports in qmldir for
tooling to load. Previously we would just load all entries.
This allows code that that relies on modules utilizing optional imports
(i.e. everything utilizing QtQuick.Controls) to still be supported in a
limited capacity.
This change also adds the necessary CMake API to add these entries to
qmldir files. It also disables loading optional imports by default
and only leaves them enabled for qmllint.
Change-Id: Iff6aaac9cb0ec72b7a2853b60840a4d28c84aa25
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/Qt6QmlBuildInternals.cmake | 1 | ||||
-rw-r--r-- | src/qml/Qt6QmlMacros.cmake | 5 | ||||
-rw-r--r-- | src/qml/doc/src/cmake/qt_add_qml_module.qdoc | 10 | ||||
-rw-r--r-- | src/qml/qmldirparser/qqmldirparser.cpp | 17 | ||||
-rw-r--r-- | src/qml/qmldirparser/qqmldirparser_p.h | 8 |
5 files changed, 37 insertions, 4 deletions
diff --git a/src/qml/Qt6QmlBuildInternals.cmake b/src/qml/Qt6QmlBuildInternals.cmake index 87daf8688f..ad42596931 100644 --- a/src/qml/Qt6QmlBuildInternals.cmake +++ b/src/qml/Qt6QmlBuildInternals.cmake @@ -36,6 +36,7 @@ macro(qt_internal_get_internal_add_qml_module_keywords IMPORTS IMPORT_PATH OPTIONAL_IMPORTS + DEFAULT_IMPORTS DEPENDENCIES PAST_MAJOR_VERSIONS ) diff --git a/src/qml/Qt6QmlMacros.cmake b/src/qml/Qt6QmlMacros.cmake index feda48872a..d740f0e682 100644 --- a/src/qml/Qt6QmlMacros.cmake +++ b/src/qml/Qt6QmlMacros.cmake @@ -66,6 +66,7 @@ function(qt6_add_qml_module target) IMPORTS IMPORT_PATH OPTIONAL_IMPORTS + DEFAULT_IMPORTS DEPENDENCIES PAST_MAJOR_VERSIONS ) @@ -382,7 +383,7 @@ function(qt6_add_qml_module target) set(arg_TYPEINFO ${target}.qmltypes) endif() - foreach(import_set IN ITEMS IMPORTS OPTIONAL_IMPORTS) + foreach(import_set IN ITEMS IMPORTS OPTIONAL_IMPORTS DEFAULT_IMPORTS) foreach(import IN LISTS arg_${import_set}) string(FIND ${import} "/" slash_position REVERSE) if (slash_position EQUAL -1) @@ -986,6 +987,8 @@ function(_qt_internal_target_generate_qmldir target) _qt_internal_qmldir_item_list(import QT_QML_MODULE_IMPORTS) _qt_internal_qmldir_item_list("optional import" QT_QML_MODULE_OPTIONAL_IMPORTS) + _qt_internal_qmldir_item_list("default import" QT_QML_MODULE_DEFAULT_IMPORTS) + _qt_internal_qmldir_item_list(depends QT_QML_MODULE_DEPENDENCIES) get_target_property(prefix ${target} QT_QML_MODULE_RESOURCE_PREFIX) diff --git a/src/qml/doc/src/cmake/qt_add_qml_module.qdoc b/src/qml/doc/src/cmake/qt_add_qml_module.qdoc index b3e8bc2a8c..00381d086d 100644 --- a/src/qml/doc/src/cmake/qt_add_qml_module.qdoc +++ b/src/qml/doc/src/cmake/qt_add_qml_module.qdoc @@ -52,6 +52,7 @@ qt_add_qml_module( [TYPEINFO typeinfo] [IMPORTS ...] [OPTIONAL_IMPORTS ...] + [DEFAULT_IMPORTS ...] [DEPENDENCIES ...] [IMPORT_PATH ...] [SOURCES ...] @@ -469,6 +470,15 @@ like \c qmllint. Versions can be specified in the same way as for \c IMPORTS. Each module listed here will be added as an \c{optional import} entry in the generated \l{Module Definition qmldir Files}{qmldir} file. +\c DEFAULT_IMPORTS specifies which of the optional imports are the default entries +that should be loaded by tooling. One entry should be specified for every group of +\c OPTIONAL_IMPORTS in the module. As optional imports are only resolved at runtime, +tooling like qmllint cannot in general know which of the optional imports should +be resolved. To remedy this, you can specify one of the optional imports as the +default import; tooling will then pick it. If you have one optional import that +gets used at runtime without any further configuration, that is an ideal candidate +for the default import. + \c DEPENDENCIES provides a list of other QML modules that this module depends on, but doesn't necessarily import. It would typically be used for dependencies that only exist at the C++ level, such as a module registering a class to QML diff --git a/src/qml/qmldirparser/qqmldirparser.cpp b/src/qml/qmldirparser/qqmldirparser.cpp index bb9036582e..3bc960291a 100644 --- a/src/qml/qmldirparser/qqmldirparser.cpp +++ b/src/qml/qmldirparser/qqmldirparser.cpp @@ -241,6 +241,23 @@ bool QQmlDirParser::parse(const QString &source) "not %1.").arg(sections[1])); continue; } + } else if (sections[0] == QLatin1String("default")) { + if (sectionCount < 2) { + reportError(lineNumber, 0, + QStringLiteral("default directive requires further " + "arguments, but none were provided.")); + continue; + } + if (sections[1] == QLatin1String("import")) { + if (!readImport(sections + 1, sectionCount - 1, + Import::Flags({ Import::Optional, Import::OptionalDefault }))) + continue; + } else { + reportError(lineNumber, 0, + QStringLiteral("only optional imports can have a a defaultl, " + "not %1.") + .arg(sections[1])); + } } else if (sections[0] == QLatin1String("classname")) { if (sectionCount < 2) { reportError(lineNumber, 0, diff --git a/src/qml/qmldirparser/qqmldirparser_p.h b/src/qml/qmldirparser/qqmldirparser_p.h index 6367cae141..1a9fa6ff2b 100644 --- a/src/qml/qmldirparser/qqmldirparser_p.h +++ b/src/qml/qmldirparser/qqmldirparser_p.h @@ -134,9 +134,11 @@ public: struct Import { enum Flag { - Default = 0x0, - Auto = 0x1, // forward the version of the importing module - Optional = 0x2 // is not automatically imported but only a tooling hint + Default = 0x0, + Auto = 0x1, // forward the version of the importing module + Optional = 0x2, // is not automatically imported but only a tooling hint + OptionalDefault = + 0x4, // tooling hint only, denotes this entry should be imported by tooling }; Q_DECLARE_FLAGS(Flags, Flag) |