source: trunk/src/gui/itemviews/qitemselectionmodel.h@ 112

Last change on this file since 112 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QITEMSELECTIONMODEL_H
43#define QITEMSELECTIONMODEL_H
44
45#include <QtCore/qset.h>
46#include <QtCore/qvector.h>
47#include <QtCore/qlist.h>
48#include <QtCore/qabstractitemmodel.h>
49
50QT_BEGIN_HEADER
51
52QT_BEGIN_NAMESPACE
53
54QT_MODULE(Gui)
55
56#ifndef QT_NO_ITEMVIEWS
57
58class Q_GUI_EXPORT QItemSelectionRange
59{
60
61public:
62 inline QItemSelectionRange() {}
63 inline QItemSelectionRange(const QItemSelectionRange &other)
64 : tl(other.tl), br(other.br) {}
65 inline QItemSelectionRange(const QModelIndex &topLeft, const QModelIndex &bottomRight);
66 explicit inline QItemSelectionRange(const QModelIndex &index)
67 { tl = index; br = tl; }
68
69 inline int top() const { return tl.row(); }
70 inline int left() const { return tl.column(); }
71 inline int bottom() const { return br.row(); }
72 inline int right() const { return br.column(); }
73 inline int width() const { return br.column() - tl.column() + 1; }
74 inline int height() const { return br.row() - tl.row() + 1; }
75
76 inline QModelIndex topLeft() const { return QModelIndex(tl); }
77 inline QModelIndex bottomRight() const { return QModelIndex(br); }
78 inline QModelIndex parent() const { return tl.parent(); }
79 inline const QAbstractItemModel *model() const { return tl.model(); }
80
81 inline bool contains(const QModelIndex &index) const
82 {
83 return (parent() == index.parent()
84 && tl.row() <= index.row() && tl.column() <= index.column()
85 && br.row() >= index.row() && br.column() >= index.column());
86 }
87
88 inline bool contains(int row, int column, const QModelIndex &parentIndex) const
89 {
90 return (parent() == parentIndex
91 && tl.row() <= row && tl.column() <= column
92 && br.row() >= row && br.column() >= column);
93 }
94
95 bool intersects(const QItemSelectionRange &other) const;
96 QItemSelectionRange intersect(const QItemSelectionRange &other) const; // ### Qt 5: make QT4_SUPPORT
97 inline QItemSelectionRange intersected(const QItemSelectionRange &other) const
98 { return intersect(other); }
99
100 inline bool operator==(const QItemSelectionRange &other) const
101 { return (tl == other.tl && br == other.br); }
102 inline bool operator!=(const QItemSelectionRange &other) const
103 { return !operator==(other); }
104
105 inline bool isValid() const
106 {
107 return (tl.isValid() && br.isValid() && tl.parent() == br.parent()
108 && top() <= bottom() && left() <= right());
109 }
110
111 QModelIndexList indexes() const;
112
113private:
114 QPersistentModelIndex tl, br;
115};
116Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_MOVABLE_TYPE);
117
118inline QItemSelectionRange::QItemSelectionRange(const QModelIndex &atopLeft,
119 const QModelIndex &abottomRight)
120{ tl = atopLeft; br = abottomRight; }
121
122class QItemSelection;
123class QItemSelectionModelPrivate;
124
125class Q_GUI_EXPORT QItemSelectionModel : public QObject
126{
127 Q_OBJECT
128 Q_DECLARE_PRIVATE(QItemSelectionModel)
129 Q_FLAGS(SelectionFlags)
130
131public:
132
133 enum SelectionFlag {
134 NoUpdate = 0x0000,
135 Clear = 0x0001,
136 Select = 0x0002,
137 Deselect = 0x0004,
138 Toggle = 0x0008,
139 Current = 0x0010,
140 Rows = 0x0020,
141 Columns = 0x0040,
142 SelectCurrent = Select | Current,
143 ToggleCurrent = Toggle | Current,
144 ClearAndSelect = Clear | Select
145 };
146
147 Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag)
148
149 explicit QItemSelectionModel(QAbstractItemModel *model);
150 explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent);
151 virtual ~QItemSelectionModel();
152
153 QModelIndex currentIndex() const;
154
155 bool isSelected(const QModelIndex &index) const;
156 bool isRowSelected(int row, const QModelIndex &parent) const;
157 bool isColumnSelected(int column, const QModelIndex &parent) const;
158
159 bool rowIntersectsSelection(int row, const QModelIndex &parent) const;
160 bool columnIntersectsSelection(int column, const QModelIndex &parent) const;
161
162 bool hasSelection() const;
163
164 QModelIndexList selectedIndexes() const;
165 QModelIndexList selectedRows(int column = 0) const;
166 QModelIndexList selectedColumns(int row = 0) const;
167 const QItemSelection selection() const;
168
169 const QAbstractItemModel *model() const;
170
171public Q_SLOTS:
172 void setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
173 virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
174 virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
175 virtual void clear();
176 virtual void reset();
177
178 void clearSelection();
179
180Q_SIGNALS:
181 void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
182 void currentChanged(const QModelIndex &current, const QModelIndex &previous);
183 void currentRowChanged(const QModelIndex &current, const QModelIndex &previous);
184 void currentColumnChanged(const QModelIndex &current, const QModelIndex &previous);
185
186protected:
187 QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model);
188 void emitSelectionChanged(const QItemSelection &newSelection, const QItemSelection &oldSelection);
189
190private:
191 Q_DISABLE_COPY(QItemSelectionModel)
192 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeRemoved(const QModelIndex&, int, int))
193 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex&, int, int))
194 Q_PRIVATE_SLOT(d_func(), void _q_columnsAboutToBeInserted(const QModelIndex&, int, int))
195 Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int))
196 Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged())
197 Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged())
198};
199
200Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags)
201
202// dummy implentation of qHash() necessary for instantiating QList<QItemSelectionRange>::toSet() with MSVC
203inline uint qHash(const QItemSelectionRange &) { return 0; }
204
205class Q_GUI_EXPORT QItemSelection : public QList<QItemSelectionRange>
206{
207public:
208 QItemSelection() {}
209 QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight);
210 void select(const QModelIndex &topLeft, const QModelIndex &bottomRight);
211 bool contains(const QModelIndex &index) const;
212 QModelIndexList indexes() const;
213 void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command);
214 static void split(const QItemSelectionRange &range,
215 const QItemSelectionRange &other,
216 QItemSelection *result);
217};
218
219#ifndef QT_NO_DEBUG_STREAM
220Q_GUI_EXPORT QDebug operator<<(QDebug, const QItemSelectionRange &);
221#endif
222
223#endif // QT_NO_ITEMVIEWS
224
225QT_END_NAMESPACE
226
227QT_END_HEADER
228
229#endif // QITEMSELECTIONMODEL_H
Note: See TracBrowser for help on using the repository browser.