1/* This file is part of the KDE project
2 Copyright 2010 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3
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.
8
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.
13
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.
18*/
19
20#ifndef KSPREAD_MODEL_SUPPORT
21#define KSPREAD_MODEL_SUPPORT
22
23#include <QAbstractItemModel>
24#include <QItemSelection>
25
26#include "Cell.h"
27#include "Region.h"
28#include "Sheet.h"
29
30namespace Calligra
31{
32namespace Sheets
33{
34
35/**
36 * Item roles representing sheet data.
37 * \ingroup Model
38 */
39enum SheetDataRole {
40 // Qt::UserRole = 32
41 // Sheet properties; MapModel, MapViewModel
42 VisibilityRole = Qt::UserRole, ///< sheet visibility; bool
43 ProtectionRole, ///< sheet protection; bool
44 ActivityRole ///< active sheet; bool
45};
46
47
48/**
49 * Item roles representing cell data.
50 * \ingroup Model
51 */
52enum CellDataRole {
53 // Qt::UserRole = 0x00000020 = 32
54 NoCellDataRole = Qt::UserRole, ///< used for iterating over all data, default and non-default
55 // Cell contents; SheetModel, RegionModel
56 UserInputRole = 0x00000100, ///< cell's user input; QString
57 FormulaRole = 0x00000200, ///< cell's formula; Formula
58 ValueRole = 0x00000400, ///< cell's value; Value
59 LinkRole = 0x00000800, ///< cell's hyperlink; QString
60 RichTextRole = 0x00001000, ///< cell's rich text; QSharedPointer<QTextDocument>
61 // Cell range associations; SheetModel, RegionModel
62 CommentRole = 0x00002000, ///< a comment; QString
63 ConditionRole = 0x00004000, ///< a conditional style; Conditions
64 StyleRole = 0x00008000, ///< a style; Style
65 ValidityRole = 0x00010000, ///< a cell validition; Validity
66 FusionedRangeRole = 0x00020000, ///< a fusioned cell range; bool
67 LockedRangeRole = 0x00040000, ///< a locked cell range; bool
68 NamedAreaRole = 0x00080000, ///< a named area; QString
69 SourceRangeRole = 0x00100000, ///< a source cell range; Binding
70 TargetRangeRole = 0x00200000, ///< a target cell range; Database
71 AllCellDataRoles = 0x00FFFF00 ///< used for iterating over the non-default data only
72};
73Q_DECLARE_FLAGS(CellDataRoles, CellDataRole)
74
75
76/**
77 * Converts a model index into a Cell.
78 * \ingroup Model
79 */
80static inline Cell toCell(const QModelIndex &index)
81{
82 const int column = index.column() + 1;
83 const int row = index.row() + 1;
84 Sheet *const sheet = static_cast<Sheet*>(index.internalPointer());
85 return Cell(sheet, column, row);
86}
87
88/**
89 * Converts a model index into cell coordinates.
90 * \ingroup Model
91 */
92static inline QPoint toPoint(const QModelIndex &index)
93{
94 const int column = index.column() + 1;
95 const int row = index.row() + 1;
96 return QPoint(column, row);
97}
98
99/**
100 * Converts an item range into a range in cell coordinates.
101 * \ingroup Model
102 */
103static inline QRect toRange(const QItemSelectionRange &range)
104{
105 return QRect(range.left() + 1, range.top() + 1, range.width(), range.height());
106}
107
108/**
109 * Converts an item selection into a cell region.
110 * \ingroup Model
111 */
112static inline Region toRegion(const QItemSelection &selection)
113{
114 Region region;
115 for (int i = 0; i < selection.count(); ++i) {
116 const QItemSelectionRange range = selection[i];
117 Sheet *const sheet = static_cast<Sheet*>(range.topLeft().internalPointer());
118 region.add(toRange(range), sheet);
119 }
120 return region;
121}
122
123/**
124 * Converts a range in cell coordinates into a model item range.
125 * \ingroup Model
126 */
127static inline QItemSelectionRange fromRange(const QRect &range, const QAbstractItemModel *const model)
128{
129 const QModelIndex topLeft = model->index(range.top() - 1, range.left() - 1);
130 const QModelIndex bottomRight = model->index(range.bottom() - 1, range.right() - 1);
131 return QItemSelectionRange(topLeft, bottomRight);
132}
133
134/**
135 * Converts a range in cell coordinates into a model item range.
136 * \ingroup Model
137 */
138static inline QItemSelectionRange fromRange(const QRect &range, Sheet *const sheet)
139{
140 return fromRange(range, sheet->model());
141}
142
143/**
144 * Converts a cell region into an item selection.
145 * \ingroup Model
146 */
147static inline QItemSelection fromRegion(const Region &region)
148{
149 QItemSelection selection;
150 for (Region::ConstIterator it = region.constBegin(), end = region.constEnd(); it != end; ++it) {
151 selection.append(fromRange((*it)->rect(), (*it)->sheet()));
152 }
153 return selection;
154}
155
156} // namespace Sheets
157} // namespace Calligra
158
159#endif // KSPREAD_MODEL_SUPPORT
160