KItemModels

krearrangecolumnsproxymodel.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 "krearrangecolumnsproxymodel.h"
9
10class KRearrangeColumnsProxyModelPrivate
11{
12public:
13 QList<int> m_sourceColumns;
14};
15
17 : QIdentityProxyModel(parent)
18 , d_ptr(new KRearrangeColumnsProxyModelPrivate)
19{
20}
21
25
27{
28 // We could use layoutChanged() here, but we would have to map persistent
29 // indexes from the old to the new location...
31 d_ptr->m_sourceColumns = columns;
33}
34
36{
38 if (!sourceModel()) {
39 return 0;
40 }
41 return d_ptr->m_sourceColumns.count();
42}
43
45{
46 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
47 if (!sourceModel()) {
48 return 0;
49 }
50 if (parent.column() > 0) {
51 return 0;
52 }
53 // The parent in the source model is on column 0, whatever swapping we are doing
55 return sourceModel()->rowCount(sourceParent);
56}
57
59{
60 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
61 if (!sourceModel()) {
62 return false;
63 }
64 if (d_ptr->m_sourceColumns.isEmpty()) { // no columns configured yet
65 return false;
66 }
67 if (parent.column() > 0) {
68 return false;
69 }
71 return sourceModel()->rowCount(sourceParent) > 0;
72}
73
74QModelIndex KRearrangeColumnsProxyModel::index(int row, int column, const QModelIndex &parent) const
75{
76 Q_ASSERT(parent.isValid() ? parent.model() == this : true);
77 Q_ASSERT(row >= 0);
78 Q_ASSERT(column >= 0);
79
80 // Only first column has children
81 if (parent.column() > 0) {
82 return {};
83 }
84
85 if (!sourceModel()) {
86 return {};
87 }
88 if (d_ptr->m_sourceColumns.isEmpty()) {
89 return {};
90 }
91
92 // The parent in the source model is on column 0, whatever swapping we are doing
94
95 // Find the child in the source model, we need its internal pointer
97 if (!sourceIndex.isValid()) {
98 return QModelIndex();
99 }
100
101 return createIndex(row, column, sourceIndex.internalPointer());
102}
103
105{
106 Q_ASSERT(child.isValid() ? child.model() == this : true);
107 const QModelIndex sourceIndex = mapToSource(child);
108 const QModelIndex sourceParent = sourceIndex.parent();
109 if (!sourceParent.isValid()) {
110 return QModelIndex();
111 }
112 return createIndex(sourceParent.row(), 0, sourceParent.internalPointer());
113}
114
115QVariant KRearrangeColumnsProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
116{
117 if (orientation == Qt::Horizontal) {
118 if (!sourceModel() || section >= d_ptr->m_sourceColumns.count()) {
119 return QVariant();
120 }
121 const int sourceCol = sourceColumnForProxyColumn(section);
122 return sourceModel()->headerData(sourceCol, orientation, role);
123 } else {
124 return QIdentityProxyModel::headerData(section, orientation, role);
125 }
126}
127
129{
130 if (column >= d_ptr->m_sourceColumns.count()) {
131 return QModelIndex();
132 }
133 return index(row, column, idx.parent());
134}
135
137{
138 if (!sourceIndex.isValid()) {
139 return QModelIndex();
140 }
141 Q_ASSERT(sourceIndex.model() == sourceModel());
143 return createIndex(sourceIndex.row(), proxyColumn, sourceIndex.internalPointer());
144}
145
147{
148 if (!proxyIndex.isValid()) {
149 return QModelIndex();
150 }
151 return createSourceIndex(proxyIndex.row(), sourceColumnForProxyColumn(proxyIndex.column()), proxyIndex.internalPointer());
152}
153
155{
156 // If this is too slow, we could add a second QList with index=logical_source_column value=desired_pos_in_proxy.
157 return d_ptr->m_sourceColumns.indexOf(sourceColumn);
158}
159
161{
162 Q_ASSERT(proxyColumn >= 0);
163 Q_ASSERT(proxyColumn < d_ptr->m_sourceColumns.size());
164 return d_ptr->m_sourceColumns.at(proxyColumn);
165}
166
167#include "moc_krearrangecolumnsproxymodel.cpp"
KRearrangeColumnsProxyModel(QObject *parent=nullptr)
Creates a KRearrangeColumnsProxyModel proxy.
int sourceColumnForProxyColumn(int proxyColumn) const
Returns the source column for the given proxy column.
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
int proxyColumnForSourceColumn(int sourceColumn) const
Returns the proxy column for the given source column or -1 if the source column isn't shown in the pr...
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
void setSourceColumns(const QList< int > &columns)
Set the chosen source columns, in the desired order for the proxy columns columns[proxyColumn=0] is t...
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
~KRearrangeColumnsProxyModel() override
Destructor.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QModelIndex createIndex(int row, int column, const void *ptr) const const
QModelIndex createSourceIndex(int row, int col, void *internalPtr) const const
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const override
bool isValid() const const
const QAbstractItemModel * model() const const
QModelIndex sibling(int row, int column) const const
QObject * parent() const const
T qobject_cast(QObject *object)
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.