1 | // Copyright (C) 2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com> |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qidentityproxymodel.h" |
5 | #include "qidentityproxymodel_p.h" |
6 | #include "qitemselectionmodel.h" |
7 | #include <private/qabstractproxymodel_p.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \since 4.8 |
13 | \class QIdentityProxyModel |
14 | \inmodule QtCore |
15 | \brief The QIdentityProxyModel class proxies its source model unmodified. |
16 | |
17 | \ingroup model-view |
18 | |
19 | QIdentityProxyModel can be used to forward the structure of a source model exactly, with no sorting, filtering or other transformation. |
20 | This is similar in concept to an identity matrix where A.I = A. |
21 | |
22 | Because it does no sorting or filtering, this class is most suitable to proxy models which transform the data() of the source model. |
23 | For example, a proxy model could be created to define the font used, or the background colour, or the tooltip etc. This removes the |
24 | need to implement all data handling in the same class that creates the structure of the model, and can also be used to create |
25 | re-usable components. |
26 | |
27 | This also provides a way to change the data in the case where a source model is supplied by a third party which cannot be modified. |
28 | |
29 | \snippet code/src_gui_itemviews_qidentityproxymodel.cpp 0 |
30 | |
31 | \sa QAbstractProxyModel, {Model/View Programming}, QAbstractItemModel |
32 | |
33 | */ |
34 | |
35 | /*! |
36 | Constructs an identity model with the given \a parent. |
37 | */ |
38 | QIdentityProxyModel::QIdentityProxyModel(QObject* parent) |
39 | : QAbstractProxyModel(*new QIdentityProxyModelPrivate, parent) |
40 | { |
41 | |
42 | } |
43 | |
44 | /*! |
45 | \internal |
46 | */ |
47 | QIdentityProxyModel::QIdentityProxyModel(QIdentityProxyModelPrivate &dd, QObject* parent) |
48 | : QAbstractProxyModel(dd, parent) |
49 | { |
50 | |
51 | } |
52 | |
53 | /*! |
54 | Destroys this identity model. |
55 | */ |
56 | QIdentityProxyModel::~QIdentityProxyModel() |
57 | { |
58 | } |
59 | |
60 | /*! |
61 | \reimp |
62 | */ |
63 | int QIdentityProxyModel::columnCount(const QModelIndex& parent) const |
64 | { |
65 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
66 | Q_D(const QIdentityProxyModel); |
67 | return d->model->columnCount(parent: mapToSource(proxyIndex: parent)); |
68 | } |
69 | |
70 | /*! |
71 | \reimp |
72 | */ |
73 | bool QIdentityProxyModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) |
74 | { |
75 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
76 | Q_D(QIdentityProxyModel); |
77 | return d->model->dropMimeData(data, action, row, column, parent: mapToSource(proxyIndex: parent)); |
78 | } |
79 | |
80 | /*! |
81 | \reimp |
82 | */ |
83 | QModelIndex QIdentityProxyModel::index(int row, int column, const QModelIndex& parent) const |
84 | { |
85 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
86 | Q_D(const QIdentityProxyModel); |
87 | const QModelIndex sourceParent = mapToSource(proxyIndex: parent); |
88 | const QModelIndex sourceIndex = d->model->index(row, column, parent: sourceParent); |
89 | return mapFromSource(sourceIndex); |
90 | } |
91 | |
92 | /*! |
93 | \reimp |
94 | */ |
95 | QModelIndex QIdentityProxyModel::sibling(int row, int column, const QModelIndex &idx) const |
96 | { |
97 | Q_D(const QIdentityProxyModel); |
98 | return mapFromSource(sourceIndex: d->model->sibling(row, column, idx: mapToSource(proxyIndex: idx))); |
99 | } |
100 | |
101 | /*! |
102 | \reimp |
103 | */ |
104 | bool QIdentityProxyModel::insertColumns(int column, int count, const QModelIndex& parent) |
105 | { |
106 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
107 | Q_D(QIdentityProxyModel); |
108 | return d->model->insertColumns(column, count, parent: mapToSource(proxyIndex: parent)); |
109 | } |
110 | |
111 | /*! |
112 | \reimp |
113 | */ |
114 | bool QIdentityProxyModel::insertRows(int row, int count, const QModelIndex& parent) |
115 | { |
116 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
117 | Q_D(QIdentityProxyModel); |
118 | return d->model->insertRows(row, count, parent: mapToSource(proxyIndex: parent)); |
119 | } |
120 | |
121 | /*! |
122 | \reimp |
123 | */ |
124 | QModelIndex QIdentityProxyModel::mapFromSource(const QModelIndex& sourceIndex) const |
125 | { |
126 | Q_D(const QIdentityProxyModel); |
127 | if (!d->model || !sourceIndex.isValid()) |
128 | return QModelIndex(); |
129 | |
130 | Q_ASSERT(sourceIndex.model() == d->model); |
131 | return createIndex(arow: sourceIndex.row(), acolumn: sourceIndex.column(), adata: sourceIndex.internalPointer()); |
132 | } |
133 | |
134 | /*! |
135 | \reimp |
136 | */ |
137 | QItemSelection QIdentityProxyModel::mapSelectionFromSource(const QItemSelection& selection) const |
138 | { |
139 | Q_D(const QIdentityProxyModel); |
140 | QItemSelection proxySelection; |
141 | |
142 | if (!d->model) |
143 | return proxySelection; |
144 | |
145 | QItemSelection::const_iterator it = selection.constBegin(); |
146 | const QItemSelection::const_iterator end = selection.constEnd(); |
147 | proxySelection.reserve(size: selection.size()); |
148 | for ( ; it != end; ++it) { |
149 | Q_ASSERT(it->model() == d->model); |
150 | const QItemSelectionRange range(mapFromSource(sourceIndex: it->topLeft()), mapFromSource(sourceIndex: it->bottomRight())); |
151 | proxySelection.append(t: range); |
152 | } |
153 | |
154 | return proxySelection; |
155 | } |
156 | |
157 | /*! |
158 | \reimp |
159 | */ |
160 | QItemSelection QIdentityProxyModel::mapSelectionToSource(const QItemSelection& selection) const |
161 | { |
162 | Q_D(const QIdentityProxyModel); |
163 | QItemSelection sourceSelection; |
164 | |
165 | if (!d->model) |
166 | return sourceSelection; |
167 | |
168 | QItemSelection::const_iterator it = selection.constBegin(); |
169 | const QItemSelection::const_iterator end = selection.constEnd(); |
170 | sourceSelection.reserve(size: selection.size()); |
171 | for ( ; it != end; ++it) { |
172 | Q_ASSERT(it->model() == this); |
173 | const QItemSelectionRange range(mapToSource(proxyIndex: it->topLeft()), mapToSource(proxyIndex: it->bottomRight())); |
174 | sourceSelection.append(t: range); |
175 | } |
176 | |
177 | return sourceSelection; |
178 | } |
179 | |
180 | /*! |
181 | \reimp |
182 | */ |
183 | QModelIndex QIdentityProxyModel::mapToSource(const QModelIndex& proxyIndex) const |
184 | { |
185 | Q_D(const QIdentityProxyModel); |
186 | if (!d->model || !proxyIndex.isValid()) |
187 | return QModelIndex(); |
188 | Q_ASSERT(proxyIndex.model() == this); |
189 | return createSourceIndex(row: proxyIndex.row(), col: proxyIndex.column(), internalPtr: proxyIndex.internalPointer()); |
190 | } |
191 | |
192 | /*! |
193 | \reimp |
194 | */ |
195 | QModelIndexList QIdentityProxyModel::match(const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags) const |
196 | { |
197 | Q_D(const QIdentityProxyModel); |
198 | Q_ASSERT(start.isValid() ? start.model() == this : true); |
199 | if (!d->model) |
200 | return QModelIndexList(); |
201 | |
202 | const QModelIndexList sourceList = d->model->match(start: mapToSource(proxyIndex: start), role, value, hits, flags); |
203 | QModelIndexList::const_iterator it = sourceList.constBegin(); |
204 | const QModelIndexList::const_iterator end = sourceList.constEnd(); |
205 | QModelIndexList proxyList; |
206 | proxyList.reserve(size: sourceList.size()); |
207 | for ( ; it != end; ++it) |
208 | proxyList.append(t: mapFromSource(sourceIndex: *it)); |
209 | return proxyList; |
210 | } |
211 | |
212 | /*! |
213 | \reimp |
214 | */ |
215 | QModelIndex QIdentityProxyModel::parent(const QModelIndex& child) const |
216 | { |
217 | Q_ASSERT(child.isValid() ? child.model() == this : true); |
218 | const QModelIndex sourceIndex = mapToSource(proxyIndex: child); |
219 | const QModelIndex sourceParent = sourceIndex.parent(); |
220 | return mapFromSource(sourceIndex: sourceParent); |
221 | } |
222 | |
223 | /*! |
224 | \reimp |
225 | */ |
226 | bool QIdentityProxyModel::removeColumns(int column, int count, const QModelIndex& parent) |
227 | { |
228 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
229 | Q_D(QIdentityProxyModel); |
230 | return d->model->removeColumns(column, count, parent: mapToSource(proxyIndex: parent)); |
231 | } |
232 | |
233 | /*! |
234 | \reimp |
235 | */ |
236 | bool QIdentityProxyModel::removeRows(int row, int count, const QModelIndex& parent) |
237 | { |
238 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
239 | Q_D(QIdentityProxyModel); |
240 | return d->model->removeRows(row, count, parent: mapToSource(proxyIndex: parent)); |
241 | } |
242 | |
243 | /*! |
244 | \reimp |
245 | \since 5.15 |
246 | */ |
247 | bool QIdentityProxyModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) |
248 | { |
249 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true); |
250 | Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true); |
251 | Q_D(QIdentityProxyModel); |
252 | return d->model->moveRows(sourceParent: mapToSource(proxyIndex: sourceParent), sourceRow, count, destinationParent: mapToSource(proxyIndex: destinationParent), destinationChild); |
253 | } |
254 | |
255 | /*! |
256 | \reimp |
257 | \since 5.15 |
258 | */ |
259 | bool QIdentityProxyModel::moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) |
260 | { |
261 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true); |
262 | Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true); |
263 | Q_D(QIdentityProxyModel); |
264 | return d->model->moveColumns(sourceParent: mapToSource(proxyIndex: sourceParent), sourceColumn, count, destinationParent: mapToSource(proxyIndex: destinationParent), destinationChild); |
265 | } |
266 | |
267 | /*! |
268 | \reimp |
269 | */ |
270 | int QIdentityProxyModel::rowCount(const QModelIndex& parent) const |
271 | { |
272 | Q_ASSERT(parent.isValid() ? parent.model() == this : true); |
273 | Q_D(const QIdentityProxyModel); |
274 | return d->model->rowCount(parent: mapToSource(proxyIndex: parent)); |
275 | } |
276 | |
277 | /*! |
278 | \reimp |
279 | */ |
280 | QVariant QIdentityProxyModel::(int section, Qt::Orientation orientation, int role) const |
281 | { |
282 | Q_D(const QIdentityProxyModel); |
283 | return d->model->headerData(section, orientation, role); |
284 | } |
285 | |
286 | /*! |
287 | \reimp |
288 | */ |
289 | void QIdentityProxyModel::setSourceModel(QAbstractItemModel* newSourceModel) |
290 | { |
291 | beginResetModel(); |
292 | |
293 | if (sourceModel()) { |
294 | disconnect(sender: sourceModel(), SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), |
295 | receiver: this, SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int))); |
296 | disconnect(sender: sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), |
297 | receiver: this, SLOT(_q_sourceRowsInserted(QModelIndex,int,int))); |
298 | disconnect(sender: sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), |
299 | receiver: this, SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int))); |
300 | disconnect(sender: sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)), |
301 | receiver: this, SLOT(_q_sourceRowsRemoved(QModelIndex,int,int))); |
302 | disconnect(sender: sourceModel(), SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), |
303 | receiver: this, SLOT(_q_sourceRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); |
304 | disconnect(sender: sourceModel(), SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), |
305 | receiver: this, SLOT(_q_sourceRowsMoved(QModelIndex,int,int,QModelIndex,int))); |
306 | disconnect(sender: sourceModel(), SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), |
307 | receiver: this, SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int))); |
308 | disconnect(sender: sourceModel(), SIGNAL(columnsInserted(QModelIndex,int,int)), |
309 | receiver: this, SLOT(_q_sourceColumnsInserted(QModelIndex,int,int))); |
310 | disconnect(sender: sourceModel(), SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), |
311 | receiver: this, SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int))); |
312 | disconnect(sender: sourceModel(), SIGNAL(columnsRemoved(QModelIndex,int,int)), |
313 | receiver: this, SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int))); |
314 | disconnect(sender: sourceModel(), SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), |
315 | receiver: this, SLOT(_q_sourceColumnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); |
316 | disconnect(sender: sourceModel(), SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)), |
317 | receiver: this, SLOT(_q_sourceColumnsMoved(QModelIndex,int,int,QModelIndex,int))); |
318 | disconnect(sender: sourceModel(), SIGNAL(modelAboutToBeReset()), |
319 | receiver: this, SLOT(_q_sourceModelAboutToBeReset())); |
320 | disconnect(sender: sourceModel(), SIGNAL(modelReset()), |
321 | receiver: this, SLOT(_q_sourceModelReset())); |
322 | disconnect(sender: sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex,QList<int>)), |
323 | receiver: this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QList<int>))); |
324 | disconnect(sender: sourceModel(), SIGNAL(headerDataChanged(Qt::Orientation,int,int)), |
325 | receiver: this, SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); |
326 | disconnect(sender: sourceModel(), SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)), |
327 | receiver: this, SLOT(_q_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint))); |
328 | disconnect(sender: sourceModel(), SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)), |
329 | receiver: this, SLOT(_q_sourceLayoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint))); |
330 | } |
331 | |
332 | QAbstractProxyModel::setSourceModel(newSourceModel); |
333 | |
334 | if (sourceModel()) { |
335 | connect(asender: sourceModel(), SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), |
336 | SLOT(_q_sourceRowsAboutToBeInserted(QModelIndex,int,int))); |
337 | connect(asender: sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), |
338 | SLOT(_q_sourceRowsInserted(QModelIndex,int,int))); |
339 | connect(asender: sourceModel(), SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), |
340 | SLOT(_q_sourceRowsAboutToBeRemoved(QModelIndex,int,int))); |
341 | connect(asender: sourceModel(), SIGNAL(rowsRemoved(QModelIndex,int,int)), |
342 | SLOT(_q_sourceRowsRemoved(QModelIndex,int,int))); |
343 | connect(asender: sourceModel(), SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), |
344 | SLOT(_q_sourceRowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); |
345 | connect(asender: sourceModel(), SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), |
346 | SLOT(_q_sourceRowsMoved(QModelIndex,int,int,QModelIndex,int))); |
347 | connect(asender: sourceModel(), SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)), |
348 | SLOT(_q_sourceColumnsAboutToBeInserted(QModelIndex,int,int))); |
349 | connect(asender: sourceModel(), SIGNAL(columnsInserted(QModelIndex,int,int)), |
350 | SLOT(_q_sourceColumnsInserted(QModelIndex,int,int))); |
351 | connect(asender: sourceModel(), SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), |
352 | SLOT(_q_sourceColumnsAboutToBeRemoved(QModelIndex,int,int))); |
353 | connect(asender: sourceModel(), SIGNAL(columnsRemoved(QModelIndex,int,int)), |
354 | SLOT(_q_sourceColumnsRemoved(QModelIndex,int,int))); |
355 | connect(asender: sourceModel(), SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)), |
356 | SLOT(_q_sourceColumnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int))); |
357 | connect(asender: sourceModel(), SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)), |
358 | SLOT(_q_sourceColumnsMoved(QModelIndex,int,int,QModelIndex,int))); |
359 | connect(asender: sourceModel(), SIGNAL(modelAboutToBeReset()), |
360 | SLOT(_q_sourceModelAboutToBeReset())); |
361 | connect(asender: sourceModel(), SIGNAL(modelReset()), |
362 | SLOT(_q_sourceModelReset())); |
363 | connect(asender: sourceModel(), SIGNAL(dataChanged(QModelIndex,QModelIndex,QList<int>)), |
364 | SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex,QList<int>))); |
365 | connect(asender: sourceModel(), SIGNAL(headerDataChanged(Qt::Orientation,int,int)), |
366 | SLOT(_q_sourceHeaderDataChanged(Qt::Orientation,int,int))); |
367 | connect(asender: sourceModel(), SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)), |
368 | SLOT(_q_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint))); |
369 | connect(asender: sourceModel(), SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)), |
370 | SLOT(_q_sourceLayoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint))); |
371 | } |
372 | |
373 | endResetModel(); |
374 | } |
375 | |
376 | void QIdentityProxyModelPrivate::_q_sourceColumnsAboutToBeInserted(const QModelIndex &parent, int start, int end) |
377 | { |
378 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
379 | Q_Q(QIdentityProxyModel); |
380 | q->beginInsertColumns(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
381 | } |
382 | |
383 | void QIdentityProxyModelPrivate::_q_sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest) |
384 | { |
385 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
386 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
387 | Q_Q(QIdentityProxyModel); |
388 | q->beginMoveColumns(sourceParent: q->mapFromSource(sourceIndex: sourceParent), sourceFirst: sourceStart, sourceLast: sourceEnd, destinationParent: q->mapFromSource(sourceIndex: destParent), destinationColumn: dest); |
389 | } |
390 | |
391 | void QIdentityProxyModelPrivate::_q_sourceColumnsAboutToBeRemoved(const QModelIndex &parent, int start, int end) |
392 | { |
393 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
394 | Q_Q(QIdentityProxyModel); |
395 | q->beginRemoveColumns(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
396 | } |
397 | |
398 | void QIdentityProxyModelPrivate::_q_sourceColumnsInserted(const QModelIndex &parent, int start, int end) |
399 | { |
400 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
401 | Q_Q(QIdentityProxyModel); |
402 | Q_UNUSED(parent); |
403 | Q_UNUSED(start); |
404 | Q_UNUSED(end); |
405 | q->endInsertColumns(); |
406 | } |
407 | |
408 | void QIdentityProxyModelPrivate::_q_sourceColumnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest) |
409 | { |
410 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
411 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
412 | Q_Q(QIdentityProxyModel); |
413 | Q_UNUSED(sourceParent); |
414 | Q_UNUSED(sourceStart); |
415 | Q_UNUSED(sourceEnd); |
416 | Q_UNUSED(destParent); |
417 | Q_UNUSED(dest); |
418 | q->endMoveColumns(); |
419 | } |
420 | |
421 | void QIdentityProxyModelPrivate::_q_sourceColumnsRemoved(const QModelIndex &parent, int start, int end) |
422 | { |
423 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
424 | Q_Q(QIdentityProxyModel); |
425 | Q_UNUSED(parent); |
426 | Q_UNUSED(start); |
427 | Q_UNUSED(end); |
428 | q->endRemoveColumns(); |
429 | } |
430 | |
431 | void QIdentityProxyModelPrivate::_q_sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles) |
432 | { |
433 | Q_ASSERT(topLeft.isValid() ? topLeft.model() == model : true); |
434 | Q_ASSERT(bottomRight.isValid() ? bottomRight.model() == model : true); |
435 | Q_Q(QIdentityProxyModel); |
436 | emit q->dataChanged(topLeft: q->mapFromSource(sourceIndex: topLeft), bottomRight: q->mapFromSource(sourceIndex: bottomRight), roles); |
437 | } |
438 | |
439 | void QIdentityProxyModelPrivate::(Qt::Orientation orientation, int first, int last) |
440 | { |
441 | Q_Q(QIdentityProxyModel); |
442 | emit q->headerDataChanged(orientation, first, last); |
443 | } |
444 | |
445 | void QIdentityProxyModelPrivate::_q_sourceLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint) |
446 | { |
447 | Q_Q(QIdentityProxyModel); |
448 | |
449 | QList<QPersistentModelIndex> parents; |
450 | parents.reserve(size: sourceParents.size()); |
451 | for (const QPersistentModelIndex &parent : sourceParents) { |
452 | if (!parent.isValid()) { |
453 | parents << QPersistentModelIndex(); |
454 | continue; |
455 | } |
456 | const QModelIndex mappedParent = q->mapFromSource(sourceIndex: parent); |
457 | Q_ASSERT(mappedParent.isValid()); |
458 | parents << mappedParent; |
459 | } |
460 | |
461 | emit q->layoutAboutToBeChanged(parents, hint); |
462 | |
463 | const auto proxyPersistentIndexes = q->persistentIndexList(); |
464 | for (const QModelIndex &proxyPersistentIndex : proxyPersistentIndexes) { |
465 | proxyIndexes << proxyPersistentIndex; |
466 | Q_ASSERT(proxyPersistentIndex.isValid()); |
467 | const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyIndex: proxyPersistentIndex); |
468 | Q_ASSERT(srcPersistentIndex.isValid()); |
469 | layoutChangePersistentIndexes << srcPersistentIndex; |
470 | } |
471 | } |
472 | |
473 | void QIdentityProxyModelPrivate::_q_sourceLayoutChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint) |
474 | { |
475 | Q_Q(QIdentityProxyModel); |
476 | |
477 | for (int i = 0; i < proxyIndexes.size(); ++i) { |
478 | q->changePersistentIndex(from: proxyIndexes.at(i), to: q->mapFromSource(sourceIndex: layoutChangePersistentIndexes.at(i))); |
479 | } |
480 | |
481 | layoutChangePersistentIndexes.clear(); |
482 | proxyIndexes.clear(); |
483 | |
484 | QList<QPersistentModelIndex> parents; |
485 | parents.reserve(size: sourceParents.size()); |
486 | for (const QPersistentModelIndex &parent : sourceParents) { |
487 | if (!parent.isValid()) { |
488 | parents << QPersistentModelIndex(); |
489 | continue; |
490 | } |
491 | const QModelIndex mappedParent = q->mapFromSource(sourceIndex: parent); |
492 | Q_ASSERT(mappedParent.isValid()); |
493 | parents << mappedParent; |
494 | } |
495 | |
496 | emit q->layoutChanged(parents, hint); |
497 | } |
498 | |
499 | void QIdentityProxyModelPrivate::_q_sourceModelAboutToBeReset() |
500 | { |
501 | Q_Q(QIdentityProxyModel); |
502 | q->beginResetModel(); |
503 | } |
504 | |
505 | void QIdentityProxyModelPrivate::_q_sourceModelReset() |
506 | { |
507 | Q_Q(QIdentityProxyModel); |
508 | q->endResetModel(); |
509 | } |
510 | |
511 | void QIdentityProxyModelPrivate::_q_sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, int end) |
512 | { |
513 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
514 | Q_Q(QIdentityProxyModel); |
515 | q->beginInsertRows(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
516 | } |
517 | |
518 | void QIdentityProxyModelPrivate::_q_sourceRowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest) |
519 | { |
520 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
521 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
522 | Q_Q(QIdentityProxyModel); |
523 | q->beginMoveRows(sourceParent: q->mapFromSource(sourceIndex: sourceParent), sourceFirst: sourceStart, sourceLast: sourceEnd, destinationParent: q->mapFromSource(sourceIndex: destParent), destinationRow: dest); |
524 | } |
525 | |
526 | void QIdentityProxyModelPrivate::_q_sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) |
527 | { |
528 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
529 | Q_Q(QIdentityProxyModel); |
530 | q->beginRemoveRows(parent: q->mapFromSource(sourceIndex: parent), first: start, last: end); |
531 | } |
532 | |
533 | void QIdentityProxyModelPrivate::_q_sourceRowsInserted(const QModelIndex &parent, int start, int end) |
534 | { |
535 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
536 | Q_Q(QIdentityProxyModel); |
537 | Q_UNUSED(parent); |
538 | Q_UNUSED(start); |
539 | Q_UNUSED(end); |
540 | q->endInsertRows(); |
541 | } |
542 | |
543 | void QIdentityProxyModelPrivate::_q_sourceRowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest) |
544 | { |
545 | Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true); |
546 | Q_ASSERT(destParent.isValid() ? destParent.model() == model : true); |
547 | Q_Q(QIdentityProxyModel); |
548 | Q_UNUSED(sourceParent); |
549 | Q_UNUSED(sourceStart); |
550 | Q_UNUSED(sourceEnd); |
551 | Q_UNUSED(destParent); |
552 | Q_UNUSED(dest); |
553 | q->endMoveRows(); |
554 | } |
555 | |
556 | void QIdentityProxyModelPrivate::_q_sourceRowsRemoved(const QModelIndex &parent, int start, int end) |
557 | { |
558 | Q_ASSERT(parent.isValid() ? parent.model() == model : true); |
559 | Q_Q(QIdentityProxyModel); |
560 | Q_UNUSED(parent); |
561 | Q_UNUSED(start); |
562 | Q_UNUSED(end); |
563 | q->endRemoveRows(); |
564 | } |
565 | |
566 | QT_END_NAMESPACE |
567 | |
568 | #include "moc_qidentityproxymodel.cpp" |
569 | |