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
qtransposeproxymodel.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 Luca Beldi <v.ronin@yahoo.it>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <private/qtransposeproxymodel_p.h>
6#include <QtCore/qlist.h>
7#include <QtCore/qmetaobject.h>
8#include <QtCore/qsize.h>
9#include <QtCore/qmap.h>
10
12
13QModelIndex QTransposeProxyModelPrivate::uncheckedMapToSource(const QModelIndex &proxyIndex) const
14{
15 if (!model || !proxyIndex.isValid())
16 return QModelIndex();
17 Q_Q(const QTransposeProxyModel);
18 return q->createSourceIndex(proxyIndex.column(), proxyIndex.row(), proxyIndex.internalPointer());
19}
20
21QModelIndex QTransposeProxyModelPrivate::uncheckedMapFromSource(const QModelIndex &sourceIndex) const
22{
23 if (!model || !sourceIndex.isValid())
24 return QModelIndex();
25 Q_Q(const QTransposeProxyModel);
26 return q->createIndex(sourceIndex.column(), sourceIndex.row(), sourceIndex.internalPointer());
27}
28
29void QTransposeProxyModelPrivate::onLayoutChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint)
30{
32 Q_ASSERT(layoutChangeProxyIndexes.size() == layoutChangePersistentIndexes.size());
34 toList.reserve(layoutChangePersistentIndexes.size());
35 for (const QPersistentModelIndex &persistIdx : std::as_const(layoutChangePersistentIndexes))
36 toList << q->mapFromSource(persistIdx);
37 q->changePersistentIndexList(layoutChangeProxyIndexes, toList);
38 layoutChangeProxyIndexes.clear();
39 layoutChangePersistentIndexes.clear();
40 QList<QPersistentModelIndex> proxyParents;
41 proxyParents.reserve(parents.size());
42 for (const QPersistentModelIndex &srcParent : parents)
43 proxyParents << q->mapFromSource(srcParent);
49 emit q->layoutChanged(proxyParents, proxyHint);
50}
51
52void QTransposeProxyModelPrivate::onLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
53{
55 QList<QPersistentModelIndex> proxyParents;
56 proxyParents.reserve(sourceParents.size());
57 for (const QPersistentModelIndex &parent : sourceParents) {
58 if (!parent.isValid()) {
59 proxyParents << QPersistentModelIndex();
60 continue;
61 }
62 const QModelIndex mappedParent = q->mapFromSource(parent);
63 Q_ASSERT(mappedParent.isValid());
64 proxyParents << mappedParent;
65 }
71 emit q->layoutAboutToBeChanged(proxyParents, proxyHint);
72 const QModelIndexList proxyPersistentIndexes = q->persistentIndexList();
73 layoutChangeProxyIndexes.clear();
74 layoutChangePersistentIndexes.clear();
75 layoutChangeProxyIndexes.reserve(proxyPersistentIndexes.size());
76 layoutChangePersistentIndexes.reserve(proxyPersistentIndexes.size());
77 for (const QModelIndex &proxyPersistentIndex : proxyPersistentIndexes) {
78 layoutChangeProxyIndexes << proxyPersistentIndex;
79 Q_ASSERT(proxyPersistentIndex.isValid());
80 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
81 Q_ASSERT(srcPersistentIndex.isValid());
82 layoutChangePersistentIndexes << srcPersistentIndex;
83 }
84}
85
86void QTransposeProxyModelPrivate::onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
87 const QList<int> &roles)
88{
90 emit q->dataChanged(q->mapFromSource(topLeft), q->mapFromSource(bottomRight), roles);
91}
92
93void QTransposeProxyModelPrivate::onHeaderDataChanged(Qt::Orientation orientation, int first, int last)
94{
96 emit q->headerDataChanged(orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal, first, last);
97}
98
99void QTransposeProxyModelPrivate::onColumnsAboutToBeInserted(const QModelIndex &parent, int first, int last)
100{
102 q->beginInsertRows(q->mapFromSource(parent), first, last);
103}
104
105void QTransposeProxyModelPrivate::onColumnsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
106{
108 q->beginRemoveRows(q->mapFromSource(parent), first, last);
109}
110
111void QTransposeProxyModelPrivate::onColumnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
112{
114 q->beginMoveRows(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destinationParent), destinationColumn);
115}
116
117void QTransposeProxyModelPrivate::onRowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
118{
120 q->beginInsertColumns(q->mapFromSource(parent), first, last);
121}
122
123void QTransposeProxyModelPrivate::onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
124{
126 q->beginRemoveColumns(q->mapFromSource(parent), first, last);
127}
128
129void QTransposeProxyModelPrivate::onRowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
130{
132 q->beginMoveColumns(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destinationParent), destinationRow);
133}
134
152
157
164
169{
171 if (newSourceModel == d->model)
172 return;
174 if (d->model) {
175 for (const QMetaObject::Connection& discIter : std::as_const(d->sourceConnections))
176 disconnect(discIter);
177 }
178 d->sourceConnections.clear();
180 if (d->model) {
181 using namespace std::placeholders;
182 d->sourceConnections = QList<QMetaObject::Connection>{
185 connect(d->model, &QAbstractItemModel::dataChanged, this, std::bind(&QTransposeProxyModelPrivate::onDataChanged, d, _1, _2, _3)),
186 connect(d->model, &QAbstractItemModel::headerDataChanged, this, std::bind(&QTransposeProxyModelPrivate::onHeaderDataChanged, d, _1, _2, _3)),
187 connect(d->model, &QAbstractItemModel::columnsAboutToBeInserted, this, std::bind(&QTransposeProxyModelPrivate::onColumnsAboutToBeInserted, d, _1, _2, _3)),
188 connect(d->model, &QAbstractItemModel::columnsAboutToBeMoved, this, std::bind(&QTransposeProxyModelPrivate::onColumnsAboutToBeMoved, d, _1, _2, _3, _4, _5)),
189 connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved, this, std::bind(&QTransposeProxyModelPrivate::onColumnsAboutToBeRemoved, d, _1, _2, _3)),
193 connect(d->model, &QAbstractItemModel::rowsAboutToBeInserted, this, std::bind(&QTransposeProxyModelPrivate::onRowsAboutToBeInserted, d, _1, _2, _3)),
194 connect(d->model, &QAbstractItemModel::rowsAboutToBeMoved, this, std::bind(&QTransposeProxyModelPrivate::onRowsAboutToBeMoved, d, _1, _2, _3, _4, _5)),
195 connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved, this, std::bind(&QTransposeProxyModelPrivate::onRowsAboutToBeRemoved, d, _1, _2, _3)),
199 connect(d->model, &QAbstractItemModel::layoutAboutToBeChanged, this, std::bind(&QTransposeProxyModelPrivate::onLayoutAboutToBeChanged, d, _1, _2)),
200 connect(d->model, &QAbstractItemModel::layoutChanged, this, std::bind(&QTransposeProxyModelPrivate::onLayoutChanged, d, _1, _2))
201 };
202 }
204}
205
210{
211 Q_D(const QTransposeProxyModel);
212 if (!d->model)
213 return 0;
215 return d->model->columnCount(mapToSource(parent));
216}
217
222{
223 Q_D(const QTransposeProxyModel);
224 if (!d->model)
225 return 0;
227 return d->model->rowCount(mapToSource(parent));
228}
229
233QVariant QTransposeProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
234{
235 Q_D(const QTransposeProxyModel);
236 if (!d->model)
237 return QVariant();
238 return d->model->headerData(section, orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal, role);
239}
240
244bool QTransposeProxyModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
245{
247 if (!d->model)
248 return false;
249 return d->model->setHeaderData(section, orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal, value, role);
250}
251
255bool QTransposeProxyModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
256{
259 if (!d->model || !index.isValid())
260 return false;
261 return d->model->setItemData(mapToSource(index), roles);
262}
263
268{
269 Q_D(const QTransposeProxyModel);
271 if (!d->model || !index.isValid())
272 return QSize();
273 return d->model->span(mapToSource(index)).transposed();
274}
275
279QMap<int, QVariant> QTransposeProxyModel::itemData(const QModelIndex &index) const
280{
281 Q_D(const QTransposeProxyModel);
282 if (!d->model)
283 return QMap<int, QVariant>();
285 return d->model->itemData(mapToSource(index));
286}
287
292{
293 Q_D(const QTransposeProxyModel);
294 if (!d->model || !sourceIndex.isValid())
295 return QModelIndex();
296 Q_ASSERT(d->model->checkIndex(sourceIndex));
297 return d->uncheckedMapFromSource(sourceIndex);
298}
299
304{
305 Q_D(const QTransposeProxyModel);
306 Q_ASSERT(checkIndex(proxyIndex));
307 if (!d->model || !proxyIndex.isValid())
308 return QModelIndex();
309 return d->uncheckedMapToSource(proxyIndex);
310}
311
316{
317 Q_D(const QTransposeProxyModel);
319 if (!d->model || !index.isValid())
320 return QModelIndex();
321 return d->uncheckedMapFromSource(d->uncheckedMapToSource(index).parent());
322}
323
328{
329 Q_D(const QTransposeProxyModel);
331 if (!d->model)
332 return QModelIndex();
333 return mapFromSource(d->model->index(column, row, mapToSource(parent)));
334}
335
340{
343 if (!d->model)
344 return false;
345 return d->model->insertColumns(row, count, mapToSource(parent));
346}
347
352{
355 if (!d->model)
356 return false;
357 return d->model->removeColumns(row, count, mapToSource(parent));
358}
359
363bool QTransposeProxyModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
364{
366 Q_ASSERT(checkIndex(sourceParent));
368 if (!d->model)
369 return false;
370 return d->model->moveColumns(mapToSource(sourceParent), sourceRow, count, mapToSource(destinationParent), destinationChild);
371}
372
377{
380 if (!d->model)
381 return false;
382 return d->model->insertRows(column, count, mapToSource(parent));
383}
384
389{
392 if (!d->model)
393 return false;
394 return d->model->removeRows(column, count, mapToSource(parent));
395}
396
400bool QTransposeProxyModel::moveColumns(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
401{
403 Q_ASSERT(checkIndex(sourceParent));
405 if (!d->model)
406 return false;
407 return d->model->moveRows(mapToSource(sourceParent), sourceRow, count, mapToSource(destinationParent), destinationChild);
408}
409
420
422
423#include "moc_qtransposeproxymodel.cpp"
void rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow, QPrivateSignal)
Q_INVOKABLE int const QModelIndex & parent
Returns the parent of the model item with the given index.
void endResetModel()
Completes a model reset operation.
void modelAboutToBeReset(QPrivateSignal)
void endMoveColumns()
Ends a column move operation.
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)
void endRemoveRows()
Ends a row removal operation.
void endMoveRows()
Ends a row move operation.
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)
bool checkIndex(const QModelIndex &index, CheckIndexOptions options=CheckIndexOption::NoOption) const
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 endInsertRows()
Ends a row insertion operation.
void beginResetModel()
Begins a model reset operation.
void endRemoveColumns()
Ends a column removal 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.
void columnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn, QPrivateSignal)
void endInsertColumns()
Ends a column insertion operation.
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,...
virtual void setSourceModel(QAbstractItemModel *sourceModel)
Sets the given sourceModel to be processed by the proxy model.
qsizetype size() const noexcept
Definition qlist.h:397
void reserve(qsizetype size)
Definition qlist.h:753
void clear()
Definition qlist.h:434
\inmodule QtCore Represents a handle to a signal-slot (or signal-functor) connection.
\inmodule QtCore
QObject * parent
Definition qobject.h:73
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore
Definition qsize.h:25
This proxy transposes the source model.
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole) override
\reimp
bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) override
\reimp
~QTransposeProxyModel()
Destructs the proxy model.
QMap< int, QVariant > itemData(const QModelIndex &index) const override
\reimp
QSize span(const QModelIndex &index) const override
\reimp
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
\reimp
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
bool removeColumns(int column, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
\reimp This method will perform no action.
QTransposeProxyModel(QObject *parent=nullptr)
Constructs a new proxy model with the given parent.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
\reimp
void setSourceModel(QAbstractItemModel *newSourceModel) override
\reimp
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
\reimp
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override
\reimp
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
\reimp
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &roles) override
\reimp
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
\reimp
int columnCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
Definition qcompare.h:63
Orientation
Definition qnamespace.h:98
@ Horizontal
Definition qnamespace.h:99
@ Vertical
Definition qnamespace.h:100
SortOrder
Definition qnamespace.h:121
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLuint index
[2]
GLenum GLenum GLsizei count
GLint first
GLenum GLenum GLsizei void GLsizei void * column
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLenum GLenum GLsizei void * row
GLfixed GLfixed GLint GLint order
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static QList< QVariant > toList(char **buf, int count)
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define emit
#define Q_UNUSED(x)
QSqlQueryModel * model
[16]
myObject disconnect()
[26]