##// END OF EJS Templates
Implement QChartView::setChart()
Jani Honkonen -
r2056:4b7228265b30
parent child
Show More
@@ -46,7 +46,8 ChartLayout::~ChartLayout()
46
46
47 void ChartLayout::setGeometry(const QRectF& rect)
47 void ChartLayout::setGeometry(const QRectF& rect)
48 {
48 {
49 Q_ASSERT(rect.isValid());
49 if (!rect.isValid())
50 return;
50
51
51 QList<ChartAxis*> axes = m_presenter->axisItems();
52 QList<ChartAxis*> axes = m_presenter->axisItems();
52 ChartTitle* title = m_presenter->titleElement();
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 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
103 Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed.
92 */
104 */
93 void QChartView::setRubberBand(const RubberBands& rubberBand)
105 void QChartView::setRubberBand(const RubberBands& rubberBand)
@@ -191,9 +203,7 void QChartView::mouseReleaseEvent(QMouseEvent *event)
191 void QChartView::resizeEvent(QResizeEvent *event)
203 void QChartView::resizeEvent(QResizeEvent *event)
192 {
204 {
193 QGraphicsView::resizeEvent(event);
205 QGraphicsView::resizeEvent(event);
194 d_ptr->m_chart->resize(size());
206 d_ptr->resize();
195 setMinimumSize(d_ptr->m_chart->minimumSize().toSize());
196 setSceneRect(d_ptr->m_chart->geometry());
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 #include "moc_qchartview.cpp"
257 #include "moc_qchartview.cpp"
225
258
226 QTCOMMERCIALCHART_END_NAMESPACE
259 QTCOMMERCIALCHART_END_NAMESPACE
@@ -54,7 +54,9 public:
54
54
55 void setRubberBand(const RubberBands& rubberBands);
55 void setRubberBand(const RubberBands& rubberBands);
56 RubberBands rubberBand() const;
56 RubberBands rubberBand() const;
57
57 QChart* chart() const;
58 QChart* chart() const;
59 void setChart(QChart *chart);
58
60
59 protected:
61 protected:
60 void resizeEvent(QResizeEvent *event);
62 void resizeEvent(QResizeEvent *event);
@@ -45,6 +45,8 class QChartViewPrivate {
45 public:
45 public:
46 explicit QChartViewPrivate(QChartView *q, QChart *chart = 0);
46 explicit QChartViewPrivate(QChartView *q, QChart *chart = 0);
47 ~QChartViewPrivate();
47 ~QChartViewPrivate();
48 void setChart(QChart *chart);
49 void resize();
48
50
49 protected:
51 protected:
50 QChartView *q_ptr;
52 QChartView *q_ptr;
@@ -49,6 +49,7 private Q_SLOTS:
49 void chart();
49 void chart();
50 void rubberBand_data();
50 void rubberBand_data();
51 void rubberBand();
51 void rubberBand();
52 void setChart();
52
53
53 private:
54 private:
54 QChartView* m_view;
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 QTEST_MAIN(tst_QChartView)
221 QTEST_MAIN(tst_QChartView)
187 #include "tst_qchartview.moc"
222 #include "tst_qchartview.moc"
188
223
General Comments 0
You need to be logged in to leave comments. Login now