Add GroupModel and corresponding unit test.

This commit is contained in:
Felix Geyer
2010-08-15 12:31:48 +02:00
parent 9b0ba46b31
commit 072a8ccf1b
12 changed files with 922 additions and 8 deletions

View File

@@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
configure_file( config-keepassx.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-keepassx.h )
@@ -26,6 +26,7 @@ set(keepassx_SOURCES
core/Parser.cpp
core/TimeInfo.cpp
core/Uuid.cpp
gui/GroupModel.cpp
)
automoc4_add_library( keepassx_core STATIC ${keepassx_SOURCES} )

View File

@@ -35,7 +35,7 @@ Group* Database::rootGroup()
void Database::setRootGroup(Group* group)
{
Q_ASSERT(group == 0 || group->parent() == this);
Q_ASSERT(group == 0 || group->database() == this);
m_rootGroup = group;
}

View File

@@ -122,6 +122,11 @@ void Group::setLastTopVisibleEntry(Entry* entry)
m_lastTopVisibleEntry = entry;
}
const Group* Group::parentGroup() const
{
return m_parent;
}
void Group::setParent(Group* parent)
{
Q_ASSERT(parent != 0);
@@ -168,12 +173,32 @@ const Database* Group::database() const
return m_db;
}
QList<Group*> Group::children() const
QList<const Group*> Group::children() const
{
QList<const Group*> constChildren;
Q_FOREACH (Group* group, m_children) {
constChildren << group;
}
return constChildren;
}
QList<Group*> Group::children()
{
return m_children;
}
QList<Entry*> Group::entries() const
QList<const Entry*> Group::entries() const
{
QList<const Entry*> constEntries;
Q_FOREACH (Entry* entry, m_entries) {
constEntries << entry;
}
return constEntries;
}
QList<Entry*> Group::entries()
{
return m_entries;
}

View File

@@ -51,12 +51,15 @@ public:
void setDefaultAutoTypeSequence(const QString& sequence);
void setLastTopVisibleEntry(Entry* entry);
const Group* parentGroup() const;
void setParent(Group* parent);
void setParent(Database* db);
const Database* database() const;
QList<Group*> children() const;
QList<Entry*> entries() const;
QList<const Group*> children() const;
QList<Group*> children();
QList<const Entry*> entries() const;
QList<Entry*> entries();
void addEntry(Entry* entry);
void removeEntry(Entry* entry);

120
src/gui/GroupModel.cpp Normal file
View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "GroupModel.h"
#include "core/Group.h"
GroupModel::GroupModel(const Group* rootGroup, QObject* parent) : QAbstractItemModel(parent)
{
m_root = rootGroup;
}
void GroupModel::setRootGroup(const Group* group)
{
m_root = group;
}
int GroupModel::rowCount(const QModelIndex& parent) const
{
if (!parent.isValid()) {
return 1;
}
else {
const Group* group = groupFromIndex(parent);
return group->children().size();
}
}
int GroupModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 1;
}
QModelIndex GroupModel::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
const Group* group;
if (!parent.isValid()) {
group = m_root;
}
else {
group = groupFromIndex(parent)->children().at(row);
}
return createIndex(row, column, group);
}
QModelIndex GroupModel::parent(const QModelIndex& index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const Group* childGroup = groupFromIndex(index);
const Group* parentGroup = childGroup->parentGroup();
if (!parentGroup) {
return QModelIndex();
}
else {
const Group* grandParentGroup = parentGroup->parentGroup();
if (!grandParentGroup) {
return createIndex(0, 0, parentGroup);
}
else {
return createIndex(grandParentGroup->children().indexOf(parentGroup), 0, parentGroup);
}
}
}
QVariant GroupModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole) {
return QVariant();
}
const Group* group = groupFromIndex(index);
return group->name();
}
QVariant GroupModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(section);
Q_UNUSED(orientation);
Q_UNUSED(role);
return QVariant();
}
QModelIndex GroupModel::createIndex(int row, int column, const Group* group) const
{
return QAbstractItemModel::createIndex(row, column, const_cast<Group*>(group));
}
const Group* GroupModel::groupFromIndex(const QModelIndex& index) const
{
Q_ASSERT(index.internalPointer());
return static_cast<const Group*>(index.internalPointer());
}

47
src/gui/GroupModel.h Normal file
View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_GROUPMODEL_H
#define KEEPASSX_GROUPMODEL_H
#include <QtCore/QAbstractItemModel>
class Group;
class GroupModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit GroupModel(const Group* rootGroup, QObject* parent = 0);
void setRootGroup(const Group* group);
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex& index) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
private:
QModelIndex createIndex(int row, int column, const Group* group) const;
const Group* groupFromIndex(const QModelIndex& index) const;
const Group* m_root;
};
#endif // KEEPASSX_GROUPMODEL_H

View File

@@ -15,9 +15,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtGui/QApplication>
#include <QtGui/QTreeView>
#include "core/Database.h"
#include "core/Parser.h"
#include "gui/GroupModel.h"
#include "../tests/config-keepassx-tests.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Database* db= new Database();
Parser* parser = new Parser(db);
parser->parse(QString(KEEPASSX_TEST_DIR).append("/NewDatabase.xml"));
GroupModel groupModel(db->rootGroup());
QTreeView view;
view.setModel(&groupModel);
view.setHeaderHidden(true);
view.expandAll();
view.show();
return app.exec();
}