##// END OF EJS Templates
PieSeries: model now supports custom mapping.
Marek Rosa -
r1056:65526593187b
parent child
Show More
@@ -45,7 +45,7 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qr
45 45 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
46 46 QVector<QRectF> result;
47 47
48 Q_ASSERT(startVector.count() == endVector.count()) ;
48 Q_ASSERT(startVector.count() == endVector.count());
49 49
50 50 for(int i = 0; i < startVector.count(); i++) {
51 51 qreal w = endVector[i].width();
@@ -200,6 +200,12 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoun
200 200 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
201 201 }
202 202
203 void QBarSeries::setModelMappingRange(int first, int count)
204 {
205 Q_D(QBarSeries);
206 d->setModelMappingRange(first, count);
207 }
208
203 209 /*!
204 210 Returns the bar categories of the series.
205 211 */
@@ -381,52 +387,226 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int t
381 387 m_mapOrientation = orientation;
382 388
383 389 // connect the signals
384 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
385 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
390 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
391 if (m_mapOrientation == Qt::Vertical) {
392 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
393 connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
394 } else {
395 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
396 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
397 }
386 398
387 399 // create the initial bars
388 400 m_categories.clear();
389 401 if (m_mapOrientation == Qt::Vertical) {
390 for (int k = 0; k < m_model->rowCount(); k++) {
402 int rowCount = 0;
403 if(m_mapCount == -1)
404 rowCount = m_model->rowCount() - m_mapFirst;
405 else
406 rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
407 for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
391 408 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
392 409 }
393 410
394 411 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
395 412 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
396 for(int m = 0; m < m_model->rowCount(); m++)
413 for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
397 414 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
398 415 q->appendBarSet(barSet);
399 416 }
400 417 } else {
401 for (int k = 0; k < m_model->columnCount(); k++) {
418 int columnCount = 0;
419 if(m_mapCount == -1)
420 columnCount = m_model->columnCount() - m_mapFirst;
421 else
422 columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
423 for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
402 424 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
403 425 }
404 426
405 427 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
406 428 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
407 for(int m = 0; m < m_model->columnCount(); m++)
429 for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
408 430 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
409 431 q->appendBarSet(barSet);
410 432 }
411 433 }
412 434 }
413 435
414 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
436 void QBarSeriesPrivate::setModelMappingRange(int first, int count)
415 437 {
416 Q_UNUSED(bottomRight)
438 m_mapFirst = first;
439 m_mapCount = count;
440 }
417 441
418 if (m_mapOrientation == Qt::Vertical)
419 {
420 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
421 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
422 barsetAt(topLeft.column() - m_mapBarBottom)->replace(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
442 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
443 {
444 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
445 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
446 if (m_mapOrientation == Qt::Vertical)
447 {
448 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
449 if ( row >= m_mapFirst && (m_mapCount == - 1 || row < m_mapFirst + m_mapCount)) {
450 if (column >= m_mapBarBottom && column <= m_mapBarTop)
451 barsetAt(column - m_mapBarBottom)->replace(row - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
452 // if (column == m_mapCategories);// TODO:
453 }
454 }
455 else
456 {
457 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
458 if (column >= m_mapFirst && (m_mapCount == - 1 || column < m_mapFirst + m_mapCount)) {
459 if (row >= m_mapBarBottom && row <= m_mapBarTop)
460 barsetAt(row - m_mapBarBottom)->replace(column - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
461 // if (row == m_mapCategories);// TODO:
462 }
463 }
464 }
423 465 }
424 else
425 {
426 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
427 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
428 barsetAt(topLeft.row() - m_mapBarBottom)->replace(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
466 }
467
468 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
469 {
470 Q_UNUSED(parent);
471 Q_UNUSED(start);
472 Q_UNUSED(end);
473 initializeDataFromModel();
474 // // series uses model as a data sourceupda
475 // int addedCount = end - start + 1;
476 // if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
477 // return;
478 // } else {
479
480 // for (int bar = m_mapBarBottom; bar <= m_mapBarTop; bar++) {
481 // QBarSet *barSet = barsetAt(bar - m_mapBarBottom);
482 // // adding items to unlimited map
483 // if (m_mapCount == -1 && start >= m_mapFirst) {
484 // for (int i = start; i <= end; i++) {
485 // if (bar == m_mapBarBottom)
486 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
487 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
488 // }
489 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
490 // // not all newly added items
491 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
492 // if (bar == m_mapBarBottom)
493 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
494 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
495 // }
496 // }
497
498 // // adding items to limited map
499 // else if (start >= m_mapFirst) {
500 // // remove the items that will no longer fit into the map
501 // // int toRemove = addedCount - (count - points().size());
502 // for (int i = start; i <= end; i++) {
503 // if (bar == m_mapBarBottom)
504 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
505 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
506 // }
507 // if (m_barSets.size() > m_mapCount)
508 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
509 // if (bar == m_mapBarBottom)
510 // removeCategory(i);
511 // barSet->remove(i);
512 // }
513 // } else {
514 // //
515 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
516 // if (bar == m_mapBarBottom)
517 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
518 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
519 // }
520 // if (m_barSets.size() > m_mapCount)
521 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
522 // if (bar == m_mapBarBottom)
523 // removeCategory(i);
524 // barSet->remove(i);
525 // }
526 // }
527 // }
528 // emit restructuredBars();
529 // emit barsetChanged();
530 // emit categoriesUpdated();
531 // }
532 }
533
534 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
535 {
536 Q_UNUSED(parent);
537 Q_UNUSED(start);
538 Q_UNUSED(end);
539 initializeDataFromModel();
540 }
541
542 void QBarSeriesPrivate::initializeDataFromModel()
543 {
544 Q_Q(QBarSeries);
545
546 if (m_model == 0)
547 return;
548
549 // connect the signals
550 // connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
551 // if (m_mapOrientation == Qt::Vertical) {
552 // connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
553 // connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
554 // } else {
555 // connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
556 // connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
557 // }
558
559 // create the initial bars
560 m_categories.clear();
561 m_barSets.clear();
562 emit restructuredBars();
563 if (m_mapOrientation == Qt::Vertical) {
564 int rowCount = 0;
565 if(m_mapCount == -1)
566 rowCount = m_model->rowCount() - m_mapFirst;
567 else
568 rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
569 for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
570 m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
571 }
572
573 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
574 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
575 for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
576 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
577 q->appendBarSet(barSet);
578 }
579 } else {
580 int columnCount = 0;
581 if(m_mapCount == -1)
582 columnCount = m_model->columnCount() - m_mapFirst;
583 else
584 columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
585 for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
586 m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
587 }
588
589 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
590 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
591 for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
592 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
593 q->appendBarSet(barSet);
594 }
429 595 }
596 emit restructuredBars();
597 emit updatedBars();
598 }
599
600 void QBarSeriesPrivate::insertCategory(int index, const QString category)
601 {
602 m_categories.insert(index, category);
603 emit categoriesUpdated();
604 }
605
606 void QBarSeriesPrivate::removeCategory(int index)
607 {
608 m_categories.removeAt(index);
609 emit categoriesUpdated();
430 610 }
431 611
432 612 void QBarSeriesPrivate::barsetChanged()
@@ -57,6 +57,7 public:
57 57
58 58 bool setModel(QAbstractItemModel *model);
59 59 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
60 void setModelMappingRange(int first, int count = -1);
60 61
61 62 protected:
62 63 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
@@ -23,6 +23,10 public:
23 23
24 24 bool setModel(QAbstractItemModel *model);
25 25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
26 void setModelMappingRange(int first, int count = -1);
27
28 void insertCategory(int index, const QString category);
29 void removeCategory(int index);
26 30
27 31 QBarSet* barsetAt(int index);
28 32 QString categoryName(int category);
@@ -38,10 +42,14 Q_SIGNALS:
38 42 void clicked(QBarSet *barset, QString category);
39 43 void updatedBars();
40 44 void restructuredBars();
45 void categoriesUpdated();
41 46
42 47 private Q_SLOTS:
43 48 // slots for updating bars when data in model changes
44 49 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 void modelDataAdded(QModelIndex parent, int start, int end);
51 void modelDataRemoved(QModelIndex parent, int start, int end);
52 void initializeDataFromModel();
45 53 void barsetChanged();
46 54
47 55 protected:
@@ -110,7 +110,7 QBarSet& QBarSet::operator << (const qreal &value)
110 110 void QBarSet::insert(const int index, const qreal value)
111 111 {
112 112 d_ptr->m_values.insert(index, value);
113 emit d_ptr->updatedBars();
113 // emit d_ptr->updatedBars();
114 114 }
115 115
116 116 /*!
@@ -120,7 +120,7 void QBarSet::insert(const int index, const qreal value)
120 120 void QBarSet::remove(const int index)
121 121 {
122 122 d_ptr->m_values.removeAt(index);
123 emit d_ptr->updatedBars();
123 // emit d_ptr->updatedBars();
124 124 }
125 125
126 126 /*!
@@ -449,18 +449,26 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::O
449 449 d->m_mapLabels = modelLabelsLine;
450 450 d->m_mapOrientation = orientation;
451 451
452 // connect the signals
453 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
452 d->initializePieFromModel();
453 // // connect the signals
454 // connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
454 455
455 456
456 // create the initial slices set
457 if (d->m_mapOrientation == Qt::Vertical) {
458 for (int i = 0; i < d->m_model->rowCount(); i++)
459 append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
460 } else {
461 for (int i = 0; i < d->m_model->columnCount(); i++)
462 append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
463 }
457 // // create the initial slices set
458 // if (d->m_mapOrientation == Qt::Vertical) {
459 // for (int i = 0; i < d->m_model->rowCount(); i++)
460 // append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
461 // } else {
462 // for (int i = 0; i < d->m_model->columnCount(); i++)
463 // append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
464 // }
465 }
466
467 void QPieSeries::setModelMappingRange(int first, int count)
468 {
469 Q_D(QPieSeries);
470 d->m_mapFirst = first;
471 d->m_mapCount = count;
464 472 }
465 473
466 474 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -553,37 +561,243 void QPieSeriesPrivate::sliceHovered(bool state)
553 561
554 562 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
555 563 {
556 Q_UNUSED(bottomRight)
557
558 if (m_mapOrientation == Qt::Vertical)
559 {
560 if (topLeft.column() == m_mapValues)
561 if (m_mapValues == m_mapLabels)
564 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
565 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
566 if (m_mapOrientation == Qt::Vertical)
562 567 {
563 m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
564 m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
568 if ( topLeft.row() >= m_mapFirst && (m_mapCount == - 1 || topLeft.row() < m_mapFirst + m_mapCount)) {
569 if (topLeft.column() == m_mapValues)
570 m_slices.at(topLeft.row() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
571 if (topLeft.column() == m_mapLabels)
572 m_slices.at(topLeft.row() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
573 }
565 574 }
566 575 else
567 576 {
568 m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
577 if (topLeft.column() >= m_mapFirst && (m_mapCount == - 1 || topLeft.column() < m_mapFirst + m_mapCount)) {
578 if (topLeft.row() == m_mapValues)
579 m_slices.at(topLeft.column() - m_mapFirst)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
580 if (topLeft.row() == m_mapLabels)
581 m_slices.at(topLeft.column() - m_mapFirst)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
582 }
569 583 }
570 else if (topLeft.column() == m_mapLabels)
571 m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
584 }
572 585 }
573 else
574 {
575 if (topLeft.row() == m_mapValues)
576 if (m_mapValues == m_mapLabels)
577 {
578 m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
579 m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
586 }
587
588
589 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
590 {
591 Q_UNUSED(parent);
592 Q_Q(QPieSeries);
593 // series uses model as a data sourceupda
594 int addedCount = end - start + 1;
595 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
596 return;
597 } else {
598 int first = qMax(start, m_mapFirst);
599 int last = qMin(first + addedCount - 1, end);
600 for (int i = first; i <= last; i++) {
601 QPieSlice *slice = new QPieSlice;
602 if (m_mapOrientation == Qt::Vertical) {
603 slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
604 slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
605 } else {
606 slice->setValue(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble());
607 slice->setLabel(m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
580 608 }
581 else
582 {
583 m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
609 slice->setLabelVisible();
610 q->insert(i - m_mapFirst, slice);
611 }
612 if (m_mapCount != -1 && m_slices.size() > m_mapCount)
613 for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
614 q->remove(q->slices().at(i));
615 }
616
617 // // adding items to unlimited map
618 // else if (m_mapCount == -1 && start >= m_mapFirst) {
619 // for (int i = start; i <= end; i++) {
620 // QPieSlice *slice = new QPieSlice;
621 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
622 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
623 // slice->setLabelVisible(true);
624 // q->insert(i - m_mapFirst, slice);
625 // // handlePointAdded(i - first);
626 // }
627 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
628 // // not all newly added items
629 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
630 // QPieSlice *slice = new QPieSlice;
631 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
632 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
633 // slice->setLabelVisible(true);
634 // q->insert(i - m_mapFirst, slice);
635 // // handlePointAdded(i - first);
636 // }
637 // }
638
639 // // adding items to limited map
640 // else if (start >= m_mapFirst) {
641 // // remove the items that will no longer fit into the map
642 // // int toRemove = addedCount - (count - points().size());
643 // for (int i = start; i <= end; i++) {
644 // QPieSlice *slice = new QPieSlice;
645 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
646 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
647 // slice->setLabelVisible(true);
648 // q->insert(i - m_mapFirst, slice);
649 // }
650 // if (m_slices.size() > m_mapCount)
651 // for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
652 // q->remove(q->slices().at(i));
653 // // handlePointRemoved(i);
654 // // update();
655 // } else {
656 // //
657 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
658 // QPieSlice *slice = new QPieSlice;
659 // slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
660 // slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
661 // slice->setLabelVisible(true);
662 // q->insert(i - m_mapFirst, slice);
663 // // handlePointAdded(i - first);
664 // }
665 // if (m_slices.size() > m_mapCount)
666 // for (int i = m_slices.size() - 1; i >= m_mapCount; i--)
667 // q->remove(q->slices().at(i));
668 // // handlePointRemoved(i);
669 // }
670 }
671
672 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
673 {
674 Q_UNUSED(parent);
675 Q_Q(QPieSeries);
676 int removedCount = end - start + 1;
677 if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
678 return;
679 }
680
681 // else if (m_mapCount == -1) {
682 // int first = qMax(start, m_mapFirst);
683 // int last = qMax(end, m_mapFirst + removedCount - 1);
684 // for (int i = last; i >= first; i--)
685 // q->remove(q->slices().at(i - m_mapFirst));
686 // }
687 // else {
688 // int toRemove = qMin(m_slices.size() - 1, removedCount);
689 // int first = qMax(start, m_mapFirst);
690 // int last = qMax(end, m_mapFirst + toRemove - 1);
691
692 // }
693
694 // removing items from unlimited map
695 else if (m_mapCount == -1 && start >= m_mapFirst) {
696 for (int i = end; i >= start; i--)
697 q->remove(q->slices().at(i - m_mapFirst));
698 // handlePointRemoved(i - m_mapFirst);
699 } else if (m_mapCount == - 1 && start < m_mapFirst) {
700 // not all removed items
701 for (int i = m_mapFirst + removedCount - 1; i >= m_mapFirst; i--)
702 q->remove(q->slices().at(i - m_mapFirst));
703 // handlePointRemoved(i - m_mapFirst);
704 }
705
706 // removing items from limited map
707 else if (start >= m_mapFirst) {
708 //
709 // int sizeAfter = m_slices.size();
710 int lastExisting = qMin(m_mapFirst + m_slices.size() - 1, end);
711 for (int i = lastExisting; i >= start; i--) {
712 q->remove(q->slices().at(i - m_mapFirst));
713 // sizeAfter--;
714 // handlePointRemoved(i - m_mapFirst);
715 }
716
717 // the map is limited, so after removing the items some new items may have fall within the mapped area
718 int itemsAvailable;
719 if (m_mapOrientation == Qt::Vertical)
720 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
721 else
722 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
723 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size());
724 int currentSize = m_slices.size();
725 if (itemsAvailable > 0)
726 for (int i = m_slices.size() + m_mapFirst; i < currentSize + toBeAdded + m_mapFirst; i++) {
727 // handlePointAdded(i);
728 QPieSlice *slice = new QPieSlice;
729 slice->setValue(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble());
730 slice->setLabel(m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
731 slice->setLabelVisible(true);
732 q->insert(i - m_mapFirst, slice);
733 }
734
735 // for (int i = lastExisting; i >= start; i--) {
736 // q->remove(q->slices().at(i - m_mapFirst));
737 //// sizeAfter--;
738 //// handlePointRemoved(i - m_mapFirst);
739 // }
740 } else {
741 // first remove item lays before the mapped area
742 int toRemove = qMin(m_slices.size(), removedCount);
743 for (int i = m_mapFirst; i < m_mapFirst + toRemove; i++)
744 q->remove(q->slices().at(0));
745 // handlePointRemoved(0);
746
747 // the map is limited, so after removing the items some new items may have fall into the map
748 int itemsAvailable;
749 if (m_mapOrientation == Qt::Vertical)
750 itemsAvailable = m_model->rowCount() - m_mapFirst - m_slices.size();
751 else
752 itemsAvailable = m_model->columnCount() - m_mapFirst - m_slices.size();
753 int toBeAdded = qMin(itemsAvailable, m_mapCount - m_slices.size());
754 int currentSize = m_slices.size();
755 if (itemsAvailable > 0)
756 for (int i = m_slices.size(); i < currentSize + toBeAdded; i++) {
757 QPieSlice *slice = new QPieSlice;
758 slice->setValue(m_model->data(m_model->index(i + m_mapFirst, m_mapValues), Qt::DisplayRole).toDouble());
759 slice->setLabel(m_model->data(m_model->index(i + m_mapFirst, m_mapLabels), Qt::DisplayRole).toString());
760 slice->setLabelVisible(true);
761 q->insert(i, slice);
762 // handlePointAdded(i);
584 763 }
585 else if (topLeft.row() == m_mapLabels)
586 m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
764 }
765
766 }
767
768 void QPieSeriesPrivate::initializePieFromModel()
769 {
770 Q_Q(QPieSeries);
771 // clear current content
772 q->clear();
773
774 // connect the signals
775 connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
776 if (m_mapOrientation == Qt::Vertical) {
777 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
778 connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
779 } else {
780 connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
781 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
782 }
783
784 // create the initial slices set
785 if (m_mapOrientation == Qt::Vertical) {
786 int sliceCount = 0;
787 if(m_mapCount == -1)
788 sliceCount = m_model->rowCount() - m_mapFirst;
789 else
790 sliceCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
791 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
792 q->append(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString());
793 } else {
794 int sliceCount = 0;
795 if(m_mapCount == -1)
796 sliceCount = m_model->columnCount() - m_mapFirst;
797 else
798 sliceCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
799 for (int i = m_mapFirst; i < m_mapFirst + sliceCount; i++)
800 q->append(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString());
587 801 }
588 802 }
589 803
@@ -73,6 +73,7 public:
73 73
74 74 bool setModel(QAbstractItemModel* model);
75 75 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
76 void setModelMappingRange(int first, int count = -1);
76 77
77 78 Q_SIGNALS:
78 79 void clicked(QPieSlice* slice);
@@ -56,6 +56,9 public Q_SLOTS:
56 56 void sliceClicked();
57 57 void sliceHovered(bool state);
58 58 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
59 void modelDataAdded(QModelIndex parent, int start, int end);
60 void modelDataRemoved(QModelIndex parent, int start, int end);
61 void initializePieFromModel();
59 62 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
60 63
61 64 public:
@@ -70,6 +73,7 public:
70 73 // model map
71 74 int m_mapValues;
72 75 int m_mapLabels;
76 bool m_modelReady;
73 77
74 78 private:
75 79 friend class QLegendPrivate;
@@ -220,7 +220,7 void XYChartItem::handlePointsRemoved(int start, int end)
220 220 handlePointRemoved(i - first);
221 221 }
222 222
223 // the map is limited, so after removing the items some new items may have fall into the map
223 // the map is limited, so after removing the items some new items may have fall within the mapped area
224 224 int itemsAvailable;
225 225 if (m_series->mapOrientation() == Qt::Vertical)
226 226 itemsAvailable = m_series->model()->rowCount() - first - m_points.size();
@@ -232,7 +232,7 void XYChartItem::handlePointsRemoved(int start, int end)
232 232 for (int i = m_points.size(); i < currentSize + toBeAdded; i++)
233 233 handlePointAdded(i);
234 234 } else {
235 // TODO:
235 // first removed item lays before the mapped area
236 236 int toRemove = qMin(m_points.size() - 1, removedCount);
237 237 for (int i = first; i < first + toRemove; i++)
238 238 handlePointRemoved(0);
@@ -43,7 +43,7 CustomTableModel::CustomTableModel(QObject *parent) :
43 43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 44
45 45 // m_data
46 for (int i = 0; i < 6; i++)
46 for (int i = 0; i < 8; i++)
47 47 {
48 48 QVector<qreal>* dataVec = new QVector<qreal>(6);
49 49 // QVector<QColor>* colorVec = new QVector<QColor>(6);
@@ -21,6 +21,7
21 21 #include "tablewidget.h"
22 22 #include <QGridLayout>
23 23 #include <QTableView>
24 #include <QChart>
24 25 #include <QStyledItemDelegate>
25 26 #include <QLineSeries>
26 27 #include <QSplineSeries>
@@ -38,9 +39,9
38 39 #include <QTime>
39 40
40 41 TableWidget::TableWidget(QWidget *parent)
41 : QWidget(parent)
42 : QWidget(parent),specialPie(0)
42 43 {
43 setGeometry(100, 100, 1000, 600);
44 setGeometry(1900, 100, 1000, 600);
44 45 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
45 46 // create simple model for storing data
46 47 // user's table data model
@@ -52,7 +53,7 TableWidget::TableWidget(QWidget *parent)
52 53 // tableView->setItemDelegate(new QStyledItemDelegate);
53 54 m_chart = new QChart;
54 55 m_chart->legend()->setVisible(true);
55 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
56 m_chart->setAnimationOptions(QChart::SeriesAnimations);
56 57 m_chartView = new QChartView(m_chart);
57 58 m_chartView->setRenderHint(QPainter::Antialiasing);
58 59 m_chartView->setMinimumSize(640, 480);
@@ -67,6 +68,10 TableWidget::TableWidget(QWidget *parent)
67 68 QPushButton* removeRowButton = new QPushButton("Remove row");
68 69 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
69 70
71 QPushButton* specialPieButton = new QPushButton("Test pie");
72 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
73
74
70 75 QLabel *spinBoxLabel = new QLabel("Rows affected:");
71 76
72 77 // spin box for setting number of affected items (add, remove)
@@ -81,6 +86,7 TableWidget::TableWidget(QWidget *parent)
81 86 buttonsLayout->addWidget(addRowAboveButton);
82 87 buttonsLayout->addWidget(addRowBelowButton);
83 88 buttonsLayout->addWidget(removeRowButton);
89 buttonsLayout->addWidget(specialPieButton);
84 90 buttonsLayout->addStretch();
85 91
86 92 // chart type radio buttons
@@ -141,7 +147,8 void TableWidget::updateChartType(bool toggle)
141 147 // this if is needed, so that the function is only called once.
142 148 // For the radioButton that was enabled.
143 149 if (toggle) {
144 m_chart->removeAllSeries();
150 specialPie = 0;
151 m_chart->removeAllSeries();
145 152 m_chart->axisX()->setNiceNumbersEnabled(false);
146 153 m_chart->axisY()->setNiceNumbersEnabled(false);
147 154
@@ -198,7 +205,7 void TableWidget::updateChartType(bool toggle)
198 205 m_series = new QSplineSeries;
199 206 m_series->setModel(m_model);
200 207 m_series->setModelMapping(0,1, Qt::Vertical);
201 // m_series->setModelMappingRange(1, 4);
208 // m_series->setModelMappingRange(1, 4);
202 209 // series->setModelMapping(0,1, Qt::Horizontal);
203 210 m_chart->addSeries(m_series);
204 211 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
@@ -208,7 +215,7 void TableWidget::updateChartType(bool toggle)
208 215 m_series = new QSplineSeries;
209 216 m_series->setModel(m_model);
210 217 m_series->setModelMapping(2,3, Qt::Vertical);
211 // m_series->setModelMappingRange(0, 0);
218 // m_series->setModelMappingRange(0, 0);
212 219 // series->setModelMapping(2,3, Qt::Horizontal);
213 220 m_chart->addSeries(m_series);
214 221 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
@@ -218,7 +225,7 void TableWidget::updateChartType(bool toggle)
218 225 m_series = new QSplineSeries;
219 226 m_series->setModel(m_model);
220 227 m_series->setModelMapping(4,5, Qt::Vertical);
221 // m_series->setModelMappingRange(2, 0);
228 // m_series->setModelMappingRange(2, 0);
222 229 // series->setModelMapping(4,5, Qt::Horizontal);
223 230 m_chart->addSeries(m_series);
224 231 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
@@ -230,7 +237,7 void TableWidget::updateChartType(bool toggle)
230 237 m_series = new QScatterSeries;
231 238 m_series->setModel(m_model);
232 239 m_series->setModelMapping(0,1, Qt::Vertical);
233 // m_series->setModelMappingRange(2, 0);
240 // m_series->setModelMappingRange(2, 0);
234 241 // series->setModelMapping(0,1, Qt::Horizontal);
235 242 m_chart->addSeries(m_series);
236 243
@@ -241,7 +248,7 void TableWidget::updateChartType(bool toggle)
241 248 m_series = new QScatterSeries;
242 249 m_series->setModel(m_model);
243 250 m_series->setModelMapping(2,3, Qt::Vertical);
244 // m_series->setModelMappingRange(1, 6);
251 // m_series->setModelMappingRange(1, 6);
245 252 // series->setModelMapping(2,3, Qt::Horizontal);
246 253 m_chart->addSeries(m_series);
247 254
@@ -262,46 +269,62 void TableWidget::updateChartType(bool toggle)
262 269 // pie 1
263 270 QPieSeries* pieSeries = new QPieSeries;
264 271 pieSeries->setModel(m_model);
272 pieSeries->setModelMappingRange(3, 3);
265 273 pieSeries->setModelMapping(0,0, Qt::Vertical);
266 274 pieSeries->setLabelsVisible(true);
267 pieSeries->setPieSize(0.4);
275 pieSeries->setPieSize(0.35);
268 276 pieSeries->setHorizontalPosition(0.2);
269 pieSeries->setVerticalPosition(0.35);
277 pieSeries->setVerticalPosition(0.3);
270 278
271 279 m_chart->addSeries(pieSeries);
272 280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
273 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
281 m_model->addMapping(seriesColorHex, QRect(0, 3, 1, 3));
274 282
275 283 // pie 2
276 284 pieSeries = new QPieSeries;
277 285 pieSeries->setModel(m_model);
286 pieSeries->setModelMappingRange(2, -1);
278 287 pieSeries->setModelMapping(1,1, Qt::Vertical);
279 288 pieSeries->setLabelsVisible(true);
280 pieSeries->setPieSize(0.4);
289 pieSeries->setPieSize(0.35);
281 290 pieSeries->setHorizontalPosition(0.8);
282 pieSeries->setVerticalPosition(0.35);
291 pieSeries->setVerticalPosition(0.3);
283 292 m_chart->addSeries(pieSeries);
284 293 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
285 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
294 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000));
286 295
287 296 // pie 3
288 297 pieSeries = new QPieSeries;
289 298 pieSeries->setModel(m_model);
290 299 pieSeries->setModelMapping(2,2, Qt::Vertical);
291 300 pieSeries->setLabelsVisible(true);
292 pieSeries->setPieSize(0.4);
293 pieSeries->setHorizontalPosition(0.5);
294 pieSeries->setVerticalPosition(0.65);
301 pieSeries->setPieSize(0.35);
302 pieSeries->setHorizontalPosition(0.2);
303 pieSeries->setVerticalPosition(0.75);
295 304 m_chart->addSeries(pieSeries);
296 305 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
297 306 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
307
308 // special pie
309 specialPie = new QPieSeries;
310 specialPie->append(17, "1");
311 specialPie->append(45, "2");
312 specialPie->append(77, "3");
313 specialPie->append(37, "4");
314 specialPie->append(27, "5");
315 specialPie->append(47, "6");
316 specialPie->setPieSize(0.35);
317 specialPie->setHorizontalPosition(0.8);
318 specialPie->setVerticalPosition(0.75);
319 specialPie->setLabelsVisible(true);
320 m_chart->addSeries(specialPie);
298 321 }
299 322 else if (m_areaRadioButton->isChecked())
300 323 {
301 324 QLineSeries* upperLineSeries = new QLineSeries;
302 325 upperLineSeries->setModel(m_model);
303 326 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
304 // upperLineSeries->setModelMappingRange(1, 5);
327 // upperLineSeries->setModelMappingRange(1, 5);
305 328 QLineSeries* lowerLineSeries = new QLineSeries;
306 329 lowerLineSeries->setModel(m_model);
307 330 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
@@ -315,6 +338,7 void TableWidget::updateChartType(bool toggle)
315 338 {
316 339 QBarSeries* barSeries = new QBarSeries(QStringList());
317 340 barSeries->setModel(m_model);
341 // barSeries->setModelMappingRange(2, 5);
318 342 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
319 343 m_chart->addSeries(barSeries);
320 344 QList<QBarSet*> barsets = barSeries->barSets();
@@ -327,7 +351,7 void TableWidget::updateChartType(bool toggle)
327 351
328 352 if (!m_barRadioButton->isChecked())
329 353 m_chart->axisX()->setRange(0, 500);
330 m_chart->axisY()->setRange(0, 120);
354 // m_chart->axisY()->setRange(0, 120);
331 355 m_chart->legend()->setVisible(true);
332 356
333 357 // repaint table view colors
@@ -336,6 +360,15 void TableWidget::updateChartType(bool toggle)
336 360 }
337 361 }
338 362
363 void TableWidget::testPie()
364 {
365 if (specialPie) {
366 specialPie->remove(specialPie->slices().at(2));
367 // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
368 specialPie->append(4, "heloo");
369 }
370 }
371
339 372 TableWidget::~TableWidget()
340 373 {
341 374
@@ -22,16 +22,18
22 22 #define TABLEWIDGET_H
23 23
24 24 #include <QtGui/QWidget>
25 //#include <QChartGlobal>
25 26 #include "qchartview.h"
26 #include "qxyseries.h"
27
28 QTCOMMERCIALCHART_USE_NAMESPACE
27 //#include "qxyseries.h"
28 //#include <QPieSeries>
29 29
30 30 class CustomTableModel;
31 31 class QTableView;
32 32 class QRadioButton;
33 33 class QSpinBox;
34 34
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
35 37 class TableWidget : public QWidget
36 38 {
37 39 Q_OBJECT
@@ -46,6 +48,7 public:
46 48 void addRowBelow();
47 49 void removeRow();
48 50 void updateChartType(bool toggle);
51 void testPie();
49 52
50 53 private:
51 54 QChartView* m_chartView;
@@ -60,6 +63,7 public:
60 63 QRadioButton* m_areaRadioButton;
61 64 QRadioButton* m_barRadioButton;
62 65 QSpinBox* m_linesCountSpinBox;
66 QPieSeries* specialPie;
63 67 };
64 68
65 69 #endif // TABLEWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now