Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.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 QtCore 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 "qabstractitemmodel.h" | - | ||||||||||||
41 | #include <private/qabstractitemmodel_p.h> | - | ||||||||||||
42 | #include <qdatastream.h> | - | ||||||||||||
43 | #include <qstringlist.h> | - | ||||||||||||
44 | #include <qsize.h> | - | ||||||||||||
45 | #include <qmimedata.h> | - | ||||||||||||
46 | #include <qdebug.h> | - | ||||||||||||
47 | #include <qvector.h> | - | ||||||||||||
48 | #include <qstack.h> | - | ||||||||||||
49 | #include <qbitarray.h> | - | ||||||||||||
50 | - | |||||||||||||
51 | #include <limits.h> | - | ||||||||||||
52 | - | |||||||||||||
53 | QT_BEGIN_NAMESPACE | - | ||||||||||||
54 | - | |||||||||||||
55 | QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index) | - | ||||||||||||
56 | { | - | ||||||||||||
57 | Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list | - | ||||||||||||
58 | QPersistentModelIndexData *d = 0; | - | ||||||||||||
59 | QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model()); | - | ||||||||||||
60 | QHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes; | - | ||||||||||||
61 | const QHash<QModelIndex, QPersistentModelIndexData *>::iteratorauto it = indexes.findconstFind(index); | - | ||||||||||||
62 | if (it != indexes.endcend()) {
| 26745-77648 | ||||||||||||
63 | d = (*it); | - | ||||||||||||
64 | } else { executed 77648 times by 34 tests: end of block Executed by:
| 77648 | ||||||||||||
65 | d = new QPersistentModelIndexData(index); | - | ||||||||||||
66 | indexes.insert(index, d); | - | ||||||||||||
67 | } executed 26745 times by 41 tests: end of block Executed by:
| 26745 | ||||||||||||
68 | Q_ASSERT(d); | - | ||||||||||||
69 | return d; executed 104393 times by 41 tests: return d; Executed by:
| 104393 | ||||||||||||
70 | } | - | ||||||||||||
71 | - | |||||||||||||
72 | void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data) | - | ||||||||||||
73 | { | - | ||||||||||||
74 | Q_ASSERT(data); | - | ||||||||||||
75 | Q_ASSERT(data->ref.load() == 0); | - | ||||||||||||
76 | QAbstractItemModel *model = const_cast<QAbstractItemModel *>(data->model); | - | ||||||||||||
77 | // a valid persistent model index with a null model pointer can only happen if the model was destroyed | - | ||||||||||||
78 | if (model) { | - | ||||||||||||
79 | QAbstractItemModelPrivate *p = model->d_func(); | - | ||||||||||||
80 | Q_ASSERT(p); | - | ||||||||||||
81 | p->removePersistentIndexData(data); | - | ||||||||||||
82 | } | - | ||||||||||||
83 | delete data; | - | ||||||||||||
84 | } | - | ||||||||||||
85 | - | |||||||||||||
86 | /*! | - | ||||||||||||
87 | \class QPersistentModelIndex | - | ||||||||||||
88 | \inmodule QtCore | - | ||||||||||||
89 | \ingroup shared | - | ||||||||||||
90 | - | |||||||||||||
91 | \brief The QPersistentModelIndex class is used to locate data in a data model. | - | ||||||||||||
92 | - | |||||||||||||
93 | \ingroup model-view | - | ||||||||||||
94 | - | |||||||||||||
95 | A QPersistentModelIndex is a model index that can be stored by an | - | ||||||||||||
96 | application, and later used to access information in a model. | - | ||||||||||||
97 | Unlike the QModelIndex class, it is safe to store a | - | ||||||||||||
98 | QPersistentModelIndex since the model will ensure that references | - | ||||||||||||
99 | to items will continue to be valid as long as they can be accessed | - | ||||||||||||
100 | by the model. | - | ||||||||||||
101 | - | |||||||||||||
102 | It is good practice to check that persistent model indexes are valid | - | ||||||||||||
103 | before using them. | - | ||||||||||||
104 | - | |||||||||||||
105 | \sa {Model/View Programming}, QModelIndex, QAbstractItemModel | - | ||||||||||||
106 | */ | - | ||||||||||||
107 | - | |||||||||||||
108 | /*! | - | ||||||||||||
109 | \fn QPersistentModelIndex::QPersistentModelIndex(QPersistentModelIndex &&other) | - | ||||||||||||
110 | - | |||||||||||||
111 | Move-constructs a QPersistentModelIndex instance, making it point at the same | - | ||||||||||||
112 | object that \a other was pointing to. | - | ||||||||||||
113 | - | |||||||||||||
114 | \since 5.2 | - | ||||||||||||
115 | */ | - | ||||||||||||
116 | - | |||||||||||||
117 | /*! | - | ||||||||||||
118 | \fn QPersistentModelIndex &QPersistentModelIndex::operator=(QPersistentModelIndex &&other) | - | ||||||||||||
119 | - | |||||||||||||
120 | Move-assigns \a other to this QPersistentModelIndex instance. | - | ||||||||||||
121 | - | |||||||||||||
122 | \since 5.2 | - | ||||||||||||
123 | */ | - | ||||||||||||
124 | - | |||||||||||||
125 | - | |||||||||||||
126 | /*! | - | ||||||||||||
127 | \fn QPersistentModelIndex::QPersistentModelIndex() | - | ||||||||||||
128 | - | |||||||||||||
129 | \internal | - | ||||||||||||
130 | */ | - | ||||||||||||
131 | - | |||||||||||||
132 | QPersistentModelIndex::QPersistentModelIndex() | - | ||||||||||||
133 | : d(0) | - | ||||||||||||
134 | { | - | ||||||||||||
135 | } | - | ||||||||||||
136 | - | |||||||||||||
137 | /*! | - | ||||||||||||
138 | \fn QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other) | - | ||||||||||||
139 | - | |||||||||||||
140 | Creates a new QPersistentModelIndex that is a copy of the \a other persistent | - | ||||||||||||
141 | model index. | - | ||||||||||||
142 | */ | - | ||||||||||||
143 | - | |||||||||||||
144 | QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other) | - | ||||||||||||
145 | : d(other.d) | - | ||||||||||||
146 | { | - | ||||||||||||
147 | if (d) d->ref.ref(); | - | ||||||||||||
148 | } | - | ||||||||||||
149 | - | |||||||||||||
150 | /*! | - | ||||||||||||
151 | Creates a new QPersistentModelIndex that is a copy of the model \a index. | - | ||||||||||||
152 | */ | - | ||||||||||||
153 | - | |||||||||||||
154 | QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index) | - | ||||||||||||
155 | : d(0) | - | ||||||||||||
156 | { | - | ||||||||||||
157 | if (index.isValid()) { | - | ||||||||||||
158 | d = QPersistentModelIndexData::create(index); | - | ||||||||||||
159 | d->ref.ref(); | - | ||||||||||||
160 | } | - | ||||||||||||
161 | } | - | ||||||||||||
162 | - | |||||||||||||
163 | /*! | - | ||||||||||||
164 | \fn QPersistentModelIndex::~QPersistentModelIndex() | - | ||||||||||||
165 | - | |||||||||||||
166 | \internal | - | ||||||||||||
167 | */ | - | ||||||||||||
168 | - | |||||||||||||
169 | QPersistentModelIndex::~QPersistentModelIndex() | - | ||||||||||||
170 | { | - | ||||||||||||
171 | if (d && !d->ref.deref()) { | - | ||||||||||||
172 | QPersistentModelIndexData::destroy(d); | - | ||||||||||||
173 | d = 0; | - | ||||||||||||
174 | } | - | ||||||||||||
175 | } | - | ||||||||||||
176 | - | |||||||||||||
177 | /*! | - | ||||||||||||
178 | Returns \c{true} if this persistent model index is equal to the \a other | - | ||||||||||||
179 | persistent model index; otherwise returns \c{false}. | - | ||||||||||||
180 | - | |||||||||||||
181 | All values in the persistent model index are used when comparing | - | ||||||||||||
182 | with another persistent model index. | - | ||||||||||||
183 | */ | - | ||||||||||||
184 | - | |||||||||||||
185 | bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const | - | ||||||||||||
186 | { | - | ||||||||||||
187 | if (d && other.d) | - | ||||||||||||
188 | return d->index == other.d->index; | - | ||||||||||||
189 | return d == other.d; | - | ||||||||||||
190 | } | - | ||||||||||||
191 | - | |||||||||||||
192 | /*! | - | ||||||||||||
193 | \since 4.1 | - | ||||||||||||
194 | - | |||||||||||||
195 | Returns \c{true} if this persistent model index is smaller than the \a other | - | ||||||||||||
196 | persistent model index; otherwise returns \c{false}. | - | ||||||||||||
197 | - | |||||||||||||
198 | All values in the persistent model index are used when comparing | - | ||||||||||||
199 | with another persistent model index. | - | ||||||||||||
200 | */ | - | ||||||||||||
201 | - | |||||||||||||
202 | bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const | - | ||||||||||||
203 | { | - | ||||||||||||
204 | if (d && other.d) | - | ||||||||||||
205 | return d->index < other.d->index; | - | ||||||||||||
206 | - | |||||||||||||
207 | return d < other.d; | - | ||||||||||||
208 | } | - | ||||||||||||
209 | - | |||||||||||||
210 | /*! | - | ||||||||||||
211 | \fn bool QPersistentModelIndex::operator!=(const QPersistentModelIndex &other) const | - | ||||||||||||
212 | \since 4.2 | - | ||||||||||||
213 | - | |||||||||||||
214 | Returns \c{true} if this persistent model index is not equal to the \a | - | ||||||||||||
215 | other persistent model index; otherwise returns \c{false}. | - | ||||||||||||
216 | */ | - | ||||||||||||
217 | - | |||||||||||||
218 | /*! | - | ||||||||||||
219 | Sets the persistent model index to refer to the same item in a model | - | ||||||||||||
220 | as the \a other persistent model index. | - | ||||||||||||
221 | */ | - | ||||||||||||
222 | - | |||||||||||||
223 | QPersistentModelIndex &QPersistentModelIndex::operator=(const QPersistentModelIndex &other) | - | ||||||||||||
224 | { | - | ||||||||||||
225 | if (d == other.d) | - | ||||||||||||
226 | return *this; | - | ||||||||||||
227 | if (d && !d->ref.deref()) | - | ||||||||||||
228 | QPersistentModelIndexData::destroy(d); | - | ||||||||||||
229 | d = other.d; | - | ||||||||||||
230 | if (d) d->ref.ref(); | - | ||||||||||||
231 | return *this; | - | ||||||||||||
232 | } | - | ||||||||||||
233 | /*! | - | ||||||||||||
234 | \fn void QPersistentModelIndex::swap(QPersistentModelIndex &other) | - | ||||||||||||
235 | \since 5.0 | - | ||||||||||||
236 | - | |||||||||||||
237 | Swaps this persistent modelindex with \a other. This function is | - | ||||||||||||
238 | very fast and never fails. | - | ||||||||||||
239 | */ | - | ||||||||||||
240 | - | |||||||||||||
241 | /*! | - | ||||||||||||
242 | Sets the persistent model index to refer to the same item in a model | - | ||||||||||||
243 | as the \a other model index. | - | ||||||||||||
244 | */ | - | ||||||||||||
245 | - | |||||||||||||
246 | QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other) | - | ||||||||||||
247 | { | - | ||||||||||||
248 | if (d && !d->ref.deref()) | - | ||||||||||||
249 | QPersistentModelIndexData::destroy(d); | - | ||||||||||||
250 | if (other.isValid()) { | - | ||||||||||||
251 | d = QPersistentModelIndexData::create(other); | - | ||||||||||||
252 | if (d) d->ref.ref(); | - | ||||||||||||
253 | } else { | - | ||||||||||||
254 | d = 0; | - | ||||||||||||
255 | } | - | ||||||||||||
256 | return *this; | - | ||||||||||||
257 | } | - | ||||||||||||
258 | - | |||||||||||||
259 | /*! | - | ||||||||||||
260 | \fn QPersistentModelIndex::operator const QModelIndex&() const | - | ||||||||||||
261 | - | |||||||||||||
262 | Cast operator that returns a const QModelIndex&. | - | ||||||||||||
263 | */ | - | ||||||||||||
264 | - | |||||||||||||
265 | QPersistentModelIndex::operator const QModelIndex&() const | - | ||||||||||||
266 | { | - | ||||||||||||
267 | static const QModelIndex invalid; | - | ||||||||||||
268 | if (d) | - | ||||||||||||
269 | return d->index; | - | ||||||||||||
270 | return invalid; | - | ||||||||||||
271 | } | - | ||||||||||||
272 | - | |||||||||||||
273 | /*! | - | ||||||||||||
274 | \fn bool QPersistentModelIndex::operator==(const QModelIndex &other) const | - | ||||||||||||
275 | - | |||||||||||||
276 | Returns \c{true} if this persistent model index refers to the same location as | - | ||||||||||||
277 | the \a other model index; otherwise returns \c{false}. | - | ||||||||||||
278 | - | |||||||||||||
279 | All values in the persistent model index are used when comparing with | - | ||||||||||||
280 | another model index. | - | ||||||||||||
281 | */ | - | ||||||||||||
282 | - | |||||||||||||
283 | bool QPersistentModelIndex::operator==(const QModelIndex &other) const | - | ||||||||||||
284 | { | - | ||||||||||||
285 | if (d) | - | ||||||||||||
286 | return d->index == other; | - | ||||||||||||
287 | return !other.isValid(); | - | ||||||||||||
288 | } | - | ||||||||||||
289 | - | |||||||||||||
290 | /*! | - | ||||||||||||
291 | \fn bool QPersistentModelIndex::operator!=(const QModelIndex &other) const | - | ||||||||||||
292 | - | |||||||||||||
293 | Returns \c{true} if this persistent model index does not refer to the same | - | ||||||||||||
294 | location as the \a other model index; otherwise returns \c{false}. | - | ||||||||||||
295 | */ | - | ||||||||||||
296 | - | |||||||||||||
297 | bool QPersistentModelIndex::operator!=(const QModelIndex &other) const | - | ||||||||||||
298 | { | - | ||||||||||||
299 | if (d) | - | ||||||||||||
300 | return d->index != other; | - | ||||||||||||
301 | return other.isValid(); | - | ||||||||||||
302 | } | - | ||||||||||||
303 | - | |||||||||||||
304 | /*! | - | ||||||||||||
305 | \fn int QPersistentModelIndex::row() const | - | ||||||||||||
306 | - | |||||||||||||
307 | Returns the row this persistent model index refers to. | - | ||||||||||||
308 | */ | - | ||||||||||||
309 | - | |||||||||||||
310 | int QPersistentModelIndex::row() const | - | ||||||||||||
311 | { | - | ||||||||||||
312 | if (d) | - | ||||||||||||
313 | return d->index.row(); | - | ||||||||||||
314 | return -1; | - | ||||||||||||
315 | } | - | ||||||||||||
316 | - | |||||||||||||
317 | /*! | - | ||||||||||||
318 | \fn int QPersistentModelIndex::column() const | - | ||||||||||||
319 | - | |||||||||||||
320 | Returns the column this persistent model index refers to. | - | ||||||||||||
321 | */ | - | ||||||||||||
322 | - | |||||||||||||
323 | int QPersistentModelIndex::column() const | - | ||||||||||||
324 | { | - | ||||||||||||
325 | if (d) | - | ||||||||||||
326 | return d->index.column(); | - | ||||||||||||
327 | return -1; | - | ||||||||||||
328 | } | - | ||||||||||||
329 | - | |||||||||||||
330 | /*! | - | ||||||||||||
331 | \fn void *QPersistentModelIndex::internalPointer() const | - | ||||||||||||
332 | - | |||||||||||||
333 | \internal | - | ||||||||||||
334 | - | |||||||||||||
335 | Returns a \c{void} \c{*} pointer used by the model to associate the index with | - | ||||||||||||
336 | the internal data structure. | - | ||||||||||||
337 | */ | - | ||||||||||||
338 | - | |||||||||||||
339 | void *QPersistentModelIndex::internalPointer() const | - | ||||||||||||
340 | { | - | ||||||||||||
341 | if (d) | - | ||||||||||||
342 | return d->index.internalPointer(); | - | ||||||||||||
343 | return 0; | - | ||||||||||||
344 | } | - | ||||||||||||
345 | - | |||||||||||||
346 | /*! | - | ||||||||||||
347 | \fn quintptr QPersistentModelIndex::internalId() const | - | ||||||||||||
348 | - | |||||||||||||
349 | \internal | - | ||||||||||||
350 | - | |||||||||||||
351 | Returns a \c{quintptr} used by the model to associate the index with | - | ||||||||||||
352 | the internal data structure. | - | ||||||||||||
353 | */ | - | ||||||||||||
354 | - | |||||||||||||
355 | quintptr QPersistentModelIndex::internalId() const | - | ||||||||||||
356 | { | - | ||||||||||||
357 | if (d) | - | ||||||||||||
358 | return d->index.internalId(); | - | ||||||||||||
359 | return 0; | - | ||||||||||||
360 | } | - | ||||||||||||
361 | - | |||||||||||||
362 | /*! | - | ||||||||||||
363 | Returns the parent QModelIndex for this persistent index, or an invalid | - | ||||||||||||
364 | QModelIndex if it has no parent. | - | ||||||||||||
365 | - | |||||||||||||
366 | \sa child(), sibling(), model() | - | ||||||||||||
367 | */ | - | ||||||||||||
368 | QModelIndex QPersistentModelIndex::parent() const | - | ||||||||||||
369 | { | - | ||||||||||||
370 | if (d) | - | ||||||||||||
371 | return d->index.parent(); | - | ||||||||||||
372 | return QModelIndex(); | - | ||||||||||||
373 | } | - | ||||||||||||
374 | - | |||||||||||||
375 | /*! | - | ||||||||||||
376 | Returns the sibling at \a row and \a column or an invalid QModelIndex if | - | ||||||||||||
377 | there is no sibling at this position. | - | ||||||||||||
378 | - | |||||||||||||
379 | \sa parent(), child() | - | ||||||||||||
380 | */ | - | ||||||||||||
381 | - | |||||||||||||
382 | QModelIndex QPersistentModelIndex::sibling(int row, int column) const | - | ||||||||||||
383 | { | - | ||||||||||||
384 | if (d) | - | ||||||||||||
385 | return d->index.sibling(row, column); | - | ||||||||||||
386 | return QModelIndex(); | - | ||||||||||||
387 | } | - | ||||||||||||
388 | - | |||||||||||||
389 | /*! | - | ||||||||||||
390 | Returns the child of the model index that is stored in the given \a row | - | ||||||||||||
391 | and \a column. | - | ||||||||||||
392 | - | |||||||||||||
393 | \sa parent(), sibling() | - | ||||||||||||
394 | */ | - | ||||||||||||
395 | - | |||||||||||||
396 | QModelIndex QPersistentModelIndex::child(int row, int column) const | - | ||||||||||||
397 | { | - | ||||||||||||
398 | if (d) | - | ||||||||||||
399 | return d->index.child(row, column); | - | ||||||||||||
400 | return QModelIndex(); | - | ||||||||||||
401 | } | - | ||||||||||||
402 | - | |||||||||||||
403 | /*! | - | ||||||||||||
404 | Returns the data for the given \a role for the item referred to by the | - | ||||||||||||
405 | index. | - | ||||||||||||
406 | - | |||||||||||||
407 | \sa Qt::ItemDataRole, QAbstractItemModel::setData() | - | ||||||||||||
408 | */ | - | ||||||||||||
409 | QVariant QPersistentModelIndex::data(int role) const | - | ||||||||||||
410 | { | - | ||||||||||||
411 | if (d) | - | ||||||||||||
412 | return d->index.data(role); | - | ||||||||||||
413 | return QVariant(); | - | ||||||||||||
414 | } | - | ||||||||||||
415 | - | |||||||||||||
416 | /*! | - | ||||||||||||
417 | \since 4.2 | - | ||||||||||||
418 | - | |||||||||||||
419 | Returns the flags for the item referred to by the index. | - | ||||||||||||
420 | */ | - | ||||||||||||
421 | Qt::ItemFlags QPersistentModelIndex::flags() const | - | ||||||||||||
422 | { | - | ||||||||||||
423 | if (d) | - | ||||||||||||
424 | return d->index.flags(); | - | ||||||||||||
425 | return 0; | - | ||||||||||||
426 | } | - | ||||||||||||
427 | - | |||||||||||||
428 | /*! | - | ||||||||||||
429 | Returns the model that the index belongs to. | - | ||||||||||||
430 | */ | - | ||||||||||||
431 | const QAbstractItemModel *QPersistentModelIndex::model() const | - | ||||||||||||
432 | { | - | ||||||||||||
433 | if (d) | - | ||||||||||||
434 | return d->index.model(); | - | ||||||||||||
435 | return 0; | - | ||||||||||||
436 | } | - | ||||||||||||
437 | - | |||||||||||||
438 | /*! | - | ||||||||||||
439 | \fn bool QPersistentModelIndex::isValid() const | - | ||||||||||||
440 | - | |||||||||||||
441 | Returns \c{true} if this persistent model index is valid; otherwise returns | - | ||||||||||||
442 | \c{false}. | - | ||||||||||||
443 | - | |||||||||||||
444 | A valid index belongs to a model, and has non-negative row and column | - | ||||||||||||
445 | numbers. | - | ||||||||||||
446 | - | |||||||||||||
447 | \sa model(), row(), column() | - | ||||||||||||
448 | */ | - | ||||||||||||
449 | - | |||||||||||||
450 | bool QPersistentModelIndex::isValid() const | - | ||||||||||||
451 | { | - | ||||||||||||
452 | return d && d->index.isValid(); | - | ||||||||||||
453 | } | - | ||||||||||||
454 | - | |||||||||||||
455 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||
456 | QDebug operator<<(QDebug dbg, const QModelIndex &idx) | - | ||||||||||||
457 | { | - | ||||||||||||
458 | QDebugStateSaver saver(dbg); | - | ||||||||||||
459 | dbg.nospace() << "QModelIndex(" << idx.row() << ',' << idx.column() | - | ||||||||||||
460 | << ',' << idx.internalPointer() << ',' << idx.model() << ')'; | - | ||||||||||||
461 | return dbg; | - | ||||||||||||
462 | } | - | ||||||||||||
463 | - | |||||||||||||
464 | QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx) | - | ||||||||||||
465 | { | - | ||||||||||||
466 | if (idx.d) | - | ||||||||||||
467 | dbg << idx.d->index; | - | ||||||||||||
468 | else | - | ||||||||||||
469 | dbg << QModelIndex(); | - | ||||||||||||
470 | return dbg; | - | ||||||||||||
471 | } | - | ||||||||||||
472 | #endif | - | ||||||||||||
473 | - | |||||||||||||
474 | class QEmptyItemModel : public QAbstractItemModel | - | ||||||||||||
475 | { | - | ||||||||||||
476 | public: | - | ||||||||||||
477 | explicit QEmptyItemModel(QObject *parent = 0) : QAbstractItemModel(parent) {} | - | ||||||||||||
478 | QModelIndex index(int, int, const QModelIndex &) const Q_DECL_OVERRIDE { return QModelIndex(); } | - | ||||||||||||
479 | QModelIndex parent(const QModelIndex &) const Q_DECL_OVERRIDE { return QModelIndex(); } | - | ||||||||||||
480 | int rowCount(const QModelIndex &) const Q_DECL_OVERRIDE { return 0; } | - | ||||||||||||
481 | int columnCount(const QModelIndex &) const Q_DECL_OVERRIDE { return 0; } | - | ||||||||||||
482 | bool hasChildren(const QModelIndex &) const Q_DECL_OVERRIDE { return false; } | - | ||||||||||||
483 | QVariant data(const QModelIndex &, int) const Q_DECL_OVERRIDE { return QVariant(); } | - | ||||||||||||
484 | }; | - | ||||||||||||
485 | - | |||||||||||||
486 | Q_GLOBAL_STATIC(QEmptyItemModel, qEmptyModel) | - | ||||||||||||
487 | - | |||||||||||||
488 | - | |||||||||||||
489 | QAbstractItemModelPrivate::QAbstractItemModelPrivate() | - | ||||||||||||
490 | : QObjectPrivate(), | - | ||||||||||||
491 | supportedDragActions(-1), | - | ||||||||||||
492 | roleNames(defaultRoleNames()) | - | ||||||||||||
493 | { | - | ||||||||||||
494 | } executed 5394 times by 54 tests: end of block Executed by:
| 5394 | ||||||||||||
495 | - | |||||||||||||
496 | QAbstractItemModelPrivate::~QAbstractItemModelPrivate() | - | ||||||||||||
497 | { | - | ||||||||||||
498 | } | - | ||||||||||||
499 | - | |||||||||||||
500 | QAbstractItemModel *QAbstractItemModelPrivate::staticEmptyModel() | - | ||||||||||||
501 | { | - | ||||||||||||
502 | return qEmptyModel(); | - | ||||||||||||
503 | } | - | ||||||||||||
504 | - | |||||||||||||
505 | void QAbstractItemModelPrivate::invalidatePersistentIndexes() | - | ||||||||||||
506 | { | - | ||||||||||||
507 | for (QPersistentModelIndexData *data : qAsConst(persistent.indexes)) { | - | ||||||||||||
508 | data->index = QModelIndex(); | - | ||||||||||||
509 | data->model = 0; | - | ||||||||||||
510 | } executed 4404 times by 38 tests: end of block Executed by:
| 4404 | ||||||||||||
511 | persistent.indexes.clear(); | - | ||||||||||||
512 | } executed 14712 times by 98 tests: end of block Executed by:
| 14712 | ||||||||||||
513 | - | |||||||||||||
514 | /*! | - | ||||||||||||
515 | \internal | - | ||||||||||||
516 | Clean the QPersistentModelIndex relative to the index if there is one. | - | ||||||||||||
517 | To be used before an index is invalided | - | ||||||||||||
518 | */ | - | ||||||||||||
519 | void QAbstractItemModelPrivate::invalidatePersistentIndex(const QModelIndex &index) { | - | ||||||||||||
520 | const auto it = persistent.indexes.constFind(index); | - | ||||||||||||
521 | if (it != persistent.indexes.cend()) {
| 1130-3496222 | ||||||||||||
522 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
523 | persistent.indexes.erase(it); | - | ||||||||||||
524 | data->index = QModelIndex(); | - | ||||||||||||
525 | data->model = 0; | - | ||||||||||||
526 | } executed 1130 times by 11 tests: end of block Executed by:
| 1130 | ||||||||||||
527 | } executed 3497352 times by 37 tests: end of block Executed by:
| 3497352 | ||||||||||||
528 | - | |||||||||||||
529 | namespace { | - | ||||||||||||
530 | struct DefaultRoleNames : public QHash<int, QByteArray> | - | ||||||||||||
531 | { | - | ||||||||||||
532 | DefaultRoleNames() { | - | ||||||||||||
533 | (*this)[Qt::DisplayRole] = "display"; | - | ||||||||||||
534 | (*this)[Qt::DecorationRole] = "decoration"; | - | ||||||||||||
535 | (*this)[Qt::EditRole] = "edit"; | - | ||||||||||||
536 | (*this)[Qt::ToolTipRole] = "toolTip"; | - | ||||||||||||
537 | (*this)[Qt::StatusTipRole] = "statusTip"; | - | ||||||||||||
538 | (*this)[Qt::WhatsThisRole] = "whatsThis"; | - | ||||||||||||
539 | } | - | ||||||||||||
540 | }; | - | ||||||||||||
541 | } | - | ||||||||||||
542 | - | |||||||||||||
543 | Q_GLOBAL_STATIC(DefaultRoleNames, qDefaultRoleNames) | - | ||||||||||||
544 | - | |||||||||||||
545 | const QHash<int,QByteArray> &QAbstractItemModelPrivate::defaultRoleNames() | - | ||||||||||||
546 | { | - | ||||||||||||
547 | return *qDefaultRoleNames(); | - | ||||||||||||
548 | } | - | ||||||||||||
549 | - | |||||||||||||
550 | - | |||||||||||||
551 | static uint typeOfVariant(const QVariant &value) | - | ||||||||||||
552 | { | - | ||||||||||||
553 | //return 0 for integer, 1 for floating point and 2 for other | - | ||||||||||||
554 | switch (value.userType()) { | - | ||||||||||||
555 | case QVariant::Bool: | - | ||||||||||||
556 | case QVariant::Int: | - | ||||||||||||
557 | case QVariant::UInt: | - | ||||||||||||
558 | case QVariant::LongLong: | - | ||||||||||||
559 | case QVariant::ULongLong: | - | ||||||||||||
560 | case QVariant::Char: | - | ||||||||||||
561 | case QMetaType::Short: | - | ||||||||||||
562 | case QMetaType::UShort: | - | ||||||||||||
563 | case QMetaType::UChar: | - | ||||||||||||
564 | case QMetaType::ULong: | - | ||||||||||||
565 | case QMetaType::Long: | - | ||||||||||||
566 | return 0; | - | ||||||||||||
567 | case QVariant::Double: | - | ||||||||||||
568 | case QMetaType::Float: | - | ||||||||||||
569 | return 1; | - | ||||||||||||
570 | default: | - | ||||||||||||
571 | return 2; | - | ||||||||||||
572 | } | - | ||||||||||||
573 | } | - | ||||||||||||
574 | - | |||||||||||||
575 | /*! | - | ||||||||||||
576 | \internal | - | ||||||||||||
577 | Return \c{true} if \a value contains a numerical type. | - | ||||||||||||
578 | - | |||||||||||||
579 | This function is used by our Q{Tree,Widget,Table}WidgetModel classes to sort. | - | ||||||||||||
580 | */ | - | ||||||||||||
581 | bool QAbstractItemModelPrivate::variantLessThan(const QVariant &v1, const QVariant &v2) | - | ||||||||||||
582 | { | - | ||||||||||||
583 | switch(qMax(typeOfVariant(v1), typeOfVariant(v2))) | - | ||||||||||||
584 | { | - | ||||||||||||
585 | case 0: //integer type | - | ||||||||||||
586 | return v1.toLongLong() < v2.toLongLong(); | - | ||||||||||||
587 | case 1: //floating point | - | ||||||||||||
588 | return v1.toReal() < v2.toReal(); | - | ||||||||||||
589 | default: | - | ||||||||||||
590 | return v1.toString().localeAwareCompare(v2.toString()) < 0; | - | ||||||||||||
591 | } | - | ||||||||||||
592 | } | - | ||||||||||||
593 | - | |||||||||||||
594 | void QAbstractItemModelPrivate::removePersistentIndexData(QPersistentModelIndexData *data) | - | ||||||||||||
595 | { | - | ||||||||||||
596 | if (data->index.isValid()) {
| 2-21134 | ||||||||||||
597 | int removed = persistent.indexes.remove(data->index); | - | ||||||||||||
598 | Q_ASSERT_X(removed == 1, "QPersistentModelIndex::~QPersistentModelIndex", | - | ||||||||||||
599 | "persistent model indexes corrupted"); //maybe the index was somewhat invalid? | - | ||||||||||||
600 | // This assert may happen if the model use changePersistentIndex in a way that could result on two | - | ||||||||||||
601 | // QPersistentModelIndex pointing to the same index. | - | ||||||||||||
602 | Q_UNUSED(removed); | - | ||||||||||||
603 | } executed 21134 times by 38 tests: end of block Executed by:
| 21134 | ||||||||||||
604 | // make sure our optimization still works | - | ||||||||||||
605 | for (int i = persistent.moved.count() - 1; i >= 0; --i) {
| 0-21136 | ||||||||||||
606 | int idx = persistent.moved[.at(i].).indexOf(data); | - | ||||||||||||
607 | if (idx >= 0)
| 0 | ||||||||||||
608 | persistent.moved[i].remove(idx); never executed: persistent.moved[i].remove(idx); | 0 | ||||||||||||
609 | } never executed: end of block | 0 | ||||||||||||
610 | // update the references to invalidated persistent indexes | - | ||||||||||||
611 | for (int i = persistent.invalidated.count() - 1; i >= 0; --i) {
| 0-21136 | ||||||||||||
612 | int idx = persistent.invalidated[.at(i].).indexOf(data); | - | ||||||||||||
613 | if (idx >= 0)
| 0 | ||||||||||||
614 | persistent.invalidated[i].remove(idx); never executed: persistent.invalidated[i].remove(idx); | 0 | ||||||||||||
615 | } never executed: end of block | 0 | ||||||||||||
616 | - | |||||||||||||
617 | } executed 21136 times by 38 tests: end of block Executed by:
| 21136 | ||||||||||||
618 | - | |||||||||||||
619 | void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent, | - | ||||||||||||
620 | int first, int last) | - | ||||||||||||
621 | { | - | ||||||||||||
622 | Q_Q(QAbstractItemModel); | - | ||||||||||||
623 | Q_UNUSED(last); | - | ||||||||||||
624 | QVector<QPersistentModelIndexData *> persistent_moved; | - | ||||||||||||
625 | if (first < q->rowCount(parent)) { | - | ||||||||||||
626 | for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); | - | ||||||||||||
627 | it != persistent.indexes.constEnd(); ++it) { | - | ||||||||||||
628 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
629 | const QModelIndex &index = data->index; | - | ||||||||||||
630 | if (index.row() >= first && index.isValid() && index.parent() == parent) { | - | ||||||||||||
631 | persistent_moved.append(data); | - | ||||||||||||
632 | } | - | ||||||||||||
633 | } | - | ||||||||||||
634 | } | - | ||||||||||||
635 | persistent.moved.push(persistent_moved); | - | ||||||||||||
636 | } | - | ||||||||||||
637 | - | |||||||||||||
638 | void QAbstractItemModelPrivate::rowsInserted(const QModelIndex &parent, | - | ||||||||||||
639 | int first, int last) | - | ||||||||||||
640 | { | - | ||||||||||||
641 | QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop(); | - | ||||||||||||
642 | int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested | - | ||||||||||||
643 | for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin(); | - | ||||||||||||
644 | it != persistent_moved.constEnd(); ++it) {
| 6164-121996 | ||||||||||||
645 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
646 | QModelIndex old = data->index; | - | ||||||||||||
647 | persistent.indexes.erase(persistent.indexes.findconstFind(old)); | - | ||||||||||||
648 | data->index = q_func()->index(old.row() + count, old.column(), parent); | - | ||||||||||||
649 | if (data->index.isValid()) {
| 0-6164 | ||||||||||||
650 | persistent.insertMultiAtEnd(data->index, data); | - | ||||||||||||
651 | } else { executed 6164 times by 12 tests: end of block Executed by:
| 6164 | ||||||||||||
652 | qWarning() << "QAbstractItemModel::endInsertRows: Invalid index (" << old.row() + count << ',' << old.column() << ") in model" << q_func(); | - | ||||||||||||
653 | } never executed: end of block | 0 | ||||||||||||
654 | } | - | ||||||||||||
655 | } executed 121996 times by 45 tests: end of block Executed by:
| 121996 | ||||||||||||
656 | - | |||||||||||||
657 | void QAbstractItemModelPrivate::itemsAboutToBeMoved(const QModelIndex &srcParent, int srcFirst, int srcLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation) | - | ||||||||||||
658 | { | - | ||||||||||||
659 | QVector<QPersistentModelIndexData *> persistent_moved_explicitly; | - | ||||||||||||
660 | QVector<QPersistentModelIndexData *> persistent_moved_in_source; | - | ||||||||||||
661 | QVector<QPersistentModelIndexData *> persistent_moved_in_destination; | - | ||||||||||||
662 | - | |||||||||||||
663 | QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it; | - | ||||||||||||
664 | const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator begin = persistent.indexes.constBegin(); | - | ||||||||||||
665 | const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator end = persistent.indexes.constEnd(); | - | ||||||||||||
666 | - | |||||||||||||
667 | const bool sameParent = (srcParent == destinationParent); | - | ||||||||||||
668 | const bool movingUp = (srcFirst > destinationChild); | - | ||||||||||||
669 | - | |||||||||||||
670 | for ( it = begin; it != end; ++it) { | - | ||||||||||||
671 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
672 | const QModelIndex &index = data->index; | - | ||||||||||||
673 | const QModelIndex &parent = index.parent(); | - | ||||||||||||
674 | const bool isSourceIndex = (parent == srcParent); | - | ||||||||||||
675 | const bool isDestinationIndex = (parent == destinationParent); | - | ||||||||||||
676 | - | |||||||||||||
677 | int childPosition; | - | ||||||||||||
678 | if (orientation == Qt::Vertical) | - | ||||||||||||
679 | childPosition = index.row(); | - | ||||||||||||
680 | else | - | ||||||||||||
681 | childPosition = index.column(); | - | ||||||||||||
682 | - | |||||||||||||
683 | if (!index.isValid() || !(isSourceIndex || isDestinationIndex ) ) | - | ||||||||||||
684 | continue; | - | ||||||||||||
685 | - | |||||||||||||
686 | if (!sameParent && isDestinationIndex) { | - | ||||||||||||
687 | if (childPosition >= destinationChild) | - | ||||||||||||
688 | persistent_moved_in_destination.append(data); | - | ||||||||||||
689 | continue; | - | ||||||||||||
690 | } | - | ||||||||||||
691 | - | |||||||||||||
692 | if (sameParent && movingUp && childPosition < destinationChild) | - | ||||||||||||
693 | continue; | - | ||||||||||||
694 | - | |||||||||||||
695 | if (sameParent && !movingUp && childPosition < srcFirst ) | - | ||||||||||||
696 | continue; | - | ||||||||||||
697 | - | |||||||||||||
698 | if (!sameParent && childPosition < srcFirst) | - | ||||||||||||
699 | continue; | - | ||||||||||||
700 | - | |||||||||||||
701 | if (sameParent && (childPosition > srcLast) && (childPosition >= destinationChild )) | - | ||||||||||||
702 | continue; | - | ||||||||||||
703 | - | |||||||||||||
704 | if ((childPosition <= srcLast) && (childPosition >= srcFirst)) { | - | ||||||||||||
705 | persistent_moved_explicitly.append(data); | - | ||||||||||||
706 | } else { | - | ||||||||||||
707 | persistent_moved_in_source.append(data); | - | ||||||||||||
708 | } | - | ||||||||||||
709 | } | - | ||||||||||||
710 | persistent.moved.push(persistent_moved_explicitly); | - | ||||||||||||
711 | persistent.moved.push(persistent_moved_in_source); | - | ||||||||||||
712 | persistent.moved.push(persistent_moved_in_destination); | - | ||||||||||||
713 | } | - | ||||||||||||
714 | - | |||||||||||||
715 | /*! | - | ||||||||||||
716 | \internal | - | ||||||||||||
717 | - | |||||||||||||
718 | Moves persistent indexes \a indexes by amount \a change. The change will be either a change in row value or a change in | - | ||||||||||||
719 | column value depending on the value of \a orientation. The indexes may also be moved to a different parent if \a parent | - | ||||||||||||
720 | differs from the existing parent for the index. | - | ||||||||||||
721 | */ | - | ||||||||||||
722 | void QAbstractItemModelPrivate::movePersistentIndexes(const QVector<QPersistentModelIndexData *> &indexes, int change, const QModelIndex &parent, Qt::Orientation orientation) | - | ||||||||||||
723 | { | - | ||||||||||||
724 | QVector<QPersistentModelIndexData *>::const_iterator it; | - | ||||||||||||
725 | const QVector<QPersistentModelIndexData *>::const_iterator begin = indexes.constBegin(); | - | ||||||||||||
726 | const QVector<QPersistentModelIndexData *>::const_iterator end = indexes.constEnd(); | - | ||||||||||||
727 | - | |||||||||||||
728 | for (it = begin; it != end; ++it)
| 216-1178 | ||||||||||||
729 | { | - | ||||||||||||
730 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
731 | - | |||||||||||||
732 | int row = data->index.row(); | - | ||||||||||||
733 | int column = data->index.column(); | - | ||||||||||||
734 | - | |||||||||||||
735 | if (Qt::Vertical == orientation)
| 0-1178 | ||||||||||||
736 | row += change; executed 1178 times by 2 tests: row += change; Executed by:
| 1178 | ||||||||||||
737 | else | - | ||||||||||||
738 | column += change; never executed: column += change; | 0 | ||||||||||||
739 | - | |||||||||||||
740 | persistent.indexes.erase(persistent.indexes.findconstFind(data->index)); | - | ||||||||||||
741 | data->index = q_func()->index(row, column, parent); | - | ||||||||||||
742 | if (data->index.isValid()) {
| 0-1178 | ||||||||||||
743 | persistent.insertMultiAtEnd(data->index, data); | - | ||||||||||||
744 | } else { executed 1178 times by 2 tests: end of block Executed by:
| 1178 | ||||||||||||
745 | qWarning() << "QAbstractItemModel::endMoveRows: Invalid index (" << row << "," << column << ") in model" << q_func(); | - | ||||||||||||
746 | } never executed: end of block | 0 | ||||||||||||
747 | } | - | ||||||||||||
748 | } executed 216 times by 5 tests: end of block Executed by:
| 216 | ||||||||||||
749 | - | |||||||||||||
750 | void QAbstractItemModelPrivate::itemsMoved(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild, Qt::Orientation orientation) | - | ||||||||||||
751 | { | - | ||||||||||||
752 | QVector<QPersistentModelIndexData *> moved_in_destination = persistent.moved.pop(); | - | ||||||||||||
753 | QVector<QPersistentModelIndexData *> moved_in_source = persistent.moved.pop(); | - | ||||||||||||
754 | QVector<QPersistentModelIndexData *> moved_explicitly = persistent.moved.pop(); | - | ||||||||||||
755 | - | |||||||||||||
756 | const bool sameParent = (sourceParent == destinationParent); | - | ||||||||||||
757 | const bool movingUp = (sourceFirst > destinationChild); | - | ||||||||||||
758 | - | |||||||||||||
759 | const int explicit_change = (!sameParent || movingUp) ? destinationChild - sourceFirst : destinationChild - sourceLast - 1 ; | - | ||||||||||||
760 | const int source_change = (!sameParent || !movingUp) ? -1*(sourceLast - sourceFirst + 1) : sourceLast - sourceFirst + 1 ; | - | ||||||||||||
761 | const int destination_change = sourceLast - sourceFirst + 1; | - | ||||||||||||
762 | - | |||||||||||||
763 | movePersistentIndexes(moved_explicitly, explicit_change, destinationParent, orientation); | - | ||||||||||||
764 | movePersistentIndexes(moved_in_source, source_change, sourceParent, orientation); | - | ||||||||||||
765 | movePersistentIndexes(moved_in_destination, destination_change, destinationParent, orientation); | - | ||||||||||||
766 | } | - | ||||||||||||
767 | - | |||||||||||||
768 | void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent, | - | ||||||||||||
769 | int first, int last) | - | ||||||||||||
770 | { | - | ||||||||||||
771 | QVector<QPersistentModelIndexData *> persistent_moved; | - | ||||||||||||
772 | QVector<QPersistentModelIndexData *> persistent_invalidated; | - | ||||||||||||
773 | // find the persistent indexes that are affected by the change, either by being in the removed subtree | - | ||||||||||||
774 | // or by being on the same level and below the removed rows | - | ||||||||||||
775 | for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); | - | ||||||||||||
776 | it != persistent.indexes.constEnd(); ++it) { | - | ||||||||||||
777 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
778 | bool level_changed = false; | - | ||||||||||||
779 | QModelIndex current = data->index; | - | ||||||||||||
780 | while (current.isValid()) { | - | ||||||||||||
781 | QModelIndex current_parent = current.parent(); | - | ||||||||||||
782 | if (current_parent == parent) { // on the same level as the change | - | ||||||||||||
783 | if (!level_changed && current.row() > last) // below the removed rows | - | ||||||||||||
784 | persistent_moved.append(data); | - | ||||||||||||
785 | else if (current.row() <= last && current.row() >= first) // in the removed subtree | - | ||||||||||||
786 | persistent_invalidated.append(data); | - | ||||||||||||
787 | break; | - | ||||||||||||
788 | } | - | ||||||||||||
789 | current = current_parent; | - | ||||||||||||
790 | level_changed = true; | - | ||||||||||||
791 | } | - | ||||||||||||
792 | } | - | ||||||||||||
793 | - | |||||||||||||
794 | persistent.moved.push(persistent_moved); | - | ||||||||||||
795 | persistent.invalidated.push(persistent_invalidated); | - | ||||||||||||
796 | } | - | ||||||||||||
797 | - | |||||||||||||
798 | void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent, | - | ||||||||||||
799 | int first, int last) | - | ||||||||||||
800 | { | - | ||||||||||||
801 | QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop(); | - | ||||||||||||
802 | int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested | - | ||||||||||||
803 | for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin(); | - | ||||||||||||
804 | it != persistent_moved.constEnd(); ++it) {
| 1097-2919 | ||||||||||||
805 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
806 | QModelIndex old = data->index; | - | ||||||||||||
807 | persistent.indexes.erase(persistent.indexes.findconstFind(old)); | - | ||||||||||||
808 | data->index = q_func()->index(old.row() - count, old.column(), parent); | - | ||||||||||||
809 | if (data->index.isValid()) {
| 0-1097 | ||||||||||||
810 | persistent.insertMultiAtEnd(data->index, data); | - | ||||||||||||
811 | } else { executed 1097 times by 12 tests: end of block Executed by:
| 1097 | ||||||||||||
812 | qWarning() << "QAbstractItemModel::endRemoveRows: Invalid index (" << old.row() - count << ',' << old.column() << ") in model" << q_func(); | - | ||||||||||||
813 | } never executed: end of block | 0 | ||||||||||||
814 | } | - | ||||||||||||
815 | QVector<QPersistentModelIndexData *> persistent_invalidated = persistent.invalidated.pop(); | - | ||||||||||||
816 | for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_invalidated.constBegin(); | - | ||||||||||||
817 | it != persistent_invalidated.constEnd(); ++it) {
| 1191-2919 | ||||||||||||
818 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
819 | persistent.indexes.erase(persistent.indexes.findconstFind(data->index)); | - | ||||||||||||
820 | data->index = QModelIndex(); | - | ||||||||||||
821 | data->model = 0; | - | ||||||||||||
822 | } executed 1191 times by 15 tests: end of block Executed by:
| 1191 | ||||||||||||
823 | } executed 2919 times by 31 tests: end of block Executed by:
| 2919 | ||||||||||||
824 | - | |||||||||||||
825 | void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &parent, | - | ||||||||||||
826 | int first, int last) | - | ||||||||||||
827 | { | - | ||||||||||||
828 | Q_Q(QAbstractItemModel); | - | ||||||||||||
829 | Q_UNUSED(last); | - | ||||||||||||
830 | QVector<QPersistentModelIndexData *> persistent_moved; | - | ||||||||||||
831 | if (first < q->columnCount(parent)) { | - | ||||||||||||
832 | for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); | - | ||||||||||||
833 | it != persistent.indexes.constEnd(); ++it) { | - | ||||||||||||
834 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
835 | const QModelIndex &index = data->index; | - | ||||||||||||
836 | if (index.column() >= first && index.isValid() && index.parent() == parent) | - | ||||||||||||
837 | persistent_moved.append(data); | - | ||||||||||||
838 | } | - | ||||||||||||
839 | } | - | ||||||||||||
840 | persistent.moved.push(persistent_moved); | - | ||||||||||||
841 | } | - | ||||||||||||
842 | - | |||||||||||||
843 | void QAbstractItemModelPrivate::columnsInserted(const QModelIndex &parent, | - | ||||||||||||
844 | int first, int last) | - | ||||||||||||
845 | { | - | ||||||||||||
846 | QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop(); | - | ||||||||||||
847 | int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested | - | ||||||||||||
848 | for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin(); | - | ||||||||||||
849 | it != persistent_moved.constEnd(); ++it) {
| 15-13532 | ||||||||||||
850 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
851 | QModelIndex old = data->index; | - | ||||||||||||
852 | persistent.indexes.erase(persistent.indexes.findconstFind(old)); | - | ||||||||||||
853 | data->index = q_func()->index(old.row(), old.column() + count, parent); | - | ||||||||||||
854 | if (data->index.isValid()) {
| 0-15 | ||||||||||||
855 | persistent.insertMultiAtEnd(data->index, data); | - | ||||||||||||
856 | } else { executed 15 times by 4 tests: end of block Executed by:
| 15 | ||||||||||||
857 | qWarning() << "QAbstractItemModel::endInsertColumns: Invalid index (" << old.row() << ',' << old.column() + count << ") in model" << q_func(); | - | ||||||||||||
858 | } never executed: end of block | 0 | ||||||||||||
859 | } | - | ||||||||||||
860 | } executed 13532 times by 33 tests: end of block Executed by:
| 13532 | ||||||||||||
861 | - | |||||||||||||
862 | void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &parent, | - | ||||||||||||
863 | int first, int last) | - | ||||||||||||
864 | { | - | ||||||||||||
865 | QVector<QPersistentModelIndexData *> persistent_moved; | - | ||||||||||||
866 | QVector<QPersistentModelIndexData *> persistent_invalidated; | - | ||||||||||||
867 | // find the persistent indexes that are affected by the change, either by being in the removed subtree | - | ||||||||||||
868 | // or by being on the same level and to the right of the removed columns | - | ||||||||||||
869 | for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin(); | - | ||||||||||||
870 | it != persistent.indexes.constEnd(); ++it) { | - | ||||||||||||
871 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
872 | bool level_changed = false; | - | ||||||||||||
873 | QModelIndex current = data->index; | - | ||||||||||||
874 | while (current.isValid()) { | - | ||||||||||||
875 | QModelIndex current_parent = current.parent(); | - | ||||||||||||
876 | if (current_parent == parent) { // on the same level as the change | - | ||||||||||||
877 | if (!level_changed && current.column() > last) // right of the removed columns | - | ||||||||||||
878 | persistent_moved.append(data); | - | ||||||||||||
879 | else if (current.column() <= last && current.column() >= first) // in the removed subtree | - | ||||||||||||
880 | persistent_invalidated.append(data); | - | ||||||||||||
881 | break; | - | ||||||||||||
882 | } | - | ||||||||||||
883 | current = current_parent; | - | ||||||||||||
884 | level_changed = true; | - | ||||||||||||
885 | } | - | ||||||||||||
886 | } | - | ||||||||||||
887 | - | |||||||||||||
888 | persistent.moved.push(persistent_moved); | - | ||||||||||||
889 | persistent.invalidated.push(persistent_invalidated); | - | ||||||||||||
890 | - | |||||||||||||
891 | } | - | ||||||||||||
892 | - | |||||||||||||
893 | void QAbstractItemModelPrivate::columnsRemoved(const QModelIndex &parent, | - | ||||||||||||
894 | int first, int last) | - | ||||||||||||
895 | { | - | ||||||||||||
896 | QVector<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop(); | - | ||||||||||||
897 | int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested | - | ||||||||||||
898 | for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_moved.constBegin(); | - | ||||||||||||
899 | it != persistent_moved.constEnd(); ++it) {
| 15-210 | ||||||||||||
900 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
901 | QModelIndex old = data->index; | - | ||||||||||||
902 | persistent.indexes.erase(persistent.indexes.findconstFind(old)); | - | ||||||||||||
903 | data->index = q_func()->index(old.row(), old.column() - count, parent); | - | ||||||||||||
904 | if (data->index.isValid()) {
| 0-15 | ||||||||||||
905 | persistent.insertMultiAtEnd(data->index, data); | - | ||||||||||||
906 | } else { executed 15 times by 6 tests: end of block Executed by:
| 15 | ||||||||||||
907 | qWarning() << "QAbstractItemModel::endRemoveColumns: Invalid index (" << old.row() << ',' << old.column() - count << ") in model" << q_func(); | - | ||||||||||||
908 | } never executed: end of block | 0 | ||||||||||||
909 | } | - | ||||||||||||
910 | QVector<QPersistentModelIndexData *> persistent_invalidated = persistent.invalidated.pop(); | - | ||||||||||||
911 | for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_invalidated.constBegin(); | - | ||||||||||||
912 | it != persistent_invalidated.constEnd(); ++it) {
| 26-210 | ||||||||||||
913 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
914 | persistent.indexes.erase(persistent.indexes.findconstFind(data->index)); | - | ||||||||||||
915 | data->index = QModelIndex(); | - | ||||||||||||
916 | data->model = 0; | - | ||||||||||||
917 | } executed 26 times by 7 tests: end of block Executed by:
| 26 | ||||||||||||
918 | } executed 210 times by 16 tests: end of block Executed by:
| 210 | ||||||||||||
919 | - | |||||||||||||
920 | /*! | - | ||||||||||||
921 | \since 4.8 | - | ||||||||||||
922 | - | |||||||||||||
923 | This slot is called just after the internal data of a model is cleared | - | ||||||||||||
924 | while it is being reset. | - | ||||||||||||
925 | - | |||||||||||||
926 | This slot is provided the convenience of subclasses of concrete proxy | - | ||||||||||||
927 | models, such as subclasses of QSortFilterProxyModel which maintain extra | - | ||||||||||||
928 | data. | - | ||||||||||||
929 | - | |||||||||||||
930 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 12 | - | ||||||||||||
931 | - | |||||||||||||
932 | \note Due to a mistake, this slot is missing in Qt 5.0. | - | ||||||||||||
933 | - | |||||||||||||
934 | \sa modelAboutToBeReset(), modelReset() | - | ||||||||||||
935 | */ | - | ||||||||||||
936 | void QAbstractItemModel::resetInternalData() | - | ||||||||||||
937 | { | - | ||||||||||||
938 | - | |||||||||||||
939 | } | - | ||||||||||||
940 | - | |||||||||||||
941 | /*! | - | ||||||||||||
942 | \class QModelIndex | - | ||||||||||||
943 | \inmodule QtCore | - | ||||||||||||
944 | - | |||||||||||||
945 | \brief The QModelIndex class is used to locate data in a data model. | - | ||||||||||||
946 | - | |||||||||||||
947 | \ingroup model-view | - | ||||||||||||
948 | - | |||||||||||||
949 | - | |||||||||||||
950 | This class is used as an index into item models derived from | - | ||||||||||||
951 | QAbstractItemModel. The index is used by item views, delegates, and | - | ||||||||||||
952 | selection models to locate an item in the model. | - | ||||||||||||
953 | - | |||||||||||||
954 | New QModelIndex objects are created by the model using the | - | ||||||||||||
955 | QAbstractItemModel::createIndex() function. An \e invalid model index can | - | ||||||||||||
956 | be constructed with the QModelIndex constructor. Invalid indexes are often | - | ||||||||||||
957 | used as parent indexes when referring to top-level items in a model. | - | ||||||||||||
958 | - | |||||||||||||
959 | Model indexes refer to items in models, and contain all the information | - | ||||||||||||
960 | required to specify their locations in those models. Each index is located | - | ||||||||||||
961 | in a given row and column, and may have a parent index; use row(), | - | ||||||||||||
962 | column(), and parent() to obtain this information. Each top-level item in a | - | ||||||||||||
963 | model is represented by a model index that does not have a parent index - | - | ||||||||||||
964 | in this case, parent() will return an invalid model index, equivalent to an | - | ||||||||||||
965 | index constructed with the zero argument form of the QModelIndex() | - | ||||||||||||
966 | constructor. | - | ||||||||||||
967 | - | |||||||||||||
968 | To obtain a model index that refers to an existing item in a model, call | - | ||||||||||||
969 | QAbstractItemModel::index() with the required row and column values, and | - | ||||||||||||
970 | the model index of the parent. When referring to top-level items in a | - | ||||||||||||
971 | model, supply QModelIndex() as the parent index. | - | ||||||||||||
972 | - | |||||||||||||
973 | The model() function returns the model that the index references as a | - | ||||||||||||
974 | QAbstractItemModel. The child() function is used to examine items held | - | ||||||||||||
975 | under the index in the model. The sibling() function allows you to traverse | - | ||||||||||||
976 | items in the model on the same level as the index. | - | ||||||||||||
977 | - | |||||||||||||
978 | \note Model indexes should be used immediately and then discarded. You | - | ||||||||||||
979 | should not rely on indexes to remain valid after calling model functions | - | ||||||||||||
980 | that change the structure of the model or delete items. If you need to | - | ||||||||||||
981 | keep a model index over time use a QPersistentModelIndex. | - | ||||||||||||
982 | - | |||||||||||||
983 | \sa {Model/View Programming}, QPersistentModelIndex, QAbstractItemModel | - | ||||||||||||
984 | */ | - | ||||||||||||
985 | - | |||||||||||||
986 | /*! | - | ||||||||||||
987 | \fn QModelIndex::QModelIndex() | - | ||||||||||||
988 | - | |||||||||||||
989 | Creates a new empty model index. This type of model index is used to | - | ||||||||||||
990 | indicate that the position in the model is invalid. | - | ||||||||||||
991 | - | |||||||||||||
992 | \sa isValid(), QAbstractItemModel | - | ||||||||||||
993 | */ | - | ||||||||||||
994 | - | |||||||||||||
995 | /*! | - | ||||||||||||
996 | \fn QModelIndex::QModelIndex(int row, int column, void *data, const QAbstractItemModel *model) | - | ||||||||||||
997 | - | |||||||||||||
998 | \internal | - | ||||||||||||
999 | - | |||||||||||||
1000 | Creates a new model index at the given \a row and \a column, | - | ||||||||||||
1001 | pointing to some \a data. | - | ||||||||||||
1002 | */ | - | ||||||||||||
1003 | - | |||||||||||||
1004 | /*! | - | ||||||||||||
1005 | \fn int QModelIndex::row() const | - | ||||||||||||
1006 | - | |||||||||||||
1007 | Returns the row this model index refers to. | - | ||||||||||||
1008 | */ | - | ||||||||||||
1009 | - | |||||||||||||
1010 | - | |||||||||||||
1011 | /*! | - | ||||||||||||
1012 | \fn int QModelIndex::column() const | - | ||||||||||||
1013 | - | |||||||||||||
1014 | Returns the column this model index refers to. | - | ||||||||||||
1015 | */ | - | ||||||||||||
1016 | - | |||||||||||||
1017 | - | |||||||||||||
1018 | /*! | - | ||||||||||||
1019 | \fn void *QModelIndex::internalPointer() const | - | ||||||||||||
1020 | - | |||||||||||||
1021 | Returns a \c{void} \c{*} pointer used by the model to associate | - | ||||||||||||
1022 | the index with the internal data structure. | - | ||||||||||||
1023 | - | |||||||||||||
1024 | \sa QAbstractItemModel::createIndex() | - | ||||||||||||
1025 | */ | - | ||||||||||||
1026 | - | |||||||||||||
1027 | /*! | - | ||||||||||||
1028 | \fn quintptr QModelIndex::internalId() const | - | ||||||||||||
1029 | - | |||||||||||||
1030 | Returns a \c{quintptr} used by the model to associate | - | ||||||||||||
1031 | the index with the internal data structure. | - | ||||||||||||
1032 | - | |||||||||||||
1033 | \sa QAbstractItemModel::createIndex() | - | ||||||||||||
1034 | */ | - | ||||||||||||
1035 | - | |||||||||||||
1036 | /*! | - | ||||||||||||
1037 | \fn bool QModelIndex::isValid() const | - | ||||||||||||
1038 | - | |||||||||||||
1039 | Returns \c{true} if this model index is valid; otherwise returns \c{false}. | - | ||||||||||||
1040 | - | |||||||||||||
1041 | A valid index belongs to a model, and has non-negative row and column | - | ||||||||||||
1042 | numbers. | - | ||||||||||||
1043 | - | |||||||||||||
1044 | \sa model(), row(), column() | - | ||||||||||||
1045 | */ | - | ||||||||||||
1046 | - | |||||||||||||
1047 | /*! | - | ||||||||||||
1048 | \fn const QAbstractItemModel *QModelIndex::model() const | - | ||||||||||||
1049 | - | |||||||||||||
1050 | Returns a pointer to the model containing the item that this index | - | ||||||||||||
1051 | refers to. | - | ||||||||||||
1052 | - | |||||||||||||
1053 | A const pointer to the model is returned because calls to non-const | - | ||||||||||||
1054 | functions of the model might invalidate the model index and possibly | - | ||||||||||||
1055 | crash your application. | - | ||||||||||||
1056 | */ | - | ||||||||||||
1057 | - | |||||||||||||
1058 | /*! | - | ||||||||||||
1059 | \fn QModelIndex QModelIndex::sibling(int row, int column) const | - | ||||||||||||
1060 | - | |||||||||||||
1061 | Returns the sibling at \a row and \a column. If there is no sibling at this | - | ||||||||||||
1062 | position, an invalid QModelIndex is returned. | - | ||||||||||||
1063 | - | |||||||||||||
1064 | \sa parent(), child() | - | ||||||||||||
1065 | */ | - | ||||||||||||
1066 | - | |||||||||||||
1067 | /*! | - | ||||||||||||
1068 | \fn QModelIndex QModelIndex::child(int row, int column) const | - | ||||||||||||
1069 | - | |||||||||||||
1070 | Returns the child of the model index that is stored in the given \a row and | - | ||||||||||||
1071 | \a column. | - | ||||||||||||
1072 | - | |||||||||||||
1073 | \note This function does not work for an invalid model index which is often | - | ||||||||||||
1074 | used as the root index. | - | ||||||||||||
1075 | - | |||||||||||||
1076 | \sa parent(), sibling() | - | ||||||||||||
1077 | */ | - | ||||||||||||
1078 | - | |||||||||||||
1079 | /*! | - | ||||||||||||
1080 | \fn QVariant QModelIndex::data(int role) const | - | ||||||||||||
1081 | - | |||||||||||||
1082 | Returns the data for the given \a role for the item referred to by the | - | ||||||||||||
1083 | index. | - | ||||||||||||
1084 | */ | - | ||||||||||||
1085 | - | |||||||||||||
1086 | /*! | - | ||||||||||||
1087 | \fn Qt::ItemFlags QModelIndex::flags() const | - | ||||||||||||
1088 | \since 4.2 | - | ||||||||||||
1089 | - | |||||||||||||
1090 | Returns the flags for the item referred to by the index. | - | ||||||||||||
1091 | */ | - | ||||||||||||
1092 | - | |||||||||||||
1093 | /*! | - | ||||||||||||
1094 | \fn bool QModelIndex::operator==(const QModelIndex &other) const | - | ||||||||||||
1095 | - | |||||||||||||
1096 | Returns \c{true} if this model index refers to the same location as the | - | ||||||||||||
1097 | \a other model index; otherwise returns \c{false}. | - | ||||||||||||
1098 | - | |||||||||||||
1099 | All values in the model index are used when comparing with another model | - | ||||||||||||
1100 | index. | - | ||||||||||||
1101 | */ | - | ||||||||||||
1102 | - | |||||||||||||
1103 | - | |||||||||||||
1104 | /*! | - | ||||||||||||
1105 | \fn bool QModelIndex::operator!=(const QModelIndex &other) const | - | ||||||||||||
1106 | - | |||||||||||||
1107 | Returns \c{true} if this model index does not refer to the same location as | - | ||||||||||||
1108 | the \a other model index; otherwise returns \c{false}. | - | ||||||||||||
1109 | */ | - | ||||||||||||
1110 | - | |||||||||||||
1111 | - | |||||||||||||
1112 | /*! | - | ||||||||||||
1113 | \fn QModelIndex QModelIndex::parent() const | - | ||||||||||||
1114 | - | |||||||||||||
1115 | Returns the parent of the model index, or QModelIndex() if it has no | - | ||||||||||||
1116 | parent. | - | ||||||||||||
1117 | - | |||||||||||||
1118 | \sa child(), sibling(), model() | - | ||||||||||||
1119 | */ | - | ||||||||||||
1120 | - | |||||||||||||
1121 | /*! | - | ||||||||||||
1122 | \class QAbstractItemModel | - | ||||||||||||
1123 | \inmodule QtCore | - | ||||||||||||
1124 | - | |||||||||||||
1125 | \brief The QAbstractItemModel class provides the abstract interface for | - | ||||||||||||
1126 | item model classes. | - | ||||||||||||
1127 | - | |||||||||||||
1128 | \ingroup model-view | - | ||||||||||||
1129 | - | |||||||||||||
1130 | - | |||||||||||||
1131 | The QAbstractItemModel class defines the standard interface that item | - | ||||||||||||
1132 | models must use to be able to interoperate with other components in the | - | ||||||||||||
1133 | model/view architecture. It is not supposed to be instantiated directly. | - | ||||||||||||
1134 | Instead, you should subclass it to create new models. | - | ||||||||||||
1135 | - | |||||||||||||
1136 | The QAbstractItemModel class is one of the \l{Model/View Classes} | - | ||||||||||||
1137 | and is part of Qt's \l{Model/View Programming}{model/view framework}. It | - | ||||||||||||
1138 | can be used as the underlying data model for the item view elements in | - | ||||||||||||
1139 | QML or the item view classes in the Qt Widgets module. | - | ||||||||||||
1140 | - | |||||||||||||
1141 | If you need a model to use with an item view such as QML's List View | - | ||||||||||||
1142 | element or the C++ widgets QListView or QTableView, you should consider | - | ||||||||||||
1143 | subclassing QAbstractListModel or QAbstractTableModel instead of this class. | - | ||||||||||||
1144 | - | |||||||||||||
1145 | The underlying data model is exposed to views and delegates as a hierarchy | - | ||||||||||||
1146 | of tables. If you do not make use of the hierarchy, then the model is a | - | ||||||||||||
1147 | simple table of rows and columns. Each item has a unique index specified by | - | ||||||||||||
1148 | a QModelIndex. | - | ||||||||||||
1149 | - | |||||||||||||
1150 | \image modelindex-no-parent.png | - | ||||||||||||
1151 | - | |||||||||||||
1152 | Every item of data that can be accessed via a model has an associated model | - | ||||||||||||
1153 | index. You can obtain this model index using the index() function. Each | - | ||||||||||||
1154 | index may have a sibling() index; child items have a parent() index. | - | ||||||||||||
1155 | - | |||||||||||||
1156 | Each item has a number of data elements associated with it and they can be | - | ||||||||||||
1157 | retrieved by specifying a role (see \l Qt::ItemDataRole) to the model's | - | ||||||||||||
1158 | data() function. Data for all available roles can be obtained at the same | - | ||||||||||||
1159 | time using the itemData() function. | - | ||||||||||||
1160 | - | |||||||||||||
1161 | Data for each role is set using a particular \l Qt::ItemDataRole. Data for | - | ||||||||||||
1162 | individual roles are set individually with setData(), or they can be set | - | ||||||||||||
1163 | for all roles with setItemData(). | - | ||||||||||||
1164 | - | |||||||||||||
1165 | Items can be queried with flags() (see \l Qt::ItemFlag) to see if they can | - | ||||||||||||
1166 | be selected, dragged, or manipulated in other ways. | - | ||||||||||||
1167 | - | |||||||||||||
1168 | If an item has child objects, hasChildren() returns \c{true} for the | - | ||||||||||||
1169 | corresponding index. | - | ||||||||||||
1170 | - | |||||||||||||
1171 | The model has a rowCount() and a columnCount() for each level of the | - | ||||||||||||
1172 | hierarchy. Rows and columns can be inserted and removed with insertRows(), | - | ||||||||||||
1173 | insertColumns(), removeRows(), and removeColumns(). | - | ||||||||||||
1174 | - | |||||||||||||
1175 | The model emits signals to indicate changes. For example, dataChanged() is | - | ||||||||||||
1176 | emitted whenever items of data made available by the model are changed. | - | ||||||||||||
1177 | Changes to the headers supplied by the model cause headerDataChanged() to | - | ||||||||||||
1178 | be emitted. If the structure of the underlying data changes, the model can | - | ||||||||||||
1179 | emit layoutChanged() to indicate to any attached views that they should | - | ||||||||||||
1180 | redisplay any items shown, taking the new structure into account. | - | ||||||||||||
1181 | - | |||||||||||||
1182 | The items available through the model can be searched for particular data | - | ||||||||||||
1183 | using the match() function. | - | ||||||||||||
1184 | - | |||||||||||||
1185 | To sort the model, you can use sort(). | - | ||||||||||||
1186 | - | |||||||||||||
1187 | - | |||||||||||||
1188 | \section1 Subclassing | - | ||||||||||||
1189 | - | |||||||||||||
1190 | \note Some general guidelines for subclassing models are available in the | - | ||||||||||||
1191 | \l{Model Subclassing Reference}. | - | ||||||||||||
1192 | - | |||||||||||||
1193 | When subclassing QAbstractItemModel, at the very least you must implement | - | ||||||||||||
1194 | index(), parent(), rowCount(), columnCount(), and data(). These functions | - | ||||||||||||
1195 | are used in all read-only models, and form the basis of editable models. | - | ||||||||||||
1196 | - | |||||||||||||
1197 | You can also reimplement hasChildren() to provide special behavior for | - | ||||||||||||
1198 | models where the implementation of rowCount() is expensive. This makes it | - | ||||||||||||
1199 | possible for models to restrict the amount of data requested by views, and | - | ||||||||||||
1200 | can be used as a way to implement lazy population of model data. | - | ||||||||||||
1201 | - | |||||||||||||
1202 | To enable editing in your model, you must also implement setData(), and | - | ||||||||||||
1203 | reimplement flags() to ensure that \c ItemIsEditable is returned. You can | - | ||||||||||||
1204 | also reimplement headerData() and setHeaderData() to control the way the | - | ||||||||||||
1205 | headers for your model are presented. | - | ||||||||||||
1206 | - | |||||||||||||
1207 | The dataChanged() and headerDataChanged() signals must be emitted | - | ||||||||||||
1208 | explicitly when reimplementing the setData() and setHeaderData() functions, | - | ||||||||||||
1209 | respectively. | - | ||||||||||||
1210 | - | |||||||||||||
1211 | Custom models need to create model indexes for other components to use. To | - | ||||||||||||
1212 | do this, call createIndex() with suitable row and column numbers for the | - | ||||||||||||
1213 | item, and an identifier for it, either as a pointer or as an integer value. | - | ||||||||||||
1214 | The combination of these values must be unique for each item. Custom models | - | ||||||||||||
1215 | typically use these unique identifiers in other reimplemented functions to | - | ||||||||||||
1216 | retrieve item data and access information about the item's parents and | - | ||||||||||||
1217 | children. See the \l{Simple Tree Model Example} for more information about | - | ||||||||||||
1218 | unique identifiers. | - | ||||||||||||
1219 | - | |||||||||||||
1220 | It is not necessary to support every role defined in Qt::ItemDataRole. | - | ||||||||||||
1221 | Depending on the type of data contained within a model, it may only be | - | ||||||||||||
1222 | useful to implement the data() function to return valid information for | - | ||||||||||||
1223 | some of the more common roles. Most models provide at least a textual | - | ||||||||||||
1224 | representation of item data for the Qt::DisplayRole, and well-behaved | - | ||||||||||||
1225 | models should also provide valid information for the Qt::ToolTipRole and | - | ||||||||||||
1226 | Qt::WhatsThisRole. Supporting these roles enables models to be used with | - | ||||||||||||
1227 | standard Qt views. However, for some models that handle highly-specialized | - | ||||||||||||
1228 | data, it may be appropriate to provide data only for user-defined roles. | - | ||||||||||||
1229 | - | |||||||||||||
1230 | Models that provide interfaces to resizable data structures can provide | - | ||||||||||||
1231 | implementations of insertRows(), removeRows(), insertColumns(),and | - | ||||||||||||
1232 | removeColumns(). When implementing these functions, it is important to | - | ||||||||||||
1233 | notify any connected views about changes to the model's dimensions both | - | ||||||||||||
1234 | \e before and \e after they occur: | - | ||||||||||||
1235 | - | |||||||||||||
1236 | \list | - | ||||||||||||
1237 | \li An insertRows() implementation must call beginInsertRows() \e before | - | ||||||||||||
1238 | inserting new rows into the data structure, and endInsertRows() | - | ||||||||||||
1239 | \e{immediately afterwards}. | - | ||||||||||||
1240 | \li An insertColumns() implementation must call beginInsertColumns() | - | ||||||||||||
1241 | \e before inserting new columns into the data structure, and | - | ||||||||||||
1242 | endInsertColumns() \e{immediately afterwards}. | - | ||||||||||||
1243 | \li A removeRows() implementation must call beginRemoveRows() \e before | - | ||||||||||||
1244 | the rows are removed from the data structure, and endRemoveRows() | - | ||||||||||||
1245 | \e{immediately afterwards}. | - | ||||||||||||
1246 | \li A removeColumns() implementation must call beginRemoveColumns() | - | ||||||||||||
1247 | \e before the columns are removed from the data structure, and | - | ||||||||||||
1248 | endRemoveColumns() \e{immediately afterwards}. | - | ||||||||||||
1249 | \endlist | - | ||||||||||||
1250 | - | |||||||||||||
1251 | The \e private signals that these functions emit give attached components | - | ||||||||||||
1252 | the chance to take action before any data becomes unavailable. The | - | ||||||||||||
1253 | encapsulation of the insert and remove operations with these begin and end | - | ||||||||||||
1254 | functions also enables the model to manage \l{QPersistentModelIndex} | - | ||||||||||||
1255 | {persistent model indexes} correctly. \b{If you want selections to be | - | ||||||||||||
1256 | handled properly, you must ensure that you call these functions.} If you | - | ||||||||||||
1257 | insert or remove an item with children, you do not need to call these | - | ||||||||||||
1258 | functions for the child items. In other words, the parent item will take | - | ||||||||||||
1259 | care of its child items. | - | ||||||||||||
1260 | - | |||||||||||||
1261 | To create models that populate incrementally, you can reimplement | - | ||||||||||||
1262 | fetchMore() and canFetchMore(). If the reimplementation of fetchMore() adds | - | ||||||||||||
1263 | rows to the model, \l{QAbstractItemModel::}{beginInsertRows()} and | - | ||||||||||||
1264 | \l{QAbstractItemModel::}{endInsertRows()} must be called. | - | ||||||||||||
1265 | - | |||||||||||||
1266 | \sa {Model Classes}, {Model Subclassing Reference}, QModelIndex, | - | ||||||||||||
1267 | QAbstractItemView, {Using drag and drop with item views}, | - | ||||||||||||
1268 | {Simple DOM Model Example}, {Simple Tree Model Example}, | - | ||||||||||||
1269 | {Editable Tree Model Example}, {Fetch More Example} | - | ||||||||||||
1270 | */ | - | ||||||||||||
1271 | - | |||||||||||||
1272 | /*! | - | ||||||||||||
1273 | \fn QModelIndex QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const = 0 | - | ||||||||||||
1274 | - | |||||||||||||
1275 | Returns the index of the item in the model specified by the given \a row, | - | ||||||||||||
1276 | \a column and \a parent index. | - | ||||||||||||
1277 | - | |||||||||||||
1278 | When reimplementing this function in a subclass, call createIndex() to | - | ||||||||||||
1279 | generate model indexes that other components can use to refer to items in | - | ||||||||||||
1280 | your model. | - | ||||||||||||
1281 | - | |||||||||||||
1282 | \sa createIndex() | - | ||||||||||||
1283 | */ | - | ||||||||||||
1284 | - | |||||||||||||
1285 | /*! | - | ||||||||||||
1286 | \fn bool QAbstractItemModel::insertColumn(int column, const QModelIndex &parent) | - | ||||||||||||
1287 | - | |||||||||||||
1288 | Inserts a single column before the given \a column in the child items of | - | ||||||||||||
1289 | the \a parent specified. | - | ||||||||||||
1290 | - | |||||||||||||
1291 | Returns \c{true} if the column is inserted; otherwise returns \c{false}. | - | ||||||||||||
1292 | - | |||||||||||||
1293 | \sa insertColumns(), insertRow(), removeColumn() | - | ||||||||||||
1294 | */ | - | ||||||||||||
1295 | - | |||||||||||||
1296 | /*! | - | ||||||||||||
1297 | \fn bool QAbstractItemModel::insertRow(int row, const QModelIndex &parent) | - | ||||||||||||
1298 | - | |||||||||||||
1299 | Inserts a single row before the given \a row in the child items of the | - | ||||||||||||
1300 | \a parent specified. | - | ||||||||||||
1301 | - | |||||||||||||
1302 | \note This function calls the virtual method insertRows. | - | ||||||||||||
1303 | - | |||||||||||||
1304 | Returns \c{true} if the row is inserted; otherwise returns \c{false}. | - | ||||||||||||
1305 | - | |||||||||||||
1306 | \sa insertRows(), insertColumn(), removeRow() | - | ||||||||||||
1307 | */ | - | ||||||||||||
1308 | - | |||||||||||||
1309 | /*! | - | ||||||||||||
1310 | \fn QModelIndex QAbstractItemModel::parent(const QModelIndex &index) const = 0 | - | ||||||||||||
1311 | - | |||||||||||||
1312 | Returns the parent of the model item with the given \a index. If the item | - | ||||||||||||
1313 | has no parent, an invalid QModelIndex is returned. | - | ||||||||||||
1314 | - | |||||||||||||
1315 | A common convention used in models that expose tree data structures is that | - | ||||||||||||
1316 | only items in the first column have children. For that case, when | - | ||||||||||||
1317 | reimplementing this function in a subclass the column of the returned | - | ||||||||||||
1318 | QModelIndex would be 0. | - | ||||||||||||
1319 | - | |||||||||||||
1320 | When reimplementing this function in a subclass, be careful to avoid | - | ||||||||||||
1321 | calling QModelIndex member functions, such as QModelIndex::parent(), since | - | ||||||||||||
1322 | indexes belonging to your model will simply call your implementation, | - | ||||||||||||
1323 | leading to infinite recursion. | - | ||||||||||||
1324 | - | |||||||||||||
1325 | \sa createIndex() | - | ||||||||||||
1326 | */ | - | ||||||||||||
1327 | - | |||||||||||||
1328 | /*! | - | ||||||||||||
1329 | \fn bool QAbstractItemModel::removeColumn(int column, const QModelIndex &parent) | - | ||||||||||||
1330 | - | |||||||||||||
1331 | Removes the given \a column from the child items of the \a parent | - | ||||||||||||
1332 | specified. | - | ||||||||||||
1333 | - | |||||||||||||
1334 | Returns \c{true} if the column is removed; otherwise returns \c{false}. | - | ||||||||||||
1335 | - | |||||||||||||
1336 | \sa removeColumns(), removeRow(), insertColumn() | - | ||||||||||||
1337 | */ | - | ||||||||||||
1338 | - | |||||||||||||
1339 | /*! | - | ||||||||||||
1340 | \fn bool QAbstractItemModel::removeRow(int row, const QModelIndex &parent) | - | ||||||||||||
1341 | - | |||||||||||||
1342 | Removes the given \a row from the child items of the \a parent specified. | - | ||||||||||||
1343 | - | |||||||||||||
1344 | Returns \c{true} if the row is removed; otherwise returns \c{false}. | - | ||||||||||||
1345 | - | |||||||||||||
1346 | This is a convenience function that calls removeRows(). The | - | ||||||||||||
1347 | QAbstractItemModel implementation of removeRows() does nothing. | - | ||||||||||||
1348 | - | |||||||||||||
1349 | \sa removeRows(), removeColumn(), insertRow() | - | ||||||||||||
1350 | */ | - | ||||||||||||
1351 | - | |||||||||||||
1352 | /*! | - | ||||||||||||
1353 | \fn bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sourceRow, const QModelIndex &destinationParent, int destinationChild) | - | ||||||||||||
1354 | - | |||||||||||||
1355 | On models that support this, moves \a sourceRow from \a sourceParent to \a destinationChild under | - | ||||||||||||
1356 | \a destinationParent. | - | ||||||||||||
1357 | - | |||||||||||||
1358 | Returns \c{true} if the rows were successfully moved; otherwise returns | - | ||||||||||||
1359 | \c{false}. | - | ||||||||||||
1360 | - | |||||||||||||
1361 | \sa moveRows(), moveColumn() | - | ||||||||||||
1362 | */ | - | ||||||||||||
1363 | - | |||||||||||||
1364 | /*! | - | ||||||||||||
1365 | \fn bool QAbstractItemModel::moveColumn(const QModelIndex &sourceParent, int sourceColumn, const QModelIndex &destinationParent, int destinationChild) | - | ||||||||||||
1366 | - | |||||||||||||
1367 | On models that support this, moves \a sourceColumn from \a sourceParent to \a destinationChild under | - | ||||||||||||
1368 | \a destinationParent. | - | ||||||||||||
1369 | - | |||||||||||||
1370 | Returns \c{true} if the columns were successfully moved; otherwise returns | - | ||||||||||||
1371 | \c{false}. | - | ||||||||||||
1372 | - | |||||||||||||
1373 | \sa moveColumns(), moveRow() | - | ||||||||||||
1374 | */ | - | ||||||||||||
1375 | - | |||||||||||||
1376 | - | |||||||||||||
1377 | /*! | - | ||||||||||||
1378 | \fn void QAbstractItemModel::headerDataChanged(Qt::Orientation orientation, int first, int last) | - | ||||||||||||
1379 | - | |||||||||||||
1380 | This signal is emitted whenever a header is changed. The \a orientation | - | ||||||||||||
1381 | indicates whether the horizontal or vertical header has changed. The | - | ||||||||||||
1382 | sections in the header from the \a first to the \a last need to be updated. | - | ||||||||||||
1383 | - | |||||||||||||
1384 | When reimplementing the setHeaderData() function, this signal must be | - | ||||||||||||
1385 | emitted explicitly. | - | ||||||||||||
1386 | - | |||||||||||||
1387 | If you are changing the number of columns or rows you do not need to emit | - | ||||||||||||
1388 | this signal, but use the begin/end functions (refer to the section on | - | ||||||||||||
1389 | subclassing in the QAbstractItemModel class description for details). | - | ||||||||||||
1390 | - | |||||||||||||
1391 | \sa headerData(), setHeaderData(), dataChanged() | - | ||||||||||||
1392 | */ | - | ||||||||||||
1393 | - | |||||||||||||
1394 | - | |||||||||||||
1395 | /*! | - | ||||||||||||
1396 | \enum QAbstractItemModel::LayoutChangeHint | - | ||||||||||||
1397 | - | |||||||||||||
1398 | This enum describes the way the model changes layout. | - | ||||||||||||
1399 | - | |||||||||||||
1400 | \value NoLayoutChangeHint No hint is available. | - | ||||||||||||
1401 | \value VerticalSortHint Rows are being sorted. | - | ||||||||||||
1402 | \value HorizontalSortHint Columns are being sorted. | - | ||||||||||||
1403 | - | |||||||||||||
1404 | Note that VerticalSortHint and HorizontalSortHint carry the meaning that | - | ||||||||||||
1405 | items are being moved within the same parent, not moved to a different | - | ||||||||||||
1406 | parent in the model, and not filtered out or in. | - | ||||||||||||
1407 | */ | - | ||||||||||||
1408 | - | |||||||||||||
1409 | /*! | - | ||||||||||||
1410 | \fn void QAbstractItemModel::layoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint) | - | ||||||||||||
1411 | \since 5.0 | - | ||||||||||||
1412 | - | |||||||||||||
1413 | This signal is emitted just before the layout of a model is changed. | - | ||||||||||||
1414 | Components connected to this signal use it to adapt to changes in the | - | ||||||||||||
1415 | model's layout. | - | ||||||||||||
1416 | - | |||||||||||||
1417 | Subclasses should update any persistent model indexes after emitting | - | ||||||||||||
1418 | layoutAboutToBeChanged(). | - | ||||||||||||
1419 | - | |||||||||||||
1420 | The optional \a parents parameter is used to give a more specific notification | - | ||||||||||||
1421 | about what parts of the layout of the model are changing. An empty list indicates | - | ||||||||||||
1422 | a change to the layout of the entire model. The order of elements in the \a parents list is not significant. The optional \a hint parameter is used | - | ||||||||||||
1423 | to give a hint about what is happening while the model is relayouting. | - | ||||||||||||
1424 | - | |||||||||||||
1425 | \sa layoutChanged(), changePersistentIndex() | - | ||||||||||||
1426 | */ | - | ||||||||||||
1427 | - | |||||||||||||
1428 | /*! | - | ||||||||||||
1429 | \fn void QAbstractItemModel::layoutChanged(const QList<QPersistentModelIndex> &parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint) | - | ||||||||||||
1430 | \since 5.0 | - | ||||||||||||
1431 | - | |||||||||||||
1432 | This signal is emitted whenever the layout of items exposed by the model | - | ||||||||||||
1433 | has changed; for example, when the model has been sorted. When this signal | - | ||||||||||||
1434 | is received by a view, it should update the layout of items to reflect this | - | ||||||||||||
1435 | change. | - | ||||||||||||
1436 | - | |||||||||||||
1437 | When subclassing QAbstractItemModel or QAbstractProxyModel, ensure that you | - | ||||||||||||
1438 | emit layoutAboutToBeChanged() before changing the order of items or | - | ||||||||||||
1439 | altering the structure of the data you expose to views, and emit | - | ||||||||||||
1440 | layoutChanged() after changing the layout. | - | ||||||||||||
1441 | - | |||||||||||||
1442 | The optional \a parents parameter is used to give a more specific notification | - | ||||||||||||
1443 | about what parts of the layout of the model are changing. An empty list indicates | - | ||||||||||||
1444 | a change to the layout of the entire model. The order of elements in the \a parents list is not significant. The optional \a hint parameter is used | - | ||||||||||||
1445 | to give a hint about what is happening while the model is relayouting. | - | ||||||||||||
1446 | - | |||||||||||||
1447 | Subclasses should update any persistent model indexes before emitting | - | ||||||||||||
1448 | layoutChanged(). In other words, when the structure changes: | - | ||||||||||||
1449 | - | |||||||||||||
1450 | \list | - | ||||||||||||
1451 | \li emit layoutAboutToBeChanged | - | ||||||||||||
1452 | \li Remember the QModelIndex that will change | - | ||||||||||||
1453 | \li Update your internal data | - | ||||||||||||
1454 | \li Call changePersistentIndex() | - | ||||||||||||
1455 | \li emit layoutChanged | - | ||||||||||||
1456 | \endlist | - | ||||||||||||
1457 | - | |||||||||||||
1458 | \sa layoutAboutToBeChanged(), dataChanged(), headerDataChanged(), modelReset(), | - | ||||||||||||
1459 | changePersistentIndex() | - | ||||||||||||
1460 | */ | - | ||||||||||||
1461 | - | |||||||||||||
1462 | /*! | - | ||||||||||||
1463 | Constructs an abstract item model with the given \a parent. | - | ||||||||||||
1464 | */ | - | ||||||||||||
1465 | QAbstractItemModel::QAbstractItemModel(QObject *parent) | - | ||||||||||||
1466 | : QObject(*new QAbstractItemModelPrivate, parent) | - | ||||||||||||
1467 | { | - | ||||||||||||
1468 | } | - | ||||||||||||
1469 | - | |||||||||||||
1470 | /*! | - | ||||||||||||
1471 | \internal | - | ||||||||||||
1472 | */ | - | ||||||||||||
1473 | QAbstractItemModel::QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent) | - | ||||||||||||
1474 | : QObject(dd, parent) | - | ||||||||||||
1475 | { | - | ||||||||||||
1476 | } | - | ||||||||||||
1477 | - | |||||||||||||
1478 | /*! | - | ||||||||||||
1479 | Destroys the abstract item model. | - | ||||||||||||
1480 | */ | - | ||||||||||||
1481 | QAbstractItemModel::~QAbstractItemModel() | - | ||||||||||||
1482 | { | - | ||||||||||||
1483 | d_func()->invalidatePersistentIndexes(); | - | ||||||||||||
1484 | } | - | ||||||||||||
1485 | - | |||||||||||||
1486 | - | |||||||||||||
1487 | /*! | - | ||||||||||||
1488 | \fn int QAbstractItemModel::rowCount(const QModelIndex &parent) const | - | ||||||||||||
1489 | - | |||||||||||||
1490 | Returns the number of rows under the given \a parent. When the parent is | - | ||||||||||||
1491 | valid it means that rowCount is returning the number of children of parent. | - | ||||||||||||
1492 | - | |||||||||||||
1493 | \note When implementing a table based model, rowCount() should return 0 | - | ||||||||||||
1494 | when the parent is valid. | - | ||||||||||||
1495 | - | |||||||||||||
1496 | \sa columnCount() | - | ||||||||||||
1497 | */ | - | ||||||||||||
1498 | - | |||||||||||||
1499 | /*! | - | ||||||||||||
1500 | \fn int QAbstractItemModel::columnCount(const QModelIndex &parent) const | - | ||||||||||||
1501 | - | |||||||||||||
1502 | Returns the number of columns for the children of the given \a parent. | - | ||||||||||||
1503 | - | |||||||||||||
1504 | In most subclasses, the number of columns is independent of the \a parent. | - | ||||||||||||
1505 | - | |||||||||||||
1506 | For example: | - | ||||||||||||
1507 | - | |||||||||||||
1508 | \snippet ../widgets/itemviews/simpledommodel/dommodel.cpp 2 | - | ||||||||||||
1509 | - | |||||||||||||
1510 | \note When implementing a table based model, columnCount() should return 0 | - | ||||||||||||
1511 | when the parent is valid. | - | ||||||||||||
1512 | - | |||||||||||||
1513 | \sa rowCount() | - | ||||||||||||
1514 | */ | - | ||||||||||||
1515 | - | |||||||||||||
1516 | /*! | - | ||||||||||||
1517 | \fn void QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) | - | ||||||||||||
1518 | - | |||||||||||||
1519 | This signal is emitted whenever the data in an existing item changes. | - | ||||||||||||
1520 | - | |||||||||||||
1521 | If the items are of the same parent, the affected ones are those between | - | ||||||||||||
1522 | \a topLeft and \a bottomRight inclusive. If the items do not have the same | - | ||||||||||||
1523 | parent, the behavior is undefined. | - | ||||||||||||
1524 | - | |||||||||||||
1525 | When reimplementing the setData() function, this signal must be emitted | - | ||||||||||||
1526 | explicitly. | - | ||||||||||||
1527 | - | |||||||||||||
1528 | The optional \a roles argument can be used to specify which data roles have actually | - | ||||||||||||
1529 | been modified. An empty vector in the roles argument means that all roles should be | - | ||||||||||||
1530 | considered modified. The order of elements in the roles argument does not have any | - | ||||||||||||
1531 | relevance. | - | ||||||||||||
1532 | - | |||||||||||||
1533 | \sa headerDataChanged(), setData(), layoutChanged() | - | ||||||||||||
1534 | */ | - | ||||||||||||
1535 | - | |||||||||||||
1536 | /*! | - | ||||||||||||
1537 | \fn void QAbstractItemModel::rowsInserted(const QModelIndex &parent, int first, int last) | - | ||||||||||||
1538 | - | |||||||||||||
1539 | This signal is emitted after rows have been inserted into the | - | ||||||||||||
1540 | model. The new items are those between \a first and \a last | - | ||||||||||||
1541 | inclusive, under the given \a parent item. | - | ||||||||||||
1542 | - | |||||||||||||
1543 | \note Components connected to this signal use it to adapt to changes in the | - | ||||||||||||
1544 | model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1545 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1546 | - | |||||||||||||
1547 | \sa insertRows(), beginInsertRows() | - | ||||||||||||
1548 | */ | - | ||||||||||||
1549 | - | |||||||||||||
1550 | /*! | - | ||||||||||||
1551 | \fn void QAbstractItemModel::rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) | - | ||||||||||||
1552 | - | |||||||||||||
1553 | This signal is emitted just before rows are inserted into the model. The | - | ||||||||||||
1554 | new items will be positioned between \a start and \a end inclusive, under | - | ||||||||||||
1555 | the given \a parent item. | - | ||||||||||||
1556 | - | |||||||||||||
1557 | \note Components connected to this signal use it to adapt to changes | - | ||||||||||||
1558 | in the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1559 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1560 | - | |||||||||||||
1561 | \sa insertRows(), beginInsertRows() | - | ||||||||||||
1562 | */ | - | ||||||||||||
1563 | - | |||||||||||||
1564 | /*! | - | ||||||||||||
1565 | \fn void QAbstractItemModel::rowsRemoved(const QModelIndex &parent, int first, int last) | - | ||||||||||||
1566 | - | |||||||||||||
1567 | This signal is emitted after rows have been removed from the model. The | - | ||||||||||||
1568 | removed items are those between \a first and \a last inclusive, under the | - | ||||||||||||
1569 | given \a parent item. | - | ||||||||||||
1570 | - | |||||||||||||
1571 | \note Components connected to this signal use it to adapt to changes | - | ||||||||||||
1572 | in the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1573 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1574 | - | |||||||||||||
1575 | \sa removeRows(), beginRemoveRows() | - | ||||||||||||
1576 | */ | - | ||||||||||||
1577 | - | |||||||||||||
1578 | /*! | - | ||||||||||||
1579 | \fn void QAbstractItemModel::rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last) | - | ||||||||||||
1580 | - | |||||||||||||
1581 | This signal is emitted just before rows are removed from the model. The | - | ||||||||||||
1582 | items that will be removed are those between \a first and \a last inclusive, | - | ||||||||||||
1583 | under the given \a parent item. | - | ||||||||||||
1584 | - | |||||||||||||
1585 | \note Components connected to this signal use it to adapt to changes | - | ||||||||||||
1586 | in the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1587 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1588 | - | |||||||||||||
1589 | \sa removeRows(), beginRemoveRows() | - | ||||||||||||
1590 | */ | - | ||||||||||||
1591 | - | |||||||||||||
1592 | /*! | - | ||||||||||||
1593 | \fn void QAbstractItemModel::rowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row) | - | ||||||||||||
1594 | \since 4.6 | - | ||||||||||||
1595 | - | |||||||||||||
1596 | This signal is emitted after rows have been moved within the | - | ||||||||||||
1597 | model. The items between \a start and \a end | - | ||||||||||||
1598 | inclusive, under the given \a parent item have been moved to \a destination | - | ||||||||||||
1599 | starting at the row \a row. | - | ||||||||||||
1600 | - | |||||||||||||
1601 | \b{Note:} Components connected to this signal use it to adapt to changes | - | ||||||||||||
1602 | in the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1603 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1604 | - | |||||||||||||
1605 | \sa beginMoveRows() | - | ||||||||||||
1606 | */ | - | ||||||||||||
1607 | - | |||||||||||||
1608 | /*! | - | ||||||||||||
1609 | \fn void QAbstractItemModel::rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow) | - | ||||||||||||
1610 | \since 4.6 | - | ||||||||||||
1611 | - | |||||||||||||
1612 | This signal is emitted just before rows are moved within the | - | ||||||||||||
1613 | model. The items that will be moved are those between \a sourceStart and \a sourceEnd | - | ||||||||||||
1614 | inclusive, under the given \a sourceParent item. They will be moved to \a destinationParent | - | ||||||||||||
1615 | starting at the row \a destinationRow. | - | ||||||||||||
1616 | - | |||||||||||||
1617 | \b{Note:} Components connected to this signal use it to adapt to changes | - | ||||||||||||
1618 | in the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1619 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1620 | - | |||||||||||||
1621 | \sa beginMoveRows() | - | ||||||||||||
1622 | */ | - | ||||||||||||
1623 | - | |||||||||||||
1624 | /*! | - | ||||||||||||
1625 | \fn void QAbstractItemModel::columnsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column) | - | ||||||||||||
1626 | \since 4.6 | - | ||||||||||||
1627 | - | |||||||||||||
1628 | This signal is emitted after columns have been moved within the | - | ||||||||||||
1629 | model. The items between \a start and \a end | - | ||||||||||||
1630 | inclusive, under the given \a parent item have been moved to \a destination | - | ||||||||||||
1631 | starting at the column \a column. | - | ||||||||||||
1632 | - | |||||||||||||
1633 | \b{Note:} Components connected to this signal use it to adapt to changes | - | ||||||||||||
1634 | in the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1635 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1636 | - | |||||||||||||
1637 | \sa beginMoveRows() | - | ||||||||||||
1638 | */ | - | ||||||||||||
1639 | - | |||||||||||||
1640 | /*! | - | ||||||||||||
1641 | \fn void QAbstractItemModel::columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn) | - | ||||||||||||
1642 | \since 4.6 | - | ||||||||||||
1643 | - | |||||||||||||
1644 | This signal is emitted just before columns are moved within the | - | ||||||||||||
1645 | model. The items that will be moved are those between \a sourceStart and \a sourceEnd | - | ||||||||||||
1646 | inclusive, under the given \a sourceParent item. They will be moved to \a destinationParent | - | ||||||||||||
1647 | starting at the column \a destinationColumn. | - | ||||||||||||
1648 | - | |||||||||||||
1649 | \b{Note:} Components connected to this signal use it to adapt to changes | - | ||||||||||||
1650 | in the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1651 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1652 | - | |||||||||||||
1653 | \sa beginMoveRows() | - | ||||||||||||
1654 | */ | - | ||||||||||||
1655 | - | |||||||||||||
1656 | /*! | - | ||||||||||||
1657 | \fn void QAbstractItemModel::columnsInserted(const QModelIndex &parent, int first, int last) | - | ||||||||||||
1658 | - | |||||||||||||
1659 | This signal is emitted after columns have been inserted into the model. The | - | ||||||||||||
1660 | new items are those between \a first and \a last inclusive, under the given | - | ||||||||||||
1661 | \a parent item. | - | ||||||||||||
1662 | - | |||||||||||||
1663 | \note Components connected to this signal use it to adapt to changes in the | - | ||||||||||||
1664 | model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1665 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1666 | - | |||||||||||||
1667 | \sa insertColumns(), beginInsertColumns() | - | ||||||||||||
1668 | */ | - | ||||||||||||
1669 | - | |||||||||||||
1670 | /*! | - | ||||||||||||
1671 | \fn void QAbstractItemModel::columnsAboutToBeInserted(const QModelIndex &parent, int first, int last) | - | ||||||||||||
1672 | - | |||||||||||||
1673 | This signal is emitted just before columns are inserted into the model. The | - | ||||||||||||
1674 | new items will be positioned between \a first and \a last inclusive, under | - | ||||||||||||
1675 | the given \a parent item. | - | ||||||||||||
1676 | - | |||||||||||||
1677 | \note Components connected to this signal use it to adapt to changes in the | - | ||||||||||||
1678 | model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1679 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1680 | - | |||||||||||||
1681 | \sa insertColumns(), beginInsertColumns() | - | ||||||||||||
1682 | */ | - | ||||||||||||
1683 | - | |||||||||||||
1684 | /*! | - | ||||||||||||
1685 | \fn void QAbstractItemModel::columnsRemoved(const QModelIndex &parent, int first, int last) | - | ||||||||||||
1686 | - | |||||||||||||
1687 | This signal is emitted after columns have been removed from the model. | - | ||||||||||||
1688 | The removed items are those between \a first and \a last inclusive, | - | ||||||||||||
1689 | under the given \a parent item. | - | ||||||||||||
1690 | - | |||||||||||||
1691 | \note Components connected to this signal use it to adapt to changes in | - | ||||||||||||
1692 | the model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1693 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1694 | - | |||||||||||||
1695 | \sa removeColumns(), beginRemoveColumns() | - | ||||||||||||
1696 | */ | - | ||||||||||||
1697 | - | |||||||||||||
1698 | /*! | - | ||||||||||||
1699 | \fn void QAbstractItemModel::columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last) | - | ||||||||||||
1700 | - | |||||||||||||
1701 | This signal is emitted just before columns are removed from the model. The | - | ||||||||||||
1702 | items to be removed are those between \a first and \a last inclusive, under | - | ||||||||||||
1703 | the given \a parent item. | - | ||||||||||||
1704 | - | |||||||||||||
1705 | \note Components connected to this signal use it to adapt to changes in the | - | ||||||||||||
1706 | model's dimensions. It can only be emitted by the QAbstractItemModel | - | ||||||||||||
1707 | implementation, and cannot be explicitly emitted in subclass code. | - | ||||||||||||
1708 | - | |||||||||||||
1709 | \sa removeColumns(), beginRemoveColumns() | - | ||||||||||||
1710 | */ | - | ||||||||||||
1711 | - | |||||||||||||
1712 | /*! | - | ||||||||||||
1713 | Returns \c{true} if the model returns a valid QModelIndex for \a row and | - | ||||||||||||
1714 | \a column with \a parent, otherwise returns \c{false}. | - | ||||||||||||
1715 | */ | - | ||||||||||||
1716 | bool QAbstractItemModel::hasIndex(int row, int column, const QModelIndex &parent) const | - | ||||||||||||
1717 | { | - | ||||||||||||
1718 | if (row < 0 || column < 0) | - | ||||||||||||
1719 | return false; | - | ||||||||||||
1720 | return row < rowCount(parent) && column < columnCount(parent); | - | ||||||||||||
1721 | } | - | ||||||||||||
1722 | - | |||||||||||||
1723 | - | |||||||||||||
1724 | /*! | - | ||||||||||||
1725 | Returns \c{true} if \a parent has any children; otherwise returns \c{false}. | - | ||||||||||||
1726 | - | |||||||||||||
1727 | Use rowCount() on the parent to find out the number of children. | - | ||||||||||||
1728 | - | |||||||||||||
1729 | Note that it is undefined behavior to report that a particular index hasChildren | - | ||||||||||||
1730 | with this method if the same index has the flag Qt::ItemNeverHasChildren set. | - | ||||||||||||
1731 | - | |||||||||||||
1732 | \sa parent(), index() | - | ||||||||||||
1733 | */ | - | ||||||||||||
1734 | bool QAbstractItemModel::hasChildren(const QModelIndex &parent) const | - | ||||||||||||
1735 | { | - | ||||||||||||
1736 | return (rowCount(parent) > 0) && (columnCount(parent) > 0); | - | ||||||||||||
1737 | } | - | ||||||||||||
1738 | - | |||||||||||||
1739 | /*! | - | ||||||||||||
1740 | \fn QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &index) const | - | ||||||||||||
1741 | - | |||||||||||||
1742 | Returns the sibling at \a row and \a column for the item at \a index, or an | - | ||||||||||||
1743 | invalid QModelIndex if there is no sibling at that location. | - | ||||||||||||
1744 | - | |||||||||||||
1745 | sibling() is just a convenience function that finds the item's parent, and | - | ||||||||||||
1746 | uses it to retrieve the index of the child item in the specified \a row and | - | ||||||||||||
1747 | \a column. | - | ||||||||||||
1748 | - | |||||||||||||
1749 | This method can optionally be overridden for implementation-specific optimization. | - | ||||||||||||
1750 | - | |||||||||||||
1751 | \sa index(), QModelIndex::row(), QModelIndex::column() | - | ||||||||||||
1752 | */ | - | ||||||||||||
1753 | QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex &idx) const | - | ||||||||||||
1754 | { | - | ||||||||||||
1755 | return (row == idx.row() && column == idx.column()) ? idx : index(row, column, parent(idx)); | - | ||||||||||||
1756 | } | - | ||||||||||||
1757 | - | |||||||||||||
1758 | - | |||||||||||||
1759 | /*! | - | ||||||||||||
1760 | Returns a map with values for all predefined roles in the model for the | - | ||||||||||||
1761 | item at the given \a index. | - | ||||||||||||
1762 | - | |||||||||||||
1763 | Reimplement this function if you want to extend the default behavior of | - | ||||||||||||
1764 | this function to include custom roles in the map. | - | ||||||||||||
1765 | - | |||||||||||||
1766 | \sa Qt::ItemDataRole, data() | - | ||||||||||||
1767 | */ | - | ||||||||||||
1768 | QMap<int, QVariant> QAbstractItemModel::itemData(const QModelIndex &index) const | - | ||||||||||||
1769 | { | - | ||||||||||||
1770 | QMap<int, QVariant> roles; | - | ||||||||||||
1771 | for (int i = 0; i < Qt::UserRole; ++i) { | - | ||||||||||||
1772 | QVariant variantData = data(index, i); | - | ||||||||||||
1773 | if (variantData.isValid()) | - | ||||||||||||
1774 | roles.insert(i, variantData); | - | ||||||||||||
1775 | } | - | ||||||||||||
1776 | return roles; | - | ||||||||||||
1777 | } | - | ||||||||||||
1778 | - | |||||||||||||
1779 | /*! | - | ||||||||||||
1780 | Sets the \a role data for the item at \a index to \a value. | - | ||||||||||||
1781 | - | |||||||||||||
1782 | Returns \c{true} if successful; otherwise returns \c{false}. | - | ||||||||||||
1783 | - | |||||||||||||
1784 | The dataChanged() signal should be emitted if the data was successfully | - | ||||||||||||
1785 | set. | - | ||||||||||||
1786 | - | |||||||||||||
1787 | The base class implementation returns \c{false}. This function and data() must | - | ||||||||||||
1788 | be reimplemented for editable models. | - | ||||||||||||
1789 | - | |||||||||||||
1790 | \sa Qt::ItemDataRole, data(), itemData() | - | ||||||||||||
1791 | */ | - | ||||||||||||
1792 | bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role) | - | ||||||||||||
1793 | { | - | ||||||||||||
1794 | Q_UNUSED(index); | - | ||||||||||||
1795 | Q_UNUSED(value); | - | ||||||||||||
1796 | Q_UNUSED(role); | - | ||||||||||||
1797 | return false; | - | ||||||||||||
1798 | } | - | ||||||||||||
1799 | - | |||||||||||||
1800 | /*! | - | ||||||||||||
1801 | \fn QVariant QAbstractItemModel::data(const QModelIndex &index, int role) const = 0 | - | ||||||||||||
1802 | - | |||||||||||||
1803 | Returns the data stored under the given \a role for the item referred to | - | ||||||||||||
1804 | by the \a index. | - | ||||||||||||
1805 | - | |||||||||||||
1806 | \note If you do not have a value to return, return an \b invalid | - | ||||||||||||
1807 | QVariant instead of returning 0. | - | ||||||||||||
1808 | - | |||||||||||||
1809 | \sa Qt::ItemDataRole, setData(), headerData() | - | ||||||||||||
1810 | */ | - | ||||||||||||
1811 | - | |||||||||||||
1812 | /*! | - | ||||||||||||
1813 | Sets the role data for the item at \a index to the associated value in | - | ||||||||||||
1814 | \a roles, for every Qt::ItemDataRole. | - | ||||||||||||
1815 | - | |||||||||||||
1816 | Returns \c{true} if successful; otherwise returns \c{false}. | - | ||||||||||||
1817 | - | |||||||||||||
1818 | Roles that are not in \a roles will not be modified. | - | ||||||||||||
1819 | - | |||||||||||||
1820 | \sa setData(), data(), itemData() | - | ||||||||||||
1821 | */ | - | ||||||||||||
1822 | bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) | - | ||||||||||||
1823 | { | - | ||||||||||||
1824 | bool b = true; | - | ||||||||||||
1825 | for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it) | - | ||||||||||||
1826 | b = b && setData(index, it.value(), it.key()); | - | ||||||||||||
1827 | return b; | - | ||||||||||||
1828 | } | - | ||||||||||||
1829 | - | |||||||||||||
1830 | /*! | - | ||||||||||||
1831 | Returns the list of allowed MIME types. By default, the built-in | - | ||||||||||||
1832 | models and views use an internal MIME type: | - | ||||||||||||
1833 | \c{application/x-qabstractitemmodeldatalist}. | - | ||||||||||||
1834 | - | |||||||||||||
1835 | When implementing drag and drop support in a custom model, if you | - | ||||||||||||
1836 | will return data in formats other than the default internal MIME | - | ||||||||||||
1837 | type, reimplement this function to return your list of MIME types. | - | ||||||||||||
1838 | - | |||||||||||||
1839 | If you reimplement this function in your custom model, you must | - | ||||||||||||
1840 | also reimplement the member functions that call it: mimeData() and | - | ||||||||||||
1841 | dropMimeData(). | - | ||||||||||||
1842 | - | |||||||||||||
1843 | \sa mimeData(), dropMimeData() | - | ||||||||||||
1844 | */ | - | ||||||||||||
1845 | QStringList QAbstractItemModel::mimeTypes() const | - | ||||||||||||
1846 | { | - | ||||||||||||
1847 | QStringList types; | - | ||||||||||||
1848 | types << QLatin1StringQStringLiteral("application/x-qabstractitemmodeldatalist"); executed 174 times by 7 tests: return qstring_literal_temp; Executed by:
| 174 | ||||||||||||
1849 | return types; executed 174 times by 7 tests: return types; Executed by:
| 174 | ||||||||||||
1850 | } | - | ||||||||||||
1851 | - | |||||||||||||
1852 | /*! | - | ||||||||||||
1853 | Returns an object that contains serialized items of data corresponding to | - | ||||||||||||
1854 | the list of \a indexes specified. The format used to describe the encoded | - | ||||||||||||
1855 | data is obtained from the mimeTypes() function. This default implementation | - | ||||||||||||
1856 | uses the default MIME type returned by the default implementation of | - | ||||||||||||
1857 | mimeTypes(). If you reimplement mimeTypes() in your custom model to return | - | ||||||||||||
1858 | more MIME types, reimplement this function to make use of them. | - | ||||||||||||
1859 | - | |||||||||||||
1860 | If the list of \a indexes is empty, or there are no supported MIME types, 0 | - | ||||||||||||
1861 | is returned rather than a serialized empty list. | - | ||||||||||||
1862 | - | |||||||||||||
1863 | \sa mimeTypes(), dropMimeData() | - | ||||||||||||
1864 | */ | - | ||||||||||||
1865 | QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const | - | ||||||||||||
1866 | { | - | ||||||||||||
1867 | if (indexes.count() <= 0) | - | ||||||||||||
1868 | return 0; | - | ||||||||||||
1869 | QStringList types = mimeTypes(); | - | ||||||||||||
1870 | if (types.isEmpty()) | - | ||||||||||||
1871 | return 0; | - | ||||||||||||
1872 | QMimeData *data = new QMimeData(); | - | ||||||||||||
1873 | QString format = types.at(0); | - | ||||||||||||
1874 | QByteArray encoded; | - | ||||||||||||
1875 | QDataStream stream(&encoded, QIODevice::WriteOnly); | - | ||||||||||||
1876 | encodeData(indexes, stream); | - | ||||||||||||
1877 | data->setData(format, encoded); | - | ||||||||||||
1878 | return data; | - | ||||||||||||
1879 | } | - | ||||||||||||
1880 | - | |||||||||||||
1881 | /*! | - | ||||||||||||
1882 | Returns \c{true} if a model can accept a drop of the \a data. This | - | ||||||||||||
1883 | default implementation only checks if \a data has at least one format | - | ||||||||||||
1884 | in the list of mimeTypes() and if \a action is among the | - | ||||||||||||
1885 | model's supportedDropActions(). | - | ||||||||||||
1886 | - | |||||||||||||
1887 | Reimplement this function in your custom model, if you want to | - | ||||||||||||
1888 | test whether the \a data can be dropped at \a row, \a column, | - | ||||||||||||
1889 | \a parent with \a action. If you don't need that test, it is not | - | ||||||||||||
1890 | necessary to reimplement this function. | - | ||||||||||||
1891 | - | |||||||||||||
1892 | \sa dropMimeData(), {Using drag and drop with item views} | - | ||||||||||||
1893 | */ | - | ||||||||||||
1894 | bool QAbstractItemModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||
1895 | int row, int column, | - | ||||||||||||
1896 | const QModelIndex &parent) const | - | ||||||||||||
1897 | { | - | ||||||||||||
1898 | Q_UNUSED(row) | - | ||||||||||||
1899 | Q_UNUSED(column) | - | ||||||||||||
1900 | Q_UNUSED(parent) | - | ||||||||||||
1901 | - | |||||||||||||
1902 | if (!(action & supportedDropActions())) | - | ||||||||||||
1903 | return false; | - | ||||||||||||
1904 | - | |||||||||||||
1905 | const QStringList modelTypes = mimeTypes(); | - | ||||||||||||
1906 | for (int i = 0; i < modelTypes.count(); ++i) { | - | ||||||||||||
1907 | if (data->hasFormat(modelTypes.at(i))) | - | ||||||||||||
1908 | return true; | - | ||||||||||||
1909 | } | - | ||||||||||||
1910 | return false; | - | ||||||||||||
1911 | } | - | ||||||||||||
1912 | - | |||||||||||||
1913 | /*! | - | ||||||||||||
1914 | Handles the \a data supplied by a drag and drop operation that ended with | - | ||||||||||||
1915 | the given \a action. | - | ||||||||||||
1916 | - | |||||||||||||
1917 | Returns \c{true} if the data and action were handled by the model; otherwise | - | ||||||||||||
1918 | returns \c{false}. | - | ||||||||||||
1919 | - | |||||||||||||
1920 | The specified \a row, \a column and \a parent indicate the location of an | - | ||||||||||||
1921 | item in the model where the operation ended. It is the responsibility of | - | ||||||||||||
1922 | the model to complete the action at the correct location. | - | ||||||||||||
1923 | - | |||||||||||||
1924 | For instance, a drop action on an item in a QTreeView can result in new | - | ||||||||||||
1925 | items either being inserted as children of the item specified by \a row, | - | ||||||||||||
1926 | \a column, and \a parent, or as siblings of the item. | - | ||||||||||||
1927 | - | |||||||||||||
1928 | When \a row and \a column are -1 it means that the dropped data should be | - | ||||||||||||
1929 | considered as dropped directly on \a parent. Usually this will mean | - | ||||||||||||
1930 | appending the data as child items of \a parent. If \a row and \a column are | - | ||||||||||||
1931 | greater than or equal zero, it means that the drop occurred just before the | - | ||||||||||||
1932 | specified \a row and \a column in the specified \a parent. | - | ||||||||||||
1933 | - | |||||||||||||
1934 | The mimeTypes() member is called to get the list of acceptable MIME types. | - | ||||||||||||
1935 | This default implementation assumes the default implementation of mimeTypes(), | - | ||||||||||||
1936 | which returns a single default MIME type. If you reimplement mimeTypes() in | - | ||||||||||||
1937 | your custom model to return multiple MIME types, you must reimplement this | - | ||||||||||||
1938 | function to make use of them. | - | ||||||||||||
1939 | - | |||||||||||||
1940 | \sa supportedDropActions(), canDropMimeData(), {Using drag and drop with item views} | - | ||||||||||||
1941 | */ | - | ||||||||||||
1942 | bool QAbstractItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||
1943 | int row, int column, const QModelIndex &parent) | - | ||||||||||||
1944 | { | - | ||||||||||||
1945 | // check if the action is supported | - | ||||||||||||
1946 | if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction)) | - | ||||||||||||
1947 | return false; | - | ||||||||||||
1948 | // check if the format is supported | - | ||||||||||||
1949 | QStringList types = mimeTypes(); | - | ||||||||||||
1950 | if (types.isEmpty()) | - | ||||||||||||
1951 | return false; | - | ||||||||||||
1952 | QString format = types.at(0); | - | ||||||||||||
1953 | if (!data->hasFormat(format)) | - | ||||||||||||
1954 | return false; | - | ||||||||||||
1955 | if (row > rowCount(parent)) | - | ||||||||||||
1956 | row = rowCount(parent); | - | ||||||||||||
1957 | if (row == -1) | - | ||||||||||||
1958 | row = rowCount(parent); | - | ||||||||||||
1959 | if (column == -1) | - | ||||||||||||
1960 | column = 0; | - | ||||||||||||
1961 | // decode and insert | - | ||||||||||||
1962 | QByteArray encoded = data->data(format); | - | ||||||||||||
1963 | QDataStream stream(&encoded, QIODevice::ReadOnly); | - | ||||||||||||
1964 | return decodeData(row, column, parent, stream); | - | ||||||||||||
1965 | } | - | ||||||||||||
1966 | - | |||||||||||||
1967 | /*! | - | ||||||||||||
1968 | \since 4.2 | - | ||||||||||||
1969 | - | |||||||||||||
1970 | Returns the drop actions supported by this model. | - | ||||||||||||
1971 | - | |||||||||||||
1972 | The default implementation returns Qt::CopyAction. Reimplement this | - | ||||||||||||
1973 | function if you wish to support additional actions. You must also | - | ||||||||||||
1974 | reimplement the dropMimeData() function to handle the additional | - | ||||||||||||
1975 | operations. | - | ||||||||||||
1976 | - | |||||||||||||
1977 | \sa dropMimeData(), Qt::DropActions, {Using drag and drop with item | - | ||||||||||||
1978 | views} | - | ||||||||||||
1979 | */ | - | ||||||||||||
1980 | Qt::DropActions QAbstractItemModel::supportedDropActions() const | - | ||||||||||||
1981 | { | - | ||||||||||||
1982 | return Qt::CopyAction; | - | ||||||||||||
1983 | } | - | ||||||||||||
1984 | - | |||||||||||||
1985 | /*! | - | ||||||||||||
1986 | Returns the actions supported by the data in this model. | - | ||||||||||||
1987 | - | |||||||||||||
1988 | The default implementation returns supportedDropActions(). Reimplement | - | ||||||||||||
1989 | this function if you wish to support additional actions. | - | ||||||||||||
1990 | - | |||||||||||||
1991 | supportedDragActions() is used by QAbstractItemView::startDrag() as the | - | ||||||||||||
1992 | default values when a drag occurs. | - | ||||||||||||
1993 | - | |||||||||||||
1994 | \sa Qt::DropActions, {Using drag and drop with item views} | - | ||||||||||||
1995 | */ | - | ||||||||||||
1996 | Qt::DropActions QAbstractItemModel::supportedDragActions() const | - | ||||||||||||
1997 | { | - | ||||||||||||
1998 | Q_D(const QAbstractItemModel); | - | ||||||||||||
1999 | if (d->supportedDragActions != -1) | - | ||||||||||||
2000 | return d->supportedDragActions; | - | ||||||||||||
2001 | return supportedDropActions(); | - | ||||||||||||
2002 | } | - | ||||||||||||
2003 | - | |||||||||||||
2004 | /*! | - | ||||||||||||
2005 | \internal | - | ||||||||||||
2006 | */ | - | ||||||||||||
2007 | void QAbstractItemModel::doSetSupportedDragActions(Qt::DropActions actions) | - | ||||||||||||
2008 | { | - | ||||||||||||
2009 | Q_D(QAbstractItemModel); | - | ||||||||||||
2010 | d->supportedDragActions = actions; | - | ||||||||||||
2011 | } | - | ||||||||||||
2012 | - | |||||||||||||
2013 | /*! | - | ||||||||||||
2014 | \since 4.2 | - | ||||||||||||
2015 | \obsolete | - | ||||||||||||
2016 | \fn void QAbstractItemModel::setSupportedDragActions(Qt::DropActions actions) | - | ||||||||||||
2017 | - | |||||||||||||
2018 | This function is obsolete. Reimplement supportedDragActions() instead. | - | ||||||||||||
2019 | - | |||||||||||||
2020 | Sets the supported drag \a actions for the items in the model. | - | ||||||||||||
2021 | - | |||||||||||||
2022 | \sa supportedDragActions(), {Using drag and drop with item views} | - | ||||||||||||
2023 | */ | - | ||||||||||||
2024 | - | |||||||||||||
2025 | /*! | - | ||||||||||||
2026 | \note The base class implementation of this function does nothing and | - | ||||||||||||
2027 | returns \c{false}. | - | ||||||||||||
2028 | - | |||||||||||||
2029 | On models that support this, inserts \a count rows into the model before | - | ||||||||||||
2030 | the given \a row. Items in the new row will be children of the item | - | ||||||||||||
2031 | represented by the \a parent model index. | - | ||||||||||||
2032 | - | |||||||||||||
2033 | If \a row is 0, the rows are prepended to any existing rows in the parent. | - | ||||||||||||
2034 | - | |||||||||||||
2035 | If \a row is rowCount(), the rows are appended to any existing rows in the | - | ||||||||||||
2036 | parent. | - | ||||||||||||
2037 | - | |||||||||||||
2038 | If \a parent has no children, a single column with \a count rows is | - | ||||||||||||
2039 | inserted. | - | ||||||||||||
2040 | - | |||||||||||||
2041 | Returns \c{true} if the rows were successfully inserted; otherwise returns | - | ||||||||||||
2042 | \c{false}. | - | ||||||||||||
2043 | - | |||||||||||||
2044 | If you implement your own model, you can reimplement this function if you | - | ||||||||||||
2045 | want to support insertions. Alternatively, you can provide your own API for | - | ||||||||||||
2046 | altering the data. In either case, you will need to call | - | ||||||||||||
2047 | beginInsertRows() and endInsertRows() to notify other components that the | - | ||||||||||||
2048 | model has changed. | - | ||||||||||||
2049 | - | |||||||||||||
2050 | \sa insertColumns(), removeRows(), beginInsertRows(), endInsertRows() | - | ||||||||||||
2051 | */ | - | ||||||||||||
2052 | bool QAbstractItemModel::insertRows(int, int, const QModelIndex &) | - | ||||||||||||
2053 | { | - | ||||||||||||
2054 | return false; | - | ||||||||||||
2055 | } | - | ||||||||||||
2056 | - | |||||||||||||
2057 | /*! | - | ||||||||||||
2058 | On models that support this, inserts \a count new columns into the model | - | ||||||||||||
2059 | before the given \a column. The items in each new column will be children | - | ||||||||||||
2060 | of the item represented by the \a parent model index. | - | ||||||||||||
2061 | - | |||||||||||||
2062 | If \a column is 0, the columns are prepended to any existing columns. | - | ||||||||||||
2063 | - | |||||||||||||
2064 | If \a column is columnCount(), the columns are appended to any existing | - | ||||||||||||
2065 | columns. | - | ||||||||||||
2066 | - | |||||||||||||
2067 | If \a parent has no children, a single row with \a count columns is | - | ||||||||||||
2068 | inserted. | - | ||||||||||||
2069 | - | |||||||||||||
2070 | Returns \c{true} if the columns were successfully inserted; otherwise returns | - | ||||||||||||
2071 | \c{false}. | - | ||||||||||||
2072 | - | |||||||||||||
2073 | The base class implementation does nothing and returns \c{false}. | - | ||||||||||||
2074 | - | |||||||||||||
2075 | If you implement your own model, you can reimplement this function if you | - | ||||||||||||
2076 | want to support insertions. Alternatively, you can provide your own API for | - | ||||||||||||
2077 | altering the data. | - | ||||||||||||
2078 | - | |||||||||||||
2079 | \sa insertRows(), removeColumns(), beginInsertColumns(), endInsertColumns() | - | ||||||||||||
2080 | */ | - | ||||||||||||
2081 | bool QAbstractItemModel::insertColumns(int, int, const QModelIndex &) | - | ||||||||||||
2082 | { | - | ||||||||||||
2083 | return false; | - | ||||||||||||
2084 | } | - | ||||||||||||
2085 | - | |||||||||||||
2086 | /*! | - | ||||||||||||
2087 | On models that support this, removes \a count rows starting with the given | - | ||||||||||||
2088 | \a row under parent \a parent from the model. | - | ||||||||||||
2089 | - | |||||||||||||
2090 | Returns \c{true} if the rows were successfully removed; otherwise returns | - | ||||||||||||
2091 | \c{false}. | - | ||||||||||||
2092 | - | |||||||||||||
2093 | The base class implementation does nothing and returns \c{false}. | - | ||||||||||||
2094 | - | |||||||||||||
2095 | If you implement your own model, you can reimplement this function if you | - | ||||||||||||
2096 | want to support removing. Alternatively, you can provide your own API for | - | ||||||||||||
2097 | altering the data. | - | ||||||||||||
2098 | - | |||||||||||||
2099 | \sa removeRow(), removeColumns(), insertColumns(), beginRemoveRows(), | - | ||||||||||||
2100 | endRemoveRows() | - | ||||||||||||
2101 | */ | - | ||||||||||||
2102 | bool QAbstractItemModel::removeRows(int, int, const QModelIndex &) | - | ||||||||||||
2103 | { | - | ||||||||||||
2104 | return false; | - | ||||||||||||
2105 | } | - | ||||||||||||
2106 | - | |||||||||||||
2107 | /*! | - | ||||||||||||
2108 | On models that support this, removes \a count columns starting with the | - | ||||||||||||
2109 | given \a column under parent \a parent from the model. | - | ||||||||||||
2110 | - | |||||||||||||
2111 | Returns \c{true} if the columns were successfully removed; otherwise returns | - | ||||||||||||
2112 | \c{false}. | - | ||||||||||||
2113 | - | |||||||||||||
2114 | The base class implementation does nothing and returns \c{false}. | - | ||||||||||||
2115 | - | |||||||||||||
2116 | If you implement your own model, you can reimplement this function if you | - | ||||||||||||
2117 | want to support removing. Alternatively, you can provide your own API for | - | ||||||||||||
2118 | altering the data. | - | ||||||||||||
2119 | - | |||||||||||||
2120 | \sa removeColumn(), removeRows(), insertColumns(), beginRemoveColumns(), | - | ||||||||||||
2121 | endRemoveColumns() | - | ||||||||||||
2122 | */ | - | ||||||||||||
2123 | bool QAbstractItemModel::removeColumns(int, int, const QModelIndex &) | - | ||||||||||||
2124 | { | - | ||||||||||||
2125 | return false; | - | ||||||||||||
2126 | } | - | ||||||||||||
2127 | - | |||||||||||||
2128 | /*! | - | ||||||||||||
2129 | On models that support this, moves \a count rows starting with the given | - | ||||||||||||
2130 | \a sourceRow under parent \a sourceParent to row \a destinationChild under | - | ||||||||||||
2131 | parent \a destinationParent. | - | ||||||||||||
2132 | - | |||||||||||||
2133 | Returns \c{true} if the rows were successfully moved; otherwise returns | - | ||||||||||||
2134 | \c{false}. | - | ||||||||||||
2135 | - | |||||||||||||
2136 | The base class implementation does nothing and returns \c{false}. | - | ||||||||||||
2137 | - | |||||||||||||
2138 | If you implement your own model, you can reimplement this function if you | - | ||||||||||||
2139 | want to support moving. Alternatively, you can provide your own API for | - | ||||||||||||
2140 | altering the data. | - | ||||||||||||
2141 | - | |||||||||||||
2142 | \sa beginMoveRows(), endMoveRows() | - | ||||||||||||
2143 | */ | - | ||||||||||||
2144 | bool QAbstractItemModel::moveRows(const QModelIndex &, int , int , const QModelIndex &, int) | - | ||||||||||||
2145 | { | - | ||||||||||||
2146 | return false; | - | ||||||||||||
2147 | } | - | ||||||||||||
2148 | - | |||||||||||||
2149 | /*! | - | ||||||||||||
2150 | On models that support this, moves \a count columns starting with the given | - | ||||||||||||
2151 | \a sourceColumn under parent \a sourceParent to column \a destinationChild under | - | ||||||||||||
2152 | parent \a destinationParent. | - | ||||||||||||
2153 | - | |||||||||||||
2154 | Returns \c{true} if the columns were successfully moved; otherwise returns | - | ||||||||||||
2155 | \c{false}. | - | ||||||||||||
2156 | - | |||||||||||||
2157 | The base class implementation does nothing and returns \c{false}. | - | ||||||||||||
2158 | - | |||||||||||||
2159 | If you implement your own model, you can reimplement this function if you | - | ||||||||||||
2160 | want to support moving. Alternatively, you can provide your own API for | - | ||||||||||||
2161 | altering the data. | - | ||||||||||||
2162 | - | |||||||||||||
2163 | \sa beginMoveColumns(), endMoveColumns() | - | ||||||||||||
2164 | */ | - | ||||||||||||
2165 | bool QAbstractItemModel::moveColumns(const QModelIndex &, int , int , const QModelIndex &, int) | - | ||||||||||||
2166 | { | - | ||||||||||||
2167 | return false; | - | ||||||||||||
2168 | } | - | ||||||||||||
2169 | - | |||||||||||||
2170 | /*! | - | ||||||||||||
2171 | Fetches any available data for the items with the parent specified by the | - | ||||||||||||
2172 | \a parent index. | - | ||||||||||||
2173 | - | |||||||||||||
2174 | Reimplement this if you are populating your model incrementally. | - | ||||||||||||
2175 | - | |||||||||||||
2176 | The default implementation does nothing. | - | ||||||||||||
2177 | - | |||||||||||||
2178 | \sa canFetchMore() | - | ||||||||||||
2179 | */ | - | ||||||||||||
2180 | void QAbstractItemModel::fetchMore(const QModelIndex &) | - | ||||||||||||
2181 | { | - | ||||||||||||
2182 | // do nothing | - | ||||||||||||
2183 | } | - | ||||||||||||
2184 | - | |||||||||||||
2185 | /*! | - | ||||||||||||
2186 | Returns \c{true} if there is more data available for \a parent; otherwise | - | ||||||||||||
2187 | returns \c{false}. | - | ||||||||||||
2188 | - | |||||||||||||
2189 | The default implementation always returns \c{false}. | - | ||||||||||||
2190 | - | |||||||||||||
2191 | If canFetchMore() returns \c true, the fetchMore() function should | - | ||||||||||||
2192 | be called. This is the behavior of QAbstractItemView, for example. | - | ||||||||||||
2193 | - | |||||||||||||
2194 | \sa fetchMore() | - | ||||||||||||
2195 | */ | - | ||||||||||||
2196 | bool QAbstractItemModel::canFetchMore(const QModelIndex &) const | - | ||||||||||||
2197 | { | - | ||||||||||||
2198 | return false; | - | ||||||||||||
2199 | } | - | ||||||||||||
2200 | - | |||||||||||||
2201 | /*! | - | ||||||||||||
2202 | Returns the item flags for the given \a index. | - | ||||||||||||
2203 | - | |||||||||||||
2204 | The base class implementation returns a combination of flags that enables | - | ||||||||||||
2205 | the item (\c ItemIsEnabled) and allows it to be selected | - | ||||||||||||
2206 | (\c ItemIsSelectable). | - | ||||||||||||
2207 | - | |||||||||||||
2208 | \sa Qt::ItemFlags | - | ||||||||||||
2209 | */ | - | ||||||||||||
2210 | Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const | - | ||||||||||||
2211 | { | - | ||||||||||||
2212 | Q_D(const QAbstractItemModel); | - | ||||||||||||
2213 | if (!d->indexValid(index)) | - | ||||||||||||
2214 | return 0; | - | ||||||||||||
2215 | - | |||||||||||||
2216 | return Qt::ItemIsSelectable|Qt::ItemIsEnabled; | - | ||||||||||||
2217 | } | - | ||||||||||||
2218 | - | |||||||||||||
2219 | /*! | - | ||||||||||||
2220 | Sorts the model by \a column in the given \a order. | - | ||||||||||||
2221 | - | |||||||||||||
2222 | The base class implementation does nothing. | - | ||||||||||||
2223 | */ | - | ||||||||||||
2224 | void QAbstractItemModel::sort(int column, Qt::SortOrder order) | - | ||||||||||||
2225 | { | - | ||||||||||||
2226 | Q_UNUSED(column); | - | ||||||||||||
2227 | Q_UNUSED(order); | - | ||||||||||||
2228 | // do nothing | - | ||||||||||||
2229 | } | - | ||||||||||||
2230 | - | |||||||||||||
2231 | /*! | - | ||||||||||||
2232 | Returns a model index for the buddy of the item represented by \a index. | - | ||||||||||||
2233 | When the user wants to edit an item, the view will call this function to | - | ||||||||||||
2234 | check whether another item in the model should be edited instead. Then, the | - | ||||||||||||
2235 | view will construct a delegate using the model index returned by the buddy | - | ||||||||||||
2236 | item. | - | ||||||||||||
2237 | - | |||||||||||||
2238 | The default implementation of this function has each item as its own buddy. | - | ||||||||||||
2239 | */ | - | ||||||||||||
2240 | QModelIndex QAbstractItemModel::buddy(const QModelIndex &index) const | - | ||||||||||||
2241 | { | - | ||||||||||||
2242 | return index; | - | ||||||||||||
2243 | } | - | ||||||||||||
2244 | - | |||||||||||||
2245 | /*! | - | ||||||||||||
2246 | Returns a list of indexes for the items in the column of the \a start index | - | ||||||||||||
2247 | where data stored under the given \a role matches the specified \a value. | - | ||||||||||||
2248 | The way the search is performed is defined by the \a flags given. The list | - | ||||||||||||
2249 | that is returned may be empty. Note also that the order of results in the | - | ||||||||||||
2250 | list may not correspond to the order in the model, if for example a proxy | - | ||||||||||||
2251 | model is used. The order of the results can not be relied upon. | - | ||||||||||||
2252 | - | |||||||||||||
2253 | The search begins from the \a start index, and continues until the number | - | ||||||||||||
2254 | of matching data items equals \a hits, the search reaches the last row, or | - | ||||||||||||
2255 | the search reaches \a start again - depending on whether \c MatchWrap is | - | ||||||||||||
2256 | specified in \a flags. If you want to search for all matching items, use | - | ||||||||||||
2257 | \a hits = -1. | - | ||||||||||||
2258 | - | |||||||||||||
2259 | By default, this function will perform a wrapping, string-based comparison | - | ||||||||||||
2260 | on all items, searching for items that begin with the search term specified | - | ||||||||||||
2261 | by \a value. | - | ||||||||||||
2262 | - | |||||||||||||
2263 | \note The default implementation of this function only searches columns. | - | ||||||||||||
2264 | Reimplement this function to include a different search behavior. | - | ||||||||||||
2265 | */ | - | ||||||||||||
2266 | QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role, | - | ||||||||||||
2267 | const QVariant &value, int hits, | - | ||||||||||||
2268 | Qt::MatchFlags flags) const | - | ||||||||||||
2269 | { | - | ||||||||||||
2270 | QModelIndexList result; | - | ||||||||||||
2271 | uint matchType = flags & 0x0F; | - | ||||||||||||
2272 | Qt::CaseSensitivity cs = flags & Qt::MatchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive; | - | ||||||||||||
2273 | bool recurse = flags & Qt::MatchRecursive; | - | ||||||||||||
2274 | bool wrap = flags & Qt::MatchWrap; | - | ||||||||||||
2275 | bool allHits = (hits == -1); | - | ||||||||||||
2276 | QString text; // only convert to a string if it is needed | - | ||||||||||||
2277 | QModelIndex p = parent(start); | - | ||||||||||||
2278 | int from = start.row(); | - | ||||||||||||
2279 | int to = rowCount(p); | - | ||||||||||||
2280 | - | |||||||||||||
2281 | // iterates twice if wrapping | - | ||||||||||||
2282 | for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) { | - | ||||||||||||
2283 | for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) { | - | ||||||||||||
2284 | QModelIndex idx = index(r, start.column(), p); | - | ||||||||||||
2285 | if (!idx.isValid()) | - | ||||||||||||
2286 | continue; | - | ||||||||||||
2287 | QVariant v = data(idx, role); | - | ||||||||||||
2288 | // QVariant based matching | - | ||||||||||||
2289 | if (matchType == Qt::MatchExactly) { | - | ||||||||||||
2290 | if (value == v) | - | ||||||||||||
2291 | result.append(idx); | - | ||||||||||||
2292 | } else { // QString based matching | - | ||||||||||||
2293 | if (text.isEmpty()) // lazy conversion | - | ||||||||||||
2294 | text = value.toString(); | - | ||||||||||||
2295 | QString t = v.toString(); | - | ||||||||||||
2296 | switch (matchType) { | - | ||||||||||||
2297 | case Qt::MatchRegExp: | - | ||||||||||||
2298 | if (QRegExp(text, cs).exactMatch(t)) | - | ||||||||||||
2299 | result.append(idx); | - | ||||||||||||
2300 | break; | - | ||||||||||||
2301 | case Qt::MatchWildcard: | - | ||||||||||||
2302 | if (QRegExp(text, cs, QRegExp::Wildcard).exactMatch(t)) | - | ||||||||||||
2303 | result.append(idx); | - | ||||||||||||
2304 | break; | - | ||||||||||||
2305 | case Qt::MatchStartsWith: | - | ||||||||||||
2306 | if (t.startsWith(text, cs)) | - | ||||||||||||
2307 | result.append(idx); | - | ||||||||||||
2308 | break; | - | ||||||||||||
2309 | case Qt::MatchEndsWith: | - | ||||||||||||
2310 | if (t.endsWith(text, cs)) | - | ||||||||||||
2311 | result.append(idx); | - | ||||||||||||
2312 | break; | - | ||||||||||||
2313 | case Qt::MatchFixedString: | - | ||||||||||||
2314 | if (t.compare(text, cs) == 0) | - | ||||||||||||
2315 | result.append(idx); | - | ||||||||||||
2316 | break; | - | ||||||||||||
2317 | case Qt::MatchContains: | - | ||||||||||||
2318 | default: | - | ||||||||||||
2319 | if (t.contains(text, cs)) | - | ||||||||||||
2320 | result.append(idx); | - | ||||||||||||
2321 | } | - | ||||||||||||
2322 | } | - | ||||||||||||
2323 | if (recurse && hasChildren(idx)) { // search the hierarchy | - | ||||||||||||
2324 | result += match(index(0, idx.column(), idx), role, | - | ||||||||||||
2325 | (text.isEmpty() ? value : text), | - | ||||||||||||
2326 | (allHits ? -1 : hits - result.count()), flags); | - | ||||||||||||
2327 | } | - | ||||||||||||
2328 | } | - | ||||||||||||
2329 | // prepare for the next iteration | - | ||||||||||||
2330 | from = 0; | - | ||||||||||||
2331 | to = start.row(); | - | ||||||||||||
2332 | } | - | ||||||||||||
2333 | return result; | - | ||||||||||||
2334 | } | - | ||||||||||||
2335 | - | |||||||||||||
2336 | /*! | - | ||||||||||||
2337 | Returns the row and column span of the item represented by \a index. | - | ||||||||||||
2338 | - | |||||||||||||
2339 | \note Currently, span is not used. | - | ||||||||||||
2340 | */ | - | ||||||||||||
2341 | - | |||||||||||||
2342 | QSize QAbstractItemModel::span(const QModelIndex &) const | - | ||||||||||||
2343 | { | - | ||||||||||||
2344 | return QSize(1, 1); | - | ||||||||||||
2345 | } | - | ||||||||||||
2346 | - | |||||||||||||
2347 | /*! | - | ||||||||||||
2348 | \fn void QAbstractItemModel::setRoleNames(const QHash<int,QByteArray> &roleNames) | - | ||||||||||||
2349 | \since 4.6 | - | ||||||||||||
2350 | \obsolete | - | ||||||||||||
2351 | - | |||||||||||||
2352 | This function is obsolete. Reimplement roleNames() instead. | - | ||||||||||||
2353 | - | |||||||||||||
2354 | Sets the model's role names to \a roleNames. | - | ||||||||||||
2355 | - | |||||||||||||
2356 | This function allows mapping of role identifiers to role property names in | - | ||||||||||||
2357 | scripting languages. | - | ||||||||||||
2358 | - | |||||||||||||
2359 | \sa roleNames() | - | ||||||||||||
2360 | */ | - | ||||||||||||
2361 | - | |||||||||||||
2362 | /*! | - | ||||||||||||
2363 | \internal | - | ||||||||||||
2364 | */ | - | ||||||||||||
2365 | void QAbstractItemModel::doSetRoleNames(const QHash<int,QByteArray> &roleNames) | - | ||||||||||||
2366 | { | - | ||||||||||||
2367 | Q_D(QAbstractItemModel); | - | ||||||||||||
2368 | d->roleNames = roleNames; | - | ||||||||||||
2369 | } | - | ||||||||||||
2370 | - | |||||||||||||
2371 | /*! | - | ||||||||||||
2372 | \since 4.6 | - | ||||||||||||
2373 | - | |||||||||||||
2374 | Returns the model's role names. | - | ||||||||||||
2375 | - | |||||||||||||
2376 | The default role names set by Qt are: | - | ||||||||||||
2377 | - | |||||||||||||
2378 | \table | - | ||||||||||||
2379 | \header | - | ||||||||||||
2380 | \li Qt Role | - | ||||||||||||
2381 | \li QML Role Name | - | ||||||||||||
2382 | \row | - | ||||||||||||
2383 | \li Qt::DisplayRole | - | ||||||||||||
2384 | \li display | - | ||||||||||||
2385 | \row | - | ||||||||||||
2386 | \li Qt::DecorationRole | - | ||||||||||||
2387 | \li decoration | - | ||||||||||||
2388 | \row | - | ||||||||||||
2389 | \li Qt::EditRole | - | ||||||||||||
2390 | \li edit | - | ||||||||||||
2391 | \row | - | ||||||||||||
2392 | \li Qt::ToolTipRole | - | ||||||||||||
2393 | \li toolTip | - | ||||||||||||
2394 | \row | - | ||||||||||||
2395 | \li Qt::StatusTipRole | - | ||||||||||||
2396 | \li statusTip | - | ||||||||||||
2397 | \row | - | ||||||||||||
2398 | \li Qt::WhatsThisRole | - | ||||||||||||
2399 | \li whatsThis | - | ||||||||||||
2400 | \endtable | - | ||||||||||||
2401 | */ | - | ||||||||||||
2402 | QHash<int,QByteArray> QAbstractItemModel::roleNames() const | - | ||||||||||||
2403 | { | - | ||||||||||||
2404 | Q_D(const QAbstractItemModel); | - | ||||||||||||
2405 | return d->roleNames; | - | ||||||||||||
2406 | } | - | ||||||||||||
2407 | - | |||||||||||||
2408 | /*! | - | ||||||||||||
2409 | Lets the model know that it should submit cached information to permanent | - | ||||||||||||
2410 | storage. This function is typically used for row editing. | - | ||||||||||||
2411 | - | |||||||||||||
2412 | Returns \c{true} if there is no error; otherwise returns \c{false}. | - | ||||||||||||
2413 | - | |||||||||||||
2414 | \sa revert() | - | ||||||||||||
2415 | */ | - | ||||||||||||
2416 | - | |||||||||||||
2417 | bool QAbstractItemModel::submit() | - | ||||||||||||
2418 | { | - | ||||||||||||
2419 | return true; | - | ||||||||||||
2420 | } | - | ||||||||||||
2421 | - | |||||||||||||
2422 | /*! | - | ||||||||||||
2423 | Lets the model know that it should discard cached information. This | - | ||||||||||||
2424 | function is typically used for row editing. | - | ||||||||||||
2425 | - | |||||||||||||
2426 | \sa submit() | - | ||||||||||||
2427 | */ | - | ||||||||||||
2428 | - | |||||||||||||
2429 | void QAbstractItemModel::revert() | - | ||||||||||||
2430 | { | - | ||||||||||||
2431 | // do nothing | - | ||||||||||||
2432 | } | - | ||||||||||||
2433 | - | |||||||||||||
2434 | /*! | - | ||||||||||||
2435 | Returns the data for the given \a role and \a section in the header with | - | ||||||||||||
2436 | the specified \a orientation. | - | ||||||||||||
2437 | - | |||||||||||||
2438 | For horizontal headers, the section number corresponds to the column | - | ||||||||||||
2439 | number. Similarly, for vertical headers, the section number corresponds to | - | ||||||||||||
2440 | the row number. | - | ||||||||||||
2441 | - | |||||||||||||
2442 | \sa Qt::ItemDataRole, setHeaderData(), QHeaderView | - | ||||||||||||
2443 | */ | - | ||||||||||||
2444 | - | |||||||||||||
2445 | QVariant QAbstractItemModel::headerData(int section, Qt::Orientation orientation, int role) const | - | ||||||||||||
2446 | { | - | ||||||||||||
2447 | Q_UNUSED(orientation); | - | ||||||||||||
2448 | if (role == Qt::DisplayRole) | - | ||||||||||||
2449 | return section + 1; | - | ||||||||||||
2450 | return QVariant(); | - | ||||||||||||
2451 | } | - | ||||||||||||
2452 | - | |||||||||||||
2453 | /*! | - | ||||||||||||
2454 | Sets the data for the given \a role and \a section in the header with the | - | ||||||||||||
2455 | specified \a orientation to the \a value supplied. | - | ||||||||||||
2456 | - | |||||||||||||
2457 | Returns \c{true} if the header's data was updated; otherwise returns \c{false}. | - | ||||||||||||
2458 | - | |||||||||||||
2459 | When reimplementing this function, the headerDataChanged() signal must be | - | ||||||||||||
2460 | emitted explicitly. | - | ||||||||||||
2461 | - | |||||||||||||
2462 | \sa Qt::ItemDataRole, headerData() | - | ||||||||||||
2463 | */ | - | ||||||||||||
2464 | - | |||||||||||||
2465 | bool QAbstractItemModel::setHeaderData(int section, Qt::Orientation orientation, | - | ||||||||||||
2466 | const QVariant &value, int role) | - | ||||||||||||
2467 | { | - | ||||||||||||
2468 | Q_UNUSED(section); | - | ||||||||||||
2469 | Q_UNUSED(orientation); | - | ||||||||||||
2470 | Q_UNUSED(value); | - | ||||||||||||
2471 | Q_UNUSED(role); | - | ||||||||||||
2472 | return false; | - | ||||||||||||
2473 | } | - | ||||||||||||
2474 | - | |||||||||||||
2475 | /*! | - | ||||||||||||
2476 | \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, void *ptr) const | - | ||||||||||||
2477 | - | |||||||||||||
2478 | Creates a model index for the given \a row and \a column with the internal | - | ||||||||||||
2479 | pointer \a ptr. | - | ||||||||||||
2480 | - | |||||||||||||
2481 | When using a QSortFilterProxyModel, its indexes have their own internal | - | ||||||||||||
2482 | pointer. It is not advisable to access this internal pointer outside of the | - | ||||||||||||
2483 | model. Use the data() function instead. | - | ||||||||||||
2484 | - | |||||||||||||
2485 | This function provides a consistent interface that model subclasses must | - | ||||||||||||
2486 | use to create model indexes. | - | ||||||||||||
2487 | */ | - | ||||||||||||
2488 | - | |||||||||||||
2489 | /*! | - | ||||||||||||
2490 | \fn QModelIndex QAbstractItemModel::createIndex(int row, int column, quintptr id) const | - | ||||||||||||
2491 | - | |||||||||||||
2492 | Creates a model index for the given \a row and \a column with the internal | - | ||||||||||||
2493 | identifier, \a id. | - | ||||||||||||
2494 | - | |||||||||||||
2495 | This function provides a consistent interface that model subclasses must | - | ||||||||||||
2496 | use to create model indexes. | - | ||||||||||||
2497 | - | |||||||||||||
2498 | \sa QModelIndex::internalId() | - | ||||||||||||
2499 | */ | - | ||||||||||||
2500 | - | |||||||||||||
2501 | /*! | - | ||||||||||||
2502 | \internal | - | ||||||||||||
2503 | */ | - | ||||||||||||
2504 | void QAbstractItemModel::encodeData(const QModelIndexList &indexes, QDataStream &stream) const | - | ||||||||||||
2505 | { | - | ||||||||||||
2506 | QModelIndexList::ConstIterator it = indexes.begin(); | - | ||||||||||||
2507 | for (; it != indexes.end(); ++it) | - | ||||||||||||
2508 | stream << (*it).row() << (*it).column() << itemData(*it); | - | ||||||||||||
2509 | } | - | ||||||||||||
2510 | - | |||||||||||||
2511 | /*! | - | ||||||||||||
2512 | \internal | - | ||||||||||||
2513 | */ | - | ||||||||||||
2514 | bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &parent, | - | ||||||||||||
2515 | QDataStream &stream) | - | ||||||||||||
2516 | { | - | ||||||||||||
2517 | int top = INT_MAX; | - | ||||||||||||
2518 | int left = INT_MAX; | - | ||||||||||||
2519 | int bottom = 0; | - | ||||||||||||
2520 | int right = 0; | - | ||||||||||||
2521 | QVector<int> rows, columns; | - | ||||||||||||
2522 | QVector<QMap<int, QVariant> > data; | - | ||||||||||||
2523 | - | |||||||||||||
2524 | while (!stream.atEnd()) {
| 12-46 | ||||||||||||
2525 | int r, c; | - | ||||||||||||
2526 | QMap<int, QVariant> v; | - | ||||||||||||
2527 | stream >> r >> c >> v; | - | ||||||||||||
2528 | rows.append(r); | - | ||||||||||||
2529 | columns.append(c); | - | ||||||||||||
2530 | data.append(v); | - | ||||||||||||
2531 | top = qMin(r, top); | - | ||||||||||||
2532 | left = qMin(c, left); | - | ||||||||||||
2533 | bottom = qMax(r, bottom); | - | ||||||||||||
2534 | right = qMax(c, right); | - | ||||||||||||
2535 | } executed 46 times by 1 test: end of block Executed by:
| 46 | ||||||||||||
2536 | - | |||||||||||||
2537 | // insert the dragged items into the table, use a bit array to avoid overwriting items, | - | ||||||||||||
2538 | // since items from different tables can have the same row and column | - | ||||||||||||
2539 | int dragRowCount = 0; | - | ||||||||||||
2540 | int dragColumnCount = right - left + 1; | - | ||||||||||||
2541 | - | |||||||||||||
2542 | // Compute the number of continuous rows upon insertion and modify the rows to match | - | ||||||||||||
2543 | QVector<int> rowsToInsert(bottom + 1); | - | ||||||||||||
2544 | for (int i = 0; i < rows.count(); ++i)
| 12-46 | ||||||||||||
2545 | rowsToInsert[rows.at(i)] = 1; executed 46 times by 1 test: rowsToInsert[rows.at(i)] = 1; Executed by:
| 46 | ||||||||||||
2546 | for (int i = 0; i < rowsToInsert.count(); ++i) {
| 12-24 | ||||||||||||
2547 | if (rowsToInsert[.at(i]) == 1){
| 2-22 | ||||||||||||
2548 | rowsToInsert[i] = dragRowCount; | - | ||||||||||||
2549 | ++dragRowCount; | - | ||||||||||||
2550 | } executed 22 times by 1 test: end of block Executed by:
| 22 | ||||||||||||
2551 | } executed 24 times by 1 test: end of block Executed by:
| 24 | ||||||||||||
2552 | for (int i = 0; i < rows.count(); ++i)
| 12-46 | ||||||||||||
2553 | rows[i] = top + rowsToInsert[.at(rows[.at(i]];)); executed 46 times by 1 test: rows[i] = top + rowsToInsert.at(rows.at(i)); Executed by:
| 46 | ||||||||||||
2554 | - | |||||||||||||
2555 | QBitArray isWrittenTo(dragRowCount * dragColumnCount); | - | ||||||||||||
2556 | - | |||||||||||||
2557 | // make space in the table for the dropped data | - | ||||||||||||
2558 | int colCount = columnCount(parent); | - | ||||||||||||
2559 | if (colCount == 0) {
| 0-12 | ||||||||||||
2560 | insertColumns(colCount, dragColumnCount - colCount, parent); | - | ||||||||||||
2561 | colCount = columnCount(parent); | - | ||||||||||||
2562 | } never executed: end of block | 0 | ||||||||||||
2563 | insertRows(row, dragRowCount, parent); | - | ||||||||||||
2564 | - | |||||||||||||
2565 | row = qMax(0, row); | - | ||||||||||||
2566 | column = qMax(0, column); | - | ||||||||||||
2567 | - | |||||||||||||
2568 | QVector<QPersistentModelIndex> newIndexes(data.size()); | - | ||||||||||||
2569 | // set the data in the table | - | ||||||||||||
2570 | for (int j = 0; j < data.size(); ++j) {
| 12-46 | ||||||||||||
2571 | int relativeRow = rows.at(j) - top; | - | ||||||||||||
2572 | int relativeColumn = columns.at(j) - left; | - | ||||||||||||
2573 | int destinationRow = relativeRow + row; | - | ||||||||||||
2574 | int destinationColumn = relativeColumn + column; | - | ||||||||||||
2575 | int flat = (relativeRow * dragColumnCount) + relativeColumn; | - | ||||||||||||
2576 | // if the item was already written to, or we just can't fit it in the table, create a new row | - | ||||||||||||
2577 | if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
| 3-37 | ||||||||||||
2578 | destinationColumn = qBound(column, destinationColumn, colCount - 1); | - | ||||||||||||
2579 | destinationRow = row + dragRowCount; | - | ||||||||||||
2580 | insertRows(row + dragRowCount, 1, parent); | - | ||||||||||||
2581 | flat = (dragRowCount * dragColumnCount) + relativeColumn; | - | ||||||||||||
2582 | isWrittenTo.resize(++dragRowCount * dragColumnCount); | - | ||||||||||||
2583 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||
2584 | if (!isWrittenTo.testBit(flat)) {
| 0-46 | ||||||||||||
2585 | newIndexes[j] = index(destinationRow, destinationColumn, parent); | - | ||||||||||||
2586 | isWrittenTo.setBit(flat); | - | ||||||||||||
2587 | } executed 46 times by 1 test: end of block Executed by:
| 46 | ||||||||||||
2588 | } executed 46 times by 1 test: end of block Executed by:
| 46 | ||||||||||||
2589 | - | |||||||||||||
2590 | for(int k = 0; k < newIndexes.size(); k++) {
| 12-46 | ||||||||||||
2591 | if (newIndexes.at(k).isValid())
| 0-46 | ||||||||||||
2592 | setItemData(newIndexes.at(k), data.at(k)); executed 46 times by 1 test: setItemData(newIndexes.at(k), data.at(k)); Executed by:
| 46 | ||||||||||||
2593 | } executed 46 times by 1 test: end of block Executed by:
| 46 | ||||||||||||
2594 | - | |||||||||||||
2595 | return true; executed 12 times by 1 test: return true; Executed by:
| 12 | ||||||||||||
2596 | } | - | ||||||||||||
2597 | - | |||||||||||||
2598 | /*! | - | ||||||||||||
2599 | Begins a row insertion operation. | - | ||||||||||||
2600 | - | |||||||||||||
2601 | When reimplementing insertRows() in a subclass, you must call this function | - | ||||||||||||
2602 | \e before inserting data into the model's underlying data store. | - | ||||||||||||
2603 | - | |||||||||||||
2604 | The \a parent index corresponds to the parent into which the new rows are | - | ||||||||||||
2605 | inserted; \a first and \a last are the row numbers that the new rows will | - | ||||||||||||
2606 | have after they have been inserted. | - | ||||||||||||
2607 | - | |||||||||||||
2608 | \table 80% | - | ||||||||||||
2609 | \row | - | ||||||||||||
2610 | \li \inlineimage modelview-begin-insert-rows.png Inserting rows | - | ||||||||||||
2611 | \li Specify the first and last row numbers for the span of rows you | - | ||||||||||||
2612 | want to insert into an item in a model. | - | ||||||||||||
2613 | - | |||||||||||||
2614 | For example, as shown in the diagram, we insert three rows before | - | ||||||||||||
2615 | row 2, so \a first is 2 and \a last is 4: | - | ||||||||||||
2616 | - | |||||||||||||
2617 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 0 | - | ||||||||||||
2618 | - | |||||||||||||
2619 | This inserts the three new rows as rows 2, 3, and 4. | - | ||||||||||||
2620 | \row | - | ||||||||||||
2621 | \li \inlineimage modelview-begin-append-rows.png Appending rows | - | ||||||||||||
2622 | \li To append rows, insert them after the last row. | - | ||||||||||||
2623 | - | |||||||||||||
2624 | For example, as shown in the diagram, we append two rows to a | - | ||||||||||||
2625 | collection of 4 existing rows (ending in row 3), so \a first is 4 | - | ||||||||||||
2626 | and \a last is 5: | - | ||||||||||||
2627 | - | |||||||||||||
2628 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 1 | - | ||||||||||||
2629 | - | |||||||||||||
2630 | This appends the two new rows as rows 4 and 5. | - | ||||||||||||
2631 | \endtable | - | ||||||||||||
2632 | - | |||||||||||||
2633 | \note This function emits the rowsAboutToBeInserted() signal which | - | ||||||||||||
2634 | connected views (or proxies) must handle before the data is inserted. | - | ||||||||||||
2635 | Otherwise, the views may end up in an invalid state. | - | ||||||||||||
2636 | \sa endInsertRows() | - | ||||||||||||
2637 | */ | - | ||||||||||||
2638 | void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last) | - | ||||||||||||
2639 | { | - | ||||||||||||
2640 | Q_ASSERT(first >= 0); | - | ||||||||||||
2641 | Q_ASSERT(last >= first); | - | ||||||||||||
2642 | Q_D(QAbstractItemModel); | - | ||||||||||||
2643 | d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); | - | ||||||||||||
2644 | emit rowsAboutToBeInserted(parent, first, last, QPrivateSignal()); | - | ||||||||||||
2645 | d->rowsAboutToBeInserted(parent, first, last); | - | ||||||||||||
2646 | } | - | ||||||||||||
2647 | - | |||||||||||||
2648 | /*! | - | ||||||||||||
2649 | Ends a row insertion operation. | - | ||||||||||||
2650 | - | |||||||||||||
2651 | When reimplementing insertRows() in a subclass, you must call this function | - | ||||||||||||
2652 | \e after inserting data into the model's underlying data store. | - | ||||||||||||
2653 | - | |||||||||||||
2654 | \sa beginInsertRows() | - | ||||||||||||
2655 | */ | - | ||||||||||||
2656 | void QAbstractItemModel::endInsertRows() | - | ||||||||||||
2657 | { | - | ||||||||||||
2658 | Q_D(QAbstractItemModel); | - | ||||||||||||
2659 | QAbstractItemModelPrivate::Change change = d->changes.pop(); | - | ||||||||||||
2660 | d->rowsInserted(change.parent, change.first, change.last); | - | ||||||||||||
2661 | emit rowsInserted(change.parent, change.first, change.last, QPrivateSignal()); | - | ||||||||||||
2662 | } | - | ||||||||||||
2663 | - | |||||||||||||
2664 | /*! | - | ||||||||||||
2665 | Begins a row removal operation. | - | ||||||||||||
2666 | - | |||||||||||||
2667 | When reimplementing removeRows() in a subclass, you must call this | - | ||||||||||||
2668 | function \e before removing data from the model's underlying data store. | - | ||||||||||||
2669 | - | |||||||||||||
2670 | The \a parent index corresponds to the parent from which the new rows are | - | ||||||||||||
2671 | removed; \a first and \a last are the row numbers of the rows to be | - | ||||||||||||
2672 | removed. | - | ||||||||||||
2673 | - | |||||||||||||
2674 | \table 80% | - | ||||||||||||
2675 | \row | - | ||||||||||||
2676 | \li \inlineimage modelview-begin-remove-rows.png Removing rows | - | ||||||||||||
2677 | \li Specify the first and last row numbers for the span of rows you | - | ||||||||||||
2678 | want to remove from an item in a model. | - | ||||||||||||
2679 | - | |||||||||||||
2680 | For example, as shown in the diagram, we remove the two rows from | - | ||||||||||||
2681 | row 2 to row 3, so \a first is 2 and \a last is 3: | - | ||||||||||||
2682 | - | |||||||||||||
2683 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 2 | - | ||||||||||||
2684 | \endtable | - | ||||||||||||
2685 | - | |||||||||||||
2686 | \note This function emits the rowsAboutToBeRemoved() signal which connected | - | ||||||||||||
2687 | views (or proxies) must handle before the data is removed. Otherwise, the | - | ||||||||||||
2688 | views may end up in an invalid state. | - | ||||||||||||
2689 | - | |||||||||||||
2690 | \sa endRemoveRows() | - | ||||||||||||
2691 | */ | - | ||||||||||||
2692 | void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, int last) | - | ||||||||||||
2693 | { | - | ||||||||||||
2694 | Q_ASSERT(first >= 0); | - | ||||||||||||
2695 | Q_ASSERT(last >= first); | - | ||||||||||||
2696 | Q_D(QAbstractItemModel); | - | ||||||||||||
2697 | d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); | - | ||||||||||||
2698 | emit rowsAboutToBeRemoved(parent, first, last, QPrivateSignal()); | - | ||||||||||||
2699 | d->rowsAboutToBeRemoved(parent, first, last); | - | ||||||||||||
2700 | } | - | ||||||||||||
2701 | - | |||||||||||||
2702 | /*! | - | ||||||||||||
2703 | Ends a row removal operation. | - | ||||||||||||
2704 | - | |||||||||||||
2705 | When reimplementing removeRows() in a subclass, you must call this function | - | ||||||||||||
2706 | \e after removing data from the model's underlying data store. | - | ||||||||||||
2707 | - | |||||||||||||
2708 | \sa beginRemoveRows() | - | ||||||||||||
2709 | */ | - | ||||||||||||
2710 | void QAbstractItemModel::endRemoveRows() | - | ||||||||||||
2711 | { | - | ||||||||||||
2712 | Q_D(QAbstractItemModel); | - | ||||||||||||
2713 | QAbstractItemModelPrivate::Change change = d->changes.pop(); | - | ||||||||||||
2714 | d->rowsRemoved(change.parent, change.first, change.last); | - | ||||||||||||
2715 | emit rowsRemoved(change.parent, change.first, change.last, QPrivateSignal()); | - | ||||||||||||
2716 | } | - | ||||||||||||
2717 | - | |||||||||||||
2718 | /*! | - | ||||||||||||
2719 | Returns whether a move operation is valid. | - | ||||||||||||
2720 | - | |||||||||||||
2721 | A move operation is not allowed if it moves a continuous range of rows to a destination within | - | ||||||||||||
2722 | itself, or if it attempts to move a row to one of its own descendants. | - | ||||||||||||
2723 | - | |||||||||||||
2724 | \internal | - | ||||||||||||
2725 | */ | - | ||||||||||||
2726 | bool QAbstractItemModelPrivate::allowMove(const QModelIndex &srcParent, int start, int end, const QModelIndex &destinationParent, int destinationStart, Qt::Orientation orientation) | - | ||||||||||||
2727 | { | - | ||||||||||||
2728 | // Don't move the range within itself. | - | ||||||||||||
2729 | if (destinationParent == srcParent) | - | ||||||||||||
2730 | return !(destinationStart >= start && destinationStart <= end + 1); | - | ||||||||||||
2731 | - | |||||||||||||
2732 | QModelIndex destinationAncestor = destinationParent; | - | ||||||||||||
2733 | int pos = (Qt::Vertical == orientation) ? destinationAncestor.row() : destinationAncestor.column(); | - | ||||||||||||
2734 | forever { | - | ||||||||||||
2735 | if (destinationAncestor == srcParent) { | - | ||||||||||||
2736 | if (pos >= start && pos <= end) | - | ||||||||||||
2737 | return false; | - | ||||||||||||
2738 | break; | - | ||||||||||||
2739 | } | - | ||||||||||||
2740 | - | |||||||||||||
2741 | if (!destinationAncestor.isValid()) | - | ||||||||||||
2742 | break; | - | ||||||||||||
2743 | - | |||||||||||||
2744 | pos = (Qt::Vertical == orientation) ? destinationAncestor.row() : destinationAncestor.column(); | - | ||||||||||||
2745 | destinationAncestor = destinationAncestor.parent(); | - | ||||||||||||
2746 | } | - | ||||||||||||
2747 | - | |||||||||||||
2748 | return true; | - | ||||||||||||
2749 | } | - | ||||||||||||
2750 | - | |||||||||||||
2751 | /*! | - | ||||||||||||
2752 | \since 4.6 | - | ||||||||||||
2753 | - | |||||||||||||
2754 | Begins a row move operation. | - | ||||||||||||
2755 | - | |||||||||||||
2756 | When reimplementing a subclass, this method simplifies moving | - | ||||||||||||
2757 | entities in your model. This method is responsible for moving | - | ||||||||||||
2758 | persistent indexes in the model, which you would otherwise be | - | ||||||||||||
2759 | required to do yourself. Using beginMoveRows and endMoveRows | - | ||||||||||||
2760 | is an alternative to emitting layoutAboutToBeChanged and | - | ||||||||||||
2761 | layoutChanged directly along with changePersistentIndex. | - | ||||||||||||
2762 | - | |||||||||||||
2763 | The \a sourceParent index corresponds to the parent from which the | - | ||||||||||||
2764 | rows are moved; \a sourceFirst and \a sourceLast are the first and last | - | ||||||||||||
2765 | row numbers of the rows to be moved. The \a destinationParent index | - | ||||||||||||
2766 | corresponds to the parent into which those rows are moved. The \a | - | ||||||||||||
2767 | destinationChild is the row to which the rows will be moved. That | - | ||||||||||||
2768 | is, the index at row \a sourceFirst in \a sourceParent will become | - | ||||||||||||
2769 | row \a destinationChild in \a destinationParent, followed by all other | - | ||||||||||||
2770 | rows up to \a sourceLast. | - | ||||||||||||
2771 | - | |||||||||||||
2772 | However, when moving rows down in the same parent (\a sourceParent | - | ||||||||||||
2773 | and \a destinationParent are equal), the rows will be placed before the | - | ||||||||||||
2774 | \a destinationChild index. That is, if you wish to move rows 0 and 1 so | - | ||||||||||||
2775 | they will become rows 1 and 2, \a destinationChild should be 3. In this | - | ||||||||||||
2776 | case, the new index for the source row \c i (which is between | - | ||||||||||||
2777 | \a sourceFirst and \a sourceLast) is equal to | - | ||||||||||||
2778 | \c {(destinationChild-sourceLast-1+i)}. | - | ||||||||||||
2779 | - | |||||||||||||
2780 | Note that if \a sourceParent and \a destinationParent are the same, | - | ||||||||||||
2781 | you must ensure that the \a destinationChild is not within the range | - | ||||||||||||
2782 | of \a sourceFirst and \a sourceLast + 1. You must also ensure that you | - | ||||||||||||
2783 | do not attempt to move a row to one of its own children or ancestors. | - | ||||||||||||
2784 | This method returns \c{false} if either condition is true, in which case you | - | ||||||||||||
2785 | should abort your move operation. | - | ||||||||||||
2786 | - | |||||||||||||
2787 | \table 80% | - | ||||||||||||
2788 | \row | - | ||||||||||||
2789 | \li \inlineimage modelview-move-rows-1.png Moving rows to another parent | - | ||||||||||||
2790 | \li Specify the first and last row numbers for the span of rows in | - | ||||||||||||
2791 | the source parent you want to move in the model. Also specify | - | ||||||||||||
2792 | the row in the destination parent to move the span to. | - | ||||||||||||
2793 | - | |||||||||||||
2794 | For example, as shown in the diagram, we move three rows from | - | ||||||||||||
2795 | row 2 to 4 in the source, so \a sourceFirst is 2 and \a sourceLast is 4. | - | ||||||||||||
2796 | We move those items to above row 2 in the destination, so \a destinationChild is 2. | - | ||||||||||||
2797 | - | |||||||||||||
2798 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 6 | - | ||||||||||||
2799 | - | |||||||||||||
2800 | This moves the three rows rows 2, 3, and 4 in the source to become 2, 3 and 4 in | - | ||||||||||||
2801 | the destination. Other affected siblings are displaced accordingly. | - | ||||||||||||
2802 | \row | - | ||||||||||||
2803 | \li \inlineimage modelview-move-rows-2.png Moving rows to append to another parent | - | ||||||||||||
2804 | \li To append rows to another parent, move them to after the last row. | - | ||||||||||||
2805 | - | |||||||||||||
2806 | For example, as shown in the diagram, we move three rows to a | - | ||||||||||||
2807 | collection of 6 existing rows (ending in row 5), so \a destinationChild is 6: | - | ||||||||||||
2808 | - | |||||||||||||
2809 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 7 | - | ||||||||||||
2810 | - | |||||||||||||
2811 | This moves the target rows to the end of the target parent as 6, 7 and 8. | - | ||||||||||||
2812 | \row | - | ||||||||||||
2813 | \li \inlineimage modelview-move-rows-3.png Moving rows in the same parent up | - | ||||||||||||
2814 | \li To move rows within the same parent, specify the row to move them to. | - | ||||||||||||
2815 | - | |||||||||||||
2816 | For example, as shown in the diagram, we move one item from row 2 to row 0, | - | ||||||||||||
2817 | so \a sourceFirst and \a sourceLast are 2 and \a destinationChild is 0. | - | ||||||||||||
2818 | - | |||||||||||||
2819 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 8 | - | ||||||||||||
2820 | - | |||||||||||||
2821 | Note that other rows may be displaced accordingly. Note also that when moving | - | ||||||||||||
2822 | items within the same parent you should not attempt invalid or no-op moves. In | - | ||||||||||||
2823 | the above example, item 2 is at row 2 before the move, so it can not be moved | - | ||||||||||||
2824 | to row 2 (where it is already) or row 3 (no-op as row 3 means above row 3, where | - | ||||||||||||
2825 | it is already) | - | ||||||||||||
2826 | - | |||||||||||||
2827 | \row | - | ||||||||||||
2828 | \li \inlineimage modelview-move-rows-4.png Moving rows in the same parent down | - | ||||||||||||
2829 | \li To move rows within the same parent, specify the row to move them to. | - | ||||||||||||
2830 | - | |||||||||||||
2831 | For example, as shown in the diagram, we move one item from row 2 to row 4, | - | ||||||||||||
2832 | so \a sourceFirst and \a sourceLast are 2 and \a destinationChild is 4. | - | ||||||||||||
2833 | - | |||||||||||||
2834 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 9 | - | ||||||||||||
2835 | - | |||||||||||||
2836 | Note that other rows may be displaced accordingly. | - | ||||||||||||
2837 | \endtable | - | ||||||||||||
2838 | - | |||||||||||||
2839 | \sa endMoveRows() | - | ||||||||||||
2840 | */ | - | ||||||||||||
2841 | bool QAbstractItemModel::beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild) | - | ||||||||||||
2842 | { | - | ||||||||||||
2843 | Q_ASSERT(sourceFirst >= 0); | - | ||||||||||||
2844 | Q_ASSERT(sourceLast >= sourceFirst); | - | ||||||||||||
2845 | Q_ASSERT(destinationChild >= 0); | - | ||||||||||||
2846 | Q_D(QAbstractItemModel); | - | ||||||||||||
2847 | - | |||||||||||||
2848 | if (!d->allowMove(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical)) { | - | ||||||||||||
2849 | return false; | - | ||||||||||||
2850 | } | - | ||||||||||||
2851 | - | |||||||||||||
2852 | QAbstractItemModelPrivate::Change sourceChange(sourceParent, sourceFirst, sourceLast); | - | ||||||||||||
2853 | sourceChange.needsAdjust = sourceParent.isValid() && sourceParent.row() >= destinationChild && sourceParent.parent() == destinationParent; | - | ||||||||||||
2854 | d->changes.push(sourceChange); | - | ||||||||||||
2855 | int destinationLast = destinationChild + (sourceLast - sourceFirst); | - | ||||||||||||
2856 | QAbstractItemModelPrivate::Change destinationChange(destinationParent, destinationChild, destinationLast); | - | ||||||||||||
2857 | destinationChange.needsAdjust = destinationParent.isValid() && destinationParent.row() >= sourceLast && destinationParent.parent() == sourceParent; | - | ||||||||||||
2858 | d->changes.push(destinationChange); | - | ||||||||||||
2859 | - | |||||||||||||
2860 | emit rowsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, QPrivateSignal()); | - | ||||||||||||
2861 | d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Vertical); | - | ||||||||||||
2862 | return true; | - | ||||||||||||
2863 | } | - | ||||||||||||
2864 | - | |||||||||||||
2865 | /*! | - | ||||||||||||
2866 | Ends a row move operation. | - | ||||||||||||
2867 | - | |||||||||||||
2868 | When implementing a subclass, you must call this | - | ||||||||||||
2869 | function \e after moving data within the model's underlying data | - | ||||||||||||
2870 | store. | - | ||||||||||||
2871 | - | |||||||||||||
2872 | \sa beginMoveRows() | - | ||||||||||||
2873 | - | |||||||||||||
2874 | \since 4.6 | - | ||||||||||||
2875 | */ | - | ||||||||||||
2876 | void QAbstractItemModel::endMoveRows() | - | ||||||||||||
2877 | { | - | ||||||||||||
2878 | Q_D(QAbstractItemModel); | - | ||||||||||||
2879 | - | |||||||||||||
2880 | QAbstractItemModelPrivate::Change insertChange = d->changes.pop(); | - | ||||||||||||
2881 | QAbstractItemModelPrivate::Change removeChange = d->changes.pop(); | - | ||||||||||||
2882 | - | |||||||||||||
2883 | QModelIndex adjustedSource = removeChange.parent; | - | ||||||||||||
2884 | QModelIndex adjustedDestination = insertChange.parent; | - | ||||||||||||
2885 | - | |||||||||||||
2886 | const int numMoved = removeChange.last - removeChange.first + 1; | - | ||||||||||||
2887 | if (insertChange.needsAdjust) | - | ||||||||||||
2888 | adjustedDestination = createIndex(adjustedDestination.row() - numMoved, adjustedDestination.column(), adjustedDestination.internalPointer()); | - | ||||||||||||
2889 | - | |||||||||||||
2890 | if (removeChange.needsAdjust) | - | ||||||||||||
2891 | adjustedSource = createIndex(adjustedSource.row() + numMoved, adjustedSource.column(), adjustedSource.internalPointer()); | - | ||||||||||||
2892 | - | |||||||||||||
2893 | d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Vertical); | - | ||||||||||||
2894 | - | |||||||||||||
2895 | emit rowsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, QPrivateSignal()); | - | ||||||||||||
2896 | } | - | ||||||||||||
2897 | - | |||||||||||||
2898 | /*! | - | ||||||||||||
2899 | Begins a column insertion operation. | - | ||||||||||||
2900 | - | |||||||||||||
2901 | When reimplementing insertColumns() in a subclass, you must call this | - | ||||||||||||
2902 | function \e before inserting data into the model's underlying data store. | - | ||||||||||||
2903 | - | |||||||||||||
2904 | The \a parent index corresponds to the parent into which the new columns | - | ||||||||||||
2905 | are inserted; \a first and \a last are the column numbers of the new | - | ||||||||||||
2906 | columns will have after they have been inserted. | - | ||||||||||||
2907 | - | |||||||||||||
2908 | \table 80% | - | ||||||||||||
2909 | \row | - | ||||||||||||
2910 | \li \inlineimage modelview-begin-insert-columns.png Inserting columns | - | ||||||||||||
2911 | \li Specify the first and last column numbers for the span of columns | - | ||||||||||||
2912 | you want to insert into an item in a model. | - | ||||||||||||
2913 | - | |||||||||||||
2914 | For example, as shown in the diagram, we insert three columns | - | ||||||||||||
2915 | before column 4, so \a first is 4 and \a last is 6: | - | ||||||||||||
2916 | - | |||||||||||||
2917 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 3 | - | ||||||||||||
2918 | - | |||||||||||||
2919 | This inserts the three new columns as columns 4, 5, and 6. | - | ||||||||||||
2920 | \row | - | ||||||||||||
2921 | \li \inlineimage modelview-begin-append-columns.png Appending columns | - | ||||||||||||
2922 | \li To append columns, insert them after the last column. | - | ||||||||||||
2923 | - | |||||||||||||
2924 | For example, as shown in the diagram, we append three columns to a | - | ||||||||||||
2925 | collection of six existing columns (ending in column 5), so | - | ||||||||||||
2926 | \a first is 6 and \a last is 8: | - | ||||||||||||
2927 | - | |||||||||||||
2928 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 4 | - | ||||||||||||
2929 | - | |||||||||||||
2930 | This appends the two new columns as columns 6, 7, and 8. | - | ||||||||||||
2931 | \endtable | - | ||||||||||||
2932 | - | |||||||||||||
2933 | \note This function emits the columnsAboutToBeInserted() signal which | - | ||||||||||||
2934 | connected views (or proxies) must handle before the data is inserted. | - | ||||||||||||
2935 | Otherwise, the views may end up in an invalid state. | - | ||||||||||||
2936 | - | |||||||||||||
2937 | \sa endInsertColumns() | - | ||||||||||||
2938 | */ | - | ||||||||||||
2939 | void QAbstractItemModel::beginInsertColumns(const QModelIndex &parent, int first, int last) | - | ||||||||||||
2940 | { | - | ||||||||||||
2941 | Q_ASSERT(first >= 0); | - | ||||||||||||
2942 | Q_ASSERT(last >= first); | - | ||||||||||||
2943 | Q_D(QAbstractItemModel); | - | ||||||||||||
2944 | d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); | - | ||||||||||||
2945 | emit columnsAboutToBeInserted(parent, first, last, QPrivateSignal()); | - | ||||||||||||
2946 | d->columnsAboutToBeInserted(parent, first, last); | - | ||||||||||||
2947 | } | - | ||||||||||||
2948 | - | |||||||||||||
2949 | /*! | - | ||||||||||||
2950 | Ends a column insertion operation. | - | ||||||||||||
2951 | - | |||||||||||||
2952 | When reimplementing insertColumns() in a subclass, you must call this | - | ||||||||||||
2953 | function \e after inserting data into the model's underlying data | - | ||||||||||||
2954 | store. | - | ||||||||||||
2955 | - | |||||||||||||
2956 | \sa beginInsertColumns() | - | ||||||||||||
2957 | */ | - | ||||||||||||
2958 | void QAbstractItemModel::endInsertColumns() | - | ||||||||||||
2959 | { | - | ||||||||||||
2960 | Q_D(QAbstractItemModel); | - | ||||||||||||
2961 | QAbstractItemModelPrivate::Change change = d->changes.pop(); | - | ||||||||||||
2962 | d->columnsInserted(change.parent, change.first, change.last); | - | ||||||||||||
2963 | emit columnsInserted(change.parent, change.first, change.last, QPrivateSignal()); | - | ||||||||||||
2964 | } | - | ||||||||||||
2965 | - | |||||||||||||
2966 | /*! | - | ||||||||||||
2967 | Begins a column removal operation. | - | ||||||||||||
2968 | - | |||||||||||||
2969 | When reimplementing removeColumns() in a subclass, you must call this | - | ||||||||||||
2970 | function \e before removing data from the model's underlying data store. | - | ||||||||||||
2971 | - | |||||||||||||
2972 | The \a parent index corresponds to the parent from which the new columns | - | ||||||||||||
2973 | are removed; \a first and \a last are the column numbers of the first and | - | ||||||||||||
2974 | last columns to be removed. | - | ||||||||||||
2975 | - | |||||||||||||
2976 | \table 80% | - | ||||||||||||
2977 | \row | - | ||||||||||||
2978 | \li \inlineimage modelview-begin-remove-columns.png Removing columns | - | ||||||||||||
2979 | \li Specify the first and last column numbers for the span of columns | - | ||||||||||||
2980 | you want to remove from an item in a model. | - | ||||||||||||
2981 | - | |||||||||||||
2982 | For example, as shown in the diagram, we remove the three columns | - | ||||||||||||
2983 | from column 4 to column 6, so \a first is 4 and \a last is 6: | - | ||||||||||||
2984 | - | |||||||||||||
2985 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 5 | - | ||||||||||||
2986 | \endtable | - | ||||||||||||
2987 | - | |||||||||||||
2988 | \note This function emits the columnsAboutToBeRemoved() signal which | - | ||||||||||||
2989 | connected views (or proxies) must handle before the data is removed. | - | ||||||||||||
2990 | Otherwise, the views may end up in an invalid state. | - | ||||||||||||
2991 | - | |||||||||||||
2992 | \sa endRemoveColumns() | - | ||||||||||||
2993 | */ | - | ||||||||||||
2994 | void QAbstractItemModel::beginRemoveColumns(const QModelIndex &parent, int first, int last) | - | ||||||||||||
2995 | { | - | ||||||||||||
2996 | Q_ASSERT(first >= 0); | - | ||||||||||||
2997 | Q_ASSERT(last >= first); | - | ||||||||||||
2998 | Q_D(QAbstractItemModel); | - | ||||||||||||
2999 | d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); | - | ||||||||||||
3000 | emit columnsAboutToBeRemoved(parent, first, last, QPrivateSignal()); | - | ||||||||||||
3001 | d->columnsAboutToBeRemoved(parent, first, last); | - | ||||||||||||
3002 | } | - | ||||||||||||
3003 | - | |||||||||||||
3004 | /*! | - | ||||||||||||
3005 | Ends a column removal operation. | - | ||||||||||||
3006 | - | |||||||||||||
3007 | When reimplementing removeColumns() in a subclass, you must call this | - | ||||||||||||
3008 | function \e after removing data from the model's underlying data store. | - | ||||||||||||
3009 | - | |||||||||||||
3010 | \sa beginRemoveColumns() | - | ||||||||||||
3011 | */ | - | ||||||||||||
3012 | void QAbstractItemModel::endRemoveColumns() | - | ||||||||||||
3013 | { | - | ||||||||||||
3014 | Q_D(QAbstractItemModel); | - | ||||||||||||
3015 | QAbstractItemModelPrivate::Change change = d->changes.pop(); | - | ||||||||||||
3016 | d->columnsRemoved(change.parent, change.first, change.last); | - | ||||||||||||
3017 | emit columnsRemoved(change.parent, change.first, change.last, QPrivateSignal()); | - | ||||||||||||
3018 | } | - | ||||||||||||
3019 | - | |||||||||||||
3020 | /*! | - | ||||||||||||
3021 | Begins a column move operation. | - | ||||||||||||
3022 | - | |||||||||||||
3023 | When reimplementing a subclass, this method simplifies moving | - | ||||||||||||
3024 | entities in your model. This method is responsible for moving | - | ||||||||||||
3025 | persistent indexes in the model, which you would otherwise be | - | ||||||||||||
3026 | required to do yourself. Using beginMoveRows and endMoveRows | - | ||||||||||||
3027 | is an alternative to emitting layoutAboutToBeChanged and | - | ||||||||||||
3028 | layoutChanged directly along with changePersistentIndex. | - | ||||||||||||
3029 | - | |||||||||||||
3030 | The \a sourceParent index corresponds to the parent from which the | - | ||||||||||||
3031 | columns are moved; \a sourceFirst and \a sourceLast are the first and last | - | ||||||||||||
3032 | column numbers of the columns to be moved. The \a destinationParent index | - | ||||||||||||
3033 | corresponds to the parent into which those columns are moved. The \a | - | ||||||||||||
3034 | destinationChild is the column to which the columns will be moved. That | - | ||||||||||||
3035 | is, the index at column \a sourceFirst in \a sourceParent will become | - | ||||||||||||
3036 | column \a destinationChild in \a destinationParent, followed by all other | - | ||||||||||||
3037 | columns up to \a sourceLast. | - | ||||||||||||
3038 | - | |||||||||||||
3039 | However, when moving columns down in the same parent (\a sourceParent | - | ||||||||||||
3040 | and \a destinationParent are equal), the columns will be placed before the | - | ||||||||||||
3041 | \a destinationChild index. That is, if you wish to move columns 0 and 1 so | - | ||||||||||||
3042 | they will become columns 1 and 2, \a destinationChild should be 3. In this | - | ||||||||||||
3043 | case, the new index for the source column \c i (which is between | - | ||||||||||||
3044 | \a sourceFirst and \a sourceLast) is equal to | - | ||||||||||||
3045 | \c {(destinationChild-sourceLast-1+i)}. | - | ||||||||||||
3046 | - | |||||||||||||
3047 | Note that if \a sourceParent and \a destinationParent are the same, | - | ||||||||||||
3048 | you must ensure that the \a destinationChild is not within the range | - | ||||||||||||
3049 | of \a sourceFirst and \a sourceLast + 1. You must also ensure that you | - | ||||||||||||
3050 | do not attempt to move a column to one of its own children or ancestors. | - | ||||||||||||
3051 | This method returns \c{false} if either condition is true, in which case you | - | ||||||||||||
3052 | should abort your move operation. | - | ||||||||||||
3053 | - | |||||||||||||
3054 | \sa endMoveColumns() | - | ||||||||||||
3055 | - | |||||||||||||
3056 | \since 4.6 | - | ||||||||||||
3057 | */ | - | ||||||||||||
3058 | bool QAbstractItemModel::beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationChild) | - | ||||||||||||
3059 | { | - | ||||||||||||
3060 | Q_ASSERT(sourceFirst >= 0); | - | ||||||||||||
3061 | Q_ASSERT(sourceLast >= sourceFirst); | - | ||||||||||||
3062 | Q_ASSERT(destinationChild >= 0); | - | ||||||||||||
3063 | Q_D(QAbstractItemModel); | - | ||||||||||||
3064 | - | |||||||||||||
3065 | if (!d->allowMove(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Horizontal)) { | - | ||||||||||||
3066 | return false; | - | ||||||||||||
3067 | } | - | ||||||||||||
3068 | - | |||||||||||||
3069 | QAbstractItemModelPrivate::Change sourceChange(sourceParent, sourceFirst, sourceLast); | - | ||||||||||||
3070 | sourceChange.needsAdjust = sourceParent.isValid() && sourceParent.row() >= destinationChild && sourceParent.parent() == destinationParent; | - | ||||||||||||
3071 | d->changes.push(sourceChange); | - | ||||||||||||
3072 | int destinationLast = destinationChild + (sourceLast - sourceFirst); | - | ||||||||||||
3073 | QAbstractItemModelPrivate::Change destinationChange(destinationParent, destinationChild, destinationLast); | - | ||||||||||||
3074 | destinationChange.needsAdjust = destinationParent.isValid() && destinationParent.row() >= sourceLast && destinationParent.parent() == sourceParent; | - | ||||||||||||
3075 | d->changes.push(destinationChange); | - | ||||||||||||
3076 | - | |||||||||||||
3077 | d->itemsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, Qt::Horizontal); | - | ||||||||||||
3078 | - | |||||||||||||
3079 | emit columnsAboutToBeMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationChild, QPrivateSignal()); | - | ||||||||||||
3080 | return true; | - | ||||||||||||
3081 | } | - | ||||||||||||
3082 | - | |||||||||||||
3083 | /*! | - | ||||||||||||
3084 | Ends a column move operation. | - | ||||||||||||
3085 | - | |||||||||||||
3086 | When implementing a subclass, you must call this | - | ||||||||||||
3087 | function \e after moving data within the model's underlying data | - | ||||||||||||
3088 | store. | - | ||||||||||||
3089 | - | |||||||||||||
3090 | \sa beginMoveColumns() | - | ||||||||||||
3091 | - | |||||||||||||
3092 | \since 4.6 | - | ||||||||||||
3093 | */ | - | ||||||||||||
3094 | void QAbstractItemModel::endMoveColumns() | - | ||||||||||||
3095 | { | - | ||||||||||||
3096 | Q_D(QAbstractItemModel); | - | ||||||||||||
3097 | - | |||||||||||||
3098 | QAbstractItemModelPrivate::Change insertChange = d->changes.pop(); | - | ||||||||||||
3099 | QAbstractItemModelPrivate::Change removeChange = d->changes.pop(); | - | ||||||||||||
3100 | - | |||||||||||||
3101 | QModelIndex adjustedSource = removeChange.parent; | - | ||||||||||||
3102 | QModelIndex adjustedDestination = insertChange.parent; | - | ||||||||||||
3103 | - | |||||||||||||
3104 | const int numMoved = removeChange.last - removeChange.first + 1; | - | ||||||||||||
3105 | if (insertChange.needsAdjust) | - | ||||||||||||
3106 | adjustedDestination = createIndex(adjustedDestination.row(), adjustedDestination.column() - numMoved, adjustedDestination.internalPointer()); | - | ||||||||||||
3107 | - | |||||||||||||
3108 | if (removeChange.needsAdjust) | - | ||||||||||||
3109 | adjustedSource = createIndex(adjustedSource.row(), adjustedSource.column() + numMoved, adjustedSource.internalPointer()); | - | ||||||||||||
3110 | - | |||||||||||||
3111 | d->itemsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, Qt::Horizontal); | - | ||||||||||||
3112 | - | |||||||||||||
3113 | emit columnsMoved(adjustedSource, removeChange.first, removeChange.last, adjustedDestination, insertChange.first, QPrivateSignal()); | - | ||||||||||||
3114 | } | - | ||||||||||||
3115 | - | |||||||||||||
3116 | /*! | - | ||||||||||||
3117 | \fn void QAbstractItemModel::reset() | - | ||||||||||||
3118 | \obsolete | - | ||||||||||||
3119 | - | |||||||||||||
3120 | Resets the model to its original state in any attached views. | - | ||||||||||||
3121 | - | |||||||||||||
3122 | This function emits the signals modelAboutToBeReset() and modelReset(). | - | ||||||||||||
3123 | - | |||||||||||||
3124 | \note Use beginResetModel() and endResetModel() instead whenever possible. | - | ||||||||||||
3125 | Use this method only if there is no way to call beginResetModel() before invalidating the model. | - | ||||||||||||
3126 | Otherwise it could lead to unexpected behaviour, especially when used with proxy models. | - | ||||||||||||
3127 | - | |||||||||||||
3128 | For example, in this code both signals modelAboutToBeReset() and modelReset() | - | ||||||||||||
3129 | are emitted \e after the data changes: | - | ||||||||||||
3130 | - | |||||||||||||
3131 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 10 | - | ||||||||||||
3132 | - | |||||||||||||
3133 | Instead you should use: | - | ||||||||||||
3134 | - | |||||||||||||
3135 | \snippet code/src_corelib_kernel_qabstractitemmodel.cpp 11 | - | ||||||||||||
3136 | */ | - | ||||||||||||
3137 | - | |||||||||||||
3138 | /*! | - | ||||||||||||
3139 | Begins a model reset operation. | - | ||||||||||||
3140 | - | |||||||||||||
3141 | A reset operation resets the model to its current state in any attached views. | - | ||||||||||||
3142 | - | |||||||||||||
3143 | \note Any views attached to this model will be reset as well. | - | ||||||||||||
3144 | - | |||||||||||||
3145 | When a model is reset it means that any previous data reported from the | - | ||||||||||||
3146 | model is now invalid and has to be queried for again. This also means that | - | ||||||||||||
3147 | the current item and any selected items will become invalid. | - | ||||||||||||
3148 | - | |||||||||||||
3149 | When a model radically changes its data it can sometimes be easier to just | - | ||||||||||||
3150 | call this function rather than emit dataChanged() to inform other | - | ||||||||||||
3151 | components when the underlying data source, or its structure, has changed. | - | ||||||||||||
3152 | - | |||||||||||||
3153 | You must call this function before resetting any internal data structures in your model | - | ||||||||||||
3154 | or proxy model. | - | ||||||||||||
3155 | - | |||||||||||||
3156 | This function emits the signal modelAboutToBeReset(). | - | ||||||||||||
3157 | - | |||||||||||||
3158 | \sa modelAboutToBeReset(), modelReset(), endResetModel() | - | ||||||||||||
3159 | \since 4.6 | - | ||||||||||||
3160 | */ | - | ||||||||||||
3161 | void QAbstractItemModel::beginResetModel() | - | ||||||||||||
3162 | { | - | ||||||||||||
3163 | emit modelAboutToBeReset(QPrivateSignal()); | - | ||||||||||||
3164 | } | - | ||||||||||||
3165 | - | |||||||||||||
3166 | /*! | - | ||||||||||||
3167 | Completes a model reset operation. | - | ||||||||||||
3168 | - | |||||||||||||
3169 | You must call this function after resetting any internal data structure in your model | - | ||||||||||||
3170 | or proxy model. | - | ||||||||||||
3171 | - | |||||||||||||
3172 | This function emits the signal modelReset(). | - | ||||||||||||
3173 | - | |||||||||||||
3174 | \sa beginResetModel() | - | ||||||||||||
3175 | \since 4.6 | - | ||||||||||||
3176 | */ | - | ||||||||||||
3177 | void QAbstractItemModel::endResetModel() | - | ||||||||||||
3178 | { | - | ||||||||||||
3179 | Q_D(QAbstractItemModel); | - | ||||||||||||
3180 | d->invalidatePersistentIndexes(); | - | ||||||||||||
3181 | QMetaObject::invokeMethod(this, "resetInternalData"); | - | ||||||||||||
3182 | emit modelReset(QPrivateSignal()); | - | ||||||||||||
3183 | } | - | ||||||||||||
3184 | - | |||||||||||||
3185 | /*! | - | ||||||||||||
3186 | Changes the QPersistentModelIndex that is equal to the given \a from model | - | ||||||||||||
3187 | index to the given \a to model index. | - | ||||||||||||
3188 | - | |||||||||||||
3189 | If no persistent model index equal to the given \a from model index was | - | ||||||||||||
3190 | found, nothing is changed. | - | ||||||||||||
3191 | - | |||||||||||||
3192 | \sa persistentIndexList(), changePersistentIndexList() | - | ||||||||||||
3193 | */ | - | ||||||||||||
3194 | void QAbstractItemModel::changePersistentIndex(const QModelIndex &from, const QModelIndex &to) | - | ||||||||||||
3195 | { | - | ||||||||||||
3196 | Q_D(QAbstractItemModel); | - | ||||||||||||
3197 | if (d->persistent.indexes.isEmpty())
| 0-12 | ||||||||||||
3198 | return; never executed: return; | 0 | ||||||||||||
3199 | // find the data and reinsert it sorted | - | ||||||||||||
3200 | const QHash<QModelIndex, QPersistentModelIndexData *>::iteratorauto it = d->persistent.indexes.findconstFind(from); | - | ||||||||||||
3201 | if (it != d->persistent.indexes.endcend()) {
| 0-12 | ||||||||||||
3202 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
3203 | d->persistent.indexes.erase(it); | - | ||||||||||||
3204 | data->index = to; | - | ||||||||||||
3205 | if (to.isValid())
| 0-12 | ||||||||||||
3206 | d->persistent.insertMultiAtEnd(to, data); executed 12 times by 1 test: d->persistent.insertMultiAtEnd(to, data); Executed by:
| 12 | ||||||||||||
3207 | else | - | ||||||||||||
3208 | data->model = 0; never executed: data->model = 0; | 0 | ||||||||||||
3209 | } | - | ||||||||||||
3210 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||
3211 | - | |||||||||||||
3212 | /*! | - | ||||||||||||
3213 | \since 4.1 | - | ||||||||||||
3214 | - | |||||||||||||
3215 | Changes the {QPersistentModelIndex}es that are equal to the indexes in the | - | ||||||||||||
3216 | given \a from model index list to the given \a to model index list. | - | ||||||||||||
3217 | - | |||||||||||||
3218 | If no persistent model indexes equal to the indexes in the given \a from | - | ||||||||||||
3219 | model index list are found, nothing is changed. | - | ||||||||||||
3220 | - | |||||||||||||
3221 | \sa persistentIndexList(), changePersistentIndex() | - | ||||||||||||
3222 | */ | - | ||||||||||||
3223 | void QAbstractItemModel::changePersistentIndexList(const QModelIndexList &from, | - | ||||||||||||
3224 | const QModelIndexList &to) | - | ||||||||||||
3225 | { | - | ||||||||||||
3226 | Q_D(QAbstractItemModel); | - | ||||||||||||
3227 | if (d->persistent.indexes.isEmpty())
| 735-4679 | ||||||||||||
3228 | return; executed 4679 times by 24 tests: return; Executed by:
| 4679 | ||||||||||||
3229 | QVector<QPersistentModelIndexData *> toBeReinserted; | - | ||||||||||||
3230 | toBeReinserted.reserve(to.count()); | - | ||||||||||||
3231 | for (int i = 0; i < from.count(); ++i) {
| 735-1672 | ||||||||||||
3232 | if (from.at(i) == to.at(i))
| 660-1012 | ||||||||||||
3233 | continue; executed 1012 times by 16 tests: continue; Executed by:
| 1012 | ||||||||||||
3234 | const QHash<QModelIndex, QPersistentModelIndexData *>::iteratorauto it = d->persistent.indexes.findconstFind(from.at(i)); | - | ||||||||||||
3235 | if (it != d->persistent.indexes.endcend()) {
| 126-534 | ||||||||||||
3236 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
3237 | d->persistent.indexes.erase(it); | - | ||||||||||||
3238 | data->index = to.at(i); | - | ||||||||||||
3239 | if (data->index.isValid())
| 0-534 | ||||||||||||
3240 | toBeReinserted << data; executed 534 times by 13 tests: toBeReinserted << data; Executed by:
| 534 | ||||||||||||
3241 | else | - | ||||||||||||
3242 | data->model = 0; never executed: data->model = 0; | 0 | ||||||||||||
3243 | } | - | ||||||||||||
3244 | } executed 660 times by 13 tests: end of block Executed by:
| 660 | ||||||||||||
3245 | - | |||||||||||||
3246 | for (QVector<QPersistentModelIndexData *>::const_iterator it = toBeReinserted.constBegin(); | - | ||||||||||||
3247 | it != toBeReinserted.constEnd() ; ++it) {
| 534-735 | ||||||||||||
3248 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
3249 | d->persistent.insertMultiAtEnd(data->index, data); | - | ||||||||||||
3250 | } executed 534 times by 13 tests: end of block Executed by:
| 534 | ||||||||||||
3251 | } executed 735 times by 18 tests: end of block Executed by:
| 735 | ||||||||||||
3252 | - | |||||||||||||
3253 | /*! | - | ||||||||||||
3254 | \since 4.2 | - | ||||||||||||
3255 | - | |||||||||||||
3256 | Returns the list of indexes stored as persistent indexes in the model. | - | ||||||||||||
3257 | */ | - | ||||||||||||
3258 | QModelIndexList QAbstractItemModel::persistentIndexList() const | - | ||||||||||||
3259 | { | - | ||||||||||||
3260 | Q_D(const QAbstractItemModel); | - | ||||||||||||
3261 | QModelIndexList result; | - | ||||||||||||
3262 | result.reserve(d->persistent.indexes.count()); | - | ||||||||||||
3263 | for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = d->persistent.indexes.constBegin(); | - | ||||||||||||
3264 | it != d->persistent.indexes.constEnd(); ++it) { | - | ||||||||||||
3265 | QPersistentModelIndexData *data = *it; | - | ||||||||||||
3266 | result.append(data->index); | - | ||||||||||||
3267 | } | - | ||||||||||||
3268 | return result; | - | ||||||||||||
3269 | } | - | ||||||||||||
3270 | - | |||||||||||||
3271 | - | |||||||||||||
3272 | /*! | - | ||||||||||||
3273 | \class QAbstractTableModel | - | ||||||||||||
3274 | \inmodule QtCore | - | ||||||||||||
3275 | \brief The QAbstractTableModel class provides an abstract model that can be | - | ||||||||||||
3276 | subclassed to create table models. | - | ||||||||||||
3277 | - | |||||||||||||
3278 | \ingroup model-view | - | ||||||||||||
3279 | - | |||||||||||||
3280 | QAbstractTableModel provides a standard interface for models that represent | - | ||||||||||||
3281 | their data as a two-dimensional array of items. It is not used directly, | - | ||||||||||||
3282 | but must be subclassed. | - | ||||||||||||
3283 | - | |||||||||||||
3284 | Since the model provides a more specialized interface than | - | ||||||||||||
3285 | QAbstractItemModel, it is not suitable for use with tree views, although it | - | ||||||||||||
3286 | can be used to provide data to a QListView. If you need to represent a | - | ||||||||||||
3287 | simple list of items, and only need a model to contain a single column of | - | ||||||||||||
3288 | data, subclassing the QAbstractListModel may be more appropriate. | - | ||||||||||||
3289 | - | |||||||||||||
3290 | The rowCount() and columnCount() functions return the dimensions of the | - | ||||||||||||
3291 | table. To retrieve a model index corresponding to an item in the model, use | - | ||||||||||||
3292 | index() and provide only the row and column numbers. | - | ||||||||||||
3293 | - | |||||||||||||
3294 | \section1 Subclassing | - | ||||||||||||
3295 | - | |||||||||||||
3296 | When subclassing QAbstractTableModel, you must implement rowCount(), | - | ||||||||||||
3297 | columnCount(), and data(). Default implementations of the index() and | - | ||||||||||||
3298 | parent() functions are provided by QAbstractTableModel. | - | ||||||||||||
3299 | Well behaved models will also implement headerData(). | - | ||||||||||||
3300 | - | |||||||||||||
3301 | Editable models need to implement setData(), and implement flags() to | - | ||||||||||||
3302 | return a value containing | - | ||||||||||||
3303 | \l{Qt::ItemFlags}{Qt::ItemIsEditable}. | - | ||||||||||||
3304 | - | |||||||||||||
3305 | Models that provide interfaces to resizable data structures can | - | ||||||||||||
3306 | provide implementations of insertRows(), removeRows(), insertColumns(), | - | ||||||||||||
3307 | and removeColumns(). When implementing these functions, it is | - | ||||||||||||
3308 | important to call the appropriate functions so that all connected views | - | ||||||||||||
3309 | are aware of any changes: | - | ||||||||||||
3310 | - | |||||||||||||
3311 | \list | - | ||||||||||||
3312 | \li An insertRows() implementation must call beginInsertRows() | - | ||||||||||||
3313 | \e before inserting new rows into the data structure, and it must | - | ||||||||||||
3314 | call endInsertRows() \e{immediately afterwards}. | - | ||||||||||||
3315 | \li An insertColumns() implementation must call beginInsertColumns() | - | ||||||||||||
3316 | \e before inserting new columns into the data structure, and it must | - | ||||||||||||
3317 | call endInsertColumns() \e{immediately afterwards}. | - | ||||||||||||
3318 | \li A removeRows() implementation must call beginRemoveRows() | - | ||||||||||||
3319 | \e before the rows are removed from the data structure, and it must | - | ||||||||||||
3320 | call endRemoveRows() \e{immediately afterwards}. | - | ||||||||||||
3321 | \li A removeColumns() implementation must call beginRemoveColumns() | - | ||||||||||||
3322 | \e before the columns are removed from the data structure, and it must | - | ||||||||||||
3323 | call endRemoveColumns() \e{immediately afterwards}. | - | ||||||||||||
3324 | \endlist | - | ||||||||||||
3325 | - | |||||||||||||
3326 | \note Some general guidelines for subclassing models are available in the | - | ||||||||||||
3327 | \l{Model Subclassing Reference}. | - | ||||||||||||
3328 | - | |||||||||||||
3329 | \note | - | ||||||||||||
3330 | - | |||||||||||||
3331 | \sa {Model Classes}, QAbstractItemModel, QAbstractListModel, | - | ||||||||||||
3332 | {Pixelator Example} | - | ||||||||||||
3333 | */ | - | ||||||||||||
3334 | - | |||||||||||||
3335 | /*! | - | ||||||||||||
3336 | Constructs an abstract table model for the given \a parent. | - | ||||||||||||
3337 | */ | - | ||||||||||||
3338 | - | |||||||||||||
3339 | QAbstractTableModel::QAbstractTableModel(QObject *parent) | - | ||||||||||||
3340 | : QAbstractItemModel(parent) | - | ||||||||||||
3341 | { | - | ||||||||||||
3342 | - | |||||||||||||
3343 | } | - | ||||||||||||
3344 | - | |||||||||||||
3345 | /*! | - | ||||||||||||
3346 | \internal | - | ||||||||||||
3347 | - | |||||||||||||
3348 | Constructs an abstract table model with \a dd and the given \a parent. | - | ||||||||||||
3349 | */ | - | ||||||||||||
3350 | - | |||||||||||||
3351 | QAbstractTableModel::QAbstractTableModel(QAbstractItemModelPrivate &dd, QObject *parent) | - | ||||||||||||
3352 | : QAbstractItemModel(dd, parent) | - | ||||||||||||
3353 | { | - | ||||||||||||
3354 | - | |||||||||||||
3355 | } | - | ||||||||||||
3356 | - | |||||||||||||
3357 | /*! | - | ||||||||||||
3358 | Destroys the abstract table model. | - | ||||||||||||
3359 | */ | - | ||||||||||||
3360 | - | |||||||||||||
3361 | QAbstractTableModel::~QAbstractTableModel() | - | ||||||||||||
3362 | { | - | ||||||||||||
3363 | - | |||||||||||||
3364 | } | - | ||||||||||||
3365 | - | |||||||||||||
3366 | /*! | - | ||||||||||||
3367 | \fn QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const | - | ||||||||||||
3368 | - | |||||||||||||
3369 | Returns the index of the data in \a row and \a column with \a parent. | - | ||||||||||||
3370 | - | |||||||||||||
3371 | \sa parent() | - | ||||||||||||
3372 | */ | - | ||||||||||||
3373 | - | |||||||||||||
3374 | QModelIndex QAbstractTableModel::index(int row, int column, const QModelIndex &parent) const | - | ||||||||||||
3375 | { | - | ||||||||||||
3376 | return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex(); | - | ||||||||||||
3377 | } | - | ||||||||||||
3378 | - | |||||||||||||
3379 | /*! | - | ||||||||||||
3380 | \fn QModelIndex QAbstractTableModel::parent(const QModelIndex &index) const | - | ||||||||||||
3381 | - | |||||||||||||
3382 | Returns the parent of the model item with the given \a index. | - | ||||||||||||
3383 | - | |||||||||||||
3384 | \sa index(), hasChildren() | - | ||||||||||||
3385 | */ | - | ||||||||||||
3386 | - | |||||||||||||
3387 | QModelIndex QAbstractTableModel::parent(const QModelIndex &) const | - | ||||||||||||
3388 | { | - | ||||||||||||
3389 | return QModelIndex(); | - | ||||||||||||
3390 | } | - | ||||||||||||
3391 | - | |||||||||||||
3392 | /*! | - | ||||||||||||
3393 | \reimp | - | ||||||||||||
3394 | */ | - | ||||||||||||
3395 | QModelIndex QAbstractTableModel::sibling(int row, int column, const QModelIndex &) const | - | ||||||||||||
3396 | { | - | ||||||||||||
3397 | return index(row, column); | - | ||||||||||||
3398 | } | - | ||||||||||||
3399 | - | |||||||||||||
3400 | bool QAbstractTableModel::hasChildren(const QModelIndex &parent) const | - | ||||||||||||
3401 | { | - | ||||||||||||
3402 | if (parent.model() == this || !parent.isValid()) | - | ||||||||||||
3403 | return rowCount(parent) > 0 && columnCount(parent) > 0; | - | ||||||||||||
3404 | return false; | - | ||||||||||||
3405 | } | - | ||||||||||||
3406 | - | |||||||||||||
3407 | /*! | - | ||||||||||||
3408 | \reimp | - | ||||||||||||
3409 | */ | - | ||||||||||||
3410 | Qt::ItemFlags QAbstractTableModel::flags(const QModelIndex &index) const | - | ||||||||||||
3411 | { | - | ||||||||||||
3412 | Qt::ItemFlags f = QAbstractItemModel::flags(index); | - | ||||||||||||
3413 | if (index.isValid()) | - | ||||||||||||
3414 | f |= Qt::ItemNeverHasChildren; | - | ||||||||||||
3415 | return f; | - | ||||||||||||
3416 | } | - | ||||||||||||
3417 | - | |||||||||||||
3418 | /*! | - | ||||||||||||
3419 | \class QAbstractListModel | - | ||||||||||||
3420 | \inmodule QtCore | - | ||||||||||||
3421 | \brief The QAbstractListModel class provides an abstract model that can be | - | ||||||||||||
3422 | subclassed to create one-dimensional list models. | - | ||||||||||||
3423 | - | |||||||||||||
3424 | \ingroup model-view | - | ||||||||||||
3425 | - | |||||||||||||
3426 | QAbstractListModel provides a standard interface for models that represent | - | ||||||||||||
3427 | their data as a simple non-hierarchical sequence of items. It is not used | - | ||||||||||||
3428 | directly, but must be subclassed. | - | ||||||||||||
3429 | - | |||||||||||||
3430 | Since the model provides a more specialized interface than | - | ||||||||||||
3431 | QAbstractItemModel, it is not suitable for use with tree views; you will | - | ||||||||||||
3432 | need to subclass QAbstractItemModel if you want to provide a model for | - | ||||||||||||
3433 | that purpose. If you need to use a number of list models to manage data, | - | ||||||||||||
3434 | it may be more appropriate to subclass QAbstractTableModel instead. | - | ||||||||||||
3435 | - | |||||||||||||
3436 | Simple models can be created by subclassing this class and implementing | - | ||||||||||||
3437 | the minimum number of required functions. For example, we could implement | - | ||||||||||||
3438 | a simple read-only QStringList-based model that provides a list of strings | - | ||||||||||||
3439 | to a QListView widget. In such a case, we only need to implement the | - | ||||||||||||
3440 | rowCount() function to return the number of items in the list, and the | - | ||||||||||||
3441 | data() function to retrieve items from the list. | - | ||||||||||||
3442 | - | |||||||||||||
3443 | Since the model represents a one-dimensional structure, the rowCount() | - | ||||||||||||
3444 | function returns the total number of items in the model. The columnCount() | - | ||||||||||||
3445 | function is implemented for interoperability with all kinds of views, but | - | ||||||||||||
3446 | by default informs views that the model contains only one column. | - | ||||||||||||
3447 | - | |||||||||||||
3448 | \section1 Subclassing | - | ||||||||||||
3449 | - | |||||||||||||
3450 | When subclassing QAbstractListModel, you must provide implementations | - | ||||||||||||
3451 | of the rowCount() and data() functions. Well behaved models also provide | - | ||||||||||||
3452 | a headerData() implementation. | - | ||||||||||||
3453 | - | |||||||||||||
3454 | If your model is used within QML and requires roles other than the | - | ||||||||||||
3455 | default ones provided by the roleNames() function, you must override it. | - | ||||||||||||
3456 | - | |||||||||||||
3457 | For editable list models, you must also provide an implementation of | - | ||||||||||||
3458 | setData(), and implement the flags() function so that it returns a value | - | ||||||||||||
3459 | containing \l{Qt::ItemFlags}{Qt::ItemIsEditable}. | - | ||||||||||||
3460 | - | |||||||||||||
3461 | Note that QAbstractListModel provides a default implementation of | - | ||||||||||||
3462 | columnCount() that informs views that there is only a single column | - | ||||||||||||
3463 | of items in this model. | - | ||||||||||||
3464 | - | |||||||||||||
3465 | Models that provide interfaces to resizable list-like data structures | - | ||||||||||||
3466 | can provide implementations of insertRows() and removeRows(). When | - | ||||||||||||
3467 | implementing these functions, it is important to call the appropriate | - | ||||||||||||
3468 | functions so that all connected views are aware of any changes: | - | ||||||||||||
3469 | - | |||||||||||||
3470 | \list | - | ||||||||||||
3471 | \li An insertRows() implementation must call beginInsertRows() | - | ||||||||||||
3472 | \e before inserting new rows into the data structure, and it must | - | ||||||||||||
3473 | call endInsertRows() \e{immediately afterwards}. | - | ||||||||||||
3474 | \li A removeRows() implementation must call beginRemoveRows() | - | ||||||||||||
3475 | \e before the rows are removed from the data structure, and it must | - | ||||||||||||
3476 | call endRemoveRows() \e{immediately afterwards}. | - | ||||||||||||
3477 | \endlist | - | ||||||||||||
3478 | - | |||||||||||||
3479 | \note Some general guidelines for subclassing models are available in the | - | ||||||||||||
3480 | \l{Model Subclassing Reference}. | - | ||||||||||||
3481 | - | |||||||||||||
3482 | \sa {Model Classes}, {Model Subclassing Reference}, QAbstractItemView, | - | ||||||||||||
3483 | QAbstractTableModel, {Item Views Puzzle Example} | - | ||||||||||||
3484 | */ | - | ||||||||||||
3485 | - | |||||||||||||
3486 | /*! | - | ||||||||||||
3487 | Constructs an abstract list model with the given \a parent. | - | ||||||||||||
3488 | */ | - | ||||||||||||
3489 | - | |||||||||||||
3490 | QAbstractListModel::QAbstractListModel(QObject *parent) | - | ||||||||||||
3491 | : QAbstractItemModel(parent) | - | ||||||||||||
3492 | { | - | ||||||||||||
3493 | - | |||||||||||||
3494 | } | - | ||||||||||||
3495 | - | |||||||||||||
3496 | /*! | - | ||||||||||||
3497 | \internal | - | ||||||||||||
3498 | - | |||||||||||||
3499 | Constructs an abstract list model with \a dd and the given \a parent. | - | ||||||||||||
3500 | */ | - | ||||||||||||
3501 | - | |||||||||||||
3502 | QAbstractListModel::QAbstractListModel(QAbstractItemModelPrivate &dd, QObject *parent) | - | ||||||||||||
3503 | : QAbstractItemModel(dd, parent) | - | ||||||||||||
3504 | { | - | ||||||||||||
3505 | - | |||||||||||||
3506 | } | - | ||||||||||||
3507 | - | |||||||||||||
3508 | /*! | - | ||||||||||||
3509 | Destroys the abstract list model. | - | ||||||||||||
3510 | */ | - | ||||||||||||
3511 | - | |||||||||||||
3512 | QAbstractListModel::~QAbstractListModel() | - | ||||||||||||
3513 | { | - | ||||||||||||
3514 | - | |||||||||||||
3515 | } | - | ||||||||||||
3516 | - | |||||||||||||
3517 | /*! | - | ||||||||||||
3518 | \fn QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const | - | ||||||||||||
3519 | - | |||||||||||||
3520 | Returns the index of the data in \a row and \a column with \a parent. | - | ||||||||||||
3521 | - | |||||||||||||
3522 | \sa parent() | - | ||||||||||||
3523 | */ | - | ||||||||||||
3524 | - | |||||||||||||
3525 | QModelIndex QAbstractListModel::index(int row, int column, const QModelIndex &parent) const | - | ||||||||||||
3526 | { | - | ||||||||||||
3527 | return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex(); | - | ||||||||||||
3528 | } | - | ||||||||||||
3529 | - | |||||||||||||
3530 | /*! | - | ||||||||||||
3531 | Returns the parent of the model item with the given \a index. | - | ||||||||||||
3532 | - | |||||||||||||
3533 | \sa index(), hasChildren() | - | ||||||||||||
3534 | */ | - | ||||||||||||
3535 | - | |||||||||||||
3536 | QModelIndex QAbstractListModel::parent(const QModelIndex & /* index */) const | - | ||||||||||||
3537 | { | - | ||||||||||||
3538 | return QModelIndex(); | - | ||||||||||||
3539 | } | - | ||||||||||||
3540 | - | |||||||||||||
3541 | /*! | - | ||||||||||||
3542 | \reimp | - | ||||||||||||
3543 | */ | - | ||||||||||||
3544 | QModelIndex QAbstractListModel::sibling(int row, int column, const QModelIndex &) const | - | ||||||||||||
3545 | { | - | ||||||||||||
3546 | return index(row, column); | - | ||||||||||||
3547 | } | - | ||||||||||||
3548 | - | |||||||||||||
3549 | /*! | - | ||||||||||||
3550 | \reimp | - | ||||||||||||
3551 | */ | - | ||||||||||||
3552 | Qt::ItemFlags QAbstractListModel::flags(const QModelIndex &index) const | - | ||||||||||||
3553 | { | - | ||||||||||||
3554 | Qt::ItemFlags f = QAbstractItemModel::flags(index); | - | ||||||||||||
3555 | if (index.isValid()) | - | ||||||||||||
3556 | f |= Qt::ItemNeverHasChildren; | - | ||||||||||||
3557 | return f; | - | ||||||||||||
3558 | } | - | ||||||||||||
3559 | - | |||||||||||||
3560 | /*! | - | ||||||||||||
3561 | \internal | - | ||||||||||||
3562 | - | |||||||||||||
3563 | Returns the number of columns in the list with the given \a parent. | - | ||||||||||||
3564 | - | |||||||||||||
3565 | \sa rowCount() | - | ||||||||||||
3566 | */ | - | ||||||||||||
3567 | - | |||||||||||||
3568 | int QAbstractListModel::columnCount(const QModelIndex &parent) const | - | ||||||||||||
3569 | { | - | ||||||||||||
3570 | return parent.isValid() ? 0 : 1; | - | ||||||||||||
3571 | } | - | ||||||||||||
3572 | - | |||||||||||||
3573 | bool QAbstractListModel::hasChildren(const QModelIndex &parent) const | - | ||||||||||||
3574 | { | - | ||||||||||||
3575 | return parent.isValid() ? false : (rowCount() > 0); | - | ||||||||||||
3576 | } | - | ||||||||||||
3577 | - | |||||||||||||
3578 | /*! | - | ||||||||||||
3579 | \typedef QModelIndexList | - | ||||||||||||
3580 | \relates QModelIndex | - | ||||||||||||
3581 | - | |||||||||||||
3582 | Synonym for QList<QModelIndex>. | - | ||||||||||||
3583 | */ | - | ||||||||||||
3584 | - | |||||||||||||
3585 | /*! | - | ||||||||||||
3586 | \reimp | - | ||||||||||||
3587 | */ | - | ||||||||||||
3588 | bool QAbstractTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||
3589 | int row, int column, const QModelIndex &parent) | - | ||||||||||||
3590 | { | - | ||||||||||||
3591 | if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction)) | - | ||||||||||||
3592 | return false; | - | ||||||||||||
3593 | - | |||||||||||||
3594 | QStringList types = mimeTypes(); | - | ||||||||||||
3595 | if (types.isEmpty()) | - | ||||||||||||
3596 | return false; | - | ||||||||||||
3597 | QString format = types.at(0); | - | ||||||||||||
3598 | if (!data->hasFormat(format)) | - | ||||||||||||
3599 | return false; | - | ||||||||||||
3600 | - | |||||||||||||
3601 | QByteArray encoded = data->data(format); | - | ||||||||||||
3602 | QDataStream stream(&encoded, QIODevice::ReadOnly); | - | ||||||||||||
3603 | - | |||||||||||||
3604 | // if the drop is on an item, replace the data in the items | - | ||||||||||||
3605 | if (parent.isValid() && row == -1 && column == -1) { | - | ||||||||||||
3606 | int top = INT_MAX; | - | ||||||||||||
3607 | int left = INT_MAX; | - | ||||||||||||
3608 | QVector<int> rows, columns; | - | ||||||||||||
3609 | QVector<QMap<int, QVariant> > data; | - | ||||||||||||
3610 | - | |||||||||||||
3611 | while (!stream.atEnd()) { | - | ||||||||||||
3612 | int r, c; | - | ||||||||||||
3613 | QMap<int, QVariant> v; | - | ||||||||||||
3614 | stream >> r >> c >> v; | - | ||||||||||||
3615 | rows.append(r); | - | ||||||||||||
3616 | columns.append(c); | - | ||||||||||||
3617 | data.append(v); | - | ||||||||||||
3618 | top = qMin(r, top); | - | ||||||||||||
3619 | left = qMin(c, left); | - | ||||||||||||
3620 | } | - | ||||||||||||
3621 | - | |||||||||||||
3622 | for (int i = 0; i < data.size(); ++i) { | - | ||||||||||||
3623 | int r = (rows.at(i) - top) + parent.row(); | - | ||||||||||||
3624 | int c = (columns.at(i) - left) + parent.column(); | - | ||||||||||||
3625 | if (hasIndex(r, c)) | - | ||||||||||||
3626 | setItemData(index(r, c), data.at(i)); | - | ||||||||||||
3627 | } | - | ||||||||||||
3628 | - | |||||||||||||
3629 | return true; | - | ||||||||||||
3630 | } | - | ||||||||||||
3631 | - | |||||||||||||
3632 | // otherwise insert new rows for the data | - | ||||||||||||
3633 | return decodeData(row, column, parent, stream); | - | ||||||||||||
3634 | } | - | ||||||||||||
3635 | - | |||||||||||||
3636 | /*! | - | ||||||||||||
3637 | \reimp | - | ||||||||||||
3638 | */ | - | ||||||||||||
3639 | bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||
3640 | int row, int column, const QModelIndex &parent) | - | ||||||||||||
3641 | { | - | ||||||||||||
3642 | if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction)) | - | ||||||||||||
3643 | return false; | - | ||||||||||||
3644 | - | |||||||||||||
3645 | QStringList types = mimeTypes(); | - | ||||||||||||
3646 | if (types.isEmpty()) | - | ||||||||||||
3647 | return false; | - | ||||||||||||
3648 | QString format = types.at(0); | - | ||||||||||||
3649 | if (!data->hasFormat(format)) | - | ||||||||||||
3650 | return false; | - | ||||||||||||
3651 | - | |||||||||||||
3652 | QByteArray encoded = data->data(format); | - | ||||||||||||
3653 | QDataStream stream(&encoded, QIODevice::ReadOnly); | - | ||||||||||||
3654 | - | |||||||||||||
3655 | // if the drop is on an item, replace the data in the items | - | ||||||||||||
3656 | if (parent.isValid() && row == -1 && column == -1) { | - | ||||||||||||
3657 | int top = INT_MAX; | - | ||||||||||||
3658 | int left = INT_MAX; | - | ||||||||||||
3659 | QVector<int> rows, columns; | - | ||||||||||||
3660 | QVector<QMap<int, QVariant> > data; | - | ||||||||||||
3661 | - | |||||||||||||
3662 | while (!stream.atEnd()) { | - | ||||||||||||
3663 | int r, c; | - | ||||||||||||
3664 | QMap<int, QVariant> v; | - | ||||||||||||
3665 | stream >> r >> c >> v; | - | ||||||||||||
3666 | rows.append(r); | - | ||||||||||||
3667 | columns.append(c); | - | ||||||||||||
3668 | data.append(v); | - | ||||||||||||
3669 | top = qMin(r, top); | - | ||||||||||||
3670 | left = qMin(c, left); | - | ||||||||||||
3671 | } | - | ||||||||||||
3672 | - | |||||||||||||
3673 | for (int i = 0; i < data.size(); ++i) { | - | ||||||||||||
3674 | int r = (rows.at(i) - top) + parent.row(); | - | ||||||||||||
3675 | if (columns.at(i) == left && hasIndex(r, 0)) | - | ||||||||||||
3676 | setItemData(index(r), data.at(i)); | - | ||||||||||||
3677 | } | - | ||||||||||||
3678 | - | |||||||||||||
3679 | return true; | - | ||||||||||||
3680 | } | - | ||||||||||||
3681 | - | |||||||||||||
3682 | if (row == -1) | - | ||||||||||||
3683 | row = rowCount(parent); | - | ||||||||||||
3684 | - | |||||||||||||
3685 | // otherwise insert new rows for the data | - | ||||||||||||
3686 | return decodeData(row, column, parent, stream); | - | ||||||||||||
3687 | } | - | ||||||||||||
3688 | - | |||||||||||||
3689 | /*! | - | ||||||||||||
3690 | \fn QAbstractItemModel::modelAboutToBeReset() | - | ||||||||||||
3691 | \since 4.2 | - | ||||||||||||
3692 | - | |||||||||||||
3693 | This signal is emitted when beginResetModel() is called, before the model's internal | - | ||||||||||||
3694 | state (e.g. persistent model indexes) has been invalidated. | - | ||||||||||||
3695 | - | |||||||||||||
3696 | \sa beginResetModel(), modelReset() | - | ||||||||||||
3697 | */ | - | ||||||||||||
3698 | - | |||||||||||||
3699 | /*! | - | ||||||||||||
3700 | \fn QAbstractItemModel::modelReset() | - | ||||||||||||
3701 | \since 4.1 | - | ||||||||||||
3702 | - | |||||||||||||
3703 | This signal is emitted when endResetModel() is called, after the | - | ||||||||||||
3704 | model's internal state (e.g. persistent model indexes) has been invalidated. | - | ||||||||||||
3705 | - | |||||||||||||
3706 | Note that if a model is reset it should be considered that all information | - | ||||||||||||
3707 | previously retrieved from it is invalid. This includes but is not limited | - | ||||||||||||
3708 | to the rowCount() and columnCount(), flags(), data retrieved through data(), | - | ||||||||||||
3709 | and roleNames(). | - | ||||||||||||
3710 | - | |||||||||||||
3711 | \sa endResetModel(), modelAboutToBeReset() | - | ||||||||||||
3712 | */ | - | ||||||||||||
3713 | - | |||||||||||||
3714 | /*! | - | ||||||||||||
3715 | \fn bool QModelIndex::operator<(const QModelIndex &other) const | - | ||||||||||||
3716 | \since 4.1 | - | ||||||||||||
3717 | - | |||||||||||||
3718 | Returns \c{true} if this model index is smaller than the \a other | - | ||||||||||||
3719 | model index; otherwise returns \c{false}. | - | ||||||||||||
3720 | - | |||||||||||||
3721 | The less than calculation is not directly useful to developers - the way that indexes | - | ||||||||||||
3722 | with different parents compare is not defined. This operator only exists so that the | - | ||||||||||||
3723 | class can be used with QMap. | - | ||||||||||||
3724 | */ | - | ||||||||||||
3725 | - | |||||||||||||
3726 | /*! | - | ||||||||||||
3727 | \fn uint qHash(const QPersistentModelIndex &index, uint seed = 0) | - | ||||||||||||
3728 | \since 5.0 | - | ||||||||||||
3729 | \relates QPersistentModelIndex | - | ||||||||||||
3730 | - | |||||||||||||
3731 | Returns a hash of the QPersistentModelIndex \a index, using \a seed to | - | ||||||||||||
3732 | seed the calculation. | - | ||||||||||||
3733 | */ | - | ||||||||||||
3734 | - | |||||||||||||
3735 | - | |||||||||||||
3736 | /*! | - | ||||||||||||
3737 | \internal | - | ||||||||||||
3738 | QHash::insertMulti insert the value before the old value. and find() return the new value. | - | ||||||||||||
3739 | We need insertMultiAtEnd because we don't want to overwrite the old one, which should be removed later | - | ||||||||||||
3740 | - | |||||||||||||
3741 | There should be only one instance QPersistentModelIndexData per index, but in some intermediate state there may be | - | ||||||||||||
3742 | severals of PersistantModelIndex pointing to the same index, but one is already updated, and the other one is not. | - | ||||||||||||
3743 | This make sure than when updating the first one we don't overwrite the second one in the hash, and the second one | - | ||||||||||||
3744 | will be updated right later. | - | ||||||||||||
3745 | */ | - | ||||||||||||
3746 | void QAbstractItemModelPrivate::Persistent::insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data) | - | ||||||||||||
3747 | { | - | ||||||||||||
3748 | QHash<QModelIndex,QPersistentModelIndexData *>::iterator newIt = | - | ||||||||||||
3749 | indexes.insertMulti(key, data); | - | ||||||||||||
3750 | QHash<QModelIndex,QPersistentModelIndexData *>::iterator it = newIt + 1; | - | ||||||||||||
3751 | while (it != indexes.end() && it.key() == key) { | - | ||||||||||||
3752 | qSwap(*newIt,*it); | - | ||||||||||||
3753 | newIt = it; | - | ||||||||||||
3754 | ++it; | - | ||||||||||||
3755 | } | - | ||||||||||||
3756 | } | - | ||||||||||||
3757 | - | |||||||||||||
3758 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |