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