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

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