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