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
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
5
7
9
10/*
11 Forms complete paths to a module, from a list of base paths,
12 a module URI and version specification.
13
14 For example, QtQml.Models 2.0:
15 - base/QtQml/Models.2.0
16 - base/QtQml.2.0/Models
17 - base/QtQml/Models.2
18 - base/QtQml.2/Models
19 - base/QtQml/Models
20*/
22 QTypeRevision version)
23{
24 static const QLatin1Char Slash('/');
25 static const QLatin1Char Backslash('\\');
26
27 const QVector<QStringView> parts = uri.split(u'.', Qt::SkipEmptyParts);
28
29 QStringList importPaths;
30 // fully & partially versioned parts + 1 unversioned for each base path
31 importPaths.reserve(2 * parts.size() + 1);
32
33 auto versionString = [](QTypeRevision version, ImportVersion mode)
34 {
35 if (mode == FullyVersioned) {
36 // extension with fully encoded version number (eg. MyModule.3.2)
37 return QString::fromLatin1(".%1.%2").arg(version.majorVersion())
38 .arg(version.minorVersion());
39 }
40 if (mode == PartiallyVersioned) {
41 // extension with encoded version major (eg. MyModule.3)
42 return QString::fromLatin1(".%1").arg(version.majorVersion());
43 }
44 // else extension without version number (eg. MyModule)
45 return QString();
46 };
47
48 auto joinStringRefs = [](const QVector<QStringView> &refs, const QChar &sep) {
50 for (auto it = refs.cbegin(); it != refs.cend(); ++it) {
51 if (it != refs.cbegin())
52 str += sep;
53 str += *it;
54 }
55 return str;
56 };
57
58 const ImportVersion initial = (version.hasMinorVersion())
61 for (int mode = initial; mode <= Unversioned; ++mode) {
62 const QString ver = versionString(version, ImportVersion(mode));
63
64 for (const QString &path : basePaths) {
67 dir += Slash;
68
69 // append to the end
70 importPaths += dir + joinStringRefs(parts, Slash) + ver;
71
72 if (mode != Unversioned) {
73 // insert in the middle
74 for (int index = parts.size() - 2; index >= 0; --index) {
75 importPaths += dir + joinStringRefs(parts.mid(0, index + 1), Slash)
76 + ver + Slash
77 + joinStringRefs(parts.mid(index + 1), Slash);
78 }
79 }
80 }
81 }
82
83 return importPaths;
84}
85
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
Q_CORE_EXPORT QList< QStringView > split(QStringView sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Splits the view into substring views wherever sep occurs, and returns the list of those string views.
Definition qstring.cpp:8249
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
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 endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5506
\inmodule QtCore
constexpr bool hasMinorVersion() const
Returns true if the minor version is known, otherwise false.
constexpr bool hasMajorVersion() const
Returns true if the major version is known, otherwise false.
constexpr quint8 minorVersion() const
Returns the minor version encoded in the revision.
constexpr quint8 majorVersion() const
Returns the major version encoded in the revision.
QString str
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
@ SkipEmptyParts
Definition qnamespace.h:128
GLenum mode
GLuint index
[2]
GLsizei const GLchar *const * path
static const QLatin1Char Backslash('\\')
static const QLatin1Char Slash('/')
@ Unversioned
@ FullyVersioned
@ PartiallyVersioned
QStringList qQmlResolveImportPaths(QStringView uri, const QStringList &basePaths, QTypeRevision version)
static constexpr QChar sep
QString dir
[11]
\inmodule QtCore \reentrant
Definition qchar.h:18