KChart

KChartDatasetProxyModel.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartDatasetProxyModel.h"
10
11#include "KChartMath_p.h"
12
13#include <QtDebug>
14
15
16using namespace KChart;
17
22
23QModelIndex DatasetProxyModel::buddy( const QModelIndex& index ) const
24{
25 return index;
26}
27
28Qt::ItemFlags DatasetProxyModel::flags( const QModelIndex& index ) const
29{
30 return sourceModel()->flags( mapToSource( index ) );
31}
32
34 const DatasetDescriptionVector& configuration )
35{
36 Q_ASSERT_X( sourceModel(), "DatasetProxyModel::setDatasetRowDescriptionVector",
37 "A source model must be set before the selection can be configured." );
38 initializeDatasetDecriptors( configuration, sourceModel()->rowCount(mRootIndex),
39 mRowSrcToProxyMap, mRowProxyToSrcMap );
40 invalidate(); // clear emits layoutChanged()
41}
42
44 const DatasetDescriptionVector& configuration )
45{
46 Q_ASSERT_X( sourceModel(), "DatasetProxyModel::setDatasetColumnDescriptionVector",
47 "A source model must be set before the selection can be configured." );
48 initializeDatasetDecriptors( configuration, sourceModel()->columnCount(mRootIndex),
49 mColSrcToProxyMap, mColProxyToSrcMap );
50 invalidate(); // clear emits layoutChanged()
51}
52
60
61QModelIndex DatasetProxyModel::index( int row, int column,
62 const QModelIndex &parent ) const
63{
64 return mapFromSource( sourceModel()->index( mapProxyRowToSource(row),
65 mapProxyColumnToSource(column),
66 parent ) );
67}
68
70{
71// return mapFromSource( sourceModel()->parent( child ) );
72 return mapFromSource( sourceModel()->parent( mapToSource( child ) ) );
73}
74
76{
77 Q_ASSERT_X( sourceModel(), "DatasetProxyModel::mapFromSource", "A source "
78 "model must be set before the selection can be configured." );
79
80 if ( !sourceIndex.isValid() ) return sourceIndex;
81
82 if ( mRowSrcToProxyMap.isEmpty() && mColSrcToProxyMap.isEmpty() )
83 {
84 return createIndex( sourceIndex.row(), sourceIndex.column(),
85 sourceIndex.internalPointer() );
86 } else {
87 int row = mapSourceRowToProxy( sourceIndex.row() );
88 int column = mapSourceColumnToProxy( sourceIndex.column() );
89 return createIndex( row, column, sourceIndex.internalPointer() );
90 }
91}
92
94{
95 Q_ASSERT_X( sourceModel(), "DatasetProxyModel::mapToSource", "A source "
96 "model must be set before the selection can be configured." );
97
98 if ( !proxyIndex.isValid() ) return proxyIndex;
99 if ( mRowSrcToProxyMap.isEmpty() && mColSrcToProxyMap.isEmpty() )
100 {
101 return sourceModel()->index( proxyIndex.row(), proxyIndex.column(), mRootIndex );
102 } else {
103 int row = mapProxyRowToSource( proxyIndex.row() );
104 int column = mapProxyColumnToSource( proxyIndex.column() );
105 return sourceModel()->index( row, column, mRootIndex );
106 }
107}
108
110 const QModelIndex & ) const
111{
112 if ( mRowSrcToProxyMap.isEmpty() )
113 { // no row mapping set, all rows are passed down:
114 return true;
115 } else {
117 Q_ASSERT( mRowSrcToProxyMap.size() == sourceModel()->rowCount(mRootIndex) );
118 if ( mRowSrcToProxyMap[sourceRow] == -1 )
119 { // this row is explicitly not accepted:
120 return false;
121 } else {
122 Q_ASSERT( mRowSrcToProxyMap[sourceRow] >= 0
123 && mRowSrcToProxyMap[sourceRow] < mRowSrcToProxyMap.size() );
124 return true;
125 }
126 }
127}
128
130 const QModelIndex & ) const
131{
132 if ( mColSrcToProxyMap.isEmpty() )
133 { // no column mapping set up yet, all columns are passed down:
134 return true;
135 } else {
137 Q_ASSERT( mColSrcToProxyMap.size() == sourceModel()->columnCount(mRootIndex) );
138 if ( mColSrcToProxyMap[sourceColumn] == -1 )
139 { // this column is explicitly not accepted:
140 return false;
141 } else {
142 Q_ASSERT( mColSrcToProxyMap[sourceColumn] >= 0
143 && mColSrcToProxyMap[sourceColumn] < mColSrcToProxyMap.size() );
144 return true;
145 }
146 }
147}
148
149int DatasetProxyModel::mapProxyRowToSource( const int& proxyRow ) const
150{
151 if ( mRowProxyToSrcMap.isEmpty() )
152 { // if no row mapping is set, we pass down the row:
153 return proxyRow;
154 } else {
155 Q_ASSERT( proxyRow >= 0 && proxyRow < mRowProxyToSrcMap.size() );
156 return mRowProxyToSrcMap[ proxyRow ];
157 }
158}
159
160int DatasetProxyModel::mapProxyColumnToSource( const int& proxyColumn ) const
161{
162 if ( mColProxyToSrcMap.isEmpty() )
163 { // if no column mapping is set, we pass down the column:
164 return proxyColumn;
165 } else {
166 Q_ASSERT( proxyColumn >= 0 && proxyColumn < mColProxyToSrcMap.size() );
167 return mColProxyToSrcMap[ proxyColumn ];
168 }
169}
170
171int DatasetProxyModel::mapSourceRowToProxy( const int& sourceRow ) const
172{
173 if ( mRowSrcToProxyMap.isEmpty() )
174 {
175 return sourceRow;
176 } else {
177 Q_ASSERT( sourceRow >= 0 && sourceRow < mRowSrcToProxyMap.size() );
178 return mRowSrcToProxyMap[sourceRow];
179 }
180}
181
182int DatasetProxyModel::mapSourceColumnToProxy( const int& sourceColumn ) const
183{
184 if ( mColSrcToProxyMap.isEmpty() )
185 {
186 return sourceColumn;
187 } else {
188 Q_ASSERT( sourceColumn >= 0 && sourceColumn < mColSrcToProxyMap.size() );
189 return mColSrcToProxyMap.at( sourceColumn ) ;
190 }
191}
192
194{
195 mRowSrcToProxyMap.clear();
196 mRowProxyToSrcMap.clear();
197 mColSrcToProxyMap.clear();
198 mColProxyToSrcMap.clear();
199 invalidate();
200}
201
202QVariant DatasetProxyModel::data(const QModelIndex &index, int role) const
203{
204 return sourceModel()->data( mapToSource( index ), role );
205}
206
207bool DatasetProxyModel::setData( const QModelIndex& index, const QVariant& value, int role )
208{
209 return sourceModel()->setData( mapToSource( index ), value, role );
210}
211
212QVariant DatasetProxyModel::headerData( int section, Qt::Orientation orientation, int role ) const
213{
214 if ( orientation == Qt::Horizontal )
215 {
216 if ( mapProxyColumnToSource ( section ) == -1 )
217 {
218 return QVariant();
219 } else {
220 return sourceModel()->headerData( mapProxyColumnToSource( section ), orientation, role );
221 }
222 } else {
223 if ( mapProxyRowToSource ( section ) == -1 )
224 {
225 return QVariant();
226 } else {
227 return sourceModel()->headerData( mapProxyRowToSource ( section ), orientation, role );
228 }
229 }
230}
231
232void DatasetProxyModel::initializeDatasetDecriptors(
233 const DatasetDescriptionVector& inConfiguration,
234 const int sourceCount,
235 DatasetDescriptionVector& outSourceToProxyMap,
236 DatasetDescriptionVector& outProxyToSourceMap )
237{
238 // in the current mapping implementation, the proxy-to-source map is
239 // identical to the configuration vector:
241 outSourceToProxyMap.fill( -1, sourceCount );
242
243 for ( int index = 0; index < inConfiguration.size(); ++index ) {
244 // make sure the values in inConfiguration point to columns in the
245 // source model:
246
247 if ( inConfiguration[index] == -1 ) {
248 continue;
249 }
250
251 Q_ASSERT_X( inConfiguration[ index ] >= 0 && inConfiguration[ index ] < sourceCount,
252 "DatasetProxyModel::initializeDatasetDecriptors",
253 "column index outside of source model" );
255 "DatasetProxyModel::initializeDatasetDecriptors",
256 "no duplicates allowed in mapping configuration, mapping has to be reversible" );
257
258 outSourceToProxyMap[ inConfiguration[ index ] ] = index;
259 }
260}
261
277
279{
280 mRootIndex = rootIdx;
282}
283
void setDatasetDescriptionVectors(const KChart::DatasetDescriptionVector &rowConfig, const KChart::DatasetDescriptionVector &columnConfig)
Convenience method to configure rows and columns in one step.
QVariant data(const QModelIndex &index, int role) const override
Overloaded from base class.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Implements the mapping from the proxy to the source indexes.
bool filterAcceptsColumn(int sourceColumn, const QModelIndex &) const override
Decide whether the column is accepted.
DatasetProxyModel(QObject *parent=nullptr)
Create a DatasetProxyModel.
void setSourceRootIndex(const QModelIndex &rootIdx)
Set the root index of the table in the source model.
void setDatasetRowDescriptionVector(const KChart::DatasetDescriptionVector &rowConfig)
Configure the dataset selection for the rows.
void setSourceModel(QAbstractItemModel *sourceModel) override
Overloaded from base class.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Overloaded from base class.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Overloaded from base class.
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
Implements the mapping from the source to the proxy indexes.
void setDatasetColumnDescriptionVector(const KChart::DatasetDescriptionVector &columnConfig)
Configure the dataset selection for the columns.
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
Decide whether the row is accepted.
void resetDatasetDescriptions()
Reset all dataset description.
QModelIndex createIndex(int row, int column, const void *ptr) const const
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QObject * parent() const const
T qobject_cast(QObject *object)
virtual int columnCount(const QModelIndex &parent) const const override
virtual int rowCount(const QModelIndex &parent) const const override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
typedef ItemFlags
Orientation
void * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.