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