Add GroupView.

This commit is contained in:
Felix Geyer 2010-08-22 16:02:44 +02:00
parent 707cf1bbe8
commit 97ca81f316
5 changed files with 116 additions and 12 deletions

View File

@ -29,6 +29,7 @@ set(keepassx_SOURCES
core/Writer.cpp core/Writer.cpp
gui/EntryModel.cpp gui/EntryModel.cpp
gui/GroupModel.cpp gui/GroupModel.cpp
gui/GroupView.cpp
) )
automoc4_add_library( keepassx_core STATIC ${keepassx_SOURCES} ) automoc4_add_library( keepassx_core STATIC ${keepassx_SOURCES} )

View File

@ -124,6 +124,20 @@ QVariant GroupModel::headerData(int section, Qt::Orientation orientation, int ro
return QVariant(); return QVariant();
} }
QModelIndex GroupModel::index(const Group* group) const
{
int row;
if (!group->parentGroup()) {
row = 0;
}
else {
row = group->parentGroup()->children().indexOf(group);
}
return createIndex(row, 0, group);
}
QModelIndex GroupModel::createIndex(int row, int column, const Group* group) const QModelIndex GroupModel::createIndex(int row, int column, const Group* group) const
{ {
return QAbstractItemModel::createIndex(row, column, const_cast<Group*>(group)); return QAbstractItemModel::createIndex(row, column, const_cast<Group*>(group));
@ -138,17 +152,8 @@ const Group* GroupModel::groupFromIndex(const QModelIndex& index) const
void GroupModel::groupDataChanged(const Group* group) void GroupModel::groupDataChanged(const Group* group)
{ {
int row; QModelIndex ix = index(group);
Q_EMIT dataChanged(ix, ix);
if (!group->parentGroup()) {
row = 0;
}
else {
row = group->parentGroup()->children().indexOf(group);
}
QModelIndex index = createIndex(row, 0, group);
Q_EMIT dataChanged(index, index);
} }
void GroupModel::groupAboutToRemove(const Group* group) void GroupModel::groupAboutToRemove(const Group* group)

View File

@ -29,6 +29,8 @@ class GroupModel : public QAbstractItemModel
public: public:
explicit GroupModel(const Database* db, QObject* parent = 0); explicit GroupModel(const Database* db, QObject* parent = 0);
QModelIndex index(const Group* group) const;
const Group* groupFromIndex(const QModelIndex& index) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const; int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const; int columnCount(const QModelIndex& parent = QModelIndex()) const;
@ -39,7 +41,6 @@ public:
private: private:
QModelIndex createIndex(int row, int column, const Group* group) const; QModelIndex createIndex(int row, int column, const Group* group) const;
const Group* groupFromIndex(const QModelIndex& index) const;
QModelIndex parent(const Group* group) const; QModelIndex parent(const Group* group) const;
private Q_SLOTS: private Q_SLOTS:

52
src/gui/GroupView.cpp Normal file
View File

@ -0,0 +1,52 @@
/*
* 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 "GroupView.h"
#include "core/Database.h"
#include "core/Group.h"
#include "gui/GroupModel.h"
GroupView::GroupView(Database* db, QWidget* parent) : QTreeView(parent)
{
model = new GroupModel(db, this);
QTreeView::setModel(model);
recInitExpanded(db->rootGroup());
setHeaderHidden(true);
}
void GroupView::expandedChanged(const QModelIndex& index)
{
Group* group = const_cast<Group*>(model->groupFromIndex(index));
group->setExpanded(isExpanded(index));
}
void GroupView::recInitExpanded(const Group* group)
{
QModelIndex index = model->index(group);
setExpanded(index, group->isExpanded());
Q_FOREACH (const Group* child, group->children()) {
recInitExpanded(child);
}
}
void GroupView::setModel(QAbstractItemModel* model)
{
Q_UNUSED(model);
Q_ASSERT(false);
}

45
src/gui/GroupView.h Normal file
View File

@ -0,0 +1,45 @@
/*
* 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_GROUPVIEW_H
#define KEEPASSX_GROUPVIEW_H
#include <QtGui/QTreeView>
class Database;
class Group;
class GroupModel;
class GroupView : public QTreeView
{
Q_OBJECT
public:
GroupView(Database* db, QWidget* parent = 0);
void setModel(QAbstractItemModel* model);
private Q_SLOTS:
void expandedChanged(const QModelIndex& index);
private:
void recInitExpanded(const Group* group);
Database* db;
GroupModel* model;
};
#endif // KEEPASSX_GROUPVIEW_H