Add optional count parameter to ListModel.remove
authorGlenn Watson <glenn.watson@nokia.com>
Tue, 15 Nov 2011 01:32:56 +0000 (11:32 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 16 Nov 2011 03:39:26 +0000 (04:39 +0100)
When calling remove on ListModel, it's now possible to supply
a second parameter giving the number of items to remove. If the
count parameter is omitted, it defaults to one, which maintains
the previous behaviour.

Task-number: QTBUG-22601
Change-Id: I0fb5c394a2b312095ce6e99e379d0f789d3ab4e6
Reviewed-by: Martin Jones <martin.jones@nokia.com>

src/declarative/util/qdeclarativelistmodel.cpp
src/declarative/util/qdeclarativelistmodel_p.h
src/declarative/util/qdeclarativelistmodel_p_p.h
src/declarative/util/qdeclarativelistmodelworkeragent.cpp
src/declarative/util/qdeclarativelistmodelworkeragent_p.h
tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp

index dceb004..9e4d726 100644 (file)
@@ -554,11 +554,13 @@ void ListModel::clear()
     elements.clear();
 }
 
-void ListModel::remove(int index)
+void ListModel::remove(int index, int count)
 {
-    elements[index]->destroy(m_layout);
-    delete elements[index];
-    elements.remove(index);
+    for (int i=0 ; i < count ; ++i) {
+        elements[index+i]->destroy(m_layout);
+        delete elements[index+i];
+    }
+    elements.remove(index, count);
     updateCacheIndices();
 }
 
@@ -1455,22 +1457,30 @@ void QDeclarativeListModel::clear()
 }
 
 /*!
-    \qmlmethod QtQuick2::ListModel::remove(int index)
+    \qmlmethod QtQuick2::ListModel::remove(int index, int count = 1)
 
     Deletes the content at \a index from the model.
 
     \sa clear()
 */
