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
qqmlimportresolver.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:critical reason:data-parser
4
6
8
10
11/*
12 Forms complete paths to a module, from a list of base paths,
13 a module URI and version specification.
14
15 For example, QtQml.Models 2.0:
16 - base/QtQml/Models.2.0
17 - base/QtQml.2.0/Models
18 - base/QtQml/Models.2
19 - base/QtQml.2/Models
20 - base/QtQml/Models
21*/
22QStringList qQmlResolveImportPaths(QStringView uri, const QStringList &basePaths,
23 QTypeRevision version)
24{
25 static const QLatin1Char Slash('/');
26 static const QLatin1Char Backslash('\\');
27
28 const QList<QStringView> parts = uri.split(u'.', Qt::SkipEmptyParts);
29
30 QStringList importPaths;
31 // fully & partially versioned parts + 1 unversioned for each base path
32 importPaths.reserve(2 * parts.size() + 1);
33
34 auto versionString = [](QTypeRevision version, ImportVersion mode)
35 {
36 if (mode == FullyVersioned) {
37 // extension with fully encoded version number (eg. MyModule.3.2)
38 return QString::fromLatin1(".%1.%2").arg(version.majorVersion())
39 .arg(version.minorVersion());
40 }
41 if (mode == PartiallyVersioned) {
42 // extension with encoded version major (eg. MyModule.3)
43 return QString::fromLatin1(".%1").arg(version.majorVersion());
44 }
45 // else extension without version number (eg. MyModule)
46 return QString();
47 };
48
49 auto joinStringRefs = [](const QList<QStringView> &refs, const QChar &sep) {
50 QString str;
51 for (auto it = refs.cbegin(); it != refs.cend(); ++it) {
52 if (it != refs.cbegin())
53 str += sep;
54 str += *it;
55 }
56 return str;
57 };
58
59 const ImportVersion initial = (version.hasMinorVersion())
61 : (version.hasMajorVersion() ? PartiallyVersioned : Unversioned);
62 for (int mode = initial; mode <= Unversioned; ++mode) {
63 const QString ver = versionString(version, ImportVersion(mode));
64
65 for (const QString &path : basePaths) {
66 QString dir = path;
67 if (!dir.endsWith(Slash) && !dir.endsWith(Backslash))
68 dir += Slash;
69
70 // append to the end
71 importPaths += dir + joinStringRefs(parts, Slash) + ver;
72
73 if (mode != Unversioned) {
74 // insert in the middle
75 for (int index = parts.size() - 2; index >= 0; --index) {
76 importPaths += dir + joinStringRefs(parts.mid(0, index + 1), Slash)
77 + ver + Slash
78 + joinStringRefs(parts.mid(index + 1), Slash);
79 }
80 }
81 }
82 }
83
84 return importPaths;
85}
86
87QT_END_NAMESPACE
Combined button and popup list for selecting options.
@ Unversioned
@ PartiallyVersioned
@ FullyVersioned
QStringList qQmlResolveImportPaths(QStringView uri, const QStringList &basePaths, QTypeRevision version)