From e1337e5f8780502bf58cddab41ce6b1765a610f0 2012-04-10 11:19:22 From: Michal Klocek Date: 2012-04-10 11:19:22 Subject: [PATCH] Adds trivial legend handling for slices when removed or added --- diff --git a/src/legendmarker.cpp b/src/legendmarker.cpp index 9c6aade..7e2f2c2 100644 --- a/src/legendmarker.cpp +++ b/src/legendmarker.cpp @@ -156,7 +156,6 @@ m_pieslice(pieslice) { QObject::connect(this, SIGNAL(selected()),pieslice, SIGNAL(selected())); QObject::connect(pieslice, SIGNAL(changed()), this, SLOT(updated())); - QObject::connect(pieslice, SIGNAL(destroyed()), this, SLOT(deleteLater())); //TODO:checkthis updated(); } diff --git a/src/qchart.cpp b/src/qchart.cpp index bfd34c2..a5433f1 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -73,10 +73,7 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); d_ptr->createConnections(); d_ptr->m_legend = new LegendScroller(this); - // d_ptr->m_legend->setVisible(false); d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); - - //TODO:fix me setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3); } /*! diff --git a/src/qlegend.cpp b/src/qlegend.cpp index 7e2701b..a1bfc0a 100644 --- a/src/qlegend.cpp +++ b/src/qlegend.cpp @@ -90,7 +90,7 @@ d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this)) setZValue(ChartPresenter::LegendZValue); setFlags(QGraphicsItem::ItemClipsChildrenToShape); setEnabled(false); // By default legend is disabled - + setVisible(false); QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QSeries*,Domain*))); QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QSeries*))); } @@ -404,6 +404,13 @@ void QLegendPrivate::handleSeriesAdded(QSeries *series, Domain *domain) foreach(LegendMarker* marker , markers) m_markers->addToGroup(marker); + if(series->type()==QSeries::SeriesTypePie) + { + QPieSeries *pieSeries = static_cast(series); + QObject::connect(pieSeries,SIGNAL(added(QList)),this,SLOT(handleUpdateSeries())); + QObject::connect(pieSeries,SIGNAL(removed(QList)),this,SLOT(handleUpdateSeries())); + } + updateLayout(); } @@ -412,16 +419,32 @@ void QLegendPrivate::handleSeriesRemoved(QSeries *series) QList items = m_markers->childItems(); - foreach (QGraphicsItem *markers, items) { - LegendMarker *marker = static_cast(markers); - if (marker->series() == series) { - delete marker; - } - } + foreach (QGraphicsItem *markers, items) { + LegendMarker *marker = static_cast(markers); + if (marker->series() == series) { + delete marker; + } + } + + if(series->type()==QSeries::SeriesTypePie) + { + QPieSeries *pieSeries = static_cast(series); + QObject::disconnect(pieSeries,SIGNAL(added(QList)),this,SLOT(handleUpdateSeries())); + QObject::disconnect(pieSeries,SIGNAL(removed(QList)),this,SLOT(handleUpdateSeries())); + } updateLayout(); } +void QLegendPrivate::handleUpdateSeries() +{ + //TODO: reimplement to be optimal + QSeries* series = qobject_cast (sender()); + Q_ASSERT(series); + handleSeriesRemoved(series); + handleSeriesAdded(series,0); +} + #include "moc_qlegend.cpp" #include "moc_qlegend_p.cpp" diff --git a/src/qlegend_p.h b/src/qlegend_p.h index 36373e4..f30f705 100644 --- a/src/qlegend_p.h +++ b/src/qlegend_p.h @@ -49,6 +49,7 @@ public: public Q_SLOTS: void handleSeriesAdded(QSeries *series, Domain *domain); void handleSeriesRemoved(QSeries *series); + void handleUpdateSeries(); //TODO remove this function private: QLegend *q_ptr;