~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to kspread/ModelSupport.h

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
30
namespace KSpread
 
31
{
 
32
 
 
33
/**
 
34
 * Item roles representing sheet data.
 
35
 * \ingroup Model
 
36
 */
 
37
enum SheetDataRole {
 
38
    // Qt::UserRole = 32
 
39
    // Sheet properties; MapModel, MapViewModel
 
40
    VisibilityRole      = Qt::UserRole, ///< sheet visibility; bool
 
41
    ProtectionRole,                     ///< sheet protection; bool
 
42
    ActivityRole                        ///< active sheet; bool
 
43
};
 
44
 
 
45
 
 
46
/**
 
47
 * Item roles representing cell data.
 
48
 * \ingroup Model
 
49
 */
 
50
enum CellDataRole {
 
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
 
70
};
 
71
Q_DECLARE_FLAGS(CellDataRoles, CellDataRole)
 
72
 
 
73
 
 
74
/**
 
75
 * Converts a model index into a Cell.
 
76
 * \ingroup Model
 
77
 */
 
78
static inline Cell toCell(const QModelIndex &index)
 
79
{
 
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);
 
84
}
 
85
 
 
86
/**
 
87
 * Converts a model index into cell coordinates.
 
88
 * \ingroup Model
 
89
 */
 
90
static inline QPoint toPoint(const QModelIndex &index)
 
91
{
 
92
    const int column = index.column() + 1;
 
93
    const int row = index.row() + 1;
 
94
    return QPoint(column, row);
 
95
}
 
96
 
 
97
/**
 
98
 * Converts an item range into a range in cell coordinates.
 
99
 * \ingroup Model
 
100
 */
 
101
static inline QRect toRange(const QItemSelectionRange &range)
 
102
{
 
103
    return QRect(range.left() + 1, range.top() + 1, range.width(), range.height());
 
104
}
 
105
 
 
106
/**
 
107
 * Converts an item selection into a cell region.
 
108
 * \ingroup Model
 
109
 */
 
110
static inline Region toRegion(const QItemSelection &selection)
 
111
{
 
112
    Region region;
 
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);
 
117
    }
 
118
    return region;
 
119
}
 
120
 
 
121
/**
 
122
 * Converts a range in cell coordinates into a model item range.
 
123
 * \ingroup Model
 
124
 */
 
125
static inline QItemSelectionRange fromRange(const QRect &range, const QAbstractItemModel *const model)
 
126
{
 
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);
 
130
}
 
131
 
 
132
/**
 
133
 * Converts a range in cell coordinates into a model item range.
 
134
 * \ingroup Model
 
135
 */
 
136
static inline QItemSelectionRange fromRange(const QRect &range, Sheet *const sheet)
 
137
{
 
138
    return fromRange(range, sheet->model());
 
139
}
 
140
 
 
141
/**
 
142
 * Converts a cell region into an item selection.
 
143
 * \ingroup Model
 
144
 */
 
145
static inline QItemSelection fromRegion(const Region &region)
 
146
{
 
147
    QItemSelection selection;
 
148
    for (Region::ConstIterator it = region.constBegin(), end = region.constEnd(); it != end; ++it) {
 
149
        selection.append(fromRange((*it)->rect(), (*it)->sheet()));
 
150
    }
 
151
    return selection;
 
152
}
 
153
 
 
154
} // namespace KSpread
 
155
 
 
156
#endif // KSPREAD_MODEL_SUPPORT