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