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}
41
45
47{
49 d->m_extraHeaders.append(header);
50}
51
53{
55 d->m_extraHeaders.remove(idx);
56}
57
58bool KExtraColumnsProxyModel::setExtraColumnData(const QModelIndex &parent, int row, int extraColumn, const QVariant &data, int role)
59{
60 Q_UNUSED(parent);
61 Q_UNUSED(row);
62 Q_UNUSED(extraColumn);
63 Q_UNUSED(data);
64 Q_UNUSED(role);
65 return false;
66}
67
68void KExtraColumnsProxyModel::extraColumnDataChanged(const QModelIndex &parent, int row, int extraColumn, const QList<int> &roles)
69{
70 const QModelIndex idx = index(row, proxyColumnForExtraColumn(extraColumn), parent);
71 Q_EMIT dataChanged(idx, idx, roles);
72}
73
75{
76 if (sourceModel()) {
79 this,
80 SLOT(_ec_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint)));
83 this,
85 }
86
88
89 if (model) {
90 // The handling of persistent model indexes assumes mapToSource can be called for any index
91 // This breaks for the extra column, so we'll have to do it ourselves
92 disconnect(model,
94 this,
95 SLOT(_q_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint)));
96 disconnect(model,
98 this,
100 connect(model,
102 this,
103 SLOT(_ec_sourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint)));
104 connect(model,
106 this,
108 }
109}
110
112{
113 if (!proxyIndex.isValid()) { // happens in e.g. rowCount(mapToSource(parent))
114 return QModelIndex();
115 }
116 const int column = proxyIndex.column();
117 if (column >= sourceModel()->columnCount()) {
118 qCDebug(KITEMMODELS_LOG) << "Returning invalid index in mapToSource";
119 return QModelIndex();
120 }
121 return QIdentityProxyModel::mapToSource(proxyIndex);
122}
123
125{
126 if (sourceModel()) {
127 const int column = proxyIndex.column();
128 if (column >= sourceModel()->columnCount()) {
129 return proxyIndex;
130 }
131 }
132 return QIdentityProxyModel::buddy(proxyIndex);
133}
134
135QModelIndex KExtraColumnsProxyModel::sibling(int row, int column, const QModelIndex &idx) const
136{
137 if (row == idx.row() && column == idx.column()) {
138 return idx;
139 }
140 return index(row, column, parent(idx));
141}
142
144{
145 QItemSelection sourceSelection;
146
147 if (!sourceModel()) {
148 return sourceSelection;
149 }
150
151 // mapToSource will give invalid index for our additional columns, so truncate the selection
152 // to the columns known by the source model
153 const int sourceColumnCount = sourceModel()->columnCount();
155 const QItemSelection::const_iterator end = selection.constEnd();
156 for (; it != end; ++it) {
157 Q_ASSERT(it->model() == this);
158 QModelIndex topLeft = it->topLeft();
159 Q_ASSERT(topLeft.isValid());
160 Q_ASSERT(topLeft.model() == this);
161 topLeft = topLeft.sibling(topLeft.row(), 0);
162 QModelIndex bottomRight = it->bottomRight();
163 Q_ASSERT(bottomRight.isValid());
164 Q_ASSERT(bottomRight.model() == this);
165 if (bottomRight.column() >= sourceColumnCount) {
166 bottomRight = bottomRight.sibling(bottomRight.row(), sourceColumnCount - 1);
167 }
168 // This can lead to duplicate source indexes, so use merge().
169 const QItemSelectionRange range(mapToSource(topLeft), mapToSource(bottomRight));
170 QItemSelection newSelection;
171 newSelection << range;
172 sourceSelection.merge(newSelection, QItemSelectionModel::Select);
173 }
174
175 return sourceSelection;
176}
177
179{
181 return QIdentityProxyModel::columnCount(parent) + d->m_extraHeaders.count();
182}
183
185{
187 const int extraCol = extraColumnForProxyColumn(index.column());
188 if (extraCol >= 0 && !d->m_extraHeaders.isEmpty()) {
189 return extraColumnData(index.parent(), index.row(), extraCol, role);
190 }
191 return sourceModel()->data(mapToSource(index), role);
192}
193
194bool KExtraColumnsProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
195{
197 const int extraCol = extraColumnForProxyColumn(index.column());
198 if (extraCol >= 0 && !d->m_extraHeaders.isEmpty()) {
199 return setExtraColumnData(index.parent(), index.row(), extraCol, value, role);
200 }
201 return sourceModel()->setData(mapToSource(index), value, role);
202}
203
205{
206 const int extraCol = extraColumnForProxyColumn(index.column());
207 if (extraCol >= 0) {
208 // extra columns are readonly
210 }
211 return sourceModel() != nullptr ? sourceModel()->flags(mapToSource(index)) : Qt::NoItemFlags;
212}
213
215{
216 if (index.column() > 0) {
217 return false;
218 }
220}
221
222QVariant KExtraColumnsProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
223{
225 if (orientation == Qt::Horizontal) {
226 const int extraCol = extraColumnForProxyColumn(section);
227 if (extraCol >= 0) {
228 // Only text is supported, in headers for extra columns
229 if (role == Qt::DisplayRole) {
230 return d->m_extraHeaders.at(extraCol);
231 }
232 return QVariant();
233 }
234 }
235 return QIdentityProxyModel::headerData(section, orientation, role);
236}
237
238QModelIndex KExtraColumnsProxyModel::index(int row, int column, const QModelIndex &parent) const
239{
240 const int extraCol = extraColumnForProxyColumn(column);
241 if (extraCol >= 0) {
242 // We store the internal pointer of the index for column 0 in the proxy index for extra columns.
243 // This will be useful in the parent method.
244 return createIndex(row, column, QIdentityProxyModel::index(row, 0, parent).internalPointer());
245 }
246 return QIdentityProxyModel::index(row, column, parent);
247}
248
250{
251 const int extraCol = extraColumnForProxyColumn(child.column());
252 if (extraCol >= 0) {
253 // Create an index for column 0 and use that to get the parent.
254 const QModelIndex proxySibling = createIndex(child.row(), 0, child.internalPointer());
255 return QIdentityProxyModel::parent(proxySibling);
256 }
257 return QIdentityProxyModel::parent(child);
258}
259
261{
262 if (sourceModel() != nullptr) {
263 const int sourceColumnCount = sourceModel()->columnCount();
264 if (proxyColumn >= sourceColumnCount) {
265 return proxyColumn - sourceColumnCount;
266 }
267 }
268 return -1;
269}
270
272{
273 return sourceModel()->columnCount() + extraColumn;
274}
275
276void KExtraColumnsProxyModelPrivate::_ec_sourceLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents,
278{
280
282 parents.reserve(sourceParents.size());
283 for (const QPersistentModelIndex &parent : sourceParents) {
284 if (!parent.isValid()) {
285 parents << QPersistentModelIndex();
286 continue;
287 }
288 const QModelIndex mappedParent = q->mapFromSource(parent);
289 Q_ASSERT(mappedParent.isValid());
290 parents << mappedParent;
291 }
292
293 Q_EMIT q->layoutAboutToBeChanged(parents, hint);
294
295 const QModelIndexList persistentIndexList = q->persistentIndexList();
296 layoutChangePersistentIndexes.reserve(persistentIndexList.size());
297 layoutChangeProxyColumns.reserve(persistentIndexList.size());
298
299 for (QModelIndex proxyPersistentIndex : persistentIndexList) {
300 proxyIndexes << proxyPersistentIndex;
301 Q_ASSERT(proxyPersistentIndex.isValid());
302 const int column = proxyPersistentIndex.column();
303 layoutChangeProxyColumns << column;
304 if (column >= q->sourceModel()->columnCount()) {
305 proxyPersistentIndex = proxyPersistentIndex.sibling(proxyPersistentIndex.row(), 0);
306 }
307 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
308 Q_ASSERT(srcPersistentIndex.isValid());
309 layoutChangePersistentIndexes << srcPersistentIndex;
310 }
311}
312
313void KExtraColumnsProxyModelPrivate::_ec_sourceLayoutChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
314{
316 for (int i = 0; i < proxyIndexes.size(); ++i) {
317 const QModelIndex proxyIdx = proxyIndexes.at(i);
318 QModelIndex newProxyIdx = q->mapFromSource(layoutChangePersistentIndexes.at(i));
319 if (proxyIdx.column() >= q->sourceModel()->columnCount()) {
320 newProxyIdx = newProxyIdx.sibling(newProxyIdx.row(), layoutChangeProxyColumns.at(i));
321 }
322 q->changePersistentIndex(proxyIdx, newProxyIdx);
323 }
324
325 layoutChangePersistentIndexes.clear();
326 layoutChangeProxyColumns.clear();
327 proxyIndexes.clear();
328
330 parents.reserve(sourceParents.size());
331 for (const QPersistentModelIndex &parent : sourceParents) {
332 if (!parent.isValid()) {
333 parents << QPersistentModelIndex();
334 continue;
335 }
336 const QModelIndex mappedParent = q->mapFromSource(parent);
337 Q_ASSERT(mappedParent.isValid());
338 parents << mappedParent;
339 }
340
341 Q_EMIT q->layoutChanged(parents, hint);
342}
343
344#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 Tue Mar 26 2024 11:20:33 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.