From: Aaron McCarthy Date: Mon, 23 Jul 2012 00:59:30 +0000 (+1000) Subject: Fix QML import paths in Qt resources. X-Git-Tag: upstream/5.2.1~1362 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=73842034aac7b9788add4f1ba6cf7a9ec6057598;p=platform%2Fupstream%2Fqtdeclarative.git Fix QML import paths in Qt resources. Allow adding qrc: urls as import paths. Store an import path of the form :/import/path as qrc:/import/path which is expected by other parts of the code. Update documentation for QQmlEngine::addImportPath() to explicitly state what types of paths are supported. Add auto tests to check that importing a module from a Qt resource works. Change-Id: If0e75c75078a608b20d7a5c4080bccf6241e97f6 Reviewed-by: Chris Adams --- diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 580aaf4..f9b261b 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -1610,7 +1610,13 @@ void QQmlEnginePrivate::dereferenceScarceResources() /*! Adds \a path as a directory where the engine searches for installed modules in a URL-based directory structure. - The \a path may be a local filesystem directory or a URL. + + The \a path may be a local filesystem directory, a + \l {The Qt Resource System}{Qt Resource} path (\c {:/imports}), a + \l {The Qt Resource System}{Qt Resource} url (\c {qrc:/imports}) or a URL. + + The \a path will be converted into canonical form before it + is added to the import path list. The newly added \a path will be first in the importPathList(). diff --git a/src/qml/qml/qqmlimport.cpp b/src/qml/qml/qqmlimport.cpp index 55c07ac..20da154 100644 --- a/src/qml/qml/qqmlimport.cpp +++ b/src/qml/qml/qqmlimport.cpp @@ -1536,6 +1536,11 @@ void QQmlImportDatabase::addImportPath(const QString& path) if (url.scheme() == QLatin1String("file")) { cPath = QQmlFile::urlToLocalFileOrQrc(url); + } else if (path.startsWith(QLatin1Char(':'))) { + // qrc directory, e.g. :/foo + // need to convert to a qrc url, e.g. qrc:/foo + cPath = QStringLiteral("qrc") + path; + cPath.replace(Backslash, Slash); } else if (url.isRelative() || (url.scheme().length() == 1 && QFile::exists(path)) ) { // windows path QDir dir = QDir(path); diff --git a/src/qml/qml/qqmltypeloader.cpp b/src/qml/qml/qqmltypeloader.cpp index 1d70706..c66cd28 100644 --- a/src/qml/qml/qqmltypeloader.cpp +++ b/src/qml/qml/qqmltypeloader.cpp @@ -1633,6 +1633,11 @@ QString QQmlTypeLoader::absoluteFilePath(const QString &path) // qrc resource QFileInfo fileInfo(path); return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString(); + } else if (path.count() > 3 && path.at(3) == QLatin1Char(':') && + path.startsWith(QLatin1String("qrc"), Qt::CaseInsensitive)) { + // qrc resource url + QFileInfo fileInfo(QQmlFile::urlToLocalFileOrQrc(path)); + return fileInfo.isFile() ? fileInfo.absoluteFilePath() : QString(); } int lastSlash = path.lastIndexOf(QLatin1Char('/')); QStringRef dirPath(&path, 0, lastSlash); diff --git a/tests/auto/qml/qrcqml/data/imports/QrcImport/Imported.qml b/tests/auto/qml/qrcqml/data/imports/QrcImport/Imported.qml new file mode 100644 index 0000000..c2ef0a6 --- /dev/null +++ b/tests/auto/qml/qrcqml/data/imports/QrcImport/Imported.qml @@ -0,0 +1,5 @@ +import QtQuick 2.0 + +Item { + property string tokenProperty: "bar" +} diff --git a/tests/auto/qml/qrcqml/data/imports/QrcImport/qmldir b/tests/auto/qml/qrcqml/data/imports/QrcImport/qmldir new file mode 100644 index 0000000..8eb1fa5 --- /dev/null +++ b/tests/auto/qml/qrcqml/data/imports/QrcImport/qmldir @@ -0,0 +1 @@ +Imported 1.0 Imported.qml diff --git a/tests/auto/qml/qrcqml/data/importtest.qml b/tests/auto/qml/qrcqml/data/importtest.qml new file mode 100644 index 0000000..dd45bd8 --- /dev/null +++ b/tests/auto/qml/qrcqml/data/importtest.qml @@ -0,0 +1,5 @@ +import QrcImport 1.0 + +Imported { + tokenProperty: "foo" +} diff --git a/tests/auto/qml/qrcqml/qrcqml.qrc b/tests/auto/qml/qrcqml/qrcqml.qrc index 4ee303a..9ab0565 100644 --- a/tests/auto/qml/qrcqml/qrcqml.qrc +++ b/tests/auto/qml/qrcqml/qrcqml.qrc @@ -1,19 +1,21 @@ - - - data/main.qml - data/SameDir.qml - data/SubDir.qml - - - - data/main2.qml - data/SameDir2.qml - - - data/main3.qml - data/SameDir3.qml - - - data/SubDir.qml - + + + data/main.qml + data/SameDir.qml + data/SubDir.qml + data/main2.qml + data/SameDir2.qml + data/importtest.qml + + + data/main3.qml + data/SameDir3.qml + + + data/SubDir.qml + + + data/imports/QrcImport/Imported.qml + data/imports/QrcImport/qmldir + diff --git a/tests/auto/qml/qrcqml/tst_qrcqml.cpp b/tests/auto/qml/qrcqml/tst_qrcqml.cpp index a09c908..37a7e78 100644 --- a/tests/auto/qml/qrcqml/tst_qrcqml.cpp +++ b/tests/auto/qml/qrcqml/tst_qrcqml.cpp @@ -57,6 +57,8 @@ public: private slots: void basicLoad_data(); void basicLoad(); + void qrcImport_data(); + void qrcImport(); }; tst_qrcqml::tst_qrcqml() @@ -101,6 +103,35 @@ void tst_qrcqml::basicLoad() delete o; } +void tst_qrcqml::qrcImport_data() +{ + QTest::addColumn("importPath"); + QTest::addColumn("token"); + + QTest::newRow("qrc path") + << ":/imports" + << "foo"; + + QTest::newRow("qrc url") + << "qrc:/imports" + << "foo"; +} + +void tst_qrcqml::qrcImport() +{ + QFETCH(QString, importPath); + QFETCH(QString, token); + + QQmlEngine e; + e.addImportPath(importPath); + QQmlComponent c(&e, QUrl("qrc:///importtest.qml")); + QVERIFY(c.isReady()); + QObject *o = c.create(); + QVERIFY(o); + QCOMPARE(o->property("tokenProperty").toString(), token); + delete o; +} + QTEST_MAIN(tst_qrcqml) #include "tst_qrcqml.moc"