1
/* This file is part of the KDE project
2
Copyright 2010 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#ifndef KSPREAD_MODEL_SUPPORT
21
#define KSPREAD_MODEL_SUPPORT
23
#include <QAbstractItemModel>
24
#include <QItemSelection>
34
* Item roles representing sheet data.
39
// Sheet properties; MapModel, MapViewModel
40
VisibilityRole = Qt::UserRole, ///< sheet visibility; bool
41
ProtectionRole, ///< sheet protection; bool
42
ActivityRole ///< active sheet; bool
47
* Item roles representing cell data.
51
// Qt::UserRole = 0x00000020 = 32
52
NoCellDataRole = Qt::UserRole, ///< used for iterating over all data, default and non-default
53
// Cell contents; SheetModel, RegionModel
54
UserInputRole = 0x00000100, ///< cell's user input; QString
55
FormulaRole = 0x00000200, ///< cell's formula; Formula
56
ValueRole = 0x00000400, ///< cell's value; Value
57
LinkRole = 0x00000800, ///< cell's hyperlink; QString
58
RichTextRole = 0x00001000, ///< cell's rich text; QSharedPointer<QTextDocument>
59
// Cell range associations; SheetModel, RegionModel
60
CommentRole = 0x00002000, ///< a comment; QString
61
ConditionRole = 0x00004000, ///< a conditional style; Conditions
62
StyleRole = 0x00008000, ///< a style; Style
63
ValidityRole = 0x00010000, ///< a cell validition; Validity
64
FusionedRangeRole = 0x00020000, ///< a fusioned cell range; bool
65
LockedRangeRole = 0x00040000, ///< a locked cell range; bool
66
NamedAreaRole = 0x00080000, ///< a named area; QString
67
SourceRangeRole = 0x00100000, ///< a source cell range; Binding
68
TargetRangeRole = 0x00200000, ///< a target cell range; Database
69
AllCellDataRoles = 0x00FFFF00 ///< used for iterating over the non-default data only
71
Q_DECLARE_FLAGS(CellDataRoles, CellDataRole)
75
* Converts a model index into a Cell.
78
static inline Cell toCell(const QModelIndex &index)
80
const int column = index.column() + 1;
81
const int row = index.row() + 1;
82
Sheet *const sheet = static_cast<Sheet*>(index.internalPointer());
83
return Cell(sheet, column, row);
87
* Converts a model index into cell coordinates.
90
static inline QPoint toPoint(const QModelIndex &index)
92
const int column = index.column() + 1;
93
const int row = index.row() + 1;
94
return QPoint(column, row);
98
* Converts an item range into a range in cell coordinates.
101
static inline QRect toRange(const QItemSelectionRange &range)
103
return QRect(range.left() + 1, range.top() + 1, range.width(), range.height());
107
* Converts an item selection into a cell region.
110
static inline Region toRegion(const QItemSelection &selection)
113
for (int i = 0; i < selection.count(); ++i) {
114
const QItemSelectionRange range = selection[i];
115
Sheet *const sheet = static_cast<Sheet*>(range.topLeft().internalPointer());
116
region.add(toRange(range), sheet);
122
* Converts a range in cell coordinates into a model item range.
125
static inline QItemSelectionRange fromRange(const QRect &range, const QAbstractItemModel *const model)
127
const QModelIndex topLeft = model->index(range.top() - 1, range.left() - 1);
128
const QModelIndex bottomRight = model->index(range.bottom() - 1, range.right() - 1);
129
return QItemSelectionRange(topLeft, bottomRight);
133
* Converts a range in cell coordinates into a model item range.
136
static inline QItemSelectionRange fromRange(const QRect &range, Sheet *const sheet)
138
return fromRange(range, sheet->model());
142
* Converts a cell region into an item selection.
145
static inline QItemSelection fromRegion(const Region ®ion)
147
QItemSelection selection;
148
for (Region::ConstIterator it = region.constBegin(), end = region.constEnd(); it != end; ++it) {
149
selection.append(fromRange((*it)->rect(), (*it)->sheet()));
154
} // namespace KSpread
156
#endif // KSPREAD_MODEL_SUPPORT