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>
This commit is contained in:
parent
af48e0ba36
commit
764840ec0e
@ -1958,6 +1958,48 @@ bool QAbstractItemModel::removeColumns(int, int, const QModelIndex &)
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
On models that support this, moves \a count rows starting with the given
|
||||
\a sourceRow under parent \a sourceParent to row \a destinationChild under
|
||||
\a parent \a destinationParent.
|
||||
|
||||
Returns true if the rows were successfully moved; otherwise returns
|
||||
false.
|
||||
|
||||
The base class implementation does nothing and returns false.
|
||||
|
||||
If you implement your own model, you can reimplement this function if you
|
||||
want to support moving. Alternatively, you can provide your own API for
|
||||
altering the data.
|
||||
|
||||
\sa beginMoveRows(), endMoveRows()
|
||||
*/
|
||||
bool QAbstractItemModel::moveRows(const QModelIndex &, int , int , const QModelIndex &, int)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
On models that support this, moves \a count columns starting with the given
|
||||
\a sourceColumn under parent \a sourceParent to column \a destinationChild under
|
||||
\a parent \a destinationParent.
|
||||
|
||||
Returns true if the columns were successfully moved; otherwise returns
|
||||
false.
|
||||
|
||||
The base class implementation does nothing and returns false.
|
||||
|
||||
If you implement your own model, you can reimplement this function if you
|
||||
want to support moving. Alternatively, you can provide your own API for
|
||||
altering the data.
|
||||
|
||||
\sa beginMoveColumns(), endMoveColumns()
|
||||
*/
|
||||
bool QAbstractItemModel::moveColumns(const QModelIndex &, int , int , const QModelIndex &, int)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
Fetches any available data for the items with the parent specified by the
|
||||
\a parent index.
|
||||
|
@ -210,11 +210,19 @@ public:
|
||||
virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex());
|
||||
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
|
||||
virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex());
|
||||
virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
|
||||
const QModelIndex &destinationParent, int destinationChild);
|
||||
virtual bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count,
|
||||
const QModelIndex &destinationParent, int destinationChild);
|
||||
|
||||
inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
|
||||
inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
|
||||
inline bool removeRow(int row, const QModelIndex &parent = QModelIndex());
|
||||
inline bool removeColumn(int column, const QModelIndex &parent = QModelIndex());
|
||||
inline bool moveRow(const QModelIndex &sourceParent, int sourceRow,
|
||||
const QModelIndex &destinationParent, int destinationChild);
|
||||
inline bool moveColumn(const QModelIndex &sourceParent, int sourceColumn,
|
||||
const QModelIndex &destinationParent, int destinationChild);
|
||||
|
||||
virtual void fetchMore(const QModelIndex &parent);
|
||||
virtual bool canFetchMore(const QModelIndex &parent) const;
|
||||
@ -330,7 +338,12 @@ inline bool QAbstractItemModel::removeRow(int arow, const QModelIndex &aparent)
|
||||
{ return removeRows(arow, 1, aparent); }
|
||||
inline bool QAbstractItemModel::removeColumn(int acolumn, const QModelIndex &aparent)
|
||||
{ return removeColumns(acolumn, 1, aparent); }
|
||||
|
||||
inline bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sourceRow,
|
||||
const QModelIndex &destinationParent, int destinationChild)
|
||||
{ return moveRows(sourceParent, sourceRow, 1, destinationParent, destinationChild); }
|
||||
inline bool QAbstractItemModel::moveColumn(const QModelIndex &sourceParent, int sourceColumn,
|
||||
const QModelIndex &destinationParent, int destinationChild)
|
||||
{ return moveRows(sourceParent, sourceColumn, 1, destinationParent, destinationChild); }
|
||||
inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, void *adata) const
|
||||
{ return QModelIndex(arow, acolumn, adata, this); }
|
||||
inline QModelIndex QAbstractItemModel::createIndex(int arow, int acolumn, int aid) const
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user