-void QDeclarativeListModel::remove(int index)
+void QDeclarativeListModel::remove(QDeclarativeV8Function *args)
 {
-    if (index < 0 || index >= count()) {
-        qmlInfo(this) << tr("remove: index %1 out of range").arg(index);
-        return;
-    }
+    int argLength = args->Length();
+
+    if (argLength == 1 || argLength == 2) {
+        int index = (*args)[0]->Int32Value();
+        int removeCount = (argLength == 2 ? ((*args)[1]->Int32Value()) : 1);
 
-    m_listModel->remove(index);
+        if (index < 0 || index+removeCount > count() || removeCount <= 0) {
+            qmlInfo(this) << tr("remove: indices [%1 - %2] out of range [0 - %3]").arg(index).arg(index+removeCount).arg(count());
+            return;
+        }
 
-    emitItemsRemoved(index, 1);
+        m_listModel->remove(index, removeCount);
+        emitItemsRemoved(index, removeCount);
+    } else {
+        qmlInfo(this) << tr("remove: incorrect number of arguments");
+    }
 }
 
 /*!
index 05cc8c2..83a41eb 100644 (file)
@@ -80,7 +80,7 @@ public:
     virtual QVariant data(int index, int role) const;
 
     Q_INVOKABLE void clear();
-    Q_INVOKABLE void remove(int index);
+    Q_INVOKABLE void remove(QDeclarativeV8Function *args);
     Q_INVOKABLE void append(QDeclarativeV8Function *args);
     Q_INVOKABLE void insert(QDeclarativeV8Function *args);
     Q_INVOKABLE QDeclarativeV8Handle get(int index) const;
index 89e8e5e..3c31950 100644 (file)
@@ -275,7 +275,7 @@ public:
     void insert(int elementIndex, v8::Handle<v8::Object> object, QV8Engine *eng);
 
     void clear();
-    void remove(int index);
+    void remove(int index, int count);
 
     int appendElement();
     void insertElement(int index);
index 2e7ef05..c01025c 100644 (file)
@@ -122,9 +122,9 @@ void QDeclarativeListModelWorkerAgent::clear()
     m_copy->clear();
 }
 
-void QDeclarativeListModelWorkerAgent::remove(int index)
+void QDeclarativeListModelWorkerAgent::remove(QDeclarativeV8Function *args)
 {
-    m_copy->remove(index);
+    m_copy->remove(args);
 }
 
 void QDeclarativeListModelWorkerAgent::append(QDeclarativeV8Function *args)
index 5865138..05c3647 100644 (file)
@@ -85,7 +85,7 @@ public:
     int count() const;
 
     Q_INVOKABLE void clear();
-    Q_INVOKABLE void remove(int index);
+    Q_INVOKABLE void remove(QDeclarativeV8Function *args);
     Q_INVOKABLE void append(QDeclarativeV8Function *args);
     Q_INVOKABLE void insert(QDeclarativeV8Function *args);
     Q_INVOKABLE QDeclarativeV8Handle get(int index) const;
index e5e1209..c32a904 100644 (file)
@@ -439,10 +439,20 @@ void tst_qdeclarativelistmodel::dynamic_data()
     QTest::newRow("remove2b") << "{append({'foo':123});append({'foo':456});remove(0);get(0).foo}" << 456 << "";
     QTest::newRow("remove2c") << "{append({'foo':123});append({'foo':456});remove(1);get(0).foo}" << 123 << "";
     QTest::newRow("remove3") << "{append({'foo':123});remove(0)}" << 0 << "";
-    QTest::newRow("remove3a") << "{append({'foo':123});remove(-1);count}" << 1 << "<Unknown File>: QML ListModel: remove: index -1 out of range";
-    QTest::newRow("remove4a") << "{remove(0)}" << 0 << "<Unknown File>: QML ListModel: remove: index 0 out of range";
-    QTest::newRow("remove4b") << "{append({'foo':123});remove(0);remove(0);count}" << 0 << "<Unknown File>: QML ListModel: remove: index 0 out of range";
-    QTest::newRow("remove4c") << "{append({'foo':123});remove(1);count}" << 1 << "<Unknown File>: QML ListModel: remove: index 1 out of range";
+    QTest::newRow("remove3a") << "{append({'foo':123});remove(-1);count}" << 1 << "<Unknown File>: QML ListModel: remove: indices [-1 - 0] out of range [0 - 1]";
+    QTest::newRow("remove4a") << "{remove(0)}" << 0 << "<Unknown File>: QML ListModel: remove: indices [0 - 1] out of range [0 - 0]";
+    QTest::newRow("remove4b") << "{append({'foo':123});remove(0);remove(0);count}" << 0 << "<Unknown File>: QML ListModel: remove: indices [0 - 1] out of range [0 - 0]";
+    QTest::newRow("remove4c") << "{append({'foo':123});remove(1);count}" << 1 << "<Unknown File>: QML ListModel: remove: indices [1 - 2] out of range [0 - 1]";
+    QTest::newRow("remove5a") << "{append({'foo':123});append({'foo':456});remove(0,2);count}" << 0 << "";
+    QTest::newRow("remove5b") << "{append({'foo':123});append({'foo':456});remove(0,1);count}" << 1 << "";
+    QTest::newRow("remove5c") << "{append({'foo':123});append({'foo':456});remove(1,1);count}" << 1 << "";
+    QTest::newRow("remove5d") << "{append({'foo':123});append({'foo':456});remove(0,1);get(0).foo}" << 456 << "";
+    QTest::newRow("remove5e") << "{append({'foo':123});append({'foo':456});remove(1,1);get(0).foo}" << 123 << "";
+    QTest::newRow("remove5f") << "{append({'foo':123});append({'foo':456});append({'foo':789});remove(0,1);remove(1,1);get(0).foo}" << 456 << "";
+    QTest::newRow("remove6a") << "{remove();count}" << 0 << "<Unknown File>: QML ListModel: remove: incorrect number of arguments";
+    QTest::newRow("remove6b") << "{remove(1,2,3);count}" << 0 << "<Unknown File>: QML ListModel: remove: incorrect number of arguments";
+    QTest::newRow("remove7a") << "{append({'foo':123});remove(0,0);count}" << 1 << "<Unknown File>: QML ListModel: remove: indices [0 - 0] out of range [0 - 1]";
+    QTest::newRow("remove7b") << "{append({'foo':123});remove(0,-1);count}" << 1 << "<Unknown File>: QML ListModel: remove: indices [0 - -1] out of range [0 - 1]";
 
     QTest::newRow("insert1") << "{insert(0,{'foo':123});count}" << 1 << "";
     QTest::newRow("insert2") << "{insert(1,{'foo':123});count}" << 0 << "<Unknown File>: QML ListModel: insert: index 1 out of range";