src/declarative/util/qdeclarativelistmodel.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
--- a/src/declarative/util/qdeclarativelistmodel.cpp	Fri Sep 17 08:34:18 2010 +0300
+++ b/src/declarative/util/qdeclarativelistmodel.cpp	Mon Oct 04 01:19:32 2010 +0300
@@ -58,9 +58,6 @@
 
 QT_BEGIN_NAMESPACE
 
-#define DATA_ROLE_ID 1
-#define DATA_ROLE_NAME "data"
-
 QDeclarativeListModelParser::ListInstruction *QDeclarativeListModelParser::ListModelData::instructions() const
 {
     return (QDeclarativeListModelParser::ListInstruction *)((char *)this + sizeof(ListModelData));
@@ -68,6 +65,7 @@
 
 /*!
     \qmlclass ListModel QDeclarativeListModel
+    \ingroup qml-working-with-data
     \since 4.7
     \brief The ListModel element defines a free-form list data source.
 
@@ -108,9 +106,9 @@
     
     \snippet doc/src/snippets/declarative/listmodel-modify.qml delegate
 
-    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 to the model are then the
-    only permitted properties in the model until it is cleared.
+    Note that when creating content dynamically the set of available properties cannot be changed
+    once set. Whatever properties are first added to the model are the
+    only permitted properties in the model.
 
 
     \section2 Using threaded list models with WorkerScript
@@ -129,7 +127,7 @@
 
     \snippet examples/declarative/threading/threadedlistmodel/dataloader.js 0
 
-    The application's \tt Timer object periodically sends a message to the
+working-with-data
     worker script by calling \l WorkerScript::sendMessage(). When this message
     is received, \l {WorkerScript::onMessage}{WorkerScript.onMessage()} is invoked in
     \tt dataloader.js, which appends the current time to the list model.
@@ -283,8 +281,7 @@
 /*!
     \qmlmethod ListModel::clear()
 
-    Deletes all content from the model. The properties are cleared such that
-    different properties may be set on subsequent additions.
+    Deletes all content from the model.
 
     \sa append() remove()
 */
@@ -570,7 +567,7 @@
             QList<QDeclarativeCustomParserProperty> props = node.properties();
             for(int jj = 0; jj < props.count(); ++jj) {
                 const QDeclarativeCustomParserProperty &nodeProp = props.at(jj);
-                if (nodeProp.name() == "") {
+                if (nodeProp.name().isEmpty()) {
                     error(nodeProp, QDeclarativeListModel::tr("ListElement: cannot contain nested elements"));
                     return false;
                 }
@@ -658,7 +655,7 @@
 
     for(int ii = 0; ii < customProps.count(); ++ii) {
         const QDeclarativeCustomParserProperty &prop = customProps.at(ii);
-        if(prop.name() != "") { // isn't default property
+        if(!prop.name().isEmpty()) { // isn't default property
             error(prop, QDeclarativeListModel::tr("ListModel: undefined property '%1'").arg(QString::fromUtf8(prop.name())));
             return QByteArray();
         }
@@ -770,6 +767,7 @@
 
 /*!
     \qmlclass ListElement
+    \ingroup qml-working-with-data
     \since 4.7
     \brief The ListElement element defines a data item in a ListModel.
 
@@ -945,13 +943,14 @@
 }
 
 NestedListModel::NestedListModel(QDeclarativeListModel *base)
-    : _root(0), m_listModel(base), _rolesOk(false)
+    : _root(0), m_ownsRoot(false), m_listModel(base), _rolesOk(false)
 {
 }
 
 NestedListModel::~NestedListModel()
 {
-    delete _root;
+    if (m_ownsRoot)
+        delete _root;
 }
 
 QVariant NestedListModel::valueForNode(ModelNode *node, bool *hasNested) const
@@ -1051,8 +1050,8 @@
     _rolesOk = false;
     roleStrings.clear();
 
-    delete _root;
-    _root = 0;
+    if (_root)
+        _root->clear();
 }
 
 void NestedListModel::remove(int index)
@@ -1067,8 +1066,10 @@
 
 bool NestedListModel::insert(int index, const QScriptValue& valuemap)
 {
-    if (!_root)
+    if (!_root) {
         _root = new ModelNode;
+        m_ownsRoot = true;
+    }
 
     ModelNode *mn = new ModelNode;
     mn->setObjectValue(valuemap);
@@ -1099,8 +1100,10 @@
 
 bool NestedListModel::append(const QScriptValue& valuemap)
 {
-    if (!_root)
+    if (!_root) {
         _root = new ModelNode;
+        m_ownsRoot = true;
+    }
     ModelNode *mn = new ModelNode;
     mn->setObjectValue(valuemap);
     _root->values << qVariantFromValue(mn);
@@ -1205,16 +1208,22 @@
 
 ModelNode::~ModelNode()
 {
-    qDeleteAll(properties.values());
+    clear();
+    if (modelCache) { modelCache->m_nested->_root = 0/* ==this */; delete modelCache; modelCache = 0; }
+    if (objectCache) { delete objectCache; objectCache = 0; }
+}
 
+void ModelNode::clear()
+{
     ModelNode *node;
     for (int ii = 0; ii < values.count(); ++ii) {
         node = qvariant_cast<ModelNode *>(values.at(ii));
         if (node) { delete node; node = 0; }
     }
+    values.clear();
 
-    if (modelCache) { modelCache->m_nested->_root = 0/* ==this */; delete modelCache; modelCache = 0; }
-    if (objectCache) { delete objectCache; objectCache = 0; }
+    qDeleteAll(properties.values());
+    properties.clear();
 }
 
 void ModelNode::setObjectValue(const QScriptValue& valuemap) {