summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2010-05-20 06:42:06 (GMT)
committerBea Lam <bea.lam@nokia.com>2010-05-21 00:11:34 (GMT)
commit30b490476bad5d486332093fc672856a8dd6a195 (patch)
treed33650d24441fd2dc4d91076fca6821aedcb9d83
parent0a2ef85bc7c560f38f5ba591968ce49969893a1c (diff)
downloadQt-30b490476bad5d486332093fc672856a8dd6a195.zip
Qt-30b490476bad5d486332093fc672856a8dd6a195.tar.gz
Qt-30b490476bad5d486332093fc672856a8dd6a195.tar.bz2
Add XmlListModel::get()
Task-number: QTBUG-10761
-rw-r--r--src/declarative/util/qdeclarativexmllistmodel.cpp38
-rw-r--r--src/declarative/util/qdeclarativexmllistmodel_p.h3
-rw-r--r--tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp41
3 files changed, 81 insertions, 1 deletions
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 <qdeclarativecontext.h>
-#include <qdeclarativeengine.h>
+#include <qdeclarativeengine_p.h>
#include <QDebug>
#include <QStringList>
@@ -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; i<d->roleObjects.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 <QtCore/qurl.h>
#include <QtCore/qstringlist.h>
+#include <QtScript/qscriptvalue.h>
#include <private/qlistmodelinterface_p.h>
@@ -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<QDeclarativeXmlListModel*>(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