diff options
author | Mark Brand <mabrand@mabrand.nl> | 2011-04-10 21:48:36 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-24 19:12:23 +0100 |
commit | 764840ec0e189b4a9f76cdd659d82a2a50324190 (patch) | |
tree | 42b40caa1892cd1c219b2865b2e7e3bcb529234e /tests | |
parent | af48e0ba36aed330e8b262687e3192d7e09796f7 (diff) |
add missing move* methods to QAbstractItemModel
The existence of QAbstractItemModel virtual methods
moveRow, moveColumn, moveRows, and moveColumns
is implied by the existence of
beginMoveRows, endMoveRows, beginMoveColumns and endMoveColumns.
However, these were not actually provided by QAbstractItemModel.
With this change, subclasses can implement support for moving rows
and columns following the same pattern as for insert* and remove*.
Change-Id: Iad8b2223d4b9303abb6459c174a82ffed71a0fdf
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp index 7168a95397..24177e0859 100644 --- a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp +++ b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp @@ -79,6 +79,8 @@ private slots: void insertColumns(); void removeRows(); void removeColumns(); + void moveRows(); + void moveColumns(); void reset(); @@ -142,6 +144,10 @@ public: void setPersistent(const QModelIndex &from, const QModelIndex &to); bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); bool removeColumns( int column, int count, const QModelIndex & parent = QModelIndex()); + bool moveRows (const QModelIndex &sourceParent, int sourceRow, int count, + const QModelIndex &destinationParent, int destinationChild); + bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, + const QModelIndex &destinationParent, int destinationChild); void reset(); int cCount, rCount; @@ -246,6 +252,64 @@ bool QtTestModel::removeColumns(int column, int count, const QModelIndex & paren return true; } +bool QtTestModel::moveRows(const QModelIndex &sourceParent, int src, int cnt, + const QModelIndex &destinationParent, int dst) +{ + if (!QAbstractItemModel::beginMoveRows(sourceParent, src, src + cnt - 1, + destinationParent, dst)) + return false; + + QVector<QString> buf; + if (dst < src) { + for (int i = 0; i < cnt; ++i) { + buf.swap(table[src + i]); + table.remove(src + 1); + table.insert(dst, buf); + } + } else if (src < dst) { + for (int i = 0; i < cnt; ++i) { + buf.swap(table[src]); + table.remove(src); + table.insert(dst + i, buf); + } + } + + rCount = table.count(); + + QAbstractItemModel::endMoveRows(); + return true; +} + +bool QtTestModel::moveColumns(const QModelIndex &sourceParent, int src, int cnt, + const QModelIndex &destinationParent, int dst) +{ + if (!QAbstractItemModel::beginMoveColumns(sourceParent, src, src + cnt - 1, + destinationParent, dst)) + return false; + + for (int r = 0; r < rCount; ++r) { + QString buf; + if (dst < src) { + for (int i = 0; i < cnt; ++i) { + buf = table[r][src + i]; + table[r].remove(src + 1); + table[r].insert(dst, buf); + } + } else if (src < dst) { + for (int i = 0; i < cnt; ++i) { + buf = table[r][src]; + table[r].remove(src); + table[r].insert(dst + i, buf); + } + } + } + + cCount = table.at(0).count(); + + QAbstractItemModel::endMoveColumns(); + return true; +} + void QtTestModel::reset() { QAbstractItemModel::reset(); @@ -781,6 +845,36 @@ void tst_QAbstractItemModel::insertColumns() QCOMPARE(columnsInsertedSpy.count(), 1); } +void tst_QAbstractItemModel::moveRows() +{ + QtTestModel model(10, 10); + + QSignalSpy rowsAboutToBeMovedSpy(&model, SIGNAL(rowsAboutToBeMoved(const QModelIndex &, int , int, const QModelIndex &, int))); + QSignalSpy rowsMovedSpy(&model, SIGNAL(rowsMoved(const QModelIndex &, int , int, const QModelIndex &, int))); + + QVERIFY(rowsAboutToBeMovedSpy.isValid()); + QVERIFY(rowsMovedSpy.isValid()); + + QCOMPARE(model.moveRows(QModelIndex(), 6, 4, QModelIndex(), 1), true); + QCOMPARE(rowsAboutToBeMovedSpy.count(), 1); + QCOMPARE(rowsMovedSpy.count(), 1); +} + +void tst_QAbstractItemModel::moveColumns() +{ + QtTestModel model(10, 10); + + QSignalSpy columnsAboutToBeMovedSpy(&model, SIGNAL(columnsAboutToBeMoved(const QModelIndex &, int , int, const QModelIndex &, int))); + QSignalSpy columnsMovedSpy(&model, SIGNAL(columnsMoved(const QModelIndex &, int , int, const QModelIndex &, int))); + + QVERIFY(columnsAboutToBeMovedSpy.isValid()); + QVERIFY(columnsMovedSpy.isValid()); + + QCOMPARE(model.moveColumns(QModelIndex(), 6, 4, QModelIndex(), 1), true); + QCOMPARE(columnsAboutToBeMovedSpy.count(), 1); + QCOMPARE(columnsMovedSpy.count(), 1); +} + void tst_QAbstractItemModel::reset() { QtTestModel model(10, 10); |