Introduce Group::aboutToMove() and moved() signals.

Emit them instead of remove/add when groups are moved within
a database.
This commit is contained in:
Felix Geyer
2012-04-23 16:57:08 +02:00
parent 6340ed8993
commit bddc29101c
10 changed files with 239 additions and 37 deletions

View File

@@ -99,26 +99,82 @@ void TestGroup::testParenting()
void TestGroup::testSignals()
{
Database* db = new Database();
Database* db2 = new Database();
QPointer<Group> root = db->rootGroup();
Group* g1 = new Group();
Group* g2 = new Group();
g1->setParent(root);
g2->setParent(root);
QSignalSpy spyAboutToAdd(db, SIGNAL(groupAboutToAdd(Group*,int)));
QSignalSpy spyAdded(db, SIGNAL(groupAdded()));
QSignalSpy spyAboutToRemove(db, SIGNAL(groupAboutToRemove(Group*)));
QSignalSpy spyRemoved(db, SIGNAL(groupRemoved()));
QSignalSpy spyAboutToMove(db, SIGNAL(groupAboutToMove(Group*,Group*,int)));
QSignalSpy spyMoved(db, SIGNAL(groupMoved()));
g2->setParent(root, 0);
QSignalSpy spyAboutToAdd2(db2, SIGNAL(groupAboutToAdd(Group*,int)));
QSignalSpy spyAdded2(db2, SIGNAL(groupAdded()));
QSignalSpy spyAboutToRemove2(db2, SIGNAL(groupAboutToRemove(Group*)));
QSignalSpy spyRemoved2(db2, SIGNAL(groupRemoved()));
QSignalSpy spyAboutToMove2(db2, SIGNAL(groupAboutToMove(Group*,Group*,int)));
QSignalSpy spyMoved2(db2, SIGNAL(groupMoved()));
Group* g1 = new Group();
Group* g2 = new Group();
g1->setParent(root);
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 0);
QCOMPARE(spyMoved.count(), 0);
g2->setParent(root);
QCOMPARE(spyAboutToAdd.count(), 2);
QCOMPARE(spyAdded.count(), 2);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 0);
QCOMPARE(spyMoved.count(), 0);
g2->setParent(root, 0);
QCOMPARE(spyAboutToAdd.count(), 2);
QCOMPARE(spyAdded.count(), 2);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 1);
QCOMPARE(spyMoved.count(), 1);
g1->setParent(g2);
QCOMPARE(spyAboutToAdd.count(), 2);
QCOMPARE(spyAdded.count(), 2);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 2);
QCOMPARE(spyMoved.count(), 2);
delete g1;
QCOMPARE(spyAboutToAdd.count(), 2);
QCOMPARE(spyAdded.count(), 2);
QCOMPARE(spyAboutToRemove.count(), 1);
QCOMPARE(spyRemoved.count(), 1);
QCOMPARE(spyAboutToMove.count(), 2);
QCOMPARE(spyMoved.count(), 2);
g2->setParent(db2->rootGroup());
QCOMPARE(spyAboutToAdd.count(), 2);
QCOMPARE(spyAdded.count(), 2);
QCOMPARE(spyAboutToRemove.count(), 2);
QCOMPARE(spyRemoved.count(), 2);
QCOMPARE(spyAboutToMove.count(), 2);
QCOMPARE(spyMoved.count(), 2);
QCOMPARE(spyAboutToAdd2.count(), 1);
QCOMPARE(spyAdded2.count(), 1);
QCOMPARE(spyAboutToRemove2.count(), 0);
QCOMPARE(spyRemoved2.count(), 0);
QCOMPARE(spyAboutToMove2.count(), 0);
QCOMPARE(spyMoved2.count(), 0);
delete db;
delete db2;
QVERIFY(root.isNull());
}

View File

@@ -61,11 +61,6 @@ void TestGroupModel::test()
group121->setName("group121");
group121->setParent(group12);
Group* group2 = new Group();
group1->setObjectName("group2");
group2->setName("group2");
group2->setParent(groupRoot);
GroupModel* model = new GroupModel(db, this);
ModelTest* modelTest = new ModelTest(model, this);
@@ -73,33 +68,78 @@ void TestGroupModel::test()
QModelIndex indexRoot = model->index(0, 0);
QModelIndex index1 = model->index(0, 0, indexRoot);
QModelIndex index11 = model->index(0, 0, index1);
QModelIndex index12 = model->index(1, 0, index1);
QPersistentModelIndex index12 = model->index(1, 0, index1);
QModelIndex index121 = model->index(0, 0, index12);
QModelIndex index2 = model->index(1, 0, indexRoot);
QCOMPARE(model->data(indexRoot).toString(), QString("groupRoot"));
QCOMPARE(model->data(index1).toString(), QString("group1"));
QCOMPARE(model->data(index11).toString(), QString("group11"));
QCOMPARE(model->data(index12).toString(), QString("group12"));
QCOMPARE(model->data(index121).toString(), QString("group121"));
QCOMPARE(model->data(index2).toString(), QString("group2"));
QSignalSpy spy1(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)));
QSignalSpy spy1(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)));
group11->setName("test");
group121->setIcon(4);
QCOMPARE(spy1.count(), 2);
QCOMPARE(model->data(index11).toString(), QString("test"));
QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(const QModelIndex&,int,int)));
QSignalSpy spyAdded(model, SIGNAL(rowsInserted(const QModelIndex&,int,int)));
QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&,int,int)));
QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(const QModelIndex&,int,int)));
QSignalSpy spyAboutToAdd(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)));
QSignalSpy spyAdded(model, SIGNAL(rowsInserted(QModelIndex,int,int)));
QSignalSpy spyAboutToRemove(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)));
QSignalSpy spyRemoved(model, SIGNAL(rowsRemoved(QModelIndex,int,int)));
QSignalSpy spyAboutToMove(model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
QSignalSpy spyMoved(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)));
Group* group2 = new Group();
group1->setObjectName("group2");
group2->setName("group2");
group2->setParent(groupRoot);
QModelIndex index2 = model->index(1, 0, indexRoot);
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 0);
QCOMPARE(spyMoved.count(), 0);
QCOMPARE(model->data(index2).toString(), QString("group2"));
group12->setParent(group1, 0);
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 1);
QCOMPARE(spyMoved.count(), 1);
QCOMPARE(model->data(index12).toString(), QString("group12"));
group12->setParent(group1, 1);
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 2);
QCOMPARE(spyMoved.count(), 2);
QCOMPARE(model->data(index12).toString(), QString("group12"));
group12->setParent(group2);
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 0);
QCOMPARE(spyRemoved.count(), 0);
QCOMPARE(spyAboutToMove.count(), 3);
QCOMPARE(spyMoved.count(), 3);
QVERIFY(index12.isValid());
QCOMPARE(model->data(index12).toString(), QString("group12"));
QCOMPARE(model->data(index12.child(0, 0)).toString(), QString("group121"));
delete group12;
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 1);
QCOMPARE(spyRemoved.count(), 1);
QCOMPARE(spyAboutToMove.count(), 3);
QCOMPARE(spyMoved.count(), 3);
QVERIFY(!index12.isValid());
// test removing a group that has children
delete group1;

