Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/itemmodels/qitemselectionmodel.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | - | |||||||||||||
6 | - | |||||||||||||
7 | bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const | - | ||||||||||||
8 | { | - | ||||||||||||
9 | return (isValid() && other.isValid() | - | ||||||||||||
10 | && parent() == other.parent() | - | ||||||||||||
11 | && model() == other.model() | - | ||||||||||||
12 | && ((top() <= other.top() && bottom() >= other.top()) | - | ||||||||||||
13 | || (top() >= other.top() && top() <= other.bottom())) | - | ||||||||||||
14 | && ((left() <= other.left() && right() >= other.left()) | - | ||||||||||||
15 | || (left() >= other.left() && left() <= other.right()))); | - | ||||||||||||
16 | } | - | ||||||||||||
17 | QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange &other) const | - | ||||||||||||
18 | { | - | ||||||||||||
19 | if (model() == other.model() && parent() == other.parent()) { | - | ||||||||||||
20 | QModelIndex topLeft = model()->index(qMax(top(), other.top()), | - | ||||||||||||
21 | qMax(left(), other.left()), | - | ||||||||||||
22 | other.parent()); | - | ||||||||||||
23 | QModelIndex bottomRight = model()->index(qMin(bottom(), other.bottom()), | - | ||||||||||||
24 | qMin(right(), other.right()), | - | ||||||||||||
25 | other.parent()); | - | ||||||||||||
26 | return QItemSelectionRange(topLeft, bottomRight); | - | ||||||||||||
27 | } | - | ||||||||||||
28 | return QItemSelectionRange(); | - | ||||||||||||
29 | } | - | ||||||||||||
30 | static void rowLengthsFromRange(const QItemSelectionRange &range, QVector<QPair<QPersistentModelIndex, uint> > &result) | - | ||||||||||||
31 | { | - | ||||||||||||
32 | if (range.isValid() && range.model()) { | - | ||||||||||||
33 | const QModelIndex topLeft = range.topLeft(); | - | ||||||||||||
34 | const int bottom = range.bottom(); | - | ||||||||||||
35 | const uint width = range.width(); | - | ||||||||||||
36 | const int column = topLeft.column(); | - | ||||||||||||
37 | for (int row = topLeft.row(); row <= bottom; ++row) { | - | ||||||||||||
38 | - | |||||||||||||
39 | - | |||||||||||||
40 | - | |||||||||||||
41 | result.push_back(qMakePair(QPersistentModelIndex(topLeft.sibling(row, column)), width)); | - | ||||||||||||
42 | } | - | ||||||||||||
43 | } | - | ||||||||||||
44 | } | - | ||||||||||||
45 | - | |||||||||||||
46 | template<typename ModelIndexContainer> | - | ||||||||||||
47 | static void indexesFromRange(const QItemSelectionRange &range, ModelIndexContainer &result) | - | ||||||||||||
48 | { | - | ||||||||||||
49 | if (range.isValid() && range.model()) { | - | ||||||||||||
50 | const QModelIndex topLeft = range.topLeft(); | - | ||||||||||||
51 | const int bottom = range.bottom(); | - | ||||||||||||
52 | const int right = range.right(); | - | ||||||||||||
53 | for (int row = topLeft.row(); row <= bottom; ++row) { | - | ||||||||||||
54 | const QModelIndex columnLeader = topLeft.sibling(row, topLeft.column()); | - | ||||||||||||
55 | for (int column = topLeft.column(); column <= right; ++column) { | - | ||||||||||||
56 | QModelIndex index = columnLeader.sibling(row, column); | - | ||||||||||||
57 | Qt::ItemFlags flags = range.model()->flags(index); | - | ||||||||||||
58 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
59 | result.push_back(index); | - | ||||||||||||
60 | } | - | ||||||||||||
61 | } | - | ||||||||||||
62 | } | - | ||||||||||||
63 | } | - | ||||||||||||
64 | - | |||||||||||||
65 | template<typename ModelIndexContainer> | - | ||||||||||||
66 | static ModelIndexContainer qSelectionIndexes(const QItemSelection &selection) | - | ||||||||||||
67 | { | - | ||||||||||||
68 | ModelIndexContainer result; | - | ||||||||||||
69 | for (const auto &range : selection) | - | ||||||||||||
70 | indexesFromRange(range, result); executed 1046 times by 18 tests: indexesFromRange(range, result); Executed by:
| 1046 | ||||||||||||
71 | return executed 6813 times by 24 tests: result;return result; Executed by:
executed 6813 times by 24 tests: return result; Executed by:
| 6813 | ||||||||||||
72 | } | - | ||||||||||||
73 | - | |||||||||||||
74 | - | |||||||||||||
75 | - | |||||||||||||
76 | - | |||||||||||||
77 | - | |||||||||||||
78 | - | |||||||||||||
79 | bool QItemSelectionRange::isEmpty() const | - | ||||||||||||
80 | { | - | ||||||||||||
81 | if (!isValid() || !model()) | - | ||||||||||||
82 | return true; | - | ||||||||||||
83 | - | |||||||||||||
84 | for (int column = left(); column <= right(); ++column) { | - | ||||||||||||
85 | for (int row = top(); row <= bottom(); ++row) { | - | ||||||||||||
86 | QModelIndex index = model()->index(row, column, parent()); | - | ||||||||||||
87 | Qt::ItemFlags flags = model()->flags(index); | - | ||||||||||||
88 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
89 | return false; | - | ||||||||||||
90 | } | - | ||||||||||||
91 | } | - | ||||||||||||
92 | return true; | - | ||||||||||||
93 | } | - | ||||||||||||
94 | - | |||||||||||||
95 | - | |||||||||||||
96 | - | |||||||||||||
97 | - | |||||||||||||
98 | - | |||||||||||||
99 | QModelIndexList QItemSelectionRange::indexes() const | - | ||||||||||||
100 | { | - | ||||||||||||
101 | QModelIndexList result; | - | ||||||||||||
102 | indexesFromRange(*this, result); | - | ||||||||||||
103 | return result; | - | ||||||||||||
104 | } | - | ||||||||||||
105 | QItemSelection::QItemSelection(const QModelIndex &topLeft, const QModelIndex &bottomRight) | - | ||||||||||||
106 | { | - | ||||||||||||
107 | select(topLeft, bottomRight); | - | ||||||||||||
108 | } | - | ||||||||||||
109 | void QItemSelection::select(const QModelIndex &topLeft, const QModelIndex &bottomRight) | - | ||||||||||||
110 | { | - | ||||||||||||
111 | if (!topLeft.isValid() || !bottomRight.isValid()) | - | ||||||||||||
112 | return; | - | ||||||||||||
113 | - | |||||||||||||
114 | if ((topLeft.model() != bottomRight.model()) | - | ||||||||||||
115 | || topLeft.parent() != bottomRight.parent()) { | - | ||||||||||||
116 | QMessageLogger(__FILE__, 428443, __PRETTY_FUNCTION__).warning("Can't select indexes from different model or with different parents"); | - | ||||||||||||
117 | return; | - | ||||||||||||
118 | } | - | ||||||||||||
119 | if (topLeft.row() > bottomRight.row() || topLeft.column() > bottomRight.column()) { | - | ||||||||||||
120 | int top = qMin(topLeft.row(), bottomRight.row()); | - | ||||||||||||
121 | int bottom = qMax(topLeft.row(), bottomRight.row()); | - | ||||||||||||
122 | int left = qMin(topLeft.column(), bottomRight.column()); | - | ||||||||||||
123 | int right = qMax(topLeft.column(), bottomRight.column()); | - | ||||||||||||
124 | QModelIndex tl = topLeft.sibling(top, left); | - | ||||||||||||
125 | QModelIndex br = bottomRight.sibling(bottom, right); | - | ||||||||||||
126 | append(QItemSelectionRange(tl, br)); | - | ||||||||||||
127 | return; | - | ||||||||||||
128 | } | - | ||||||||||||
129 | append(QItemSelectionRange(topLeft, bottomRight)); | - | ||||||||||||
130 | } | - | ||||||||||||
131 | - | |||||||||||||
132 | - | |||||||||||||
133 | - | |||||||||||||
134 | - | |||||||||||||
135 | - | |||||||||||||
136 | - | |||||||||||||
137 | bool QItemSelection::contains(const QModelIndex &index) const | - | ||||||||||||
138 | { | - | ||||||||||||
139 | if (index.flags() & Qt::ItemIsSelectable) { | - | ||||||||||||
140 | QList<QItemSelectionRange>::const_iterator it = begin(); | - | ||||||||||||
141 | for (; it != end(); ++it) | - | ||||||||||||
142 | if ((*it).contains(index)) | - | ||||||||||||
143 | return true; | - | ||||||||||||
144 | } | - | ||||||||||||
145 | return false; | - | ||||||||||||
146 | } | - | ||||||||||||
147 | - | |||||||||||||
148 | - | |||||||||||||
149 | - | |||||||||||||
150 | - | |||||||||||||
151 | - | |||||||||||||
152 | QModelIndexList QItemSelection::indexes() const | - | ||||||||||||
153 | { | - | ||||||||||||
154 | QModelIndexList result; | 1153 | ||||||||||||
QList<QItemSelectionRange>::const_iterator it = begin(); | ||||||||||||||
for (; it != end(); ++it) | ||||||||||||||
indexesFromRange(*it, result);return executed 1153 times by 17 tests: 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: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
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: return qSelectionIndexes<QModelIndexList>(*this); Executed by:
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;} | ||||||||||||||
156 | - | |||||||||||||
157 | static QVector<QPair<QPersistentModelIndex, uint> > qSelectionPersistentRowLengths(const QItemSelection &sel) | - | ||||||||||||
158 | { | - | ||||||||||||
159 | QVector<QPair<QPersistentModelIndex, uint> > result; | - | ||||||||||||
for (QForeachContainer<typename QtPrivate::remove_reference<decltype(sel)>::type> _container_((sel)); _container_.control && _container_.i != _container_.e; | ||||||||||||||
160 | ++_container_.i, _container_.control ^= 1)for (const QItemSelectionRange &range = *_container_.i; _container_.control; _container_.control = 0: sel) | - | ||||||||||||
161 | rowLengthsFromRange(range, result); executed 15 times by 2 tests: rowLengthsFromRange(range, result); Executed by:
| 15 | ||||||||||||
162 | return executed 250 times by 9 tests: result;return result; Executed by:
executed 250 times by 9 tests: return result; Executed by:
| 250 | ||||||||||||
163 | } | - | ||||||||||||
164 | void QItemSelection::merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
165 | { | - | ||||||||||||
166 | if (other.isEmpty() || | - | ||||||||||||
167 | !(command & QItemSelectionModel::Select || | - | ||||||||||||
168 | command & QItemSelectionModel::Deselect || | - | ||||||||||||
169 | command & QItemSelectionModel::Toggle)) | - | ||||||||||||
170 | return; | - | ||||||||||||
171 | - | |||||||||||||
172 | QItemSelection newSelection = other; | - | ||||||||||||
173 | - | |||||||||||||
174 | QItemSelection intersections; | - | ||||||||||||
175 | QItemSelection::iterator it = newSelection.begin(); | - | ||||||||||||
176 | while (it != newSelection.end()) { | - | ||||||||||||
177 | if (!(*it).isValid()) { | - | ||||||||||||
178 | it = newSelection.erase(it); | - | ||||||||||||
179 | continue; | - | ||||||||||||
180 | } | - | ||||||||||||
181 | for (int t = 0; t < count(); ++t) { | - | ||||||||||||
182 | if ((*it).intersects(at(t))) | - | ||||||||||||
183 | intersections.append(at(t).intersected(*it)); | - | ||||||||||||
184 | } | - | ||||||||||||
185 | ++it; | - | ||||||||||||
186 | } | - | ||||||||||||
187 | - | |||||||||||||
188 | - | |||||||||||||
189 | for (int i = 0; i < intersections.count(); ++i) { | - | ||||||||||||
190 | for (int t = 0; t < count();) { | - | ||||||||||||
191 | if (at(t).intersects(intersections.at(i))) { | - | ||||||||||||
192 | split(at(t), intersections.at(i), this); | - | ||||||||||||
193 | removeAt(t); | - | ||||||||||||
194 | } else { | - | ||||||||||||
195 | ++t; | - | ||||||||||||
196 | } | - | ||||||||||||
197 | } | - | ||||||||||||
198 | - | |||||||||||||
199 | for (int n = 0; (command & QItemSelectionModel::Toggle) && n < newSelection.count();) { | - | ||||||||||||
200 | if (newSelection.at(n).intersects(intersections.at(i))) { | - | ||||||||||||
201 | split(newSelection.at(n), intersections.at(i), &newSelection); | - | ||||||||||||
202 | newSelection.removeAt(n); | - | ||||||||||||
203 | } else { | - | ||||||||||||
204 | ++n; | - | ||||||||||||
205 | } | - | ||||||||||||
206 | } | - | ||||||||||||
207 | } | - | ||||||||||||
208 | - | |||||||||||||
209 | if (!(command & QItemSelectionModel::Deselect)) | - | ||||||||||||
210 | operator+=(newSelection); | - | ||||||||||||
211 | } | - | ||||||||||||
212 | void QItemSelection::split(const QItemSelectionRange &range, | - | ||||||||||||
213 | const QItemSelectionRange &other, QItemSelection *result) | - | ||||||||||||
214 | { | - | ||||||||||||
215 | if (range.parent() != other.parent() || range.model() != other.model()) | - | ||||||||||||
216 | return; | - | ||||||||||||
217 | - | |||||||||||||
218 | QModelIndex parent = other.parent(); | - | ||||||||||||
219 | int top = range.top(); | - | ||||||||||||
220 | int left = range.left(); | - | ||||||||||||
221 | int bottom = range.bottom(); | - | ||||||||||||
222 | int right = range.right(); | - | ||||||||||||
223 | int other_top = other.top(); | - | ||||||||||||
224 | int other_left = other.left(); | - | ||||||||||||
225 | int other_bottom = other.bottom(); | - | ||||||||||||
226 | int other_right = other.right(); | - | ||||||||||||
227 | const QAbstractItemModel *model = range.model(); | - | ||||||||||||
228 | ((!(model)) ? qt_assert("model",__FILE__,572574) : qt_noop()); | - | ||||||||||||
229 | if (other_top > top) { | - | ||||||||||||
230 | QModelIndex tl = model->index(top, left, parent); | - | ||||||||||||
231 | QModelIndex br = model->index(other_top - 1, right, parent); | - | ||||||||||||
232 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
233 | top = other_top; | - | ||||||||||||
234 | } | - | ||||||||||||
235 | if (other_bottom < bottom) { | - | ||||||||||||
236 | QModelIndex tl = model->index(other_bottom + 1, left, parent); | - | ||||||||||||
237 | QModelIndex br = model->index(bottom, right, parent); | - | ||||||||||||
238 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
239 | bottom = other_bottom; | - | ||||||||||||
240 | } | - | ||||||||||||
241 | if (other_left > left) { | - | ||||||||||||
242 | QModelIndex tl = model->index(top, left, parent); | - | ||||||||||||
243 | QModelIndex br = model->index(bottom, other_left - 1, parent); | - | ||||||||||||
244 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
245 | left = other_left; | - | ||||||||||||
246 | } | - | ||||||||||||
247 | if (other_right < right) { | - | ||||||||||||
248 | QModelIndex tl = model->index(top, other_right + 1, parent); | - | ||||||||||||
249 | QModelIndex br = model->index(bottom, right, parent); | - | ||||||||||||
250 | result->append(QItemSelectionRange(tl, br)); | - | ||||||||||||
251 | right = other_right; | - | ||||||||||||
252 | } | - | ||||||||||||
253 | } | - | ||||||||||||
254 | - | |||||||||||||
255 | - | |||||||||||||
256 | void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m) | - | ||||||||||||
257 | { | - | ||||||||||||
258 | struct Cx { | - | ||||||||||||
259 | const char *signal; | - | ||||||||||||
260 | const char *slot; | - | ||||||||||||
261 | }; | - | ||||||||||||
262 | static const Cx connections[] = { | - | ||||||||||||
263 | { qFlagLocation("2""rowsAboutToBeRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "607""609"), | - | ||||||||||||
264 | qFlagLocation("1""_q_rowsAboutToBeRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "608""610") }, | - | ||||||||||||
265 | { qFlagLocation("2""columnsAboutToBeRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "609""611"), | - | ||||||||||||
266 | qFlagLocation("1""_q_columnsAboutToBeRemoved(QModelIndex,int,int)" "\0" __FILE__ ":" "610""612") }, | - | ||||||||||||
267 | { qFlagLocation("2""rowsAboutToBeInserted(QModelIndex,int,int)" "\0" __FILE__ ":" "611""613"), | - | ||||||||||||
268 | qFlagLocation("1""_q_rowsAboutToBeInserted(QModelIndex,int,int)" "\0" __FILE__ ":" "612""614") }, | - | ||||||||||||
269 | { qFlagLocation("2""columnsAboutToBeInserted(QModelIndex,int,int)" "\0" __FILE__ ":" "613""615"), | - | ||||||||||||
270 | qFlagLocation("1""_q_columnsAboutToBeInserted(QModelIndex,int,int)" "\0" __FILE__ ":" "614""616") }, | - | ||||||||||||
271 | { qFlagLocation("2""rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)" "\0" __FILE__ ":" "615""617"), | - | ||||||||||||
272 | qFlagLocation("1""_q_layoutAboutToBeChanged()" "\0" __FILE__ ":" "616""618") }, | - | ||||||||||||
273 | { qFlagLocation("2""columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)" "\0" __FILE__ ":" "617""619"), | - | ||||||||||||
274 | qFlagLocation("1""_q_layoutAboutToBeChanged()" "\0" __FILE__ ":" "618""620") }, | - | ||||||||||||
275 | { qFlagLocation("2""rowsMoved(QModelIndex,int,int,QModelIndex,int)" "\0" __FILE__ ":" "619""621"), | - | ||||||||||||
276 | qFlagLocation("1""_q_layoutChanged()" "\0" __FILE__ ":" "620""622") }, | - | ||||||||||||
277 | { qFlagLocation("2""columnsMoved(QModelIndex,int,int,QModelIndex,int)" "\0" __FILE__ ":" "621""623"), | - | ||||||||||||
278 | qFlagLocation("1""_q_layoutChanged()" "\0" __FILE__ ":" "622""624") }, | - | ||||||||||||
279 | { qFlagLocation("2""layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)" "\0" __FILE__ ":" "623""625"), | - | ||||||||||||
280 | qFlagLocation("1""_q_layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)" "\0" __FILE__ ":" "624""626") }, | - | ||||||||||||
281 | { qFlagLocation("2""layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)" "\0" __FILE__ ":" "625""627"), | - | ||||||||||||
282 | qFlagLocation("1""_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)" "\0" __FILE__ ":" "626""628") }, | - | ||||||||||||
283 | { qFlagLocation("2""modelReset()" "\0" __FILE__ ":" "627""629"), | - | ||||||||||||
284 | qFlagLocation("1""reset()" "\0" __FILE__ ":" "628""630") }, | - | ||||||||||||
285 | { 0, 0 } | - | ||||||||||||
286 | }; | - | ||||||||||||
287 | - | |||||||||||||
288 | if (model == m) | - | ||||||||||||
289 | return; | - | ||||||||||||
290 | - | |||||||||||||
291 | QItemSelectionModel * const q = q_func(); | - | ||||||||||||
292 | if (model) { | - | ||||||||||||
293 | for (const Cx *cx = &connections[0]; cx->signal; cx++) | - | ||||||||||||
294 | QObject::disconnect(model, cx->signal, q, cx->slot); | - | ||||||||||||
295 | q->reset(); | - | ||||||||||||
296 | } | - | ||||||||||||
297 | model = m; | - | ||||||||||||
298 | if (model) { | - | ||||||||||||
299 | for (const Cx *cx = &connections[0]; cx->signal; cx++) | - | ||||||||||||
300 | QObject::connect(model, cx->signal, q, cx->slot); | - | ||||||||||||
301 | } | - | ||||||||||||
302 | } | - | ||||||||||||
303 | QItemSelection QItemSelectionModelPrivate::expandSelection(const QItemSelection &selection, | - | ||||||||||||
304 | QItemSelectionModel::SelectionFlags command) const | - | ||||||||||||
305 | { | - | ||||||||||||
306 | if (selection.isEmpty() && !((command & QItemSelectionModel::Rows) || | - | ||||||||||||
307 | (command & QItemSelectionModel::Columns))) | - | ||||||||||||
308 | return selection; | - | ||||||||||||
309 | - | |||||||||||||
310 | QItemSelection expanded; | - | ||||||||||||
311 | if (command & QItemSelectionModel::Rows) { | - | ||||||||||||
312 | for (int i = 0; i < selection.count(); ++i) { | - | ||||||||||||
313 | QModelIndex parent = selection.at(i).parent(); | - | ||||||||||||
314 | int colCount = model->columnCount(parent); | - | ||||||||||||
315 | QModelIndex tl = model->index(selection.at(i).top(), 0, parent); | - | ||||||||||||
316 | QModelIndex br = model->index(selection.at(i).bottom(), colCount - 1, parent); | - | ||||||||||||
317 | - | |||||||||||||
318 | expanded.merge(QItemSelection(tl, br), QItemSelectionModel::Select); | - | ||||||||||||
319 | } | - | ||||||||||||
320 | } | - | ||||||||||||
321 | if (command & QItemSelectionModel::Columns) { | - | ||||||||||||
322 | for (int i = 0; i < selection.count(); ++i) { | - | ||||||||||||
323 | QModelIndex parent = selection.at(i).parent(); | - | ||||||||||||
324 | int rowCount = model->rowCount(parent); | - | ||||||||||||
325 | QModelIndex tl = model->index(0, selection.at(i).left(), parent); | - | ||||||||||||
326 | QModelIndex br = model->index(rowCount - 1, selection.at(i).right(), parent); | - | ||||||||||||
327 | - | |||||||||||||
328 | expanded.merge(QItemSelection(tl, br), QItemSelectionModel::Select); | - | ||||||||||||
329 | } | - | ||||||||||||
330 | } | - | ||||||||||||
331 | return expanded; | - | ||||||||||||
332 | } | - | ||||||||||||
333 | - | |||||||||||||
334 | - | |||||||||||||
335 | - | |||||||||||||
336 | - | |||||||||||||
337 | void QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &parent, | - | ||||||||||||
338 | int start, int end) | - | ||||||||||||
339 | { | - | ||||||||||||
340 | QItemSelectionModel * const q = q_func(); | - | ||||||||||||
341 | finalize(); | - | ||||||||||||
342 | - | |||||||||||||
343 | - | |||||||||||||
344 | if (currentIndex.isValid() && parent == currentIndex.parent() | - | ||||||||||||
345 | && currentIndex.row() >= start && currentIndex.row() <= end) { | - | ||||||||||||
346 | QModelIndex old = currentIndex; | - | ||||||||||||
347 | if (start > 0) | - | ||||||||||||
348 | currentIndex = model->index(start - 1, old.column(), parent); | - | ||||||||||||
349 | else if (model && end < model->rowCount(parent) - 1) | - | ||||||||||||
350 | currentIndex = model->index(end + 1, old.column(), parent); | - | ||||||||||||
351 | else | - | ||||||||||||
352 | currentIndex = QModelIndex(); | - | ||||||||||||
353 | q->currentChanged(currentIndex, old); | - | ||||||||||||
354 | q->currentRowChanged(currentIndex, old); | - | ||||||||||||
355 | if (currentIndex.column() != old.column()) | - | ||||||||||||
356 | q->currentColumnChanged(currentIndex, old); | - | ||||||||||||
357 | } | - | ||||||||||||
358 | - | |||||||||||||
359 | QItemSelection deselected; | - | ||||||||||||
360 | QItemSelection newParts; | - | ||||||||||||
361 | QItemSelection::iterator it = ranges.begin(); | - | ||||||||||||
362 | while (it != ranges.end()) { | - | ||||||||||||
363 | if (it->topLeft().parent() != parent) { | - | ||||||||||||
364 | QModelIndex itParent = it->topLeft().parent(); | - | ||||||||||||
365 | while (itParent.isValid() && itParent.parent() != parent) | - | ||||||||||||
366 | itParent = itParent.parent(); | - | ||||||||||||
367 | - | |||||||||||||
368 | if (itParent.isValid() && start <= itParent.row() && itParent.row() <= end) { | - | ||||||||||||
369 | deselected.append(*it); | - | ||||||||||||
370 | it = ranges.erase(it); | - | ||||||||||||
371 | } else { | - | ||||||||||||
372 | ++it; | - | ||||||||||||
373 | } | - | ||||||||||||
374 | } else if (start <= it->bottom() && it->bottom() <= end | - | ||||||||||||
375 | && start <= it->top() && it->top() <= end) { | - | ||||||||||||
376 | deselected.append(*it); | - | ||||||||||||
377 | it = ranges.erase(it); | - | ||||||||||||
378 | } else if (start <= it->top() && it->top() <= end) { | - | ||||||||||||
379 | deselected.append(QItemSelectionRange(it->topLeft(), model->index(end, it->right(), it->parent()))); | - | ||||||||||||
380 | *it = QItemSelectionRange(model->index(end + 1, it->left(), it->parent()), it->bottomRight()); | - | ||||||||||||
381 | ++it; | - | ||||||||||||
382 | } else if (start <= it->bottom() && it->bottom() <= end) { | - | ||||||||||||
383 | deselected.append(QItemSelectionRange(model->index(start, it->left(), it->parent()), it->bottomRight())); | - | ||||||||||||
384 | *it = QItemSelectionRange(it->topLeft(), model->index(start - 1, it->right(), it->parent())); | - | ||||||||||||
385 | ++it; | - | ||||||||||||
386 | } else if (it->top() < start && end < it->bottom()) { | - | ||||||||||||
387 | - | |||||||||||||
388 | - | |||||||||||||
389 | - | |||||||||||||
390 | - | |||||||||||||
391 | const QItemSelectionRange removedRange(model->index(start, it->left(), it->parent()), | - | ||||||||||||
392 | model->index(end, it->right(), it->parent())); | - | ||||||||||||
393 | deselected.append(removedRange); | - | ||||||||||||
394 | QItemSelection::split(*it, removedRange, &newParts); | - | ||||||||||||
395 | it = ranges.erase(it); | - | ||||||||||||
396 | } else | - | ||||||||||||
397 | ++it; | - | ||||||||||||
398 | } | - | ||||||||||||
399 | ranges.append(newParts); | - | ||||||||||||
400 | - | |||||||||||||
401 | if (!deselected.isEmpty()) | - | ||||||||||||
402 | q->selectionChanged(QItemSelection(), deselected); | - | ||||||||||||
403 | } | - | ||||||||||||
404 | - | |||||||||||||
405 | - | |||||||||||||
406 | - | |||||||||||||
407 | - | |||||||||||||
408 | void QItemSelectionModelPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent, | - | ||||||||||||
409 | int start, int end) | - | ||||||||||||
410 | { | - | ||||||||||||
411 | QItemSelectionModel * const q = q_func(); | - | ||||||||||||
412 | - | |||||||||||||
413 | - | |||||||||||||
414 | if (currentIndex.isValid() && parent == currentIndex.parent() | - | ||||||||||||
415 | && currentIndex.column() >= start && currentIndex.column() <= end) { | - | ||||||||||||
416 | QModelIndex old = currentIndex; | - | ||||||||||||
417 | if (start > 0) | - | ||||||||||||
418 | currentIndex = model->index(old.row(), start - 1, parent); | - | ||||||||||||
419 | else if (model && end < model->columnCount() - 1) | - | ||||||||||||
420 | currentIndex = model->index(old.row(), end + 1, parent); | - | ||||||||||||
421 | else | - | ||||||||||||
422 | currentIndex = QModelIndex(); | - | ||||||||||||
423 | q->currentChanged(currentIndex, old); | - | ||||||||||||
424 | if (currentIndex.row() != old.row()) | - | ||||||||||||
425 | q->currentRowChanged(currentIndex, old); | - | ||||||||||||
426 | q->currentColumnChanged(currentIndex, old); | - | ||||||||||||
427 | } | - | ||||||||||||
428 | - | |||||||||||||
429 | - | |||||||||||||
430 | QModelIndex tl = model->index(0, start, parent); | - | ||||||||||||
431 | QModelIndex br = model->index(model->rowCount(parent) - 1, end, parent); | - | ||||||||||||
432 | q->select(QItemSelection(tl, br), QItemSelectionModel::Deselect); | - | ||||||||||||
433 | finalize(); | - | ||||||||||||
434 | } | - | ||||||||||||
435 | - | |||||||||||||
436 | - | |||||||||||||
437 | - | |||||||||||||
438 | - | |||||||||||||
439 | - | |||||||||||||
440 | - | |||||||||||||
441 | void QItemSelectionModelPrivate::_q_columnsAboutToBeInserted(const QModelIndex &parent, | - | ||||||||||||
442 | int start, int end) | - | ||||||||||||
443 | { | - | ||||||||||||
444 | (void)end;; | - | ||||||||||||
445 | finalize(); | - | ||||||||||||
446 | QList<QItemSelectionRange> split; | - | ||||||||||||
447 | QList<QItemSelectionRange>::iterator it = ranges.begin(); | - | ||||||||||||
448 | for (; it != ranges.end(); ) { | - | ||||||||||||
449 | if ((*it).isValid() && (*it).parent() == parent | - | ||||||||||||
450 | && (*it).left() < start && (*it).right() >= start) { | - | ||||||||||||
451 | QModelIndex bottomMiddle = model->index((*it).bottom(), start - 1, (*it).parent()); | - | ||||||||||||
452 | QItemSelectionRange left((*it).topLeft(), bottomMiddle); | - | ||||||||||||
453 | QModelIndex topMiddle = model->index((*it).top(), start, (*it).parent()); | - | ||||||||||||
454 | QItemSelectionRange right(topMiddle, (*it).bottomRight()); | - | ||||||||||||
455 | it = ranges.erase(it); | - | ||||||||||||
456 | split.append(left); | - | ||||||||||||
457 | split.append(right); | - | ||||||||||||
458 | } else { | - | ||||||||||||
459 | ++it; | - | ||||||||||||
460 | } | - | ||||||||||||
461 | } | - | ||||||||||||
462 | ranges += split; | - | ||||||||||||
463 | } | - | ||||||||||||
464 | - | |||||||||||||
465 | - | |||||||||||||
466 | - | |||||||||||||
467 | - | |||||||||||||
468 | - | |||||||||||||
469 | - | |||||||||||||
470 | void QItemSelectionModelPrivate::_q_rowsAboutToBeInserted(const QModelIndex &parent, | - | ||||||||||||
471 | int start, int end) | - | ||||||||||||
472 | { | - | ||||||||||||
473 | (void)end;; | - | ||||||||||||
474 | finalize(); | - | ||||||||||||
475 | QList<QItemSelectionRange> split; | - | ||||||||||||
476 | QList<QItemSelectionRange>::iterator it = ranges.begin(); | - | ||||||||||||
477 | for (; it != ranges.end(); ) { | - | ||||||||||||
478 | if ((*it).isValid() && (*it).parent() == parent | - | ||||||||||||
479 | && (*it).top() < start && (*it).bottom() >= start) { | - | ||||||||||||
480 | QModelIndex middleRight = model->index(start - 1, (*it).right(), (*it).parent()); | - | ||||||||||||
481 | QItemSelectionRange top((*it).topLeft(), middleRight); | - | ||||||||||||
482 | QModelIndex middleLeft = model->index(start, (*it).left(), (*it).parent()); | - | ||||||||||||
483 | QItemSelectionRange bottom(middleLeft, (*it).bottomRight()); | - | ||||||||||||
484 | it = ranges.erase(it); | - | ||||||||||||
485 | split.append(top); | - | ||||||||||||
486 | split.append(bottom); | - | ||||||||||||
487 | } else { | - | ||||||||||||
488 | ++it; | - | ||||||||||||
489 | } | - | ||||||||||||
490 | } | - | ||||||||||||
491 | ranges += split; | - | ||||||||||||
492 | } | - | ||||||||||||
493 | void QItemSelectionModelPrivate::_q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint hint) | - | ||||||||||||
494 | { | - | ||||||||||||
495 | savedPersistentIndexes.clear(); | - | ||||||||||||
496 | savedPersistentCurrentIndexes.clear(); | - | ||||||||||||
497 | savedPersistentRowLengths.clear(); | - | ||||||||||||
498 | savedPersistentCurrentRowLengths.clear(); | - | ||||||||||||
499 | - | |||||||||||||
500 | - | |||||||||||||
501 | - | |||||||||||||
502 | if (ranges.isEmpty()
| 14-2919 | ||||||||||||
503 | QItemSelectionRange range = currentSelection.firstconstFirst(); | - | ||||||||||||
504 | QModelIndex parent = range.parent(); | - | ||||||||||||
505 | tableRowCount = model->rowCount(parent); | - | ||||||||||||
506 | tableColCount = model->columnCount(parent); | - | ||||||||||||
507 | if (tableRowCount * tableColCount > 1000
| 0-14 | ||||||||||||
508 | && range.top() == 0
| 0 | ||||||||||||
509 | && range.left() == 0
| 0 | ||||||||||||
510 | && range.bottom() == tableRowCount - 1
| 0 | ||||||||||||
511 | && range.right() == tableColCount - 1
| 0 | ||||||||||||
512 | tableSelected = true; | - | ||||||||||||
513 | tableParent = parent; | - | ||||||||||||
514 | return; never executed: return; | 0 | ||||||||||||
515 | } | - | ||||||||||||
516 | } executed 14 times by 6 tests: end of block Executed by:
| 14 | ||||||||||||
517 | tableSelected = false; | - | ||||||||||||
518 | - | |||||||||||||
519 | if (hint == QAbstractItemModel::VerticalSortHint
| 125-2830 | ||||||||||||
520 | - | |||||||||||||
521 | - | |||||||||||||
522 | - | |||||||||||||
523 | - | |||||||||||||
524 | - | |||||||||||||
525 | savedPersistentRowLengths = qSelectionPersistentRowLengths(ranges); | - | ||||||||||||
526 | savedPersistentCurrentRowLengths = qSelectionPersistentRowLengths(currentSelection); | - | ||||||||||||
527 | } executed 125 times by 9 tests: else {end of block Executed by:
| 125 | ||||||||||||
528 | savedPersistentIndexes = qSelectionPersistentindexes(qSelectionIndexes<QVector<QPersistentModelIndex>>(ranges); | - | ||||||||||||
529 | savedPersistentCurrentIndexes = qSelectionPersistentindexes(qSelectionIndexes<QVector<QPersistentModelIndex>>(currentSelection); | - | ||||||||||||
530 | } executed 2830 times by 16 tests: end of block Executed by:
| 2830 | ||||||||||||
531 | } | - | ||||||||||||
532 | - | |||||||||||||
533 | - | |||||||||||||
534 | - | |||||||||||||
535 | static QItemSelection mergeRowLengths(const QVector<QPair<QPersistentModelIndex, uint> > &rowLengths) | - | ||||||||||||
536 | { | - | ||||||||||||
537 | if (rowLengths.isEmpty()) | - | ||||||||||||
538 | return QItemSelection(); | - | ||||||||||||
539 | - | |||||||||||||
540 | QItemSelection result; | - | ||||||||||||
541 | int i = 0; | - | ||||||||||||
542 | while (i < rowLengths.count()) { | - | ||||||||||||
543 | const QPersistentModelIndex &tl = rowLengths.at(i).first; | - | ||||||||||||
544 | if (!tl.isValid()) { | - | ||||||||||||
545 | ++i; | - | ||||||||||||
546 | continue; | - | ||||||||||||
547 | } | - | ||||||||||||
548 | QPersistentModelIndex br = tl; | - | ||||||||||||
549 | const uint length = rowLengths.at(i).second; | - | ||||||||||||
550 | while (++i < rowLengths.count()) { | - | ||||||||||||
551 | const QPersistentModelIndex &next = rowLengths.at(i).first; | - | ||||||||||||
552 | if (!next.isValid()) | - | ||||||||||||
553 | continue; | - | ||||||||||||
554 | const uint nextLength = rowLengths.at(i).second; | - | ||||||||||||
555 | if ((nextLength == length) | - | ||||||||||||
556 | && (next.row() == br.row() + 1) | - | ||||||||||||
557 | && (next.parent() == br.parent())) { | - | ||||||||||||
558 | br = next; | - | ||||||||||||
559 | } else { | - | ||||||||||||
560 | break; | - | ||||||||||||
561 | } | - | ||||||||||||
562 | } | - | ||||||||||||
563 | result.append(QItemSelectionRange(tl, br.sibling(br.row(), length - 1))); | - | ||||||||||||
564 | } | - | ||||||||||||
565 | return result; | - | ||||||||||||
566 | } | - | ||||||||||||
567 | - | |||||||||||||
568 | - | |||||||||||||
569 | - | |||||||||||||
570 | - | |||||||||||||
571 | - | |||||||||||||
572 | - | |||||||||||||
573 | - | |||||||||||||
574 | static QItemSelection mergeIndexes(const QVector<QPersistentModelIndex> &indexes) | - | ||||||||||||
575 | { | - | ||||||||||||
576 | QItemSelection colSpans; | - | ||||||||||||
577 | - | |||||||||||||
578 | int i = 0; | - | ||||||||||||
579 | while (i < indexes.count()) { | - | ||||||||||||
580 | const QPersistentModelIndex &tl = indexes.at(i); | - | ||||||||||||
581 | if (!tl.isValid()) { | - | ||||||||||||
582 | ++i; | - | ||||||||||||
583 | continue; | - | ||||||||||||
584 | } | - | ||||||||||||
585 | QPersistentModelIndex br = tl; | - | ||||||||||||
586 | QModelIndex brParent = br.parent(); | - | ||||||||||||
587 | int brRow = br.row(); | - | ||||||||||||
588 | int brColumn = br.column(); | - | ||||||||||||
589 | while (++i < indexes.count()) { | - | ||||||||||||
590 | const QPersistentModelIndex &next = indexes.at(i); | - | ||||||||||||
591 | if (!next.isValid()) | - | ||||||||||||
592 | continue; | - | ||||||||||||
593 | const QModelIndex nextParent = next.parent(); | - | ||||||||||||
594 | const int nextRow = next.row(); | - | ||||||||||||
595 | const int nextColumn = next.column(); | - | ||||||||||||
596 | if ((nextParent == brParent) | - | ||||||||||||
597 | && (nextRow == brRow) | - | ||||||||||||
598 | && (nextColumn == brColumn + 1)) { | - | ||||||||||||
599 | br = next; | - | ||||||||||||
600 | brParent = nextParent; | - | ||||||||||||
601 | brRow = nextRow; | - | ||||||||||||
602 | brColumn = nextColumn; | - | ||||||||||||
603 | } else { | - | ||||||||||||
604 | break; | - | ||||||||||||
605 | } | - | ||||||||||||
606 | } | - | ||||||||||||
607 | colSpans.append(QItemSelectionRange(tl, br)); | - | ||||||||||||
608 | } | - | ||||||||||||
609 | - | |||||||||||||
610 | QItemSelection rowSpans; | - | ||||||||||||
611 | i = 0; | - | ||||||||||||
612 | while (i < colSpans.count()) { | - | ||||||||||||
613 | QModelIndex tl = colSpans.at(i).topLeft(); | - | ||||||||||||
614 | QModelIndex br = colSpans.at(i).bottomRight(); | - | ||||||||||||
615 | QModelIndex prevTl = tl; | - | ||||||||||||
616 | while (++i < colSpans.count()) { | - | ||||||||||||
617 | QModelIndex nextTl = colSpans.at(i).topLeft(); | - | ||||||||||||
618 | QModelIndex nextBr = colSpans.at(i).bottomRight(); | - | ||||||||||||
619 | - | |||||||||||||
620 | if (nextTl.parent() != tl.parent()) | - | ||||||||||||
621 | break; | - | ||||||||||||
622 | - | |||||||||||||
623 | if ((nextTl.column() == prevTl.column()) && (nextBr.column() == br.column()) | - | ||||||||||||
624 | && (nextTl.row() == prevTl.row() + 1) && (nextBr.row() == br.row() + 1)) { | - | ||||||||||||
625 | br = nextBr; | - | ||||||||||||
626 | prevTl = nextTl; | - | ||||||||||||
627 | } else { | - | ||||||||||||
628 | break; | - | ||||||||||||
629 | } | - | ||||||||||||
630 | } | - | ||||||||||||
631 | rowSpans.append(QItemSelectionRange(tl, br)); | - | ||||||||||||
632 | } | - | ||||||||||||
633 | return rowSpans; | - | ||||||||||||
634 | } | - | ||||||||||||
635 | static bool qt_PersistentModelIndexLessThan(const QPersistentModelIndex &i1, const QPersistentModelIndex &i2) | - | ||||||||||||
636 | { | - | ||||||||||||
637 | const QModelIndex parent1 = i1.parent(); | - | ||||||||||||
638 | const QModelIndex parent2 = i2.parent(); | - | ||||||||||||
639 | return parent1 == parent2 ? i1 < i2 : parent1 < parent2; | - | ||||||||||||
640 | } | - | ||||||||||||
641 | - | |||||||||||||
642 | - | |||||||||||||
643 | - | |||||||||||||
644 | - | |||||||||||||
645 | - | |||||||||||||
646 | - | |||||||||||||
647 | void QItemSelectionModelPrivate::_q_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint hint) | - | ||||||||||||
648 | { | - | ||||||||||||
649 | - | |||||||||||||
650 | if (tableSelected && tableColCount == model->columnCount(tableParent) | - | ||||||||||||
651 | && tableRowCount == model->rowCount(tableParent)) { | - | ||||||||||||
652 | ranges.clear(); | - | ||||||||||||
653 | currentSelection.clear(); | - | ||||||||||||
654 | int bottom = tableRowCount - 1; | - | ||||||||||||
655 | int right = tableColCount - 1; | - | ||||||||||||
656 | QModelIndex tl = model->index(0, 0, tableParent); | - | ||||||||||||
657 | QModelIndex br = model->index(bottom, right, tableParent); | - | ||||||||||||
658 | currentSelection << QItemSelectionRange(tl, br); | - | ||||||||||||
659 | tableParent = QModelIndex(); | - | ||||||||||||
660 | tableSelected = false; | - | ||||||||||||
661 | return; | - | ||||||||||||
662 | } | - | ||||||||||||
663 | - | |||||||||||||
664 | if ((hint != QAbstractItemModel::VerticalSortHint && savedPersistentCurrentIndexes.isEmpty() && savedPersistentIndexes.isEmpty()) | - | ||||||||||||
665 | || (hint == QAbstractItemModel::VerticalSortHint && savedPersistentRowLengths.isEmpty() && savedPersistentCurrentRowLengths.isEmpty())) { | - | ||||||||||||
666 | - | |||||||||||||
667 | - | |||||||||||||
668 | return; | - | ||||||||||||
669 | } | - | ||||||||||||
670 | - | |||||||||||||
671 | - | |||||||||||||
672 | ranges.clear(); | - | ||||||||||||
673 | currentSelection.clear(); | - | ||||||||||||
674 | - | |||||||||||||
675 | if (hint != QAbstractItemModel::VerticalSortHint) { | - | ||||||||||||
676 | - | |||||||||||||
677 | std::stable_sort(savedPersistentIndexes.begin(), savedPersistentIndexes.end(), | - | ||||||||||||
678 | qt_PersistentModelIndexLessThan); | - | ||||||||||||
679 | std::stable_sort(savedPersistentCurrentIndexes.begin(), savedPersistentCurrentIndexes.end(), | - | ||||||||||||
680 | qt_PersistentModelIndexLessThan); | - | ||||||||||||
681 | - | |||||||||||||
682 | - | |||||||||||||
683 | ranges = mergeIndexes(savedPersistentIndexes); | - | ||||||||||||
684 | currentSelection = mergeIndexes(savedPersistentCurrentIndexes); | - | ||||||||||||
685 | - | |||||||||||||
686 | - | |||||||||||||
687 | savedPersistentIndexes.clear(); | - | ||||||||||||
688 | savedPersistentCurrentIndexes.clear(); | - | ||||||||||||
689 | } else { | - | ||||||||||||
690 | - | |||||||||||||
691 | std::stable_sort(savedPersistentRowLengths.begin(), savedPersistentRowLengths.end()); | - | ||||||||||||
692 | std::stable_sort(savedPersistentCurrentRowLengths.begin(), savedPersistentCurrentRowLengths.end()); | - | ||||||||||||
693 | - | |||||||||||||
694 | - | |||||||||||||
695 | ranges = mergeRowLengths(savedPersistentRowLengths); | - | ||||||||||||
696 | currentSelection = mergeRowLengths(savedPersistentCurrentRowLengths); | - | ||||||||||||
697 | - | |||||||||||||
698 | - | |||||||||||||
699 | savedPersistentRowLengths.clear(); | - | ||||||||||||
700 | savedPersistentCurrentRowLengths.clear(); | - | ||||||||||||
701 | } | - | ||||||||||||
702 | } | - | ||||||||||||
703 | QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model) | - | ||||||||||||
704 | : QObject(*new QItemSelectionModelPrivate, model) | - | ||||||||||||
705 | { | - | ||||||||||||
706 | d_func()->initModel(model); | - | ||||||||||||
707 | } | - | ||||||||||||
708 | - | |||||||||||||
709 | - | |||||||||||||
710 | - | |||||||||||||
711 | - | |||||||||||||
712 | QItemSelectionModel::QItemSelectionModel(QAbstractItemModel *model, QObject *parent) | - | ||||||||||||
713 | : QObject(*new QItemSelectionModelPrivate, parent) | - | ||||||||||||
714 | { | - | ||||||||||||
715 | d_func()->initModel(model); | - | ||||||||||||
716 | } | - | ||||||||||||
717 | - | |||||||||||||
718 | - | |||||||||||||
719 | - | |||||||||||||
720 | - | |||||||||||||
721 | QItemSelectionModel::QItemSelectionModel(QItemSelectionModelPrivate &dd, QAbstractItemModel *model) | - | ||||||||||||
722 | : QObject(dd, model) | - | ||||||||||||
723 | { | - | ||||||||||||
724 | dd.initModel(model); | - | ||||||||||||
725 | } | - | ||||||||||||
726 | - | |||||||||||||
727 | - | |||||||||||||
728 | - | |||||||||||||
729 | - | |||||||||||||
730 | QItemSelectionModel::~QItemSelectionModel() | - | ||||||||||||
731 | { | - | ||||||||||||
732 | } | - | ||||||||||||
733 | - | |||||||||||||
734 | - | |||||||||||||
735 | - | |||||||||||||
736 | - | |||||||||||||
737 | - | |||||||||||||
738 | - | |||||||||||||
739 | - | |||||||||||||
740 | void QItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
741 | { | - | ||||||||||||
742 | QItemSelection selection(index, index); | - | ||||||||||||
743 | select(selection, command); | - | ||||||||||||
744 | } | - | ||||||||||||
745 | void QItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
746 | { | - | ||||||||||||
747 | QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
748 | if (!d->model) { | - | ||||||||||||
749 | QMessageLogger(__FILE__, 12491251, __PRETTY_FUNCTION__).warning("QItemSelectionModel: Selecting when no model has been set will result in a no-op."); | - | ||||||||||||
750 | return; | - | ||||||||||||
751 | } | - | ||||||||||||
752 | if (command == NoUpdate) | - | ||||||||||||
753 | return; | - | ||||||||||||
754 | - | |||||||||||||
755 | - | |||||||||||||
756 | QItemSelection sel = selection; | - | ||||||||||||
757 | - | |||||||||||||
758 | - | |||||||||||||
759 | - | |||||||||||||
760 | - | |||||||||||||
761 | - | |||||||||||||
762 | QItemSelection::iterator it = d->ranges.begin(); | - | ||||||||||||
763 | while (it != d->ranges.end()) { | - | ||||||||||||
764 | if (!it->isValid()) | - | ||||||||||||
765 | it = d->ranges.erase(it); | - | ||||||||||||
766 | else | - | ||||||||||||
767 | ++it; | - | ||||||||||||
768 | } | - | ||||||||||||
769 | - | |||||||||||||
770 | QItemSelection old = d->ranges; | - | ||||||||||||
771 | old.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
772 | - | |||||||||||||
773 | - | |||||||||||||
774 | if (command & Rows || command & Columns) | - | ||||||||||||
775 | sel = d->expandSelection(sel, command); | - | ||||||||||||
776 | - | |||||||||||||
777 | - | |||||||||||||
778 | if (command & Clear) { | - | ||||||||||||
779 | d->ranges.clear(); | - | ||||||||||||
780 | d->currentSelection.clear(); | - | ||||||||||||
781 | } | - | ||||||||||||
782 | - | |||||||||||||
783 | - | |||||||||||||
784 | if (!(command & Current)) | - | ||||||||||||
785 | d->finalize(); | - | ||||||||||||
786 | - | |||||||||||||
787 | - | |||||||||||||
788 | if (command & Toggle || command & Select || command & Deselect) { | - | ||||||||||||
789 | d->currentCommand = command; | - | ||||||||||||
790 | d->currentSelection = sel; | - | ||||||||||||
791 | } | - | ||||||||||||
792 | - | |||||||||||||
793 | - | |||||||||||||
794 | QItemSelection newSelection = d->ranges; | - | ||||||||||||
795 | newSelection.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
796 | emitSelectionChanged(newSelection, old); | - | ||||||||||||
797 | } | - | ||||||||||||
798 | - | |||||||||||||
799 | - | |||||||||||||
800 | - | |||||||||||||
801 | - | |||||||||||||
802 | void QItemSelectionModel::clear() | - | ||||||||||||
803 | { | - | ||||||||||||
804 | clearSelection(); | - | ||||||||||||
805 | clearCurrentIndex(); | - | ||||||||||||
806 | } | - | ||||||||||||
807 | - | |||||||||||||
808 | - | |||||||||||||
809 | - | |||||||||||||
810 | - | |||||||||||||
811 | void QItemSelectionModel::clearCurrentIndex() | - | ||||||||||||
812 | { | - | ||||||||||||
813 | QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
814 | QModelIndex previous = d->currentIndex; | - | ||||||||||||
815 | d->currentIndex = QModelIndex(); | - | ||||||||||||
816 | if (previous.isValid()) { | - | ||||||||||||
817 | currentChanged(d->currentIndex, previous); | - | ||||||||||||
818 | currentRowChanged(d->currentIndex, previous); | - | ||||||||||||
819 | currentColumnChanged(d->currentIndex, previous); | - | ||||||||||||
820 | } | - | ||||||||||||
821 | } | - | ||||||||||||
822 | - | |||||||||||||
823 | - | |||||||||||||
824 | - | |||||||||||||
825 | - | |||||||||||||
826 | void QItemSelectionModel::reset() | - | ||||||||||||
827 | { | - | ||||||||||||
828 | const QSignalBlocker blocker(this); | - | ||||||||||||
829 | clear(); | - | ||||||||||||
830 | } | - | ||||||||||||
831 | - | |||||||||||||
832 | - | |||||||||||||
833 | - | |||||||||||||
834 | - | |||||||||||||
835 | - | |||||||||||||
836 | void QItemSelectionModel::clearSelection() | - | ||||||||||||
837 | { | - | ||||||||||||
838 | QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
839 | if (d->ranges.count() == 0 && d->currentSelection.count() == 0) | - | ||||||||||||
840 | return; | - | ||||||||||||
841 | - | |||||||||||||
842 | select(QItemSelection(), Clear); | - | ||||||||||||
843 | } | - | ||||||||||||
844 | void QItemSelectionModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
845 | { | - | ||||||||||||
846 | QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
847 | if (!d->model) { | - | ||||||||||||
848 | QMessageLogger(__FILE__, 13601362, __PRETTY_FUNCTION__).warning("QItemSelectionModel: Setting the current index when no model has been set will result in a no-op."); | - | ||||||||||||
849 | return; | - | ||||||||||||
850 | } | - | ||||||||||||
851 | if (index == d->currentIndex) { | - | ||||||||||||
852 | if (command != NoUpdate) | - | ||||||||||||
853 | select(index, command); | - | ||||||||||||
854 | return; | - | ||||||||||||
855 | } | - | ||||||||||||
856 | QPersistentModelIndex previous = d->currentIndex; | - | ||||||||||||
857 | d->currentIndex = index; | - | ||||||||||||
858 | if (command != NoUpdate) | - | ||||||||||||
859 | select(d->currentIndex, command); | - | ||||||||||||
860 | currentChanged(d->currentIndex, previous); | - | ||||||||||||
861 | if (d->currentIndex.row() != previous.row() || | - | ||||||||||||
862 | d->currentIndex.parent() != previous.parent()) | - | ||||||||||||
863 | currentRowChanged(d->currentIndex, previous); | - | ||||||||||||
864 | if (d->currentIndex.column() != previous.column() || | - | ||||||||||||
865 | d->currentIndex.parent() != previous.parent()) | - | ||||||||||||
866 | currentColumnChanged(d->currentIndex, previous); | - | ||||||||||||
867 | } | - | ||||||||||||
868 | - | |||||||||||||
869 | - | |||||||||||||
870 | - | |||||||||||||
871 | - | |||||||||||||
872 | - | |||||||||||||
873 | QModelIndex QItemSelectionModel::currentIndex() const | - | ||||||||||||
874 | { | - | ||||||||||||
875 | return static_cast<QModelIndex>(d_func()->currentIndex); | - | ||||||||||||
876 | } | - | ||||||||||||
877 | - | |||||||||||||
878 | - | |||||||||||||
879 | - | |||||||||||||
880 | - | |||||||||||||
881 | bool QItemSelectionModel::isSelected(const QModelIndex &index) const | - | ||||||||||||
882 | { | - | ||||||||||||
883 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
884 | if (d->model != index.model() || !index.isValid()) | - | ||||||||||||
885 | return false; | - | ||||||||||||
886 | - | |||||||||||||
887 | bool selected = false; | - | ||||||||||||
888 | - | |||||||||||||
889 | QList<QItemSelectionRange>::const_iterator it = d->ranges.begin(); | - | ||||||||||||
890 | for (; it != d->ranges.end(); ++it) { | - | ||||||||||||
891 | if ((*it).isValid() && (*it).contains(index)) { | - | ||||||||||||
892 | selected = true; | - | ||||||||||||
893 | break; | - | ||||||||||||
894 | } | - | ||||||||||||
895 | } | - | ||||||||||||
896 | - | |||||||||||||
897 | - | |||||||||||||
898 | if (d->currentSelection.count()) { | - | ||||||||||||
899 | if ((d->currentCommand & Deselect) && selected) | - | ||||||||||||
900 | selected = !d->currentSelection.contains(index); | - | ||||||||||||
901 | else if (d->currentCommand & Toggle) | - | ||||||||||||
902 | selected ^= d->currentSelection.contains(index); | - | ||||||||||||
903 | else if ((d->currentCommand & Select) && !selected) | - | ||||||||||||
904 | selected = d->currentSelection.contains(index); | - | ||||||||||||
905 | } | - | ||||||||||||
906 | - | |||||||||||||
907 | if (selected) { | - | ||||||||||||
908 | Qt::ItemFlags flags = d->model->flags(index); | - | ||||||||||||
909 | return (flags & Qt::ItemIsSelectable); | - | ||||||||||||
910 | } | - | ||||||||||||
911 | - | |||||||||||||
912 | return false; | - | ||||||||||||
913 | } | - | ||||||||||||
914 | bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) const | - | ||||||||||||
915 | { | - | ||||||||||||
916 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
917 | if (!d->model) | - | ||||||||||||
918 | return false; | - | ||||||||||||
919 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
920 | return false; | - | ||||||||||||
921 | - | |||||||||||||
922 | - | |||||||||||||
923 | if (d->currentCommand & Deselect && d->currentSelection.count()) { | - | ||||||||||||
924 | for (int i=0; i<d->currentSelection.count(); ++i) { | - | ||||||||||||
925 | if (d->currentSelection.at(i).parent() == parent && | - | ||||||||||||
926 | row >= d->currentSelection.at(i).top() && | - | ||||||||||||
927 | row <= d->currentSelection.at(i).bottom()) | - | ||||||||||||
928 | return false; | - | ||||||||||||
929 | } | - | ||||||||||||
930 | } | - | ||||||||||||
931 | - | |||||||||||||
932 | - | |||||||||||||
933 | if (d->currentCommand & Toggle && d->currentSelection.count()) { | - | ||||||||||||
934 | for (int i=0; i<d->currentSelection.count(); ++i) | - | ||||||||||||
935 | if (d->currentSelection.at(i).top() <= row && | - | ||||||||||||
936 | d->currentSelection.at(i).bottom() >= row) | - | ||||||||||||
937 | for (int j=0; j<d->ranges.count(); ++j) | - | ||||||||||||
938 | if (d->ranges.at(j).top() <= row && d->ranges.at(j).bottom() >= row | - | ||||||||||||
939 | && d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) | - | ||||||||||||
940 | return false; | - | ||||||||||||
941 | } | - | ||||||||||||
942 | - | |||||||||||||
943 | QList<QItemSelectionRange>::const_iterator it; | - | ||||||||||||
944 | QList<QItemSelectionRange> joined = d->ranges; | - | ||||||||||||
945 | if (d->currentSelection.count()) | - | ||||||||||||
946 | joined += d->currentSelection; | - | ||||||||||||
947 | int colCount = d->model->columnCount(parent); | - | ||||||||||||
948 | for (int column = 0; column < colCount; ++column) { | - | ||||||||||||
949 | for (it = joined.constBegin(); it != joined.constEnd(); ++it) { | - | ||||||||||||
950 | if ((*it).contains(row, column, parent)) { | - | ||||||||||||
951 | bool selectable = false; | - | ||||||||||||
952 | for (int i = column; !selectable && i <= (*it).right(); ++i) { | - | ||||||||||||
953 | Qt::ItemFlags flags = d->model->index(row, i, parent).flags(); | - | ||||||||||||
954 | selectable = flags & Qt::ItemIsSelectable; | - | ||||||||||||
955 | } | - | ||||||||||||
956 | if (selectable){ | - | ||||||||||||
957 | column = qMax(column, (*it).right()); | - | ||||||||||||
958 | break; | - | ||||||||||||
959 | } | - | ||||||||||||
960 | } | - | ||||||||||||
961 | } | - | ||||||||||||
962 | if (it == joined.constEnd()) | - | ||||||||||||
963 | return false; | - | ||||||||||||
964 | } | - | ||||||||||||
965 | return colCount > 0; | - | ||||||||||||
966 | } | - | ||||||||||||
967 | bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent) const | - | ||||||||||||
968 | { | - | ||||||||||||
969 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
970 | if (!d->model) | - | ||||||||||||
971 | return false; | - | ||||||||||||
972 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
973 | return false; | - | ||||||||||||
974 | - | |||||||||||||
975 | - | |||||||||||||
976 | if (d->currentCommand & Deselect && d->currentSelection.count()) { | - | ||||||||||||
977 | for (int i = 0; i < d->currentSelection.count(); ++i) { | - | ||||||||||||
978 | if (d->currentSelection.at(i).parent() == parent && | - | ||||||||||||
979 | column >= d->currentSelection.at(i).left() && | - | ||||||||||||
980 | column <= d->currentSelection.at(i).right()) | - | ||||||||||||
981 | return false; | - | ||||||||||||
982 | } | - | ||||||||||||
983 | } | - | ||||||||||||
984 | - | |||||||||||||
985 | - | |||||||||||||
986 | if (d->currentCommand & Toggle && d->currentSelection.count()) { | - | ||||||||||||
987 | for (int i = 0; i < d->currentSelection.count(); ++i) { | - | ||||||||||||
988 | if (d->currentSelection.at(i).left() <= column && | - | ||||||||||||
989 | d->currentSelection.at(i).right() >= column) { | - | ||||||||||||
990 | for (int j = 0; j < d->ranges.count(); ++j) { | - | ||||||||||||
991 | if (d->ranges.at(j).left() <= column && d->ranges.at(j).right() >= column | - | ||||||||||||
992 | && d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) { | - | ||||||||||||
993 | return false; | - | ||||||||||||
994 | } | - | ||||||||||||
995 | } | - | ||||||||||||
996 | } | - | ||||||||||||
997 | } | - | ||||||||||||
998 | } | - | ||||||||||||
999 | - | |||||||||||||
1000 | QList<QItemSelectionRange>::const_iterator it; | - | ||||||||||||
1001 | QList<QItemSelectionRange> joined = d->ranges; | - | ||||||||||||
1002 | if (d->currentSelection.count()) | - | ||||||||||||
1003 | joined += d->currentSelection; | - | ||||||||||||
1004 | int rowCount = d->model->rowCount(parent); | - | ||||||||||||
1005 | for (int row = 0; row < rowCount; ++row) { | - | ||||||||||||
1006 | for (it = joined.constBegin(); it != joined.constEnd(); ++it) { | - | ||||||||||||
1007 | if ((*it).contains(row, column, parent)) { | - | ||||||||||||
1008 | Qt::ItemFlags flags = d->model->index(row, column, parent).flags(); | - | ||||||||||||
1009 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) { | - | ||||||||||||
1010 | row = qMax(row, (*it).bottom()); | - | ||||||||||||
1011 | break; | - | ||||||||||||
1012 | } | - | ||||||||||||
1013 | } | - | ||||||||||||
1014 | } | - | ||||||||||||
1015 | if (it == joined.constEnd()) | - | ||||||||||||
1016 | return false; | - | ||||||||||||
1017 | } | - | ||||||||||||
1018 | return rowCount > 0; | - | ||||||||||||
1019 | } | - | ||||||||||||
1020 | - | |||||||||||||
1021 | - | |||||||||||||
1022 | - | |||||||||||||
1023 | - | |||||||||||||
1024 | - | |||||||||||||
1025 | bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &parent) const | - | ||||||||||||
1026 | { | - | ||||||||||||
1027 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
1028 | if (!d->model) | - | ||||||||||||
1029 | return false; | - | ||||||||||||
1030 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
1031 | return false; | - | ||||||||||||
1032 | - | |||||||||||||
1033 | QItemSelection sel = d->ranges; | - | ||||||||||||
1034 | sel.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1035 | for (int i = 0; i < sel.count(); ++i) { | - | ||||||||||||
1036 | QItemSelectionRange range = sel.at(i); | - | ||||||||||||
1037 | if (range.parent() != parent) | - | ||||||||||||
1038 | return false; | - | ||||||||||||
1039 | int top = range.top(); | - | ||||||||||||
1040 | int bottom = range.bottom(); | - | ||||||||||||
1041 | int left = range.left(); | - | ||||||||||||
1042 | int right = range.right(); | - | ||||||||||||
1043 | if (top <= row && bottom >= row) { | - | ||||||||||||
1044 | for (int j = left; j <= right; j++) { | - | ||||||||||||
1045 | const Qt::ItemFlags flags = d->model->index(row, j, parent).flags(); | - | ||||||||||||
1046 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
1047 | return true; | - | ||||||||||||
1048 | } | - | ||||||||||||
1049 | } | - | ||||||||||||
1050 | } | - | ||||||||||||
1051 | - | |||||||||||||
1052 | return false; | - | ||||||||||||
1053 | } | - | ||||||||||||
1054 | - | |||||||||||||
1055 | - | |||||||||||||
1056 | - | |||||||||||||
1057 | - | |||||||||||||
1058 | - | |||||||||||||
1059 | bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelIndex &parent) const | - | ||||||||||||
1060 | { | - | ||||||||||||
1061 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
1062 | if (!d->model) | - | ||||||||||||
1063 | return false; | - | ||||||||||||
1064 | if (parent.isValid() && d->model != parent.model()) | - | ||||||||||||
1065 | return false; | - | ||||||||||||
1066 | - | |||||||||||||
1067 | QItemSelection sel = d->ranges; | - | ||||||||||||
1068 | sel.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1069 | for (int i = 0; i < sel.count(); ++i) { | - | ||||||||||||
1070 | int left = sel.at(i).left(); | - | ||||||||||||
1071 | int right = sel.at(i).right(); | - | ||||||||||||
1072 | int top = sel.at(i).top(); | - | ||||||||||||
1073 | int bottom = sel.at(i).bottom(); | - | ||||||||||||
1074 | if (left <= column && right >= column) { | - | ||||||||||||
1075 | for (int j = top; j <= bottom; j++) { | - | ||||||||||||
1076 | const Qt::ItemFlags flags = d->model->index(j, column, parent).flags(); | - | ||||||||||||
1077 | if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled)) | - | ||||||||||||
1078 | return true; | - | ||||||||||||
1079 | } | - | ||||||||||||
1080 | } | - | ||||||||||||
1081 | } | - | ||||||||||||
1082 | - | |||||||||||||
1083 | return false; | - | ||||||||||||
1084 | } | - | ||||||||||||
1085 | - | |||||||||||||
1086 | - | |||||||||||||
1087 | - | |||||||||||||
1088 | - | |||||||||||||
1089 | - | |||||||||||||
1090 | - | |||||||||||||
1091 | - | |||||||||||||
1092 | bool QItemSelectionModel::hasSelection() const | - | ||||||||||||
1093 | { | - | ||||||||||||
1094 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
1095 | if (d->currentCommand & (Toggle | Deselect)) { | - | ||||||||||||
1096 | QItemSelection sel = d->ranges; | - | ||||||||||||
1097 | sel.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1098 | return !sel.isEmpty(); | - | ||||||||||||
1099 | } else { | - | ||||||||||||
1100 | return !(d->ranges.isEmpty() && d->currentSelection.isEmpty()); | - | ||||||||||||
1101 | } | - | ||||||||||||
1102 | } | - | ||||||||||||
1103 | - | |||||||||||||
1104 | - | |||||||||||||
1105 | - | |||||||||||||
1106 | - | |||||||||||||
1107 | - | |||||||||||||
1108 | QModelIndexList QItemSelectionModel::selectedIndexes() const | - | ||||||||||||
1109 | { | - | ||||||||||||
1110 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
1111 | QItemSelection selected = d->ranges; | - | ||||||||||||
1112 | selected.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1113 | return selected.indexes(); | - | ||||||||||||
1114 | } | - | ||||||||||||
1115 | QModelIndexList QItemSelectionModel::selectedRows(int column) const | - | ||||||||||||
1116 | { | - | ||||||||||||
1117 | QModelIndexList indexes; | - | ||||||||||||
1118 | - | |||||||||||||
1119 | - | |||||||||||||
1120 | QSet< QPair<QModelIndex, int> > rowsSeen; | - | ||||||||||||
1121 | - | |||||||||||||
1122 | const QItemSelection ranges = selection(); | - | ||||||||||||
1123 | for (int i = 0; i < ranges.count(); ++i) { | - | ||||||||||||
1124 | const QItemSelectionRange &range = ranges.at(i); | - | ||||||||||||
1125 | QModelIndex parent = range.parent(); | - | ||||||||||||
1126 | for (int row = range.top(); row <= range.bottom(); row++) { | - | ||||||||||||
1127 | QPair<QModelIndex, int> rowDef = qMakePair(parent, row); | - | ||||||||||||
1128 | if (!rowsSeen.contains(rowDef)) { | - | ||||||||||||
1129 | rowsSeen << rowDef; | - | ||||||||||||
1130 | if (isRowSelected(row, parent)) { | - | ||||||||||||
1131 | indexes.append(model()->index(row, column, parent)); | - | ||||||||||||
1132 | } | - | ||||||||||||
1133 | } | - | ||||||||||||
1134 | } | - | ||||||||||||
1135 | } | - | ||||||||||||
1136 | - | |||||||||||||
1137 | return indexes; | - | ||||||||||||
1138 | } | - | ||||||||||||
1139 | QModelIndexList QItemSelectionModel::selectedColumns(int row) const | - | ||||||||||||
1140 | { | - | ||||||||||||
1141 | QModelIndexList indexes; | - | ||||||||||||
1142 | - | |||||||||||||
1143 | - | |||||||||||||
1144 | QSet< QPair<QModelIndex, int> > columnsSeen; | - | ||||||||||||
1145 | - | |||||||||||||
1146 | const QItemSelection ranges = selection(); | - | ||||||||||||
1147 | for (int i = 0; i < ranges.count(); ++i) { | - | ||||||||||||
1148 | const QItemSelectionRange &range = ranges.at(i); | - | ||||||||||||
1149 | QModelIndex parent = range.parent(); | - | ||||||||||||
1150 | for (int column = range.left(); column <= range.right(); column++) { | - | ||||||||||||
1151 | QPair<QModelIndex, int> columnDef = qMakePair(parent, column); | - | ||||||||||||
1152 | if (!columnsSeen.contains(columnDef)) { | - | ||||||||||||
1153 | columnsSeen << columnDef; | - | ||||||||||||
1154 | if (isColumnSelected(column, parent)) { | - | ||||||||||||
1155 | indexes.append(model()->index(row, column, parent)); | - | ||||||||||||
1156 | } | - | ||||||||||||
1157 | } | - | ||||||||||||
1158 | } | - | ||||||||||||
1159 | } | - | ||||||||||||
1160 | - | |||||||||||||
1161 | return indexes; | - | ||||||||||||
1162 | } | - | ||||||||||||
1163 | - | |||||||||||||
1164 | - | |||||||||||||
1165 | - | |||||||||||||
1166 | - | |||||||||||||
1167 | const QItemSelection QItemSelectionModel::selection() const | - | ||||||||||||
1168 | { | - | ||||||||||||
1169 | const QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
1170 | QItemSelection selected = d->ranges; | - | ||||||||||||
1171 | selected.merge(d->currentSelection, d->currentCommand); | - | ||||||||||||
1172 | - | |||||||||||||
1173 | - | |||||||||||||
1174 | int iauto isNotValid = 0; | - | ||||||||||||
while (i<selected.count())[](const QItemSelectionRange& range) { | ||||||||||||||
1175 | if (return executed 1284 times by 12 tests: return !range.isValid(); Executed by:
executed 1284 times by 12 tests: !range.isValid();return !range.isValid(); Executed by:
executed 1284 times by 12 tests: return !range.isValid(); Executed by:
| 1284 | ||||||||||||
1176 | }; | - | ||||||||||||
1177 | - | |||||||||||||
1178 | selected.aterase(i).isValid()) | - | ||||||||||||
++i; | ||||||||||||||
elsestd::remove_if(selected.removeAt(i)); | ||||||||||||||
}begin(), selected.end(), | ||||||||||||||
1179 | isNotValid), | - | ||||||||||||
1180 | selected.end()); | - | ||||||||||||
1181 | return executed 11454 times by 42 tests: selected;return selected; Executed by:
executed 11454 times by 42 tests: return selected; Executed by:
| 11454 | ||||||||||||
1182 | } | - | ||||||||||||
1183 | QAbstractItemModel *QItemSelectionModel::model() | - | ||||||||||||
1184 | { | - | ||||||||||||
1185 | return d_func()->model; | - | ||||||||||||
1186 | } | - | ||||||||||||
1187 | - | |||||||||||||
1188 | - | |||||||||||||
1189 | - | |||||||||||||
1190 | - | |||||||||||||
1191 | const QAbstractItemModel *QItemSelectionModel::model() const | - | ||||||||||||
1192 | { | - | ||||||||||||
1193 | return d_func()->model; | - | ||||||||||||
1194 | } | - | ||||||||||||
1195 | void QItemSelectionModel::setModel(QAbstractItemModel *model) | - | ||||||||||||
1196 | { | - | ||||||||||||
1197 | QItemSelectionModelPrivate * const d = d_func(); | - | ||||||||||||
1198 | if (d->model == model) | - | ||||||||||||
1199 | return; | - | ||||||||||||
1200 | - | |||||||||||||
1201 | d->initModel(model); | - | ||||||||||||
1202 | modelChanged(model); | - | ||||||||||||
1203 | } | - | ||||||||||||
1204 | - | |||||||||||||
1205 | - | |||||||||||||
1206 | - | |||||||||||||
1207 | - | |||||||||||||
1208 | - | |||||||||||||
1209 | void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelection, | - | ||||||||||||
1210 | const QItemSelection &oldSelection) | - | ||||||||||||
1211 | { | - | ||||||||||||
1212 | - | |||||||||||||
1213 | if ((oldSelection.isEmpty() && newSelection.isEmpty()) || | - | ||||||||||||
1214 | oldSelection == newSelection) | - | ||||||||||||
1215 | return; | - | ||||||||||||
1216 | - | |||||||||||||
1217 | - | |||||||||||||
1218 | if (oldSelection.isEmpty() || newSelection.isEmpty()) { | - | ||||||||||||
1219 | selectionChanged(newSelection, oldSelection); | - | ||||||||||||
1220 | return; | - | ||||||||||||
1221 | } | - | ||||||||||||
1222 | - | |||||||||||||
1223 | QItemSelection deselected = oldSelection; | - | ||||||||||||
1224 | QItemSelection selected = newSelection; | - | ||||||||||||
1225 | - | |||||||||||||
1226 | - | |||||||||||||
1227 | bool advance; | - | ||||||||||||
1228 | for (int o = 0; o < deselected.count(); ++o) { | - | ||||||||||||
1229 | advance = true; | - | ||||||||||||
1230 | for (int s = 0; s < selected.count() && o < deselected.count();) { | - | ||||||||||||
1231 | if (deselected.at(o) == selected.at(s)) { | - | ||||||||||||
1232 | deselected.removeAt(o); | - | ||||||||||||
1233 | selected.removeAt(s); | - | ||||||||||||
1234 | advance = false; | - | ||||||||||||
1235 | } else { | - | ||||||||||||
1236 | ++s; | - | ||||||||||||
1237 | } | - | ||||||||||||
1238 | } | - | ||||||||||||
1239 | if (advance) | - | ||||||||||||
1240 | ++o; | - | ||||||||||||
1241 | } | - | ||||||||||||
1242 | - | |||||||||||||
1243 | - | |||||||||||||
1244 | QItemSelection intersections; | - | ||||||||||||
1245 | for (int o = 0; o < deselected.count(); ++o) { | - | ||||||||||||
1246 | for (int s = 0; s < selected.count(); ++s) { | - | ||||||||||||
1247 | if (deselected.at(o).intersects(selected.at(s))) | - | ||||||||||||
1248 | intersections.append(deselected.at(o).intersected(selected.at(s))); | - | ||||||||||||
1249 | } | - | ||||||||||||
1250 | } | - | ||||||||||||
1251 | - | |||||||||||||
1252 | - | |||||||||||||
1253 | for (int i = 0; i < intersections.count(); ++i) { | - | ||||||||||||
1254 | - | |||||||||||||
1255 | for (int o = 0; o < deselected.count();) { | - | ||||||||||||
1256 | if (deselected.at(o).intersects(intersections.at(i))) { | - | ||||||||||||
1257 | QItemSelection::split(deselected.at(o), intersections.at(i), &deselected); | - | ||||||||||||
1258 | deselected.removeAt(o); | - | ||||||||||||
1259 | } else { | - | ||||||||||||
1260 | ++o; | - | ||||||||||||
1261 | } | - | ||||||||||||
1262 | } | - | ||||||||||||
1263 | - | |||||||||||||
1264 | for (int s = 0; s < selected.count();) { | - | ||||||||||||
1265 | if (selected.at(s).intersects(intersections.at(i))) { | - | ||||||||||||
1266 | QItemSelection::split(selected.at(s), intersections.at(i), &selected); | - | ||||||||||||
1267 | selected.removeAt(s); | - | ||||||||||||
1268 | } else { | - | ||||||||||||
1269 | ++s; | - | ||||||||||||
1270 | } | - | ||||||||||||
1271 | } | - | ||||||||||||
1272 | } | - | ||||||||||||
1273 | - | |||||||||||||
1274 | if (!selected.isEmpty() || !deselected.isEmpty()) | - | ||||||||||||
1275 | selectionChanged(selected, deselected); | - | ||||||||||||
1276 | } | - | ||||||||||||
1277 | - | |||||||||||||
1278 | - | |||||||||||||
1279 | QDebug operator<<(QDebug dbg, const QItemSelectionRange &range) | - | ||||||||||||
1280 | { | - | ||||||||||||
1281 | QDebugStateSaver saver(dbg); | - | ||||||||||||
1282 | dbg.nospace() << "QItemSelectionRange(" << range.topLeft() | - | ||||||||||||
1283 | << ',' << range.bottomRight() << ')'; | - | ||||||||||||
1284 | return dbg; | - | ||||||||||||
1285 | } | - | ||||||||||||
1286 | - | |||||||||||||
1287 | - | |||||||||||||
1288 | - | |||||||||||||
1289 | - | |||||||||||||
Switch to Source code | Preprocessed file |