##// END OF EJS Templates
fix: series visible now affects legend items also
sauimone -
r1459:e9693669a76b
parent child
Show More
@@ -22,10 +22,6
22 22
23 23 #include <QApplication>
24 24 #include <QMainWindow>
25 #include <QChartView>
26 #include <QBarSeries>
27 #include <QBarSet>
28 #include <QLegend>
29 25
30 26 QTCOMMERCIALCHART_USE_NAMESPACE
31 27
@@ -30,6 +30,7
30 30 #include <QGraphicsGridLayout>
31 31 #include <QDoubleSpinBox>
32 32 #include <QGroupBox>
33 #include <QBarSeries>
33 34
34 35 QTCOMMERCIALCHART_USE_NAMESPACE
35 36
@@ -459,13 +459,15 void QLegendPrivate::updateLayout()
459 459 QPointF point = m_rect.topLeft();
460 460 m_width = 0;
461 461 foreach (QGraphicsItem *item, items) {
462 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
463 const QRectF& rect = item->boundingRect();
464 qreal w = rect.width();
465 m_minWidth=qMax(m_minWidth,w);
466 m_minHeight=qMax(m_minHeight,rect.height());
467 m_width+=w;
468 point.setX(point.x() + w);
462 if (item->isVisible()) {
463 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
464 const QRectF& rect = item->boundingRect();
465 qreal w = rect.width();
466 m_minWidth=qMax(m_minWidth,w);
467 m_minHeight=qMax(m_minHeight,rect.height());
468 m_width+=w;
469 point.setX(point.x() + w);
470 }
469 471 }
470 472 if(m_width<m_rect.width()) {
471 473 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
@@ -481,13 +483,15 void QLegendPrivate::updateLayout()
481 483 QPointF point = m_rect.topLeft();
482 484 m_height = 0;
483 485 foreach (QGraphicsItem *item, items) {
484 item->setPos(point);
485 const QRectF& rect = item->boundingRect();
486 qreal h = rect.height();
487 m_minWidth=qMax(m_minWidth,rect.width());
488 m_minHeight=qMax(m_minHeight,h);
489 m_height+=h;
490 point.setY(point.y() + h);
486 if (item->isVisible()) {
487 item->setPos(point);
488 const QRectF& rect = item->boundingRect();
489 qreal h = rect.height();
490 m_minWidth=qMax(m_minWidth,rect.width());
491 m_minHeight=qMax(m_minHeight,h);
492 m_height+=h;
493 point.setY(point.y() + h);
494 }
491 495 }
492 496 if(m_height<m_rect.height()) {
493 497 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
@@ -530,20 +534,22 void QLegendPrivate::updateDetachedLayout()
530 534 m_height = 0;
531 535 for (int i=0; i<items.count(); i++) {
532 536 QGraphicsItem *item = items.at(i);
533 const QRectF& rect = item->boundingRect();
534 qreal w = rect.width();
535 qreal h = rect.height();
536 m_minWidth = qMax(m_minWidth,w);
537 m_minHeight = qMax(m_minHeight,rect.height());
538 m_height = qMax(m_height,h);
539 item->setPos(point.x(),point.y());
540 point.setX(point.x() + w);
541 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
542 // Next item would go off rect.
543 point.setX(m_rect.topLeft().x());
544 point.setY(point.y() + h);
545 if (i+1 < items.count()) {
546 m_height += h;
537 if (item->isVisible()) {
538 const QRectF& rect = item->boundingRect();
539 qreal w = rect.width();
540 qreal h = rect.height();
541 m_minWidth = qMax(m_minWidth,w);
542 m_minHeight = qMax(m_minHeight,rect.height());
543 m_height = qMax(m_height,h);
544 item->setPos(point.x(),point.y());
545 point.setX(point.x() + w);
546 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
547 // Next item would go off rect.
548 point.setX(m_rect.topLeft().x());
549 point.setY(point.y() + h);
550 if (i+1 < items.count()) {
551 m_height += h;
552 }
547 553 }
548 554 }
549 555 }
@@ -562,20 +568,22 void QLegendPrivate::updateDetachedLayout()
562 568 m_height = 0;
563 569 for (int i=0; i<items.count(); i++) {
564 570 QGraphicsItem *item = items.at(i);
565 const QRectF& rect = item->boundingRect();
566 qreal w = rect.width();
567 qreal h = rect.height();
568 m_minWidth = qMax(m_minWidth,w);
569 m_minHeight = qMax(m_minHeight,rect.height());
570 m_height = qMax(m_height,h);
571 item->setPos(point.x(),point.y() - h);
572 point.setX(point.x() + w);
573 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
574 // Next item would go off rect.
575 point.setX(m_rect.bottomLeft().x());
576 point.setY(point.y() - h);
577 if (i+1 < items.count()) {
578 m_height += h;
571 if (item->isVisible()) {
572 const QRectF& rect = item->boundingRect();
573 qreal w = rect.width();
574 qreal h = rect.height();
575 m_minWidth = qMax(m_minWidth,w);
576 m_minHeight = qMax(m_minHeight,rect.height());
577 m_height = qMax(m_height,h);
578 item->setPos(point.x(),point.y() - h);
579 point.setX(point.x() + w);
580 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
581 // Next item would go off rect.
582 point.setX(m_rect.bottomLeft().x());
583 point.setY(point.y() - h);
584 if (i+1 < items.count()) {
585 m_height += h;
586 }
579 587 }
580 588 }
581 589 }
@@ -595,21 +603,23 void QLegendPrivate::updateDetachedLayout()
595 603 qreal maxWidth = 0;
596 604 for (int i=0; i<items.count(); i++) {
597 605 QGraphicsItem *item = items.at(i);
598 const QRectF& rect = item->boundingRect();
599 qreal w = rect.width();
600 qreal h = rect.height();
601 m_minWidth = qMax(m_minWidth,rect.width());
602 m_minHeight = qMax(m_minHeight,h);
603 maxWidth = qMax(maxWidth,w);
604 item->setPos(point.x(),point.y());
605 point.setY(point.y() + h);
606 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
607 // Next item would go off rect.
608 point.setX(point.x() + maxWidth);
609 point.setY(m_rect.topLeft().y());
610 if (i+1 < items.count()) {
611 m_width += maxWidth;
612 maxWidth = 0;
606 if (item->isVisible()) {
607 const QRectF& rect = item->boundingRect();
608 qreal w = rect.width();
609 qreal h = rect.height();
610 m_minWidth = qMax(m_minWidth,rect.width());
611 m_minHeight = qMax(m_minHeight,h);
612 maxWidth = qMax(maxWidth,w);
613 item->setPos(point.x(),point.y());
614 point.setY(point.y() + h);
615 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
616 // Next item would go off rect.
617 point.setX(point.x() + maxWidth);
618 point.setY(m_rect.topLeft().y());
619 if (i+1 < items.count()) {
620 m_width += maxWidth;
621 maxWidth = 0;
622 }
613 623 }
614 624 }
615 625 }
@@ -630,21 +640,23 void QLegendPrivate::updateDetachedLayout()
630 640 qreal maxWidth = 0;
631 641 for (int i=0; i<items.count(); i++) {
632 642 QGraphicsItem *item = items.at(i);
633 const QRectF& rect = item->boundingRect();
634 qreal w = rect.width();
635 qreal h = rect.height();
636 m_minWidth = qMax(m_minWidth,rect.width());
637 m_minHeight = qMax(m_minHeight,h);
638 maxWidth = qMax(maxWidth,w);
639 item->setPos(point.x() - w,point.y());
640 point.setY(point.y() + h);
641 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
642 // Next item would go off rect.
643 point.setX(point.x() - maxWidth);
644 point.setY(m_rect.topLeft().y());
645 if (i+1 < items.count()) {
646 m_width += maxWidth;
647 maxWidth = 0;
643 if (item->isVisible()) {
644 const QRectF& rect = item->boundingRect();
645 qreal w = rect.width();
646 qreal h = rect.height();
647 m_minWidth = qMax(m_minWidth,rect.width());
648 m_minHeight = qMax(m_minHeight,h);
649 maxWidth = qMax(maxWidth,w);
650 item->setPos(point.x() - w,point.y());
651 point.setY(point.y() + h);
652 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
653 // Next item would go off rect.
654 point.setX(point.x() - maxWidth);
655 point.setY(m_rect.topLeft().y());
656 if (i+1 < items.count()) {
657 m_width += maxWidth;
658 maxWidth = 0;
659 }
648 660 }
649 661 }
650 662 }
@@ -682,6 +694,8 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
682 694 foreach(LegendMarker* marker, markers)
683 695 m_markers->addToGroup(marker);
684 696
697 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
698
685 699 if(series->type() == QAbstractSeries::SeriesTypePie) {
686 700 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
687 701 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
@@ -730,6 +744,21 void QLegendPrivate::handleUpdatePieSeries()
730 744 handleSeriesAdded(series, 0);
731 745 }
732 746
747 void QLegendPrivate::handleSeriesVisibleChanged()
748 {
749 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
750 QList<QGraphicsItem *> items = m_markers->childItems();
751
752 foreach (QGraphicsItem *markers, items) {
753 LegendMarker *marker = static_cast<LegendMarker*>(markers);
754 if (marker->series() == series) {
755 marker->setVisible(!marker->isVisible());
756 }
757 }
758
759 updateLayout();
760 }
761
733 762
734 763 #include "moc_qlegend.cpp"
735 764 #include "moc_qlegend_p.cpp"
@@ -57,6 +57,7 public Q_SLOTS:
57 57 void handleSeriesRemoved(QAbstractSeries *series);
58 58 void handleSeriesUpdated(QAbstractSeries *series);
59 59 void handleUpdatePieSeries(); //TODO remove this function
60 void handleSeriesVisibleChanged();
60 61
61 62 private:
62 63 QLegend *q_ptr;
General Comments 0
You need to be logged in to leave comments. Login now