Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/itemmodels/qitemselectionmodel.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qitemselectionmodel.h" | - | ||||||||||||
41 | #include <private/qitemselectionmodel_p.h> | - | ||||||||||||
42 | #include <qdebug.h> | - | ||||||||||||
43 | - | |||||||||||||
44 | #include <algorithm> | - | ||||||||||||
45 | - | |||||||||||||
46 | #ifndef QT_NO_ITEMVIEWS | - | ||||||||||||
47 | - | |||||||||||||
48 | QT_BEGIN_NAMESPACE | - | ||||||||||||
49 | - | |||||||||||||
50 | /*! | - | ||||||||||||
51 | \class QItemSelectionRange | - | ||||||||||||
52 | \inmodule QtCore | - | ||||||||||||
53 | - | |||||||||||||
54 | \brief The QItemSelectionRange class manages information about a | - | ||||||||||||
55 | range of selected items in a model. | - | ||||||||||||
56 | - | |||||||||||||
57 | \ingroup model-view | - | ||||||||||||
58 | - | |||||||||||||
59 | A QItemSelectionRange contains information about a range of | - | ||||||||||||
60 | selected items in a model. A range of items is a contiguous array | - | ||||||||||||
61 | of model items, extending to cover a number of adjacent rows and | - | ||||||||||||
62 | columns with a common parent item; this can be visualized as a | - | ||||||||||||
63 | two-dimensional block of cells in a table. A selection range has a | - | ||||||||||||
64 | top(), left() a bottom(), right() and a parent(). | - | ||||||||||||
65 | - | |||||||||||||
66 | The QItemSelectionRange class is one of the \l{Model/View Classes} | - | ||||||||||||
67 | and is part of Qt's \l{Model/View Programming}{model/view framework}. | - | ||||||||||||
68 | - | |||||||||||||
69 | The model items contained in the selection range can be obtained | - | ||||||||||||
70 | using the indexes() function. Use QItemSelectionModel::selectedIndexes() | - | ||||||||||||
71 | to get a list of all selected items for a view. | - | ||||||||||||
72 | - | |||||||||||||
73 | You can determine whether a given model item lies within a | - | ||||||||||||
74 | particular range by using the contains() function. Ranges can also | - | ||||||||||||
75 | be compared using the overloaded operators for equality and | - | ||||||||||||
76 | inequality, and the intersects() function allows you to determine | - | ||||||||||||
77 | whether two ranges overlap. | - | ||||||||||||
78 | - | |||||||||||||
79 | \sa {Model/View Programming}, QAbstractItemModel, QItemSelection, | - | ||||||||||||
80 | QItemSelectionModel | - | ||||||||||||
81 | */ | - | ||||||||||||
82 | - | |||||||||||||
83 | /*! | - | ||||||||||||
84 | \fn QItemSelectionRange::QItemSelectionRange() | - | ||||||||||||
85 | - | |||||||||||||
86 | Constructs an empty selection range. | - | ||||||||||||
87 | */ | - | ||||||||||||
88 | - | |||||||||||||
89 | /*! | - | ||||||||||||
90 | \fn QItemSelectionRange::QItemSelectionRange(const QItemSelectionRange &other) | - | ||||||||||||
91 | - | |||||||||||||
92 | Copy constructor. Constructs a new selection range with the same contents | - | ||||||||||||
93 | as the \a other range given. | - | ||||||||||||
94 | - | |||||||||||||
95 | */ | - | ||||||||||||
96 | - | |||||||||||||
97 | /*! | - | ||||||||||||
98 | \fn QItemSelectionRange::QItemSelectionRange(const QModelIndex &topLeft, const QModelIndex &bottomRight) | - | ||||||||||||
99 | - | |||||||||||||
100 | Constructs a new selection range containing only the index specified | - | ||||||||||||
101 | by the \a topLeft and the index \a bottomRight. | - | ||||||||||||
102 | - | |||||||||||||
103 | */ | - | ||||||||||||
104 | - | |||||||||||||
105 | /*! | - | ||||||||||||
106 | \fn QItemSelectionRange::QItemSelectionRange(const QModelIndex &index) | - | ||||||||||||
107 | - | |||||||||||||
108 | Constructs a new selection range containing only the model item specified | - | ||||||||||||
109 | by the model index \a index. | - | ||||||||||||
110 | */ | - | ||||||||||||
111 | - | |||||||||||||
112 | /*! | - | ||||||||||||
113 | \fn QItemSelectionRange::swap(QItemSelectionRange &other) | - | ||||||||||||
114 | \since 5.6 | - | ||||||||||||
115 | - | |||||||||||||
116 | Swaps this selection range's contents with \a other. | - | ||||||||||||
117 | This function is very fast and never fails. | - | ||||||||||||
118 | */ | - | ||||||||||||
119 | - | |||||||||||||
120 | /*! | - | ||||||||||||
121 | \fn int QItemSelectionRange::top() const | - | ||||||||||||
122 | - | |||||||||||||
123 | Returns the row index corresponding to the uppermost selected row in the | - | ||||||||||||
124 | selection range. | - | ||||||||||||
125 | - | |||||||||||||
126 | */ | - | ||||||||||||
127 | - | |||||||||||||
128 | /*! | - | ||||||||||||
129 | \fn int QItemSelectionRange::left() const | - | ||||||||||||
130 | - | |||||||||||||
131 | Returns the column index corresponding to the leftmost selected column in the | - | ||||||||||||
132 | selection range. | - | ||||||||||||
133 | */ | - | ||||||||||||
134 | - | |||||||||||||
135 | /*! | - | ||||||||||||
136 | \fn int QItemSelectionRange::bottom() const | - | ||||||||||||
137 | - | |||||||||||||
138 | Returns the row index corresponding to the lowermost selected row in the | - | ||||||||||||
139 | selection range. | - | ||||||||||||
140 | - | |||||||||||||
141 | */ | - | ||||||||||||
142 | - | |||||||||||||
143 | /*! | - | ||||||||||||
144 | \fn int QItemSelectionRange::right() const | - | ||||||||||||
145 | - | |||||||||||||
146 | Returns the column index corresponding to the rightmost selected column in | - | ||||||||||||
147 | the selection range. | - | ||||||||||||
148 | - | |||||||||||||
149 | */ | - | ||||||||||||
150 | - | |||||||||||||
151 | /*! | - | ||||||||||||
152 | \fn int QItemSelectionRange::width() const | - | ||||||||||||
153 | - | |||||||||||||
154 | Returns the number of selected columns in the selection range. | - | ||||||||||||
155 | - | |||||||||||||
156 | */ | - | ||||||||||||
157 | - | |||||||||||||
158 | /*! | - | ||||||||||||
159 | \fn int QItemSelectionRange::height() const | - | ||||||||||||
160 | - | |||||||||||||
161 | Returns the number of selected rows in the selection range. | - | ||||||||||||
162 | - | |||||||||||||
163 | */ | - | ||||||||||||
164 | - | |||||||||||||
165 | /*! | - | ||||||||||||
166 | \fn const QAbstractItemModel *QItemSelectionRange::model() const | - | ||||||||||||
167 | - | |||||||||||||
168 | Returns the model that the items in the selection range belong to. | - | ||||||||||||
169 | */ | - | ||||||||||||
170 | - | |||||||||||||
171 | /*! | - | ||||||||||||
172 | \fn QModelIndex QItemSelectionRange::topLeft() const | - | ||||||||||||
173 | - | |||||||||||||
174 | Returns the index for the item located at the top-left corner of | - | ||||||||||||
175 | the selection range. | - | ||||||||||||
176 | - | |||||||||||||
177 | \sa top(), left(), bottomRight() | - | ||||||||||||
178 | */ | - | ||||||||||||
179 | - | |||||||||||||
180 | /*! | - | ||||||||||||
181 | \fn QModelIndex QItemSelectionRange::bottomRight() const | - | ||||||||||||
182 | - | |||||||||||||
183 | Returns the index for the item located at the bottom-right corner | - | ||||||||||||
184 | of the selection range. | - | ||||||||||||
185 | - | |||||||||||||
186 | \sa bottom(), right(), topLeft() | - | ||||||||||||
187 | */ | - | ||||||||||||
188 | - | |||||||||||||
189 | /*! | - | ||||||||||||
190 | \fn QModelIndex QItemSelectionRange::parent() const | - | ||||||||||||
191 | - | |||||||||||||
192 | Returns the parent model item index of the items in the selection range. | - | ||||||||||||
193 | - | |||||||||||||
194 | */ | - | ||||||||||||
195 | - | |||||||||||||
196 | /*! | - | ||||||||||||
197 | \fn bool QItemSelectionRange::contains(const QModelIndex &index) const | - | ||||||||||||
198 | - | |||||||||||||
199 | Returns \c true if the model item specified by the \a index lies within the | - | ||||||||||||
200 | range of selected items; otherwise returns \c false. | - | ||||||||||||
201 | */ | - | ||||||||||||
202 | - | |||||||||||||
203 | /*! | - | ||||||||||||
204 | \fn bool QItemSelectionRange::contains(int row, int column, | - | ||||||||||||
205 | const QModelIndex &parentIndex) const | - | ||||||||||||
206 | \overload | - | ||||||||||||
207 | - | |||||||||||||
208 | Returns \c true if the model item specified by (\a row, \a column) | - | ||||||||||||
209 | and with \a parentIndex as the parent item lies within the range | - | ||||||||||||
210 | of selected items; otherwise returns \c false. | - | ||||||||||||
211 | */ | - | ||||||||||||
212 | - | |||||||||||||
213 | /*! | - | ||||||||||||
214 | \fn bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const | - | ||||||||||||
215 | - | |||||||||||||
216 | Returns \c true if this selection range intersects (overlaps with) the \a other | - | ||||||||||||
217 | range given; otherwise returns \c false. | - | ||||||||||||
218 | - | |||||||||||||
219 | */ | - | ||||||||||||
220 | bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const | - | ||||||||||||
221 | { | - | ||||||||||||
222 | return (isValid() && other.isValid() | - | ||||||||||||
223 | && parent() == other.parent() | - | ||||||||||||
224 | && model() == other.model() | - | ||||||||||||
225 | && ((top() <= other.top() && bottom() >= other.top()) | - | ||||||||||||
226 | || (top() >= other.top() && top() <= other.bottom())) | - | ||||||||||||
227 | && ((left() <= other.left() && right() >= other.left()) | - | ||||||||||||
228 | || (left() >= other.left() && left() <= other.right()))); | - | ||||||||||||
229 | } | - | ||||||||||||
230 | - | |||||||||||||
231 | /*! | - | ||||||||||||
232 | \fn QItemSelectionRange QItemSelectionRange::intersect(const QItemSelectionRange &other) const | - | ||||||||||||
233 | \obsolete | - | ||||||||||||
234 | - | |||||||||||||
235 | Use intersected(\a other) instead. | - | ||||||||||||
236 | */ | - | ||||||||||||
237 | - | |||||||||||||
238 | /*! | - | ||||||||||||
239 | \fn QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange &other) const | - | ||||||||||||
240 | \since 4.2 | - | ||||||||||||
241 | - | |||||||||||||
242 | Returns a new selection range containing only the items that are found in | - | ||||||||||||
243 | both the selection range and the \a other selection range. | - | ||||||||||||
244 | */ | - | ||||||||||||
245 | - | |||||||||||||
246 | QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange &other) const | - | ||||||||||||
247 | { | - | ||||||||||||
248 | if (model() == other.model() && parent() == other.parent()) { | - | ||||||||||||
249 | QModelIndex topLeft = model()->index(qMax(top(), other.top()), | - | ||||||||||||
250 | qMax(left(), other.left()), | - | ||||||||||||
251 | other.parent()); | - | ||||||||||||
252 | QModelIndex bottomRight = model()->index(qMin(bottom(), other.bottom()), | - | ||||||||||||
253 | qMin(right(), other.right()), | - | ||||||||||||
254 | other.parent()); | - | ||||||||||||
255 | return QItemSelectionRange(topLeft, bottomRight); | - | ||||||||||||
256 | } | - | ||||||||||||
257 | return QItemSelectionRange(); | - | ||||||||||||
258 | } | - | ||||||||||||
259 | - | |||||||||||||
260 | /*! | - | ||||||||||||
261 | \fn bool QItemSelectionRange::operator==(const QItemSelectionRange &other) const | - | ||||||||||||
262 | - | |||||||||||||
263 | Returns \c true if the selection range is exactly the same as the \a other | - | ||||||||||||
264 | range given; otherwise returns \c false. | - | ||||||||||||
265 | - | |||||||||||||
266 | */ | - | ||||||||||||
267 | - | |||||||||||||
268 | /*! | - | ||||||||||||
269 | \fn bool QItemSelectionRange::operator!=(const QItemSelectionRange &other) const | - | ||||||||||||
270 | - | |||||||||||||
271 | Returns \c true if the selection range differs from the \a other range given; | - | ||||||||||||
272 | otherwise returns \c false. | - | ||||||||||||
273 | - | |||||||||||||
274 | */ | - | ||||||||||||
275 | - | |||||||||||||
276 | /*! | - | ||||||||||||
277 | \fn bool QItemSelectionRange::operator<(const QItemSelectionRange &other) const | - | ||||||||||||
278 | - | |||||||||||||
279 | Returns \c true if the selection range is less than the \a other | - | ||||||||||||
280 | range given; otherwise returns \c false. | - | ||||||||||||
281 | - | |||||||||||||
282 | The less than calculation is not directly useful to developers - the way that ranges | - | ||||||||||||
283 | with different parents compare is not defined. This operator only exists so that the | - | ||||||||||||
284 | class can be used with QMap. | - | ||||||||||||
285 | - | |||||||||||||
286 | */ | - | ||||||||||||
287 | - | |||||||||||||
288 | /*! | - | ||||||||||||
289 | \fn bool QItemSelectionRange::isValid() const | - | ||||||||||||
290 | - | |||||||||||||
291 | Returns \c true if the selection range is valid; otherwise returns \c false. | - | ||||||||||||
292 | - | |||||||||||||
293 | */ | - | ||||||||||||
294 | - | |||||||||||||
295 | static void rowLengthsFromRange(const QItemSelectionRange &range, QVector<QPair<QPersistentModelIndex, uint> > &result) | - | ||||||||||||
296 | { | - | ||||||||||||
297 | if (range.isValid() && range.model()) { | - | ||||||||||||
298 | const QModelIndex topLeft = range.topLeft(); | - | ||||||||||||
299 | const int bottom = range.bottom(); | - | ||||||||||||
300 | const uint width = range.width(); | - | ||||||||||||
301 | const int column = topLeft.column(); | - | ||||||||||||
302 | for (int row = topLeft.row(); row <= bottom; ++row) { | - | ||||||||||||
303 | // We don't need to keep track of ItemIsSelectable and ItemIsEnabled here. That is | - | ||||||||||||
304 | // required in indexesFromRange() because that method is called from public API | - | ||||||||||||
305 | // which requires the limitation. | - | ||||||||||||
306 | result.push_back(qMakePair(QPersistentModelIndex(topLeft.sibling(row, column)), width)); | - | ||||||||||||
307 | } | - | ||||||||||||
308 | } | - | ||||||||||||
309 | } | - | ||||||||||||
310 | - | |||||||||||||
311 | template<typename ModelIndexContainer> | - | ||||||||||||
312 | static void indexesFromRange(const QItemSelectionRange &range, ModelIndexContainer &result) | - | ||||||||||||
313 | { | - | ||||||||||||
314 | if (range.isValid() && range.model()) { | - | ||||||||||||
315 | const QModelIndex topLeft = range.topLeft(); | - | ||||||||||||
316 | const int bottom = range.bottom(); | - | ||||||||||||
317 | const int right = range.right(); | - | ||||||||||||
318 | for (int row = topLeft.row(); row <= bottom; ++row) { | - | ||||||||||||
319 | const QModelIndex columnLeader = topLeft.sibling(row, topLeft.column()); | - | ||||||||||||
320 | for (int column = topLeft.column(); column <= right; ++column) { | - | ||||||||||||
321 | QModelIndex index = columnLeader.sibling(row, column); | - | ||||||||||||
322 | Qt::ItemFlags flags = range.model()->flags(index); | - | ||||||||||||
323 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
324 | result.push_back(index); | - | ||||||||||||
325 | } | - | ||||||||||||
326 | } | - | ||||||||||||
327 | } | - | ||||||||||||
328 | } | - | ||||||||||||
329 | - | |||||||||||||
330 | template<typename ModelIndexContainer> | - | ||||||||||||
331 | static ModelIndexContainer qSelectionIndexes(const QItemSelection &selection) | - | ||||||||||||
332 | { | - | ||||||||||||
333 | ModelIndexContainer result; | - | ||||||||||||
334 | for (const auto &range : selection) | - | ||||||||||||
335 | indexesFromRange(range, result); executed 1046 times by 18 tests: indexesFromRange(range, result); Executed by:
| 1046 | ||||||||||||
336 | return result; executed 6813 times by 24 tests: return result; Executed by:
| 6813 | ||||||||||||
337 | } | - | ||||||||||||
338 | - | |||||||||||||
339 | /*! | - | ||||||||||||
340 | Returns \c true if the selection range contains no selectable item | - | ||||||||||||
341 | \since 4.7 | - | ||||||||||||
342 | */ | - | ||||||||||||
343 | - | |||||||||||||
344 | bool QItemSelectionRange::isEmpty() const | - | ||||||||||||
345 | { | - | ||||||||||||
346 | if (!isValid() || !model()) | - | ||||||||||||
347 | return true; | - | ||||||||||||
348 | - | |||||||||||||
349 | for (int column = left(); column <= right(); ++column) { | - | ||||||||||||
350 | for (int row = top(); row <= bottom(); ++row) { | - | ||||||||||||
351 | QModelIndex index = model()->index(row, column, parent()); | - | ||||||||||||
352 | Qt::ItemFlags flags = model()->flags(index); | - | ||||||||||||
353 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
354 | return false; | - | ||||||||||||
355 | } | - | ||||||||||||
356 | } | - | ||||||||||||
357 | return true; | - | ||||||||||||
358 | } | - | ||||||||||||
359 | - | |||||||||||||
360 | /*! | - | ||||||||||||
361 | Returns the list of model index items stored in the selection. | - | ||||||||||||
362 | */ | - | ||||||||||||
363 | - | |||||||||||||
364 | QModelIndexList QItemSelectionRange::indexes() const | - | ||||||||||||
365 | { | - | ||||||||||||
366 | QModelIndexList result; | - | ||||||||||||
367 | indexesFromRange(*this, result); | - | ||||||||||||
368 | return result; | - | ||||||||||||
369 | } | - | ||||||||||||
370 | - | |||||||||||||
371 | /*! | - | ||||||||||||
372 | \class QItemSelection | - | ||||||||||||
373 | \inmodule QtCore | - | ||||||||||||
374 | - | |||||||||||||
375 | \brief The QItemSelection class manages information about selected items in a model. | - | ||||||||||||
376 | - | |||||||||||||
377 | \ingroup model-view | - | ||||||||||||
378 | - | |||||||||||||
379 | A QItemSelection describes the items in a model that have been | - | ||||||||||||
380 | selected by the user. A QItemSelection is basically a list of | - | ||||||||||||
381 | selection ranges, see QItemSelectionRange. It provides functions for | - | ||||||||||||
382 | creating and manipulating selections, and selecting a range of items | - | ||||||||||||
383 | from a model. | - | ||||||||||||
384 | - | |||||||||||||
385 | The QItemSelection class is one of the \l{Model/View Classes} | - | ||||||||||||
386 | and is part of Qt's \l{Model/View Programming}{model/view framework}. | - | ||||||||||||
387 | - | |||||||||||||
388 | An item selection can be constructed and initialized to contain a | - | ||||||||||||
389 | range of items from an existing model. The following example constructs | - | ||||||||||||
390 | a selection that contains a range of items from the given \c model, | - | ||||||||||||
391 | beginning at the \c topLeft, and ending at the \c bottomRight. | - | ||||||||||||
392 | - | |||||||||||||
393 | \snippet code/src_gui_itemviews_qitemselectionmodel.cpp 0 | - | ||||||||||||
394 | - | |||||||||||||
395 | An empty item selection can be constructed, and later populated as | - | ||||||||||||
396 | required. So, if the model is going to be unavailable when we construct | - | ||||||||||||
397 | the item selection, we can rewrite the above code in the following way: | - | ||||||||||||
398 | - | |||||||||||||
399 | \snippet code/src_gui_itemviews_qitemselectionmodel.cpp 1 | - | ||||||||||||
400 | - | |||||||||||||
401 | QItemSelection saves memory, and avoids unnecessary work, by working with | - | ||||||||||||
402 | selection ranges rather than recording the model item index for each | - | ||||||||||||
403 | item in the selection. Generally, an instance of this class will contain | - | ||||||||||||
404 | a list of non-overlapping selection ranges. | - | ||||||||||||
405 | - | |||||||||||||
406 | Use merge() to merge one item selection into another without making | - | ||||||||||||
407 | overlapping ranges. Use split() to split one selection range into | - | ||||||||||||
408 | smaller ranges based on a another selection range. | - | ||||||||||||
409 | - | |||||||||||||
410 | \sa {Model/View Programming}, QItemSelectionModel | - | ||||||||||||
411 | */ | - | ||||||||||||
412 | - | |||||||||||||
413 | /*! | - | ||||||||||||
414 | \fn QItemSelection::QItemSelection() | - | ||||||||||||
415 | - | |||||||||||||
416 | Constructs an empty selection. | - | ||||||||||||
417 | */ | - | ||||||||||||
418 | - | |||||||||||||
419 | /*! | - | ||||||||||||
420 | Constructs an item selection that extends from the top-left model item, | - | ||||||||||||
421 | specified by the \a topLeft index, to the bottom-right item, specified | - | ||||||||||||
422 | by \a bottomRight. | - | ||||||||||||
423 | */ | - | ||||||||||||
424 | QItemSelection::QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight) | - | ||||||||||||
425 | { | - | ||||||||||||
426 | select(topLeft, bottomRight); | - | ||||||||||||
427 | } | - | ||||||||||||
428 | - | |||||||||||||
429 | /*! | - | ||||||||||||
430 | Adds the items in the range that extends from the top-left model | - | ||||||||||||
431 | item, specified by the \a topLeft index, to the bottom-right item, | - | ||||||||||||
432 | specified by \a bottomRight to the list. | - | ||||||||||||
433 | - | |||||||||||||
434 | \note \a topLeft and \a bottomRight must have the same parent. | - | ||||||||||||
435 | */ | - | ||||||||||||
436 | void QItemSelection::select(const QModelIndex &topLeft, const QModelIndex &bottomRight) | - | ||||||||||||
437 | { | - | ||||||||||||
438 | if (!topLeft.isValid() || !bottomRight.isValid()) | - | ||||||||||||
439 | return; | - | ||||||||||||
440 | - | |||||||||||||
441 | if ((topLeft.model() != bottomRight.model()) | - | ||||||||||||
442 | || topLeft.parent() != bottomRight.parent()) { | - | ||||||||||||
443 | qWarning("Can't select indexes from different model or with different parents"); | - | ||||||||||||
444 | return; | - | ||||||||||||
445 | } | - | ||||||||||||
446 | if (topLeft.row() > bottomRight.row() || topLeft.column() > bottomRight.column()) { | - | ||||||||||||
447 | int top = qMin(topLeft.row(), bottomRight.row()); | - | ||||||||||||
448 | int bottom = qMax(topLeft.row(), bottomRight.row()); | - | ||||||||||||
449 | int left = qMin(topLeft.column(), bottomRight.column()); | - | ||||||||||||
450 | int right = qMax(topLeft.column(), bottomRight.column()); | - | ||||||||||||
451 | QModelIndex tl = topLeft.sibling(top, left); | - | ||||||||||||
452 | QModelIndex br = bottomRight.sibling(bottom, right); | - | ||||||||||||
453 | append(QItemSelectionRange(tl, br)); | - | ||||||||||||
454 | return; | - | ||||||||||||
455 | } | - | ||||||||||||
456 | append(QItemSelectionRange(topLeft, bottomRight)); | - | ||||||||||||
457 | } | - | ||||||||||||
458 | - | |||||||||||||
459 | /*! | - | ||||||||||||
460 | Returns \c true if the selection contains the given \a index; otherwise | - | ||||||||||||
461 | returns \c false. | - | ||||||||||||
462 | */ | - | ||||||||||||
463 | - | |||||||||||||
464 | bool QItemSelection::contains(const QModelIndex &index) const | - | ||||||||||||
465 | { | - | ||||||||||||
466 | if (index.flags() & Qt::ItemIsSelectable) { | - | ||||||||||||
467 | QList<QItemSelectionRange>::const_iterator it = begin(); | - | ||||||||||||
468 | for (; it != end(); ++it) | - | ||||||||||||
469 | if ((*it).contains(index)) | - | ||||||||||||
470 | return true; | - | ||||||||||||
471 | } | - | ||||||||||||
472 | return false; | - | ||||||||||||
473 | } | - | ||||||||||||
474 | - | |||||||||||||
475 | /*! | - | ||||||||||||
476 | Returns a list of model indexes that correspond to the selected items. | - | ||||||||||||
477 | */ | - | ||||||||||||
478 | - | |||||||||||||
479 | QModelIndexList QItemSelection::indexes() const | - | ||||||||||||
480 | { | - | ||||||||||||
481 | QModelIndexList result; executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| 1153 | ||||||||||||
QList<QItemSelectionRange>::const_iterator it = begin(); executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
for (; it != end(); ++it) executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
indexesFromRange(*it, result); executed 1153 times by 17 tests: return result;return qSelectionIndexes<QModelIndexList>(*this); Executed by:
executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
} executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
static QVector<QPersistentModelIndex> qSelectionPersistentindexes(const QItemSelection &sel) executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
{ executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
QVector<QPersistentModelIndex> result; executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
QList executed 1153 times by 17 tests: qSelectionIndexes<QItemSelectionRange>::const_iterator it = sel.constBegin();return qSelectionIndexes<QModelIndexList>(*this); Executed by:
executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
for (; it != sel.constEnd(); ++it) executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
indexesFromRange(*it, result executed 1153 times by 17 tests: QModelIndexList>(*this);return qSelectionIndexes<QModelIndexList>(*this); Executed by:
executed 1153 times by 17 tests: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
| ||||||||||||||
return result;} | ||||||||||||||
483 | - | |||||||||||||
484 | static QVector<QPair<QPersistentModelIndex, uint> > qSelectionPersistentRowLengths(const QItemSelection &sel) | - | ||||||||||||
485 | { | - | ||||||||||||
486 | QVector<QPair<QPersistentModelIndex, uint> > result; | - | ||||||||||||
487 | Q_FOREACHfor (const QItemSelectionRange &range ,: sel) | - | ||||||||||||
488 | rowLengthsFromRange(range, result); executed 15 times by 2 tests: rowLengthsFromRange(range, result); Executed by:
| 15 | ||||||||||||
489 | return result; executed 250 times by 9 tests: return result; Executed by:
| 250 | ||||||||||||
490 | } | - | ||||||||||||
491 | - | |||||||||||||
492 | /*! | - | ||||||||||||
493 | Merges the \a other selection with this QItemSelection using the | - | ||||||||||||
494 | \a command given. This method guarantees that no ranges are overlapping. | - | ||||||||||||
495 | - | |||||||||||||
496 | Note that only QItemSelectionModel::Select, | - | ||||||||||||
497 | QItemSelectionModel::Deselect, and QItemSelectionModel::Toggle are | - | ||||||||||||
498 | supported. | - | ||||||||||||
499 | - | |||||||||||||
500 | \sa split() | - | ||||||||||||
501 | */ | - | ||||||||||||
502 | void QItemSelection::merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
503 | { | - | ||||||||||||
504 | if (other.isEmpty() || | - | ||||||||||||
505 | !(command & QItemSelectionModel::Select || | - | ||||||||||||
506 | command & QItemSelectionModel::Deselect || | - | ||||||||||||
507 | command & QItemSelectionModel::Toggle)) | - | ||||||||||||
508 | return; | - | ||||||||||||
509 | - | |||||||||||||
510 | QItemSelection newSelection = other; | - | ||||||||||||
511 | // Collect intersections | - | ||||||||||||
512 | QItemSelection intersections; | - | ||||||||||||
513 | QItemSelection::iterator it = newSelection.begin(); | - | ||||||||||||
514 | while (it != newSelection.end()) { | - | ||||||||||||
515 | if (!(*it).isValid()) { | - | ||||||||||||
516 | it = newSelection.erase(it); | - | ||||||||||||
517 | continue; | - | ||||||||||||
518 | } | - | ||||||||||||
519 | for (int t = 0; t < count(); ++t) { | - | ||||||||||||
520 | if ((*it).intersects(at(t))) | - | ||||||||||||
521 | intersections.append(at(t).intersected(*it)); | - | ||||||||||||
522 | } | - | ||||||||||||
523 | ++it; | - | ||||||||||||
524 | } | - | ||||||||||||
525 | - | |||||||||||||
526 | // Split the old (and new) ranges using the intersections | - | ||||||||||||
527 | for (int i = 0; i < intersections.count(); ++i) { // for each intersection | - | ||||||||||||
528 | for (int t = 0; t < count();) { // splitt each old range | - | ||||||||||||
529 | if (at(t).intersects(intersections.at(i))) { | - | ||||||||||||
530 | split(at(t), intersections.at(i), this); | - | ||||||||||||
531 | removeAt(t); | - | ||||||||||||
532 | } else { | - | ||||||||||||
533 | ++t; | - | ||||||||||||
534 | } | - | ||||||||||||
535 | } | - | ||||||||||||
536 | // only split newSelection if Toggle is specified | - | ||||||||||||
537 | for (int n = 0; (command & QItemSelectionModel::Toggle) && n < newSelection.count();) { | - | ||||||||||||
538 | if (newSelection.at(n).intersects(intersections.at(i))) { | - | ||||||||||||
539 | split(newSelection.at(n), intersections.at(i), &newSelection); | - | ||||||||||||
540 | newSelection.removeAt(n); | - | ||||||||||||
541 | } else { | - | ||||||||||||
542 | ++n; | - | ||||||||||||
543 | } | - | ||||||||||||
544 | } | - | ||||||||||||
545 | } | - | ||||||||||||
546 | // do not add newSelection for Deselect | - | ||||||||||||
547 | if (!(command & QItemSelectionModel::Deselect)) | - | ||||||||||||
548 | operator+=(newSelection); | - | ||||||||||||
549 | } | - | ||||||||||||
550 | - | |||||||||||||
551 | /*! | - | ||||||||||||
552 | Splits the selection \a range using the selection \a other range. | - | ||||||||||||
553 | Removes all items in \a other from \a range and puts the result in \a result. | - | ||||||||||||
554 | This can be compared with the semantics of the \e subtract operation of a set. | - | ||||||||||||
555 | \sa merge() | - | ||||||||||||
556 | */ | - | ||||||||||||
557 | - | |||||||||||||
558 | void QItemSelection::split(const QItemSelectionRange &range, | - | ||||||||||||
559 | const QItemSelectionRange &other, QItemSelection *result) | - | ||||||||||||
560 | { | - | ||||||||||||
561 | if (range.parent() != other.parent() || range.model() != other.model()) | - | ||||||||||||
562 | return; | - | ||||||||||||
563 | - | |||||||||||||
564 | QModelIndex parent = other.parent(); | - | ||||||||||||
565 | int top = range.top(); | - | ||||||||||||
566 | int left = range.left(); | - | ||||||||||||
567 | int bottom = range.bottom(); | - | ||||||||||||
568 | int right = range.right(); | - | ||||||||||||
569 | int other_top = other.top(); | - | ||||||||||||
570 | int other_left = other.left(); | - | ||||||||||||
571 | int other_bottom = other.bottom(); | - | ||||||||||||
572 | int other_right = other.right(); | - | ||||||||||||
573 | const QAbstractItemModel *model = range.model(); | - | ||||||||||||
574 | Q_ASSERT(model); | - | ||||||||||||
575 | if (other_top > top) { | - | ||||||||||||
576 | QModelIndex tl = model->index(top, left, parent); | - | ||||||||||||
577 | QModelIndex br = model->index(other_top - 1, right, parent); | - | ||||||||||||
578 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
579 | top = other_top; | - | ||||||||||||
580 | } | - | ||||||||||||
581 | if (other_bottom < bottom) { | - | ||||||||||||
582 | QModelIndex tl = model->index(other_bottom + 1, left, parent); | - | ||||||||||||
583 | QModelIndex br = model->index(bottom, right, parent); | - | ||||||||||||
584 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
585 | bottom = other_bottom; | - | ||||||||||||
586 | } | - | ||||||||||||
587 | if (other_left > left) { | - | ||||||||||||
588 | QModelIndex tl = model->index(top, left, parent); | - | ||||||||||||
589 | QModelIndex br = model->index(bottom, other_left - 1, parent); | - | ||||||||||||
590 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
591 | left = other_left; | - | ||||||||||||
592 | } | - | ||||||||||||
593 | if (other_right < right) { | - | ||||||||||||
594 | QModelIndex tl = model->index(top, other_right + 1, parent); | - | ||||||||||||
595 | QModelIndex br = model->index(bottom, right, parent); | - | ||||||||||||
596 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
597 | right = other_right; | - | ||||||||||||
598 | } | - | ||||||||||||
599 | } | - | ||||||||||||
600 | - | |||||||||||||
601 | - | |||||||||||||
602 | void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m) | - | ||||||||||||
603 | { | - | ||||||||||||
604 | struct Cx { | - | ||||||||||||
605 | const char *signal; | - | ||||||||||||
606 | const char *slot; | - | ||||||||||||
607 | }; | - | ||||||||||||
608 | static const Cx connections[] = { | - | ||||||||||||
609 | { SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), | - | ||||||||||||
610 | SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)) }, | - | ||||||||||||
611 | { SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), | - | ||||||||||||
612 | SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)) }, | - | ||||||||||||
613 | { SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), | - | ||||||||||||
614 | SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)) }, | - | ||||||||||||
615 | { SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), | - | ||||||||||||
616 | SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)) }, | - | ||||||||||||
617 | { SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), | - | ||||||||||||
618 | SLOT(_q_layoutAboutToBeChanged()) }, | - | ||||||||||||
619 | { SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), | - | ||||||||||||
620 | SLOT(_q_layoutAboutToBeChanged()) }, | - | ||||||||||||
621 | { SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), | - | ||||||||||||
622 | SLOT(_q_layoutChanged()) }, | - | ||||||||||||
623 | { SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)), | - | ||||||||||||
624 | SLOT(_q_layoutChanged()) }, | - | ||||||||||||
625 | { SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)), | - | ||||||||||||
626 | SLOT(_q_layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) }, | - | ||||||||||||
627 | { SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)), | - | ||||||||||||
628 | SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) }, | - | ||||||||||||
629 | { SIGNAL(modelReset()), | - | ||||||||||||
630 | SLOT(reset()) }, | - | ||||||||||||
631 | { 0, 0 } | - | ||||||||||||
632 | }; | - | ||||||||||||
633 | - | |||||||||||||
634 | if (model == m) | - | ||||||||||||
635 | return; | - | ||||||||||||
636 | - | |||||||||||||
637 | Q_Q(QItemSelectionModel); | - | ||||||||||||
638 | if (model) { | - | ||||||||||||
639 | for (const Cx *cx = &connections[0]; cx->signal; cx++) | - | ||||||||||||
640 | QObject::disconnect(model, cx->signal, q, cx->slot); | - | ||||||||||||
641 | q->reset(); | - | ||||||||||||
642 | } | - | ||||||||||||
643 | model = m; | - | ||||||||||||
644 | if (model) { | - | ||||||||||||
645 | for (const Cx *cx = &connections[0]; cx->signal; cx++) | - | ||||||||||||
646 | QObject::connect(model, cx->signal, q, cx->slot); | - | ||||||||||||
647 | } | - | ||||||||||||
648 | } | - | ||||||||||||
649 | - | |||||||||||||
650 | /*! | - | ||||||||||||
651 | \internal | - | ||||||||||||
652 | - | |||||||||||||
653 | returns a QItemSelection where all ranges have been expanded to: | - | ||||||||||||
654 | Rows: left: 0 and right: columnCount()-1 | - | ||||||||||||
655 | Columns: top: 0 and bottom: rowCount()-1 | - | ||||||||||||
656 | */ | - | ||||||||||||
657 | - | |||||||||||||
658 | QItemSelection QItemSelectionModelPrivate::expandSelection(const QItemSelection &selection, | - | ||||||||||||
659 | QItemSelectionModel::SelectionFlags command) const | - | ||||||||||||
660 | { | - | ||||||||||||
661 | if (selection.isEmpty() && !((command & QItemSelectionModel::Rows) || | - | ||||||||||||
662 | (command & QItemSelectionModel::Columns))) | - | ||||||||||||
663 | return selection; | - | ||||||||||||
664 | - | |||||||||||||
665 | QItemSelection expanded; | - | ||||||||||||
666 | if (command & QItemSelectionModel::Rows) { | - | ||||||||||||
667 | for (int i = 0; i < selection.count(); ++i) { | - | ||||||||||||
668 | QModelIndex parent = selection.at(i).parent(); | - | ||||||||||||
669 | int colCount = model->columnCount(parent); | - | ||||||||||||
670 | QModelIndex tl = model->index(selection.at(i).top(), 0, parent); | - | ||||||||||||
671 | QModelIndex br = model->index(selection.at(i).bottom(), colCount - 1, parent); | - | ||||||||||||
672 | //we need to merge because the same row could have already been inserted | - | ||||||||||||
673 | expanded.merge(QItemSelection(tl, br), QItemSelectionModel::Select); | - | ||||||||||||
674 | } | - | ||||||||||||
675 | } | - | ||||||||||||
676 | if (command & QItemSelectionModel::Columns) { | - | ||||||||||||
677 | for (int i = 0; i < selection.count(); ++i) { | - | ||||||||||||
678 | QModelIndex parent = selection.at(i).parent(); | - | ||||||||||||
679 | int rowCount = model->rowCount(parent); | - | ||||||||||||
680 | QModelIndex tl = model->index(0, selection.at(i).left(), parent); | - | ||||||||||||
681 | QModelIndex br = model->index(rowCount - 1, selection.at(i).right(), parent); | - | ||||||||||||
682 | //we need to merge because the same column could have already been inserted | - | ||||||||||||
683 | expanded.merge(QItemSelection(tl, br), QItemSelectionModel::Select); | - | ||||||||||||
684 | } | - | ||||||||||||
685 | } | - | ||||||||||||
686 | return expanded; | - | ||||||||||||
687 | } | - | ||||||||||||
688 | - | |||||||||||||
689 | /*! | - | ||||||||||||
690 | \internal | - | ||||||||||||
691 | */ | - | ||||||||||||
692 | void QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &parent, | - | ||||||||||||
693 | int start, int end) | - | ||||||||||||
694 | { | - | ||||||||||||
695 | Q_Q(QItemSelectionModel); | - | ||||||||||||
696 | finalize(); | - | ||||||||||||
697 | - | |||||||||||||
698 | // update current index | - | ||||||||||||
699 | if (currentIndex.isValid() && parent == currentIndex.parent() | - | ||||||||||||
700 | && currentIndex.row() >= start && currentIndex.row() <= end) { | - | ||||||||||||
701 | QModelIndex old = currentIndex; | - | ||||||||||||
702 | if (start > 0) // there are rows left above the change | - | ||||||||||||
703 | currentIndex = model->index(start - 1, old.column(), parent); | - | ||||||||||||
704 | else if (model && end < model->rowCount(parent) - 1) // there are rows left below the change | - | ||||||||||||
705 | currentIndex = model->index(end + 1, old.column(), parent); | - | ||||||||||||
706 | else // there are no rows left in the table | - | ||||||||||||
707 | currentIndex = QModelIndex(); | - | ||||||||||||
708 | emit q->currentChanged(currentIndex, old); | - | ||||||||||||
709 | emit q->currentRowChanged(currentIndex, old); | - | ||||||||||||
710 | if (currentIndex.column() != old.column()) | - | ||||||||||||
711 | emit q->currentColumnChanged(currentIndex, old); | - | ||||||||||||
712 | } | - | ||||||||||||
713 | - | |||||||||||||
714 | QItemSelection deselected; | - | ||||||||||||
715 | QItemSelection newParts; | - | ||||||||||||
716 | QItemSelection::iterator it = ranges.begin(); | - | ||||||||||||
717 | while (it != ranges.end()) { | - | ||||||||||||
718 | if (it->topLeft().parent() != parent) { // Check parents until reaching root or contained in range | - | ||||||||||||
719 | QModelIndex itParent = it->topLeft().parent(); | - | ||||||||||||
720 | while (itParent.isValid() && itParent.parent() != parent) | - | ||||||||||||
721 | itParent = itParent.parent(); | - | ||||||||||||
722 | - | |||||||||||||
723 | if (itParent.isValid() && start <= itParent.row() && itParent.row() <= end) { | - | ||||||||||||
724 | deselected.append(*it); | - | ||||||||||||
725 | it = ranges.erase(it); | - | ||||||||||||
726 | } else { | - | ||||||||||||
727 | ++it; | - | ||||||||||||
728 | } | - | ||||||||||||
729 | } else if (start <= it->bottom() && it->bottom() <= end // Full inclusion | - | ||||||||||||
730 | && start <= it->top() && it->top() <= end) { | - | ||||||||||||
731 | deselected.append(*it); | - | ||||||||||||
732 | it = ranges.erase(it); | - | ||||||||||||
733 | } else if (start <= it->top() && it->top() <= end) { // Top intersection | - | ||||||||||||
734 | deselected.append(QItemSelectionRange(it->topLeft(), model->index(end, it->right(), it->parent()))); | - | ||||||||||||
735 | *it = QItemSelectionRange(model->index(end + 1, it->left(), it->parent()), it->bottomRight()); | - | ||||||||||||
736 | ++it; | - | ||||||||||||
737 | } else if (start <= it->bottom() && it->bottom() <= end) { // Bottom intersection | - | ||||||||||||
738 | deselected.append(QItemSelectionRange(model->index(start, it->left(), it->parent()), it->bottomRight())); | - | ||||||||||||
739 | *it = QItemSelectionRange(it->topLeft(), model->index(start - 1, it->right(), it->parent())); | - | ||||||||||||
740 | ++it; | - | ||||||||||||
741 | } else if (it->top() < start && end < it->bottom()) { // Middle intersection | - | ||||||||||||
742 | // If the parent contains (1, 2, 3, 4, 5, 6, 7, 8) and [3, 4, 5, 6] is selected, | - | ||||||||||||
743 | // and [4, 5] is removed, we need to split [3, 4, 5, 6] into [3], [4, 5] and [6]. | - | ||||||||||||
744 | // [4, 5] is appended to deselected, and [3] and [6] remain part of the selection | - | ||||||||||||
745 | // in ranges. | - | ||||||||||||
746 | const QItemSelectionRange removedRange(model->index(start, it->left(), it->parent()), | - | ||||||||||||
747 | model->index(end, it->right(), it->parent())); | - | ||||||||||||
748 | deselected.append(removedRange); | - | ||||||||||||
749 | QItemSelection::split(*it, removedRange, &newParts); | - | ||||||||||||
750 | it = ranges.erase(it); | - | ||||||||||||
751 | } else | - | ||||||||||||
752 | ++it; | - | ||||||||||||
753 | } | - | ||||||||||||
754 | ranges.append(newParts); | - | ||||||||||||
755 | - | |||||||||||||
756 | if (!deselected.isEmpty()) | - | ||||||||||||
757 | emit q->selectionChanged(QItemSelection(), deselected); | - | ||||||||||||
758 | } | - | ||||||||||||
759 | - | |||||||||||||
760 | /*! | - | ||||||||||||
761 | \internal | - | ||||||||||||
762 | */ | - | ||||||||||||
763 | void QItemSelectionModelPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent, | - | ||||||||||||
764 | int start, int end) | - | ||||||||||||
765 | { | - | ||||||||||||
766 | Q_Q(QItemSelectionModel); | - | ||||||||||||
767 | - | |||||||||||||
768 | // update current index | - | ||||||||||||
769 | if (currentIndex.isValid() && parent == currentIndex.parent() | - | ||||||||||||
770 | && currentIndex.column() >= start && currentIndex.column() <= end) { | - | ||||||||||||
771 | QModelIndex old = currentIndex; | - | ||||||||||||
772 | if (start > 0) // there are columns to the left of the change | - | ||||||||||||
773 | currentIndex = model->index(old.row(), start - 1, parent); | - | ||||||||||||
774 | else if (model && end < model->columnCount() - 1) // there are columns to the right of the change | - | ||||||||||||
775 | currentIndex = model->index(old.row(), end + 1, parent); | - | ||||||||||||
776 | else // there are no columns left in the table | - | ||||||||||||
777 | currentIndex = QModelIndex(); | - | ||||||||||||
778 | emit q->currentChanged(currentIndex, old); | - | ||||||||||||
779 | if (currentIndex.row() != old.row()) | - | ||||||||||||
780 | emit q->currentRowChanged(currentIndex, old); | - | ||||||||||||
781 | emit q->currentColumnChanged(currentIndex, old); | - | ||||||||||||
782 | } | - | ||||||||||||
783 | - | |||||||||||||
784 | // update selections | - | ||||||||||||
785 | QModelIndex tl = model->index(0, start, parent); | - | ||||||||||||
786 | QModelIndex br = model->index(model->rowCount(parent) - 1, end, parent); | - | ||||||||||||
787 | q->select(QItemSelection(tl, br), QItemSelectionModel::Deselect); | - | ||||||||||||
788 | finalize(); | - | ||||||||||||
789 | } | - | ||||||||||||
790 | - | |||||||||||||
791 | /*! | - | ||||||||||||
792 | \internal | - | ||||||||||||
793 | - | |||||||||||||
794 | Split selection ranges if columns are about to be inserted in the middle. | - | ||||||||||||
795 | */ | - | ||||||||||||
796 | void QItemSelectionModelPrivate::_q_columnsAboutToBeInserted(const QModelIndex &parent, | - | ||||||||||||
797 | int start, int end) | - | ||||||||||||
798 | { | - | ||||||||||||
799 | Q_UNUSED(end); | - | ||||||||||||
800 | finalize(); | - | ||||||||||||
801 | QList<QItemSelectionRange> split; | - | ||||||||||||
802 | QList<QItemSelectionRange>::iterator it = ranges.begin(); | - | ||||||||||||
803 | for (; it != ranges.end(); ) { | - | ||||||||||||
804 | if ((*it).isValid() && (*it).parent() == parent | - | ||||||||||||
805 | && (*it).left() < start && (*it).right() >= start) { | - | ||||||||||||
806 | QModelIndex bottomMiddle = model->index((*it).bottom(), start - 1, (*it).parent()); | - | ||||||||||||
807 | QItemSelectionRange left((*it).topLeft(), bottomMiddle); | - | ||||||||||||
808 | QModelIndex topMiddle = model->index((*it).top(), start, (*it).parent()); | - | ||||||||||||
809 | QItemSelectionRange right(topMiddle, (*it).bottomRight()); | - | ||||||||||||
810 | it = ranges.erase(it); | - | ||||||||||||
811 | split.append(left); | - | ||||||||||||
812 | split.append(right); | - | ||||||||||||
813 | } else { | - | ||||||||||||
814 | ++it; | - | ||||||||||||
815 | } | - | ||||||||||||
816 | } | - | ||||||||||||
817 | ranges += split; | - | ||||||||||||
818 | } | - | ||||||||||||
819 | - | |||||||||||||
820 | /*! | - | ||||||||||||
821 | \internal | - | ||||||||||||
822 | - | |||||||||||||
823 | Split selection ranges if rows are about to be inserted in the middle. | - | ||||||||||||
824 | */ | - | ||||||||||||
825 | void QItemSelectionModelPrivate::_q_rowsAboutToBeInserted(const QModelIndex &parent, | - | ||||||||||||
826 | int start, int end) | - | ||||||||||||
827 | { | - | ||||||||||||
828 | Q_UNUSED(end); | - | ||||||||||||
829 | finalize(); | - | ||||||||||||
830 | QList<QItemSelectionRange> split; | - | ||||||||||||
831 | QList<QItemSelectionRange>::iterator it = ranges.begin(); | - | ||||||||||||
832 | for (; it != ranges.end(); ) { | - | ||||||||||||
833 | if ((*it).isValid() && (*it).parent() == parent | - | ||||||||||||
834 | && (*it).top() < start && (*it).bottom() >= start) { | - | ||||||||||||
835 | QModelIndex middleRight = model->index(start - 1, (*it).right(), (*it).parent()); | - | ||||||||||||
836 | QItemSelectionRange top((*it).topLeft(), middleRight); | - | ||||||||||||
837 | QModelIndex middleLeft = model->index(start, (*it).left(), (*it).parent()); | - | ||||||||||||
838 | QItemSelectionRange bottom(middleLeft, (*it).bottomRight()); | - | ||||||||||||
839 | it = ranges.erase(it); | - | ||||||||||||
840 | split.append(top); | - | ||||||||||||
841 | split.append(bottom); | - | ||||||||||||
842 | } else { | - | ||||||||||||
843 | ++it; | - | ||||||||||||
844 | } | - | ||||||||||||
845 | } | - | ||||||||||||
846 | ranges += split; | - | ||||||||||||
847 | } | - | ||||||||||||
848 | - | |||||||||||||
849 | /*! | - | ||||||||||||
850 | \internal | - | ||||||||||||
851 | - | |||||||||||||
852 | Split selection into individual (persistent) indexes. This is done in | - | ||||||||||||
853 | preparation for the layoutChanged() signal, where the indexes can be | - | ||||||||||||
854 | merged again. | - | ||||||||||||
855 | */ | - | ||||||||||||
856 | void QItemSelectionModelPrivate::_q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint hint) | - | ||||||||||||
857 | { | - | ||||||||||||
858 | savedPersistentIndexes.clear(); | - | ||||||||||||
859 | savedPersistentCurrentIndexes.clear(); | - | ||||||||||||
860 | savedPersistentRowLengths.clear(); | - | ||||||||||||
861 | savedPersistentCurrentRowLengths.clear(); | - | ||||||||||||
862 | - | |||||||||||||
863 | // optimization for when all indexes are selected | - | ||||||||||||
864 | // (only if there is lots of items (1000) because this is not entirely correct) | - | ||||||||||||
865 | if (ranges.isEmpty() && currentSelection.count() == 1) {
| 14-2919 | ||||||||||||
866 | QItemSelectionRange range = currentSelection.firstconstFirst(); | - | ||||||||||||
867 | QModelIndex parent = range.parent(); | - | ||||||||||||
868 | tableRowCount = model->rowCount(parent); | - | ||||||||||||
869 | tableColCount = model->columnCount(parent); | - | ||||||||||||
870 | if (tableRowCount * tableColCount > 1000
| 0-14 | ||||||||||||
871 | && range.top() == 0
| 0 | ||||||||||||
872 | && range.left() == 0
| 0 | ||||||||||||
873 | && range.bottom() == tableRowCount - 1
| 0 | ||||||||||||
874 | && range.right() == tableColCount - 1) {
| 0 | ||||||||||||
875 | tableSelected = true; | - | ||||||||||||
876 | tableParent = parent; | - | ||||||||||||
877 | return; never executed: return; | 0 | ||||||||||||
878 | } | - | ||||||||||||
879 | } executed 14 times by 6 tests: end of block Executed by:
| 14 | ||||||||||||
880 | tableSelected = false; | - | ||||||||||||
881 | - | |||||||||||||
882 | if (hint == QAbstractItemModel::VerticalSortHint) {
| 125-2830 | ||||||||||||
883 | // Special case when we know we're sorting vertically. We can assume that all indexes for columns | - | ||||||||||||
884 | // are displaced the same way, and therefore we only need to track an index from one column per | - | ||||||||||||
885 | // row with a QPersistentModelIndex together with the length of items to the right of it | - | ||||||||||||
886 | // which are displaced the same way. | - | ||||||||||||
887 | // An algorithm which contains the same assumption is used to process layoutChanged. | - | ||||||||||||
888 | savedPersistentRowLengths = qSelectionPersistentRowLengths(ranges); | - | ||||||||||||
889 | savedPersistentCurrentRowLengths = qSelectionPersistentRowLengths(currentSelection); | - | ||||||||||||
890 | } else { executed 125 times by 9 tests: end of block Executed by:
| 125 | ||||||||||||
891 | savedPersistentIndexes = qSelectionPersistentindexes(qSelectionIndexes<QVector<QPersistentModelIndex>>(ranges); | - | ||||||||||||
892 | savedPersistentCurrentIndexes = qSelectionPersistentindexes(qSelectionIndexes<QVector<QPersistentModelIndex>>(currentSelection); | - | ||||||||||||
893 | } executed 2830 times by 16 tests: end of block Executed by:
| 2830 | ||||||||||||
894 | } | - | ||||||||||||
895 | /*! | - | ||||||||||||
896 | \internal | - | ||||||||||||
897 | */ | - | ||||||||||||
898 | static QItemSelection mergeRowLengths(const QVector<QPair<QPersistentModelIndex, uint> > &rowLengths) | - | ||||||||||||
899 | { | - | ||||||||||||
900 | if (rowLengths.isEmpty()) | - | ||||||||||||
901 | return QItemSelection(); | - | ||||||||||||
902 | - | |||||||||||||
903 | QItemSelection result; | - | ||||||||||||
904 | int i = 0; | - | ||||||||||||
905 | while (i < rowLengths.count()) { | - | ||||||||||||
906 | const QPersistentModelIndex &tl = rowLengths.at(i).first; | - | ||||||||||||
907 | if (!tl.isValid()) { | - | ||||||||||||
908 | ++i; | - | ||||||||||||
909 | continue; | - | ||||||||||||
910 | } | - | ||||||||||||
911 | QPersistentModelIndex br = tl; | - | ||||||||||||
912 | const uint length = rowLengths.at(i).second; | - | ||||||||||||
913 | while (++i < rowLengths.count()) { | - | ||||||||||||
914 | const QPersistentModelIndex &next = rowLengths.at(i).first; | - | ||||||||||||
915 | if (!next.isValid()) | - | ||||||||||||
916 | continue; | - | ||||||||||||
917 | const uint nextLength = rowLengths.at(i).second; | - | ||||||||||||
918 | if ((nextLength == length) | - | ||||||||||||
919 | && (next.row() == br.row() + 1) | - | ||||||||||||
920 | && (next.parent() == br.parent())) { | - | ||||||||||||
921 | br = next; | - | ||||||||||||
922 | } else { | - | ||||||||||||
923 | break; | - | ||||||||||||
924 | } | - | ||||||||||||
925 | } | - | ||||||||||||
926 | result.append(QItemSelectionRange(tl, br.sibling(br.row(), length - 1))); | - | ||||||||||||
927 | } | - | ||||||||||||
928 | return result; | - | ||||||||||||
929 | } | - | ||||||||||||
930 | - | |||||||||||||
931 | /*! | - | ||||||||||||
932 | \internal | - | ||||||||||||
933 | - | |||||||||||||
934 | Merges \a indexes into an item selection made up of ranges. | - | ||||||||||||
935 | Assumes that the indexes are sorted. | - | ||||||||||||
936 | */ | - | ||||||||||||
937 | static QItemSelection mergeIndexes(const QVector<QPersistentModelIndex> &indexes) | - | ||||||||||||
938 | { | - | ||||||||||||
939 | QItemSelection colSpans; | - | ||||||||||||
940 | // merge columns | - | ||||||||||||
941 | int i = 0; | - | ||||||||||||
942 | while (i < indexes.count()) { | - | ||||||||||||
943 | const QPersistentModelIndex &tl = indexes.at(i); | - | ||||||||||||
944 | if (!tl.isValid()) { | - | ||||||||||||
945 | ++i; | - | ||||||||||||
946 | continue; | - | ||||||||||||
947 | } | - | ||||||||||||
948 | QPersistentModelIndex br = tl; | - | ||||||||||||
949 | QModelIndex brParent = br.parent(); | - | ||||||||||||
950 | int brRow = br.row(); | - | ||||||||||||
951 | int brColumn = br.column(); | - | ||||||||||||
952 | while (++i < indexes.count()) { | - | ||||||||||||
953 | const QPersistentModelIndex &next = indexes.at(i); | - | ||||||||||||
954 | if (!next.isValid()) | - | ||||||||||||
955 | continue; | - | ||||||||||||
956 | const QModelIndex nextParent = next.parent(); | - | ||||||||||||
957 | const int nextRow = next.row(); | - | ||||||||||||
958 | const int nextColumn = next.column(); | - | ||||||||||||
959 | if ((nextParent == brParent) | - | ||||||||||||
960 | && (nextRow == brRow) | - | ||||||||||||
961 | && (nextColumn == brColumn + 1)) { | - | ||||||||||||
962 | br = next; | - | ||||||||||||
963 | brParent = nextParent; | - | ||||||||||||
964 | brRow = nextRow; | - | ||||||||||||
965 | brColumn = nextColumn; | - | ||||||||||||
966 | } else { | - | ||||||||||||
967 | break; | - | ||||||||||||
968 | } | - | ||||||||||||
969 | } | - | ||||||||||||
970 | colSpans.append(QItemSelectionRange(tl, br)); | - | ||||||||||||
971 | } | - | ||||||||||||
972 | // merge rows | - | ||||||||||||
973 | QItemSelection rowSpans; | - | ||||||||||||
974 | i = 0; | - | ||||||||||||
975 | while (i < colSpans.count()) { | - | ||||||||||||
976 | QModelIndex tl = colSpans.at(i).topLeft(); | - | ||||||||||||
977 | QModelIndex br = colSpans.at(i).bottomRight(); | - | ||||||||||||
978 | QModelIndex prevTl = tl; | - | ||||||||||||
979 | while (++i < colSpans.count()) { | - | ||||||||||||
980 | QModelIndex nextTl = colSpans.at(i).topLeft(); | - | ||||||||||||
981 | QModelIndex nextBr = colSpans.at(i).bottomRight(); | - | ||||||||||||
982 | - | |||||||||||||
983 | if (nextTl.parent() != tl.parent()) | - | ||||||||||||
984 | break; // we can't merge selection ranges from different parents | - | ||||||||||||
985 | - | |||||||||||||
986 | if ((nextTl.column() == prevTl.column()) && (nextBr.column() == br.column()) | - | ||||||||||||
987 | && (nextTl.row() == prevTl.row() + 1) && (nextBr.row() == br.row() + 1)) { | - | ||||||||||||
988 | br = nextBr; | - | ||||||||||||
989 | prevTl = nextTl; | - | ||||||||||||
990 | } else { | - | ||||||||||||
991 | break; | - | ||||||||||||
992 | } | - | ||||||||||||
993 | } | - | ||||||||||||
994 | rowSpans.append(QItemSelectionRange(tl, br)); | - | ||||||||||||
995 | } | - | ||||||||||||
996 | return rowSpans; | - | ||||||||||||
997 | } | - | ||||||||||||
998 | - | |||||||||||||
999 | /*! | - | ||||||||||||
1000 | \internal | - | ||||||||||||
1001 | - | |||||||||||||
1002 | Sort predicate function for QItemSelectionModelPrivate::_q_layoutChanged(), | - | ||||||||||||
1003 | sorting by parent first in addition to operator<(). This is to prevent | - | ||||||||||||
1004 | fragmentation of the selection by grouping indexes with the same row, column | - | ||||||||||||
1005 | of different parents next to each other, which may happen when a selection | - | ||||||||||||
1006 | spans sub-trees. | - | ||||||||||||
1007 | */ | - | ||||||||||||
1008 | static bool qt_PersistentModelIndexLessThan(const QPersistentModelIndex &i1, const QPersistentModelIndex &i2) | - | ||||||||||||
1009 | { | - | ||||||||||||
1010 | const QModelIndex parent1 = i1.parent(); | - | ||||||||||||
1011 | const QModelIndex parent2 = i2.parent(); | - | ||||||||||||
1012 | return parent1 == parent2 ? i1 < i2 : parent1 < parent2; | - | ||||||||||||
1013 | } | - | ||||||||||||
1014 | - | |||||||||||||
1015 | /*! | - | ||||||||||||
1016 | \internal | - | ||||||||||||
1017 | - | |||||||||||||
1018 | Merge the selected indexes into selection ranges again. | - | ||||||||||||
1019 | */ | - | ||||||||||||
1020 | void QItemSelectionModelPrivate::_q_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint hint) | - | ||||||||||||
1021 | { | - | ||||||||||||
1022 | // special case for when all indexes are selected | - | ||||||||||||
1023 | if (tableSelected && tableColCount == model->columnCount(tableParent) | - | ||||||||||||
1024 | && tableRowCount == model->rowCount(tableParent)) { | - | ||||||||||||
1025 | ranges.clear(); | - | ||||||||||||
1026 | currentSelection.clear(); | - | ||||||||||||
1027 | int bottom = tableRowCount - 1; | - | ||||||||||||
1028 | int right = tableColCount - 1; | - | ||||||||||||
1029 | QModelIndex tl = model->index(0, 0, tableParent); | - | ||||||||||||
1030 | QModelIndex br = model->index(bottom, right, tableParent); | - | ||||||||||||
1031 | currentSelection << QItemSelectionRange(tl, br); | - | ||||||||||||
1032 | tableParent = QModelIndex(); | - | ||||||||||||
1033 | tableSelected = false; | - | ||||||||||||
1034 | return; | - | ||||||||||||
1035 | } | - | ||||||||||||
1036 | - | |||||||||||||
1037 | if ((hint != QAbstractItemModel::VerticalSortHint && savedPersistentCurrentIndexes.isEmpty() && savedPersistentIndexes.isEmpty()) | - | ||||||||||||
1038 | || (hint == QAbstractItemModel::VerticalSortHint && savedPersistentRowLengths.isEmpty() && savedPersistentCurrentRowLengths.isEmpty())) { | - | ||||||||||||
1039 | // either the selection was actually empty, or we | - | ||||||||||||
1040 | // didn't get the layoutAboutToBeChanged() signal | - | ||||||||||||
1041 | return; | - | ||||||||||||
1042 | } | - | ||||||||||||
1043 | - | |||||||||||||
1044 | // clear the "old" selection | - | ||||||||||||
1045 | ranges.clear(); | - | ||||||||||||
1046 | currentSelection.clear(); | - | ||||||||||||
1047 | - | |||||||||||||
1048 | if (hint != QAbstractItemModel::VerticalSortHint) { | - | ||||||||||||
1049 | // sort the "new" selection, as preparation for merging | - | ||||||||||||
1050 | std::stable_sort(savedPersistentIndexes.begin(), savedPersistentIndexes.end(), | - | ||||||||||||
1051 | qt_PersistentModelIndexLessThan); | - | ||||||||||||
1052 | std::stable_sort(savedPersistentCurrentIndexes.begin(), savedPersistentCurrentIndexes.end(), | - | ||||||||||||
1053 | qt_PersistentModelIndexLessThan); | - | ||||||||||||
1054 | - | |||||||||||||
1055 | // update the selection by merging the individual indexes | - | ||||||||||||
1056 | ranges = mergeIndexes(savedPersistentIndexes); | - | ||||||||||||
1057 | currentSelection = mergeIndexes(savedPersistentCurrentIndexes); | - | ||||||||||||
1058 | - | |||||||||||||
1059 | // release the persistent indexes | - | ||||||||||||
1060 | savedPersistentIndexes.clear(); | - | ||||||||||||
1061 | savedPersistentCurrentIndexes.clear(); | - | ||||||||||||
1062 | } else { | - | ||||||||||||
1063 | // sort the "new" selection, as preparation for merging | - | ||||||||||||
1064 | std::stable_sort(savedPersistentRowLengths.begin(), savedPersistentRowLengths.end()); | - | ||||||||||||
1065 | std::stable_sort(savedPersistentCurrentRowLengths.begin(), savedPersistentCurrentRowLengths.end()); | - | ||||||||||||
1066 | - | |||||||||||||
1067 | // update the selection by merging the individual indexes | - | ||||||||||||
1068 | ranges = mergeRowLengths(savedPersistentRowLengths); | - | ||||||||||||
1069 | currentSelection = mergeRowLengths(savedPersistentCurrentRowLengths); | - | ||||||||||||
1070 | - | |||||||||||||
1071 | // release the persistent indexes | - | ||||||||||||
1072 | savedPersistentRowLengths.clear(); | - | ||||||||||||
1073 | savedPersistentCurrentRowLengths.clear(); | - | ||||||||||||
1074 | } | - | ||||||||||||
1075 | } | - | ||||||||||||
1076 | - | |||||||||||||
1077 | /*! | - | ||||||||||||
1078 | \class QItemSelectionModel | - | ||||||||||||
1079 | \inmodule QtCore | - | ||||||||||||
1080 | - | |||||||||||||
1081 | \brief The QItemSelectionModel class keeps track of a view's selected items. | - | ||||||||||||
1082 | - | |||||||||||||
1083 | \ingroup model-view | - | ||||||||||||
1084 | - | |||||||||||||
1085 | A QItemSelectionModel keeps track of the selected items in a view, or | - | ||||||||||||
1086 | in several views onto the same model. It also keeps track of the | - | ||||||||||||
1087 | currently selected item in a view. | - | ||||||||||||
1088 | - | |||||||||||||
1089 | The QItemSelectionModel class is one of the \l{Model/View Classes} | - | ||||||||||||
1090 | and is part of Qt's \l{Model/View Programming}{model/view framework}. | - | ||||||||||||
1091 | - | |||||||||||||
1092 | The selected items are stored using ranges. Whenever you want to | - | ||||||||||||
1093 | modify the selected items use select() and provide either a | - | ||||||||||||
1094 | QItemSelection, or a QModelIndex and a QItemSelectionModel::SelectionFlag. | - | ||||||||||||
1095 | - | |||||||||||||
1096 | The QItemSelectionModel takes a two layer approach to selection | - | ||||||||||||
1097 | management, dealing with both selected items that have been committed | - | ||||||||||||
1098 | and items that are part of the current selection. The current | - | ||||||||||||
1099 | selected items are part of the current interactive selection (for | - | ||||||||||||
1100 | example with rubber-band selection or keyboard-shift selections). | - | ||||||||||||
1101 | - | |||||||||||||
1102 | To update the currently selected items, use the bitwise OR of | - | ||||||||||||
1103 | QItemSelectionModel::Current and any of the other SelectionFlags. | - | ||||||||||||
1104 | If you omit the QItemSelectionModel::Current command, a new current | - | ||||||||||||
1105 | selection will be created, and the previous one added to the whole | - | ||||||||||||
1106 | selection. All functions operate on both layers; for example, | - | ||||||||||||
1107 | \l {QTableWidget::selectedItems()}{selecteditems()} will return items from both layers. | - | ||||||||||||
1108 | - | |||||||||||||
1109 | \note Since 5.5, \l{QItemSelectionModel::model()}{model}, | - | ||||||||||||
1110 | \l{QItemSelectionModel::hasSelection()}{hasSelection}, and | - | ||||||||||||
1111 | \l{QItemSelectionModel::currentIndex()}{currentIndex} are meta-object properties. | - | ||||||||||||
1112 | - | |||||||||||||
1113 | \sa {Model/View Programming}, QAbstractItemModel, {Chart Example} | - | ||||||||||||
1114 | */ | - | ||||||||||||
1115 | - | |||||||||||||
1116 | /*! | - | ||||||||||||
1117 | Constructs a selection model that operates on the specified item \a model. | - | ||||||||||||
1118 | */ | - | ||||||||||||
1119 | QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model) | - | ||||||||||||
1120 | : QObject(*new QItemSelectionModelPrivate, model) | - | ||||||||||||
1121 | { | - | ||||||||||||
1122 | d_func()->initModel(model); | - | ||||||||||||
1123 | } | - | ||||||||||||
1124 | - | |||||||||||||
1125 | /*! | - | ||||||||||||
1126 | Constructs a selection model that operates on the specified item \a model with \a parent. | - | ||||||||||||
1127 | */ | - | ||||||||||||
1128 | QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent) | - | ||||||||||||
1129 | : QObject(*new QItemSelectionModelPrivate, parent) | - | ||||||||||||
1130 | { | - | ||||||||||||
1131 | d_func()->initModel(model); | - | ||||||||||||
1132 | } | - | ||||||||||||
1133 | - | |||||||||||||
1134 | /*! | - | ||||||||||||
1135 | \internal | - | ||||||||||||
1136 | */ | - | ||||||||||||
1137 | QItemSelectionModel::QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model) | - | ||||||||||||
1138 | : QObject(dd, model) | - | ||||||||||||
1139 | { | - | ||||||||||||
1140 | dd.initModel(model); | - | ||||||||||||
1141 | } | - | ||||||||||||
1142 | - | |||||||||||||
1143 | /*! | - | ||||||||||||
1144 | Destroys the selection model. | - | ||||||||||||
1145 | */ | - | ||||||||||||
1146 | QItemSelectionModel::~QItemSelectionModel() | - | ||||||||||||
1147 | { | - | ||||||||||||
1148 | } | - | ||||||||||||
1149 | - | |||||||||||||
1150 | /*! | - | ||||||||||||
1151 | Selects the model item \a index using the specified \a command, and emits | - | ||||||||||||
1152 | selectionChanged(). | - | ||||||||||||
1153 | - | |||||||||||||
1154 | \sa QItemSelectionModel::SelectionFlags | - | ||||||||||||
1155 | */ | - | ||||||||||||
1156 | void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
1157 | { | - | ||||||||||||
1158 | QItemSelection selection(index, index); | - | ||||||||||||
1159 | select(selection, command); | - | ||||||||||||
1160 | } | - | ||||||||||||
1161 | - | |||||||||||||
1162 | /*! | - | ||||||||||||
1163 | \fn void QItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) | - | ||||||||||||
1164 | - | |||||||||||||
1165 | This signal is emitted whenever the current item changes. The \a previous | - | ||||||||||||
1166 | model item index is replaced by the \a current index as the selection's | - | ||||||||||||
1167 | current item. | - | ||||||||||||
1168 | - | |||||||||||||
1169 | Note that this signal will not be emitted when the item model is reset. | - | ||||||||||||
1170 | - | |||||||||||||
1171 | \sa currentIndex(), setCurrentIndex(), selectionChanged() | - | ||||||||||||
1172 | */ | - | ||||||||||||
1173 | - | |||||||||||||
1174 | /*! | - | ||||||||||||
1175 | \fn void QItemSelectionModel::currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous) | - | ||||||||||||
1176 | - | |||||||||||||
1177 | This signal is emitted if the \a current item changes and its column is | - | ||||||||||||
1178 | different to the column of the \a previous current item. | - | ||||||||||||
1179 | - | |||||||||||||
1180 | Note that this signal will not be emitted when the item model is reset. | - | ||||||||||||
1181 | - | |||||||||||||
1182 | \sa currentChanged(), currentRowChanged(), currentIndex(), setCurrentIndex() | - | ||||||||||||
1183 | */ | - | ||||||||||||
1184 | - | |||||||||||||
1185 | /*! | - | ||||||||||||
1186 | \fn void QItemSelectionModel::currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) | - | ||||||||||||
1187 | - | |||||||||||||
1188 | This signal is emitted if the \a current item changes and its row is | - | ||||||||||||
1189 | different to the row of the \a previous current item. | - | ||||||||||||
1190 | - | |||||||||||||
1191 | Note that this signal will not be emitted when the item model is reset. | - | ||||||||||||
1192 | - | |||||||||||||
1193 | \sa currentChanged(), currentColumnChanged(), currentIndex(), setCurrentIndex() | - | ||||||||||||
1194 | */ | - | ||||||||||||
1195 | - | |||||||||||||
1196 | /*! | - | ||||||||||||
1197 | \fn void QItemSelectionModel::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) | - | ||||||||||||
1198 | - | |||||||||||||
1199 | This signal is emitted whenever the selection changes. The change in the | - | ||||||||||||
1200 | selection is represented as an item selection of \a deselected items and | - | ||||||||||||
1201 | an item selection of \a selected items. | - | ||||||||||||
1202 | - | |||||||||||||
1203 | Note the that the current index changes independently from the selection. | - | ||||||||||||
1204 | Also note that this signal will not be emitted when the item model is reset. | - | ||||||||||||
1205 | - | |||||||||||||
1206 | \sa select(), currentChanged() | - | ||||||||||||
1207 | */ | - | ||||||||||||
1208 | - | |||||||||||||
1209 | /*! | - | ||||||||||||
1210 | \fn void QItemSelectionModel::modelChanged(QAbstractItemModel *model) | - | ||||||||||||
1211 | \since 5.5 | - | ||||||||||||
1212 | - | |||||||||||||
1213 | This signal is emitted when the \a model is successfully set with setModel(). | - | ||||||||||||
1214 | - | |||||||||||||
1215 | \sa model(), setModel() | - | ||||||||||||
1216 | */ | - | ||||||||||||
1217 | - | |||||||||||||
1218 | - | |||||||||||||
1219 | /*! | - | ||||||||||||
1220 | \enum QItemSelectionModel::SelectionFlag | - | ||||||||||||
1221 | - | |||||||||||||
1222 | This enum describes the way the selection model will be updated. | - | ||||||||||||
1223 | - | |||||||||||||
1224 | \value NoUpdate No selection will be made. | - | ||||||||||||
1225 | \value Clear The complete selection will be cleared. | - | ||||||||||||
1226 | \value Select All specified indexes will be selected. | - | ||||||||||||
1227 | \value Deselect All specified indexes will be deselected. | - | ||||||||||||
1228 | \value Toggle All specified indexes will be selected or | - | ||||||||||||
1229 | deselected depending on their current state. | - | ||||||||||||
1230 | \value Current The current selection will be updated. | - | ||||||||||||
1231 | \value Rows All indexes will be expanded to span rows. | - | ||||||||||||
1232 | \value Columns All indexes will be expanded to span columns. | - | ||||||||||||
1233 | \value SelectCurrent A combination of Select and Current, provided for | - | ||||||||||||
1234 | convenience. | - | ||||||||||||
1235 | \value ToggleCurrent A combination of Toggle and Current, provided for | - | ||||||||||||
1236 | convenience. | - | ||||||||||||
1237 | \value ClearAndSelect A combination of Clear and Select, provided for | - | ||||||||||||
1238 | convenience. | - | ||||||||||||
1239 | */ | - | ||||||||||||
1240 | - | |||||||||||||
1241 | /*! | - | ||||||||||||
1242 | Selects the item \a selection using the specified \a command, and emits | - | ||||||||||||
1243 | selectionChanged(). | - | ||||||||||||
1244 | - | |||||||||||||
1245 | \sa QItemSelectionModel::SelectionFlag | - | ||||||||||||
1246 | */ | - | ||||||||||||
1247 | void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
1248 | { | - | ||||||||||||
1249 | Q_D(QItemSelectionModel); | - | ||||||||||||
1250 | if (!d->model) { | - | ||||||||||||
1251 | qWarning("QItemSelectionModel: Selecting when no model has been set will result in a no-op."); | - | ||||||||||||
1252 | return; | - | ||||||||||||
1253 | } | - | ||||||||||||
1254 | if (command == NoUpdate) | - | ||||||||||||
1255 | return; | - | ||||||||||||
1256 | - | |||||||||||||
1257 | // store old selection | - | ||||||||||||
1258 | QItemSelection sel = selection; | - | ||||||||||||
1259 | // If d->ranges is non-empty when the source model is reset the persistent indexes | - | ||||||||||||
1260 | // it contains will be invalid. We can't clear them in a modelReset slot because that might already | - | ||||||||||||
1261 | // be too late if another model observer is connected to the same modelReset slot and is invoked first | - | ||||||||||||
1262 | // it might call select() on this selection model before any such QItemSelectionModelPrivate::_q_modelReset() slot | - | ||||||||||||
1263 | // is invoked, so it would not be cleared yet. We clear it invalid ranges in it here. | - | ||||||||||||
1264 | QItemSelection::iterator it = d->ranges.begin(); | - | ||||||||||||
1265 | while (it != d->ranges.end()) { | - | ||||||||||||
1266 | if (!it->isValid()) | - | ||||||||||||
1267 | it = d->ranges.erase(it); | - | ||||||||||||
1268 | else | - | ||||||||||||
1269 | ++it; | - | ||||||||||||
1270 | } | - | ||||||||||||
1271 | - | |||||||||||||
1272 | QItemSelection old = d->ranges; | - | ||||||||||||
1273 | old.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1274 | - | |||||||||||||
1275 | // expand selection according to SelectionBehavior | - | ||||||||||||
1276 | if (command & Rows || command & Columns) | - | ||||||||||||
1277 | sel = d->expandSelection(sel, command); | - | ||||||||||||
1278 | - | |||||||||||||
1279 | // clear ranges and currentSelection | - | ||||||||||||
1280 | if (command & Clear) { | - | ||||||||||||
1281 | d->ranges.clear(); | - | ||||||||||||
1282 | d->currentSelection.clear(); | - | ||||||||||||
1283 | } | - | ||||||||||||
1284 | - | |||||||||||||
1285 | // merge and clear currentSelection if Current was not set (ie. start new currentSelection) | - | ||||||||||||
1286 | if (!(command & Current)) | - | ||||||||||||
1287 | d->finalize(); | - | ||||||||||||
1288 | - | |||||||||||||
1289 | // update currentSelection | - | ||||||||||||
1290 | if (command & Toggle || command & Select || command & Deselect) { | - | ||||||||||||
1291 | d->currentCommand = command; | - | ||||||||||||
1292 | d->currentSelection = sel; | - | ||||||||||||
1293 | } | - | ||||||||||||
1294 | - | |||||||||||||
1295 | // generate new selection, compare with old and emit selectionChanged() | - | ||||||||||||
1296 | QItemSelection newSelection = d->ranges; | - | ||||||||||||
1297 | newSelection.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1298 | emitSelectionChanged(newSelection, old); | - | ||||||||||||
1299 | } | - | ||||||||||||
1300 | - | |||||||||||||
1301 | /*! | - | ||||||||||||
1302 | Clears the selection model. Emits selectionChanged() and currentChanged(). | - | ||||||||||||
1303 | */ | - | ||||||||||||
1304 | void QItemSelectionModel::clear() | - | ||||||||||||
1305 | { | - | ||||||||||||
1306 | clearSelection(); | - | ||||||||||||
1307 | clearCurrentIndex(); | - | ||||||||||||
1308 | } | - | ||||||||||||
1309 | - | |||||||||||||
1310 | /*! | - | ||||||||||||
1311 | Clears the current index. Emits currentChanged(). | - | ||||||||||||
1312 | */ | - | ||||||||||||
1313 | void QItemSelectionModel::clearCurrentIndex() | - | ||||||||||||
1314 | { | - | ||||||||||||
1315 | Q_D(QItemSelectionModel); | - | ||||||||||||
1316 | QModelIndex previous = d->currentIndex; | - | ||||||||||||
1317 | d->currentIndex = QModelIndex(); | - | ||||||||||||
1318 | if (previous.isValid()) { | - | ||||||||||||
1319 | emit currentChanged(d->currentIndex, previous); | - | ||||||||||||
1320 | emit currentRowChanged(d->currentIndex, previous); | - | ||||||||||||
1321 | emit currentColumnChanged(d->currentIndex, previous); | - | ||||||||||||
1322 | } | - | ||||||||||||
1323 | } | - | ||||||||||||
1324 | - | |||||||||||||
1325 | /*! | - | ||||||||||||
1326 | Clears the selection model. Does not emit any signals. | - | ||||||||||||
1327 | */ | - | ||||||||||||
1328 | void QItemSelectionModel::reset() | - | ||||||||||||
1329 | { | - | ||||||||||||
1330 | const QSignalBlocker blocker(this); | - | ||||||||||||
1331 | clear(); | - | ||||||||||||
1332 | } | - | ||||||||||||
1333 | - | |||||||||||||
1334 | /*! | - | ||||||||||||
1335 | \since 4.2 | - | ||||||||||||
1336 | Clears the selection in the selection model. Emits selectionChanged(). | - | ||||||||||||
1337 | */ | - | ||||||||||||
1338 | void QItemSelectionModel::clearSelection() | - | ||||||||||||
1339 | { | - | ||||||||||||
1340 | Q_D(QItemSelectionModel); | - | ||||||||||||
1341 | if (d->ranges.count() == 0 && d->currentSelection.count() == 0) | - | ||||||||||||
1342 | return; | - | ||||||||||||
1343 | - | |||||||||||||
1344 | select(QItemSelection(), Clear); | - | ||||||||||||
1345 | } | - | ||||||||||||
1346 | - | |||||||||||||
1347 | - | |||||||||||||
1348 | /*! | - | ||||||||||||
1349 | Sets the model item \a index to be the current item, and emits | - | ||||||||||||
1350 | currentChanged(). The current item is used for keyboard navigation and | - | ||||||||||||
1351 | focus indication; it is independent of any selected items, although a | - | ||||||||||||
1352 | selected item can also be the current item. | - | ||||||||||||
1353 | - | |||||||||||||
1354 | Depending on the specified \a command, the \a index can also become part | - | ||||||||||||
1355 | of the current selection. | - | ||||||||||||
1356 | \sa select() | - | ||||||||||||
1357 | */ | - | ||||||||||||
1358 | void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
1359 | { | - | ||||||||||||
1360 | Q_D(QItemSelectionModel); | - | ||||||||||||
1361 | if (!d->model) { | - | ||||||||||||
1362 | qWarning("QItemSelectionModel: Setting the current index when no model has been set will result in a no-op."); | - | ||||||||||||
1363 | return; | - | ||||||||||||
1364 | } | - | ||||||||||||
1365 | if (index == d->currentIndex) { | - | ||||||||||||
1366 | if (command != NoUpdate) | - | ||||||||||||
1367 | select(index, command); // select item | - | ||||||||||||
1368 | return; | - | ||||||||||||
1369 | } | - | ||||||||||||
1370 | QPersistentModelIndex previous = d->currentIndex; | - | ||||||||||||
1371 | d->currentIndex = index; // set current before emitting selection changed below | - | ||||||||||||
1372 | if (command != NoUpdate) | - | ||||||||||||
1373 | select(d->currentIndex, command); // select item | - | ||||||||||||
1374 | emit currentChanged(d->currentIndex, previous); | - | ||||||||||||
1375 | if (d->currentIndex.row() != previous.row() || | - | ||||||||||||
1376 | d->currentIndex.parent() != previous.parent()) | - | ||||||||||||
1377 | emit currentRowChanged(d->currentIndex, previous); | - | ||||||||||||
1378 | if (d->currentIndex.column() != previous.column() || | - | ||||||||||||
1379 | d->currentIndex.parent() != previous.parent()) | - | ||||||||||||
1380 | emit currentColumnChanged(d->currentIndex, previous); | - | ||||||||||||
1381 | } | - | ||||||||||||
1382 | - | |||||||||||||
1383 | /*! | - | ||||||||||||
1384 | Returns the model item index for the current item, or an invalid index | - | ||||||||||||
1385 | if there is no current item. | - | ||||||||||||
1386 | */ | - | ||||||||||||
1387 | QModelIndex QItemSelectionModel::currentIndex() const | - | ||||||||||||
1388 | { | - | ||||||||||||
1389 | return static_cast<QModelIndex>(d_func()->currentIndex); | - | ||||||||||||
1390 | } | - | ||||||||||||
1391 | - | |||||||||||||
1392 | /*! | - | ||||||||||||
1393 | Returns \c true if the given model item \a index is selected. | - | ||||||||||||
1394 | */ | - | ||||||||||||
1395 | bool QItemSelectionModel::isSelected(const QModelIndex &index) const | - | ||||||||||||
1396 | { | - | ||||||||||||
1397 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1398 | if (d->model != index.model() || !index.isValid()) | - | ||||||||||||
1399 | return false; | - | ||||||||||||
1400 | - | |||||||||||||
1401 | bool selected = false; | - | ||||||||||||
1402 | // search model ranges | - | ||||||||||||
1403 | QList<QItemSelectionRange>::const_iterator it = d->ranges.begin(); | - | ||||||||||||
1404 | for (; it != d->ranges.end(); ++it) { | - | ||||||||||||
1405 | if ((*it).isValid() && (*it).contains(index)) { | - | ||||||||||||
1406 | selected = true; | - | ||||||||||||
1407 | break; | - | ||||||||||||
1408 | } | - | ||||||||||||
1409 | } | - | ||||||||||||
1410 | - | |||||||||||||
1411 | // check currentSelection | - | ||||||||||||
1412 | if (d->currentSelection.count()) { | - | ||||||||||||
1413 | if ((d->currentCommand & Deselect) && selected) | - | ||||||||||||
1414 | selected = !d->currentSelection.contains(index); | - | ||||||||||||
1415 | else if (d->currentCommand & Toggle) | - | ||||||||||||
1416 | selected ^= d->currentSelection.contains(index); | - | ||||||||||||
1417 | else if ((d->currentCommand & Select) && !selected) | - | ||||||||||||
1418 | selected = d->currentSelection.contains(index); | - | ||||||||||||
1419 | } | - | ||||||||||||
1420 | - | |||||||||||||
1421 | if (selected) { | - | ||||||||||||
1422 | Qt::ItemFlags flags = d->model->flags(index); | - | ||||||||||||
1423 | return (flags & Qt::ItemIsSelectable); | - | ||||||||||||
1424 | } | - | ||||||||||||
1425 | - | |||||||||||||
1426 | return false; | - | ||||||||||||
1427 | } | - | ||||||||||||
1428 | - | |||||||||||||
1429 | /*! | - | ||||||||||||
1430 | Returns \c true if all items are selected in the \a row with the given | - | ||||||||||||
1431 | \a parent. | - | ||||||||||||
1432 | - | |||||||||||||
1433 | Note that this function is usually faster than calling isSelected() | - | ||||||||||||
1434 | on all items in the same row and that unselectable items are | - | ||||||||||||
1435 | ignored. | - | ||||||||||||
1436 | */ | - | ||||||||||||
1437 | bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) const | - | ||||||||||||
1438 | { | - | ||||||||||||
1439 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1440 | if (!d->model) | - | ||||||||||||
1441 | return false; | - | ||||||||||||
1442 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
1443 | return false; | - | ||||||||||||
1444 | - | |||||||||||||
1445 | // return false if row exist in currentSelection (Deselect) | - | ||||||||||||
1446 | if (d->currentCommand & Deselect && d->currentSelection.count()) { | - | ||||||||||||
1447 | for (int i=0; i<d->currentSelection.count(); ++i) { | - | ||||||||||||
1448 | if (d->currentSelection.at(i).parent() == parent && | - | ||||||||||||
1449 | row >= d->currentSelection.at(i).top() && | - | ||||||||||||
1450 | row <= d->currentSelection.at(i).bottom()) | - | ||||||||||||
1451 | return false; | - | ||||||||||||
1452 | } | - | ||||||||||||
1453 | } | - | ||||||||||||
1454 | // return false if ranges in both currentSelection and ranges | - | ||||||||||||
1455 | // intersect and have the same row contained | - | ||||||||||||
1456 | if (d->currentCommand & Toggle && d->currentSelection.count()) { | - | ||||||||||||
1457 | for (int i=0; i<d->currentSelection.count(); ++i) | - | ||||||||||||
1458 | if (d->currentSelection.at(i).top() <= row && | - | ||||||||||||
1459 | d->currentSelection.at(i).bottom() >= row) | - | ||||||||||||
1460 | for (int j=0; j<d->ranges.count(); ++j) | - | ||||||||||||
1461 | if (d->ranges.at(j).top() <= row && d->ranges.at(j).bottom() >= row | - | ||||||||||||
1462 | && d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) | - | ||||||||||||
1463 | return false; | - | ||||||||||||
1464 | } | - | ||||||||||||
1465 | // add ranges and currentSelection and check through them all | - | ||||||||||||
1466 | QList<QItemSelectionRange>::const_iterator it; | - | ||||||||||||
1467 | QList<QItemSelectionRange> joined = d->ranges; | - | ||||||||||||
1468 | if (d->currentSelection.count()) | - | ||||||||||||
1469 | joined += d->currentSelection; | - | ||||||||||||
1470 | int colCount = d->model->columnCount(parent); | - | ||||||||||||
1471 | for (int column = 0; column < colCount; ++column) { | - | ||||||||||||
1472 | for (it = joined.constBegin(); it != joined.constEnd(); ++it) { | - | ||||||||||||
1473 | if ((*it).contains(row, column, parent)) { | - | ||||||||||||
1474 | bool selectable = false; | - | ||||||||||||
1475 | for (int i = column; !selectable && i <= (*it).right(); ++i) { | - | ||||||||||||
1476 | Qt::ItemFlags flags = d->model->index(row, i, parent).flags(); | - | ||||||||||||
1477 | selectable = flags & Qt::ItemIsSelectable; | - | ||||||||||||
1478 | } | - | ||||||||||||
1479 | if (selectable){ | - | ||||||||||||
1480 | column = qMax(column, (*it).right()); | - | ||||||||||||
1481 | break; | - | ||||||||||||
1482 | } | - | ||||||||||||
1483 | } | - | ||||||||||||
1484 | } | - | ||||||||||||
1485 | if (it == joined.constEnd()) | - | ||||||||||||
1486 | return false; | - | ||||||||||||
1487 | } | - | ||||||||||||
1488 | return colCount > 0; // no columns means no selected items | - | ||||||||||||
1489 | } | - | ||||||||||||
1490 | - | |||||||||||||
1491 | /*! | - | ||||||||||||
1492 | Returns \c true if all items are selected in the \a column with the given | - | ||||||||||||
1493 | \a parent. | - | ||||||||||||
1494 | - | |||||||||||||
1495 | Note that this function is usually faster than calling isSelected() | - | ||||||||||||
1496 | on all items in the same column and that unselectable items are | - | ||||||||||||
1497 | ignored. | - | ||||||||||||
1498 | */ | - | ||||||||||||
1499 | bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent) const | - | ||||||||||||
1500 | { | - | ||||||||||||
1501 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1502 | if (!d->model) | - | ||||||||||||
1503 | return false; | - | ||||||||||||
1504 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
1505 | return false; | - | ||||||||||||
1506 | - | |||||||||||||
1507 | // return false if column exist in currentSelection (Deselect) | - | ||||||||||||
1508 | if (d->currentCommand & Deselect && d->currentSelection.count()) { | - | ||||||||||||
1509 | for (int i = 0; i < d->currentSelection.count(); ++i) { | - | ||||||||||||
1510 | if (d->currentSelection.at(i).parent() == parent && | - | ||||||||||||
1511 | column >= d->currentSelection.at(i).left() && | - | ||||||||||||
1512 | column <= d->currentSelection.at(i).right()) | - | ||||||||||||
1513 | return false; | - | ||||||||||||
1514 | } | - | ||||||||||||
1515 | } | - | ||||||||||||
1516 | // return false if ranges in both currentSelection and the selection model | - | ||||||||||||
1517 | // intersect and have the same column contained | - | ||||||||||||
1518 | if (d->currentCommand & Toggle && d->currentSelection.count()) { | - | ||||||||||||
1519 | for (int i = 0; i < d->currentSelection.count(); ++i) { | - | ||||||||||||
1520 | if (d->currentSelection.at(i).left() <= column && | - | ||||||||||||
1521 | d->currentSelection.at(i).right() >= column) { | - | ||||||||||||
1522 | for (int j = 0; j < d->ranges.count(); ++j) { | - | ||||||||||||
1523 | if (d->ranges.at(j).left() <= column && d->ranges.at(j).right() >= column | - | ||||||||||||
1524 | && d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) { | - | ||||||||||||
1525 | return false; | - | ||||||||||||
1526 | } | - | ||||||||||||
1527 | } | - | ||||||||||||
1528 | } | - | ||||||||||||
1529 | } | - | ||||||||||||
1530 | } | - | ||||||||||||
1531 | // add ranges and currentSelection and check through them all | - | ||||||||||||
1532 | QList<QItemSelectionRange>::const_iterator it; | - | ||||||||||||
1533 | QList<QItemSelectionRange> joined = d->ranges; | - | ||||||||||||
1534 | if (d->currentSelection.count()) | - | ||||||||||||
1535 | joined += d->currentSelection; | - | ||||||||||||
1536 | int rowCount = d->model->rowCount(parent); | - | ||||||||||||
1537 | for (int row = 0; row < rowCount; ++row) { | - | ||||||||||||
1538 | for (it = joined.constBegin(); it != joined.constEnd(); ++it) { | - | ||||||||||||
1539 | if ((*it).contains(row, column, parent)) { | - | ||||||||||||
1540 | Qt::ItemFlags flags = d->model->index(row, column, parent).flags(); | - | ||||||||||||
1541 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) { | - | ||||||||||||
1542 | row = qMax(row, (*it).bottom()); | - | ||||||||||||
1543 | break; | - | ||||||||||||
1544 | } | - | ||||||||||||
1545 | } | - | ||||||||||||
1546 | } | - | ||||||||||||
1547 | if (it == joined.constEnd()) | - | ||||||||||||
1548 | return false; | - | ||||||||||||
1549 | } | - | ||||||||||||
1550 | return rowCount > 0; // no rows means no selected items | - | ||||||||||||
1551 | } | - | ||||||||||||
1552 | - | |||||||||||||
1553 | /*! | - | ||||||||||||
1554 | Returns \c true if there are any items selected in the \a row with the given | - | ||||||||||||
1555 | \a parent. | - | ||||||||||||
1556 | */ | - | ||||||||||||
1557 | bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent) const | - | ||||||||||||
1558 | { | - | ||||||||||||
1559 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1560 | if (!d->model) | - | ||||||||||||
1561 | return false; | - | ||||||||||||
1562 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
1563 | return false; | - | ||||||||||||
1564 | - | |||||||||||||
1565 | QItemSelection sel = d->ranges; | - | ||||||||||||
1566 | sel.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1567 | for (int i = 0; i < sel.count(); ++i) { | - | ||||||||||||
1568 | QItemSelectionRange range = sel.at(i); | - | ||||||||||||
1569 | if (range.parent() != parent) | - | ||||||||||||
1570 | return false; | - | ||||||||||||
1571 | int top = range.top(); | - | ||||||||||||
1572 | int bottom = range.bottom(); | - | ||||||||||||
1573 | int left = range.left(); | - | ||||||||||||
1574 | int right = range.right(); | - | ||||||||||||
1575 | if (top <= row && bottom >= row) { | - | ||||||||||||
1576 | for (int j = left; j <= right; j++) { | - | ||||||||||||
1577 | const Qt::ItemFlags flags = d->model->index(row, j, parent).flags(); | - | ||||||||||||
1578 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
1579 | return true; | - | ||||||||||||
1580 | } | - | ||||||||||||
1581 | } | - | ||||||||||||
1582 | } | - | ||||||||||||
1583 | - | |||||||||||||
1584 | return false; | - | ||||||||||||
1585 | } | - | ||||||||||||
1586 | - | |||||||||||||
1587 | /*! | - | ||||||||||||
1588 | Returns \c true if there are any items selected in the \a column with the given | - | ||||||||||||
1589 | \a parent. | - | ||||||||||||
1590 | */ | - | ||||||||||||
1591 | bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent) const | - | ||||||||||||
1592 | { | - | ||||||||||||
1593 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1594 | if (!d->model) | - | ||||||||||||
1595 | return false; | - | ||||||||||||
1596 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
1597 | return false; | - | ||||||||||||
1598 | - | |||||||||||||
1599 | QItemSelection sel = d->ranges; | - | ||||||||||||
1600 | sel.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1601 | for (int i = 0; i < sel.count(); ++i) { | - | ||||||||||||
1602 | int left = sel.at(i).left(); | - | ||||||||||||
1603 | int right = sel.at(i).right(); | - | ||||||||||||
1604 | int top = sel.at(i).top(); | - | ||||||||||||
1605 | int bottom = sel.at(i).bottom(); | - | ||||||||||||
1606 | if (left <= column && right >= column) { | - | ||||||||||||
1607 | for (int j = top; j <= bottom; j++) { | - | ||||||||||||
1608 | const Qt::ItemFlags flags = d->model->index(j, column, parent).flags(); | - | ||||||||||||
1609 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
1610 | return true; | - | ||||||||||||
1611 | } | - | ||||||||||||
1612 | } | - | ||||||||||||
1613 | } | - | ||||||||||||
1614 | - | |||||||||||||
1615 | return false; | - | ||||||||||||
1616 | } | - | ||||||||||||
1617 | - | |||||||||||||
1618 | /*! | - | ||||||||||||
1619 | \since 4.2 | - | ||||||||||||
1620 | - | |||||||||||||
1621 | Returns \c true if the selection model contains any selection ranges; | - | ||||||||||||
1622 | otherwise returns \c false. | - | ||||||||||||
1623 | */ | - | ||||||||||||
1624 | bool QItemSelectionModel::hasSelection() const | - | ||||||||||||
1625 | { | - | ||||||||||||
1626 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1627 | if (d->currentCommand & (Toggle | Deselect)) { | - | ||||||||||||
1628 | QItemSelection sel = d->ranges; | - | ||||||||||||
1629 | sel.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1630 | return !sel.isEmpty(); | - | ||||||||||||
1631 | } else { | - | ||||||||||||
1632 | return !(d->ranges.isEmpty() && d->currentSelection.isEmpty()); | - | ||||||||||||
1633 | } | - | ||||||||||||
1634 | } | - | ||||||||||||
1635 | - | |||||||||||||
1636 | /*! | - | ||||||||||||
1637 | Returns a list of all selected model item indexes. The list contains no | - | ||||||||||||
1638 | duplicates, and is not sorted. | - | ||||||||||||
1639 | */ | - | ||||||||||||
1640 | QModelIndexList QItemSelectionModel::selectedIndexes() const | - | ||||||||||||
1641 | { | - | ||||||||||||
1642 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1643 | QItemSelection selected = d->ranges; | - | ||||||||||||
1644 | selected.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1645 | return selected.indexes(); | - | ||||||||||||
1646 | } | - | ||||||||||||
1647 | - | |||||||||||||
1648 | /*! | - | ||||||||||||
1649 | \since 4.2 | - | ||||||||||||
1650 | Returns the indexes in the given \a column for the rows where all columns are selected. | - | ||||||||||||
1651 | - | |||||||||||||
1652 | \sa selectedIndexes(), selectedColumns() | - | ||||||||||||
1653 | */ | - | ||||||||||||
1654 | - | |||||||||||||
1655 | QModelIndexList QItemSelectionModel::selectedRows(int column) const | - | ||||||||||||
1656 | { | - | ||||||||||||
1657 | QModelIndexList indexes; | - | ||||||||||||
1658 | //the QSet contains pairs of parent modelIndex | - | ||||||||||||
1659 | //and row number | - | ||||||||||||
1660 | QSet< QPair<QModelIndex, int> > rowsSeen; | - | ||||||||||||
1661 | - | |||||||||||||
1662 | const QItemSelection ranges = selection(); | - | ||||||||||||
1663 | for (int i = 0; i < ranges.count(); ++i) { | - | ||||||||||||
1664 | const QItemSelectionRange &range = ranges.at(i); | - | ||||||||||||
1665 | QModelIndex parent = range.parent(); | - | ||||||||||||
1666 | for (int row = range.top(); row <= range.bottom(); row++) { | - | ||||||||||||
1667 | QPair<QModelIndex, int> rowDef = qMakePair(parent, row); | - | ||||||||||||
1668 | if (!rowsSeen.contains(rowDef)) { | - | ||||||||||||
1669 | rowsSeen << rowDef; | - | ||||||||||||
1670 | if (isRowSelected(row, parent)) { | - | ||||||||||||
1671 | indexes.append(model()->index(row, column, parent)); | - | ||||||||||||
1672 | } | - | ||||||||||||
1673 | } | - | ||||||||||||
1674 | } | - | ||||||||||||
1675 | } | - | ||||||||||||
1676 | - | |||||||||||||
1677 | return indexes; | - | ||||||||||||
1678 | } | - | ||||||||||||
1679 | - | |||||||||||||
1680 | /*! | - | ||||||||||||
1681 | \since 4.2 | - | ||||||||||||
1682 | Returns the indexes in the given \a row for columns where all rows are selected. | - | ||||||||||||
1683 | - | |||||||||||||
1684 | \sa selectedIndexes(), selectedRows() | - | ||||||||||||
1685 | */ | - | ||||||||||||
1686 | - | |||||||||||||
1687 | QModelIndexList QItemSelectionModel::selectedColumns(int row) const | - | ||||||||||||
1688 | { | - | ||||||||||||
1689 | QModelIndexList indexes; | - | ||||||||||||
1690 | //the QSet contains pairs of parent modelIndex | - | ||||||||||||
1691 | //and column number | - | ||||||||||||
1692 | QSet< QPair<QModelIndex, int> > columnsSeen; | - | ||||||||||||
1693 | - | |||||||||||||
1694 | const QItemSelection ranges = selection(); | - | ||||||||||||
1695 | for (int i = 0; i < ranges.count(); ++i) { | - | ||||||||||||
1696 | const QItemSelectionRange &range = ranges.at(i); | - | ||||||||||||
1697 | QModelIndex parent = range.parent(); | - | ||||||||||||
1698 | for (int column = range.left(); column <= range.right(); column++) { | - | ||||||||||||
1699 | QPair<QModelIndex, int> columnDef = qMakePair(parent, column); | - | ||||||||||||
1700 | if (!columnsSeen.contains(columnDef)) { | - | ||||||||||||
1701 | columnsSeen << columnDef; | - | ||||||||||||
1702 | if (isColumnSelected(column, parent)) { | - | ||||||||||||
1703 | indexes.append(model()->index(row, column, parent)); | - | ||||||||||||
1704 | } | - | ||||||||||||
1705 | } | - | ||||||||||||
1706 | } | - | ||||||||||||
1707 | } | - | ||||||||||||
1708 | - | |||||||||||||
1709 | return indexes; | - | ||||||||||||
1710 | } | - | ||||||||||||
1711 | - | |||||||||||||
1712 | /*! | - | ||||||||||||
1713 | Returns the selection ranges stored in the selection model. | - | ||||||||||||
1714 | */ | - | ||||||||||||
1715 | const QItemSelection QItemSelectionModel::selection() const | - | ||||||||||||
1716 | { | - | ||||||||||||
1717 | Q_D(const QItemSelectionModel); | - | ||||||||||||
1718 | QItemSelection selected = d->ranges; | - | ||||||||||||
1719 | selected.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1720 | int i = 0;// make sure we have no invalid ranges | - | ||||||||||||
1721 | // ### should probably be handled more generic somewhere else | - | ||||||||||||
1722 | while (i<selected.count())auto isNotValid = [](const QItemSelectionRange& range) { | - | ||||||||||||
1723 | if (return !range.isValid(); executed 1284 times by 12 tests: return !range.isValid(); Executed by:
| 1284 | ||||||||||||
1724 | }; | - | ||||||||||||
1725 | - | |||||||||||||
1726 | selected.aterase(i).isValid()) | - | ||||||||||||
++i; | ||||||||||||||
elsestd::remove_if(selected.removeAt(i)); | ||||||||||||||
}begin(), selected.end(), | ||||||||||||||
1727 | isNotValid), | - | ||||||||||||
1728 | selected.end()); | - | ||||||||||||
1729 | return selected; executed 11454 times by 42 tests: return selected; Executed by:
| 11454 | ||||||||||||
1730 | } | - | ||||||||||||
1731 | - | |||||||||||||
1732 | /*! | - | ||||||||||||
1733 | \since 5.5 | - | ||||||||||||
1734 | - | |||||||||||||
1735 | \property QItemSelectionModel::hasSelection | - | ||||||||||||
1736 | \internal | - | ||||||||||||
1737 | */ | - | ||||||||||||
1738 | /*! | - | ||||||||||||
1739 | \since 5.5 | - | ||||||||||||
1740 | - | |||||||||||||
1741 | \property QItemSelectionModel::currentIndex | - | ||||||||||||
1742 | \internal | - | ||||||||||||
1743 | */ | - | ||||||||||||
1744 | /*! | - | ||||||||||||
1745 | \since 5.5 | - | ||||||||||||
1746 | - | |||||||||||||
1747 | \property QItemSelectionModel::selectedIndexes | - | ||||||||||||
1748 | */ | - | ||||||||||||
1749 | - | |||||||||||||
1750 | /*! | - | ||||||||||||
1751 | \since 5.5 | - | ||||||||||||
1752 | - | |||||||||||||
1753 | \property QItemSelectionModel::selection | - | ||||||||||||
1754 | \internal | - | ||||||||||||
1755 | */ | - | ||||||||||||
1756 | /*! | - | ||||||||||||
1757 | \since 5.5 | - | ||||||||||||
1758 | - | |||||||||||||
1759 | \property QItemSelectionModel::model | - | ||||||||||||
1760 | \internal | - | ||||||||||||
1761 | */ | - | ||||||||||||
1762 | /*! | - | ||||||||||||
1763 | \since 5.5 | - | ||||||||||||
1764 | - | |||||||||||||
1765 | Returns the item model operated on by the selection model. | - | ||||||||||||
1766 | */ | - | ||||||||||||
1767 | QAbstractItemModel *QItemSelectionModel::model() | - | ||||||||||||
1768 | { | - | ||||||||||||
1769 | return d_func()->model; | - | ||||||||||||
1770 | } | - | ||||||||||||
1771 | - | |||||||||||||
1772 | /*! | - | ||||||||||||
1773 | Returns the item model operated on by the selection model. | - | ||||||||||||
1774 | */ | - | ||||||||||||
1775 | const QAbstractItemModel *QItemSelectionModel::model() const | - | ||||||||||||
1776 | { | - | ||||||||||||
1777 | return d_func()->model; | - | ||||||||||||
1778 | } | - | ||||||||||||
1779 | - | |||||||||||||
1780 | /*! | - | ||||||||||||
1781 | \since 5.5 | - | ||||||||||||
1782 | - | |||||||||||||
1783 | Sets the model to \a model. The modelChanged() signal will be emitted. | - | ||||||||||||
1784 | - | |||||||||||||
1785 | \sa model(), modelChanged() | - | ||||||||||||
1786 | */ | - | ||||||||||||
1787 | void QItemSelectionModel::setModel(QAbstractItemModel *model) | - | ||||||||||||
1788 | { | - | ||||||||||||
1789 | Q_D(QItemSelectionModel); | - | ||||||||||||
1790 | if (d->model == model) | - | ||||||||||||
1791 | return; | - | ||||||||||||
1792 | - | |||||||||||||
1793 | d->initModel(model); | - | ||||||||||||
1794 | emit modelChanged(model); | - | ||||||||||||
1795 | } | - | ||||||||||||
1796 | - | |||||||||||||
1797 | /*! | - | ||||||||||||
1798 | Compares the two selections \a newSelection and \a oldSelection | - | ||||||||||||
1799 | and emits selectionChanged() with the deselected and selected items. | - | ||||||||||||
1800 | */ | - | ||||||||||||
1801 | void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelection, | - | ||||||||||||
1802 | const QItemSelection &oldSelection) | - | ||||||||||||
1803 | { | - | ||||||||||||
1804 | // if both selections are empty or equal we return | - | ||||||||||||
1805 | if ((oldSelection.isEmpty() && newSelection.isEmpty()) || | - | ||||||||||||
1806 | oldSelection == newSelection) | - | ||||||||||||
1807 | return; | - | ||||||||||||
1808 | - | |||||||||||||
1809 | // if either selection is empty we do not need to compare | - | ||||||||||||
1810 | if (oldSelection.isEmpty() || newSelection.isEmpty()) { | - | ||||||||||||
1811 | emit selectionChanged(newSelection, oldSelection); | - | ||||||||||||
1812 | return; | - | ||||||||||||
1813 | } | - | ||||||||||||
1814 | - | |||||||||||||
1815 | QItemSelection deselected = oldSelection; | - | ||||||||||||
1816 | QItemSelection selected = newSelection; | - | ||||||||||||
1817 | - | |||||||||||||
1818 | // remove equal ranges | - | ||||||||||||
1819 | bool advance; | - | ||||||||||||
1820 | for (int o = 0; o < deselected.count(); ++o) { | - | ||||||||||||
1821 | advance = true; | - | ||||||||||||
1822 | for (int s = 0; s < selected.count() && o < deselected.count();) { | - | ||||||||||||
1823 | if (deselected.at(o) == selected.at(s)) { | - | ||||||||||||
1824 | deselected.removeAt(o); | - | ||||||||||||
1825 | selected.removeAt(s); | - | ||||||||||||
1826 | advance = false; | - | ||||||||||||
1827 | } else { | - | ||||||||||||
1828 | ++s; | - | ||||||||||||
1829 | } | - | ||||||||||||
1830 | } | - | ||||||||||||
1831 | if (advance) | - | ||||||||||||
1832 | ++o; | - | ||||||||||||
1833 | } | - | ||||||||||||
1834 | - | |||||||||||||
1835 | // find intersections | - | ||||||||||||
1836 | QItemSelection intersections; | - | ||||||||||||
1837 | for (int o = 0; o < deselected.count(); ++o) { | - | ||||||||||||
1838 | for (int s = 0; s < selected.count(); ++s) { | - | ||||||||||||
1839 | if (deselected.at(o).intersects(selected.at(s))) | - | ||||||||||||
1840 | intersections.append(deselected.at(o).intersected(selected.at(s))); | - | ||||||||||||
1841 | } | - | ||||||||||||
1842 | } | - | ||||||||||||
1843 | - | |||||||||||||
1844 | // compare remaining ranges with intersections and split them to find deselected and selected | - | ||||||||||||
1845 | for (int i = 0; i < intersections.count(); ++i) { | - | ||||||||||||
1846 | // split deselected | - | ||||||||||||
1847 | for (int o = 0; o < deselected.count();) { | - | ||||||||||||
1848 | if (deselected.at(o).intersects(intersections.at(i))) { | - | ||||||||||||
1849 | QItemSelection::split(deselected.at(o), intersections.at(i), &deselected); | - | ||||||||||||
1850 | deselected.removeAt(o); | - | ||||||||||||
1851 | } else { | - | ||||||||||||
1852 | ++o; | - | ||||||||||||
1853 | } | - | ||||||||||||
1854 | } | - | ||||||||||||
1855 | // split selected | - | ||||||||||||
1856 | for (int s = 0; s < selected.count();) { | - | ||||||||||||
1857 | if (selected.at(s).intersects(intersections.at(i))) { | - | ||||||||||||
1858 | QItemSelection::split(selected.at(s), intersections.at(i), &selected); | - | ||||||||||||
1859 | selected.removeAt(s); | - | ||||||||||||
1860 | } else { | - | ||||||||||||
1861 | ++s; | - | ||||||||||||
1862 | } | - | ||||||||||||
1863 | } | - | ||||||||||||
1864 | } | - | ||||||||||||
1865 | - | |||||||||||||
1866 | if (!selected.isEmpty() || !deselected.isEmpty()) | - | ||||||||||||
1867 | emit selectionChanged(selected, deselected); | - | ||||||||||||
1868 | } | - | ||||||||||||
1869 | - | |||||||||||||
1870 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||
1871 | QDebug operator<<(QDebug dbg, const QItemSelectionRange &range) | - | ||||||||||||
1872 | { | - | ||||||||||||
1873 | QDebugStateSaver saver(dbg); | - | ||||||||||||
1874 | dbg.nospace() << "QItemSelectionRange(" << range.topLeft() | - | ||||||||||||
1875 | << ',' << range.bottomRight() << ')'; | - | ||||||||||||
1876 | return dbg; | - | ||||||||||||
1877 | } | - | ||||||||||||
1878 | #endif | - | ||||||||||||
1879 | - | |||||||||||||
1880 | QT_END_NAMESPACE | - | ||||||||||||
1881 | - | |||||||||||||
1882 | #include "moc_qitemselectionmodel.cpp" | - | ||||||||||||
1883 | - | |||||||||||||
1884 | #endif // QT_NO_ITEMVIEWS | - | ||||||||||||
Source code | Switch to Preprocessed file |