##// END OF EJS Templates
changed barset name to label to be consistent with pie series. Series have names, barsets and pieslices have labels
sauimone -
r1429:39a4e8ac4330
parent child
Show More
@@ -1,451 +1,451
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarmodelmapper.h"
21 #include "qbarmodelmapper.h"
22 #include "qbarmodelmapper_p.h"
22 #include "qbarmodelmapper_p.h"
23 #include "qbarseries.h"
23 #include "qbarseries.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qchart.h"
25 #include "qchart.h"
26 #include <QAbstractItemModel>
26 #include <QAbstractItemModel>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 /*!
30 /*!
31 \class QBarModelMapper
31 \class QBarModelMapper
32 \brief part of QtCommercial chart API.
32 \brief part of QtCommercial chart API.
33 \mainclass
33 \mainclass
34
34
35 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
35 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
36 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
36 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
37 Curently it is NOT possible to use both QAbstractItemModel and QBarSeries model API.
37 Curently it is NOT possible to use both QAbstractItemModel and QBarSeries model API.
38 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
38 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
39 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
39 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
40 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
40 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
41 */
41 */
42
42
43 /*!
43 /*!
44 \property QBarModelMapper::series
44 \property QBarModelMapper::series
45 \brief Defines the QPieSeries object that is used by the mapper.
45 \brief Defines the QPieSeries object that is used by the mapper.
46
46
47 All the data in the series is discarded when it is set to the mapper.
47 All the data in the series is discarded when it is set to the mapper.
48 When new series is specified the old series is disconnected (it preserves its data)
48 When new series is specified the old series is disconnected (it preserves its data)
49 */
49 */
50
50
51 /*!
51 /*!
52 \property QBarModelMapper::model
52 \property QBarModelMapper::model
53 \brief Defines the model that is used by the mapper.
53 \brief Defines the model that is used by the mapper.
54 */
54 */
55
55
56 /*!
56 /*!
57 \property QBarModelMapper::first
57 \property QBarModelMapper::first
58 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
58 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
59
59
60 Minimal and default value is: 0
60 Minimal and default value is: 0
61 */
61 */
62
62
63 /*!
63 /*!
64 \property QBarModelMapper::count
64 \property QBarModelMapper::count
65 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
65 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
66
66
67 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
67 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
68 */
68 */
69
69
70 /*!
70 /*!
71 Constructs a mapper object which is a child of \a parent.
71 Constructs a mapper object which is a child of \a parent.
72 */
72 */
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
74 QObject(parent),
74 QObject(parent),
75 d_ptr(new QBarModelMapperPrivate(this))
75 d_ptr(new QBarModelMapperPrivate(this))
76 {
76 {
77 }
77 }
78
78
79 QAbstractItemModel* QBarModelMapper::model() const
79 QAbstractItemModel* QBarModelMapper::model() const
80 {
80 {
81 Q_D(const QBarModelMapper);
81 Q_D(const QBarModelMapper);
82 return d->m_model;
82 return d->m_model;
83 }
83 }
84
84
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
86 {
86 {
87 if (model == 0)
87 if (model == 0)
88 return;
88 return;
89
89
90 Q_D(QBarModelMapper);
90 Q_D(QBarModelMapper);
91 if (d->m_model) {
91 if (d->m_model) {
92 disconnect(d->m_model, 0, d, 0);
92 disconnect(d->m_model, 0, d, 0);
93 }
93 }
94
94
95 d->m_model = model;
95 d->m_model = model;
96 d->initializeBarFromModel();
96 d->initializeBarFromModel();
97 // connect signals from the model
97 // connect signals from the model
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
99 connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int)));
99 connect(d->m_model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), d, SLOT(modelHeaderDataUpdated(Qt::Orientation,int,int)));
100 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
100 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
103 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
103 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
104 }
104 }
105
105
106 QBarSeries* QBarModelMapper::series() const
106 QBarSeries* QBarModelMapper::series() const
107 {
107 {
108 Q_D(const QBarModelMapper);
108 Q_D(const QBarModelMapper);
109 return d->m_series;
109 return d->m_series;
110 }
110 }
111
111
112 void QBarModelMapper::setSeries(QBarSeries *series)
112 void QBarModelMapper::setSeries(QBarSeries *series)
113 {
113 {
114 Q_D(QBarModelMapper);
114 Q_D(QBarModelMapper);
115 if (d->m_series) {
115 if (d->m_series) {
116 disconnect(d->m_series, 0, d, 0);
116 disconnect(d->m_series, 0, d, 0);
117 }
117 }
118
118
119 if (series == 0)
119 if (series == 0)
120 return;
120 return;
121
121
122 d->m_series = series;
122 d->m_series = series;
123 d->initializeBarFromModel();
123 d->initializeBarFromModel();
124 // connect the signals from the series
124 // connect the signals from the series
125 // TODO: TO be implemented
125 // TODO: TO be implemented
126 }
126 }
127
127
128 int QBarModelMapper::first() const
128 int QBarModelMapper::first() const
129 {
129 {
130 Q_D(const QBarModelMapper);
130 Q_D(const QBarModelMapper);
131 return d->m_first;
131 return d->m_first;
132 }
132 }
133
133
134 void QBarModelMapper::setFirst(int first)
134 void QBarModelMapper::setFirst(int first)
135 {
135 {
136 Q_D(QBarModelMapper);
136 Q_D(QBarModelMapper);
137 d->m_first = qMax(first, 0);
137 d->m_first = qMax(first, 0);
138 d->initializeBarFromModel();
138 d->initializeBarFromModel();
139 }
139 }
140
140
141 int QBarModelMapper::count() const
141 int QBarModelMapper::count() const
142 {
142 {
143 Q_D(const QBarModelMapper);
143 Q_D(const QBarModelMapper);
144 return d->m_count;
144 return d->m_count;
145 }
145 }
146
146
147 void QBarModelMapper::setCount(int count)
147 void QBarModelMapper::setCount(int count)
148 {
148 {
149 Q_D(QBarModelMapper);
149 Q_D(QBarModelMapper);
150 d->m_count = qMax(count, -1);
150 d->m_count = qMax(count, -1);
151 d->initializeBarFromModel();
151 d->initializeBarFromModel();
152 }
152 }
153
153
154 /*!
154 /*!
155 Returns the orientation that is used when QBarModelMapper accesses the model.
155 Returns the orientation that is used when QBarModelMapper accesses the model.
156 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
156 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
157 or from columns (Qt::Vertical)
157 or from columns (Qt::Vertical)
158 */
158 */
159 Qt::Orientation QBarModelMapper::orientation() const
159 Qt::Orientation QBarModelMapper::orientation() const
160 {
160 {
161 Q_D(const QBarModelMapper);
161 Q_D(const QBarModelMapper);
162 return d->m_orientation;
162 return d->m_orientation;
163 }
163 }
164
164
165 /*!
165 /*!
166 Returns the \a orientation that is used when QBarModelMapper accesses the model.
166 Returns the \a orientation that is used when QBarModelMapper accesses the model.
167 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
167 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
168 or from columns (Qt::Vertical)
168 or from columns (Qt::Vertical)
169 */
169 */
170 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
170 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
171 {
171 {
172 Q_D(QBarModelMapper);
172 Q_D(QBarModelMapper);
173 d->m_orientation = orientation;
173 d->m_orientation = orientation;
174 d->initializeBarFromModel();
174 d->initializeBarFromModel();
175 }
175 }
176
176
177 /*!
177 /*!
178 Returns which section of the model is used as the data source for the first bar set
178 Returns which section of the model is used as the data source for the first bar set
179 */
179 */
180 int QBarModelMapper::firstBarSetSection() const
180 int QBarModelMapper::firstBarSetSection() const
181 {
181 {
182 Q_D(const QBarModelMapper);
182 Q_D(const QBarModelMapper);
183 return d->m_firstBarSetSection;
183 return d->m_firstBarSetSection;
184 }
184 }
185
185
186 /*!
186 /*!
187 Sets the model section that is used as the data source for the first bar set
187 Sets the model section that is used as the data source for the first bar set
188 Parameter \a firstBarSetSection specifies the section of the model.
188 Parameter \a firstBarSetSection specifies the section of the model.
189 */
189 */
190 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
190 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
191 {
191 {
192 Q_D(QBarModelMapper);
192 Q_D(QBarModelMapper);
193 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
193 d->m_firstBarSetSection = qMax(-1, firstBarSetSection);
194 d->initializeBarFromModel();
194 d->initializeBarFromModel();
195 }
195 }
196
196
197 /*!
197 /*!
198 Returns which section of the model is used as the data source for the last bar set
198 Returns which section of the model is used as the data source for the last bar set
199 */
199 */
200 int QBarModelMapper::lastBarSetSection() const
200 int QBarModelMapper::lastBarSetSection() const
201 {
201 {
202 Q_D(const QBarModelMapper);
202 Q_D(const QBarModelMapper);
203 return d->m_lastBarSetSection;
203 return d->m_lastBarSetSection;
204 }
204 }
205
205
206 /*!
206 /*!
207 Sets the model section that is used as the data source for the last bar set
207 Sets the model section that is used as the data source for the last bar set
208 Parameter \a lastBarSetSection specifies the section of the model.
208 Parameter \a lastBarSetSection specifies the section of the model.
209 */
209 */
210 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
210 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
211 {
211 {
212 Q_D(QBarModelMapper);
212 Q_D(QBarModelMapper);
213 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
213 d->m_lastBarSetSection = qMax(-1, lastBarSetSection);
214 d->initializeBarFromModel();
214 d->initializeBarFromModel();
215 }
215 }
216
216
217 /*!
217 /*!
218 Resets the QBarModelMapper to the default state.
218 Resets the QBarModelMapper to the default state.
219 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
219 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
220 */
220 */
221 void QBarModelMapper::reset()
221 void QBarModelMapper::reset()
222 {
222 {
223 Q_D(QBarModelMapper);
223 Q_D(QBarModelMapper);
224 d->m_first = 0;
224 d->m_first = 0;
225 d->m_count = -1;
225 d->m_count = -1;
226 d->m_firstBarSetSection = -1;
226 d->m_firstBarSetSection = -1;
227 d->m_lastBarSetSection = -1;
227 d->m_lastBarSetSection = -1;
228 d->initializeBarFromModel();
228 d->initializeBarFromModel();
229 }
229 }
230
230
231 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
231 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232
232
233 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
233 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
234 m_series(0),
234 m_series(0),
235 m_model(0),
235 m_model(0),
236 m_first(0),
236 m_first(0),
237 m_count(-1),
237 m_count(-1),
238 m_orientation(Qt::Vertical),
238 m_orientation(Qt::Vertical),
239 m_firstBarSetSection(-1),
239 m_firstBarSetSection(-1),
240 m_lastBarSetSection(-1),
240 m_lastBarSetSection(-1),
241 m_seriesSignalsBlock(false),
241 m_seriesSignalsBlock(false),
242 m_modelSignalsBlock(false),
242 m_modelSignalsBlock(false),
243 q_ptr(q)
243 q_ptr(q)
244 {
244 {
245 }
245 }
246
246
247 void QBarModelMapperPrivate::blockModelSignals(bool block)
247 void QBarModelMapperPrivate::blockModelSignals(bool block)
248 {
248 {
249 m_modelSignalsBlock = block;
249 m_modelSignalsBlock = block;
250 }
250 }
251
251
252 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
252 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
253 {
253 {
254 m_seriesSignalsBlock = block;
254 m_seriesSignalsBlock = block;
255 }
255 }
256
256
257 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
257 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
258 {
258 {
259 if (!index.isValid())
259 if (!index.isValid())
260 return 0;
260 return 0;
261
261
262 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
262 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
263 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
263 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
264 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
264 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
265 return m_series->barSets().at(index.column() - m_firstBarSetSection);
265 return m_series->barSets().at(index.column() - m_firstBarSetSection);
266 // else
266 // else
267 // return 0;
267 // return 0;
268 }
268 }
269 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
269 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
270 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
270 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
271 return m_series->barSets().at(index.row() - m_firstBarSetSection);
271 return m_series->barSets().at(index.row() - m_firstBarSetSection);
272 }
272 }
273 return 0; // This part of model has not been mapped to any slice
273 return 0; // This part of model has not been mapped to any slice
274 }
274 }
275
275
276 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
276 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
277 {
277 {
278 if (m_count != -1 && posInBar >= m_count)
278 if (m_count != -1 && posInBar >= m_count)
279 return QModelIndex(); // invalid
279 return QModelIndex(); // invalid
280
280
281 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
281 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
282 return QModelIndex(); // invalid
282 return QModelIndex(); // invalid
283
283
284 if (m_orientation == Qt::Vertical)
284 if (m_orientation == Qt::Vertical)
285 return m_model->index(posInBar + m_first, barSection);
285 return m_model->index(posInBar + m_first, barSection);
286 else
286 else
287 return m_model->index(barSection, posInBar + m_first);
287 return m_model->index(barSection, posInBar + m_first);
288 }
288 }
289
289
290 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
290 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
291 {
291 {
292 Q_UNUSED(topLeft)
292 Q_UNUSED(topLeft)
293 Q_UNUSED(bottomRight)
293 Q_UNUSED(bottomRight)
294
294
295 if (m_model == 0 || m_series == 0)
295 if (m_model == 0 || m_series == 0)
296 return;
296 return;
297
297
298 if (m_modelSignalsBlock)
298 if (m_modelSignalsBlock)
299 return;
299 return;
300
300
301 blockSeriesSignals();
301 blockSeriesSignals();
302 QModelIndex index;
302 QModelIndex index;
303 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
303 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
304 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
304 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
305 index = topLeft.sibling(row, column);
305 index = topLeft.sibling(row, column);
306 QBarSet* bar = barSet(index);
306 QBarSet* bar = barSet(index);
307 if (bar) {
307 if (bar) {
308 if (m_orientation == Qt::Vertical)
308 if (m_orientation == Qt::Vertical)
309 bar->replace(row - m_first, m_model->data(index).toReal());
309 bar->replace(row - m_first, m_model->data(index).toReal());
310 else
310 else
311 bar->replace(column - m_first, m_model->data(index).toReal());
311 bar->replace(column - m_first, m_model->data(index).toReal());
312 }
312 }
313 }
313 }
314 }
314 }
315 blockSeriesSignals(false);
315 blockSeriesSignals(false);
316 }
316 }
317
317
318 void QBarModelMapperPrivate::modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last)
318 void QBarModelMapperPrivate::modelHeaderDataUpdated(Qt::Orientation orientation, int first, int last)
319 {
319 {
320 if (m_model == 0 || m_series == 0)
320 if (m_model == 0 || m_series == 0)
321 return;
321 return;
322
322
323 if (m_modelSignalsBlock)
323 if (m_modelSignalsBlock)
324 return;
324 return;
325
325
326 blockSeriesSignals();
326 blockSeriesSignals();
327 if (orientation != m_orientation) {
327 if (orientation != m_orientation) {
328 for (int section = first; section <= last; section++) {
328 for (int section = first; section <= last; section++) {
329 if (section >= m_firstBarSetSection && section <= m_lastBarSetSection) {
329 if (section >= m_firstBarSetSection && section <= m_lastBarSetSection) {
330 QBarSet* bar = m_series->barSets().at(section - m_firstBarSetSection);
330 QBarSet* bar = m_series->barSets().at(section - m_firstBarSetSection);
331 if (bar)
331 if (bar)
332 bar->setName(m_model->headerData(section, orientation).toString());
332 bar->setLabel(m_model->headerData(section, orientation).toString());
333 }
333 }
334 }
334 }
335 }
335 }
336 blockSeriesSignals(false);
336 blockSeriesSignals(false);
337 }
337 }
338
338
339 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
339 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
340 {
340 {
341 Q_UNUSED(parent);
341 Q_UNUSED(parent);
342 Q_UNUSED(end)
342 Q_UNUSED(end)
343 if (m_modelSignalsBlock)
343 if (m_modelSignalsBlock)
344 return;
344 return;
345
345
346 blockSeriesSignals();
346 blockSeriesSignals();
347 if (m_orientation == Qt::Vertical)
347 if (m_orientation == Qt::Vertical)
348 // insertData(start, end);
348 // insertData(start, end);
349 initializeBarFromModel();
349 initializeBarFromModel();
350 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
350 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
351 initializeBarFromModel();
351 initializeBarFromModel();
352 blockSeriesSignals(false);
352 blockSeriesSignals(false);
353 }
353 }
354
354
355 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
355 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
356 {
356 {
357 Q_UNUSED(parent);
357 Q_UNUSED(parent);
358 Q_UNUSED(end)
358 Q_UNUSED(end)
359 if (m_modelSignalsBlock)
359 if (m_modelSignalsBlock)
360 return;
360 return;
361
361
362 blockSeriesSignals();
362 blockSeriesSignals();
363 if (m_orientation == Qt::Vertical)
363 if (m_orientation == Qt::Vertical)
364 // removeData(start, end);
364 // removeData(start, end);
365 initializeBarFromModel();
365 initializeBarFromModel();
366 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
366 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
367 initializeBarFromModel();
367 initializeBarFromModel();
368 blockSeriesSignals(false);
368 blockSeriesSignals(false);
369 }
369 }
370
370
371 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
371 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
372 {
372 {
373 Q_UNUSED(parent);
373 Q_UNUSED(parent);
374 Q_UNUSED(end)
374 Q_UNUSED(end)
375 if (m_modelSignalsBlock)
375 if (m_modelSignalsBlock)
376 return;
376 return;
377
377
378 blockSeriesSignals();
378 blockSeriesSignals();
379 if (m_orientation == Qt::Horizontal)
379 if (m_orientation == Qt::Horizontal)
380 // insertData(start, end);
380 // insertData(start, end);
381 initializeBarFromModel();
381 initializeBarFromModel();
382 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
382 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
383 initializeBarFromModel();
383 initializeBarFromModel();
384 blockSeriesSignals(false);
384 blockSeriesSignals(false);
385 }
385 }
386
386
387 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
387 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
388 {
388 {
389 Q_UNUSED(parent);
389 Q_UNUSED(parent);
390 Q_UNUSED(end)
390 Q_UNUSED(end)
391 if (m_modelSignalsBlock)
391 if (m_modelSignalsBlock)
392 return;
392 return;
393
393
394 blockSeriesSignals();
394 blockSeriesSignals();
395 if (m_orientation == Qt::Horizontal)
395 if (m_orientation == Qt::Horizontal)
396 // removeData(start, end);
396 // removeData(start, end);
397 initializeBarFromModel();
397 initializeBarFromModel();
398 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
398 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
399 initializeBarFromModel();
399 initializeBarFromModel();
400 blockSeriesSignals(false);
400 blockSeriesSignals(false);
401 }
401 }
402
402
403 void QBarModelMapperPrivate::insertData(int start, int end)
403 void QBarModelMapperPrivate::insertData(int start, int end)
404 {
404 {
405 Q_UNUSED(end)
405 Q_UNUSED(end)
406 Q_UNUSED(start)
406 Q_UNUSED(start)
407 Q_UNUSED(end)
407 Q_UNUSED(end)
408 // To be implemented
408 // To be implemented
409 }
409 }
410
410
411 void QBarModelMapperPrivate::removeData(int start, int end)
411 void QBarModelMapperPrivate::removeData(int start, int end)
412 {
412 {
413 Q_UNUSED(end)
413 Q_UNUSED(end)
414 Q_UNUSED(start)
414 Q_UNUSED(start)
415 Q_UNUSED(end)
415 Q_UNUSED(end)
416 // To be implemented
416 // To be implemented
417 }
417 }
418
418
419 void QBarModelMapperPrivate::initializeBarFromModel()
419 void QBarModelMapperPrivate::initializeBarFromModel()
420 {
420 {
421 if (m_model == 0 || m_series == 0)
421 if (m_model == 0 || m_series == 0)
422 return;
422 return;
423
423
424 blockSeriesSignals();
424 blockSeriesSignals();
425 // clear current content
425 // clear current content
426 m_series->clear();
426 m_series->clear();
427
427
428 // create the initial bar sets
428 // create the initial bar sets
429 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
429 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
430 int posInBar = 0;
430 int posInBar = 0;
431 QModelIndex barIndex = barModelIndex(i, posInBar);
431 QModelIndex barIndex = barModelIndex(i, posInBar);
432 // check if there is such model index
432 // check if there is such model index
433 if (barIndex.isValid()) {
433 if (barIndex.isValid()) {
434 QBarSet *barSet = new QBarSet(m_model->headerData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical).toString());
434 QBarSet *barSet = new QBarSet(m_model->headerData(i, m_orientation == Qt::Vertical ? Qt::Horizontal : Qt::Vertical).toString());
435 while (barIndex.isValid()) {
435 while (barIndex.isValid()) {
436 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
436 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
437 posInBar++;
437 posInBar++;
438 barIndex = barModelIndex(i, posInBar);
438 barIndex = barModelIndex(i, posInBar);
439 }
439 }
440 m_series->append(barSet);
440 m_series->append(barSet);
441 } else {
441 } else {
442 break;
442 break;
443 }
443 }
444 }
444 }
445 blockSeriesSignals(false);
445 blockSeriesSignals(false);
446 }
446 }
447
447
448 #include "moc_qbarmodelmapper.cpp"
448 #include "moc_qbarmodelmapper.cpp"
449 #include "moc_qbarmodelmapper_p.cpp"
449 #include "moc_qbarmodelmapper_p.cpp"
450
450
451 QTCOMMERCIALCHART_END_NAMESPACE
451 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,684 +1,690
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QBarSeries
34 \class QBarSeries
35 \brief part of QtCommercial chart API.
35 \brief part of QtCommercial chart API.
36 \mainclass
36 \mainclass
37
37
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 shows the x-values.
41 shows the x-values.
42
42
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 \image examples_barchart.png
44 \image examples_barchart.png
45
45
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 */
47 */
48
48
49 /*!
49 /*!
50 \property QBarSeries::barWidth
50 \property QBarSeries::barWidth
51 \brief Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
51 \brief Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
52 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
52 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
53 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
53 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
54 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
54 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
55 because with grouped series it is more logical to set width of whole group and let the chart calculate correct
55 because with grouped series it is more logical to set width of whole group and let the chart calculate correct
56 width for bar.
56 width for bar.
57 \sa QGroupedBarSeries
57 \sa QGroupedBarSeries
58 */
58 */
59
59
60 /*!
60 /*!
61 \property QBarSeries::count
61 \property QBarSeries::count
62 \brief Holds the number of sets in series.
62 \brief Holds the number of sets in series.
63 */
63 */
64
64
65 /*!
65 /*!
66 \property QBarSeries::labelsVisible
66 \property QBarSeries::labelsVisible
67 \brief Defines the visibility of the labels in series
67 \brief Defines the visibility of the labels in series
68 */
68 */
69
69
70 /*!
70 /*!
71 \fn void QBarSeries::clicked(QBarSet *barset, int index)
71 \fn void QBarSeries::clicked(QBarSet *barset, int index)
72
72
73 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
73 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
74 Clicked bar inside set is indexed by \a index
74 Clicked bar inside set is indexed by \a index
75 */
75 */
76
76
77 /*!
77 /*!
78 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
78 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
79
79
80 The signal is emitted if mouse is hovered on top of series.
80 The signal is emitted if mouse is hovered on top of series.
81 Parameter \a barset is the pointer of barset, where hover happened.
81 Parameter \a barset is the pointer of barset, where hover happened.
82 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
82 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
83 */
83 */
84
84
85 /*!
85 /*!
86 \fn void QBarSeries::labelsVisibleChanged()
86 \fn void QBarSeries::labelsVisibleChanged()
87
87
88 This signal is emitted when labels visibility have changed.
88 This signal is emitted when labels visibility have changed.
89
89
90 \sa isLabelsVisible(), setLabelsVisible()
90 \sa isLabelsVisible(), setLabelsVisible()
91 */
91 */
92
92
93 /*!
93 /*!
94 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
94 \fn void QBarSeries::barsetsAdded(QList<QBarSet*> sets)
95
95
96 This signal is emitted when \a sets have been added to the series.
96 This signal is emitted when \a sets have been added to the series.
97
97
98 \sa append(), insert()
98 \sa append(), insert()
99 */
99 */
100
100
101 /*!
101 /*!
102 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
102 \fn void QBarSeries::barsetsRemoved(QList<QBarSet*> sets)
103
103
104 This signal is emitted when \a sets have been removed from the series.
104 This signal is emitted when \a sets have been removed from the series.
105
105
106 \sa remove()
106 \sa remove()
107 */
107 */
108
108
109 /*!
109 /*!
110 Constructs empty QBarSeries.
110 Constructs empty QBarSeries.
111 QBarSeries is QObject which is a child of a \a parent.
111 QBarSeries is QObject which is a child of a \a parent.
112 */
112 */
113 QBarSeries::QBarSeries(QObject *parent) :
113 QBarSeries::QBarSeries(QObject *parent) :
114 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
114 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
115 {
115 {
116 }
116 }
117
117
118 /*!
118 /*!
119 Destructs barseries and owned barsets.
119 Destructs barseries and owned barsets.
120 */
120 */
121 QBarSeries::~QBarSeries()
121 QBarSeries::~QBarSeries()
122 {
122 {
123 Q_D(QBarSeries);
123 Q_D(QBarSeries);
124 if(d->m_dataset){
124 if(d->m_dataset){
125 d->m_dataset->removeSeries(this);
125 d->m_dataset->removeSeries(this);
126 }
126 }
127 }
127 }
128
128
129 /*!
129 /*!
130 \internal
130 \internal
131 */
131 */
132 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
132 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
133 QAbstractSeries(d,parent)
133 QAbstractSeries(d,parent)
134 {
134 {
135 }
135 }
136
136
137 /*!
137 /*!
138 Returns the type of series. Derived classes override this.
138 Returns the type of series. Derived classes override this.
139 */
139 */
140 QAbstractSeries::SeriesType QBarSeries::type() const
140 QAbstractSeries::SeriesType QBarSeries::type() const
141 {
141 {
142 return QAbstractSeries::SeriesTypeBar;
142 return QAbstractSeries::SeriesTypeBar;
143 }
143 }
144
144
145 /*!
145 /*!
146 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
146 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
147 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
147 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
148 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
148 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
149 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
149 Note that with QGroupedBarSeries this value means the width of one group of bars instead of just one bar. This is
150 because with grouped series it is more logical to set widht of whole group and let the chart calculate correct
150 because with grouped series it is more logical to set widht of whole group and let the chart calculate correct
151 width for bar.
151 width for bar.
152 \sa QGroupedBarSeries
152 \sa QGroupedBarSeries
153 */
153 */
154 void QBarSeries::setBarWidth(qreal width)
154 void QBarSeries::setBarWidth(qreal width)
155 {
155 {
156 Q_D(QBarSeries);
156 Q_D(QBarSeries);
157 d->setBarWidth(width);
157 d->setBarWidth(width);
158 emit barWidthChanged();
158 }
159 }
159
160
160 /*!
161 /*!
161 Returns the width of bars.
162 Returns the width of bars.
162 */
163 */
163 qreal QBarSeries::barWidth() const
164 qreal QBarSeries::barWidth() const
164 {
165 {
165 Q_D(const QBarSeries);
166 Q_D(const QBarSeries);
166 return d->barWidth();
167 return d->barWidth();
167 }
168 }
168
169
169 /*!
170 /*!
170 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
171 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
171 Returns true, if appending succeeded.
172 Returns true, if appending succeeded.
172
173
173 */
174 */
174 bool QBarSeries::append(QBarSet *set)
175 bool QBarSeries::append(QBarSet *set)
175 {
176 {
176 Q_D(QBarSeries);
177 Q_D(QBarSeries);
177 bool success = d->append(set);
178 bool success = d->append(set);
178 if (success) {
179 if (success) {
179 QList<QBarSet*> sets;
180 QList<QBarSet*> sets;
180 sets.append(set);
181 sets.append(set);
181 emit barsetsAdded(sets);
182 emit barsetsAdded(sets);
183 emit barsetCountChanged();
182 }
184 }
183 return success;
185 return success;
184 }
186 }
185
187
186 /*!
188 /*!
187 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
189 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
188 Returns true, if set was removed.
190 Returns true, if set was removed.
189 */
191 */
190 bool QBarSeries::remove(QBarSet *set)
192 bool QBarSeries::remove(QBarSet *set)
191 {
193 {
192 Q_D(QBarSeries);
194 Q_D(QBarSeries);
193 bool success = d->remove(set);
195 bool success = d->remove(set);
194 if (success) {
196 if (success) {
195 QList<QBarSet*> sets;
197 QList<QBarSet*> sets;
196 sets.append(set);
198 sets.append(set);
197 emit barsetsRemoved(sets);
199 emit barsetsRemoved(sets);
200 emit barsetCountChanged();
198 }
201 }
199 return success;
202 return success;
200 }
203 }
201
204
202 /*!
205 /*!
203 Adds a list of barsets to series. Takes ownership of \a sets.
206 Adds a list of barsets to series. Takes ownership of \a sets.
204 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
207 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
205 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
208 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
206 and function returns false.
209 and function returns false.
207 */
210 */
208 bool QBarSeries::append(QList<QBarSet* > sets)
211 bool QBarSeries::append(QList<QBarSet* > sets)
209 {
212 {
210 Q_D(QBarSeries);
213 Q_D(QBarSeries);
211 bool success = d->append(sets);
214 bool success = d->append(sets);
212 if (success) {
215 if (success) {
213 emit barsetsAdded(sets);
216 emit barsetsAdded(sets);
217 emit barsetCountChanged();
214 }
218 }
215 return success;
219 return success;
216 }
220 }
217
221
218 /*!
222 /*!
219 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
223 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
220 Returns true, if inserting succeeded.
224 Returns true, if inserting succeeded.
221
225
222 */
226 */
223 bool QBarSeries::insert(int index, QBarSet *set)
227 bool QBarSeries::insert(int index, QBarSet *set)
224 {
228 {
225 Q_D(QBarSeries);
229 Q_D(QBarSeries);
226 bool success = d->insert(index, set);
230 bool success = d->insert(index, set);
227 if (success) {
231 if (success) {
228 QList<QBarSet*> sets;
232 QList<QBarSet*> sets;
229 sets.append(set);
233 sets.append(set);
230 emit barsetsAdded(sets);
234 emit barsetsAdded(sets);
235 emit barsetCountChanged();
231 }
236 }
232 return success;
237 return success;
233 }
238 }
234
239
235 /*!
240 /*!
236 Removes all of the bar sets from the series
241 Removes all of the bar sets from the series
237 */
242 */
238 void QBarSeries::clear()
243 void QBarSeries::clear()
239 {
244 {
240 Q_D(QBarSeries);
245 Q_D(QBarSeries);
241 QList<QBarSet *> sets = barSets();
246 QList<QBarSet *> sets = barSets();
242 bool success = d->remove(sets);
247 bool success = d->remove(sets);
243 if (success) {
248 if (success) {
244 emit barsetsRemoved(sets);
249 emit barsetsRemoved(sets);
250 emit barsetCountChanged();
245 }
251 }
246 }
252 }
247
253
248 /*!
254 /*!
249 Returns number of sets in series.
255 Returns number of sets in series.
250 */
256 */
251 int QBarSeries::barsetCount() const
257 int QBarSeries::barsetCount() const
252 {
258 {
253 Q_D(const QBarSeries);
259 Q_D(const QBarSeries);
254 return d->m_barSets.count();
260 return d->m_barSets.count();
255 }
261 }
256
262
257 /*!
263 /*!
258 Returns a list of sets in series. Keeps ownership of sets.
264 Returns a list of sets in series. Keeps ownership of sets.
259 */
265 */
260 QList<QBarSet*> QBarSeries::barSets() const
266 QList<QBarSet*> QBarSeries::barSets() const
261 {
267 {
262 Q_D(const QBarSeries);
268 Q_D(const QBarSeries);
263 return d->m_barSets;
269 return d->m_barSets;
264 }
270 }
265
271
266 /*!
272 /*!
267 Sets the visibility of labels in series to \a visible
273 Sets the visibility of labels in series to \a visible
268 */
274 */
269 void QBarSeries::setLabelsVisible(bool visible)
275 void QBarSeries::setLabelsVisible(bool visible)
270 {
276 {
271 Q_D(QBarSeries);
277 Q_D(QBarSeries);
272 if (d->m_labelsVisible != visible) {
278 if (d->m_labelsVisible != visible) {
273 d->setLabelsVisible(visible);
279 d->setLabelsVisible(visible);
274 emit labelsVisibleChanged();
280 emit labelsVisibleChanged();
275 }
281 }
276 }
282 }
277
283
278 /*!
284 /*!
279 Returns the visibility of labels
285 Returns the visibility of labels
280 */
286 */
281 bool QBarSeries::isLabelsVisible() const
287 bool QBarSeries::isLabelsVisible() const
282 {
288 {
283 Q_D(const QBarSeries);
289 Q_D(const QBarSeries);
284 return d->m_labelsVisible;
290 return d->m_labelsVisible;
285 }
291 }
286
292
287 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
293 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
288
294
289 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
295 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
290 QAbstractSeriesPrivate(q),
296 QAbstractSeriesPrivate(q),
291 m_barWidth(0.5), // Default value is 50% of category width
297 m_barWidth(0.5), // Default value is 50% of category width
292 m_labelsVisible(false),
298 m_labelsVisible(false),
293 m_visible(true)
299 m_visible(true)
294 {
300 {
295 }
301 }
296
302
297 void QBarSeriesPrivate::setCategories(QStringList categories)
303 void QBarSeriesPrivate::setCategories(QStringList categories)
298 {
304 {
299 m_categories = categories;
305 m_categories = categories;
300 }
306 }
301
307
302 void QBarSeriesPrivate::insertCategory(int index, const QString category)
308 void QBarSeriesPrivate::insertCategory(int index, const QString category)
303 {
309 {
304 m_categories.insert(index, category);
310 m_categories.insert(index, category);
305 emit categoriesUpdated();
311 emit categoriesUpdated();
306 }
312 }
307
313
308 void QBarSeriesPrivate::removeCategory(int index)
314 void QBarSeriesPrivate::removeCategory(int index)
309 {
315 {
310 m_categories.removeAt(index);
316 m_categories.removeAt(index);
311 emit categoriesUpdated();
317 emit categoriesUpdated();
312 }
318 }
313
319
314 int QBarSeriesPrivate::categoryCount() const
320 int QBarSeriesPrivate::categoryCount() const
315 {
321 {
316 if (m_categories.count() > 0) {
322 if (m_categories.count() > 0) {
317 return m_categories.count();
323 return m_categories.count();
318 }
324 }
319
325
320 // No categories defined. return count of longest set.
326 // No categories defined. return count of longest set.
321 int count = 0;
327 int count = 0;
322 for (int i=0; i<m_barSets.count(); i++) {
328 for (int i=0; i<m_barSets.count(); i++) {
323 if (m_barSets.at(i)->count() > count) {
329 if (m_barSets.at(i)->count() > count) {
324 count = m_barSets.at(i)->count();
330 count = m_barSets.at(i)->count();
325 }
331 }
326 }
332 }
327
333
328 return count;
334 return count;
329 }
335 }
330
336
331 QStringList QBarSeriesPrivate::categories() const
337 QStringList QBarSeriesPrivate::categories() const
332 {
338 {
333 if (m_categories.count() > 0) {
339 if (m_categories.count() > 0) {
334 return m_categories;
340 return m_categories;
335 }
341 }
336
342
337 // No categories defined. retun list of indices.
343 // No categories defined. retun list of indices.
338 QStringList categories;
344 QStringList categories;
339
345
340 int count = categoryCount();
346 int count = categoryCount();
341 for (int i = 0; i < count; i++) {
347 for (int i = 0; i < count; i++) {
342 categories.append(QString::number(i));
348 categories.append(QString::number(i));
343 }
349 }
344 return categories;
350 return categories;
345 }
351 }
346
352
347 void QBarSeriesPrivate::setBarWidth(qreal width)
353 void QBarSeriesPrivate::setBarWidth(qreal width)
348 {
354 {
349 if (width < 0.0) {
355 if (width < 0.0) {
350 width = 0.0;
356 width = 0.0;
351 }
357 }
352 m_barWidth = width;
358 m_barWidth = width;
353 emit updatedBars();
359 emit updatedBars();
354 }
360 }
355
361
356 qreal QBarSeriesPrivate::barWidth() const
362 qreal QBarSeriesPrivate::barWidth() const
357 {
363 {
358 return m_barWidth;
364 return m_barWidth;
359 }
365 }
360
366
361 QBarSet* QBarSeriesPrivate::barsetAt(int index)
367 QBarSet* QBarSeriesPrivate::barsetAt(int index)
362 {
368 {
363 return m_barSets.at(index);
369 return m_barSets.at(index);
364 }
370 }
365
371
366 void QBarSeriesPrivate::setVisible(bool visible)
372 void QBarSeriesPrivate::setVisible(bool visible)
367 {
373 {
368 m_visible = visible;
374 m_visible = visible;
369 emit updatedBars();
375 emit updatedBars();
370 }
376 }
371
377
372 void QBarSeriesPrivate::setLabelsVisible(bool visible)
378 void QBarSeriesPrivate::setLabelsVisible(bool visible)
373 {
379 {
374 m_labelsVisible = visible;
380 m_labelsVisible = visible;
375 emit labelsVisibleChanged(visible);
381 emit labelsVisibleChanged(visible);
376 }
382 }
377
383
378 QString QBarSeriesPrivate::categoryName(int category)
384 QString QBarSeriesPrivate::categoryName(int category)
379 {
385 {
380 if ((category >= 0) && (category < m_categories.count())) {
386 if ((category >= 0) && (category < m_categories.count())) {
381 return m_categories.at(category);
387 return m_categories.at(category);
382 }
388 }
383
389
384 return QString::number(category);
390 return QString::number(category);
385 }
391 }
386
392
387 qreal QBarSeriesPrivate::min()
393 qreal QBarSeriesPrivate::min()
388 {
394 {
389 if (m_barSets.count() <= 0) {
395 if (m_barSets.count() <= 0) {
390 return 0;
396 return 0;
391 }
397 }
392 qreal min = INT_MAX;
398 qreal min = INT_MAX;
393
399
394 for (int i = 0; i < m_barSets.count(); i++) {
400 for (int i = 0; i < m_barSets.count(); i++) {
395 int categoryCount = m_barSets.at(i)->count();
401 int categoryCount = m_barSets.at(i)->count();
396 for (int j = 0; j < categoryCount; j++) {
402 for (int j = 0; j < categoryCount; j++) {
397 qreal temp = m_barSets.at(i)->at(j).y();
403 qreal temp = m_barSets.at(i)->at(j).y();
398 if (temp < min)
404 if (temp < min)
399 min = temp;
405 min = temp;
400 }
406 }
401 }
407 }
402 return min;
408 return min;
403 }
409 }
404
410
405 qreal QBarSeriesPrivate::max()
411 qreal QBarSeriesPrivate::max()
406 {
412 {
407 if (m_barSets.count() <= 0) {
413 if (m_barSets.count() <= 0) {
408 return 0;
414 return 0;
409 }
415 }
410 qreal max = INT_MIN;
416 qreal max = INT_MIN;
411
417
412 for (int i = 0; i < m_barSets.count(); i++) {
418 for (int i = 0; i < m_barSets.count(); i++) {
413 int categoryCount = m_barSets.at(i)->count();
419 int categoryCount = m_barSets.at(i)->count();
414 for (int j = 0; j < categoryCount; j++) {
420 for (int j = 0; j < categoryCount; j++) {
415 qreal temp = m_barSets.at(i)->at(j).y();
421 qreal temp = m_barSets.at(i)->at(j).y();
416 if (temp > max)
422 if (temp > max)
417 max = temp;
423 max = temp;
418 }
424 }
419 }
425 }
420
426
421 return max;
427 return max;
422 }
428 }
423
429
424 qreal QBarSeriesPrivate::valueAt(int set, int category)
430 qreal QBarSeriesPrivate::valueAt(int set, int category)
425 {
431 {
426 if ((set < 0) || (set >= m_barSets.count())) {
432 if ((set < 0) || (set >= m_barSets.count())) {
427 // No set, no value.
433 // No set, no value.
428 return 0;
434 return 0;
429 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
435 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
430 // No category, no value.
436 // No category, no value.
431 return 0;
437 return 0;
432 }
438 }
433
439
434 return m_barSets.at(set)->at(category).y();
440 return m_barSets.at(set)->at(category).y();
435 }
441 }
436
442
437 qreal QBarSeriesPrivate::percentageAt(int set, int category)
443 qreal QBarSeriesPrivate::percentageAt(int set, int category)
438 {
444 {
439 if ((set < 0) || (set >= m_barSets.count())) {
445 if ((set < 0) || (set >= m_barSets.count())) {
440 // No set, no value.
446 // No set, no value.
441 return 0;
447 return 0;
442 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
448 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
443 // No category, no value.
449 // No category, no value.
444 return 0;
450 return 0;
445 }
451 }
446
452
447 qreal value = m_barSets.at(set)->at(category).y();
453 qreal value = m_barSets.at(set)->at(category).y();
448 qreal sum = categorySum(category);
454 qreal sum = categorySum(category);
449 if ( qFuzzyIsNull(sum) ) {
455 if ( qFuzzyIsNull(sum) ) {
450 return 0;
456 return 0;
451 }
457 }
452
458
453 return value / sum;
459 return value / sum;
454 }
460 }
455
461
456 qreal QBarSeriesPrivate::categorySum(int category)
462 qreal QBarSeriesPrivate::categorySum(int category)
457 {
463 {
458 qreal sum(0);
464 qreal sum(0);
459 int count = m_barSets.count(); // Count sets
465 int count = m_barSets.count(); // Count sets
460 for (int set = 0; set < count; set++) {
466 for (int set = 0; set < count; set++) {
461 if (category < m_barSets.at(set)->count())
467 if (category < m_barSets.at(set)->count())
462 sum += m_barSets.at(set)->at(category).y();
468 sum += m_barSets.at(set)->at(category).y();
463 }
469 }
464 return sum;
470 return sum;
465 }
471 }
466
472
467 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
473 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
468 {
474 {
469 qreal sum(0);
475 qreal sum(0);
470 int count = m_barSets.count(); // Count sets
476 int count = m_barSets.count(); // Count sets
471 for (int set = 0; set < count; set++) {
477 for (int set = 0; set < count; set++) {
472 if (category < m_barSets.at(set)->count())
478 if (category < m_barSets.at(set)->count())
473 sum += qAbs(m_barSets.at(set)->at(category).y());
479 sum += qAbs(m_barSets.at(set)->at(category).y());
474 }
480 }
475 return sum;
481 return sum;
476 }
482 }
477
483
478 qreal QBarSeriesPrivate::maxCategorySum()
484 qreal QBarSeriesPrivate::maxCategorySum()
479 {
485 {
480 qreal max = INT_MIN;
486 qreal max = INT_MIN;
481 int count = categoryCount();
487 int count = categoryCount();
482 for (int i = 0; i < count; i++) {
488 for (int i = 0; i < count; i++) {
483 qreal sum = categorySum(i);
489 qreal sum = categorySum(i);
484 if (sum > max)
490 if (sum > max)
485 max = sum;
491 max = sum;
486 }
492 }
487 return max;
493 return max;
488 }
494 }
489
495
490 qreal QBarSeriesPrivate::minX()
496 qreal QBarSeriesPrivate::minX()
491 {
497 {
492 if (m_barSets.count() <= 0) {
498 if (m_barSets.count() <= 0) {
493 return 0;
499 return 0;
494 }
500 }
495 qreal min = INT_MAX;
501 qreal min = INT_MAX;
496
502
497 for (int i = 0; i < m_barSets.count(); i++) {
503 for (int i = 0; i < m_barSets.count(); i++) {
498 int categoryCount = m_barSets.at(i)->count();
504 int categoryCount = m_barSets.at(i)->count();
499 for (int j = 0; j < categoryCount; j++) {
505 for (int j = 0; j < categoryCount; j++) {
500 qreal temp = m_barSets.at(i)->at(j).x();
506 qreal temp = m_barSets.at(i)->at(j).x();
501 if (temp < min)
507 if (temp < min)
502 min = temp;
508 min = temp;
503 }
509 }
504 }
510 }
505 return min;
511 return min;
506 }
512 }
507
513
508 qreal QBarSeriesPrivate::maxX()
514 qreal QBarSeriesPrivate::maxX()
509 {
515 {
510 if (m_barSets.count() <= 0) {
516 if (m_barSets.count() <= 0) {
511 return 0;
517 return 0;
512 }
518 }
513 qreal max = INT_MIN;
519 qreal max = INT_MIN;
514
520
515 for (int i = 0; i < m_barSets.count(); i++) {
521 for (int i = 0; i < m_barSets.count(); i++) {
516 int categoryCount = m_barSets.at(i)->count();
522 int categoryCount = m_barSets.at(i)->count();
517 for (int j = 0; j < categoryCount; j++) {
523 for (int j = 0; j < categoryCount; j++) {
518 qreal temp = m_barSets.at(i)->at(j).x();
524 qreal temp = m_barSets.at(i)->at(j).x();
519 if (temp > max)
525 if (temp > max)
520 max = temp;
526 max = temp;
521 }
527 }
522 }
528 }
523
529
524 return max;
530 return max;
525 }
531 }
526
532
527
533
528 void QBarSeriesPrivate::scaleDomain(Domain& domain)
534 void QBarSeriesPrivate::scaleDomain(Domain& domain)
529 {
535 {
530 qreal minX(domain.minX());
536 qreal minX(domain.minX());
531 qreal minY(domain.minY());
537 qreal minY(domain.minY());
532 qreal maxX(domain.maxX());
538 qreal maxX(domain.maxX());
533 qreal maxY(domain.maxY());
539 qreal maxY(domain.maxY());
534 int tickXCount(domain.tickXCount());
540 int tickXCount(domain.tickXCount());
535 int tickYCount(domain.tickYCount());
541 int tickYCount(domain.tickYCount());
536
542
537 qreal seriesMinX = this->minX();
543 qreal seriesMinX = this->minX();
538 qreal seriesMaxX = this->maxX();
544 qreal seriesMaxX = this->maxX();
539 qreal y = max();
545 qreal y = max();
540 minX = qMin(minX, seriesMinX - 0.5);
546 minX = qMin(minX, seriesMinX - 0.5);
541 minY = qMin(minY, y);
547 minY = qMin(minY, y);
542 maxX = qMax(maxX, seriesMaxX + 0.5);
548 maxX = qMax(maxX, seriesMaxX + 0.5);
543 maxY = qMax(maxY, y);
549 maxY = qMax(maxY, y);
544 tickXCount = categoryCount()+1;
550 tickXCount = categoryCount()+1;
545
551
546 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
552 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
547 }
553 }
548
554
549 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
555 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
550 {
556 {
551 Q_Q(QBarSeries);
557 Q_Q(QBarSeries);
552
558
553 BarChartItem* bar = new BarChartItem(q,presenter);
559 BarChartItem* bar = new BarChartItem(q,presenter);
554 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
560 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
555 presenter->animator()->addAnimation(bar);
561 presenter->animator()->addAnimation(bar);
556 }
562 }
557 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
563 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
558 return bar;
564 return bar;
559
565
560 }
566 }
561
567
562 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
568 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
563 {
569 {
564 Q_Q(QBarSeries);
570 Q_Q(QBarSeries);
565 QList<LegendMarker*> markers;
571 QList<LegendMarker*> markers;
566 foreach(QBarSet* set, q->barSets()) {
572 foreach(QBarSet* set, q->barSets()) {
567 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
573 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
568 markers << marker;
574 markers << marker;
569 }
575 }
570
576
571 return markers;
577 return markers;
572 }
578 }
573
579
574 bool QBarSeriesPrivate::append(QBarSet *set)
580 bool QBarSeriesPrivate::append(QBarSet *set)
575 {
581 {
576 Q_Q(QBarSeries);
582 Q_Q(QBarSeries);
577 if ((m_barSets.contains(set)) || (set == 0)) {
583 if ((m_barSets.contains(set)) || (set == 0)) {
578 // Fail if set is already in list or set is null.
584 // Fail if set is already in list or set is null.
579 return false;
585 return false;
580 }
586 }
581 m_barSets.append(set);
587 m_barSets.append(set);
582 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
588 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
583 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
589 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
584 if (m_dataset) {
590 if (m_dataset) {
585 m_dataset->updateSeries(q); // this notifies legend
591 m_dataset->updateSeries(q); // this notifies legend
586 }
592 }
587 emit restructuredBars(); // this notifies barchartitem
593 emit restructuredBars(); // this notifies barchartitem
588 return true;
594 return true;
589 }
595 }
590
596
591 bool QBarSeriesPrivate::remove(QBarSet *set)
597 bool QBarSeriesPrivate::remove(QBarSet *set)
592 {
598 {
593 Q_Q(QBarSeries);
599 Q_Q(QBarSeries);
594 if (!m_barSets.contains(set)) {
600 if (!m_barSets.contains(set)) {
595 // Fail if set is not in list
601 // Fail if set is not in list
596 return false;
602 return false;
597 }
603 }
598 m_barSets.removeOne(set);
604 m_barSets.removeOne(set);
599 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
605 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
600 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
606 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
601 if (m_dataset) {
607 if (m_dataset) {
602 m_dataset->updateSeries(q); // this notifies legend
608 m_dataset->updateSeries(q); // this notifies legend
603 }
609 }
604 emit restructuredBars(); // this notifies barchartitem
610 emit restructuredBars(); // this notifies barchartitem
605 return true;
611 return true;
606 }
612 }
607
613
608 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
614 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
609 {
615 {
610 Q_Q(QBarSeries);
616 Q_Q(QBarSeries);
611 foreach (QBarSet* set, sets) {
617 foreach (QBarSet* set, sets) {
612 if ((set == 0) || (m_barSets.contains(set))) {
618 if ((set == 0) || (m_barSets.contains(set))) {
613 // Fail if any of the sets is null or is already appended.
619 // Fail if any of the sets is null or is already appended.
614 return false;
620 return false;
615 }
621 }
616 if (sets.count(set) != 1) {
622 if (sets.count(set) != 1) {
617 // Also fail if same set is more than once in given list.
623 // Also fail if same set is more than once in given list.
618 return false;
624 return false;
619 }
625 }
620 }
626 }
621
627
622 foreach (QBarSet* set, sets) {
628 foreach (QBarSet* set, sets) {
623 m_barSets.append(set);
629 m_barSets.append(set);
624 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
630 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
625 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
631 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
626 }
632 }
627 if (m_dataset) {
633 if (m_dataset) {
628 m_dataset->updateSeries(q); // this notifies legend
634 m_dataset->updateSeries(q); // this notifies legend
629 }
635 }
630 emit restructuredBars(); // this notifies barchartitem
636 emit restructuredBars(); // this notifies barchartitem
631 return true;
637 return true;
632 }
638 }
633
639
634 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
640 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
635 {
641 {
636 Q_Q(QBarSeries);
642 Q_Q(QBarSeries);
637 if (sets.count() == 0) {
643 if (sets.count() == 0) {
638 return false;
644 return false;
639 }
645 }
640 foreach (QBarSet* set, sets) {
646 foreach (QBarSet* set, sets) {
641 if ((set == 0) || (!m_barSets.contains(set))) {
647 if ((set == 0) || (!m_barSets.contains(set))) {
642 // Fail if any of the sets is null or is not in series
648 // Fail if any of the sets is null or is not in series
643 return false;
649 return false;
644 }
650 }
645 if (sets.count(set) != 1) {
651 if (sets.count(set) != 1) {
646 // Also fail if same set is more than once in given list.
652 // Also fail if same set is more than once in given list.
647 return false;
653 return false;
648 }
654 }
649 }
655 }
650
656
651 foreach (QBarSet* set, sets) {
657 foreach (QBarSet* set, sets) {
652 m_barSets.removeOne(set);
658 m_barSets.removeOne(set);
653 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
659 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
654 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
660 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
655 }
661 }
656
662
657 if (m_dataset) {
663 if (m_dataset) {
658 m_dataset->updateSeries(q); // this notifies legend
664 m_dataset->updateSeries(q); // this notifies legend
659 }
665 }
660 emit restructuredBars(); // this notifies barchartitem
666 emit restructuredBars(); // this notifies barchartitem
661 return true;
667 return true;
662 }
668 }
663
669
664 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
670 bool QBarSeriesPrivate::insert(int index, QBarSet *set)
665 {
671 {
666 Q_Q(QBarSeries);
672 Q_Q(QBarSeries);
667 if ((m_barSets.contains(set)) || (set == 0)) {
673 if ((m_barSets.contains(set)) || (set == 0)) {
668 // Fail if set is already in list or set is null.
674 // Fail if set is already in list or set is null.
669 return false;
675 return false;
670 }
676 }
671 m_barSets.insert(index, set);
677 m_barSets.insert(index, set);
672 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
678 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
673 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
679 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
674 if (m_dataset) {
680 if (m_dataset) {
675 m_dataset->updateSeries(q); // this notifies legend
681 m_dataset->updateSeries(q); // this notifies legend
676 }
682 }
677 emit restructuredBars(); // this notifies barchartitem
683 emit restructuredBars(); // this notifies barchartitem
678 return true;
684 return true;
679 }
685 }
680
686
681 #include "moc_qbarseries.cpp"
687 #include "moc_qbarseries.cpp"
682 #include "moc_qbarseries_p.cpp"
688 #include "moc_qbarseries_p.cpp"
683
689
684 QTCOMMERCIALCHART_END_NAMESPACE
690 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,81 +1,83
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class QBarSet;
29 class QBarSet;
30 class QBarSeriesPrivate;
30 class QBarSeriesPrivate;
31
31
32 // Container for series
32 // Container for series
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
33 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
36 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth NOTIFY barWidthChanged)
37 Q_PROPERTY(int count READ barsetCount)
37 Q_PROPERTY(int count READ barsetCount NOTIFY barsetCountChanged)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible)
38 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
39
39
40 public:
40 public:
41 explicit QBarSeries(QObject *parent = 0);
41 explicit QBarSeries(QObject *parent = 0);
42 virtual ~QBarSeries();
42 virtual ~QBarSeries();
43
43
44 QAbstractSeries::SeriesType type() const;
44 QAbstractSeries::SeriesType type() const;
45
45
46 void setBarWidth(qreal width);
46 void setBarWidth(qreal width);
47 qreal barWidth() const;
47 qreal barWidth() const;
48
48
49 bool append(QBarSet *set);
49 bool append(QBarSet *set);
50 bool remove(QBarSet *set);
50 bool remove(QBarSet *set);
51 bool append(QList<QBarSet* > sets);
51 bool append(QList<QBarSet* > sets);
52 bool insert(int index, QBarSet *set);
52 bool insert(int index, QBarSet *set);
53 int barsetCount() const;
53 int barsetCount() const;
54 QList<QBarSet*> barSets() const;
54 QList<QBarSet*> barSets() const;
55 void clear();
55 void clear();
56
56
57 void setLabelsVisible(bool visible = true);
57 void setLabelsVisible(bool visible = true);
58 bool isLabelsVisible() const;
58 bool isLabelsVisible() const;
59
59
60 protected:
60 protected:
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
61 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
62
62
63 Q_SIGNALS:
63 Q_SIGNALS:
64 void clicked(QBarSet *barset, int index);
64 void clicked(QBarSet *barset, int index);
65 void hovered(QBarSet* barset, bool status);
65 void hovered(QBarSet* barset, bool status);
66 void barWidthChanged();
67 void barsetCountChanged();
66 void labelsVisibleChanged();
68 void labelsVisibleChanged();
67
69
68 void barsetsAdded(QList<QBarSet*> sets);
70 void barsetsAdded(QList<QBarSet*> sets);
69 void barsetsRemoved(QList<QBarSet*> sets);
71 void barsetsRemoved(QList<QBarSet*> sets);
70
72
71 protected:
73 protected:
72 Q_DECLARE_PRIVATE(QBarSeries)
74 Q_DECLARE_PRIVATE(QBarSeries)
73 friend class BarChartItem;
75 friend class BarChartItem;
74 friend class PercentBarChartItem;
76 friend class PercentBarChartItem;
75 friend class StackedBarChartItem;
77 friend class StackedBarChartItem;
76 friend class GroupedBarChartItem;
78 friend class GroupedBarChartItem;
77 };
79 };
78
80
79 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
80
82
81 #endif // BARSERIES_H
83 #endif // BARSERIES_H
@@ -1,500 +1,494
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarset.h"
21 #include "qbarset.h"
22 #include "qbarset_p.h"
22 #include "qbarset_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QBarSet
27 \class QBarSet
28 \brief part of QtCommercial chart API.
28 \brief part of QtCommercial chart API.
29
29
30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 First value of set is assumed to belong to first category, second to second category and so on.
31 First value of set is assumed to belong to first category, second to second category and so on.
32 If set has fewer values than there are categories, then the missing values are assumed to be
32 If set has fewer values than there are categories, then the missing values are assumed to be
33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34
34
35 \mainclass
35 \mainclass
36
36
37 \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries
37 \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries
38 */
38 */
39
39
40 /*!
40 /*!
41 \property QBarSet::name
41 \property QBarSet::label
42 \brief Defines the name of the barSet.
42 \brief Defines the label of the barSet.
43 */
43 */
44
44
45 /*!
45 /*!
46 \property QBarSet::pen
46 \property QBarSet::pen
47 \brief Defines the pen used by the barSet.
47 \brief Defines the pen used by the barSet.
48 */
48 */
49
49
50 /*!
50 /*!
51 \property QBarSet::brush
51 \property QBarSet::brush
52 \brief Defines the brush used by the barSet.
52 \brief Defines the brush used by the barSet.
53 */
53 */
54
54
55 /*!
55 /*!
56 \property QBarSet::labelBrush
56 \property QBarSet::labelBrush
57 \brief Defines the brush used by the barSet's label.
57 \brief Defines the brush used by the barSet's label.
58 */
58 */
59
59
60 /*!
60 /*!
61 \property QBarSet::labelFont
61 \property QBarSet::labelFont
62 \brief Defines the font used by the barSet's label.
62 \brief Defines the font used by the barSet's label.
63 */
63 */
64
64
65 /*!
65 /*!
66 \fn void QBarSet::nameChanged()
66 \fn void QBarSet::labelChanged()
67
67
68 This signal is emitted when the name of the barSet has changed.
68 This signal is emitted when the label of the barSet has changed.
69
69
70 \sa name
70 \sa label
71 */
71 */
72
72
73 /*!
73 /*!
74 \fn void QBarSet::penChanged()
74 \fn void QBarSet::penChanged()
75
75
76 This signal is emitted when the pen of the barSet has changed.
76 This signal is emitted when the pen of the barSet has changed.
77
77
78 \sa pen
78 \sa pen
79 */
79 */
80
80
81 /*!
81 /*!
82 \fn void QBarSet::brushChanged()
82 \fn void QBarSet::brushChanged()
83
83
84 This signal is emitted when the brush of the barSet has changed.
84 This signal is emitted when the brush of the barSet has changed.
85
85
86 \sa brush
86 \sa brush
87 */
87 */
88
88
89 /*!
89 /*!
90 \fn void QBarSet::labelChanged()
91
92 This signal is emitted when the label of the barSet has changed.
93 */
94
95 /*!
96 \fn void QBarSet::labelBrushChanged()
90 \fn void QBarSet::labelBrushChanged()
97
91
98 This signal is emitted when the brush of the barSet's label has changed.
92 This signal is emitted when the brush of the barSet's label has changed.
99
93
100 \sa labelBrush
94 \sa labelBrush
101 */
95 */
102
96
103 /*!
97 /*!
104 \fn void QBarSet::labelFontChanged()
98 \fn void QBarSet::labelFontChanged()
105
99
106 This signal is emitted when the font of the barSet's label has changed.
100 This signal is emitted when the font of the barSet's label has changed.
107
101
108 \sa labelBrush
102 \sa labelBrush
109 */
103 */
110
104
111 /*!
105 /*!
112 \fn void QBarSet::valuesAdded(int index, int count)
106 \fn void QBarSet::valuesAdded(int index, int count)
113
107
114 This signal is emitted when new values have been added to the set.
108 This signal is emitted when new values have been added to the set.
115 Parameter \a index indicates the position of the first inserted value.
109 Parameter \a index indicates the position of the first inserted value.
116 Parameter \a count is the number of iserted values.
110 Parameter \a count is the number of iserted values.
117
111
118 \sa append(), insert()
112 \sa append(), insert()
119 */
113 */
120
114
121 /*!
115 /*!
122 \fn void QBarSet::valuesRemoved(int index, int count)
116 \fn void QBarSet::valuesRemoved(int index, int count)
123
117
124 This signal is emitted values have been removed from the set.
118 This signal is emitted values have been removed from the set.
125 Parameter \a index indicates the position of the first removed value.
119 Parameter \a index indicates the position of the first removed value.
126 Parameter \a count is the number of removed values.
120 Parameter \a count is the number of removed values.
127
121
128 \sa remove()
122 \sa remove()
129 */
123 */
130
124
131 /*!
125 /*!
132 \fn void QBarSet::valueChanged(int index)
126 \fn void QBarSet::valueChanged(int index)
133
127
134 This signal is emitted values the value in the set has been modified.
128 This signal is emitted values the value in the set has been modified.
135 Parameter \a index indicates the position of the modified value.
129 Parameter \a index indicates the position of the modified value.
136
130
137 \sa at()
131 \sa at()
138 */
132 */
139 void valueChanged(int index);
133 void valueChanged(int index);
140
134
141 /*!
135 /*!
142 Constructs QBarSet with a name of \a name and with parent of \a parent
136 Constructs QBarSet with a label of \a label and with parent of \a parent
143 */
137 */
144 QBarSet::QBarSet(const QString name, QObject *parent)
138 QBarSet::QBarSet(const QString label, QObject *parent)
145 : QObject(parent)
139 : QObject(parent)
146 ,d_ptr(new QBarSetPrivate(name,this))
140 ,d_ptr(new QBarSetPrivate(label,this))
147 {
141 {
148 }
142 }
149
143
150 /*!
144 /*!
151 Destroys the barset
145 Destroys the barset
152 */
146 */
153 QBarSet::~QBarSet()
147 QBarSet::~QBarSet()
154 {
148 {
155 // NOTE: d_ptr destroyed by QObject
149 // NOTE: d_ptr destroyed by QObject
156 }
150 }
157
151
158 /*!
152 /*!
159 Sets new \a name for set.
153 Sets new \a label for set.
160 */
154 */
161 void QBarSet::setName(const QString name)
155 void QBarSet::setLabel(const QString label)
162 {
156 {
163 d_ptr->m_name = name;
157 d_ptr->m_label = label;
164 emit nameChanged();
158 emit labelChanged();
165 }
159 }
166
160
167 /*!
161 /*!
168 Returns name of the set.
162 Returns label of the set.
169 */
163 */
170 QString QBarSet::name() const
164 QString QBarSet::label() const
171 {
165 {
172 return d_ptr->m_name;
166 return d_ptr->m_label;
173 }
167 }
174
168
175 /*!
169 /*!
176 Appends a point to set. Parameter \a value x coordinate defines the
170 Appends a point to set. Parameter \a value x coordinate defines the
177 position in x-axis and y coordinate defines the height of bar.
171 position in x-axis and y coordinate defines the height of bar.
178 Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries)
172 Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries)
179 the x values are used or ignored.
173 the x values are used or ignored.
180 */
174 */
181 void QBarSet::append(const QPointF value)
175 void QBarSet::append(const QPointF value)
182 {
176 {
183 int index = d_ptr->m_values.count();
177 int index = d_ptr->m_values.count();
184 d_ptr->append(value);
178 d_ptr->append(value);
185 emit valuesAdded(index, 1);
179 emit valuesAdded(index, 1);
186 }
180 }
187
181
188 /*!
182 /*!
189 Appends a list of \a values to set. Works like append with single point.
183 Appends a list of \a values to set. Works like append with single point.
190 \sa append()
184 \sa append()
191 */
185 */
192 void QBarSet::append(const QList<QPointF> values)
186 void QBarSet::append(const QList<QPointF> values)
193 {
187 {
194 int index = d_ptr->m_values.count();
188 int index = d_ptr->m_values.count();
195 d_ptr->append(values);
189 d_ptr->append(values);
196 emit valuesAdded(index, values.count());
190 emit valuesAdded(index, values.count());
197 }
191 }
198
192
199 /*!
193 /*!
200 Appends new value \a value to the end of set. Internally the value is converted to QPointF,
194 Appends new value \a value to the end of set. Internally the value is converted to QPointF,
201 with x coordinate being the index of appended value and y coordinate is the value.
195 with x coordinate being the index of appended value and y coordinate is the value.
202 */
196 */
203 void QBarSet::append(const qreal value)
197 void QBarSet::append(const qreal value)
204 {
198 {
205 // Convert to QPointF and use other append(QPointF) method.
199 // Convert to QPointF and use other append(QPointF) method.
206 append(QPointF(d_ptr->m_values.count(), value));
200 append(QPointF(d_ptr->m_values.count(), value));
207 }
201 }
208
202
209 /*!
203 /*!
210 Appends a list of reals to set. Works like append with single real value. The \a values in list
204 Appends a list of reals to set. Works like append with single real value. The \a values in list
211 are converted to QPointF, where x coordinate is the index of point and y coordinate is the value.
205 are converted to QPointF, where x coordinate is the index of point and y coordinate is the value.
212 \sa append()
206 \sa append()
213 */
207 */
214 void QBarSet::append(const QList<qreal> values)
208 void QBarSet::append(const QList<qreal> values)
215 {
209 {
216 int index = d_ptr->m_values.count();
210 int index = d_ptr->m_values.count();
217 d_ptr->append(values);
211 d_ptr->append(values);
218 emit valuesAdded(index, values.count());
212 emit valuesAdded(index, values.count());
219 }
213 }
220
214
221 /*!
215 /*!
222 Convinience operator. Same as append, with real \a value.
216 Convinience operator. Same as append, with real \a value.
223 \sa append()
217 \sa append()
224 */
218 */
225 QBarSet& QBarSet::operator << (const qreal &value)
219 QBarSet& QBarSet::operator << (const qreal &value)
226 {
220 {
227 append(value);
221 append(value);
228 return *this;
222 return *this;
229 }
223 }
230
224
231 /*!
225 /*!
232 Convinience operator. Same as append, with QPointF \a value.
226 Convinience operator. Same as append, with QPointF \a value.
233 \sa append()
227 \sa append()
234 */
228 */
235 QBarSet& QBarSet::operator << (const QPointF &value)
229 QBarSet& QBarSet::operator << (const QPointF &value)
236 {
230 {
237 append(value);
231 append(value);
238 return *this;
232 return *this;
239 }
233 }
240
234
241 /*!
235 /*!
242 Inserts new \a value on the \a index position.
236 Inserts new \a value on the \a index position.
243 The value that is currently at this postion is moved to postion index + 1
237 The value that is currently at this postion is moved to postion index + 1
244 \sa remove()
238 \sa remove()
245 */
239 */
246 void QBarSet::insert(const int index, const qreal value)
240 void QBarSet::insert(const int index, const qreal value)
247 {
241 {
248 d_ptr->insert(index, value);
242 d_ptr->insert(index, value);
249 emit valuesAdded(index,1);
243 emit valuesAdded(index,1);
250 }
244 }
251
245
252 /*!
246 /*!
253 Inserts new \a value on the \a index position.
247 Inserts new \a value on the \a index position.
254 The value that is currently at this postion is moved to postion index + 1
248 The value that is currently at this postion is moved to postion index + 1
255 \sa remove()
249 \sa remove()
256 */
250 */
257 void QBarSet::insert(const int index, const QPointF value)
251 void QBarSet::insert(const int index, const QPointF value)
258 {
252 {
259 d_ptr->insert(index,value);
253 d_ptr->insert(index,value);
260 emit valuesAdded(index,1);
254 emit valuesAdded(index,1);
261 }
255 }
262
256
263 /*!
257 /*!
264 Removes \a count number of values from the set starting at \a index.
258 Removes \a count number of values from the set starting at \a index.
265 Returns true if remove operation was succesfull.
259 Returns true if remove operation was succesfull.
266 \sa insert()
260 \sa insert()
267 */
261 */
268 bool QBarSet::remove(const int index, const int count)
262 bool QBarSet::remove(const int index, const int count)
269 {
263 {
270 bool success = d_ptr->remove(index,count);
264 bool success = d_ptr->remove(index,count);
271 if (success) {
265 if (success) {
272 emit valuesRemoved(index,count);
266 emit valuesRemoved(index,count);
273 }
267 }
274 return success;
268 return success;
275 }
269 }
276
270
277 /*!
271 /*!
278 Sets a new value \a value to set, indexed by \a index
272 Sets a new value \a value to set, indexed by \a index
279 */
273 */
280 void QBarSet::replace(const int index, const qreal value)
274 void QBarSet::replace(const int index, const qreal value)
281 {
275 {
282 d_ptr->replace(index,value);
276 d_ptr->replace(index,value);
283 emit valueChanged(index);
277 emit valueChanged(index);
284 }
278 }
285
279
286 /*!
280 /*!
287 Sets a new value \a value to set, indexed by \a index
281 Sets a new value \a value to set, indexed by \a index
288 */
282 */
289 void QBarSet::replace(const int index, const QPointF value)
283 void QBarSet::replace(const int index, const QPointF value)
290 {
284 {
291 d_ptr->replace(index,value);
285 d_ptr->replace(index,value);
292 emit valueChanged(index);
286 emit valueChanged(index);
293 }
287 }
294
288
295 /*!
289 /*!
296 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF.
290 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF.
297 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
291 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
298 of the QPointF (if appended with QPointF append).
292 of the QPointF (if appended with QPointF append).
299 If the index is out of bounds QPointF(0, 0.0) is returned.
293 If the index is out of bounds QPointF(0, 0.0) is returned.
300 */
294 */
301 QPointF QBarSet::at(const int index) const
295 QPointF QBarSet::at(const int index) const
302 {
296 {
303 if (index < 0 || index >= d_ptr->m_values.count()) {
297 if (index < 0 || index >= d_ptr->m_values.count()) {
304 return QPointF(index, 0.0);
298 return QPointF(index, 0.0);
305 }
299 }
306
300
307 return d_ptr->m_values.at(index);
301 return d_ptr->m_values.at(index);
308 }
302 }
309
303
310 /*!
304 /*!
311 Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF.
305 Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF.
312 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
306 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
313 of the QPointF (if appended with QPointF append).
307 of the QPointF (if appended with QPointF append).
314 */
308 */
315 QPointF QBarSet::operator [](const int index) const
309 QPointF QBarSet::operator [](const int index) const
316 {
310 {
317 return d_ptr->m_values.at(index);
311 return d_ptr->m_values.at(index);
318 }
312 }
319
313
320 /*!
314 /*!
321 Returns count of values in set.
315 Returns count of values in set.
322 */
316 */
323 int QBarSet::count() const
317 int QBarSet::count() const
324 {
318 {
325 return d_ptr->m_values.count();
319 return d_ptr->m_values.count();
326 }
320 }
327
321
328 /*!
322 /*!
329 Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation.
323 Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation.
330 */
324 */
331 qreal QBarSet::sum() const
325 qreal QBarSet::sum() const
332 {
326 {
333 qreal total(0);
327 qreal total(0);
334 for (int i=0; i < d_ptr->m_values.count(); i++) {
328 for (int i=0; i < d_ptr->m_values.count(); i++) {
335 //total += d_ptr->m_values.at(i);
329 //total += d_ptr->m_values.at(i);
336 total += d_ptr->m_values.at(i).y();
330 total += d_ptr->m_values.at(i).y();
337 }
331 }
338 return total;
332 return total;
339 }
333 }
340
334
341 /*!
335 /*!
342 Sets pen for set. Bars of this set are drawn using \a pen
336 Sets pen for set. Bars of this set are drawn using \a pen
343 */
337 */
344 void QBarSet::setPen(const QPen &pen)
338 void QBarSet::setPen(const QPen &pen)
345 {
339 {
346 if(d_ptr->m_pen!=pen){
340 if(d_ptr->m_pen!=pen){
347 d_ptr->m_pen = pen;
341 d_ptr->m_pen = pen;
348 emit d_ptr->updatedBars();
342 emit d_ptr->updatedBars();
349 emit penChanged();
343 emit penChanged();
350 }
344 }
351 }
345 }
352
346
353 /*!
347 /*!
354 Returns pen of the set.
348 Returns pen of the set.
355 */
349 */
356 QPen QBarSet::pen() const
350 QPen QBarSet::pen() const
357 {
351 {
358 return d_ptr->m_pen;
352 return d_ptr->m_pen;
359 }
353 }
360
354
361 /*!
355 /*!
362 Sets brush for the set. Bars of this set are drawn using \a brush
356 Sets brush for the set. Bars of this set are drawn using \a brush
363 */
357 */
364 void QBarSet::setBrush(const QBrush &brush)
358 void QBarSet::setBrush(const QBrush &brush)
365 {
359 {
366 if(d_ptr->m_brush!=brush){
360 if(d_ptr->m_brush!=brush){
367 d_ptr->m_brush = brush;
361 d_ptr->m_brush = brush;
368 emit d_ptr->updatedBars();
362 emit d_ptr->updatedBars();
369 emit brushChanged();
363 emit brushChanged();
370 }
364 }
371 }
365 }
372
366
373 /*!
367 /*!
374 Returns brush of the set.
368 Returns brush of the set.
375 */
369 */
376 QBrush QBarSet::brush() const
370 QBrush QBarSet::brush() const
377 {
371 {
378 return d_ptr->m_brush;
372 return d_ptr->m_brush;
379 }
373 }
380
374
381 /*!
375 /*!
382 Sets \a brush of the values that are drawn on top of this barset
376 Sets \a brush of the values that are drawn on top of this barset
383 */
377 */
384 void QBarSet::setLabelBrush(const QBrush &brush)
378 void QBarSet::setLabelBrush(const QBrush &brush)
385 {
379 {
386 if(d_ptr->m_labelBrush!=brush){
380 if(d_ptr->m_labelBrush!=brush){
387 d_ptr->m_labelBrush = brush;
381 d_ptr->m_labelBrush = brush;
388 emit d_ptr->updatedBars();
382 emit d_ptr->updatedBars();
389 emit labelBrushChanged();
383 emit labelBrushChanged();
390 }
384 }
391 }
385 }
392
386
393 /*!
387 /*!
394 Returns brush of the values that are drawn on top of this barset
388 Returns brush of the values that are drawn on top of this barset
395 */
389 */
396 QBrush QBarSet::labelBrush() const
390 QBrush QBarSet::labelBrush() const
397 {
391 {
398 return d_ptr->m_labelBrush;
392 return d_ptr->m_labelBrush;
399 }
393 }
400
394
401 /*!
395 /*!
402 Sets the \a font for values that are drawn on top of this barset
396 Sets the \a font for values that are drawn on top of this barset
403 */
397 */
404 void QBarSet::setLabelFont(const QFont &font)
398 void QBarSet::setLabelFont(const QFont &font)
405 {
399 {
406 if(d_ptr->m_labelFont!=font) {
400 if(d_ptr->m_labelFont!=font) {
407 d_ptr->m_labelFont = font;
401 d_ptr->m_labelFont = font;
408 emit d_ptr->updatedBars();
402 emit d_ptr->updatedBars();
409 emit labelFontChanged();
403 emit labelFontChanged();
410 }
404 }
411
405
412 }
406 }
413
407
414 /*!
408 /*!
415 Returns the pen for values that are drawn on top of this set
409 Returns the pen for values that are drawn on top of this set
416 */
410 */
417 QFont QBarSet::labelFont() const
411 QFont QBarSet::labelFont() const
418 {
412 {
419 return d_ptr->m_labelFont;
413 return d_ptr->m_labelFont;
420 }
414 }
421
415
422 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
416 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
423
417
424 QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent),
418 QBarSetPrivate::QBarSetPrivate(const QString label, QBarSet *parent) : QObject(parent),
425 q_ptr(parent),
419 q_ptr(parent),
426 m_name(name)
420 m_label(label)
427 {
421 {
428 }
422 }
429
423
430 QBarSetPrivate::~QBarSetPrivate()
424 QBarSetPrivate::~QBarSetPrivate()
431 {
425 {
432 }
426 }
433
427
434 void QBarSetPrivate::append(QPointF value)
428 void QBarSetPrivate::append(QPointF value)
435 {
429 {
436 m_values.append(value);
430 m_values.append(value);
437 emit restructuredBars();
431 emit restructuredBars();
438 }
432 }
439
433
440 void QBarSetPrivate::append(QList<QPointF> values)
434 void QBarSetPrivate::append(QList<QPointF> values)
441 {
435 {
442 for (int i=0; i<values.count(); i++) {
436 for (int i=0; i<values.count(); i++) {
443 m_values.append(values.at(i));
437 m_values.append(values.at(i));
444 }
438 }
445 emit restructuredBars();
439 emit restructuredBars();
446 }
440 }
447
441
448 void QBarSetPrivate::append(QList<qreal> values)
442 void QBarSetPrivate::append(QList<qreal> values)
449 {
443 {
450 int index = m_values.count();
444 int index = m_values.count();
451 for (int i=0; i<values.count(); i++) {
445 for (int i=0; i<values.count(); i++) {
452 m_values.append(QPointF(index,values.at(i)));
446 m_values.append(QPointF(index,values.at(i)));
453 index++;
447 index++;
454 }
448 }
455 emit restructuredBars();
449 emit restructuredBars();
456 }
450 }
457
451
458 void QBarSetPrivate::insert(const int index, const qreal value)
452 void QBarSetPrivate::insert(const int index, const qreal value)
459 {
453 {
460 m_values.insert(index, QPointF(index, value));
454 m_values.insert(index, QPointF(index, value));
461 emit restructuredBars();
455 emit restructuredBars();
462 }
456 }
463
457
464 void QBarSetPrivate::insert(const int index, const QPointF value)
458 void QBarSetPrivate::insert(const int index, const QPointF value)
465 {
459 {
466 m_values.insert(index, value);
460 m_values.insert(index, value);
467 emit restructuredBars();
461 emit restructuredBars();
468 }
462 }
469
463
470 bool QBarSetPrivate::remove(const int index, const int count)
464 bool QBarSetPrivate::remove(const int index, const int count)
471 {
465 {
472 if ((index + count) > m_values.count()) {
466 if ((index + count) > m_values.count()) {
473 // cant remove more values than there are
467 // cant remove more values than there are
474 return false;
468 return false;
475 }
469 }
476 int c = count;
470 int c = count;
477 while (c > 0) {
471 while (c > 0) {
478 m_values.removeAt(index);
472 m_values.removeAt(index);
479 c--;
473 c--;
480 }
474 }
481 emit restructuredBars();
475 emit restructuredBars();
482 return true;
476 return true;
483 }
477 }
484
478
485 void QBarSetPrivate::replace(const int index, const qreal value)
479 void QBarSetPrivate::replace(const int index, const qreal value)
486 {
480 {
487 m_values.replace(index,QPointF(index,value));
481 m_values.replace(index,QPointF(index,value));
488 emit updatedBars();
482 emit updatedBars();
489 }
483 }
490
484
491 void QBarSetPrivate::replace(const int index, const QPointF value)
485 void QBarSetPrivate::replace(const int index, const QPointF value)
492 {
486 {
493 m_values.replace(index,value);
487 m_values.replace(index,value);
494 emit updatedBars();
488 emit updatedBars();
495 }
489 }
496
490
497 #include "moc_qbarset.cpp"
491 #include "moc_qbarset.cpp"
498 #include "moc_qbarset_p.cpp"
492 #include "moc_qbarset_p.cpp"
499
493
500 QTCOMMERCIALCHART_END_NAMESPACE
494 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,100
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QBARSET_H
21 #ifndef QBARSET_H
22 #define QBARSET_H
22 #define QBARSET_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QPen>
25 #include <QPen>
26 #include <QBrush>
26 #include <QBrush>
27 #include <QFont>
27 #include <QFont>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QBarSetPrivate;
30 class QBarSetPrivate;
31
31
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 {
33 {
34 Q_OBJECT
34 Q_OBJECT
35 Q_PROPERTY(QString name READ name WRITE setName)
35 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
36 Q_PROPERTY(QPen pen READ pen WRITE setPen)
36 Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
37 Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
37 Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged)
38 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush)
38 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush NOTIFY labelBrushChanged)
39 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont)
39 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged)
40
40
41 public:
41 public:
42 explicit QBarSet(const QString name, QObject *parent = 0);
42 explicit QBarSet(const QString label, QObject *parent = 0);
43 virtual ~QBarSet();
43 virtual ~QBarSet();
44
44
45 void setName(const QString name);
45 void setLabel(const QString label);
46 QString name() const;
46 QString label() const;
47
47
48 void append(const QPointF value);
48 void append(const QPointF value);
49 void append(const QList<QPointF> values);
49 void append(const QList<QPointF> values);
50 void append(const qreal value);
50 void append(const qreal value);
51 void append(const QList<qreal> values);
51 void append(const QList<qreal> values);
52
52
53 QBarSet& operator << (const qreal &value);
53 QBarSet& operator << (const qreal &value);
54 QBarSet& operator << (const QPointF &value);
54 QBarSet& operator << (const QPointF &value);
55
55
56 void insert(const int index, const qreal value);
56 void insert(const int index, const qreal value);
57 void insert(const int index, const QPointF value);
57 void insert(const int index, const QPointF value);
58 bool remove(const int index, const int count = 1);
58 bool remove(const int index, const int count = 1);
59 void replace(const int index, const qreal value);
59 void replace(const int index, const qreal value);
60 void replace(const int index, const QPointF value);
60 void replace(const int index, const QPointF value);
61 QPointF at(const int index) const;
61 QPointF at(const int index) const;
62 QPointF operator [] (const int index) const;
62 QPointF operator [] (const int index) const;
63 int count() const;
63 int count() const;
64 qreal sum() const;
64 qreal sum() const;
65
65
66 void setPen(const QPen &pen);
66 void setPen(const QPen &pen);
67 QPen pen() const;
67 QPen pen() const;
68
68
69 void setBrush(const QBrush &brush);
69 void setBrush(const QBrush &brush);
70 QBrush brush() const;
70 QBrush brush() const;
71
71
72 void setLabelBrush(const QBrush &brush);
72 void setLabelBrush(const QBrush &brush);
73 QBrush labelBrush() const;
73 QBrush labelBrush() const;
74
74
75 void setLabelFont(const QFont &font);
75 void setLabelFont(const QFont &font);
76 QFont labelFont() const;
76 QFont labelFont() const;
77
77
78 Q_SIGNALS:
78 Q_SIGNALS:
79 void nameChanged();
80 void penChanged();
79 void penChanged();
81 void brushChanged();
80 void brushChanged();
82 void labelChanged();
81 void labelChanged();
83 void labelBrushChanged();
82 void labelBrushChanged();
84 void labelFontChanged();
83 void labelFontChanged();
85
84
86 void valuesAdded(int index, int count);
85 void valuesAdded(int index, int count);
87 void valuesRemoved(int index, int count);
86 void valuesRemoved(int index, int count);
88 void valueChanged(int index);
87 void valueChanged(int index);
89
88
90 private:
89 private:
91 QScopedPointer<QBarSetPrivate> d_ptr;
90 QScopedPointer<QBarSetPrivate> d_ptr;
92 Q_DISABLE_COPY(QBarSet)
91 Q_DISABLE_COPY(QBarSet)
93 friend class QBarSeries;
92 friend class QBarSeries;
94 friend class BarLegendMarker;
93 friend class BarLegendMarker;
95 friend class BarChartItem;
94 friend class BarChartItem;
96 friend class QBarSeriesPrivate;
95 friend class QBarSeriesPrivate;
97 };
96 };
98
97
99 QTCOMMERCIALCHART_END_NAMESPACE
98 QTCOMMERCIALCHART_END_NAMESPACE
100
99
101 #endif // QBARSET_H
100 #endif // QBARSET_H
@@ -1,78 +1,78
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QBARSET_P_H
30 #ifndef QBARSET_P_H
31 #define QBARSET_P_H
31 #define QBARSET_P_H
32
32
33 #include "qbarset.h"
33 #include "qbarset.h"
34 #include <QMap>
34 #include <QMap>
35 #include <QPen>
35 #include <QPen>
36 #include <QBrush>
36 #include <QBrush>
37 #include <QFont>
37 #include <QFont>
38
38
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
40
41 class QBarSetPrivate : public QObject
41 class QBarSetPrivate : public QObject
42 {
42 {
43 Q_OBJECT
43 Q_OBJECT
44
44
45 public:
45 public:
46 QBarSetPrivate(const QString name, QBarSet *parent);
46 QBarSetPrivate(const QString label, QBarSet *parent);
47 ~QBarSetPrivate();
47 ~QBarSetPrivate();
48
48
49 void append(QPointF value);
49 void append(QPointF value);
50 void append(QList<QPointF> values);
50 void append(QList<QPointF> values);
51 void append(QList<qreal> values);
51 void append(QList<qreal> values);
52
52
53 void insert(const int index, const qreal value);
53 void insert(const int index, const qreal value);
54 void insert(const int index, const QPointF value);
54 void insert(const int index, const QPointF value);
55 bool remove(const int index, const int count);
55 bool remove(const int index, const int count);
56
56
57 void replace(const int index, const qreal value);
57 void replace(const int index, const qreal value);
58 void replace(const int index, const QPointF value);
58 void replace(const int index, const QPointF value);
59
59
60 Q_SIGNALS:
60 Q_SIGNALS:
61 void restructuredBars();
61 void restructuredBars();
62 void updatedBars();
62 void updatedBars();
63
63
64 public:
64 public:
65 QBarSet * const q_ptr;
65 QBarSet * const q_ptr;
66 QString m_name;
66 QString m_label;
67 QList<QPointF> m_values;
67 QList<QPointF> m_values;
68 QPen m_pen;
68 QPen m_pen;
69 QBrush m_brush;
69 QBrush m_brush;
70 QBrush m_labelBrush;
70 QBrush m_labelBrush;
71 QFont m_labelFont;
71 QFont m_labelFont;
72
72
73 friend class QBarSet;
73 friend class QBarSet;
74 };
74 };
75
75
76 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
77
77
78 #endif // QBARSETPRIVATE_P_H
78 #endif // QBARSETPRIVATE_P_H
@@ -1,198 +1,198
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "legendmarker_p.h"
21 #include "legendmarker_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "qlegend.h"
24 #include "qlegend.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qpieseries.h"
26 #include "qpieseries.h"
27 #include "qpieslice.h"
27 #include "qpieslice.h"
28 #include "qbarset.h"
28 #include "qbarset.h"
29 #include "qbarset_p.h"
29 #include "qbarset_p.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qareaseries_p.h"
31 #include "qareaseries_p.h"
32 #include <QPainter>
32 #include <QPainter>
33 #include <QGraphicsSceneEvent>
33 #include <QGraphicsSceneEvent>
34 #include <QGraphicsSimpleTextItem>
34 #include <QGraphicsSimpleTextItem>
35 #include <QDebug>
35 #include <QDebug>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
40 QGraphicsObject(legend),
40 QGraphicsObject(legend),
41 m_series(series),
41 m_series(series),
42 m_markerRect(0,0,10.0,10.0),
42 m_markerRect(0,0,10.0,10.0),
43 m_boundingRect(0,0,0,0),
43 m_boundingRect(0,0,0,0),
44 m_legend(legend),
44 m_legend(legend),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
46 m_rectItem(new QGraphicsRectItem(this))
46 m_rectItem(new QGraphicsRectItem(this))
47 {
47 {
48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
49 m_rectItem->setRect(m_markerRect);
49 m_rectItem->setRect(m_markerRect);
50 updateLayout();
50 updateLayout();
51 }
51 }
52
52
53 void LegendMarker::setPen(const QPen &pen)
53 void LegendMarker::setPen(const QPen &pen)
54 {
54 {
55 m_textItem->setPen(pen);
55 m_textItem->setPen(pen);
56 updateLayout();
56 updateLayout();
57 }
57 }
58
58
59 QPen LegendMarker::pen() const
59 QPen LegendMarker::pen() const
60 {
60 {
61 return m_textItem->pen();
61 return m_textItem->pen();
62 }
62 }
63
63
64 void LegendMarker::setBrush(const QBrush &brush)
64 void LegendMarker::setBrush(const QBrush &brush)
65 {
65 {
66 m_rectItem->setBrush(brush);
66 m_rectItem->setBrush(brush);
67 }
67 }
68
68
69 QBrush LegendMarker::brush() const
69 QBrush LegendMarker::brush() const
70 {
70 {
71 return m_rectItem->brush();
71 return m_rectItem->brush();
72 }
72 }
73
73
74 void LegendMarker::setLabel(const QString name)
74 void LegendMarker::setLabel(const QString label)
75 {
75 {
76 m_textItem->setText(name);
76 m_textItem->setText(label);
77 updateLayout();
77 updateLayout();
78 }
78 }
79
79
80 void LegendMarker::setSize(const QSize& size)
80 void LegendMarker::setSize(const QSize& size)
81 {
81 {
82 m_markerRect = QRectF(0,0,size.width(),size.height());
82 m_markerRect = QRectF(0,0,size.width(),size.height());
83 }
83 }
84
84
85 QString LegendMarker::label() const
85 QString LegendMarker::label() const
86 {
86 {
87 return m_textItem->text();
87 return m_textItem->text();
88 }
88 }
89
89
90 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
90 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
91 {
91 {
92 Q_UNUSED(option)
92 Q_UNUSED(option)
93 Q_UNUSED(widget)
93 Q_UNUSED(widget)
94 Q_UNUSED(painter)
94 Q_UNUSED(painter)
95 }
95 }
96
96
97 QRectF LegendMarker::boundingRect() const
97 QRectF LegendMarker::boundingRect() const
98 {
98 {
99 return m_boundingRect;
99 return m_boundingRect;
100 }
100 }
101
101
102 void LegendMarker::updateLayout()
102 void LegendMarker::updateLayout()
103 {
103 {
104
104
105 static const qreal margin = 2;
105 static const qreal margin = 2;
106 static const qreal space = 4;
106 static const qreal space = 4;
107
107
108 const QRectF& textRect = m_textItem->boundingRect();
108 const QRectF& textRect = m_textItem->boundingRect();
109 prepareGeometryChange();
109 prepareGeometryChange();
110 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
110 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
111 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
111 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
112 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
112 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
113
113
114 }
114 }
115
115
116 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
116 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
117 {
117 {
118 QGraphicsObject::mousePressEvent(event);
118 QGraphicsObject::mousePressEvent(event);
119 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
119 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
120 }
120 }
121
121
122 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123
123
124 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
124 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
125 m_series(series)
125 m_series(series)
126 {
126 {
127 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
127 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
128 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
128 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
129 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
129 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
130 updated();
130 updated();
131 }
131 }
132
132
133 void AreaLegendMarker::updated()
133 void AreaLegendMarker::updated()
134 {
134 {
135 setBrush(m_series->brush());
135 setBrush(m_series->brush());
136 setLabel(m_series->name());
136 setLabel(m_series->name());
137 }
137 }
138
138
139 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140
140
141 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
141 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
142 m_barset(barset)
142 m_barset(barset)
143 {
143 {
144 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
144 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
145 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
145 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
146 updated();
146 updated();
147 }
147 }
148
148
149 void BarLegendMarker::updated()
149 void BarLegendMarker::updated()
150 {
150 {
151 setBrush(m_barset->brush());
151 setBrush(m_barset->brush());
152 setLabel(m_barset->name());
152 setLabel(m_barset->label());
153 }
153 }
154
154
155 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
155 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156
156
157 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
157 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
158 m_pieslice(pieslice)
158 m_pieslice(pieslice)
159 {
159 {
160 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
160 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
161 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
161 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
162 updated();
162 updated();
163 }
163 }
164
164
165 void PieLegendMarker::updated()
165 void PieLegendMarker::updated()
166 {
166 {
167 setBrush(m_pieslice->brush());
167 setBrush(m_pieslice->brush());
168 setLabel(m_pieslice->label());
168 setLabel(m_pieslice->label());
169 }
169 }
170
170
171 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
171 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
172
172
173 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
173 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
174 m_series(series)
174 m_series(series)
175 {
175 {
176 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
176 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
177 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
177 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
178 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
178 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
179 updated();
179 updated();
180 }
180 }
181
181
182 void XYLegendMarker::updated()
182 void XYLegendMarker::updated()
183 {
183 {
184 setLabel(m_series->name());
184 setLabel(m_series->name());
185
185
186 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
186 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
187 {
187 {
188 setBrush(m_series->brush());
188 setBrush(m_series->brush());
189
189
190 }
190 }
191 else {
191 else {
192 setBrush(QBrush(m_series->pen().color()));
192 setBrush(QBrush(m_series->pen().color()));
193 }
193 }
194 }
194 }
195
195
196 #include "moc_legendmarker_p.cpp"
196 #include "moc_legendmarker_p.cpp"
197
197
198 QTCOMMERCIALCHART_END_NAMESPACE
198 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,428 +1,428
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtTest/QtTest>
21 #include <QtTest/QtTest>
22 #include <qbarset.h>
22 #include <qbarset.h>
23 #include <qgroupedbarseries.h>
23 #include <qgroupedbarseries.h>
24 #include <qchartview.h>
24 #include <qchartview.h>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 class tst_QBarSet : public QObject
28 class tst_QBarSet : public QObject
29 {
29 {
30 Q_OBJECT
30 Q_OBJECT
31
31
32 public slots:
32 public slots:
33 void initTestCase();
33 void initTestCase();
34 void cleanupTestCase();
34 void cleanupTestCase();
35 void init();
35 void init();
36 void cleanup();
36 void cleanup();
37
37
38 private slots:
38 private slots:
39 void qbarset_data();
39 void qbarset_data();
40 void qbarset();
40 void qbarset();
41 void name_data();
41 void label_data();
42 void name();
42 void label();
43 void append_data();
43 void append_data();
44 void append();
44 void append();
45 void appendOperator_data();
45 void appendOperator_data();
46 void appendOperator();
46 void appendOperator();
47 void insert_data();
47 void insert_data();
48 void insert();
48 void insert();
49 void remove_data();
49 void remove_data();
50 void remove();
50 void remove();
51 void replace_data();
51 void replace_data();
52 void replace();
52 void replace();
53 void at_data();
53 void at_data();
54 void at();
54 void at();
55 void atOperator_data();
55 void atOperator_data();
56 void atOperator();
56 void atOperator();
57 void count_data();
57 void count_data();
58 void count();
58 void count();
59 void sum_data();
59 void sum_data();
60 void sum();
60 void sum();
61 void customize();
61 void customize();
62
62
63 private:
63 private:
64 QBarSet* m_barset;
64 QBarSet* m_barset;
65 };
65 };
66
66
67 void tst_QBarSet::initTestCase()
67 void tst_QBarSet::initTestCase()
68 {
68 {
69 }
69 }
70
70
71 void tst_QBarSet::cleanupTestCase()
71 void tst_QBarSet::cleanupTestCase()
72 {
72 {
73 }
73 }
74
74
75 void tst_QBarSet::init()
75 void tst_QBarSet::init()
76 {
76 {
77 m_barset = new QBarSet(QString("Name"));
77 m_barset = new QBarSet(QString("label"));
78 }
78 }
79
79
80 void tst_QBarSet::cleanup()
80 void tst_QBarSet::cleanup()
81 {
81 {
82 delete m_barset;
82 delete m_barset;
83 m_barset = 0;
83 m_barset = 0;
84 }
84 }
85
85
86 void tst_QBarSet::qbarset_data()
86 void tst_QBarSet::qbarset_data()
87 {
87 {
88 }
88 }
89
89
90 void tst_QBarSet::qbarset()
90 void tst_QBarSet::qbarset()
91 {
91 {
92 QBarSet barset(QString("Name"));
92 QBarSet barset(QString("label"));
93 QCOMPARE(barset.name(), QString("Name"));
93 QCOMPARE(barset.label(), QString("label"));
94 QCOMPARE(barset.count(), 0);
94 QCOMPARE(barset.count(), 0);
95 QVERIFY(qFuzzyIsNull(barset.sum()));
95 QVERIFY(qFuzzyIsNull(barset.sum()));
96 }
96 }
97
97
98 void tst_QBarSet::name_data()
98 void tst_QBarSet::label_data()
99 {
99 {
100 QTest::addColumn<QString> ("name");
100 QTest::addColumn<QString> ("label");
101 QTest::addColumn<QString> ("result");
101 QTest::addColumn<QString> ("result");
102 QTest::newRow("name0") << QString("name0") << QString("name0");
102 QTest::newRow("label0") << QString("label0") << QString("label0");
103 QTest::newRow("name1") << QString("name1") << QString("name1");
103 QTest::newRow("label1") << QString("label1") << QString("label1");
104 }
104 }
105
105
106 void tst_QBarSet::name()
106 void tst_QBarSet::label()
107 {
107 {
108 QFETCH(QString, name);
108 QFETCH(QString, label);
109 QFETCH(QString, result);
109 QFETCH(QString, result);
110
110
111 QSignalSpy nameSpy(m_barset,SIGNAL(nameChanged()));
111 QSignalSpy labelSpy(m_barset,SIGNAL(labelChanged()));
112 m_barset->setName(name);
112 m_barset->setLabel(label);
113 QCOMPARE(m_barset->name(), result);
113 QCOMPARE(m_barset->label(), result);
114 QVERIFY(nameSpy.count() == 1);
114 QVERIFY(labelSpy.count() == 1);
115 }
115 }
116
116
117 void tst_QBarSet::append_data()
117 void tst_QBarSet::append_data()
118 {
118 {
119 QTest::addColumn<int> ("count");
119 QTest::addColumn<int> ("count");
120 QTest::newRow("0") << 0;
120 QTest::newRow("0") << 0;
121 QTest::newRow("5") << 5;
121 QTest::newRow("5") << 5;
122 QTest::newRow("100") << 100;
122 QTest::newRow("100") << 100;
123 QTest::newRow("1000") << 1000;
123 QTest::newRow("1000") << 1000;
124 }
124 }
125
125
126 void tst_QBarSet::append()
126 void tst_QBarSet::append()
127 {
127 {
128 QFETCH(int, count);
128 QFETCH(int, count);
129
129
130 QCOMPARE(m_barset->count(), 0);
130 QCOMPARE(m_barset->count(), 0);
131 QVERIFY(qFuzzyIsNull(m_barset->sum()));
131 QVERIFY(qFuzzyIsNull(m_barset->sum()));
132
132
133 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
133 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
134
134
135 qreal sum(0.0);
135 qreal sum(0.0);
136 qreal value(0.0);
136 qreal value(0.0);
137
137
138 for (int i=0; i<count; i++) {
138 for (int i=0; i<count; i++) {
139 m_barset->append(value);
139 m_barset->append(value);
140 QCOMPARE(m_barset->at(i).y(), value);
140 QCOMPARE(m_barset->at(i).y(), value);
141 sum += value;
141 sum += value;
142 value += 1.0;
142 value += 1.0;
143 }
143 }
144
144
145 QCOMPARE(m_barset->count(), count);
145 QCOMPARE(m_barset->count(), count);
146 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
146 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
147
147
148 QVERIFY(valueSpy.count() == count);
148 QVERIFY(valueSpy.count() == count);
149 }
149 }
150
150
151 void tst_QBarSet::appendOperator_data()
151 void tst_QBarSet::appendOperator_data()
152 {
152 {
153 append_data();
153 append_data();
154 }
154 }
155
155
156 void tst_QBarSet::appendOperator()
156 void tst_QBarSet::appendOperator()
157 {
157 {
158 QFETCH(int, count);
158 QFETCH(int, count);
159
159
160 QCOMPARE(m_barset->count(), 0);
160 QCOMPARE(m_barset->count(), 0);
161 QVERIFY(qFuzzyIsNull(m_barset->sum()));
161 QVERIFY(qFuzzyIsNull(m_barset->sum()));
162
162
163 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
163 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
164
164
165 qreal sum(0.0);
165 qreal sum(0.0);
166 qreal value(0.0);
166 qreal value(0.0);
167
167
168 for (int i=0; i<count; i++) {
168 for (int i=0; i<count; i++) {
169 *m_barset << value;
169 *m_barset << value;
170 QCOMPARE(m_barset->at(i).y(), value);
170 QCOMPARE(m_barset->at(i).y(), value);
171 sum += value;
171 sum += value;
172 value += 1.0;
172 value += 1.0;
173 }
173 }
174
174
175 QCOMPARE(m_barset->count(), count);
175 QCOMPARE(m_barset->count(), count);
176 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
176 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
177 QVERIFY(valueSpy.count() == count);
177 QVERIFY(valueSpy.count() == count);
178
178
179 }
179 }
180
180
181 void tst_QBarSet::insert_data()
181 void tst_QBarSet::insert_data()
182 {
182 {
183 }
183 }
184
184
185 void tst_QBarSet::insert()
185 void tst_QBarSet::insert()
186 {
186 {
187 QCOMPARE(m_barset->count(), 0);
187 QCOMPARE(m_barset->count(), 0);
188 QVERIFY(qFuzzyIsNull(m_barset->sum()));
188 QVERIFY(qFuzzyIsNull(m_barset->sum()));
189 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
189 QSignalSpy valueSpy(m_barset,SIGNAL(valuesAdded(int,int)));
190
190
191 m_barset->insert(0, 1.0); // 1.0
191 m_barset->insert(0, 1.0); // 1.0
192 QCOMPARE(m_barset->at(0).y(), 1.0);
192 QCOMPARE(m_barset->at(0).y(), 1.0);
193 QCOMPARE(m_barset->count(), 1);
193 QCOMPARE(m_barset->count(), 1);
194 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
194 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
195
195
196 m_barset->insert(0, 2.0); // 2.0 1.0
196 m_barset->insert(0, 2.0); // 2.0 1.0
197 QCOMPARE(m_barset->at(0).y(), 2.0);
197 QCOMPARE(m_barset->at(0).y(), 2.0);
198 QCOMPARE(m_barset->at(1).y(), 1.0);
198 QCOMPARE(m_barset->at(1).y(), 1.0);
199 QCOMPARE(m_barset->count(), 2);
199 QCOMPARE(m_barset->count(), 2);
200 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
200 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
201
201
202 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
202 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
203 QCOMPARE(m_barset->at(1).y(), 3.0);
203 QCOMPARE(m_barset->at(1).y(), 3.0);
204 QCOMPARE(m_barset->at(0).y(), 2.0);
204 QCOMPARE(m_barset->at(0).y(), 2.0);
205 QCOMPARE(m_barset->at(2).y(), 1.0);
205 QCOMPARE(m_barset->at(2).y(), 1.0);
206 QCOMPARE(m_barset->count(), 3);
206 QCOMPARE(m_barset->count(), 3);
207 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
207 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
208 QVERIFY(valueSpy.count() == 3);
208 QVERIFY(valueSpy.count() == 3);
209 }
209 }
210
210
211 void tst_QBarSet::remove_data()
211 void tst_QBarSet::remove_data()
212 {
212 {
213 }
213 }
214
214
215 void tst_QBarSet::remove()
215 void tst_QBarSet::remove()
216 {
216 {
217 QCOMPARE(m_barset->count(), 0);
217 QCOMPARE(m_barset->count(), 0);
218 QVERIFY(qFuzzyIsNull(m_barset->sum()));
218 QVERIFY(qFuzzyIsNull(m_barset->sum()));
219
219
220 QSignalSpy valueSpy(m_barset,SIGNAL(valuesRemoved(int,int)));
220 QSignalSpy valueSpy(m_barset,SIGNAL(valuesRemoved(int,int)));
221
221
222 m_barset->append(1.0);
222 m_barset->append(1.0);
223 m_barset->append(2.0);
223 m_barset->append(2.0);
224 m_barset->append(3.0);
224 m_barset->append(3.0);
225 m_barset->append(4.0);
225 m_barset->append(4.0);
226
226
227 QCOMPARE(m_barset->count(), 4);
227 QCOMPARE(m_barset->count(), 4);
228 QCOMPARE(m_barset->sum(), 10.0);
228 QCOMPARE(m_barset->sum(), 10.0);
229
229
230 m_barset->remove(2); // 1.0 2.0 4.0
230 m_barset->remove(2); // 1.0 2.0 4.0
231 QCOMPARE(m_barset->at(0).y(), 1.0);
231 QCOMPARE(m_barset->at(0).y(), 1.0);
232 QCOMPARE(m_barset->at(1).y(), 2.0);
232 QCOMPARE(m_barset->at(1).y(), 2.0);
233 QCOMPARE(m_barset->at(2).y(), 4.0);
233 QCOMPARE(m_barset->at(2).y(), 4.0);
234 QCOMPARE(m_barset->count(), 3);
234 QCOMPARE(m_barset->count(), 3);
235 QCOMPARE(m_barset->sum(), 7.0);
235 QCOMPARE(m_barset->sum(), 7.0);
236
236
237 m_barset->remove(0); // 2.0 4.0
237 m_barset->remove(0); // 2.0 4.0
238 QCOMPARE(m_barset->at(0).y(), 2.0);
238 QCOMPARE(m_barset->at(0).y(), 2.0);
239 QCOMPARE(m_barset->at(1).y(), 4.0);
239 QCOMPARE(m_barset->at(1).y(), 4.0);
240 QCOMPARE(m_barset->count(), 2);
240 QCOMPARE(m_barset->count(), 2);
241 QCOMPARE(m_barset->sum(), 6.0);
241 QCOMPARE(m_barset->sum(), 6.0);
242
242
243 QVERIFY(valueSpy.count() == 2);
243 QVERIFY(valueSpy.count() == 2);
244 }
244 }
245
245
246 void tst_QBarSet::replace_data()
246 void tst_QBarSet::replace_data()
247 {
247 {
248
248
249 }
249 }
250
250
251 void tst_QBarSet::replace()
251 void tst_QBarSet::replace()
252 {
252 {
253 QCOMPARE(m_barset->count(), 0);
253 QCOMPARE(m_barset->count(), 0);
254 QVERIFY(qFuzzyIsNull(m_barset->sum()));
254 QVERIFY(qFuzzyIsNull(m_barset->sum()));
255 QSignalSpy valueSpy(m_barset,SIGNAL(valueChanged(int)));
255 QSignalSpy valueSpy(m_barset,SIGNAL(valueChanged(int)));
256
256
257 m_barset->append(1.0);
257 m_barset->append(1.0);
258 m_barset->append(2.0);
258 m_barset->append(2.0);
259 m_barset->append(3.0);
259 m_barset->append(3.0);
260 m_barset->append(4.0);
260 m_barset->append(4.0);
261
261
262 QCOMPARE(m_barset->count(), 4);
262 QCOMPARE(m_barset->count(), 4);
263 QCOMPARE(m_barset->sum(), 10.0);
263 QCOMPARE(m_barset->sum(), 10.0);
264
264
265 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
265 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
266 QCOMPARE(m_barset->count(), 4);
266 QCOMPARE(m_barset->count(), 4);
267 QCOMPARE(m_barset->sum(), 14.0);
267 QCOMPARE(m_barset->sum(), 14.0);
268 QCOMPARE(m_barset->at(0).y(), 5.0);
268 QCOMPARE(m_barset->at(0).y(), 5.0);
269
269
270 m_barset->replace(3, 6.0);
270 m_barset->replace(3, 6.0);
271 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
271 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
272 QCOMPARE(m_barset->sum(), 16.0);
272 QCOMPARE(m_barset->sum(), 16.0);
273 QCOMPARE(m_barset->at(0).y(), 5.0);
273 QCOMPARE(m_barset->at(0).y(), 5.0);
274 QCOMPARE(m_barset->at(1).y(), 2.0);
274 QCOMPARE(m_barset->at(1).y(), 2.0);
275 QCOMPARE(m_barset->at(2).y(), 3.0);
275 QCOMPARE(m_barset->at(2).y(), 3.0);
276 QCOMPARE(m_barset->at(3).y(), 6.0);
276 QCOMPARE(m_barset->at(3).y(), 6.0);
277
277
278 QVERIFY(valueSpy.count() == 2);
278 QVERIFY(valueSpy.count() == 2);
279 }
279 }
280
280
281 void tst_QBarSet::at_data()
281 void tst_QBarSet::at_data()
282 {
282 {
283
283
284 }
284 }
285
285
286 void tst_QBarSet::at()
286 void tst_QBarSet::at()
287 {
287 {
288 QCOMPARE(m_barset->count(), 0);
288 QCOMPARE(m_barset->count(), 0);
289 QVERIFY(qFuzzyIsNull(m_barset->sum()));
289 QVERIFY(qFuzzyIsNull(m_barset->sum()));
290
290
291 m_barset->append(1.0);
291 m_barset->append(1.0);
292 m_barset->append(2.0);
292 m_barset->append(2.0);
293 m_barset->append(3.0);
293 m_barset->append(3.0);
294 m_barset->append(4.0);
294 m_barset->append(4.0);
295
295
296 QCOMPARE(m_barset->at(0).y(), 1.0);
296 QCOMPARE(m_barset->at(0).y(), 1.0);
297 QCOMPARE(m_barset->at(1).y(), 2.0);
297 QCOMPARE(m_barset->at(1).y(), 2.0);
298 QCOMPARE(m_barset->at(2).y(), 3.0);
298 QCOMPARE(m_barset->at(2).y(), 3.0);
299 QCOMPARE(m_barset->at(3).y(), 4.0);
299 QCOMPARE(m_barset->at(3).y(), 4.0);
300 }
300 }
301
301
302 void tst_QBarSet::atOperator_data()
302 void tst_QBarSet::atOperator_data()
303 {
303 {
304
304
305 }
305 }
306
306
307 void tst_QBarSet::atOperator()
307 void tst_QBarSet::atOperator()
308 {
308 {
309 QCOMPARE(m_barset->count(), 0);
309 QCOMPARE(m_barset->count(), 0);
310 QVERIFY(qFuzzyIsNull(m_barset->sum()));
310 QVERIFY(qFuzzyIsNull(m_barset->sum()));
311
311
312 m_barset->append(1.0);
312 m_barset->append(1.0);
313 m_barset->append(2.0);
313 m_barset->append(2.0);
314 m_barset->append(3.0);
314 m_barset->append(3.0);
315 m_barset->append(4.0);
315 m_barset->append(4.0);
316
316
317 QCOMPARE(m_barset->operator [](0).y(), 1.0);
317 QCOMPARE(m_barset->operator [](0).y(), 1.0);
318 QCOMPARE(m_barset->operator [](1).y(), 2.0);
318 QCOMPARE(m_barset->operator [](1).y(), 2.0);
319 QCOMPARE(m_barset->operator [](2).y(), 3.0);
319 QCOMPARE(m_barset->operator [](2).y(), 3.0);
320 QCOMPARE(m_barset->operator [](3).y(), 4.0);
320 QCOMPARE(m_barset->operator [](3).y(), 4.0);
321 }
321 }
322
322
323 void tst_QBarSet::count_data()
323 void tst_QBarSet::count_data()
324 {
324 {
325
325
326 }
326 }
327
327
328 void tst_QBarSet::count()
328 void tst_QBarSet::count()
329 {
329 {
330 QCOMPARE(m_barset->count(), 0);
330 QCOMPARE(m_barset->count(), 0);
331 QVERIFY(qFuzzyIsNull(m_barset->sum()));
331 QVERIFY(qFuzzyIsNull(m_barset->sum()));
332
332
333 m_barset->append(1.0);
333 m_barset->append(1.0);
334 QCOMPARE(m_barset->count(),1);
334 QCOMPARE(m_barset->count(),1);
335 m_barset->append(2.0);
335 m_barset->append(2.0);
336 QCOMPARE(m_barset->count(),2);
336 QCOMPARE(m_barset->count(),2);
337 m_barset->append(3.0);
337 m_barset->append(3.0);
338 QCOMPARE(m_barset->count(),3);
338 QCOMPARE(m_barset->count(),3);
339 m_barset->append(4.0);
339 m_barset->append(4.0);
340 QCOMPARE(m_barset->count(),4);
340 QCOMPARE(m_barset->count(),4);
341 }
341 }
342
342
343 void tst_QBarSet::sum_data()
343 void tst_QBarSet::sum_data()
344 {
344 {
345
345
346 }
346 }
347
347
348 void tst_QBarSet::sum()
348 void tst_QBarSet::sum()
349 {
349 {
350 QCOMPARE(m_barset->count(), 0);
350 QCOMPARE(m_barset->count(), 0);
351 QVERIFY(qFuzzyIsNull(m_barset->sum()));
351 QVERIFY(qFuzzyIsNull(m_barset->sum()));
352
352
353 m_barset->append(1.0);
353 m_barset->append(1.0);
354 QVERIFY(qFuzzyCompare(m_barset->sum(),1.0));
354 QVERIFY(qFuzzyCompare(m_barset->sum(),1.0));
355 m_barset->append(2.0);
355 m_barset->append(2.0);
356 QVERIFY(qFuzzyCompare(m_barset->sum(),3.0));
356 QVERIFY(qFuzzyCompare(m_barset->sum(),3.0));
357 m_barset->append(3.0);
357 m_barset->append(3.0);
358 QVERIFY(qFuzzyCompare(m_barset->sum(),6.0));
358 QVERIFY(qFuzzyCompare(m_barset->sum(),6.0));
359 m_barset->append(4.0);
359 m_barset->append(4.0);
360 QVERIFY(qFuzzyCompare(m_barset->sum(),10.0));
360 QVERIFY(qFuzzyCompare(m_barset->sum(),10.0));
361 }
361 }
362
362
363 void tst_QBarSet::customize()
363 void tst_QBarSet::customize()
364 {
364 {
365 // Create sets
365 // Create sets
366 QBarSet *set1 = new QBarSet("set1");
366 QBarSet *set1 = new QBarSet("set1");
367 QBarSet *set2 = new QBarSet("set2");
367 QBarSet *set2 = new QBarSet("set2");
368
368
369 // Append set1 to series
369 // Append set1 to series
370 QGroupedBarSeries *series = new QGroupedBarSeries();
370 QGroupedBarSeries *series = new QGroupedBarSeries();
371 bool success = series->append(set1);
371 bool success = series->append(set1);
372 QVERIFY(success);
372 QVERIFY(success);
373
373
374 // Add series to the chart
374 // Add series to the chart
375 QChartView view(new QChart());
375 QChartView view(new QChart());
376 view.resize(200, 200);
376 view.resize(200, 200);
377 view.chart()->addSeries(series);
377 view.chart()->addSeries(series);
378 view.show();
378 view.show();
379 QTest::qWaitForWindowShown(&view);
379 QTest::qWaitForWindowShown(&view);
380
380
381 // Test adding data to the sets
381 // Test adding data to the sets
382 *set1 << 1 << 2 << 1 << 3;
382 *set1 << 1 << 2 << 1 << 3;
383 *set2 << 2 << 1 << 3 << 1;
383 *set2 << 2 << 1 << 3 << 1;
384
384
385 // Test pen
385 // Test pen
386 QVERIFY(set1->pen() != QPen());
386 QVERIFY(set1->pen() != QPen());
387 QVERIFY(set2->pen() == QPen());
387 QVERIFY(set2->pen() == QPen());
388 QPen pen(QColor(128,128,128,128));
388 QPen pen(QColor(128,128,128,128));
389 set1->setPen(pen);
389 set1->setPen(pen);
390 QVERIFY(set1->pen() == pen);
390 QVERIFY(set1->pen() == pen);
391 QVERIFY(set2->pen() == QPen());
391 QVERIFY(set2->pen() == QPen());
392
392
393 // Test brush
393 // Test brush
394 QVERIFY(set1->brush() != QBrush());
394 QVERIFY(set1->brush() != QBrush());
395 QVERIFY(set2->brush() == QBrush());
395 QVERIFY(set2->brush() == QBrush());
396 QBrush brush(QColor(128,128,128,128));
396 QBrush brush(QColor(128,128,128,128));
397 set1->setBrush(brush);
397 set1->setBrush(brush);
398 QVERIFY(set1->brush() == brush);
398 QVERIFY(set1->brush() == brush);
399 QVERIFY(set2->brush() == QBrush());
399 QVERIFY(set2->brush() == QBrush());
400
400
401 // Test label brush
401 // Test label brush
402 QVERIFY(set1->labelBrush() != QBrush());
402 QVERIFY(set1->labelBrush() != QBrush());
403 QVERIFY(set2->labelBrush() == QBrush());
403 QVERIFY(set2->labelBrush() == QBrush());
404 set1->setLabelBrush(brush);
404 set1->setLabelBrush(brush);
405 QVERIFY(set1->labelBrush() == brush);
405 QVERIFY(set1->labelBrush() == brush);
406 QVERIFY(set2->labelBrush() == QBrush());
406 QVERIFY(set2->labelBrush() == QBrush());
407
407
408 // Test label font
408 // Test label font
409 // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the
409 // Note: QFont empty constructor creates font with application's default font, so the font may or may not be the
410 // same for the set added to the series (depending on the QChart's theme configuration)
410 // same for the set added to the series (depending on the QChart's theme configuration)
411 QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont());
411 QVERIFY(set1->labelFont() != QFont() || set1->labelFont() == QFont());
412 QVERIFY(set2->labelFont() == QFont());
412 QVERIFY(set2->labelFont() == QFont());
413 QFont font;
413 QFont font;
414 font.setBold(true);
414 font.setBold(true);
415 font.setItalic(true);
415 font.setItalic(true);
416 set1->setLabelFont(font);
416 set1->setLabelFont(font);
417 QVERIFY(set1->labelFont() == font);
417 QVERIFY(set1->labelFont() == font);
418 QVERIFY(set2->labelFont() == QFont());
418 QVERIFY(set2->labelFont() == QFont());
419
419
420 // Test adding data to the sets
420 // Test adding data to the sets
421 *set1 << 1 << 2 << 1 << 3;
421 *set1 << 1 << 2 << 1 << 3;
422 *set2 << 2 << 1 << 3 << 1;
422 *set2 << 2 << 1 << 3 << 1;
423 }
423 }
424
424
425 QTEST_MAIN(tst_QBarSet)
425 QTEST_MAIN(tst_QBarSet)
426
426
427 #include "tst_qbarset.moc"
427 #include "tst_qbarset.moc"
428
428
General Comments 0
You need to be logged in to leave comments. Login now