From 30b490476bad5d486332093fc672856a8dd6a195 Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Thu, 20 May 2010 16:42:06 +1000 Subject: Add XmlListModel::get() Task-number: QTBUG-10761 --- src/declarative/util/qdeclarativexmllistmodel.cpp | 38 +++++++++++++++++++- src/declarative/util/qdeclarativexmllistmodel_p.h | 3 ++ .../tst_qdeclarativexmllistmodel.cpp | 41 ++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/declarative/util/qdeclarativexmllistmodel.cpp b/src/declarative/util/qdeclarativexmllistmodel.cpp index cae1d3a..ae9b323 100644 --- a/src/declarative/util/qdeclarativexmllistmodel.cpp +++ b/src/declarative/util/qdeclarativexmllistmodel.cpp @@ -42,7 +42,7 @@ #include "private/qdeclarativexmllistmodel_p.h" #include -#include +#include #include #include @@ -727,6 +727,42 @@ void QDeclarativeXmlListModel::setNamespaceDeclarations(const QString &declarati } /*! + \qmlmethod object XmlListModel::get(int index) + + Returns the item at \a index in the model. + + For example, for a model like this: + + \qml + XmlListModel { + id: model + source: "http://mysite.com/feed.xml" + query: "/feed/entry" + XmlRole { name: "title"; query: "title/string()" } + } + \qml + + This will access the \c title value for the first item in the model: + + \qml + var title = model.get(0).title; + \qml +*/ +QScriptValue QDeclarativeXmlListModel::get(int index) const +{ + Q_D(const QDeclarativeXmlListModel); + + QScriptEngine *sengine = QDeclarativeEnginePrivate::getScriptEngine(qmlContext(this)->engine()); + if (index < 0 || index >= count()) + return sengine->undefinedValue(); + + QScriptValue sv = sengine->newObject(); + for (int i=0; iroleObjects.count(); i++) + sv.setProperty(d->roleObjects[i]->name(), qScriptValueFromValue(sengine, d->data.value(i).value(index))); + return sv; +} + +/*! \qmlproperty enumeration XmlListModel::status Specifies the model loading status, which can be one of the following: diff --git a/src/declarative/util/qdeclarativexmllistmodel_p.h b/src/declarative/util/qdeclarativexmllistmodel_p.h index 7101c57..4460f24 100644 --- a/src/declarative/util/qdeclarativexmllistmodel_p.h +++ b/src/declarative/util/qdeclarativexmllistmodel_p.h @@ -47,6 +47,7 @@ #include #include +#include #include @@ -109,6 +110,8 @@ public: QString namespaceDeclarations() const; void setNamespaceDeclarations(const QString&); + Q_INVOKABLE QScriptValue get(int index) const; + enum Status { Null, Ready, Loading, Error }; Status status() const; qreal progress() const; diff --git a/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp b/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp index 7769979..e1dd6f4 100644 --- a/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp +++ b/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp @@ -81,6 +81,7 @@ private slots: void source(); void source_data(); void data(); + void get(); void reload(); void useKeys(); void useKeys_data(); @@ -388,6 +389,46 @@ void tst_qdeclarativexmllistmodel::data() delete model; } +void tst_qdeclarativexmllistmodel::get() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/model.qml")); + QDeclarativeXmlListModel *model = qobject_cast(component.create()); + QVERIFY(model != 0); + QVERIFY(model->get(0).isUndefined()); + + QTRY_COMPARE(model->count(), 9); + QVERIFY(model->get(-1).isUndefined()); + + QScriptValue sv = model->get(0); + QCOMPARE(sv.property("name").toString(), QLatin1String("Polly")); + QCOMPARE(sv.property("type").toString(), QLatin1String("Parrot")); + QCOMPARE(sv.property("age").toNumber(), qsreal(12)); + QCOMPARE(sv.property("size").toString(), QLatin1String("Small")); + + sv = model->get(1); + QCOMPARE(sv.property("name").toString(), QLatin1String("Penny")); + QCOMPARE(sv.property("type").toString(), QLatin1String("Turtle")); + QCOMPARE(sv.property("age").toNumber(), qsreal(4)); + QCOMPARE(sv.property("size").toString(), QLatin1String("Small")); + + sv = model->get(7); + QCOMPARE(sv.property("name").toString(), QLatin1String("Rover")); + QCOMPARE(sv.property("type").toString(), QLatin1String("Dog")); + QCOMPARE(sv.property("age").toNumber(), qsreal(0)); + QCOMPARE(sv.property("size").toString(), QLatin1String("Large")); + + sv = model->get(8); + QCOMPARE(sv.property("name").toString(), QLatin1String("Tiny")); + QCOMPARE(sv.property("type").toString(), QLatin1String("Elephant")); + QCOMPARE(sv.property("age").toNumber(), qsreal(15)); + QCOMPARE(sv.property("size").toString(), QLatin1String("Large")); + + sv = model->get(9); + QVERIFY(sv.isUndefined()); + + delete model; +} + void tst_qdeclarativexmllistmodel::reload() { // If no keys are used, the model should be rebuilt from scratch when -- cgit v0.12