@@ -26,30 +26,41 @@ void QmlScanner::scanDir(const QString& path) {
2626 qCDebug (logQmlScanner) << " Scanning directory" << path;
2727 auto dir = QDir (path);
2828
29+ struct Entry {
30+ QString name;
31+ bool singleton = false ;
32+ bool internal = false ;
33+ };
34+
2935 bool seenQmldir = false ;
30- auto singletons = QVector<QString >();
31- auto entries = QVector<QString>();
32- for (auto & entry : dir.entryList (QDir::Files | QDir::NoDotAndDotDot)) {
33- if (entry == " qmldir" ) {
36+ auto entries = QVector<Entry >();
37+
38+ for (auto & name : dir.entryList (QDir::Files | QDir::NoDotAndDotDot)) {
39+ if (name == " qmldir" ) {
3440 qCDebug (logQmlScanner
3541 ) << " Found qmldir file, qmldir synthesization will be disabled for directory"
3642 << path;
3743 seenQmldir = true ;
38- } else if (entry.at (0 ).isUpper () && entry.endsWith (" .qml" )) {
39- if (this ->scanQmlFile (dir.filePath (entry))) {
40- singletons.push_back (entry);
44+ } else if (name.at (0 ).isUpper () && name.endsWith (" .qml" )) {
45+ auto & entry = entries.emplaceBack ();
46+
47+ if (this ->scanQmlFile (dir.filePath (name), entry.singleton , entry.internal )) {
48+ entry.name = name;
4149 } else {
42- entries.push_back (entry);
50+ entries.pop_back ();
51+ }
52+ } else if (name.at (0 ).isUpper () && name.endsWith (" .qml.json" )) {
53+ if (this ->scanQmlJson (dir.filePath (name))) {
54+ entries.push_back ({
55+ .name = name.first (name.length () - 5 ),
56+ .singleton = true ,
57+ });
4358 }
44- } else if (entry.at (0 ).isUpper () && entry.endsWith (" .qml.json" )) {
45- this ->scanQmlJson (dir.filePath (entry));
46- singletons.push_back (entry.first (entry.length () - 5 ));
4759 }
4860 }
4961
5062 if (!seenQmldir) {
51- qCDebug (logQmlScanner) << " Synthesizing qmldir for directory" << path << " singletons"
52- << singletons;
63+ qCDebug (logQmlScanner) << " Synthesizing qmldir for directory" << path;
5364
5465 QString qmldir;
5566 auto stream = QTextStream (&qmldir);
@@ -77,21 +88,18 @@ void QmlScanner::scanDir(const QString& path) {
7788 qCWarning (logQmlScanner) << " Module path" << path << " is outside of the config folder." ;
7889 }
7990
80- for (auto & singleton: singletons) {
81- stream << " singleton " << singleton.sliced (0 , singleton.length () - 4 ) << " 1.0 " << singleton
82- << " \n " ;
83- }
84-
85- for (auto & entry: entries) {
86- stream << entry.sliced (0 , entry.length () - 4 ) << " 1.0 " << entry << " \n " ;
91+ for (const auto & entry: entries) {
92+ if (entry.internal ) stream << " internal " ;
93+ if (entry.singleton ) stream << " singleton " ;
94+ stream << entry.name .sliced (0 , entry.name .length () - 4 ) << " 1.0 " << entry.name << ' \n ' ;
8795 }
8896
8997 qCDebug (logQmlScanner) << " Synthesized qmldir for" << path << qPrintable (" \n " + qmldir);
9098 this ->fileIntercepts .insert (QDir (path).filePath (" qmldir" ), qmldir);
9199 }
92100}
93101
94- bool QmlScanner::scanQmlFile (const QString& path) {
102+ bool QmlScanner::scanQmlFile (const QString& path, bool & singleton, bool & internal ) {
95103 if (this ->scannedFiles .contains (path)) return false ;
96104 this ->scannedFiles .push_back (path);
97105
@@ -106,13 +114,12 @@ bool QmlScanner::scanQmlFile(const QString& path) {
106114 auto stream = QTextStream (&file);
107115 auto imports = QVector<QString>();
108116
109- bool singleton = false ;
110-
111117 while (!stream.atEnd ()) {
112118 auto line = stream.readLine ().trimmed ();
113119 if (!singleton && line == " pragma Singleton" ) {
114- qCDebug (logQmlScanner) << " Discovered singleton" << path;
115120 singleton = true ;
121+ } else if (!internal && line == " //@ pragma Internal" ) {
122+ internal = true ;
116123 } else if (line.startsWith (" import" )) {
117124 // we dont care about "import qs" as we always load the root folder
118125 if (auto importCursor = line.indexOf (" qs." ); importCursor != -1 ) {
@@ -188,16 +195,22 @@ bool QmlScanner::scanQmlFile(const QString& path) {
188195 else this ->scanDir (cpath);
189196 }
190197
191- return singleton;
198+ return true ;
199+ }
200+
201+ void QmlScanner::scanQmlRoot (const QString& path) {
202+ bool singleton = false ;
203+ bool internal = false ;
204+ this ->scanQmlFile (path, singleton, internal);
192205}
193206
194- void QmlScanner::scanQmlJson (const QString& path) {
207+ bool QmlScanner::scanQmlJson (const QString& path) {
195208 qCDebug (logQmlScanner) << " Scanning qml.json file" << path;
196209
197210 auto file = QFile (path);
198211 if (!file.open (QFile::ReadOnly | QFile::Text)) {
199212 qCWarning (logQmlScanner) << " Failed to open file" << path;
200- return ;
213+ return false ;
201214 }
202215
203216 auto data = file.readAll ();
@@ -209,7 +222,7 @@ void QmlScanner::scanQmlJson(const QString& path) {
209222 if (error.error != QJsonParseError::NoError) {
210223 qCCritical (logQmlScanner).nospace ()
211224 << " Failed to parse qml.json file at " << path << " : " << error.errorString ();
212- return ;
225+ return false ;
213226 }
214227
215228 const QString body =
@@ -219,6 +232,7 @@ void QmlScanner::scanQmlJson(const QString& path) {
219232
220233 this ->fileIntercepts .insert (path.first (path.length () - 5 ), body);
221234 this ->scannedFiles .push_back (path);
235+ return true ;
222236}
223237
224238QPair<QString, QString> QmlScanner::jsonToQml (const QJsonValue& value, int indent) {
0 commit comments