##// END OF EJS Templates
Implement QChartView::setChart()
Jani Honkonen -
r2056:4b7228265b30
parent child
Show More
@@ -46,7 +46,8 ChartLayout::~ChartLayout()
46 46
47 47 void ChartLayout::setGeometry(const QRectF& rect)
48 48 {
49 Q_ASSERT(rect.isValid());
49 if (!rect.isValid())
50 return;
50 51
51 52 QList<ChartAxis*> axes = m_presenter->axisItems();
52 53 ChartTitle* title = m_presenter->titleElement();
@@ -88,6 +88,18 QChart* QChartView::chart() const
88 88 }
89 89
90 90 /*!
91 Sets the current chart to \a chart. Ownership of the new chart is passed to chartview
92 and ownership of the previous chart is released.
93
94 To avoid memory leaks users needs to make sure the previous chart is deleted.
95 */
96
97 void QChartView::setChart(QChart *chart)
98 {
99 d_ptr->setChart(chart);
100 }
101
102 /*!
91 103 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
92 104 */
93 105 void QChartView::setRubberBand(const RubberBands& rubberBand)
@@ -191,9 +203,7 void QChartView::mouseReleaseEvent(QMouseEvent *event)
191 203 void QChartView::resizeEvent(QResizeEvent *event)
192 204 {
193 205 QGraphicsView::resizeEvent(event);
194 d_ptr->m_chart->resize(size());
195 setMinimumSize(d_ptr->m_chart->minimumSize().toSize());
196 setSceneRect(d_ptr->m_chart->geometry());
206 d_ptr->resize();
197 207 }
198 208
199 209 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -221,6 +231,29 QChartViewPrivate::~QChartViewPrivate()
221 231
222 232 }
223 233
234 void QChartViewPrivate::setChart(QChart *chart)
235 {
236 Q_ASSERT(chart);
237
238 if (m_chart == chart)
239 return;
240
241 if (m_chart)
242 m_scene->removeItem(m_chart);
243
244 m_chart = chart;
245 m_scene->addItem(m_chart);
246
247 resize();
248 }
249
250 void QChartViewPrivate::resize()
251 {
252 m_chart->resize(q_ptr->size());
253 q_ptr->setMinimumSize(m_chart->minimumSize().toSize());
254 q_ptr->setSceneRect(m_chart->geometry());
255 }
256
224 257 #include "moc_qchartview.cpp"
225 258
226 259 QTCOMMERCIALCHART_END_NAMESPACE
@@ -54,7 +54,9 public:
54 54
55 55 void setRubberBand(const RubberBands& rubberBands);
56 56 RubberBands rubberBand() const;
57
57 58 QChart* chart() const;
59 void setChart(QChart *chart);
58 60
59 61 protected:
60 62 void resizeEvent(QResizeEvent *event);
@@ -45,6 +45,8 class QChartViewPrivate {
45 45 public:
46 46 explicit QChartViewPrivate(QChartView *q, QChart *chart = 0);
47 47 ~QChartViewPrivate();
48 void setChart(QChart *chart);
49 void resize();
48 50
49 51 protected:
50 52 QChartView *q_ptr;
@@ -49,6 +49,7 private Q_SLOTS:
49 49 void chart();
50 50 void rubberBand_data();
51 51 void rubberBand();
52 void setChart();
52 53
53 54 private:
54 55 QChartView* m_view;
@@ -183,6 +184,40 void tst_QChartView::rubberBand()
183 184
184 185 }
185 186
187 void tst_QChartView::setChart()
188 {
189 QPointer<QChart> oldChart = m_view->chart();
190 QLineSeries *series1 = new QLineSeries();
191 series1->append(0,0);
192 series1->append(1,1);
193 oldChart->addSeries(series1);
194
195 QPointer<QChart> newChart = new QChart();
196 QLineSeries *series2 = new QLineSeries();
197 series2->append(0,1);
198 series2->append(1,0);
199 newChart->addSeries(series2);
200
201 // show current chart
202 m_view->show();
203 QTest::qWaitForWindowShown(m_view);
204 QTest::qWait(1000);
205
206 // set new chart
207 m_view->setChart(newChart);
208 QVERIFY(m_view->chart() == newChart);
209 QVERIFY(!oldChart.isNull()); // ownership of the oldChart is released and not deleted
210 QTest::qWait(1000);
211
212 // delete the view
213 delete m_view;
214 m_view = 0;
215 QVERIFY(newChart.isNull()); // view owns and deletes it
216 QVERIFY(!oldChart.isNull()); // not owned by view
217
218 delete oldChart;
219 }
220
186 221 QTEST_MAIN(tst_QChartView)
187 222 #include "tst_qchartview.moc"
188 223
General Comments 0
You need to be logged in to leave comments. Login now