1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/* This file is part of the KDE project
Copyright 2009 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "MapModel.h"
#include "Map.h"
#include "ModelSupport.h"
#include "Sheet.h"
#include "SheetModel.h"
#include "commands/SheetCommands.h"
#include <KoIcon.h>
using namespace Calligra::Sheets;
class MapModel::Private
{
public:
Map* map;
public:
bool isSheetIndex(const QModelIndex& index, const MapModel* mapModel) const;
};
bool MapModel::Private::isSheetIndex(const QModelIndex& index, const MapModel* mapModel) const
{
if (!index.parent().isValid()) {
return false;
}
// If it is a cell, the parent's (the sheet's) model has to be this model.
if (index.parent().model() != mapModel || index.parent().internalPointer() != map) {
return false;
}
// If it is a cell, the parent (the sheet) has no parent.
if (index.parent().parent().isValid()) {
return false;
}
// Do not exceed the sheet list.
if (index.parent().row() >= map->count()) {
return false;
}
// The index' (the cell's) model has to match the sheet model.
if (index.model() != map->sheet(index.parent().row())->model()) {
return false;
}
return true;
}
MapModel::MapModel(Map* map)
: QAbstractListModel(map)
, d(new Private)
{
d->map = map;
connect(d->map, SIGNAL(sheetAdded(Sheet*)),
this, SLOT(addSheet(Sheet*)));
connect(d->map, SIGNAL(sheetRemoved(Sheet*)),
this, SLOT(removeSheet(Sheet*)));
}
MapModel::~MapModel()
{
delete d;
}
QVariant MapModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
// Propagation to sheet model
if (d->isSheetIndex(index, this)) {
return d->map->sheet(index.parent().row())->model()->data(index, role);
}
if (index.row() >= d->map->count()) {
return QVariant();
}
//
const Sheet* const sheet = d->map->sheet(index.row());
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return QVariant(sheet->sheetName());
case Qt::DecorationRole:
return QVariant(koIcon("x-office-spreadsheet"));
case VisibilityRole:
return QVariant(!sheet->isHidden());
case ProtectionRole:
return QVariant(sheet->isProtected());
default:
break;
}
return QVariant();
}
Qt::ItemFlags MapModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) {
return Qt::NoItemFlags;
}
// Propagation to sheet model
if (d->isSheetIndex(index, this)) {
return d->map->sheet(index.parent().row())->model()->flags(index);
}
if (index.row() >= d->map->count()) {
return Qt::NoItemFlags;
}
Qt::ItemFlags flags = Qt::ItemIsEnabled;
if (!d->map->isProtected()) {
flags |= Qt::ItemIsSelectable;
const Sheet* const sheet = d->map->sheet(index.row());
if (!sheet->isProtected()) {
flags |= Qt::ItemIsEditable;
}
}
return flags;
}
QVariant MapModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation)
if (section == 0 && role == Qt::DisplayRole) {
return QVariant(i18n("Sheet name"));
}
return QVariant();
}
QModelIndex MapModel::index(int row, int column, const QModelIndex &parent) const
{
QModelIndex index;
if (parent.isValid()) {
// If it is a cell, the parent's (the sheet's) model has to be this model.
if (parent.model() != this || parent.internalPointer() != d->map) {
return QModelIndex();
}
// If it is a cell, the parent (the sheet) has no parent.
if (parent.parent().isValid()) {
return QModelIndex();
}
// Do not exceed the sheet list.
if (parent.row() >= d->map->count()) {
return QModelIndex();
}
Sheet* const sheet = d->map->sheet(index.parent().row());
index = sheet->model()->index(row, column, parent);
} else {
index = createIndex(row, column, d->map);
}
return index;
}
int MapModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return d->map->count();
}
bool MapModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
// Propagation to sheet model
if (d->isSheetIndex(index, this)) {
return d->map->sheet(index.parent().row())->model()->setData(index, value, role);
}
if (index.isValid() && index.row() < d->map->count()) {
Sheet* const sheet(d->map->sheet(index.row()));
switch (role) {
case Qt::EditRole: {
const QString name(value.toString());
if (!name.isEmpty()) {
KUndo2Command* const command = new RenameSheetCommand(sheet, name);
emit addCommandRequested(command);
emit dataChanged(index, index);
return true;
}
break;
}
case VisibilityRole:
setHidden(sheet, value.toBool());
break;
case ProtectionRole:
break;
default:
break;
}
}
return false;
}
bool MapModel::setHidden(Sheet* sheet, bool hidden)
{
KUndo2Command* command;
if (hidden && !sheet->isHidden()) {
command = new HideSheetCommand(sheet);
} else if (!hidden && sheet->isHidden()) {
command = new ShowSheetCommand(sheet);
} else {
return false; // nothing to do
}
emit addCommandRequested(command);
return true;
}
Map* MapModel::map() const
{
return d->map;
}
void MapModel::addSheet(Sheet* sheet)
{
kDebug() << "Added sheet:" << sheet->sheetName();
emit layoutChanged();
}
void MapModel::removeSheet(Sheet *sheet)
{
kDebug() << "Removed sheet:" << sheet->sheetName();
emit layoutChanged();
}
#include "MapModel.moc"
|