From eef97750eb1c5b33ed8e0e8c0d1bbf6385239701 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Tue, 15 Nov 2011 11:32:56 +1000 Subject: [PATCH] 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 --- src/declarative/util/qdeclarativelistmodel.cpp | 34 ++++++++++++++-------- src/declarative/util/qdeclarativelistmodel_p.h | 2 +- src/declarative/util/qdeclarativelistmodel_p_p.h | 2 +- .../util/qdeclarativelistmodelworkeragent.cpp | 4 +-- .../util/qdeclarativelistmodelworkeragent_p.h | 2 +- .../tst_qdeclarativelistmodel.cpp | 18 +++++++++--- 6 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/declarative/util/qdeclarativelistmodel.cpp b/src/declarative/util/qdeclarativelistmodel.cpp index dceb004..9e4d726 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 05cc8c2..83a41eb 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 89e8e5e..3c31950 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 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 2e7ef05..c01025c 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 5865138..05c3647 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 e5e1209..c32a904 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 << ": QML ListModel: remove: index -1 out of range"; - QTest::newRow("remove4a") << "{remove(0)}" << 0 << ": QML ListModel: remove: index 0 out of range"; - QTest::newRow("remove4b") << "{append({'foo':123});remove(0);remove(0);count}" << 0 << ": QML ListModel: remove: index 0 out of range"; - QTest::newRow("remove4c") << "{append({'foo':123});remove(1);count}" << 1 << ": QML ListModel: remove: index 1 out of range"; + QTest::newRow("remove3a") << "{append({'foo':123});remove(-1);count}" << 1 << ": QML ListModel: remove: indices [-1 - 0] out of range [0 - 1]"; + QTest::newRow("remove4a") << "{remove(0)}" << 0 << ": QML ListModel: remove: indices [0 - 1] out of range [0 - 0]"; + QTest::newRow("remove4b") << "{append({'foo':123});remove(0);remove(0);count}" << 0 << ": QML ListModel: remove: indices [0 - 1] out of range [0 - 0]"; + QTest::newRow("remove4c") << "{append({'foo':123});remove(1);count}" << 1 << ": 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 << ": QML ListModel: remove: incorrect number of arguments"; + QTest::newRow("remove6b") << "{remove(1,2,3);count}" << 0 << ": QML ListModel: remove: incorrect number of arguments"; + QTest::newRow("remove7a") << "{append({'foo':123});remove(0,0);count}" << 1 << ": QML ListModel: remove: indices [0 - 0] out of range [0 - 1]"; + QTest::newRow("remove7b") << "{append({'foo':123});remove(0,-1);count}" << 1 << ": 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 << ": QML ListModel: insert: index 1 out of range"; -- 2.7.4