File indexing completed on 2024-04-28 16:21:24

0001 /* This file is part of the KDE project
0002    Copyright 2010 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KSPREAD_MODEL_SUPPORT
0021 #define KSPREAD_MODEL_SUPPORT
0022 
0023 #include <QAbstractItemModel>
0024 #include <QItemSelection>
0025 
0026 #include "Cell.h"
0027 #include "Region.h"
0028 #include "Sheet.h"
0029 
0030 namespace Calligra
0031 {
0032 namespace Sheets
0033 {
0034 
0035 /**
0036  * Item roles representing sheet data.
0037  * \ingroup Model
0038  */
0039 enum SheetDataRole {
0040     // Qt::UserRole = 32
0041     // Sheet properties; MapModel, MapViewModel
0042     VisibilityRole      = Qt::UserRole, ///< sheet visibility; bool
0043     ProtectionRole,                     ///< sheet protection; bool
0044     ActivityRole                        ///< active sheet; bool
0045 };
0046 
0047 
0048 /**
0049  * Item roles representing cell data.
0050  * \ingroup Model
0051  */
0052 enum CellDataRole {
0053     // Qt::UserRole     = 0x00000020 = 32
0054     NoCellDataRole      = Qt::UserRole, ///< used for iterating over all data, default and non-default
0055     // Cell contents; SheetModel, RegionModel
0056     UserInputRole       = 0x00000100,   ///< cell's user input; QString
0057     FormulaRole         = 0x00000200,   ///< cell's formula; Formula
0058     ValueRole           = 0x00000400,   ///< cell's value; Value
0059     LinkRole            = 0x00000800,   ///< cell's hyperlink; QString
0060     RichTextRole        = 0x00001000,   ///< cell's rich text; QSharedPointer<QTextDocument>
0061     // Cell range associations; SheetModel, RegionModel
0062     CommentRole         = 0x00002000,   ///< a comment; QString
0063     ConditionRole       = 0x00004000,   ///< a conditional style; Conditions
0064     StyleRole           = 0x00008000,   ///< a style; Style
0065     ValidityRole        = 0x00010000,   ///< a cell validition; Validity
0066     FusionedRangeRole   = 0x00020000,   ///< a fusioned cell range; bool
0067     LockedRangeRole     = 0x00040000,   ///< a locked cell range; bool
0068     NamedAreaRole       = 0x00080000,   ///< a named area; QString
0069     SourceRangeRole     = 0x00100000,   ///< a source cell range; Binding
0070     TargetRangeRole     = 0x00200000,   ///< a target cell range; Database
0071     AllCellDataRoles    = 0x00FFFF00    ///< used for iterating over the non-default data only
0072 };
0073 Q_DECLARE_FLAGS(CellDataRoles, CellDataRole)
0074 
0075 
0076 /**
0077  * Converts a model index into a Cell.
0078  * \ingroup Model
0079  */
0080 static inline Cell toCell(const QModelIndex &index)
0081 {
0082     const int column = index.column() + 1;
0083     const int row = index.row() + 1;
0084     Sheet *const sheet = static_cast<Sheet*>(index.internalPointer());
0085     return Cell(sheet, column, row);
0086 }
0087 
0088 /**
0089  * Converts a model index into cell coordinates.
0090  * \ingroup Model
0091  */
0092 static inline QPoint toPoint(const QModelIndex &index)
0093 {
0094     const int column = index.column() + 1;
0095     const int row = index.row() + 1;
0096     return QPoint(column, row);
0097 }
0098 
0099 /**
0100  * Converts an item range into a range in cell coordinates.
0101  * \ingroup Model
0102  */
0103 static inline QRect toRange(const QItemSelectionRange &range)
0104 {
0105     return QRect(range.left() + 1, range.top() + 1, range.width(), range.height());
0106 }
0107 
0108 /**
0109  * Converts an item selection into a cell region.
0110  * \ingroup Model
0111  */
0112 static inline Region toRegion(const QItemSelection &selection)
0113 {
0114     Region region;
0115     for (int i = 0; i < selection.count(); ++i) {
0116         const QItemSelectionRange range = selection[i];
0117         Sheet *const sheet = static_cast<Sheet*>(range.topLeft().internalPointer());
0118         region.add(toRange(range), sheet);
0119     }
0120     return region;
0121 }
0122 
0123 /**
0124  * Converts a range in cell coordinates into a model item range.
0125  * \ingroup Model
0126  */
0127 static inline QItemSelectionRange fromRange(const QRect &range, const QAbstractItemModel *const model)
0128 {
0129     const QModelIndex topLeft = model->index(range.top() - 1, range.left() - 1);
0130     const QModelIndex bottomRight = model->index(range.bottom() - 1, range.right() - 1);
0131     return QItemSelectionRange(topLeft, bottomRight);
0132 }
0133 
0134 /**
0135  * Converts a range in cell coordinates into a model item range.
0136  * \ingroup Model
0137  */
0138 static inline QItemSelectionRange fromRange(const QRect &range, Sheet *const sheet)
0139 {
0140     return fromRange(range, sheet->model());
0141 }
0142 
0143 /**
0144  * Converts a cell region into an item selection.
0145  * \ingroup Model
0146  */
0147 static inline QItemSelection fromRegion(const Region &region)
0148 {
0149     QItemSelection selection;
0150     for (Region::ConstIterator it = region.constBegin(), end = region.constEnd(); it != end; ++it) {
0151         selection.append(fromRange((*it)->rect(), (*it)->sheet()));
0152     }
0153     return selection;
0154 }
0155 
0156 } // namespace Sheets
0157 } // namespace Calligra
0158 
0159 #endif // KSPREAD_MODEL_SUPPORT