2010-08-15 06:31:48 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2010-08-15 09:03:47 -04:00
|
|
|
#include "core/Database.h"
|
2010-08-15 06:31:48 -04:00
|
|
|
#include "core/Group.h"
|
|
|
|
|
2010-08-15 09:03:47 -04:00
|
|
|
GroupModel::GroupModel(const Database* db, QObject* parent) : QAbstractItemModel(parent)
|
2010-08-15 06:31:48 -04:00
|
|
|
{
|
2010-08-15 09:03:47 -04:00
|
|
|
m_root = db->rootGroup();
|
2010-08-18 04:27:40 -04:00
|
|
|
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()));
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int GroupModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (!parent.isValid()) {
|
2010-08-15 09:03:47 -04:00
|
|
|
// we have exactly 1 root item
|
2010-08-15 06:31:48 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-08-18 04:27:40 -04:00
|
|
|
return parent(groupFromIndex(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex GroupModel::parent(const Group* group) const
|
|
|
|
{
|
|
|
|
const Group* parentGroup = group->parentGroup();
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
if (!parentGroup) {
|
2010-08-15 09:03:47 -04:00
|
|
|
// index is already the root group
|
2010-08-15 06:31:48 -04:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const Group* grandParentGroup = parentGroup->parentGroup();
|
|
|
|
if (!grandParentGroup) {
|
2010-08-15 09:03:47 -04:00
|
|
|
// parent is the root group
|
2010-08-15 06:31:48 -04:00
|
|
|
return createIndex(0, 0, parentGroup);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return createIndex(grandParentGroup->children().indexOf(parentGroup), 0, parentGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GroupModel::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
2010-08-15 09:03:47 -04:00
|
|
|
if (!index.isValid()) {
|
2010-08-15 06:31:48 -04:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Group* group = groupFromIndex(index);
|
2010-08-15 09:03:47 -04:00
|
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
return group->name();
|
|
|
|
}
|
|
|
|
else if (role == Qt::DecorationRole) {
|
|
|
|
return group->icon();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GroupModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(section);
|
|
|
|
Q_UNUSED(orientation);
|
|
|
|
Q_UNUSED(role);
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2010-08-22 10:02:44 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-08-15 06:31:48 -04:00
|
|
|
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());
|
|
|
|
}
|
2010-08-15 09:03:47 -04:00
|
|
|
|
2010-08-18 04:27:40 -04:00
|
|
|
void GroupModel::groupDataChanged(const Group* group)
|
2010-08-15 09:03:47 -04:00
|
|
|
{
|
2010-08-22 10:02:44 -04:00
|
|
|
QModelIndex ix = index(group);
|
|
|
|
Q_EMIT dataChanged(ix, ix);
|
2010-08-15 09:03:47 -04:00
|
|
|
}
|
2010-08-18 04:27:40 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|