KItemModels

kextracolumnsproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3 SPDX-FileContributor: David Faure <david.faure@kdab.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kextracolumnsproxymodel.h"
9#include "kitemmodels_debug.h"
10
11#include <QItemSelection>
12
13class KExtraColumnsProxyModelPrivate
14{
15 Q_DECLARE_PUBLIC(KExtraColumnsProxyModel)
16 KExtraColumnsProxyModel *const q_ptr;
17
18public:
19 KExtraColumnsProxyModelPrivate(KExtraColumnsProxyModel *model)
20 : q_ptr(model)
21 {
22 }
23
24 void _ec_sourceLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint);
25 void _ec_sourceLayoutChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint);
26
27 // Configuration (doesn't change once source model is plugged in)
28 QList<QString> m_extraHeaders;
29
30 // for layoutAboutToBeChanged/layoutChanged
31 QList<QPersistentModelIndex> layoutChangePersistentIndexes;
32 QList<int> layoutChangeProxyColumns;
33 QModelIndexList proxyIndexes;
34};
35
37 : QIdentityProxyModel(parent)
38 , d_ptr(new KExtraColumnsProxyModelPrivate(this))
39{
40 // The handling of persistent model indexes assumes mapToSource can be called for any index
41 // This breaks for the extra column, so we'll have to do it ourselves
42#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
43 setHandleSourceLayoutChanges(false);
44#endif
45}
46
50
52{
54 d->m_extraHeaders.append(header);
55}
56
58{
60 d->m_extraHeaders.remove(idx);
61}
62
63bool KExtraColumnsProxyModel::setExtraColumnData(const QModelIndex &parent, int row, int extraColumn, const QVariant &data, int role)
64{
65 Q_UNUSED(parent);
66 Q_UNUSED(row);
67 Q_UNUSED(extraColumn);
68 Q_UNUSED(data);
69 Q_UNUSED(role);
70 return false;
71}
72
73void KExtraColumnsProxyModel::extraColumnDataChanged(const QModelIndex &parent, int row, int extraColumn, const QList<int> &roles)
74{
75 const QModelIndex idx = index(row, proxyColumnForExtraColumn(extraColumn), parent);
76 Q_EMIT dataChanged(idx, idx, roles);
77}
78
80{
81 if (sourceModel()) {
84 this,
85 SLOT(_ec_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint)));
88 this,
90 }
91
93
94 if (model) {
95 // The handling of persistent model indexes assumes mapToSource can be called for any index
96 // This breaks for the extra column, so we'll have to do it ourselves
97#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
98 disconnect(model,
100 this,
101 SLOT(_q_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint)));
102 disconnect(model,
104 this,
106#endif
107 connect(model,
109 this,
110 SLOT(_ec_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint)));
111 connect(model,
113 this,
115 }
116}
117
119{
120 if (!proxyIndex.isValid()) { // happens in e.g. rowCount(mapToSource(parent))
121 return QModelIndex();
122 }
123 const int column = proxyIndex.column();
124 if (column >= sourceModel()->columnCount()) {
125 qCDebug(KITEMMODELS_LOG) << "Returning invalid index in mapToSource";
126 return QModelIndex();
127 }
128 return QIdentityProxyModel::mapToSource(proxyIndex);
129}
130
132{
133 if (sourceModel()) {
134 const int column = proxyIndex.column();
135 if (column >= sourceModel()->columnCount()) {
136 return proxyIndex;
137 }
138 }
139 return QIdentityProxyModel::buddy(proxyIndex);
140}
141
142QModelIndex KExtraColumnsProxyModel::sibling(int row, int column, const QModelIndex &idx) const
143{
144 if (row == idx.row() && column == idx.column()) {
145 return idx;
146 }
147 return index(row, column, parent(idx));
148}
149
151{
152 QItemSelection sourceSelection;
153
154 if (!sourceModel()) {
155 return sourceSelection;
156 }
157
158 // mapToSource will give invalid index for our additional columns, so truncate the selection
159 // to the columns known by the source model
160 const int sourceColumnCount = sourceModel()->columnCount();
162 const QItemSelection::const_iterator end = selection.constEnd();
163 for (; it != end; ++it) {
164 Q_ASSERT(it->model() == this);
165 QModelIndex topLeft = it->topLeft();
166 Q_ASSERT(topLeft.isValid());
167 Q_ASSERT(topLeft.model() == this);
168 topLeft = topLeft.sibling(topLeft.row(), 0);
169 QModelIndex bottomRight = it->bottomRight();
170 Q_ASSERT(bottomRight.isValid());
171 Q_ASSERT(bottomRight.model() == this);
172 if (bottomRight.column() >= sourceColumnCount) {
173 bottomRight = bottomRight.sibling(bottomRight.row(), sourceColumnCount - 1);
174 }
175 // This can lead to duplicate source indexes, so use merge().
176 const QItemSelectionRange range(mapToSource(topLeft), mapToSource(bottomRight));
177 QItemSelection newSelection;
178 newSelection << range;
179 sourceSelection.merge(newSelection, QItemSelectionModel::Select);
180 }
181
182 return sourceSelection;
183}
184
186{
188 return QIdentityProxyModel::columnCount(parent) + d->m_extraHeaders.count();
189}
190
192{
194 const int extraCol = extraColumnForProxyColumn(index.column());
195 if (extraCol >= 0 && !d->m_extraHeaders.isEmpty()) {
196 return extraColumnData(index.parent(), index.row(), extraCol, role);
197 }
198 return sourceModel()->data(mapToSource(index), role);
199}
200
201bool KExtraColumnsProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
202{
204 const int extraCol = extraColumnForProxyColumn(index.column());
205 if (extraCol >= 0 && !d->m_extraHeaders.isEmpty()) {
206 return setExtraColumnData(index.parent(), index.row(), extraCol, value, role);
207 }
208 return sourceModel()->setData(mapToSource(index), value, role);
209}
210
212{
213 const int extraCol = extraColumnForProxyColumn(index.column());
214 if (extraCol >= 0) {
215 // extra columns are readonly
217 }
218 return sourceModel() != nullptr ? sourceModel()->flags(mapToSource(index)) : Qt::NoItemFlags;
219}
220
222{
223 if (index.column() > 0) {
224 return false;
225 }
227}
228
229QVariant KExtraColumnsProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
230{
232 if (orientation == Qt::Horizontal) {
233 const int extraCol = extraColumnForProxyColumn(section);
234 if (extraCol >= 0) {
235 // Only text is supported, in headers for extra columns
236 if (role == Qt::DisplayRole) {
237 return d->m_extraHeaders.at(extraCol);
238 }
239 return QVariant();
240 }
241 }
242 return QIdentityProxyModel::headerData(section, orientation, role);
243}
244
245QModelIndex KExtraColumnsProxyModel::index(int row, int column, const QModelIndex &parent) const
246{
247 const int extraCol = extraColumnForProxyColumn(column);
248 if (extraCol >= 0) {
249 // We store the internal pointer of the index for column 0 in the proxy index for extra columns.
250 // This will be useful in the parent method.
251 return createIndex(row, column, QIdentityProxyModel::index(row, 0, parent).internalPointer());
252 }
253 return QIdentityProxyModel::index(row, column, parent);
254}
255
257{
258 const int extraCol = extraColumnForProxyColumn(child.column());
259 if (extraCol >= 0) {
260 // Create an index for column 0 and use that to get the parent.
261 const QModelIndex proxySibling = createIndex(child.row(), 0, child.internalPointer());
262 return QIdentityProxyModel::parent(proxySibling);
263 }
264 return QIdentityProxyModel::parent(child);
265}
266
268{
269 if (sourceModel() != nullptr) {
270 const int sourceColumnCount = sourceModel()->columnCount();
271 if (proxyColumn >= sourceColumnCount) {
272 return proxyColumn - sourceColumnCount;
273 }
274 }
275 return -1;
276}
277
279{
280 return sourceModel()->columnCount() + extraColumn;
281}
282
283void KExtraColumnsProxyModelPrivate::_ec_sourceLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents,
285{
287
289 parents.reserve(sourceParents.size());
290 for (const QPersistentModelIndex &parent : sourceParents) {
291 if (!parent.isValid()) {
292 parents << QPersistentModelIndex();
293 continue;
294 }
295 const QModelIndex mappedParent = q->mapFromSource(parent);
296 Q_ASSERT(mappedParent.isValid());
297 parents << mappedParent;
298 }
299
300 Q_EMIT q->layoutAboutToBeChanged(parents, hint);
301
302 const QModelIndexList persistentIndexList = q->persistentIndexList();
303 layoutChangePersistentIndexes.reserve(persistentIndexList.size());
304 layoutChangeProxyColumns.reserve(persistentIndexList.size());
305
306 for (QModelIndex proxyPersistentIndex : persistentIndexList) {
307 proxyIndexes << proxyPersistentIndex;
308 Q_ASSERT(proxyPersistentIndex.isValid());
309 const int column = proxyPersistentIndex.column();
310 layoutChangeProxyColumns << column;
311 if (column >= q->sourceModel()->columnCount()) {
312 proxyPersistentIndex = proxyPersistentIndex.sibling(proxyPersistentIndex.row(), 0);
313 }
314 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
315 Q_ASSERT(srcPersistentIndex.isValid());
316 layoutChangePersistentIndexes << srcPersistentIndex;
317 }
318}
319
320void KExtraColumnsProxyModelPrivate::_ec_sourceLayoutChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
321{
323 for (int i = 0; i < proxyIndexes.size(); ++i) {
324 const QModelIndex proxyIdx = proxyIndexes.at(i);
325 QModelIndex newProxyIdx = q->mapFromSource(layoutChangePersistentIndexes.at(i));
326 if (proxyIdx.column() >= q->sourceModel()->columnCount()) {
327 newProxyIdx = newProxyIdx.sibling(newProxyIdx.row(), layoutChangeProxyColumns.at(i));
328 }
329 q->changePersistentIndex(proxyIdx, newProxyIdx);
330 }
331
332 layoutChangePersistentIndexes.clear();
333 layoutChangeProxyColumns.clear();
334 proxyIndexes.clear();
335
337 parents.reserve(sourceParents.size());
338 for (const QPersistentModelIndex &parent : sourceParents) {
339 if (!parent.isValid()) {
340 parents << QPersistentModelIndex();
341 continue;
342 }
343 const QModelIndex mappedParent = q->mapFromSource(parent);
344 Q_ASSERT(mappedParent.isValid());
345 parents << mappedParent;
346 }
347
348 Q_EMIT q->layoutChanged(parents, hint);
349}
350
351#include "moc_kextracolumnsproxymodel.cpp"
This proxy appends extra columns (after all existing columns).
void removeExtraColumn(int idx)
Removes an extra column.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
void setSourceModel(QAbstractItemModel *model) override
void appendColumn(const QString &header=QString())
Appends an extra column.
QItemSelection mapSelectionToSource(const QItemSelection &selection) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int extraColumnForProxyColumn(int proxyColumn) const
Returns the extra column number (0, 1, ...) for a given column number of the proxymodel.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
bool hasChildren(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
~KExtraColumnsProxyModel() override
Destructor.
int proxyColumnForExtraColumn(int extraColumn) const
Returns the proxy column number for a given extra column number (starting at 0).
virtual bool setExtraColumnData(const QModelIndex &parent, int row, int extraColumn, const QVariant &data, int role=Qt::EditRole)
This method is called by setData() for extra columns.
void extraColumnDataChanged(const QModelIndex &parent, int row, int extraColumn, const QList< int > &roles)
This method can be called by your derived class when the data in an extra column has changed.
QModelIndex buddy(const QModelIndex &index) const override
KExtraColumnsProxyModel(QObject *parent=nullptr)
Base class constructor.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Qt::ItemFlags flags(const QModelIndex &index) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
virtual QVariant extraColumnData(const QModelIndex &parent, int row, int extraColumn, int role=Qt::DisplayRole) const =0
This method is called by data() for extra columns.
virtual QModelIndex buddy(const QModelIndex &index) const const
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual bool hasChildren(const QModelIndex &parent) const const
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
virtual int columnCount(const QModelIndex &parent) const const override
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual void setSourceModel(QAbstractItemModel *newSourceModel) override
void merge(const QItemSelection &other, QItemSelectionModel::SelectionFlags command)
const_reference at(qsizetype i) const const
void clear()
const_iterator constBegin() const const
const_iterator constEnd() const const
void reserve(qsizetype size)
qsizetype size() const const
int column() const const
void * internalPointer() const const
bool isValid() const const
const QAbstractItemModel * model() const const
QModelIndex parent() const const
int row() const const
QModelIndex sibling(int row, int column) const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QObject * parent() const const
bool isValid() const const
DisplayRole
typedef ItemFlags
Orientation
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:13:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.