1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include <QtCharts/QVPieModelMapper>
31
32QT_CHARTS_BEGIN_NAMESPACE
33
34/*!
35 \class QVPieModelMapper
36 \inmodule QtCharts
37 \brief The QVPieModelMapper is a vertical model mapper for pie series.
38
39 Model mappers enable using a data model derived from the QAbstractItemModel class
40 as a data source for a chart. A vertical model mapper is used to create a connection
41 between a data model and QPieSeries, so that each row in the data model defines a
42 pie slice and each column maps to the label or the value of the pie slice.
43
44 Both model and pie series properties can be used to manipulate the data. The model
45 mapper keeps the pie series and the data model in sync.
46*/
47/*!
48 \qmltype VPieModelMapper
49 \instantiates QVPieModelMapper
50 \inqmlmodule QtCharts
51
52 \brief Vertical model mapper for pie series.
53
54 Model mappers enable using a data model derived from the QAbstractItemModel class
55 as a data source for a chart. A vertical model mapper is used to create a connection
56 between a data model and PieSeries, so that each row in the data model defines a
57 pie slice and each column maps to the label or the value of the pie slice.
58
59 Both model and pie series properties can be used to manipulate the data. The model
60 mapper keeps the pie series and the data model in sync.
61
62 The following QML example creates a pie series with four slices (assuming the model has at
63 least five rows). Each slice gets a label from column 1 and a value from column 2.
64 \code
65 VPieModelMapper {
66 series: pieSeries
67 model: customModel
68 labelsColumn: 1
69 valuesColumn: 2
70 firstRow: 1
71 rowCount: 4
72 }
73 \endcode
74*/
75
76/*!
77 \property QVPieModelMapper::series
78 \brief The pie series that is used by the mapper.
79
80 All the data in the series is discarded when it is set to the mapper.
81 When a new series is specified, the old series is disconnected (but it preserves its data).
82*/
83/*!
84 \qmlproperty PieSeries VPieModelMapper::series
85 The pie series that is used by the mapper. If you define the mapper element as a child for a
86 PieSeries, leave this property undefined. All the data in the series is discarded when it is set to the mapper.
87 When new series is specified the old series is disconnected (but it preserves its data).
88*/
89
90/*!
91 \property QVPieModelMapper::model
92 \brief The model that is used by the mapper.
93*/
94/*!
95 \qmlproperty SomeModel VPieModelMapper::model
96 The QAbstractItemModel based model that is used by the mapper. You need to implement the model
97 and expose it to QML.
98
99 \note The model has to support adding and removing rows or columns and modifying
100 the data in the cells.
101*/
102
103/*!
104 \property QVPieModelMapper::valuesColumn
105 \brief The column of the model that is kept in sync with the values of the pie's slices.
106
107 The default value is -1 (invalid mapping).
108*/
109/*!
110 \qmlproperty int VPieModelMapper::valuesColumn
111 The column of the model that is kept in sync with the values of the pie's slices.
112 The default value is -1 (invalid mapping).
113*/
114
115/*!
116 \property QVPieModelMapper::labelsColumn
117 \brief The column of the model that is kept in sync with the labels of the pie's slices.
118
119 The default value is -1 (invalid mapping).
120*/
121/*!
122 \qmlproperty int VPieModelMapper::labelsColumn
123 The column of the model that is kept in sync with the labels of the pie's slices.
124 The default value is -1 (invalid mapping).
125*/
126
127/*!
128 \property QVPieModelMapper::firstRow
129 \brief The row of the model that contains the first slice value.
130
131 The minimum and default value is 0.
132*/
133/*!
134 \qmlproperty int VPieModelMapper::firstRow
135 The row of the model that contains the first slice value.
136 The default value is 0.
137*/
138
139/*!
140 \property QVPieModelMapper::rowCount
141 \brief The number of rows of the model that are mapped as the data for a pie series.
142
143 The minimum and default value is -1 (number limited by the number of rows in the model).
144*/
145/*!
146 \qmlproperty int VPieModelMapper::rowCount
147 The number of rows of the model that are mapped as the data for a pie series.
148 The default value is -1 (number limited by the number of rows in the model).
149*/
150
151/*!
152 \fn void QVPieModelMapper::seriesReplaced()
153
154 This signal is emitted when the series that the mapper is connected to changes.
155*/
156
157/*!
158 \fn void QVPieModelMapper::modelReplaced()
159
160 This signal is emitted when the model that the mapper is connected to changes.
161*/
162
163/*!
164 \fn void QVPieModelMapper::valuesColumnChanged()
165
166 This signal is emitted when the values column changes.
167*/
168
169/*!
170 \fn void QVPieModelMapper::labelsColumnChanged()
171
172 This signal is emitted when the labels column changes.
173*/
174
175/*!
176 \fn void QVPieModelMapper::firstRowChanged()
177 This signal is emitted when the first row changes.
178*/
179
180/*!
181 \fn void QVPieModelMapper::rowCountChanged()
182 This signal is emitted when the number of rows changes.
183*/
184
185/*!
186 Constructs a mapper object that is a child of \a parent.
187*/
188QVPieModelMapper::QVPieModelMapper(QObject *parent) :
189 QPieModelMapper(parent)
190{
191 QPieModelMapper::setOrientation(Qt::Vertical);
192}
193
194QAbstractItemModel *QVPieModelMapper::model() const
195{
196 return QPieModelMapper::model();
197}
198
199void QVPieModelMapper::setModel(QAbstractItemModel *model)
200{
201 if (model != QPieModelMapper::model()) {
202 QPieModelMapper::setModel(model);
203 emit modelReplaced();
204 }
205}
206
207QPieSeries *QVPieModelMapper::series() const
208{
209 return QPieModelMapper::series();
210}
211
212void QVPieModelMapper::setSeries(QPieSeries *series)
213{
214 if (series != QPieModelMapper::series()) {
215 QPieModelMapper::setSeries(series);
216 emit seriesReplaced();
217 }
218}
219
220/*!
221 Returns the column of the model that is kept in sync with the values of the pie's slices.
222*/
223int QVPieModelMapper::valuesColumn() const
224{
225 return QPieModelMapper::valuesSection();
226}
227
228/*!
229 Sets the model column that is kept in sync with the pie slices' values to \a valuesColumn.
230*/
231void QVPieModelMapper::setValuesColumn(int valuesColumn)
232{
233 if (valuesColumn != valuesSection()) {
234 QPieModelMapper::setValuesSection(valuesColumn);
235 emit valuesColumnChanged();
236 }
237}
238
239/*!
240 Returns the column of the model that is kept in sync with the labels of the pie's slices.
241*/
242int QVPieModelMapper::labelsColumn() const
243{
244 return QPieModelMapper::labelsSection();
245}
246
247/*!
248 Sets the model column that is kept in sync with the pies slices' labels to \a labelsColumn.
249*/
250void QVPieModelMapper::setLabelsColumn(int labelsColumn)
251{
252 if (labelsColumn != labelsSection()) {
253 QPieModelMapper::setLabelsSection(labelsColumn);
254 emit labelsColumnChanged();
255 }
256}
257
258int QVPieModelMapper::firstRow() const
259{
260 return first();
261}
262
263void QVPieModelMapper::setFirstRow(int firstRow)
264{
265 if (firstRow != first()) {
266 setFirst(firstRow);
267 emit firstRowChanged();
268 }
269}
270
271int QVPieModelMapper::rowCount() const
272{
273 return count();
274}
275
276void QVPieModelMapper::setRowCount(int rowCount)
277{
278 if (rowCount != count()) {
279 setCount(rowCount);
280 emit rowCountChanged();
281 }
282}
283
284QT_CHARTS_END_NAMESPACE
285
286#include "moc_qvpiemodelmapper.cpp"
287

source code of qtcharts/src/charts/piechart/qvpiemodelmapper.cpp