summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/declarative/qml/qmlengine.cpp29
-rw-r--r--src/declarative/qml/qmlengine.h2
-rw-r--r--tests/auto/declarative/declarative.pro3
-rw-r--r--tests/auto/declarative/qmlengine/qmlengine.pro5
-rw-r--r--tests/auto/declarative/qmlengine/tst_qmlengine.cpp43
5 files changed, 81 insertions, 1 deletions
diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp
index 0209c1d..425cba4 100644
--- a/src/declarative/qml/qmlengine.cpp
+++ b/src/declarative/qml/qmlengine.cpp
@@ -615,6 +615,35 @@ QUrl QmlEngine::componentUrl(const QUrl& src, const QUrl& baseUrl) const
}
/*!
+ Returns the list of base urls the engine browses to find sub-components.
+
+ The search path consists of the base of the \a url, and, in the case of local files,
+ the directories imported using the "import" statement in \a qml.
+ */
+QList<QUrl> QmlEngine::componentSearchPath(const QByteArray &qml, const QUrl &url) const
+{
+ QList<QUrl> searchPath;
+
+ searchPath << url.resolved(QUrl(QLatin1String(".")));
+
+ if (QFileInfo(url.toLocalFile()).exists()) {
+ QmlScriptParser parser;
+ if (parser.parse(qml, url)) {
+ for (int i = 0; i < parser.imports().size(); ++i) {
+ QUrl importUrl = QUrl(parser.imports().at(i).uri);
+ if (importUrl.isRelative()) {
+ searchPath << url.resolved(importUrl);
+ } else {
+ searchPath << importUrl;
+ }
+ }
+ }
+ }
+
+ return searchPath;
+}
+
+/*!
Sets the common QNetworkAccessManager, \a network, used by all QML elements instantiated
by this engine.
diff --git a/src/declarative/qml/qmlengine.h b/src/declarative/qml/qmlengine.h
index fde84d4..9382389 100644
--- a/src/declarative/qml/qmlengine.h
+++ b/src/declarative/qml/qmlengine.h
@@ -78,6 +78,8 @@ public:
QMap<QString,QString> nameSpacePaths() const;
QUrl componentUrl(const QUrl& src, const QUrl& baseUrl) const;
+ QList<QUrl> componentSearchPath(const QByteArray &qml, const QUrl &url) const;
+
void setNetworkAccessManager(QNetworkAccessManager *);
QNetworkAccessManager *networkAccessManager() const;
diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro
index b6e3584..14d8c24 100644
--- a/tests/auto/declarative/declarative.pro
+++ b/tests/auto/declarative/declarative.pro
@@ -14,7 +14,8 @@ SUBDIRS += datetimeformatter \
qmlmetaproperty \
qmllist \
qmllistaccessor \
- visual
+ visual\
+ qmlengine
# Tests which should run in Pulse
PULSE_TESTS = $$SUBDIRS
diff --git a/tests/auto/declarative/qmlengine/qmlengine.pro b/tests/auto/declarative/qmlengine/qmlengine.pro
new file mode 100644
index 0000000..4ac81e9
--- /dev/null
+++ b/tests/auto/declarative/qmlengine/qmlengine.pro
@@ -0,0 +1,5 @@
+load(qttest_p4)
+contains(QT_CONFIG,declarative): QT += declarative
+SOURCES += tst_qmlengine.cpp
+
+DEFINES += SRCDIR=\\\"$$PWD\\\"
diff --git a/tests/auto/declarative/qmlengine/tst_qmlengine.cpp b/tests/auto/declarative/qmlengine/tst_qmlengine.cpp
new file mode 100644
index 0000000..9a04c61
--- /dev/null
+++ b/tests/auto/declarative/qmlengine/tst_qmlengine.cpp
@@ -0,0 +1,43 @@
+#include <qtest.h>
+#include <QtDeclarative/QmlEngine>
+
+#include <QtCore/QDebug>
+#include <QtCore/QFile>
+#include <QtCore/QUrl>
+
+class tst_qmlengine : public QObject
+{
+ Q_OBJECT
+public:
+ tst_qmlengine() {}
+
+private slots:
+ void componentSearchPath();
+};
+
+
+void tst_qmlengine::componentSearchPath()
+{
+ QFile file(SRCDIR "/imports.qml");
+ QVERIFY(file.open(QIODevice::ReadOnly));
+
+ QmlEngine engine;
+
+ QList<QUrl> searchPath = engine.componentSearchPath(file.readAll(),
+ QUrl::fromLocalFile(file.fileName()));
+
+ QList<QUrl> expected;
+ expected << QUrl::fromLocalFile(SRCDIR);
+ expected << QUrl::fromLocalFile(file.fileName()).resolved(QUrl("import1"));
+ expected << QUrl::fromLocalFile(file.fileName()).resolved(QUrl("import2"));
+
+ QCOMPARE(searchPath.size(), expected.size());
+ for (int i = 0; i < expected.size(); ++i) {
+ QCOMPARE(searchPath.at(i).toString(QUrl::StripTrailingSlash),
+ expected.at(i).toString(QUrl::StripTrailingSlash));
+ }
+}
+
+QTEST_MAIN(tst_qmlengine)
+
+#include "tst_qmlengine.moc"