##// END OF EJS Templates
updated barseries unit test. fixed found errors.
sauimone -
r1101:5d7dfff0c2e8
parent child
Show More
@@ -1,665 +1,665
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 #include <QAbstractItemModel>
32 32 #include <QModelIndex>
33 33
34 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 35
36 36 /*!
37 37 \class QBarSeries
38 38 \brief part of QtCommercial chart API.
39 39 \mainclass
40 40
41 41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multiple
42 42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
43 43 by QStringList.
44 44
45 45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 46 \image examples_barchart.png
47 47
48 48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 49 */
50 50
51 51 /*!
52 52 \fn void QBarSeries::clicked(QBarSet *barset, QString category)
53 53
54 54 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category
55 55 contained by the series.
56 56 */
57 57
58 58 /*!
59 59 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
60 60
61 61 The signal is emitted if mouse is hovered on top of series.
62 62 Parameter \a barset is the pointer of barset, where hover happened.
63 63 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
64 64 */
65 65
66 66 /*!
67 67 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
68 68 QBarSeries is QObject which is a child of a \a parent.
69 69 */
70 70 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) :
71 71 QAbstractSeries(*new QBarSeriesPrivate(categories, this),parent)
72 72 {
73 73 }
74 74
75 75 /*!
76 76 Destructs barseries and owned barsets.
77 77 */
78 78 QBarSeries::~QBarSeries()
79 79 {
80 80 // NOTE: d_ptr destroyed by QObject
81 81 }
82 82
83 83 /*!
84 84 \internal
85 85 */
86 86 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
87 87 QAbstractSeries(d,parent)
88 88 {
89 89 }
90 90
91 91 /*!
92 92 Returns the type of series. Derived classes override this.
93 93 */
94 94 QAbstractSeries::QSeriesType QBarSeries::type() const
95 95 {
96 96 return QAbstractSeries::SeriesTypeBar;
97 97 }
98 98
99 99 /*!
100 100 Adds a set of bars to series. Takes ownership of \a set.
101 101 */
102 102 void QBarSeries::appendBarSet(QBarSet *set)
103 103 {
104 104 Q_D(QBarSeries);
105 105 d->m_barSets.append(set);
106 106 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
107 107 emit d->restructuredBars();
108 108 }
109 109
110 110 /*!
111 111 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
112 112 */
113 113 void QBarSeries::removeBarSet(QBarSet *set)
114 114 {
115 115 Q_D(QBarSeries);
116 116 if (d->m_barSets.contains(set)) {
117 117 d->m_barSets.removeOne(set);
118 QObject::disconnect(set, SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
118 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
119 119 emit d->restructuredBars();
120 120 }
121 121 }
122 122
123 123 /*!
124 124 Adds a list of barsets to series. Takes ownership of \a sets.
125 125 */
126 126 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
127 127 {
128 128 Q_D(QBarSeries);
129 129 foreach (QBarSet* set, sets) {
130 130 d->m_barSets.append(set);
131 QObject::connect(set, SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
131 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
132 132 }
133 133 emit d->restructuredBars();
134 134 }
135 135
136 136 /*!
137 137 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
138 138 */
139 139 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
140 140 {
141 141 Q_D(QBarSeries);
142 142
143 143 foreach (QBarSet* set, sets) {
144 144 if (d->m_barSets.contains(set)) {
145 145 d->m_barSets.removeOne(set);
146 QObject::disconnect(set, SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
146 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
147 147 }
148 148 }
149 149 emit d->restructuredBars();
150 150 }
151 151
152 152 /*!
153 153 Returns number of sets in series.
154 154 */
155 155 int QBarSeries::barsetCount() const
156 156 {
157 157 Q_D(const QBarSeries);
158 158 return d->m_barSets.count();
159 159 }
160 160
161 161 /*!
162 162 Returns number of categories in series
163 163 */
164 164 int QBarSeries::categoryCount() const
165 165 {
166 166 Q_D(const QBarSeries);
167 167 return d->m_categories.count();
168 168 }
169 169
170 170 /*!
171 171 Returns a list of sets in series. Keeps ownership of sets.
172 172 */
173 173 QList<QBarSet*> QBarSeries::barSets() const
174 174 {
175 175 Q_D(const QBarSeries);
176 176 return d->m_barSets;
177 177 }
178 178
179 179 /*!
180 180 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
181 181 Sets the \a model to be used as a data source
182 182 */
183 183 bool QBarSeries::setModel(QAbstractItemModel *model)
184 184 {
185 185 Q_D(QBarSeries);
186 186 return d->setModel(model);
187 187 }
188 188
189 189 /*!
190 190 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
191 191 Sets column/row specified by \a categories to be used as a list of bar series categories.
192 192 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
193 193 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
194 194 All the columns/rows inbetween those two values are also used as data for bar sets.
195 195 The \a orientation parameter specifies whether the data is in columns or in rows.
196 196 */
197 197 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
198 198 {
199 199 Q_D(QBarSeries);
200 200 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
201 201 }
202 202
203 203 void QBarSeries::setModelMappingRange(int first, int count)
204 204 {
205 205 Q_D(QBarSeries);
206 206 d->setModelMappingRange(first, count);
207 207 }
208 208
209 209 /*!
210 210 Returns the bar categories of the series.
211 211 */
212 212 QBarCategories QBarSeries::categories() const
213 213 {
214 214 Q_D(const QBarSeries);
215 215 return d->m_categories;
216 216 }
217 217
218 218 /*!
219 219 Sets the visibility of labels in series to \a visible
220 220 */
221 221 void QBarSeries::setLabelsVisible(bool visible)
222 222 {
223 223 foreach (QBarSet* s, barSets()) {
224 224 s->setLabelsVisible(visible);
225 225 }
226 226 }
227 227
228 228 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
229 229
230 230 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) :
231 231 QAbstractSeriesPrivate(q),
232 232 m_categories(categories),
233 233 m_mapCategories(-1),
234 234 m_mapBarBottom(-1),
235 235 m_mapBarTop(-1)
236 236 {
237 237 }
238 238
239 239 QBarSet* QBarSeriesPrivate::barsetAt(int index)
240 240 {
241 241 return m_barSets.at(index);
242 242 }
243 243
244 244 QString QBarSeriesPrivate::categoryName(int category)
245 245 {
246 246 return m_categories.at(category);
247 247 }
248 248
249 249 qreal QBarSeriesPrivate::min()
250 250 {
251 251 if (m_barSets.count() <= 0) {
252 252 return 0;
253 253 }
254 254 qreal min = INT_MAX;
255 255
256 256 for (int i = 0; i < m_barSets.count(); i++) {
257 257 int categoryCount = m_barSets.at(i)->count();
258 258 for (int j = 0; j < categoryCount; j++) {
259 259 qreal temp = m_barSets.at(i)->at(j);
260 260 if (temp < min)
261 261 min = temp;
262 262 }
263 263 }
264 264 return min;
265 265 }
266 266
267 267 qreal QBarSeriesPrivate::max()
268 268 {
269 269 if (m_barSets.count() <= 0) {
270 270 return 0;
271 271 }
272 272 qreal max = INT_MIN;
273 273
274 274 for (int i = 0; i < m_barSets.count(); i++) {
275 275 int categoryCount = m_barSets.at(i)->count();
276 276 for (int j = 0; j < categoryCount; j++) {
277 277 qreal temp = m_barSets.at(i)->at(j);
278 278 if (temp > max)
279 279 max = temp;
280 280 }
281 281 }
282 282
283 283 return max;
284 284 }
285 285
286 286 qreal QBarSeriesPrivate::valueAt(int set, int category)
287 287 {
288 288 if ((set < 0) || (set >= m_barSets.count())) {
289 289 // No set, no value.
290 290 return 0;
291 291 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
292 292 // No category, no value.
293 293 return 0;
294 294 }
295 295
296 296 return m_barSets.at(set)->at(category);
297 297 }
298 298
299 299 qreal QBarSeriesPrivate::percentageAt(int set, int category)
300 300 {
301 301 if ((set < 0) || (set >= m_barSets.count())) {
302 302 // No set, no value.
303 303 return 0;
304 304 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
305 305 // No category, no value.
306 306 return 0;
307 307 }
308 308
309 309 qreal value = m_barSets.at(set)->at(category);
310 310 qreal sum = categorySum(category);
311 311 if ( qFuzzyIsNull(sum) ) {
312 312 return 0;
313 313 }
314 314
315 315 return value / sum;
316 316 }
317 317
318 318 qreal QBarSeriesPrivate::categorySum(int category)
319 319 {
320 320 qreal sum(0);
321 321 int count = m_barSets.count(); // Count sets
322 322 for (int set = 0; set < count; set++) {
323 323 if (category < m_barSets.at(set)->count())
324 324 sum += m_barSets.at(set)->at(category);
325 325 }
326 326 return sum;
327 327 }
328 328
329 329 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
330 330 {
331 331 qreal sum(0);
332 332 int count = m_barSets.count(); // Count sets
333 333 for (int set = 0; set < count; set++) {
334 334 if (category < m_barSets.at(set)->count())
335 335 sum += qAbs(m_barSets.at(set)->at(category));
336 336 }
337 337 return sum;
338 338 }
339 339
340 340 qreal QBarSeriesPrivate::maxCategorySum()
341 341 {
342 342 qreal max = INT_MIN;
343 343 int count = m_categories.count();
344 344 for (int i = 0; i < count; i++) {
345 345 qreal sum = categorySum(i);
346 346 if (sum > max)
347 347 max = sum;
348 348 }
349 349 return max;
350 350 }
351 351
352 352 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
353 353 {
354 354 // disconnect signals from old model
355 355 if(m_model)
356 356 {
357 357 disconnect(m_model, 0, this, 0);
358 358 m_mapCategories = -1;
359 359 m_mapBarBottom = -1;
360 360 m_mapBarTop = -1;
361 361 m_mapOrientation = Qt::Vertical;
362 362 }
363 363
364 364 // set new model
365 365 if(model)
366 366 {
367 367 m_model = model;
368 368 return true;
369 369 }
370 370 else
371 371 {
372 372 m_model = 0;
373 373 return false;
374 374 }
375 375 }
376 376
377 377 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
378 378 {
379 379 Q_Q(QBarSeries);
380 380
381 381 if (m_model == 0)
382 382 return;
383 383
384 384 m_mapCategories = categories;
385 385 m_mapBarBottom = bottomBoundry;
386 386 m_mapBarTop = topBoundry;
387 387 m_mapOrientation = orientation;
388 388
389 389 // connect the signals
390 390 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
391 391 if (m_mapOrientation == Qt::Vertical) {
392 392 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
393 393 connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
394 394 } else {
395 395 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
396 396 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
397 397 }
398 398
399 399 // create the initial bars
400 400 m_categories.clear();
401 401 if (m_mapOrientation == Qt::Vertical) {
402 402 int rowCount = 0;
403 403 if(m_mapCount == -1)
404 404 rowCount = m_model->rowCount() - m_mapFirst;
405 405 else
406 406 rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
407 407 for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
408 408 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
409 409 }
410 410
411 411 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
412 412 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
413 413 for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
414 414 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
415 415 q->appendBarSet(barSet);
416 416 }
417 417 } else {
418 418 int columnCount = 0;
419 419 if(m_mapCount == -1)
420 420 columnCount = m_model->columnCount() - m_mapFirst;
421 421 else
422 422 columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
423 423 for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
424 424 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
425 425 }
426 426
427 427 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
428 428 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
429 429 for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
430 430 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
431 431 q->appendBarSet(barSet);
432 432 }
433 433 }
434 434 }
435 435
436 436 void QBarSeriesPrivate::setModelMappingRange(int first, int count)
437 437 {
438 438 m_mapFirst = first;
439 439 m_mapCount = count;
440 440 }
441 441
442 442 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
443 443 {
444 444 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
445 445 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
446 446 if (m_mapOrientation == Qt::Vertical)
447 447 {
448 448 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
449 449 if ( row >= m_mapFirst && (m_mapCount == - 1 || row < m_mapFirst + m_mapCount)) {
450 450 if (column >= m_mapBarBottom && column <= m_mapBarTop)
451 451 barsetAt(column - m_mapBarBottom)->replace(row - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
452 452 // if (column == m_mapCategories);// TODO:
453 453 }
454 454 }
455 455 else
456 456 {
457 457 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
458 458 if (column >= m_mapFirst && (m_mapCount == - 1 || column < m_mapFirst + m_mapCount)) {
459 459 if (row >= m_mapBarBottom && row <= m_mapBarTop)
460 460 barsetAt(row - m_mapBarBottom)->replace(column - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
461 461 // if (row == m_mapCategories);// TODO:
462 462 }
463 463 }
464 464 }
465 465 }
466 466 }
467 467
468 468 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
469 469 {
470 470 Q_UNUSED(parent);
471 471 Q_UNUSED(start);
472 472 Q_UNUSED(end);
473 473 initializeDataFromModel();
474 474 // // series uses model as a data sourceupda
475 475 // int addedCount = end - start + 1;
476 476 // if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
477 477 // return;
478 478 // } else {
479 479
480 480 // for (int bar = m_mapBarBottom; bar <= m_mapBarTop; bar++) {
481 481 // QBarSet *barSet = barsetAt(bar - m_mapBarBottom);
482 482 // // adding items to unlimited map
483 483 // if (m_mapCount == -1 && start >= m_mapFirst) {
484 484 // for (int i = start; i <= end; i++) {
485 485 // if (bar == m_mapBarBottom)
486 486 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
487 487 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
488 488 // }
489 489 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
490 490 // // not all newly added items
491 491 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
492 492 // if (bar == m_mapBarBottom)
493 493 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
494 494 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
495 495 // }
496 496 // }
497 497
498 498 // // adding items to limited map
499 499 // else if (start >= m_mapFirst) {
500 500 // // remove the items that will no longer fit into the map
501 501 // // int toRemove = addedCount - (count - points().size());
502 502 // for (int i = start; i <= end; i++) {
503 503 // if (bar == m_mapBarBottom)
504 504 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
505 505 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
506 506 // }
507 507 // if (m_barSets.size() > m_mapCount)
508 508 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
509 509 // if (bar == m_mapBarBottom)
510 510 // removeCategory(i);
511 511 // barSet->remove(i);
512 512 // }
513 513 // } else {
514 514 // //
515 515 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
516 516 // if (bar == m_mapBarBottom)
517 517 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
518 518 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
519 519 // }
520 520 // if (m_barSets.size() > m_mapCount)
521 521 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
522 522 // if (bar == m_mapBarBottom)
523 523 // removeCategory(i);
524 524 // barSet->remove(i);
525 525 // }
526 526 // }
527 527 // }
528 528 // emit restructuredBars();
529 529 // emit barsetChanged();
530 530 // emit categoriesUpdated();
531 531 // }
532 532 }
533 533
534 534 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
535 535 {
536 536 Q_UNUSED(parent);
537 537 Q_UNUSED(start);
538 538 Q_UNUSED(end);
539 539 initializeDataFromModel();
540 540 }
541 541
542 542 void QBarSeriesPrivate::initializeDataFromModel()
543 543 {
544 544 Q_Q(QBarSeries);
545 545
546 546 if (m_model == 0)
547 547 return;
548 548
549 549 // connect the signals
550 550 // connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
551 551 // if (m_mapOrientation == Qt::Vertical) {
552 552 // connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
553 553 // connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
554 554 // } else {
555 555 // connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
556 556 // connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
557 557 // }
558 558
559 559 // create the initial bars
560 560 m_categories.clear();
561 561 m_barSets.clear();
562 562 // emit restructuredBars();
563 563 if (m_mapOrientation == Qt::Vertical) {
564 564 int rowCount = 0;
565 565 if(m_mapCount == -1)
566 566 rowCount = m_model->rowCount() - m_mapFirst;
567 567 else
568 568 rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
569 569 for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
570 570 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
571 571 }
572 572
573 573 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
574 574 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
575 575 for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
576 576 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
577 577 q->appendBarSet(barSet);
578 578 }
579 579 } else {
580 580 int columnCount = 0;
581 581 if(m_mapCount == -1)
582 582 columnCount = m_model->columnCount() - m_mapFirst;
583 583 else
584 584 columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
585 585 for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
586 586 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
587 587 }
588 588
589 589 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
590 590 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
591 591 for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
592 592 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
593 593 q->appendBarSet(barSet);
594 594 }
595 595 }
596 596 emit restructuredBars();
597 597 // emit updatedBars();
598 598 }
599 599
600 600 void QBarSeriesPrivate::insertCategory(int index, const QString category)
601 601 {
602 602 m_categories.insert(index, category);
603 603 emit categoriesUpdated();
604 604 }
605 605
606 606 void QBarSeriesPrivate::removeCategory(int index)
607 607 {
608 608 m_categories.removeAt(index);
609 609 emit categoriesUpdated();
610 610 }
611 611
612 612 void QBarSeriesPrivate::barsetChanged()
613 613 {
614 614 emit updatedBars();
615 615 }
616 616
617 617 void QBarSeriesPrivate::scaleDomain(Domain& domain)
618 618 {
619 619 qreal minX(domain.minX());
620 620 qreal minY(domain.minY());
621 621 qreal maxX(domain.maxX());
622 622 qreal maxY(domain.maxY());
623 623 int tickXCount(domain.tickXCount());
624 624 int tickYCount(domain.tickYCount());
625 625
626 626 qreal x = m_categories.count();
627 627 qreal y = max();
628 628 minX = qMin(minX, x);
629 629 minY = qMin(minY, y);
630 630 maxX = qMax(maxX, x);
631 631 maxY = qMax(maxY, y);
632 632 tickXCount = x+1;
633 633
634 634 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
635 635 }
636 636
637 637 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
638 638 {
639 639 Q_Q(QBarSeries);
640 640
641 641 BarChartItem* bar = new BarChartItem(q,presenter);
642 642 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
643 643 presenter->animator()->addAnimation(bar);
644 644 }
645 645 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
646 646 return bar;
647 647
648 648 }
649 649
650 650 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
651 651 {
652 652 Q_Q(QBarSeries);
653 653 QList<LegendMarker*> markers;
654 654 foreach(QBarSet* set, q->barSets()) {
655 655 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
656 656 markers << marker;
657 657 }
658 658
659 659 return markers;
660 660 }
661 661
662 662 #include "moc_qbarseries.cpp"
663 663 #include "moc_qbarseries_p.cpp"
664 664
665 665 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,318
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 <qbarseries.h>
23 23 #include <qbarset>
24 24
25 25 QTCOMMERCIALCHART_USE_NAMESPACE
26 26
27 27 class tst_QBarSeries : public QObject
28 28 {
29 29 Q_OBJECT
30 30
31 31 public slots:
32 32 void initTestCase();
33 33 void cleanupTestCase();
34 34 void init();
35 35 void cleanup();
36 36
37 37 private slots:
38 38 void qbarseries_data();
39 39 void qbarseries();
40 40 void type_data();
41 41 void type();
42 42 void appendBarSet_data();
43 43 void appendBarSet();
44 void removeBarSet_data();
45 void removeBarSet();
46 void appendBarSets_data();
47 void appendBarSets();
48 void removeBarSets_data();
49 void removeBarSets();
50 void barsetCount_data();
51 void barsetCount();
52 void categoryCount_data();
53 void categoryCount();
54 void barSets_data();
55 void barSets();
56 void categories_data();
57 void categories();
58 void setLabelsVisible_data();
59 void setLabelsVisible();
44 60
45 61 private:
46 62 QBarSeries* m_barseries;
63 QBarSeries* m_barseries_with_sets;
64
65 QList<QBarSet*> m_testSets;
66
67 QBarCategories m_categories;
47 68 };
48 69
49 70 void tst_QBarSeries::initTestCase()
50 71 {
51 72 }
52 73
53 74 void tst_QBarSeries::cleanupTestCase()
54 75 {
55 76 }
56 77
57 78 void tst_QBarSeries::init()
58 79 {
59 QBarCategories categories;
60 categories << "category0" << "category1" << "category2";
61 m_barseries = new QBarSeries(categories);
80 m_categories << "category0" << "category1" << "category2";
81 m_barseries = new QBarSeries(m_categories);
82 m_barseries_with_sets = new QBarSeries(m_categories);
83
84 for (int i=0; i<5; i++) {
85 m_testSets.append(new QBarSet("testset"));
86 m_barseries_with_sets->appendBarSet(m_testSets.at(i));
87 }
62 88 }
63 89
64 90 void tst_QBarSeries::cleanup()
65 91 {
92 foreach(QBarSet* s, m_testSets) {
93 m_barseries_with_sets->removeBarSet(s);
94 delete s;
95 }
96 m_testSets.clear();
97
66 98 delete m_barseries;
67 99 m_barseries = 0;
100 delete m_barseries_with_sets;
101 m_barseries_with_sets = 0;
102 m_categories.clear();
68 103 }
69 104
70 105 void tst_QBarSeries::qbarseries_data()
71 106 {
72 107 QTest::addColumn<QBarCategories> ("categories");
73 108 QBarCategories c;
74 109 c << "category0" << "category1" << "category2";
75 110 QTest::newRow("categories") << c;
76 111 }
77 112
78 113 void tst_QBarSeries::qbarseries()
79 114 {
80 115 QFETCH(QBarCategories, categories);
81 116 QBarSeries *barseries = new QBarSeries(categories);
82 117 QVERIFY(barseries != 0);
83 118 QBarCategories verifyCategories = barseries->categories();
84 119
85 120 QVERIFY(verifyCategories.count() == categories.count());
86 121 for (int i=0; i<categories.count(); i++) {
87 122 QVERIFY(verifyCategories.at(i).compare(categories.at(i)) == 0);
88 123 }
89 124 }
90 125
91 126 void tst_QBarSeries::type_data()
92 127 {
93 128
94 129 }
95 130
96 131 void tst_QBarSeries::type()
97 132 {
98 133 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
99 134 }
100 135
101 136 void tst_QBarSeries::appendBarSet_data()
102 137 {
103 138 }
104 139
105 140 void tst_QBarSeries::appendBarSet()
106 141 {
107 142 QVERIFY(m_barseries->barsetCount() == 0);
108 143
109 144 QBarSet *barset = new QBarSet("testset");
110 145 m_barseries->appendBarSet(barset);
111 146
112 147 QVERIFY(m_barseries->barsetCount() == 1);
148
149 QBarSet *barset2 = new QBarSet("testset2");
150 m_barseries->appendBarSet(barset2);
151
152 QVERIFY(m_barseries->barsetCount() == 2);
153 }
154
155 void tst_QBarSeries::removeBarSet_data()
156 {
157 }
158
159 void tst_QBarSeries::removeBarSet()
160 {
161 int count = m_testSets.count();
162 QVERIFY(m_barseries_with_sets->barsetCount() == count);
163
164 // remove some sets
165 m_barseries_with_sets->removeBarSet(m_testSets.at(2));
166 m_barseries_with_sets->removeBarSet(m_testSets.at(3));
167 m_barseries_with_sets->removeBarSet(m_testSets.at(4));
168
169 QVERIFY(m_barseries_with_sets->barsetCount() == 2);
170
171 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
172
173 QVERIFY(verifysets.at(0) == m_testSets.at(0));
174 QVERIFY(verifysets.at(1) == m_testSets.at(1));
175
176 // Try removing all sets again
177 for (int i=0; i<count; i++) {
178 m_barseries_with_sets->removeBarSet(m_testSets.at(i));
179 }
180
181 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
182 }
183
184 void tst_QBarSeries::appendBarSets_data()
185 {
186
187 }
188
189 void tst_QBarSeries::appendBarSets()
190 {
191 int count = 5;
192 QVERIFY(m_barseries->barsetCount() == 0);
193
194 QList<QBarSet*> sets;
195 for (int i=0; i<count; i++) {
196 sets.append(new QBarSet("testset"));
197 }
198
199 m_barseries->appendBarSets(sets);
200 QVERIFY(m_barseries->barsetCount() == count);
201 }
202
203 void tst_QBarSeries::removeBarSets_data()
204 {
205
206 }
207
208 void tst_QBarSeries::removeBarSets()
209 {
210 int count = m_testSets.count();
211 QVERIFY(m_barseries_with_sets->barsetCount() == count);
212
213 // Try removing empty list of sets
214 QList<QBarSet*> empty;
215 m_barseries_with_sets->removeBarSets(empty);
216 QVERIFY(m_barseries_with_sets->barsetCount() == count);
217
218 // remove all sets
219 m_barseries_with_sets->removeBarSets(m_testSets);
220 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
221
222 // Try removing empty list again
223 m_barseries_with_sets->removeBarSets(empty);
224 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
225
226 // remove all sets again
227 m_barseries_with_sets->removeBarSets(m_testSets);
228 QVERIFY(m_barseries_with_sets->barsetCount() == 0);
229 }
230
231 void tst_QBarSeries::barsetCount_data()
232 {
233
234 }
235
236 void tst_QBarSeries::barsetCount()
237 {
238 QVERIFY(m_barseries->barsetCount() == 0);
239 QVERIFY(m_barseries_with_sets->barsetCount() == m_testSets.count());
240 }
241
242 void tst_QBarSeries::categoryCount_data()
243 {
244
245 }
246
247 void tst_QBarSeries::categoryCount()
248 {
249 QVERIFY(m_barseries->categoryCount() == m_categories.count());
250 QVERIFY(m_barseries_with_sets->categoryCount() == m_categories.count());
251 }
252
253 void tst_QBarSeries::barSets_data()
254 {
255
256 }
257
258 void tst_QBarSeries::barSets()
259 {
260 QVERIFY(m_barseries->barSets().count() == 0);
261
262 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
263 QVERIFY(sets.count() == m_testSets.count());
264
265 for (int i=0; i<m_testSets.count(); i++) {
266 QVERIFY(sets.at(i) == m_testSets.at(i));
267 }
268 }
269
270 void tst_QBarSeries::categories_data()
271 {
272
273 }
274
275 void tst_QBarSeries::categories()
276 {
277 QBarCategories categories = m_barseries->categories();
278
279 QVERIFY(categories.count() == m_categories.count());
280 for (int i=0; i<m_categories.count(); i++) {
281 QVERIFY(categories.at(i).compare(m_categories.at(i)) == 0);
282 }
283 }
284
285 void tst_QBarSeries::setLabelsVisible_data()
286 {
287
288 }
289
290 void tst_QBarSeries::setLabelsVisible()
291 {
292 foreach (QBarSet* s, m_testSets) {
293 QVERIFY(s->labelsVisible() == false);
294 }
295
296 m_barseries_with_sets->setLabelsVisible(true);
297 foreach (QBarSet* s, m_testSets) {
298 QVERIFY(s->labelsVisible() == true);
299 }
300
301 m_barseries_with_sets->setLabelsVisible(false);
302 foreach (QBarSet* s, m_testSets) {
303 QVERIFY(s->labelsVisible() == false);
304 }
113 305 }
114 306
115 307 /*
116 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
117 void appendBarSets(QList<QBarSet* > sets);
118 void removeBarSets(QList<QBarSet* > sets);
119 int barsetCount() const;
120 int categoryCount() const;
121 QList<QBarSet*> barSets() const;
122 QBarCategories categories() const;
123 308
124 309 void setLabelsVisible(bool visible = true);
125 310
126 311 bool setModel(QAbstractItemModel *model);
127 312 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
128 313 void setModelMappingRange(int first, int count = -1);
129 314 */
130 315 QTEST_MAIN(tst_QBarSeries)
131 316
132 317 #include "tst_qbarseries.moc"
133 318
General Comments 0
You need to be logged in to leave comments. Login now