source: trunk/src/gui/itemviews/qitemselectionmodel.cpp@ 138

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

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

File size: 54.4 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#include "qitemselectionmodel.h"
43#include <private/qitemselectionmodel_p.h>
44#include <qdebug.h>
45
46#ifndef QT_NO_ITEMVIEWS
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \class QItemSelectionRange
52
53 \brief The QItemSelectionRange class manages information about a
54 range of selected items in a model.
55
56 \ingroup model-view
57
58 A QItemSelectionRange contains information about a range of
59 selected items in a model. A range of items is a contiguous array
60 of model items, extending to cover a number of adjacent rows and
61 columns with a common parent item; this can be visualized as a
62 two-dimensional block of cells in a table. A selection range has a
63 top(), left() a bottom(), right() and a parent().
64
65 The QItemSelectionRange class is one of the \l{Model/View Classes}
66 and is part of Qt's \l{Model/View Programming}{model/view framework}.
67
68 The model items contained in the selection range can be obtained
69 using the indexes() function. Use QItemSelectionModel::selectedIndexes()
70 to get a list of all selected items for a view.
71
72 You can determine whether a given model item lies within a
73 particular range by using the contains() function. Ranges can also
74 be compared using the overloaded operators for equality and
75 inequality, and the intersects() function allows you to determine
76 whether two ranges overlap.
77
78 \sa {Model/View Programming}, QAbstractItemModel, QItemSelection,
79 QItemSelectionModel
80*/
81
82/*!
83 \fn QItemSelectionRange::QItemSelectionRange()
84
85 Constructs an empty selection range.
86*/
87
88/*!
89 \fn QItemSelectionRange::QItemSelectionRange(const QItemSelectionRange &other)
90
91 Copy constructor. Constructs a new selection range with the same contents
92 as the \a other range given.
93
94*/
95
96/*!
97 \fn QItemSelectionRange::QItemSelectionRange(const QModelIndex &topLeft, const QModelIndex &bottomRight)
98
99 Constructs a new selection range containing only the index specified
100 by the \a topLeft and the index \a bottomRight.
101
102*/
103
104/*!
105 \fn QItemSelectionRange::QItemSelectionRange(const QModelIndex &index)
106
107 Constructs a new selection range containing only the model item specified
108 by the model index \a index.
109*/
110
111/*!
112 \fn int QItemSelectionRange::top() const
113
114 Returns the row index corresponding to the uppermost selected row in the
115 selection range.
116
117*/
118
119/*!
120 \fn int QItemSelectionRange::left() const
121
122 Returns the column index corresponding to the leftmost selected column in the
123 selection range.
124*/
125
126/*!
127 \fn int QItemSelectionRange::bottom() const
128
129 Returns the row index corresponding to the lowermost selected row in the
130 selection range.
131
132*/
133
134/*!
135 \fn int QItemSelectionRange::right() const
136
137 Returns the column index corresponding to the rightmost selected column in
138 the selection range.
139
140*/
141
142/*!
143 \fn int QItemSelectionRange::width() const
144
145 Returns the number of selected columns in the selection range.
146
147*/
148
149/*!
150 \fn int QItemSelectionRange::height() const
151
152 Returns the number of selected rows in the selection range.
153
154*/
155
156/*!
157 \fn const QAbstractItemModel *QItemSelectionRange::model() const
158
159 Returns the model that the items in the selection range belong to.
160*/
161
162/*!
163 \fn QModelIndex QItemSelectionRange::topLeft() const
164
165 Returns the index for the item located at the top-left corner of
166 the selection range.
167
168 \sa top(), left(), bottomRight()
169*/
170
171/*!
172 \fn QModelIndex QItemSelectionRange::bottomRight() const
173
174 Returns the index for the item located at the bottom-right corner
175 of the selection range.
176
177 \sa bottom(), right(), topLeft()
178*/
179
180/*!
181 \fn QModelIndex QItemSelectionRange::parent() const
182
183 Returns the parent model item index of the items in the selection range.
184
185*/
186
187/*!
188 \fn bool QItemSelectionRange::contains(const QModelIndex &index) const
189
190 Returns true if the model item specified by the \a index lies within the
191 range of selected items; otherwise returns false.
192*/
193
194/*!
195 \fn bool QItemSelectionRange::contains(int row, int column,
196 const QModelIndex &parentIndex) const
197 \overload
198
199 Returns true if the model item specified by (\a row, \a column)
200 and with \a parentIndex as the parent item lies within the range
201 of selected items; otherwise returns false.
202*/
203
204/*!
205 \fn bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const
206
207 Returns true if this selection range intersects (overlaps with) the \a other
208 range given; otherwise returns false.
209
210*/
211bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const
212{
213 return (isValid() && other.isValid()
214 && parent() == other.parent()
215 && ((top() <= other.top() && bottom() >= other.top())
216 || (top() >= other.top() && top() <= other.bottom()))
217 && ((left() <= other.left() && right() >= other.left())
218 || (left() >= other.left() && left() <= other.right())));
219}
220
221/*!
222 \fn QItemSelectionRange QItemSelectionRange::intersect(const QItemSelectionRange &other) const
223 \obsolete
224
225 Use intersected(\a other) instead.
226*/
227
228/*!
229 \fn QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange &other) const
230 \since 4.2
231
232 Returns a new selection range containing only the items that are found in
233 both the selection range and the \a other selection range.
234*/
235
236QItemSelectionRange QItemSelectionRange::intersect(const QItemSelectionRange &other) const
237{
238 if (model() == other.model() && parent() == other.parent()) {
239 QModelIndex topLeft = model()->index(qMax(top(), other.top()),
240 qMax(left(), other.left()),
241 other.parent());
242 QModelIndex bottomRight = model()->index(qMin(bottom(), other.bottom()),
243 qMin(right(), other.right()),
244 other.parent());
245 return QItemSelectionRange(topLeft, bottomRight);
246 }
247 return QItemSelectionRange();
248}
249
250/*!
251 \fn bool QItemSelectionRange::operator==(const QItemSelectionRange &other) const
252
253 Returns true if the selection range is exactly the same as the \a other
254 range given; otherwise returns false.
255
256*/
257
258/*!
259 \fn bool QItemSelectionRange::operator!=(const QItemSelectionRange &other) const
260
261 Returns true if the selection range differs from the \a other range given;
262 otherwise returns false.
263