From f0062920559dd44f5f463ea4c3fa2657fceb81fb Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Tue, 7 Jul 2009 13:28:56 +1000 Subject: Improve QmlComponent API Having to use QUrl::fromLocalFile() is crummy. Add appropriate overloads the QmlComponent, and resolve relative paths against a "base url" set on the QmlEngine. --- examples/declarative/extending/adding/main.cpp | 2 +- examples/declarative/extending/attached/main.cpp | 2 +- examples/declarative/extending/coercion/main.cpp | 2 +- examples/declarative/extending/default/main.cpp | 2 +- examples/declarative/extending/grouped/main.cpp | 2 +- examples/declarative/extending/properties/main.cpp | 2 +- src/declarative/qml/qmlcomponent.cpp | 28 ++++++++++++++++---- src/declarative/qml/qmlcomponent.h | 5 ++-- src/declarative/qml/qmlengine.cpp | 30 ++++++++++++++++++++++ src/declarative/qml/qmlengine.h | 3 +++ src/declarative/qml/qmlengine_p.h | 2 ++ 11 files changed, 67 insertions(+), 13 deletions(-) diff --git a/examples/declarative/extending/adding/main.cpp b/examples/declarative/extending/adding/main.cpp index 3d78ded..7e5cbef 100644 --- a/examples/declarative/extending/adding/main.cpp +++ b/examples/declarative/extending/adding/main.cpp @@ -9,7 +9,7 @@ int main(int argc, char ** argv) QCoreApplication app(argc, argv); QmlEngine engine; - QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + QmlComponent component(&engine, ":example.qml"); Person *person = qobject_cast(component.create()); if (person) { qWarning() << "The person's name is" << person->name(); diff --git a/examples/declarative/extending/attached/main.cpp b/examples/declarative/extending/attached/main.cpp index 1f10cd0..8b05b9d 100644 --- a/examples/declarative/extending/attached/main.cpp +++ b/examples/declarative/extending/attached/main.cpp @@ -10,7 +10,7 @@ int main(int argc, char ** argv) QCoreApplication app(argc, argv); QmlEngine engine; - QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + QmlComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); if (party && party->celebrant()) { diff --git a/examples/declarative/extending/coercion/main.cpp b/examples/declarative/extending/coercion/main.cpp index 7439ba9..75846fa 100644 --- a/examples/declarative/extending/coercion/main.cpp +++ b/examples/declarative/extending/coercion/main.cpp @@ -10,7 +10,7 @@ int main(int argc, char ** argv) QCoreApplication app(argc, argv); QmlEngine engine; - QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + QmlComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); if (party && party->celebrant()) { diff --git a/examples/declarative/extending/default/main.cpp b/examples/declarative/extending/default/main.cpp index 7439ba9..75846fa 100644 --- a/examples/declarative/extending/default/main.cpp +++ b/examples/declarative/extending/default/main.cpp @@ -10,7 +10,7 @@ int main(int argc, char ** argv) QCoreApplication app(argc, argv); QmlEngine engine; - QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + QmlComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); if (party && party->celebrant()) { diff --git a/examples/declarative/extending/grouped/main.cpp b/examples/declarative/extending/grouped/main.cpp index 0c9bb97..490e596 100644 --- a/examples/declarative/extending/grouped/main.cpp +++ b/examples/declarative/extending/grouped/main.cpp @@ -10,7 +10,7 @@ int main(int argc, char ** argv) QCoreApplication app(argc, argv); QmlEngine engine; - QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + QmlComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); if (party && party->celebrant()) { diff --git a/examples/declarative/extending/properties/main.cpp b/examples/declarative/extending/properties/main.cpp index 7b80914..d487fbe 100644 --- a/examples/declarative/extending/properties/main.cpp +++ b/examples/declarative/extending/properties/main.cpp @@ -10,7 +10,7 @@ int main(int argc, char ** argv) QCoreApplication app(argc, argv); QmlEngine engine; - QmlComponent component(&engine, QUrl::fromLocalFile(":example.qml")); + QmlComponent component(&engine, ":example.qml"); BirthdayParty *party = qobject_cast(component.create()); if (party && party->celebrant()) { diff --git a/src/declarative/qml/qmlcomponent.cpp b/src/declarative/qml/qmlcomponent.cpp index d6b38c9..3474487 100644 --- a/src/declarative/qml/qmlcomponent.cpp +++ b/src/declarative/qml/qmlcomponent.cpp @@ -270,10 +270,25 @@ QmlComponent::QmlComponent(QmlEngine *engine, const QUrl &url, QObject *parent) } /*! + Create a QmlComponent from the given \a url and give it the specified + \a parent and \a engine. + + \sa loadUrl() +*/ +QmlComponent::QmlComponent(QmlEngine *engine, const QString &url, + QObject *parent) +: QObject(*(new QmlComponentPrivate), parent) +{ + Q_D(QmlComponent); + d->engine = engine; + loadUrl(QUrl(url)); +} + +/*! Create a QmlComponent from the given QML \a data and give it the - specified \a parent and \a engine. If \a url is provided, it is used to set - the component name, and to provide a base path for items resolved - by this component. + specified \a parent and \a engine. \a url is used to provide a base path + for items resolved by this component, and may be an empty url if the + component contains no items to resolve. \sa setData() */ @@ -339,10 +354,13 @@ void QmlComponent::loadUrl(const QUrl &url) d->clear(); - d->url = url; + if (url.isRelative()) + d->url = d->engine->baseUrl().resolved(url); + else + d->url = url; QmlCompositeTypeData *data = - d->engine->d_func()->typeManager.get(url); + d->engine->d_func()->typeManager.get(d->url); if (data->status == QmlCompositeTypeData::Waiting) { diff --git a/src/declarative/qml/qmlcomponent.h b/src/declarative/qml/qmlcomponent.h index b29c123..5e6dce9 100644 --- a/src/declarative/qml/qmlcomponent.h +++ b/src/declarative/qml/qmlcomponent.h @@ -67,9 +67,10 @@ class Q_DECLARATIVE_EXPORT QmlComponent : public QObject public: QmlComponent(QObject *parent = 0); QmlComponent(QmlEngine *, QObject *parent=0); + QmlComponent(QmlEngine *, const QString &url, QObject *parent = 0); QmlComponent(QmlEngine *, const QUrl &url, QObject *parent = 0); QmlComponent(QmlEngine *, const QByteArray &data, - const QUrl &baseUrl=QUrl(), QObject *parent=0); + const QUrl &baseUrl, QObject *parent=0); virtual ~QmlComponent(); Q_ENUMS(Status) @@ -92,7 +93,7 @@ public: virtual void completeCreate(); void loadUrl(const QUrl &url); - void setData(const QByteArray &, const QUrl &baseUrl = QUrl()); + void setData(const QByteArray &, const QUrl &baseUrl); Q_SIGNALS: void statusChanged(QmlComponent::Status); diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 1342dec..d88d11f 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -638,6 +638,36 @@ QNetworkAccessManager *QmlEngine::networkAccessManager() const } /*! + Return the base URL for this engine. The base URL is only used to resolve + components when a relative URL is passed to the QmlComponent constructor. + + If a base URL has not been explicitly set, this method returns the + application's current working directory. + + \sa setBaseUrl() +*/ +QUrl QmlEngine::baseUrl() const +{ + Q_D(const QmlEngine); + if (d->baseUrl.isEmpty()) { + return QUrl::fromLocalFile(QDir::currentPath() + QDir::separator()); + } else { + return d->baseUrl; + } +} + +/*! + Set the base URL for this engine to \a url. + + \sa baseUrl() +*/ +void QmlEngine::setBaseUrl(const QUrl &url) +{ + Q_D(QmlEngine); + d->baseUrl = url; +} + +/*! Returns the QmlContext for the \a object, or 0 if no context has been set. When the QmlEngine instantiates a QObject, the context is set automatically. diff --git a/src/declarative/qml/qmlengine.h b/src/declarative/qml/qmlengine.h index c4259ce..9e0ac87 100644 --- a/src/declarative/qml/qmlengine.h +++ b/src/declarative/qml/qmlengine.h @@ -85,6 +85,9 @@ public: void setNetworkAccessManager(QNetworkAccessManager *); QNetworkAccessManager *networkAccessManager() const; + QUrl baseUrl() const; + void setBaseUrl(const QUrl &); + static QmlContext *contextForObject(const QObject *); static void setContextForObject(QObject *, QmlContext *); diff --git a/src/declarative/qml/qmlengine_p.h b/src/declarative/qml/qmlengine_p.h index 25f6edf..9171fbb 100644 --- a/src/declarative/qml/qmlengine_p.h +++ b/src/declarative/qml/qmlengine_p.h @@ -129,6 +129,8 @@ public: QScriptEngine scriptEngine; + QUrl baseUrl; + template struct SimpleList { SimpleList() -- cgit v0.12