From 68e47e91af6edca3c714cc3258b65dd88c0ee1e4 Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Wed, 15 Jul 2009 11:09:31 +1000 Subject: Expose qml import statements in QmlDom --- src/declarative/qml/qmldom.cpp | 98 +++++++++++++++++++++- src/declarative/qml/qmldom.h | 25 +++++- src/declarative/qml/qmldom_p.h | 17 +++- tests/auto/declarative/qmldom/MyComponent.qml | 2 - tests/auto/declarative/qmldom/data/MyComponent.qml | 2 + .../auto/declarative/qmldom/data/importdir/Bar.qml | 0 .../qmldom/data/importlib/sublib/qmldir/Foo.qml | 0 tests/auto/declarative/qmldom/data/top.qml | 4 + tests/auto/declarative/qmldom/top.qml | 4 - tests/auto/declarative/qmldom/tst_qmldom.cpp | 46 +++++++++- 10 files changed, 184 insertions(+), 14 deletions(-) delete mode 100644 tests/auto/declarative/qmldom/MyComponent.qml create mode 100644 tests/auto/declarative/qmldom/data/MyComponent.qml create mode 100644 tests/auto/declarative/qmldom/data/importdir/Bar.qml create mode 100644 tests/auto/declarative/qmldom/data/importlib/sublib/qmldir/Foo.qml create mode 100644 tests/auto/declarative/qmldom/data/top.qml delete mode 100644 tests/auto/declarative/qmldom/top.qml diff --git a/src/declarative/qml/qmldom.cpp b/src/declarative/qml/qmldom.cpp index 648eb36..e293a93 100644 --- a/src/declarative/qml/qmldom.cpp +++ b/src/declarative/qml/qmldom.cpp @@ -145,9 +145,9 @@ int QmlDomDocument::version() const } /*! - Return the URIs listed by "import " in the qml. + Returns all import statements in qml. */ -QList QmlDomDocument::imports() const +QList QmlDomDocument::imports() const { return d->imports; } @@ -191,7 +191,13 @@ bool QmlDomDocument::load(QmlEngine *engine, const QByteArray &data, const QUrl } for (int i = 0; i < td->data.imports().size(); ++i) { - d->imports += QUrl(td->data.imports().at(i).uri); + QmlScriptParser::Import parserImport = td->data.imports().at(i); + QmlDomImport domImport; + domImport.d->type = static_cast(parserImport.type); + domImport.d->uri = parserImport.uri; + domImport.d->qualifier = parserImport.qualifier; + domImport.d->version = parserImport.version; + d->imports += domImport; } if (td->data.tree()) { @@ -207,7 +213,6 @@ bool QmlDomDocument::load(QmlEngine *engine, const QByteArray &data, const QUrl return true; } - /*! Returns the last load errors. The load errors will be reset after a successful call to load(). @@ -1767,4 +1772,89 @@ void QmlDomComponent::setComponentRoot(const QmlDomObject &root) qWarning("QmlDomComponent::setComponentRoot(const QmlDomObject &): Not implemented"); } + +QmlDomImportPrivate::QmlDomImportPrivate() +: type(File) +{ +} + +QmlDomImportPrivate::QmlDomImportPrivate(const QmlDomImportPrivate &other) +: QSharedData(other) +{ +} + +QmlDomImportPrivate::~QmlDomImportPrivate() +{ +} + +/*! + \class QmlDomImport + \internal + \brief The QmlDomImport class represents an import statement. +*/ + +/*! + Construct an empty QmlDomImport. +*/ +QmlDomImport::QmlDomImport() +: d(new QmlDomImportPrivate) +{ +} + +/*! + Create a copy of \a other QmlDomImport. +*/ +QmlDomImport::QmlDomImport(const QmlDomImport &other) +: d(other.d) +{ +} + +/*! + Destroy the QmlDomImport. +*/ +QmlDomImport::~QmlDomImport() +{ +} + +/*! + Assign \a other to this QmlDomImport. +*/ +QmlDomImport &QmlDomImport::operator=(const QmlDomImport &other) +{ + d = other.d; + return *this; +} + +/*! + Returns the type of the import. + */ +QmlDomImport::Type QmlDomImport::type() const +{ + return static_cast(d->type); +} + +/*! + Returns the URI of the import (e.g. 'subdir' or 'com.nokia.Qt') + */ +QString QmlDomImport::uri() const +{ + return d->uri; +} + +/*! + Returns the version specified by the import. An empty string if no version was specified. + */ +QString QmlDomImport::version() const +{ + return d->version; +} + +/*! + Returns the (optional) qualifier string (the token following the 'as' keyword) of the import. + */ +QString QmlDomImport::qualifier() const +{ + return d->qualifier; +} + QT_END_NAMESPACE diff --git a/src/declarative/qml/qmldom.h b/src/declarative/qml/qmldom.h index ef9fe25..456202a 100644 --- a/src/declarative/qml/qmldom.h +++ b/src/declarative/qml/qmldom.h @@ -59,9 +59,11 @@ class QmlDomList; class QmlDomValue; class QmlEngine; class QmlDomComponent; +class QmlDomImport; class QIODevice; class QmlDomDocumentPrivate; + class Q_DECLARATIVE_EXPORT QmlDomDocument { public: @@ -71,7 +73,7 @@ public: QmlDomDocument &operator=(const QmlDomDocument &); int version() const; - QList imports() const; + QList imports() const; QList errors() const; bool load(QmlEngine *, const QByteArray &, const QUrl & = QUrl()); @@ -304,6 +306,27 @@ private: QSharedDataPointer d; }; +class QmlDomImportPrivate; +class Q_DECLARATIVE_EXPORT QmlDomImport +{ +public: + enum Type { Library, File }; + + QmlDomImport(); + QmlDomImport(const QmlDomImport &); + ~QmlDomImport(); + QmlDomImport &operator=(const QmlDomImport &); + + Type type() const; + QString uri() const; + QString version() const; + QString qualifier() const; + +private: + friend class QmlDomDocument; + QSharedDataPointer d; +}; + QT_END_NAMESPACE QT_END_HEADER diff --git a/src/declarative/qml/qmldom_p.h b/src/declarative/qml/qmldom_p.h index 5345c44..b15844a 100644 --- a/src/declarative/qml/qmldom_p.h +++ b/src/declarative/qml/qmldom_p.h @@ -66,7 +66,7 @@ public: ~QmlDomDocumentPrivate(); QList errors; - QList imports; + QList imports; QmlParser::Object *root; QList automaticSemicolonOffsets; }; @@ -129,6 +129,21 @@ public: QmlParser::Value *value; }; +class QmlDomImportPrivate : public QSharedData +{ +public: + QmlDomImportPrivate(); + QmlDomImportPrivate(const QmlDomImportPrivate &); + ~QmlDomImportPrivate(); + + enum Type { Library, File }; + + Type type; + QString uri; + QString version; + QString qualifier; +}; + QT_END_NAMESPACE #endif // QMLDOM_P_H diff --git a/tests/auto/declarative/qmldom/MyComponent.qml b/tests/auto/declarative/qmldom/MyComponent.qml deleted file mode 100644 index 0940753..0000000 --- a/tests/auto/declarative/qmldom/MyComponent.qml +++ /dev/null @@ -1,2 +0,0 @@ -Item { -} diff --git a/tests/auto/declarative/qmldom/data/MyComponent.qml b/tests/auto/declarative/qmldom/data/MyComponent.qml new file mode 100644 index 0000000..0940753 --- /dev/null +++ b/tests/auto/declarative/qmldom/data/MyComponent.qml @@ -0,0 +1,2 @@ +Item { +} diff --git a/tests/auto/declarative/qmldom/data/importdir/Bar.qml b/tests/auto/declarative/qmldom/data/importdir/Bar.qml new file mode 100644 index 0000000..e69de29 diff --git a/tests/auto/declarative/qmldom/data/importlib/sublib/qmldir/Foo.qml b/tests/auto/declarative/qmldom/data/importlib/sublib/qmldir/Foo.qml new file mode 100644 index 0000000..e69de29 diff --git a/tests/auto/declarative/qmldom/data/top.qml b/tests/auto/declarative/qmldom/data/top.qml new file mode 100644 index 0000000..142d4c8 --- /dev/null +++ b/tests/auto/declarative/qmldom/data/top.qml @@ -0,0 +1,4 @@ +MyComponent { + width: 100 + height: 100 +} diff --git a/tests/auto/declarative/qmldom/top.qml b/tests/auto/declarative/qmldom/top.qml deleted file mode 100644 index 142d4c8..0000000 --- a/tests/auto/declarative/qmldom/top.qml +++ /dev/null @@ -1,4 +0,0 @@ -MyComponent { - width: 100 - height: 100 -} diff --git a/tests/auto/declarative/qmldom/tst_qmldom.cpp b/tests/auto/declarative/qmldom/tst_qmldom.cpp index ca8929d..20419fb 100644 --- a/tests/auto/declarative/qmldom/tst_qmldom.cpp +++ b/tests/auto/declarative/qmldom/tst_qmldom.cpp @@ -17,6 +17,7 @@ private slots: void loadProperties(); void loadChildObject(); void loadComposite(); + void loadImports(); void testValueSource(); @@ -52,7 +53,7 @@ void tst_qmldom::loadProperties() QmlDomObject rootObject = document.rootObject(); QVERIFY(rootObject.isValid()); QVERIFY(rootObject.objectId() == "item"); - QVERIFY(rootObject.properties().size() == 2); + QCOMPARE(rootObject.properties().size(), 3); QmlDomProperty xProperty = rootObject.property("x"); QVERIFY(xProperty.propertyName() == "x"); @@ -91,7 +92,7 @@ void tst_qmldom::loadChildObject() void tst_qmldom::loadComposite() { - QFile file(SRCDIR "/top.qml"); + QFile file(SRCDIR "/data/top.qml"); QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); QmlDomDocument document; @@ -141,6 +142,47 @@ void tst_qmldom::testValueSource() QVERIFY(sourceValue.toBinding().binding() == "Math.min(Math.max(-130, value*2.2 - 130), 133)"); } +void tst_qmldom::loadImports() +{ + QByteArray qml = "import importlib.sublib 4.7\n" + "import importlib.sublib 4.6 as NewFoo\n" + "import 'import'\n" + "import 'import' as X\n" + "Item {}"; + + QmlEngine engine; + engine.addImportPath(SRCDIR "/data"); + QmlDomDocument document; + QVERIFY(document.load(&engine, qml)); + + QCOMPARE(document.imports().size(), 4); + + QmlDomImport import1 = document.imports().at(0); + QCOMPARE(import1.type(), QmlDomImport::Library); + QCOMPARE(import1.uri(), QLatin1String("importlib.sublib")); + QCOMPARE(import1.qualifier(), QString()); + QCOMPARE(import1.version(), QLatin1String("4.7")); + + QmlDomImport import2 = document.imports().at(1); + QCOMPARE(import2.type(), QmlDomImport::Library); + QCOMPARE(import2.uri(), QLatin1String("importlib.sublib")); + QCOMPARE(import2.qualifier(), QLatin1String("NewFoo")); + QCOMPARE(import2.version(), QLatin1String("4.6")); + + QmlDomImport import3 = document.imports().at(2); + QCOMPARE(import3.type(), QmlDomImport::File); + QCOMPARE(import3.uri(), QLatin1String("import")); + QCOMPARE(import3.qualifier(), QLatin1String("")); + QCOMPARE(import3.version(), QLatin1String("")); + + QmlDomImport import4 = document.imports().at(3); + QCOMPARE(import4.type(), QmlDomImport::File); + QCOMPARE(import4.uri(), QLatin1String("import")); + QCOMPARE(import4.qualifier(), QLatin1String("X")); + QCOMPARE(import4.version(), QLatin1String("")); +} + + QTEST_MAIN(tst_qmldom) #include "tst_qmldom.moc" -- cgit v0.12