diff --git a/src/core/Database.cpp b/src/core/Database.cpp index bc36693fa..355d7c191 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -33,6 +33,11 @@ Group* Database::rootGroup() return m_rootGroup; } +const Group* Database::rootGroup() const +{ + return m_rootGroup; +} + void Database::setRootGroup(Group* group) { Q_ASSERT(group == 0 || group->database() == this); diff --git a/src/core/Database.h b/src/core/Database.h index a7f275b9e..ca342c268 100644 --- a/src/core/Database.h +++ b/src/core/Database.h @@ -34,6 +34,7 @@ class Database : public QObject public: Database(); Group* rootGroup(); + const Group* rootGroup() const; void setRootGroup(Group* group); Metadata* metadata(); static QImage icon(int number); @@ -41,6 +42,9 @@ public: Entry* resolveEntry(const Uuid& uuid); Group* resolveGroup(const Uuid& uuid); +Q_SIGNALS: + void groupChanged(const Group* group); + private: Entry* recFindEntry(const Uuid& uuid, Group* group); Group* recFindGroup(const Uuid& uuid, Group* group); diff --git a/src/core/Group.cpp b/src/core/Group.cpp index 3a9561c82..611255c94 100644 --- a/src/core/Group.cpp +++ b/src/core/Group.cpp @@ -79,6 +79,8 @@ void Group::setUuid(const Uuid& uuid) void Group::setName(const QString& name) { m_name = name; + + Q_EMIT groupChanged(this); } void Group::setNotes(const QString& notes) @@ -92,6 +94,8 @@ void Group::setIcon(int iconNumber) m_iconNumber = iconNumber; m_customIcon = Uuid(); + + Q_EMIT groupChanged(this); } void Group::setIcon(const Uuid& uuid) @@ -100,6 +104,8 @@ void Group::setIcon(const Uuid& uuid) m_iconNumber = 0; m_customIcon = uuid; + + Q_EMIT groupChanged(this); } void Group::setTimeInfo(const TimeInfo& timeInfo) @@ -135,6 +141,7 @@ void Group::setParent(Group* parent) m_parent->m_children.removeAll(this); } else if (m_db) { + // parent was a Database m_db->setRootGroup(0); } @@ -173,6 +180,11 @@ const Database* Group::database() const return m_db; } +QList Group::children() +{ + return m_children; +} + QList Group::children() const { QList constChildren; @@ -183,9 +195,9 @@ QList Group::children() const return constChildren; } -QList Group::children() +QList Group::entries() { - return m_children; + return m_entries; } QList Group::entries() const @@ -198,11 +210,6 @@ QList Group::entries() const return constEntries; } -QList Group::entries() -{ - return m_entries; -} - void Group::addEntry(Entry *entry) { Q_ASSERT(entry != 0); @@ -217,6 +224,9 @@ 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*))); + m_db = db; Q_FOREACH (Group* group, m_children) { diff --git a/src/core/Group.h b/src/core/Group.h index 7140df4c5..eed28d5c6 100644 --- a/src/core/Group.h +++ b/src/core/Group.h @@ -56,13 +56,16 @@ public: void setParent(Database* db); const Database* database() const; - QList children() const; QList children(); - QList entries() const; + QList children() const; QList entries(); + QList entries() const; void addEntry(Entry* entry); void removeEntry(Entry* entry); +Q_SIGNALS: + void groupChanged(const Group* group); + private: void recSetDatabase(Database* db); diff --git a/src/core/Uuid.cpp b/src/core/Uuid.cpp index 5e94899e3..01d371c58 100644 --- a/src/core/Uuid.cpp +++ b/src/core/Uuid.cpp @@ -35,6 +35,7 @@ Uuid::Uuid(const QByteArray& data) Uuid Uuid::random() { QByteArray randomAray; // TODO fill with random data + randomAray.fill(1, 16); return Uuid(randomAray); } diff --git a/src/gui/GroupModel.cpp b/src/gui/GroupModel.cpp index 2092438bf..e0ffc3ff3 100644 --- a/src/gui/GroupModel.cpp +++ b/src/gui/GroupModel.cpp @@ -17,21 +17,19 @@ #include "GroupModel.h" +#include "core/Database.h" #include "core/Group.h" -GroupModel::GroupModel(const Group* rootGroup, QObject* parent) : QAbstractItemModel(parent) +GroupModel::GroupModel(const Database* db, QObject* parent) : QAbstractItemModel(parent) { - m_root = rootGroup; -} - -void GroupModel::setRootGroup(const Group* group) -{ - m_root = group; + m_root = db->rootGroup(); + connect(db, SIGNAL(groupChanged(const Group*)), SLOT(groupChanged(const Group*))); } int GroupModel::rowCount(const QModelIndex& parent) const { if (!parent.isValid()) { + // we have exactly 1 root item return 1; } else { @@ -75,11 +73,13 @@ QModelIndex GroupModel::parent(const QModelIndex& index) const const Group* parentGroup = childGroup->parentGroup(); if (!parentGroup) { + // index is already the root group return QModelIndex(); } else { const Group* grandParentGroup = parentGroup->parentGroup(); if (!grandParentGroup) { + // parent is the root group return createIndex(0, 0, parentGroup); } else { @@ -90,12 +90,21 @@ QModelIndex GroupModel::parent(const QModelIndex& index) const QVariant GroupModel::data(const QModelIndex& index, int role) const { - if (!index.isValid() || role != Qt::DisplayRole) { + if (!index.isValid()) { return QVariant(); } const Group* group = groupFromIndex(index); - return group->name(); + + if (role == Qt::DisplayRole) { + return group->name(); + } + else if (role == Qt::DecorationRole) { + return group->icon(); + } + else { + return QVariant(); + } } QVariant GroupModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -118,3 +127,18 @@ const Group* GroupModel::groupFromIndex(const QModelIndex& index) const return static_cast(index.internalPointer()); } + +void GroupModel::groupChanged(const Group* group) +{ + int row; + + if (!group->parentGroup()) { + row = 0; + } + else { + row = group->parentGroup()->children().indexOf(group); + } + + QModelIndex index = createIndex(row, 0, group); + Q_EMIT dataChanged(index, index); +} diff --git a/src/gui/GroupModel.h b/src/gui/GroupModel.h index 36be8aa0a..00b89d601 100644 --- a/src/gui/GroupModel.h +++ b/src/gui/GroupModel.h @@ -20,6 +20,7 @@ #include +class Database; class Group; class GroupModel : public QAbstractItemModel @@ -27,8 +28,7 @@ class GroupModel : public QAbstractItemModel Q_OBJECT public: - explicit GroupModel(const Group* rootGroup, QObject* parent = 0); - void setRootGroup(const Group* group); + explicit GroupModel(const Database* db, QObject* parent = 0); int rowCount(const QModelIndex& parent = QModelIndex()) const; int columnCount(const QModelIndex& parent = QModelIndex()) const; @@ -41,6 +41,10 @@ private: QModelIndex createIndex(int row, int column, const Group* group) const; const Group* groupFromIndex(const QModelIndex& index) const; +private Q_SLOTS: + void groupChanged(const Group* group); + +private: const Group* m_root; }; diff --git a/src/main.cpp b/src/main.cpp index 8fe9ce69c..f5d7c9ff1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ int main(int argc, char **argv) Parser* parser = new Parser(db); parser->parse(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml")); - GroupModel groupModel(db->rootGroup()); + GroupModel groupModel(db); QTreeView view; view.setModel(&groupModel); diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index 5a8800653..ab0fe55fc 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -15,6 +15,7 @@ * along with this program. If not, see . */ +#include #include #include "core/Database.h" @@ -65,6 +66,15 @@ void TestGroup::testParenting() QVERIFY(g1->children().at(0) == g2); QVERIFY(g1->children().at(1) == g3); QVERIFY(g3->children().contains(g4)); + + QSignalSpy spy(db, SIGNAL(groupChanged(const Group*))); + g2->setName("test"); + g4->setName("test"); + g3->setName("test"); + g1->setName("test"); + g3->setIcon(Uuid::random()); + g1->setIcon(2); + QVERIFY(spy.count() == 6); } QTEST_MAIN(TestGroup); diff --git a/tests/TestGroupModel.cpp b/tests/TestGroupModel.cpp index 41342d152..e9d193084 100644 --- a/tests/TestGroupModel.cpp +++ b/tests/TestGroupModel.cpp @@ -15,9 +15,11 @@ * along with this program. If not, see . */ +#include #include #include "modeltest.h" +#include "core/Database.h" #include "core/Group.h" #include "gui/GroupModel.h" @@ -31,7 +33,10 @@ private Q_SLOTS: void TestGroupModel::test() { + Database* db = new Database(); + Group* groupRoot = new Group(); + groupRoot->setParent(db); groupRoot->setName("groupRoot"); Group* group1 = new Group(); @@ -54,7 +59,7 @@ void TestGroupModel::test() group2->setName("group2"); group2->setParent(groupRoot); - GroupModel* model = new GroupModel(groupRoot, this); + GroupModel* model = new GroupModel(db, this); new ModelTest(model, this); @@ -72,7 +77,13 @@ void TestGroupModel::test() QVERIFY(model->data(index121) == "group121"); QVERIFY(model->data(index2) == "group2"); + QSignalSpy spy(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&))); + group11->setName("test"); + group121->setIcon(4); + QVERIFY(spy.count() == 2); + delete groupRoot; + delete db; } QTEST_MAIN(TestGroupModel);