1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "qmltypesclassdescription.h"
30
31#include <QtCore/qjsonarray.h>
32
33static void collectExtraVersions(const QJsonObject *component, const QString &key,
34 QList<int> &extraVersions)
35{
36 const QJsonArray &items = component->value(key).toArray();
37 for (const QJsonValue &item : items) {
38 const QJsonObject obj = item.toObject();
39 const auto revision = obj.find(key: QLatin1String("revision"));
40 if (revision != obj.end()) {
41 const int extraVersion = revision.value().toInt();
42 if (!extraVersions.contains(t: extraVersion))
43 extraVersions.append(t: extraVersion);
44 }
45 }
46}
47
48const QJsonObject *QmlTypesClassDescription::findType(const QVector<QJsonObject> &types,
49 const QString &name)
50{
51 static const QLatin1String qualifiedClassNameKey("qualifiedClassName");
52 auto it = std::lower_bound(first: types.begin(), last: types.end(), val: name,
53 comp: [&](const QJsonObject &type, const QString &typeName) {
54 return type.value(key: qualifiedClassNameKey).toString() < typeName;
55 });
56
57 return (it != types.end() && it->value(key: qualifiedClassNameKey) == name) ? &(*it) : nullptr;
58}
59
60void QmlTypesClassDescription::collect(const QJsonObject *classDef,
61 const QVector<QJsonObject> &types,
62 const QVector<QJsonObject> &foreign,
63 CollectMode mode)
64{
65 const QJsonObject *origClassDef = classDef; // if we find QML.Foreign, classDef changes.
66 if (file.isEmpty() && classDef->value(key: QLatin1String("registerable")).toBool())
67 file = classDef->value(key: QLatin1String("inputFile")).toString();
68 const auto classInfos = classDef->value(key: QLatin1String("classInfos")).toArray();
69 for (const QJsonValue &classInfo : classInfos) {
70 const QJsonObject obj = classInfo.toObject();
71 const QString name = obj[QLatin1String("name")].toString();
72 const QString value = obj[QLatin1String("value")].toString();
73
74 if (name == QLatin1String("DefaultProperty")) {
75 if (mode != AttachedType && defaultProp.isEmpty())
76 defaultProp = value;
77 } else if (name == QLatin1String("QML.AddedInMinorVersion")) {
78 if (mode == TopLevel) {
79 addedInRevision = value.toInt();
80 revisions.append(t: value.toInt());
81 } else if (!elementName.isEmpty()) {
82 revisions.append(t: value.toInt());
83 }
84 }
85
86 if (mode != TopLevel)
87 continue;
88
89 // These only apply to the original class
90 if (name == QLatin1String("QML.Element")) {
91 if (value == QLatin1String("auto"))
92 elementName = classDef->value(key: QLatin1String("className")).toString();
93 else if (value != QLatin1String("anonymous"))
94 elementName = value;
95 } else if (name == QLatin1String("QML.RemovedInMinorVersion")) {
96 removedInRevision = value.toInt();
97 } else if (name == QLatin1String("QML.Creatable")) {
98 isCreatable = (value != QLatin1String("false"));
99 } else if (name == QLatin1String("QML.Attached")) {
100 collectAttached(attached: value, types, foreign);
101 } else if (name == QLatin1String("QML.Singleton")) {
102 if (value == QLatin1String("true"))
103 isSingleton = true;
104 } else if (name == QLatin1String("QML.Foreign")) {
105 if (const QJsonObject *other = findType(types: foreign, name: value)) {
106 classDef = other;
107 // Foreign type can have a default property or an attached types
108 const auto classInfos = classDef->value(key: QLatin1String("classInfos")).toArray();
109 for (const QJsonValue &classInfo : classInfos) {
110 const QJsonObject obj = classInfo.toObject();
111 const QString foreignName = obj[QLatin1String("name")].toString();
112 const QString foreignValue = obj[QLatin1String("value")].toString();
113 if (defaultProp.isEmpty() && foreignName == QLatin1String("DefaultProperty"))
114 defaultProp = foreignValue;
115 else if (foreignName == QLatin1String("QML.Attached"))
116 collectAttached(attached: foreignValue, types, foreign);
117 }
118 }
119 } else if (name == QLatin1String("QML.Root")) {
120 isRootClass = true;
121 isBuiltin = true;
122 } else if (name == QLatin1String("QML.Builtin")) {
123 isBuiltin = true;
124 }
125 }
126
127 if (mode == AttachedType || !elementName.isEmpty()) {
128 collectExtraVersions(component: classDef, key: QString::fromLatin1(str: "properties"), extraVersions&: revisions);
129 collectExtraVersions(component: classDef, key: QString::fromLatin1(str: "slots"), extraVersions&: revisions);
130 collectExtraVersions(component: classDef, key: QString::fromLatin1(str: "methods"), extraVersions&: revisions);
131 collectExtraVersions(component: classDef, key: QString::fromLatin1(str: "signals"), extraVersions&: revisions);
132 }
133
134 auto supers = classDef->value(key: QLatin1String("superClasses")).toArray();
135 if (classDef != origClassDef) {
136 const QJsonArray origSupers = origClassDef->value(key: QLatin1String("superClasses")).toArray();
137 for (const QJsonValue &origSuper : origSupers)
138 supers.append(value: origSuper);
139 }
140
141 for (const QJsonValue &superValue : qAsConst(t&: supers)) {
142 const QJsonObject superObject = superValue.toObject();
143 if (superObject[QLatin1String("access")].toString() == QLatin1String("public")) {
144 const QString superName = superObject[QLatin1String("name")].toString();
145
146 if (const QJsonObject *other = findType(types, name: superName))
147 collect(classDef: other, types, foreign, mode: mode == TopLevel ? SuperClass : AttachedType);
148 else if (const QJsonObject *other = findType(types: foreign, name: superName))
149 collect(classDef: other, types, foreign, mode: mode == TopLevel ? SuperClass : AttachedType);
150 else // If we cannot locate a type for it, there is no point in recording the superClass
151 continue;
152
153 if (mode == TopLevel && superClass.isEmpty())
154 superClass = superName;
155 }
156 }
157
158 if (mode != TopLevel)
159 return;
160
161 if (addedInRevision == -1) {
162 revisions.append(t: 0);
163 addedInRevision = 0;
164 }
165
166 std::sort(first: revisions.begin(), last: revisions.end(),
167 comp: [](int a, int b) { return QByteArray::number(a) < QByteArray::number(b); });
168 const auto end = std::unique(first: revisions.begin(), last: revisions.end());
169 revisions.erase(afirst: end, alast: revisions.end());
170
171 resolvedClass = classDef;
172
173 // If it's not a QObject, it's not creatable
174 isCreatable = isCreatable && classDef->value(key: QLatin1String("object")).toBool();
175}
176
177void QmlTypesClassDescription::collectAttached(const QString &attached,
178 const QVector<QJsonObject> &types,
179 const QVector<QJsonObject> &foreign)
180{
181 attachedType = attached;
182 if (const QJsonObject *other = findType(types, name: attachedType))
183 collect(classDef: other, types, foreign, mode: AttachedType);
184 else if (const QJsonObject *other = findType(types: foreign, name: attachedType))
185 collect(classDef: other, types, foreign, mode: AttachedType);
186}
187

source code of qtdeclarative/src/qmltyperegistrar/qmltypesclassdescription.cpp