summaryrefslogtreecommitdiffstats
path: root/src/declarative/util
diff options
context:
space:
mode:
authorAaron Kennedy <aaron.kennedy@nokia.com>2009-09-08 05:30:38 (GMT)
committerAaron Kennedy <aaron.kennedy@nokia.com>2009-09-08 05:30:38 (GMT)
commitcad3b0bdacdbdf407dfbdda5f6dc9cb7d96fa709 (patch)
tree37a16ff73fe102ac0cd169b20f9b12135903d33a /src/declarative/util
parent5305831c3b0d58a61efadc1c1d3ce9ce4d6e8c77 (diff)
parentb9a924a1e77faeeba595d0619afb14e8fdbb2dd4 (diff)
downloadQt-cad3b0bdacdbdf407dfbdda5f6dc9cb7d96fa709.zip
Qt-cad3b0bdacdbdf407dfbdda5f6dc9cb7d96fa709.tar.gz
Qt-cad3b0bdacdbdf407dfbdda5f6dc9cb7d96fa709.tar.bz2
Merge branch 'kinetic-declarativeui' of git@scm.dev.nokia.troll.no:qt/kinetic into kinetic-declarativeui
Diffstat (limited to 'src/declarative/util')
-rw-r--r--src/declarative/util/qfxview.cpp2
-rw-r--r--src/declarative/util/qmllistmodel.cpp271
-rw-r--r--src/declarative/util/qmllistmodel.h8
3 files changed, 276 insertions, 5 deletions
diff --git a/src/declarative/util/qfxview.cpp b/src/declarative/util/qfxview.cpp
index 9291e1a..2a38cda 100644
--- a/src/declarative/util/qfxview.cpp
+++ b/src/declarative/util/qfxview.cpp
@@ -222,7 +222,7 @@ void QFxView::execute()
if (d->qml.isEmpty()) {
d->component = new QmlComponent(&d->engine, d->source, this);
} else {
- d->component = new QmlComponent(&d->engine, d->qml.toUtf8(), d->source);
+ d->component = new QmlComponent(&d->engine, d->qml.toUtf8(), d->source, this);
}
if (!d->component->isLoading()) {
diff --git a/src/declarative/util/qmllistmodel.cpp b/src/declarative/util/qmllistmodel.cpp
index 0d9ea94..a5ae60f 100644
--- a/src/declarative/util/qmllistmodel.cpp
+++ b/src/declarative/util/qmllistmodel.cpp
@@ -72,7 +72,9 @@ struct ListModelData
\qmlclass ListModel
\brief The ListModel element defines a free-form list data source.
- The ListModel is a simple hierarchy of elements containing data roles.
+ The ListModel is a simple hierarchy of elements containing data roles. The contents can
+ be defined dynamically, or explicitly in QML:
+
For example:
\code
@@ -166,6 +168,29 @@ struct ListModelData
}
\endcode
+ The content of a ListModel may be created and modified using the clear(),
+ append(), and set() methods. For example:
+
+ \code
+ Component {
+ id: FruitDelegate
+ Item {
+ width: 200; height: 50
+ Text { text: name }
+ Text { text: '$'+cost; anchors.right: parent.right }
+
+ // Double the price when clicked.
+ MouseRegion {
+ anchors.fill: parent
+ onClicked: FruitModel.set(index, "cost", cost*2)
+ }
+ }
+ }
+ \endcode
+
+ When creating content dynamically, note that the set of available properties cannot be changed
+ except by first clearing the model - whatever properties are first added are then the
+ only permitted properties in the model.
*/
class ModelObject : public QObject
@@ -189,7 +214,6 @@ struct ModelNode
{
ModelNode();
~ModelNode();
- QString className;
QList<QVariant> values;
QHash<QString, ModelNode *> properties;
@@ -214,6 +238,19 @@ struct ModelNode
return objectCache;
}
+ void setProperty(const QString& prop, const QVariant& val) {
+ QHash<QString, ModelNode *>::const_iterator it = properties.find(prop);
+ if (it != properties.end()) {
+ (*it)->values[0] = val;
+ } else {
+ ModelNode *n = new ModelNode;
+ n->values << val;
+ properties.insert(prop,n);
+ }
+ if (objectCache)
+ objectCache->setValue(prop.toLatin1(), val);
+ }
+
QmlListModel *modelCache;
ModelObject *objectCache;
};
@@ -235,7 +272,7 @@ QmlListModel::~QmlListModel()
void QmlListModel::checkRoles() const
{
- if (_rolesOk)
+ if (_rolesOk || !_root)
return;
for (int ii = 0; ii < _root->values.count(); ++ii) {
@@ -341,6 +378,232 @@ int QmlListModel::count() const
return _root->values.count();
}
+/*!
+ \qmlmethod ListModel::clear()
+
+ Deletes all content from the model. The properties are cleared such that
+ different properties may be set on subsequent additions.
+
+ \sa append() remove()
+*/
+void QmlListModel::clear()
+{
+ int cleared = count();
+ _rolesOk = false;
+ delete _root;
+ _root = 0;
+ roleStrings.clear();
+ emit itemsRemoved(0,cleared);
+}
+
+/*!
+ \qmlmethod ListModel::remove(int index)
+
+ Deletes the content at \a index from the model.
+
+ \sa clear()
+*/
+void QmlListModel::remove(int index)
+{
+ if (_root) {
+ ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index));
+ _root->values.remove(index);
+ if (node)
+ delete node;
+ emit itemsRemoved(index,1);
+ }
+}
+
+/*!
+ \qmlmethod ListModel::insert(index,dict)
+
+ Adds a new item to the list model at position \a index, with the
+ values in \a dict.
+
+ \code
+ FruitModel.insert(2, {"cost": 5.95, "name":"Pizza"})
+ \endcode
+
+ If \a index is not in the list, sufficient empty items are
+ added to the list.
+
+ \sa set() append()
+*/
+void QmlListModel::insert(int index, const QVariantMap& valuemap)
+{
+ if (!_root)
+ _root = new ModelNode;
+ if (index >= _root->values.count()) {
+ set(index,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);
+ }
+ _root->values.insert(index,qVariantFromValue(mn));
+ emit itemsInserted(index,1);
+}
+
+/*!
+ \qmlmethod ListModel::move(from,to,n)
+
+ Moves \a n items \a from one position \a to another.
+
+ The from and to ranges must exist; for example, to move the first 3 items
+ to the end of the list:
+
+ \code
+ FruitModel.move(0,FruitModel.count-3,3)
+ \endcode
+
+ \sa append()
+*/
+void QmlListModel::move(int from, int to, int n)
+{
+ if (from+n > count() || to+n > count() || n==0 || from==to)
+ return;
+ if (from > to) {
+ // Only move forwards - flip if backwards moving
+ int tfrom = from;
+ int tto = to;
+ from = tto;
+ to = tto+n;
+ n = tfrom-tto;
+ }
+ if (n==1) {
+ _root->values.move(from,to);
+ } else {
+ QList<QVariant> replaced;
+ int i=0;
+ QVariantList::const_iterator it=_root->values.begin(); it += from+n;
+ for (; i<to-from; ++i,++it)
+ replaced.append(*it);
+ i=0;
+ it=_root->values.begin(); it += from;
+ for (; i<n; ++i,++it)
+ replaced.append(*it);
+ QVariantList::const_iterator f=replaced.begin();
+ QVariantList::iterator t=_root->values.begin(); t += from;
+ for (; f != replaced.end(); ++f, ++t)
+ *t = *f;
+ }
+ emit itemsMoved(from,to,n);
+}
+
+/*!
+ \qmlmethod ListModel::append(dict)
+
+ Adds a new item to the end of the list model, with the
+ values in \a dict.
+
+ \code
+ FruitModel.append({"cost": 5.95, "name":"Pizza"})
+ \endcode
+
+ \sa set() remove()
+*/
+void QmlListModel::append(const QVariantMap& valuemap)
+{
+ 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);
+ }
+ _root->values << qVariantFromValue(mn);
+ emit itemsInserted(count()-1,1);
+}
+
+/*!
+ \qmlmethod ListModel::set(index,dict)
+
+ Changes the item at \a index in the list model to the
+ values in \a dict.
+
+ \code
+ FruitModel.set(3, {"cost": 5.95, "name":"Pizza"})
+ \endcode
+
+ If \a index is not in the list, sufficient empty items are
+ added to the list.
+
+ \sa append()
+*/
+void QmlListModel::set(int index, const QVariantMap& valuemap)
+{
+ if (!_root)
+ _root = new ModelNode;
+ int initialcount = _root->values.count();
+ while (index > _root->values.count())
+ _root->values.append(qVariantFromValue(new ModelNode));
+ if (index == _root->values.count())
+ append(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());
+ if (r<0) {
+ r = roleStrings.count();
+ roleStrings << it.key();
+ }
+ roles.append(r);
+ }
+ if (initialcount < index) {
+ emit itemsInserted(initialcount,index-initialcount+1);
+ } else {
+ emit itemsChanged(index,1,roles);
+ }
+ }
+}
+
+/*!
+ \qmlmethod ListModel::set(index,property,value)
+
+ Changes the \a property of the item at \a index in the list model to \a value.
+
+ \code
+ FruitModel.set(3, "cost", 5.95)
+ \endcode
+
+ If \a index is not in the list, sufficient empty items are
+ added to the list.
+
+ \sa append()
+*/
+void QmlListModel::set(int index, const QString& property, const QVariant& value)
+{
+ if (!_root)
+ _root = new ModelNode;
+ int initialcount = _root->values.count();
+ while (index >= _root->values.count())
+ _root->values.append(qVariantFromValue(new ModelNode));
+ ModelNode *node = qvariant_cast<ModelNode *>(_root->values.at(index));
+ int r = roleStrings.indexOf(property);
+ if (r<0) {
+ r = roleStrings.count();
+ roleStrings << property;
+ }
+ QList<int> roles;
+ roles.append(r);
+
+ if (node)
+ node->setProperty(property,value);
+ if (initialcount < index)
+ emit itemsInserted(initialcount,index-initialcount+1);
+ else
+ emit itemsChanged(index,1,roles);
+}
+
+
class QmlListModelParser : public QmlCustomParser
{
public:
@@ -518,7 +781,7 @@ static void dump(ModelNode *node, int ind)
for (int ii = 0; ii < node->values.count(); ++ii) {
ModelNode *subNode = qvariant_cast<ModelNode *>(node->values.at(ii));
if (subNode) {
- qWarning().nospace() << indent << "Sub-node " << ii << ": class " << subNode->className;
+ qWarning().nospace() << indent << "Sub-node " << ii;
dump(subNode, ind + 1);
} else {
qWarning().nospace() << indent << "Sub-node " << ii << ": " << node->values.at(ii).toString();
diff --git a/src/declarative/util/qmllistmodel.h b/src/declarative/util/qmllistmodel.h
index 39edbe4..8bef347 100644
--- a/src/declarative/util/qmllistmodel.h
+++ b/src/declarative/util/qmllistmodel.h
@@ -72,6 +72,14 @@ public:
virtual int count() const;
virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const;
+ Q_INVOKABLE void clear();
+ Q_INVOKABLE void remove(int index);
+ Q_INVOKABLE void append(const QVariantMap& valuemap);
+ Q_INVOKABLE void insert(int index, const QVariantMap& valuemap);
+ Q_INVOKABLE void set(int index, const QVariantMap& valuemap);
+ Q_INVOKABLE void set(int index, const QString& property, const QVariant& value);
+ Q_INVOKABLE void move(int from, int to, int count);
+
private:
QVariant valueForNode(ModelNode *) const;
mutable QStringList roleStrings;