1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QtCharts/QVBarModelMapper>
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 \class QVBarModelMapper
10 \inmodule QtCharts
11 \brief The QVBarModelMapper class is a vertical model mapper for bar series.
12
13 Model mappers enable using a data model derived from the QAbstractItemModel class
14 as a data source for a chart. A vertical model mapper is used to create a connection
15 between a data model and QAbstractBarSeries, so that each column in the data model
16 defines a bar set and each row maps to a category in a bar series.
17
18 Both model and bar series properties can be used to manipulate the data. The model mapper
19 keeps the bar series and the data model in sync.
20
21 The model mapper ensures that all the bar sets in the bar series have equal sizes.
22 Therefore, adding or removing a value from a bar set causes the same change to be
23 made in all the bar sets in the bar series.
24
25 For more information, see \l{Charts with Widgets Gallery}.
26
27 \sa QHBarModelMapper
28*/
29/*!
30 \qmltype VBarModelMapper
31 \instantiates QVBarModelMapper
32 \inqmlmodule QtCharts
33
34 \inherits BarModelMapper
35
36 \brief Vertical model mapper for bar series.
37
38 The VBarModelMapper type enables using a data model derived from the QAbstractItemModel
39 class as a data source for a chart. A vertical model mapper is used to create a connection
40 between a data model and QAbstractBarSeries, so that each column in the data model
41 defines a bar set and each row maps to a category in a bar series. You need to implement
42 the data model and expose it to QML.
43
44 Both model and bar series properties can be used to manipulate the data. The model mapper
45 keeps the bar series and the data model in sync.
46
47 The model mapper ensures that all the bar sets in the bar series have equal sizes.
48 Therefore, adding or removing a value from a bar set causes the same change to be
49 made in all the bar sets in the bar series.
50
51 The following QML code snippet creates a bar series with three bar sets (assuming the model
52 has at least four columns). Each bar set contains data starting from row 1. The name
53 of a bar set is defined by the column header.
54 \code
55 BarSeries {
56 VBarModelMapper {
57 model: myCustomModel // QAbstractItemModel derived implementation
58 firstBarSetColumn: 1
59 lastBarSetColumn: 3
60 firstRow: 1
61 }
62 }
63 \endcode
64
65 \sa HBarModelMapper
66*/
67
68/*!
69 \property QVBarModelMapper::series
70 \brief The bar series that is used by the mapper.
71
72 All the data in the series is discarded when it is set to the mapper.
73 When a new series is specified, the old series is disconnected (but it preserves its data).
74*/
75/*!
76 \qmlproperty AbstractBarSeries VBarModelMapper::series
77 The bar series that is used by the mapper. All the data in the series is discarded when it is
78 set to the mapper. When the new series is specified, the old series is disconnected (but it
79 preserves its data).
80*/
81
82/*!
83 \property QVBarModelMapper::model
84 \brief The data model that is used by the mapper.
85*/
86/*!
87 \qmlproperty SomeModel VBarModelMapper::model
88 The data model that is used by the mapper. You need to implement the model and expose it to QML.
89
90 \note The model has to support adding and removing rows or columns and modifying
91 the data in the cells.
92*/
93
94/*!
95 \property QVBarModelMapper::firstBarSetColumn
96 \brief The column of the model that is used as the data source for the first bar set.
97
98 The default value is -1 (invalid mapping).
99*/
100/*!
101 \qmlproperty int VBarModelMapper::firstBarSetColumn
102 The column of the model that is used as the data source for the first bar set. The default value
103 is -1 (invalid mapping).
104*/
105
106/*!
107 \property QVBarModelMapper::lastBarSetColumn
108 \brief The column of the model that is used as the data source for the last bar set.
109
110 The default value is -1 (invalid mapping).
111*/
112/*!
113 \qmlproperty int VBarModelMapper::lastBarSetColumn
114 The column of the model that is used as the data source for the last bar set. The default
115 value is -1 (invalid mapping).
116*/
117
118/*!
119 \property QVBarModelMapper::firstRow
120 \brief The row of the model that contains the first values of the bar sets in the bar series.
121
122 The minimum and default value is 0.
123*/
124/*!
125 \qmlproperty int VBarModelMapper::firstRow
126 The row of the model that contains the first values of the bar sets in the bar series.
127 The default value is 0.
128*/
129
130/*!
131 \property QVBarModelMapper::rowCount
132 \brief The number of rows of the model that are mapped as the data for the bar series.
133
134 The minimum and default value is -1 (number limited to the number of rows in the model).
135*/
136/*!
137 \qmlproperty int VBarModelMapper::rowCount
138 The number of rows of the model that are mapped as the data for the bar series. The default
139 value is -1 (number limited to the number of rows in the model).
140*/
141
142/*!
143 \fn void QVBarModelMapper::seriesReplaced()
144
145 This signal is emitted when the bar series that the mapper is connected to changes.
146*/
147
148/*!
149 \fn void QVBarModelMapper::modelReplaced()
150
151 This signal is emitted when the model that the mapper is connected to changes.
152*/
153
154/*!
155 \fn void QVBarModelMapper::firstBarSetColumnChanged()
156 This signal is emitted when the first bar set column changes.
157*/
158
159/*!
160 \fn void QVBarModelMapper::lastBarSetColumnChanged()
161 This signal is emitted when the last bar set column changes.
162*/
163
164/*!
165 \fn void QVBarModelMapper::firstRowChanged()
166 This signal is emitted when the first row changes.
167*/
168
169/*!
170 \fn void QVBarModelMapper::rowCountChanged()
171 This signal is emitted when the number of rows changes.
172*/
173
174/*!
175 Constructs a mapper object that is a child of \a parent.
176*/
177QVBarModelMapper::QVBarModelMapper(QObject *parent) :
178 QBarModelMapper(parent)
179{
180 QBarModelMapper::setOrientation(Qt::Vertical);
181}
182
183QAbstractItemModel *QVBarModelMapper::model() const
184{
185 return QBarModelMapper::model();
186}
187
188void QVBarModelMapper::setModel(QAbstractItemModel *model)
189{
190 if (model != QBarModelMapper::model()) {
191 QBarModelMapper::setModel(model);
192 emit modelReplaced();
193 }
194}
195
196QAbstractBarSeries *QVBarModelMapper::series() const
197{
198 return QBarModelMapper::series();
199}
200
201void QVBarModelMapper::setSeries(QAbstractBarSeries *series)
202{
203 if (series != QBarModelMapper::series()) {
204 QBarModelMapper::setSeries(series);
205 emit seriesReplaced();
206 }
207}
208
209int QVBarModelMapper::firstBarSetColumn() const
210{
211 return QBarModelMapper::firstBarSetSection();
212}
213
214void QVBarModelMapper::setFirstBarSetColumn(int firstBarSetColumn)
215{
216 if (firstBarSetColumn != firstBarSetSection()) {
217 QBarModelMapper::setFirstBarSetSection(firstBarSetColumn);
218 emit firstBarSetColumnChanged();
219 }
220}
221
222int QVBarModelMapper::lastBarSetColumn() const
223{
224 return QBarModelMapper::lastBarSetSection();
225}
226
227void QVBarModelMapper::setLastBarSetColumn(int lastBarSetColumn)
228{
229 if (lastBarSetColumn != lastBarSetSection()) {
230 QBarModelMapper::setLastBarSetSection(lastBarSetColumn);
231 emit lastBarSetColumnChanged();
232 }
233}
234
235int QVBarModelMapper::firstRow() const
236{
237 return QBarModelMapper::first();
238}
239
240void QVBarModelMapper::setFirstRow(int firstRow)
241{
242 if (firstRow != first()) {
243 QBarModelMapper::setFirst(firstRow);
244 emit firstRowChanged();
245 }
246}
247
248int QVBarModelMapper::rowCount() const
249{
250 return QBarModelMapper::count();
251}
252
253void QVBarModelMapper::setRowCount(int rowCount)
254{
255 if (rowCount != count()) {
256 QBarModelMapper::setCount(rowCount);
257 emit rowCountChanged();
258 }
259}
260
261QT_END_NAMESPACE
262
263#include "moc_qvbarmodelmapper.cpp"
264

source code of qtcharts/src/charts/barchart/qvbarmodelmapper.cpp