diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2009-09-09 04:12:45 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2009-09-09 04:12:45 (GMT) |
commit | 14e787b7e5abc1d665ba0e0229f60525283b1e38 (patch) | |
tree | 7382a0c9fc282234b2594319d48ac1dfe060770e /src/declarative/util/qmllistmodel.cpp | |
parent | 94267bb148fe4c991a10d4aa4c01373821d9216e (diff) | |
download | Qt-14e787b7e5abc1d665ba0e0229f60525283b1e38.zip Qt-14e787b7e5abc1d665ba0e0229f60525283b1e38.tar.gz Qt-14e787b7e5abc1d665ba0e0229f60525283b1e38.tar.bz2 |
Modifying ListModel from JS: work for structured data too.
Diffstat (limited to 'src/declarative/util/qmllistmodel.cpp')
-rw-r--r-- | src/declarative/util/qmllistmodel.cpp | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp index a5ae60f..dc6b02b 100644 --- a/src/declarative/util/qmllistmodel.cpp +++ b/src/declarative/util/qmllistmodel.cpp @@ -47,6 +47,7 @@ #include "qmlopenmetaobject.h" #include <qmlcontext.h> #include "qmllistmodel.h" +#include <QtScript/qscriptvalueiterator.h> Q_DECLARE_METATYPE(QListModelInterface *) @@ -68,6 +69,8 @@ struct ListModelData ListInstruction *instructions() const { return (ListInstruction *)((char *)this + sizeof(ListModelData)); } }; +static void dump(ModelNode *node, int ind); + /*! \qmlclass ListModel \brief The ListModel element defines a free-form list data source. @@ -140,7 +143,7 @@ struct ListModelData name: "Banana" cost: 1.95 attributes: [ - ListElement { description: "Tropical" } + ListElement { description: "Tropical" }, ListElement { description: "Seedless" } ] } @@ -238,6 +241,42 @@ struct ModelNode return objectCache; } + void setListValue(const QScriptValue& valuelist) { + QScriptValueIterator it(valuelist); + values.clear(); + while (it.hasNext()) { + it.next(); + ModelNode *value = new ModelNode; + QScriptValue v = it.value(); + if (v.isArray()) { + value->setListValue(v); + } else if (v.isObject()) { + value->setObjectValue(v); + } else { + value->values << v.toVariant(); + } + values.append(qVariantFromValue(value)); + + } + } + + void setObjectValue(const QScriptValue& valuemap) { + QScriptValueIterator it(valuemap); + while (it.hasNext()) { + it.next(); + ModelNode *value = new ModelNode; + QScriptValue v = it.value(); + if (v.isArray()) { + value->setListValue(v); + } else if (v.isObject()) { + value->setObjectValue(v); + } else { + value->values << v.toVariant(); + } + properties.insert(it.name(),value); + } + } + void setProperty(const QString& prop, const QVariant& val) { QHash<QString, ModelNode *>::const_iterator it = properties.find(prop); if (it != properties.end()) { @@ -429,7 +468,7 @@ void QmlListModel::remove(int index) \sa set() append() */ -void QmlListModel::insert(int index, const QVariantMap& valuemap) +void QmlListModel::insert(int index, const QScriptValue& valuemap) { if (!_root) _root = new ModelNode; @@ -438,12 +477,7 @@ void QmlListModel::insert(int index, const QVariantMap& valuemap) return; } ModelNode *mn = new ModelNode; - for (QVariantMap::const_iterator it=valuemap.begin(); it!=valuemap.end(); ++it) { - addRole(it.key()); - ModelNode *value = new ModelNode; - value->values << it.value(); - mn->properties.insert(it.key(),value); - } + mn->setObjectValue(valuemap); _root->values.insert(index,qVariantFromValue(mn)); emit itemsInserted(index,1); } @@ -506,17 +540,16 @@ void QmlListModel::move(int from, int to, int n) \sa set() remove() */ -void QmlListModel::append(const QVariantMap& valuemap) +void QmlListModel::append(const QScriptValue& valuemap) { + if (!valuemap.isObject()) { + qWarning("ListModel::append: value is not an object"); + return; + } if (!_root) _root = new ModelNode; ModelNode *mn = new ModelNode; - for (QVariantMap::const_iterator it=valuemap.begin(); it!=valuemap.end(); ++it) { - addRole(it.key()); - ModelNode *value = new ModelNode; - value->values << it.value(); - mn->properties.insert(it.key(),value); - } + mn->setObjectValue(valuemap); _root->values << qVariantFromValue(mn); emit itemsInserted(count()-1,1); } @@ -536,7 +569,7 @@ void QmlListModel::append(const QVariantMap& valuemap) \sa append() */ -void QmlListModel::set(int index, const QVariantMap& valuemap) +void QmlListModel::set(int index, const QScriptValue& valuemap) { if (!_root) _root = new ModelNode; @@ -548,12 +581,14 @@ void QmlListModel::set(int index, const QVariantMap& valuemap) else { ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index)); QList<int> roles; - for (QVariantMap::const_iterator it=valuemap.begin(); it!=valuemap.end(); ++it) { - node->setProperty(it.key(),it.value()); - int r = roleStrings.indexOf(it.key()); + node->setObjectValue(valuemap); + QScriptValueIterator it(valuemap); + while (it.hasNext()) { + it.next(); + int r = roleStrings.indexOf(it.name()); if (r<0) { r = roleStrings.count(); - roleStrings << it.key(); + roleStrings << it.name(); } roles.append(r); } |