aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlenn Watson <glenn.watson@nokia.com>2011-11-15 11:32:56 +1000
committerQt by Nokia <qt-info@nokia.com>2011-11-16 04:39:26 +0100
commiteef97750eb1c5b33ed8e0e8c0d1bbf6385239701 (patch)
treec98699d91734ef36af2eb78089af6a5475b56b72
parentc9cd3b690386661fa19904ccf8958bf83ef0ad24 (diff)
Add optional count parameter to ListModel.remove
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>
-rw-r--r--src/declarative/util/qdeclarativelistmodel.cpp34
-rw-r--r--src/declarative/util/qdeclarativelistmodel_p.h2
-rw-r--r--src/declarative/util/qdeclarativelistmodel_p_p.h2
-rw-r--r--src/declarative/util/qdeclarativelistmodelworkeragent.cpp4
-rw-r--r--src/declarative/util/qdeclarativelistmodelworkeragent_p.h2
-rw-r--r--tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp18
6 files changed, 41 insertions, 21 deletions
diff --git a/src/declarative/util/qdeclarativelistmodel.cpp b/src/declarative/util/qdeclarativelistmodel.cpp
index dceb0045e3..9e4d7269cd 100644
--- a/src/declarative/util/qdeclarativelistmodel.cpp
+++ b/src/declarative/util/qdeclarativelistmodel.cpp
@@ -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");
+ }
}
/*!
diff --git a/src/declarative/util/qdeclarativelistmodel_p.h b/src/declarative/util/qdeclarativelistmodel_p.h
index 05cc8c218f..83a41ebc7b 100644
--- a/src/declarative/util/qdeclarativelistmodel_p.h
+++ b/src/declarative/util/qdeclarativelistmodel_p.h
@@ -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;
diff --git a/src/declarative/util/qdeclarativelistmodel_p_p.h b/src/declarative/util/qdeclarativelistmodel_p_p.h
index 89e8e5e090..3c31950b9d 100644
--- a/src/declarative/util/qdeclarativelistmodel_p_p.h
+++ b/src/declarative/util/qdeclarativelistmodel_p_p.h
@@ -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);
diff --git a/src/declarative/util/qdeclarativelistmodelworkeragent.cpp b/src/declarative/util/qdeclarativelistmodelworkeragent.cpp
index 2e7ef0599b..c01025cd45 100644
--- a/src/declarative/util/qdeclarativelistmodelworkeragent.cpp
+++ b/src/declarative/util/qdeclarativelistmodelworkeragent.cpp
@@ -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)
diff --git a/src/declarative/util/qdeclarativelistmodelworkeragent_p.h b/src/declarative/util/qdeclarativelistmodelworkeragent_p.h
index 58651386af..05c36473a5 100644
--- a/src/declarative/util/qdeclarativelistmodelworkeragent_p.h
+++ b/src/declarative/util/qdeclarativelistmodelworkeragent_p.h
@@ -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;
diff --git a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp
index e5e12092e3..c32a904d3a 100644
--- a/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp
+++ b/tests/auto/declarative/qdeclarativelistmodel/tst_qdeclarativelistmodel.cpp
@@ -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";