Add signal for changes in Group.

This commit is contained in:
Felix Geyer 2010-08-15 15:03:47 +02:00
parent bfb75da123
commit e28ed4891b
10 changed files with 94 additions and 22 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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*> Group::children()
{
return m_children;
}
QList<const Group*> Group::children() const
{
QList<const Group*> constChildren;
@ -183,9 +195,9 @@ QList<const Group*> Group::children() const
return constChildren;
}
QList<Group*> Group::children()
QList<Entry*> Group::entries()
{
return m_children;
return m_entries;
}
QList<const Entry*> Group::entries() const
@ -198,11 +210,6 @@ QList<const Entry*> Group::entries() const
return constEntries;
}
QList<Entry*> 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) {

View File

@ -56,13 +56,16 @@ public:
void setParent(Database* db);
const Database* database() const;
QList<const Group*> children() const;
QList<Group*> children();
QList<const Entry*> entries() const;
QList<const Group*> children() const;
QList<Entry*> entries();
QList<const Entry*> entries() const;
void addEntry(Entry* entry);
void removeEntry(Entry* entry);
Q_SIGNALS:
void groupChanged(const Group* group);
private:
void recSetDatabase(Database* db);

View File

@ -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);
}

View File

@ -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);
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<const Group*>(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);
}

View File

@ -20,6 +20,7 @@
#include <QtCore/QAbstractItemModel>
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;
};

View File

@ -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);

View File

@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtTest/QSignalSpy>
#include <QtTest/QTest>
#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);

View File

@ -15,9 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtTest/QSignalSpy>
#include <QtTest/QTest>
#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);