Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qidentityproxymodel.cpp
Go to the documentation of this file.
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
7#include <private/qabstractproxymodel_p.h>
8
10
43
52
59
64{
65 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
66 Q_D(const QIdentityProxyModel);
67 return d->model->columnCount(mapToSource(parent));
68}
69
74{
75 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
77 return d->model->dropMimeData(data, action, row, column, mapToSource(parent));
78}
79
84{
85 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
86 Q_D(const QIdentityProxyModel);
87 const QModelIndex sourceParent = mapToSource(parent);
88 const QModelIndex sourceIndex = d->model->index(row, column, sourceParent);
89 return mapFromSource(sourceIndex);
90}
91
96{
97 Q_D(const QIdentityProxyModel);
98 return mapFromSource(d->model->sibling(row, column, mapToSource(idx)));
99}
100
105{
106 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
108 return d->model->insertColumns(column, count, mapToSource(parent));
109}
110
115{
116 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
118 return d->model->insertRows(row, count, mapToSource(parent));
119}
120
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(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
132}
133
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(selection.size());
148 for ( ; it != end; ++it) {
149 Q_ASSERT(it->model() == d->model);
150 const QItemSelectionRange range(mapFromSource(it->topLeft()), mapFromSource(it->bottomRight()));
151 proxySelection.append(range);
152 }
153
154 return proxySelection;
155}
156
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(selection.size());
171 for ( ; it != end; ++it) {
172 Q_ASSERT(it->model() == this);
173 const QItemSelectionRange range(mapToSource(it->topLeft()), mapToSource(it->bottomRight()));
174 sourceSelection.append(range);
175 }
176
177 return sourceSelection;
178}
179
184{
185 Q_D(const QIdentityProxyModel);
186 if (!d->model || !proxyIndex.isValid())
187 return QModelIndex();
188 Q_ASSERT(proxyIndex.model() == this);
189 return createSourceIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer());
190}
191
195QModelIndexList 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(mapToSource(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(sourceList.size());
207 for ( ; it != end; ++it)
208 proxyList.append(mapFromSource(*it));
209 return proxyList;
210}
211
216{
217 Q_ASSERT(child.isValid() ? child.model() == this : true);
218 const QModelIndex sourceIndex = mapToSource(child);
219 const QModelIndex sourceParent = sourceIndex.parent();
220 return mapFromSource(sourceParent);
221}
222
227{
228 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
230 return d->model->removeColumns(column, count, mapToSource(parent));
231}
232
237{
238 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
240 return d->model->removeRows(row, count, mapToSource(parent));
241}
242
247bool QIdentityProxyModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
248{
249 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true);
252 return d->model->moveRows(mapToSource(sourceParent), sourceRow, count, mapToSource(destinationParent), destinationChild);
253}
254
259bool QIdentityProxyModel::moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild)
260{
261 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true);
264 return d->model->moveColumns(mapToSource(sourceParent), sourceColumn, count, mapToSource(destinationParent), destinationChild);
265}
266
271{
272 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
273 Q_D(const QIdentityProxyModel);
274 return d->model->rowCount(mapToSource(parent));
275}
276
280QVariant QIdentityProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
281{
282 Q_D(const QIdentityProxyModel);
283 return d->model->headerData(section, orientation, role);
284}
285
290{
292
294
295 // Call QObject::disconnect() unconditionally, if there is an existing source
296 // model, it's disconnected, and if there isn't, then calling disconnect() on
297 // a default-constructed Connection does nothing
298 for (const auto &c : d->m_sourceModelConnections)
300
302
303 if (sourceModel()) {
304 auto *m = sourceModel();
305 d->m_sourceModelConnections = {
338 };
339
340 if (d->m_handleLayoutChanges) {
341 d->m_sourceModelConnections.emplace_back(
344 d->m_sourceModelConnections.emplace_back(
347 }
348 }
349
351}
352
369{
370 d_func()->m_handleLayoutChanges = b;
371}
372
380{
381 return d_func()->m_handleLayoutChanges;
382}
383
385{
386 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
388 q->beginInsertColumns(q->mapFromSource(parent), start, end);
389}
390
392 int sourceStart, int sourceEnd,
393 const QModelIndex &destParent,
394 int dest)
395{
396 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true);
397 Q_ASSERT(destParent.isValid() ? destParent.model() == model : true);
399 q->beginMoveColumns(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destParent), dest);
400}
401
403{
404 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
406 q->beginRemoveColumns(q->mapFromSource(parent), start, end);
407}
408
410{
411 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
415 Q_UNUSED(end);
416 q->endInsertColumns();
417}
418
420 int sourceStart, int sourceEnd,
421 const QModelIndex &destParent, int dest)
422{
423 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true);
424 Q_ASSERT(destParent.isValid() ? destParent.model() == model : true);
426 Q_UNUSED(sourceParent);
427 Q_UNUSED(sourceStart);
428 Q_UNUSED(sourceEnd);
429 Q_UNUSED(destParent);
430 Q_UNUSED(dest);
431 q->endMoveColumns();
432}
433
435{
436 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
440 Q_UNUSED(end);
441 q->endRemoveColumns();
442}
443
445 const QModelIndex &bottomRight,
446 const QList<int> &roles)
447{
448 Q_ASSERT(topLeft.isValid() ? topLeft.model() == model : true);
449 Q_ASSERT(bottomRight.isValid() ? bottomRight.model() == model : true);
451 emit q->dataChanged(q->mapFromSource(topLeft), q->mapFromSource(bottomRight), roles);
452}
453
455 int last)
456{
458 emit q->headerDataChanged(orientation, first, last);
459}
460
462 const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
463{
465
466 QList<QPersistentModelIndex> parents;
467 parents.reserve(sourceParents.size());
468 for (const QPersistentModelIndex &parent : sourceParents) {
469 if (!parent.isValid()) {
470 parents << QPersistentModelIndex();
471 continue;
472 }
473 const QModelIndex mappedParent = q->mapFromSource(parent);
474 Q_ASSERT(mappedParent.isValid());
475 parents << mappedParent;
476 }
477
478 emit q->layoutAboutToBeChanged(parents, hint);
479
480 const auto proxyPersistentIndexes = q->persistentIndexList();
481 for (const QModelIndex &proxyPersistentIndex : proxyPersistentIndexes) {
482 proxyIndexes << proxyPersistentIndex;
483 Q_ASSERT(proxyPersistentIndex.isValid());
484 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
485 Q_ASSERT(srcPersistentIndex.isValid());
486 layoutChangePersistentIndexes << srcPersistentIndex;
487 }
488}
489
491 const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
492{
494
495 for (int i = 0; i < proxyIndexes.size(); ++i) {
496 q->changePersistentIndex(proxyIndexes.at(i), q->mapFromSource(layoutChangePersistentIndexes.at(i)));
497 }
498
501
502 QList<QPersistentModelIndex> parents;
503 parents.reserve(sourceParents.size());
504 for (const QPersistentModelIndex &parent : sourceParents) {
505 if (!parent.isValid()) {
506 parents << QPersistentModelIndex();
507 continue;
508 }
509 const QModelIndex mappedParent = q->mapFromSource(parent);
510 Q_ASSERT(mappedParent.isValid());
511 parents << mappedParent;
512 }
513
514 emit q->layoutChanged(parents, hint);
515}
516
518{
520 q->beginResetModel();
521}
522
524{
526 q->endResetModel();
527}
528
530 int end)
531{
532 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
534 q->beginInsertRows(q->mapFromSource(parent), start, end);
535}
536
538 int sourceStart, int sourceEnd,
539 const QModelIndex &destParent, int dest)
540{
541 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true);
542 Q_ASSERT(destParent.isValid() ? destParent.model() == model : true);
544 q->beginMoveRows(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destParent), dest);
545}
546
548 int end)
549{
550 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
552 q->beginRemoveRows(q->mapFromSource(parent), start, end);
553}
554
556{
557 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
561 Q_UNUSED(end);
562 q->endInsertRows();
563}
564
565void QIdentityProxyModelPrivate::sourceRowsMoved(const QModelIndex &sourceParent, int sourceStart,
566 int sourceEnd, const QModelIndex &destParent,
567 int dest)
568{
569 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == model : true);
570 Q_ASSERT(destParent.isValid() ? destParent.model() == model : true);
572 Q_UNUSED(sourceParent);
573 Q_UNUSED(sourceStart);
574 Q_UNUSED(sourceEnd);
575 Q_UNUSED(destParent);
576 Q_UNUSED(dest);
577 q->endMoveRows();
578}
579
581{
582 Q_ASSERT(parent.isValid() ? parent.model() == model : true);
586 Q_UNUSED(end);
587 q->endRemoveRows();
588}
589
591
592#include "moc_qidentityproxymodel.cpp"
void rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal)
void endResetModel()
Completes a model reset operation.
void modelAboutToBeReset(QPrivateSignal)
void columnsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after columns have been removed from the model.
LayoutChangeHint
This enum describes the way the model changes layout.
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before rows are inserted into the model.
void columnsAboutToBeInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before columns are inserted into the model.
void modelReset(QPrivateSignal)
Q_INVOKABLE int int const QModelIndex & destinationParent
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles=QList< int >())
This signal is emitted whenever the data in an existing item changes.
void columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal)
void columnsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before columns are removed from the model.
Q_INVOKABLE int int const QModelIndex int destinationChild
void layoutChanged(const QList< QPersistentModelIndex > &parents=QList< QPersistentModelIndex >(), QAbstractItemModel::LayoutChangeHint hint=QAbstractItemModel::NoLayoutChangeHint)
Q_INVOKABLE int sourceRow
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted just before rows are removed from the model.
void beginResetModel()
Begins a model reset operation.
void rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal)
void rowsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after rows have been inserted into the model.
Q_INVOKABLE int sourceColumn
void columnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal)
QModelIndex createIndex(int row, int column, const void *data=nullptr) const
Creates a model index for the given row and column with the internal pointer ptr.
void columnsInserted(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after columns have been inserted into the model.
void rowsRemoved(const QModelIndex &parent, int first, int last, QPrivateSignal)
This signal is emitted after rows have been removed from the model.
The QAbstractProxyModel class provides a base class for proxy item models that can do sorting,...
QModelIndex createSourceIndex(int row, int col, void *internalPtr) const
Equivalent to calling createIndex on the source model.
QAbstractItemModel * sourceModel
the source model of this proxy model.
virtual void setSourceModel(QAbstractItemModel *sourceModel)
Sets the given sourceModel to be processed by the proxy model.
void sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
QList< QPersistentModelIndex > layoutChangePersistentIndexes
void sourceRowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
void sourceColumnsRemoved(const QModelIndex &parent, int start, int end)
void sourceColumnsAboutToBeInserted(const QModelIndex &parent, int start, int end)
void sourceLayoutAboutToBeChanged(const QList< QPersistentModelIndex > &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
void sourceColumnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
void sourceColumnsInserted(const QModelIndex &parent, int start, int end)
void sourceLayoutChanged(const QList< QPersistentModelIndex > &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
void sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
void sourceHeaderDataChanged(Qt::Orientation orientation, int first, int last)
void sourceColumnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
void sourceRowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
void sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
void sourceRowsRemoved(const QModelIndex &parent, int start, int end)
void sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
void sourceRowsInserted(const QModelIndex &parent, int start, int end)
The QIdentityProxyModel class proxies its source model unmodified.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override
\reimp
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
\reimp
void setSourceModel(QAbstractItemModel *sourceModel) override
\reimp
bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) override
\reimp
QItemSelection mapSelectionFromSource(const QItemSelection &selection) const override
\reimp
QItemSelection mapSelectionToSource(const QItemSelection &selection) const override
\reimp
bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
bool removeColumns(int column, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
\reimp
int rowCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
\reimp
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
\reimp
bool isHandleSourceLayoutChanges() const
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override
\reimp
QIdentityProxyModel(QObject *parent=nullptr)
Constructs an identity model with the given parent.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
\reimp
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
\reimp
~QIdentityProxyModel()
Destroys this identity model.
\inmodule QtCore
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
const_iterator constBegin() const noexcept
Definition qlist.h:632
void reserve(qsizetype size)
Definition qlist.h:753
void append(parameter_type t)
Definition qlist.h:458
const_iterator constEnd() const noexcept
Definition qlist.h:633
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
QModelIndex parent() const
Returns the parent of the model index, or QModelIndex() if it has no parent.
constexpr const QAbstractItemModel * model() const noexcept
Returns a pointer to the model containing the item that this index refers to.
constexpr bool isValid() const noexcept
Returns {true} if this model index is valid; otherwise returns {false}.
QObject * parent
Definition qobject.h:73
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:299
\inmodule QtCore
Definition qobject.h:103
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
const_iterator constBegin() const noexcept
Definition qset.h:139
\inmodule QtCore
Definition qvariant.h:65
QSet< QString >::iterator it
Combined button and popup list for selecting options.
Orientation
Definition qnamespace.h:98
DropAction
QList< QModelIndex > QModelIndexList
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLboolean GLboolean GLboolean b
const GLfloat * m
GLuint GLuint end
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLsizei range
GLbitfield flags
GLuint start
GLint first
GLenum GLenum GLsizei void GLsizei void * column
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define emit
#define Q_UNUSED(x)
QSqlQueryModel * model
[16]
QItemSelection * selection
[0]
QLayoutItem * child
[0]