Implement support for group tree changes in GroupModel.

This commit is contained in:
Felix Geyer 2010-08-18 10:27:40 +02:00
parent e28ed4891b
commit 623b325fa1
8 changed files with 134 additions and 21 deletions

View File

@ -31,5 +31,5 @@ set(keepassx_SOURCES
automoc4_add_library( keepassx_core STATIC ${keepassx_SOURCES} )
add_executable( ${PROGNAME} WIN32 MACOSX_BUNDLE main.cpp )
automoc4_add_executable( ${PROGNAME} WIN32 MACOSX_BUNDLE main.cpp )
target_link_libraries( ${PROGNAME} keepassx_core ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} )

View File

@ -43,7 +43,11 @@ public:
Group* resolveGroup(const Uuid& uuid);
Q_SIGNALS:
void groupChanged(const Group* group);
void groupDataChanged(const Group* group);
void groupAboutToAdd(const Group* group, int index);
void groupAdded();
void groupAboutToRemove(const Group* group);
void groupRemoved();
private:
Entry* recFindEntry(const Uuid& uuid, Group* group);

View File

@ -80,7 +80,7 @@ void Group::setName(const QString& name)
{
m_name = name;
Q_EMIT groupChanged(this);
Q_EMIT dataChanged(this);
}
void Group::setNotes(const QString& notes)
@ -95,7 +95,7 @@ void Group::setIcon(int iconNumber)
m_iconNumber = iconNumber;
m_customIcon = Uuid();
Q_EMIT groupChanged(this);
Q_EMIT dataChanged(this);
}
void Group::setIcon(const Uuid& uuid)
@ -105,7 +105,7 @@ void Group::setIcon(const Uuid& uuid)
m_iconNumber = 0;
m_customIcon = uuid;
Q_EMIT groupChanged(this);
Q_EMIT dataChanged(this);
}
void Group::setTimeInfo(const TimeInfo& timeInfo)
@ -133,9 +133,16 @@ const Group* Group::parentGroup() const
return m_parent;
}
void Group::setParent(Group* parent)
void Group::setParent(Group* parent, int index)
{
Q_ASSERT(parent != 0);
Q_ASSERT(parent);
Q_ASSERT(index >= -1 && index <= parent->children().size());
if (index == -1) {
index = parent->children().size();
}
Q_EMIT aboutToRemove(this);
if (m_parent) {
m_parent->m_children.removeAll(this);
@ -145,6 +152,8 @@ void Group::setParent(Group* parent)
m_db->setRootGroup(0);
}
Q_EMIT removed();
m_parent = parent;
if (m_db != parent->m_db) {
@ -153,12 +162,18 @@ void Group::setParent(Group* parent)
QObject::setParent(parent);
parent->m_children << this;
Q_EMIT aboutToAdd(this, index);
parent->m_children.insert(index, this);
Q_EMIT added();
}
void Group::setParent(Database* db)
{
Q_ASSERT(db != 0);
Q_ASSERT(db);
Q_EMIT aboutToRemove(this);
if (m_parent) {
m_parent->m_children.removeAll(this);
@ -167,6 +182,8 @@ void Group::setParent(Database* db)
m_db->setRootGroup(0);
}
Q_EMIT removed();
m_parent = 0;
recSetDatabase(db);
@ -224,8 +241,17 @@ void Group::removeEntry(Entry* entry)
void Group::recSetDatabase(Database* db)
{
disconnect(SIGNAL(groupChanged(const Group*)), m_db);
connect(this, SIGNAL(groupChanged(const Group*)), db, SIGNAL(groupChanged(const Group*)));
disconnect(SIGNAL(dataChanged(const Group*)), m_db);
disconnect(SIGNAL(aboutToRemove(const Group*)), m_db);
disconnect(SIGNAL(removed()), m_db);
disconnect(SIGNAL(aboutToAdd(const Group*,int)), m_db);
disconnect(SIGNAL(added()), m_db);
connect(this, SIGNAL(dataChanged(const Group*)), db, SIGNAL(groupDataChanged(const Group*)));
connect(this, SIGNAL(aboutToRemove(const Group*)), db, SIGNAL(groupAboutToRemove(const Group*)));
connect(this, SIGNAL(removed()), db, SIGNAL(groupRemoved()));
connect(this, SIGNAL(aboutToAdd(const Group*,int)), db, SIGNAL(groupAboutToAdd(const Group*,int)));
connect(this, SIGNAL(added()), db, SIGNAL(groupAdded()));
m_db = db;

View File

@ -52,7 +52,7 @@ public:
void setLastTopVisibleEntry(Entry* entry);
const Group* parentGroup() const;
void setParent(Group* parent);
void setParent(Group* parent, int index = -1);
void setParent(Database* db);
const Database* database() const;
@ -64,7 +64,11 @@ public:
void removeEntry(Entry* entry);
Q_SIGNALS:
void groupChanged(const Group* group);
void dataChanged(const Group* group);
void aboutToRemove(const Group* group);
void removed();
void aboutToAdd(const Group* group, int index);
void added();
private:
void recSetDatabase(Database* db);

View File

@ -23,7 +23,11 @@
GroupModel::GroupModel(const Database* db, QObject* parent) : QAbstractItemModel(parent)
{
m_root = db->rootGroup();
connect(db, SIGNAL(groupChanged(const Group*)), SLOT(groupChanged(const Group*)));
connect(db, SIGNAL(groupDataChanged(const Group*)), SLOT(groupDataChanged(const Group*)));
connect(db, SIGNAL(groupAboutToAdd(const Group*,int)), SLOT(groupAboutToAdd(const Group*,int)));
connect(db, SIGNAL(groupAdded()), SLOT(groupAdded()));
connect(db, SIGNAL(groupAboutToRemove(const Group*)), SLOT(groupAboutToRemove(const Group*)));
connect(db, SIGNAL(groupRemoved()), SLOT(groupRemoved()));
}
int GroupModel::rowCount(const QModelIndex& parent) const
@ -69,8 +73,12 @@ QModelIndex GroupModel::parent(const QModelIndex& index) const
return QModelIndex();
}
const Group* childGroup = groupFromIndex(index);
const Group* parentGroup = childGroup->parentGroup();
return parent(groupFromIndex(index));
}
QModelIndex GroupModel::parent(const Group* group) const
{
const Group* parentGroup = group->parentGroup();
if (!parentGroup) {
// index is already the root group
@ -128,7 +136,7 @@ const Group* GroupModel::groupFromIndex(const QModelIndex& index) const
return static_cast<const Group*>(index.internalPointer());
}
void GroupModel::groupChanged(const Group* group)
void GroupModel::groupDataChanged(const Group* group)
{
int row;
@ -142,3 +150,32 @@ void GroupModel::groupChanged(const Group* group)
QModelIndex index = createIndex(row, 0, group);
Q_EMIT dataChanged(index, index);
}
void GroupModel::groupAboutToRemove(const Group* group)
{
Q_ASSERT(group->parentGroup());
QModelIndex parentIndex = parent(group);
int pos = group->parentGroup()->children().indexOf(group);
beginRemoveRows(parentIndex, pos, pos);
}
void GroupModel::groupRemoved()
{
endRemoveRows();
}
void GroupModel::groupAboutToAdd(const Group* group, int index)
{
Q_ASSERT(group->parentGroup());
QModelIndex parentIndex = parent(group);
beginInsertRows(parentIndex, index, index);
}
void GroupModel::groupAdded()
{
endInsertRows();
}

View File

@ -40,9 +40,14 @@ public:
private:
QModelIndex createIndex(int row, int column, const Group* group) const;
const Group* groupFromIndex(const QModelIndex& index) const;
QModelIndex parent(const Group* group) const;
private Q_SLOTS:
void groupChanged(const Group* group);
void groupDataChanged(const Group* group);
void groupAboutToRemove(const Group* group);
void groupRemoved();
void groupAboutToAdd(const Group* group, int index);
void groupAdded();
private:
const Group* m_root;

View File

@ -26,6 +26,7 @@ class TestGroup : public QObject
private Q_SLOTS:
void testParenting();
void testSignals();
};
void TestGroup::testParenting()
@ -67,7 +68,7 @@ void TestGroup::testParenting()
QVERIFY(g1->children().at(1) == g3);
QVERIFY(g3->children().contains(g4));
QSignalSpy spy(db, SIGNAL(groupChanged(const Group*)));
QSignalSpy spy(db, SIGNAL(groupDataChanged(const Group*)));
g2->setName("test");
g4->setName("test");
g3->setName("test");
@ -77,6 +78,30 @@ void TestGroup::testParenting()
QVERIFY(spy.count() == 6);
}
void TestGroup::testSignals()
{
Database* db = new Database();
Group* root = new Group();
root->setParent(db);
Group* g1 = new Group();
Group* g2 = new Group();
g1->setParent(root);
g2->setParent(root);
QSignalSpy spyAboutToAdd(db, SIGNAL(groupAboutToAdd(const Group*,int)));
QSignalSpy spyAdded(db, SIGNAL(groupAdded()));
QSignalSpy spyAboutToRemove(db, SIGNAL(groupAboutToRemove(const Group*)));
QSignalSpy spyRemoved(db, SIGNAL(groupRemoved()));
g2->setParent(root, 0);
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 1);
QCOMPARE(spyRemoved.count(), 1);
}
QTEST_MAIN(TestGroup);
#include "TestGroup.moc"

View File

@ -77,10 +77,22 @@ void TestGroupModel::test()
QVERIFY(model->data(index121) == "group121");
QVERIFY(model->data(index2) == "group2");
QSignalSpy spy(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)));
QSignalSpy spy1(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)));
group11->setName("test");
group121->setIcon(4);
QVERIFY(spy.count() == 2);
QCOMPARE(spy1.count(), 2);
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)));
group12->setParent(group1, 0);
QCOMPARE(spyAboutToAdd.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(spyAboutToRemove.count(), 1);
QCOMPARE(spyRemoved.count(), 1);
delete groupRoot;
delete db;