KChart

KChartDatasetProxyModel.h
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#ifndef KCHARTDATASETPROXYMODEL_H
10#define KCHARTDATASETPROXYMODEL_H
11
12#include <QVector>
13#include <QSortFilterProxyModel>
14
15#include "kchart_export.h"
16
17namespace KChart {
18
19 class IndexOutOfBoundsException;
20
21 typedef QVector<int> DatasetDescriptionVector;
22
23 /** DatasetProxyModel takes a KChart dataset configuration and translates
24 it into a filtering proxy model.
25
26 The resulting model will only contain the part of the model that is
27 selected by the dataset, and the according row and column header
28 data.
29
30 Currently, this model is implemented for table models only. The way it
31 would work with models representing a tree is to be decided.
32
33 The column selection is configured by passing a dataset description
34 vector to the model. This vector (of integers) is supposed to have one
35 value for each column of the original model. If the value at position
36 x is -1, column x of the original model is not included in the
37 dataset. If it is between 0 and (columnCount() -1), it is the column
38 the source column is mapped to in the resulting model. Any other value
39 is an error.
40 */
41 class KCHART_EXPORT DatasetProxyModel : public QSortFilterProxyModel
42 {
43 Q_OBJECT
44 public:
45 /** Create a DatasetProxyModel.
46 Without further configuration, this model is invalid.
47 @see setDatasetDescriptionVector
48 */
49 explicit DatasetProxyModel ( QObject* parent = nullptr );
50
51 QModelIndex buddy( const QModelIndex& index ) const override;
52
53 Qt::ItemFlags flags( const QModelIndex& index ) const override;
54
55 QModelIndex index( int row, int column,
56 const QModelIndex &parent = QModelIndex() ) const override;
57 QModelIndex parent(const QModelIndex &child ) const override;
58
59 /** Implements the mapping from the source to the proxy indexes. */
60 QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const override;
61
62 /** Implements the mapping from the proxy to the source indexes. */
63 QModelIndex mapToSource ( const QModelIndex& proxyIndex ) const override;
64
65 /** Overloaded from base class. */
66 QVariant data(const QModelIndex &index, int role) const override;
67
68 /** Overloaded from base class. */
69 bool setData( const QModelIndex& index, const QVariant& value, int role ) override;
70
71 /** Overloaded from base class. */
72 QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
73
74 /** Overloaded from base class. */
75 void setSourceModel(QAbstractItemModel *sourceModel) override;
76
77 /** Set the root index of the table in
78 the source model */
79 void setSourceRootIndex(const QModelIndex& rootIdx);
80
81
82 public Q_SLOTS:
83 /** Reset all dataset description.
84 After that, the result of the proxying is an empty model (a new
85 dataset description needs to be set to achieve a non-empty result).
86 */
87 void resetDatasetDescriptions();
88
89 /** Configure the dataset selection for the columns.
90 Every call to this method resets the previous dataset
91 description.
92 */
93 void setDatasetColumnDescriptionVector ( const KChart::DatasetDescriptionVector& columnConfig );
94
95 /** Configure the dataset selection for the rows.
96 Every call to this method resets the previous dataset
97 description.
98 */
99 void setDatasetRowDescriptionVector ( const KChart::DatasetDescriptionVector& rowConfig );
100
101 /** Convenience method to configure rows and columns in one step. */
102 void setDatasetDescriptionVectors (
103 const KChart::DatasetDescriptionVector& rowConfig,
104 const KChart::DatasetDescriptionVector& columnConfig );
105
106 // FIXME: add convenience methods to configure common dataset
107 // selections (like rectangular areas etc)
108
109 protected:
110 /** Decide whether the column is accepted. */
111 bool filterAcceptsColumn ( int sourceColumn,
112 const QModelIndex & ) const override;
113
114
115 /** Decide whether the row is accepted. */
116 bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const override;
117
118 private:
119
120 /** Map a proxy column to a source column. */
121 int mapProxyColumnToSource ( const int& proxyColumn ) const;
122
123 /** Map a source column to a proxy column. */
124 int mapSourceColumnToProxy ( const int& sourceColumn ) const;
125
126 /** Map a proxy row to a source row. */
127 int mapProxyRowToSource ( const int& proxyRow ) const;
128
129 /** Map a source row to a proxy row. */
130 int mapSourceRowToProxy ( const int& sourceRow ) const;
131
132 /** Initialize the transformation vectors from the dataset
133 description.
134
135 The input parameter "Configuration" is a vector that specifies
136 what srce column will be mapped to what proxy column. Example:
137
138 position: [0][1][2]
139 value: [2][0][1]
140
141 This will map the source column 2 to proxy column 0, source 0 to
142 proxy 1, and source 1 to proxy 2. Source needs to have at least 2
143 column. The source-to-proxy mapping looks the same, except that it
144 may contain values of -1, which means this column is not part of
145 the resulting model. The values in the configuration vector must
146 be unique (otherwise, a 1-to-1 mapping in both directions is
147 impossible).
148
149 sourceCount is the number of columns in the source model. The proxy-to-source map has
150 as many elements as the proxy has columns, the source-to-proxy map
151 has as many elements as the source has columns. Same goes for rows
152 (the mapping logic is the same).
153
154 */
155 void initializeDatasetDecriptors (
156 const DatasetDescriptionVector& inConfiguration,
157 int sourceCount,
158 DatasetDescriptionVector& outSourceToProxyMap,
159 DatasetDescriptionVector& outProxyToSourceMap );
160
161 DatasetDescriptionVector mColSrcToProxyMap;
162 DatasetDescriptionVector mColProxyToSrcMap;
163 DatasetDescriptionVector mRowSrcToProxyMap;
164 DatasetDescriptionVector mRowProxyToSrcMap;
165
166 int mProxyRowCount;
167 int mProxyColumnCount;
168 QModelIndex mRootIndex;
169 };
170
171}
172
173
174#endif
DatasetProxyModel takes a KChart dataset configuration and translates it into a filtering proxy model...
DisplayRole
typedef ItemFlags
Orientation
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.