View File

@@ -52,6 +52,9 @@ void TestModified::testSignals()
g2->setParent(root);
QCOMPARE(spyModified.count(), ++spyCount);
g2->setParent(root, 0);
QCOMPARE(spyModified.count(), ++spyCount);
Entry* entry1 = new Entry();
entry1->setGroup(g1);
QCOMPARE(spyModified.count(), ++spyCount);

View File

@@ -1,8 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the test suite of the Qt Toolkit.
**
@@ -94,6 +93,12 @@ ModelTest::ModelTest ( QAbstractItemModel *_model, QObject *parent ) : QObject (
connect ( model, SIGNAL ( rowsRemoved ( const QModelIndex &, int, int ) ),
this, SLOT ( rowsRemoved ( const QModelIndex &, int, int ) ) );
connect ( model, SIGNAL (rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT (rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)) );
connect ( model, SIGNAL (rowsMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT (rowsMoved(QModelIndex,int,int,QModelIndex,int)) );
runAllTests();
}
@@ -559,3 +564,50 @@ void ModelTest::rowsRemoved ( const QModelIndex & parent, int start, int end )
QVERIFY( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
QVERIFY( c.next == model->data ( model->index ( start, 0, c.parent ) ) );
}
void ModelTest::rowsAboutToBeMoved( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow )
{
Changing cs;
cs.parent = srcParent;
cs.oldSize = model->rowCount ( srcParent );
cs.last = model->data ( model->index ( start - 1, 0, srcParent ) );
cs.next = model->data ( model->index ( end + 1, 0, srcParent ) );
remove.push ( cs );
Changing cd;
cd.parent = destParent;
cd.oldSize = model->rowCount ( destParent );
cd.last = model->data ( model->index ( destinationRow - 1, 0, destParent ) );
cd.next = model->data ( model->index ( destinationRow, 0, destParent ) );
insert.push ( cd );
}
void ModelTest::rowsMoved( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow )
{
Changing cd = insert.pop();
Q_ASSERT ( cd.parent == destParent );
if (srcParent == destParent) {
Q_ASSERT ( cd.oldSize == model->rowCount ( destParent ) );
// TODO: Find out what I can assert here about last and next.
// Q_ASSERT ( cd.last == model->data ( model->index ( destinationRow - 1, 0, cd.parent ) ) );
// Q_ASSERT ( cd.next == model->data ( model->index ( destinationRow + (end - start + 1), 0, cd.parent ) ) );
}
else {
Q_ASSERT ( cd.oldSize + ( end - start + 1 ) == model->rowCount ( destParent ) );
Q_ASSERT ( cd.last == model->data ( model->index ( destinationRow - 1, 0, cd.parent ) ) );
Q_ASSERT ( cd.next == model->data ( model->index ( destinationRow + (end - start + 1), 0, cd.parent ) ) );
}
Changing cs = remove.pop();
Q_ASSERT ( cs.parent == srcParent );
if (srcParent == destParent) {
Q_ASSERT ( cs.oldSize == model->rowCount ( srcParent ) );
}
else {
Q_ASSERT ( cs.oldSize - ( end - start + 1 ) == model->rowCount ( srcParent ) );
Q_ASSERT ( cs.last == model->data ( model->index ( start - 1, 0, srcParent ) ) );
Q_ASSERT ( cs.next == model->data ( model->index ( start, 0, srcParent ) ) );
}
}

View File

@@ -71,6 +71,9 @@ protected Q_SLOTS:
void rowsInserted( const QModelIndex & parent, int start, int end );
void rowsAboutToBeRemoved( const QModelIndex &parent, int start, int end );
void rowsRemoved( const QModelIndex & parent, int start, int end );
void rowsAboutToBeMoved ( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow );
void rowsMoved ( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow );
private:
void checkChildren( const QModelIndex &parent, int currentDepth = 0 );