From 09026acabba0dcc2b85d1afaca0e29a3a99ba038 2012-02-21 12:55:45 From: Michal Klocek Date: 2012-02-21 12:55:45 Subject: [PATCH] Adds RemoveAllSeries method to API --- diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 6d78be0..b2487a8 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -168,16 +168,36 @@ void ChartDataSet::removeSeries(QChartSeries* series) emit seriesRemoved(series); m_seriesMap.remove(axis,series); //remove axis if no longer there - if(!m_seriesMap.contains(axis) && axis != m_axisY){ + if(!m_seriesMap.contains(axis)){ emit axisRemoved(axis); m_domainMap.remove(axis); + if(axis != m_axisY) delete axis; } + series->setParent(0); break; } } } +void ChartDataSet::removeAllSeries() +{ + QList keys = m_seriesMap.uniqueKeys(); + foreach(QChartAxis* axis , keys) { + QList seriesList = m_seriesMap.values(axis); + for(int i =0 ; i < seriesList.size();i++ ) + { + emit seriesRemoved(seriesList.at(i)); + delete(seriesList.at(i)); + } + m_seriesMap.remove(axis); + m_domainMap.remove(axis); + emit axisRemoved(axis); + if(axis != m_axisY) delete axis; + } + m_domainIndex=0; +} + bool ChartDataSet::nextDomain() { int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1; diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index c350b04..b5851c3 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -18,6 +18,7 @@ public: void addSeries(QChartSeries* series,QChartAxis *axisY = 0); void removeSeries(QChartSeries* series); + void removeAllSeries(); void addDomain(const QRectF& rect, const QRectF& viewport); bool nextDomain(); bool previousDomain(); diff --git a/src/qchart.cpp b/src/qchart.cpp index 3617c83..26d13e5 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -28,6 +28,11 @@ void QChart::removeSeries(QChartSeries* series) m_dataset->removeSeries(series); } +void QChart::removeAllSeries() +{ + m_dataset->removeAllSeries(); +} + void QChart::setChartBackgroundBrush(const QBrush& brush) { createChartBackgroundItem(); diff --git a/src/qchart.h b/src/qchart.h index f0d98a1..d9f72f7 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -42,8 +42,9 @@ public: QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); ~QChart(); - void addSeries(QChartSeries* series,QChartAxis *axisY=0); - void removeSeries(QChartSeries* series); + void addSeries(QChartSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership + void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached + void removeAllSeries(); // deletes series and axis void setMargin(int margin); int margin() const; diff --git a/src/qchartview.cpp b/src/qchartview.cpp index d9e54f5..bd8652f 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -51,6 +51,11 @@ void QChartView::removeSeries(QChartSeries* series) m_chart->removeSeries(series); } +void QChartView::removeAllSeries() +{ + m_chart->removeAllSeries(); +} + void QChartView::zoomIn() { m_chart->zoomIn(); diff --git a/src/qchartview.h b/src/qchartview.h index b7acb5b..a52c959 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -24,8 +24,9 @@ public: //implement from QWidget void resizeEvent(QResizeEvent *event); - void addSeries(QChartSeries* series,QChartAxis* axisY=0);// takes ownership - void removeSeries(QChartSeries* series); + void addSeries(QChartSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership + void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached + void removeAllSeries(); // deletes series and axis int margin() const; void setChartTitle(const QString& title); diff --git a/test/auto/chartdataset/tst_chartdataset.cpp b/test/auto/chartdataset/tst_chartdataset.cpp index 9c691b9..9d6e5e6 100644 --- a/test/auto/chartdataset/tst_chartdataset.cpp +++ b/test/auto/chartdataset/tst_chartdataset.cpp @@ -39,6 +39,8 @@ private slots: void nextpreviousDomain(); void removeSeries_data(); void removeSeries(); + void removeAllSeries_data(); + void removeAllSeries(); }; void tst_ChartDataSet::initTestCase() @@ -189,8 +191,6 @@ void tst_ChartDataSet::addSeries() else axisYCount++; - - QCOMPARE(spy0.count(), axisYCount); QCOMPARE(spy1.count(), seriesCount*2); QCOMPARE(spy2.count(), 0); @@ -513,6 +513,10 @@ void tst_ChartDataSet::removeSeries() dataSet.removeSeries(seriesList.at(i)); } + //default axis + if (axisYCount == 0) + axisYCount++; + QCOMPARE(spy0.count(), 0); QCOMPARE(spy1.count(), 0); QCOMPARE(spy2.count(), axisYCount); @@ -521,8 +525,75 @@ void tst_ChartDataSet::removeSeries() QCOMPARE(spy5.count(), seriesCount); QCOMPARE(dataSet.domainIndex(), 0); + + qDeleteAll(seriesList); } +void tst_ChartDataSet::removeAllSeries_data() +{ + QTest::addColumn("seriesCount"); + QTest::addColumn("axisYCount"); + QTest::newRow("2 series, default axis") << 2 << 0; + QTest::newRow("2 series, 2 new axis") << 2 << 2; + QTest::newRow("2 series, 1 new axis") << 2 << 2; + QTest::newRow("3 series, 3 new axis") << 3 << 3; + QTest::newRow("3 series, 2 new axis") << 3 << 2; + QTest::newRow("3 series, 1 new axis") << 3 << 1; +} + +void tst_ChartDataSet::removeAllSeries() +{ + QFETCH(int, seriesCount); + QFETCH(int, axisYCount); + + ChartDataSet dataSet; + + QList axisList; + + for (int i = 0; i < axisYCount; i++) { + QChartAxis* axis = new QChartAxis(); + axisList << axis; + } + + QList::iterator iterator = axisList.begin(); + + for (int i = 0; i < seriesCount; i++) { + QChartAxis* axisY = 0; + QLineChartSeries* series = new QLineChartSeries(); + if (iterator != axisList.end()) { + axisY = *iterator; + iterator++; + } else if (axisList.count() > 0) { + iterator--; + axisY = *iterator; + iterator++; + } + dataSet.addSeries(series, axisY); + } + + QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*))); + QSignalSpy spy1(&dataSet, SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&))); + QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*))); + QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QChartSeries*))); + QSignalSpy spy4(&dataSet, SIGNAL(seriesDomainChanged(QChartSeries*, Domain const&))); + QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QChartSeries*))); + + dataSet.removeAllSeries(); + //default axis + if (axisYCount == 0) + axisYCount++; + + QCOMPARE(spy0.count(), 0); + QCOMPARE(spy1.count(), 0); + QCOMPARE(spy2.count(), axisYCount); + QCOMPARE(spy3.count(), 0); + QCOMPARE(spy4.count(), 0); + QCOMPARE(spy5.count(), seriesCount); + + QCOMPARE(dataSet.domainIndex(), 0); +} + + QTEST_MAIN(tst_ChartDataSet) #include "tst_chartdataset.moc"