##// END OF EJS Templates
Better handling for new or removed markers
sauimone -
r2182:7a5f7d3b1a79
parent child
Show More
@@ -67,6 +67,16 QAreaLegendMarkerPrivate::~QAreaLegendMarkerPrivate()
67 67 QObject::disconnect(m_series, SIGNAL(nameChanged()), this, SLOT(updated()));
68 68 }
69 69
70 QAreaSeries* QAreaLegendMarkerPrivate::series()
71 {
72 return m_series;
73 }
74
75 QObject* QAreaLegendMarkerPrivate::relatedObject()
76 {
77 return m_series;
78 }
79
70 80 void QAreaLegendMarkerPrivate::updated()
71 81 {
72 82 m_item->setBrush(m_series->brush());
@@ -48,6 +48,10 public:
48 48 explicit QAreaLegendMarkerPrivate(QAreaLegendMarker *q, QAreaSeries *series, QLegend *legend);
49 49 virtual ~QAreaLegendMarkerPrivate();
50 50
51 // internal
52 virtual QAreaSeries* series();
53 virtual QObject* relatedObject();
54
51 55 public Q_SLOTS:
52 56 virtual void updated();
53 57
@@ -76,6 +76,16 QBarLegendMarkerPrivate::~QBarLegendMarkerPrivate()
76 76 QObject::disconnect(m_barset, SIGNAL(penChanged()), this, SLOT(updated()));
77 77 }
78 78
79 QAbstractBarSeries* QBarLegendMarkerPrivate::series()
80 {
81 return m_series;
82 }
83
84 QObject* QBarLegendMarkerPrivate::relatedObject()
85 {
86 return m_barset;
87 }
88
79 89 void QBarLegendMarkerPrivate::updated()
80 90 {
81 91 m_item->setPen(m_barset->pen());
@@ -49,6 +49,9 public:
49 49 explicit QBarLegendMarkerPrivate(QBarLegendMarker *q, QAbstractBarSeries *series, QBarSet *barset, QLegend *legend);
50 50 virtual ~QBarLegendMarkerPrivate();
51 51
52 virtual QAbstractBarSeries* series();
53 virtual QObject* relatedObject();
54
52 55 public Q_SLOTS:
53 56 virtual void updated();
54 57
@@ -43,7 +43,7
43 43 #include <QTimer>
44 44 #include <QGraphicsSceneEvent>
45 45
46 #include <QLegendMarker>
46 #include "qlegendmarker.h"
47 47 #include "qlegendmarker_p.h"
48 48 #include "legendmarkeritem_p.h"
49 49
@@ -483,13 +483,8 void QLegendPrivate::appendSeries(QAbstractSeries* series)
483 483 }
484 484
485 485 QList<QLegendMarker*> newMarkers = series->d_ptr->createLegendMarkers(q_ptr);
486 foreach (QLegendMarker* marker, newMarkers) {
487 marker->setFont(m_font);
488 marker->setLabelBrush(m_labelBrush);
489 marker->setVisible(series->isVisible());
490 m_items->addToGroup(marker->d_ptr.data()->item());
491 m_legendMarkers << marker;
492 }
486 decorateMarkers(newMarkers);
487 addMarkers(newMarkers);
493 488
494 489 // TODO: This is the part I don't like. There should be better solution.
495 490 // On the other hand. It is only one switch case for appending and another for removing series
@@ -534,14 +529,14 void QLegendPrivate::removeSeries(QAbstractSeries* series)
534 529 m_series.removeOne(series);
535 530 }
536 531
537 foreach (QLegendMarker *marker, m_legendMarkers) {
538 if (marker->series() == series) {
539 marker->d_ptr.data()->item()->setVisible(false);
540 m_items->removeFromGroup(marker->d_ptr.data()->item());
541 delete marker;
542 m_legendMarkers.removeAll(marker);
532 // Find out, which markers to remove
533 QList<QLegendMarker *> removed;
534 foreach (QLegendMarker *m, m_legendMarkers) {
535 if (m->series() == series) {
536 removed << m;
543 537 }
544 538 }
539 removeMarkers(removed);
545 540
546 541 switch (series->type())
547 542 {
@@ -571,9 +566,7 void QLegendPrivate::removeSeries(QAbstractSeries* series)
571 566 }
572 567 }
573 568
574
575 569 QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
576 // QObject::disconnect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*)));
577 570
578 571 m_layout->invalidate();
579 572 // q_ptr->layout()->activate();
@@ -608,18 +601,70 void QLegendPrivate::handleSeriesVisibleChanged()
608 601
609 602 void QLegendPrivate::handleCountChanged()
610 603 {
611 // TODO: With new markers, the series should notify markers directly.
612 // TODO: Better way to handle updates. Remove/Add series again seems like overkill.
604 // Here we handle the changes in marker count.
605 // Can happen for example when pieslice(s) have been added to or removed from pieseries.
613 606
614 607 QAbstractSeries *series = qobject_cast<QAbstractSeries *> (sender());
615 608 qDebug() << "QLegendPrivate::handleSeriesUpdated" << series;
616 609
617 // Handle new or removed markers
618 // Handle changes of marker pen/brush/label. every property that legend is interested
619 handleSeriesRemoved(series);
620 handleSeriesAdded(series);
610 QList<QLegendMarker *> createdMarkers = series->d_ptr->createLegendMarkers(q_ptr);
611
612 // Find out removed markers and created markers
613 QList<QLegendMarker *> removedMarkers;
614 foreach (QLegendMarker *oldMarker, m_legendMarkers) {
615 // we have marker, which is related to sender.
616 if (oldMarker->series() == series) {
617 bool found = false;
618 foreach(QLegendMarker *newMarker, createdMarkers) {
619 // New marker considered existing if:
620 // - d_ptr->relatedObject() is same for both markers.
621 if (newMarker->d_ptr->relatedObject() == oldMarker->d_ptr->relatedObject()) {
622 // Delete the new marker, since we already have existing marker, that might be connected on user side.
623 found = true;
624 createdMarkers.removeOne(newMarker);
625 delete newMarker;
626 }
627 }
628 if (!found) {
629 // No related object found for marker, add to removedMarkers list
630 removedMarkers << oldMarker;
631 }
632 }
633 }
634
635 removeMarkers(removedMarkers);
636 addMarkers(createdMarkers);
637
638 q_ptr->layout()->invalidate();
639 }
640
641 void QLegendPrivate::addMarkers(QList<QLegendMarker *> markers)
642 {
643 foreach (QLegendMarker* marker, markers) {
644 m_items->addToGroup(marker->d_ptr.data()->item());
645 m_legendMarkers << marker;
646 }
647 }
648
649 void QLegendPrivate::removeMarkers(QList<QLegendMarker *> markers)
650 {
651 foreach (QLegendMarker *marker, markers) {
652 marker->d_ptr->item()->setVisible(false);
653 m_items->removeFromGroup(marker->d_ptr->item());
654 delete marker;
655 m_legendMarkers.removeOne(marker);
656 }
657 }
658
659 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
660 {
661 foreach (QLegendMarker* marker, markers) {
662 marker->setFont(m_font);
663 marker->setLabelBrush(m_labelBrush);
664 }
621 665 }
622 666
667
623 668 #include "moc_qlegend.cpp"
624 669 #include "moc_qlegend_p.cpp"
625 670
@@ -66,6 +66,12 public Q_SLOTS:
66 66 void handleCountChanged();
67 67
68 68 private:
69 // Internal helpers
70 void addMarkers(QList<QLegendMarker *> markers);
71 void removeMarkers(QList<QLegendMarker *> markers);
72 void decorateMarkers(QList<QLegendMarker *> markers);
73
74 private:
69 75 QLegend *q_ptr;
70 76 ChartPresenter *m_presenter;
71 77 LegendLayout *m_layout;
@@ -25,6 +25,7
25 25 #include <QDebug>
26 26 #include <QFontMetrics>
27 27 #include <QGraphicsSceneEvent>
28 #include <QAbstractSeries>
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31
@@ -32,6 +33,7 QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) :
32 33 QObject(parent),
33 34 d_ptr(&d)
34 35 {
36 d_ptr->m_item->setVisible(d_ptr->series()->isVisible());
35 37 }
36 38
37 39 QLegendMarker::~QLegendMarker()
@@ -104,6 +106,7 QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q, QLegend *legend) :
104 106 m_legend(legend)
105 107 {
106 108 m_item = new LegendMarkerItem(this);
109 // m_item->setVisible(q->series()->isVisible());
107 110 }
108 111
109 112 QLegendMarkerPrivate::~QLegendMarkerPrivate()
@@ -74,14 +74,13 public:
74 74
75 75 virtual QAbstractSeries* series() = 0;
76 76
77 protected:
78 explicit QLegendMarker(QLegendMarkerPrivate &d, QObject *parent = 0);
79
80 77 Q_SIGNALS:
81 78 void clicked();
82 79 void hovered(bool status);
83 80
84 81 protected:
82 explicit QLegendMarker(QLegendMarkerPrivate &d, QObject *parent = 0);
83
85 84 QScopedPointer<QLegendMarkerPrivate> d_ptr;
86 85 Q_DISABLE_COPY(QLegendMarker)
87 86 friend class QLegendPrivate;
@@ -66,6 +66,10 public:
66 66 // Item gets the event, logic for event is here
67 67 void handleMousePressEvent(QGraphicsSceneEvent *event);
68 68
69 // internal
70 virtual QAbstractSeries* series() = 0;
71 virtual QObject* relatedObject() = 0;
72
69 73 public Q_SLOTS:
70 74 virtual void updated() {};
71 75
@@ -76,6 +76,16 QPieLegendMarkerPrivate::~QPieLegendMarkerPrivate()
76 76 QObject::disconnect(m_slice, SIGNAL(penChanged()), this, SLOT(updated()));
77 77 }
78 78
79 QPieSeries* QPieLegendMarkerPrivate::series()
80 {
81 return m_series;
82 }
83
84 QObject* QPieLegendMarkerPrivate::relatedObject()
85 {
86 return m_slice;
87 }
88
79 89 void QPieLegendMarkerPrivate::updated()
80 90 {
81 91 m_item->setPen(m_slice->pen());
@@ -49,6 +49,10 public:
49 49 explicit QPieLegendMarkerPrivate(QPieLegendMarker *q, QPieSeries *series, QPieSlice *slice, QLegend *legend);
50 50 virtual ~QPieLegendMarkerPrivate();
51 51
52 // internal
53 virtual QPieSeries* series();
54 virtual QObject* relatedObject();
55
52 56 public Q_SLOTS:
53 57 virtual void updated();
54 58
@@ -67,6 +67,16 QXYLegendMarkerPrivate::~QXYLegendMarkerPrivate()
67 67 QObject::disconnect(m_series, SIGNAL(nameChanged()), this, SLOT(updated()));
68 68 }
69 69
70 QAbstractSeries* QXYLegendMarkerPrivate::series()
71 {
72 return m_series;
73 }
74
75 QObject* QXYLegendMarkerPrivate::relatedObject()
76 {
77 return m_series;
78 }
79
70 80 void QXYLegendMarkerPrivate::updated()
71 81 {
72 82 m_item->setLabel(m_series->name());
@@ -48,6 +48,10 public:
48 48 explicit QXYLegendMarkerPrivate(QXYLegendMarker *q, QXYSeries *series, QLegend *legend);
49 49 virtual ~QXYLegendMarkerPrivate();
50 50
51 // internal
52 virtual QAbstractSeries* series();
53 virtual QObject* relatedObject();
54
51 55 public Q_SLOTS:
52 56 virtual void updated();
53 57
General Comments 0
You need to be logged in to leave comments. Login now