From b2efc1272dac057ae34ae09ead815d881bc6bdb9 2012-09-19 12:54:57 From: sauimone Date: 2012-09-19 12:54:57 Subject: [PATCH] legend private implemantation modified. --- diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 3e91203..1c4150a 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -677,14 +677,14 @@ bool QAbstractBarSeriesPrivate::append(QBarSet *set) // Fail if set is already in list or set is null. return false; } + m_barSets.append(set); QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); - emit restructuredBars(); // this notifies barchartitem - if (m_dataset) { - m_dataset->updateSeries(q); // this notifies legend - } + + emit restructuredBars(); // this notifies barchartitem + emit legendPropertiesUpdated(q); // this notifies legend return true; } @@ -695,14 +695,14 @@ bool QAbstractBarSeriesPrivate::remove(QBarSet *set) // Fail if set is not in list return false; } + m_barSets.removeOne(set); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); + emit restructuredBars(); // this notifies barchartitem - if (m_dataset) { - m_dataset->updateSeries(q); // this notifies legend - } + emit legendPropertiesUpdated(q); return true; } @@ -726,10 +726,9 @@ bool QAbstractBarSeriesPrivate::append(QList sets) QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); } + emit restructuredBars(); // this notifies barchartitem - if (m_dataset) { - m_dataset->updateSeries(q); // this notifies legend - } + emit legendPropertiesUpdated(q); // this notifies legend return true; } @@ -758,9 +757,7 @@ bool QAbstractBarSeriesPrivate::remove(QList sets) } emit restructuredBars(); // this notifies barchartitem - if (m_dataset) { - m_dataset->updateSeries(q); // this notifies legend - } + emit legendPropertiesUpdated(q); return true; } @@ -771,14 +768,14 @@ bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) // Fail if set is already in list or set is null. return false; } + m_barSets.insert(index, set); QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); + emit restructuredBars(); // this notifies barchartitem - if (m_dataset) { - m_dataset->updateSeries(q); // this notifies legend - } + emit legendPropertiesUpdated(q); return true; } diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 2b4e227..79fb89c 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -485,12 +485,12 @@ QList ChartDataSet::series() const { return m_seriesDomainMap.keys(); } - +/* void ChartDataSet::updateSeries(QAbstractSeries *series) { emit seriesUpdated(series); } - +*/ #include "moc_chartdataset_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 9b72e64..1996209 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -49,7 +49,7 @@ public: void addSeries(QAbstractSeries* series); void removeSeries(QAbstractSeries* series); void removeAllSeries(); - void updateSeries(QAbstractSeries* series); +// void updateSeries(QAbstractSeries* series); void zoomInDomain(const QRectF& rect, const QSizeF& size); void zoomOutDomain(const QRectF& rect, const QSizeF& size); @@ -72,7 +72,7 @@ public: Q_SIGNALS: void seriesAdded(QAbstractSeries* series, Domain* domain); void seriesRemoved(QAbstractSeries* series); - void seriesUpdated(QAbstractSeries* series); +// void seriesUpdated(QAbstractSeries* series); void axisAdded(QAbstractAxis* axis,Domain* domain); void axisRemoved(QAbstractAxis* axis); diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index 1392ff8..c61ecf9 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -174,7 +174,7 @@ d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) setFlags(QGraphicsItem::ItemClipsChildrenToShape); QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*))); - QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); +// QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); setLayout(d_ptr->m_layout); } @@ -464,7 +464,7 @@ void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) QList markers = series->d_ptr->createLegendMarker(q_ptr); - foreach(LegendMarker* marker, markers) { + foreach (LegendMarker* marker, markers) { marker->setFont(m_font); marker->setLabelBrush(m_labelBrush); marker->setVisible(series->isVisible()); @@ -473,12 +473,7 @@ void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) } QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); - - if(series->type() == QAbstractSeries::SeriesTypePie) { - QPieSeries *pieSeries = static_cast(series); - QObject::connect(pieSeries, SIGNAL(added(QList)), this, SLOT(handleUpdatePieSeries())); - QObject::connect(pieSeries, SIGNAL(removed(QList)), this, SLOT(handleUpdatePieSeries())); - } + QObject::connect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*))); q_ptr->layout()->invalidate(); q_ptr->layout()->activate(); @@ -493,34 +488,12 @@ void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) } } - if(series->type() == QAbstractSeries::SeriesTypePie) - { - QPieSeries *pieSeries = static_cast(series); - QObject::disconnect(pieSeries, SIGNAL(added(QList)), this, SLOT(handleUpdatePieSeries())); - QObject::disconnect(pieSeries, SIGNAL(removed(QList)), this, SLOT(handleUpdatePieSeries())); - } + QObject::disconnect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); + QObject::disconnect(series->d_ptr.data(), SIGNAL(legendPropertiesUpdated(QAbstractSeries*)), this, SLOT(handleLegendPropertiesUpdated(QAbstractSeries*))); q_ptr->layout()->invalidate(); } -void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series) -{ - // TODO: find out which markers are are added or removed. Update them - // TODO: better implementation - handleSeriesRemoved(series); - Domain domain; - handleSeriesAdded(series, &domain); -} - -void QLegendPrivate::handleUpdatePieSeries() -{ - //TODO: reimplement to be optimal - QPieSeries* series = qobject_cast (sender()); - Q_ASSERT(series); - handleSeriesRemoved(series); - handleSeriesAdded(series, 0); -} - void QLegendPrivate::handleSeriesVisibleChanged() { QAbstractSeries* series = qobject_cast (sender()); @@ -534,6 +507,15 @@ void QLegendPrivate::handleSeriesVisibleChanged() q_ptr->layout()->invalidate(); } +void QLegendPrivate::handleLegendPropertiesUpdated(QAbstractSeries *series) +{ + // Handle new or removed markers + // Handle changes of marker pen/brush/label. every property that legend is interested + handleSeriesRemoved(series); + Domain domain; + handleSeriesAdded(series, &domain); +} + #include "moc_qlegend.cpp" #include "moc_qlegend_p.cpp" diff --git a/src/legend/qlegend_p.h b/src/legend/qlegend_p.h index 24eb9fe..667734a 100644 --- a/src/legend/qlegend_p.h +++ b/src/legend/qlegend_p.h @@ -58,9 +58,8 @@ public: public Q_SLOTS: void handleSeriesAdded(QAbstractSeries *series, Domain *domain); void handleSeriesRemoved(QAbstractSeries *series); - void handleSeriesUpdated(QAbstractSeries *series); - void handleUpdatePieSeries(); //TODO remove this function void handleSeriesVisibleChanged(); + void handleLegendPropertiesUpdated(QAbstractSeries *series); private: QLegend *q_ptr; diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 90eef3e..a425c22 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -412,6 +412,13 @@ bool QPieSeries::append(QList slices) connect(s, SIGNAL(valueChanged()), d, SLOT(sliceValueChanged())); connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked())); connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); + + connect(s, SIGNAL(labelChanged()), d, SLOT(updateLegendProperties())); + connect(s, SIGNAL(penChanged()), d, SLOT(updateLegendProperties())); + connect(s, SIGNAL(brushChanged()), d, SLOT(updateLegendProperties())); + connect(s, SIGNAL(labelBrushChanged()), d, SLOT(updateLegendProperties())); + connect(s, SIGNAL(labelFontChanged()), d, SLOT(updateLegendProperties())); + connect(s, SIGNAL(labelFontChanged()), d, SLOT(updateLegendProperties())); } emit added(slices); @@ -471,6 +478,13 @@ bool QPieSeries::insert(int index, QPieSlice* slice) connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked())); connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool))); + connect(slice, SIGNAL(labelChanged()), d, SLOT(updateLegendProperties())); + connect(slice, SIGNAL(penChanged()), d, SLOT(updateLegendProperties())); + connect(slice, SIGNAL(brushChanged()), d, SLOT(updateLegendProperties())); + connect(slice, SIGNAL(labelBrushChanged()), d, SLOT(updateLegendProperties())); + connect(slice, SIGNAL(labelFontChanged()), d, SLOT(updateLegendProperties())); + connect(slice, SIGNAL(labelFontChanged()), d, SLOT(updateLegendProperties())); + emit added(QList() << slice); emit countChanged(); @@ -830,6 +844,13 @@ void QPieSeriesPrivate::sliceHovered(bool state) emit q->hovered(slice, state); } +void QPieSeriesPrivate::updateLegendProperties() +{ + // This slot listens to all properties of slices, which may interest legend and signals it. + Q_Q(QPieSeries); + emit legendPropertiesUpdated(q); +} + void QPieSeriesPrivate::scaleDomain(Domain& domain) { Q_UNUSED(domain); diff --git a/src/piechart/qpieseries_p.h b/src/piechart/qpieseries_p.h index 163d335..68fb5ce 100644 --- a/src/piechart/qpieseries_p.h +++ b/src/piechart/qpieseries_p.h @@ -67,6 +67,7 @@ public Q_SLOTS: void sliceValueChanged(); void sliceClicked(); void sliceHovered(bool state); + void updateLegendProperties(); private: QList m_slices; diff --git a/src/qabstractseries_p.h b/src/qabstractseries_p.h index 583c299..ac7da48 100644 --- a/src/qabstractseries_p.h +++ b/src/qabstractseries_p.h @@ -55,6 +55,9 @@ public: virtual void initializeAxis(QAbstractAxis* axis) = 0; virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation) const = 0; +Q_SIGNALS: + void legendPropertiesUpdated(QAbstractSeries* series); + protected: QAbstractSeries *q_ptr; QChart *m_chart;