##// END OF EJS Templates
Bugfix: delete presenter first, before root of all graphical items
Michal Klocek -
r686:8c2240c1bb61
parent child
Show More
@@ -1,388 +1,391
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "qlegend.h"
3 #include "qlegend.h"
4 #include "chartpresenter_p.h"
4 #include "chartpresenter_p.h"
5 #include "chartdataset_p.h"
5 #include "chartdataset_p.h"
6 #include "chartbackground_p.h"
6 #include "chartbackground_p.h"
7 #include <QGraphicsScene>
7 #include <QGraphicsScene>
8 #include <QGraphicsSceneResizeEvent>
8 #include <QGraphicsSceneResizeEvent>
9 #include <QDebug>
9 #include <QDebug>
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 /*!
13 /*!
14 \enum QChart::ChartTheme
14 \enum QChart::ChartTheme
15
15
16 This enum describes the theme used by the chart.
16 This enum describes the theme used by the chart.
17
17
18 \value ChartThemeDefault Follows the GUI style of the Operating System
18 \value ChartThemeDefault Follows the GUI style of the Operating System
19 \value ChartThemeVanilla
19 \value ChartThemeVanilla
20 \value ChartThemeIcy
20 \value ChartThemeIcy
21 \value ChartThemeGrayscale
21 \value ChartThemeGrayscale
22 \value ChartThemeScientific
22 \value ChartThemeScientific
23 \value ChartThemeBlueCerulean
23 \value ChartThemeBlueCerulean
24 \value ChartThemeLight
24 \value ChartThemeLight
25 \value ChartThemeCount Not really a theme; the total count of themes.
25 \value ChartThemeCount Not really a theme; the total count of themes.
26 */
26 */
27
27
28 /*!
28 /*!
29 \enum QChart::AnimationOption
29 \enum QChart::AnimationOption
30
30
31 For enabling/disabling animations. Defaults to NoAnimation.
31 For enabling/disabling animations. Defaults to NoAnimation.
32
32
33 \value NoAnimation
33 \value NoAnimation
34 \value GridAxisAnimations
34 \value GridAxisAnimations
35 \value SeriesAnimations
35 \value SeriesAnimations
36 \value AllAnimations
36 \value AllAnimations
37 */
37 */
38
38
39 /*!
39 /*!
40 \class QChart
40 \class QChart
41 \brief QtCommercial chart API.
41 \brief QtCommercial chart API.
42
42
43 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
43 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
44 representation of different types of QChartSeries and other chart related objects like
44 representation of different types of QChartSeries and other chart related objects like
45 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
45 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
46 convenience class QChartView instead of QChart.
46 convenience class QChartView instead of QChart.
47 \sa QChartView
47 \sa QChartView
48 */
48 */
49
49
50 /*!
50 /*!
51 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
51 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
52 */
52 */
53 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
53 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
54 m_backgroundItem(0),
54 m_backgroundItem(0),
55 m_titleItem(0),
55 m_titleItem(0),
56 m_legend(new QLegend(this)),
56 m_legend(new QLegend(this)),
57 m_dataset(new ChartDataSet(this)),
57 m_dataset(new ChartDataSet(this)),
58 m_presenter(new ChartPresenter(this,m_dataset)),
58 m_presenter(new ChartPresenter(this,m_dataset)),
59 m_padding(50),
59 m_padding(50),
60 m_backgroundPadding(10)
60 m_backgroundPadding(10)
61 {
61 {
62 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
62 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
63 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
63 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
64 }
64 }
65
65
66 /*!
66 /*!
67 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
67 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
68 */
68 */
69 QChart::~QChart()
69 QChart::~QChart()
70 {
70 {
71 //delete first presenter , since this is a root of all the graphical items
72 delete m_presenter;
73 m_presenter=0;
71 }
74 }
72
75
73 /*!
76 /*!
74 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
77 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
75 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
78 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
76 the y axis).
79 the y axis).
77 */
80 */
78 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
81 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
79 {
82 {
80 m_dataset->addSeries(series, axisY);
83 m_dataset->addSeries(series, axisY);
81 }
84 }
82
85
83 /*!
86 /*!
84 Removes the \a series specified in a perameter from the QChartView.
87 Removes the \a series specified in a perameter from the QChartView.
85 It releses its ownership of the specified QChartSeries object.
88 It releses its ownership of the specified QChartSeries object.
86 It does not delete the pointed QChartSeries data object
89 It does not delete the pointed QChartSeries data object
87 \sa addSeries(), removeAllSeries()
90 \sa addSeries(), removeAllSeries()
88 */
91 */
89 void QChart::removeSeries(QSeries* series)
92 void QChart::removeSeries(QSeries* series)
90 {
93 {
91 m_dataset->removeSeries(series);
94 m_dataset->removeSeries(series);
92 }
95 }
93
96
94 /*!
97 /*!
95 Removes all the QChartSeries that have been added to the QChartView
98 Removes all the QChartSeries that have been added to the QChartView
96 It also deletes the pointed QChartSeries data objects
99 It also deletes the pointed QChartSeries data objects
97 \sa addSeries(), removeSeries()
100 \sa addSeries(), removeSeries()
98 */
101 */
99 void QChart::removeAllSeries()
102 void QChart::removeAllSeries()
100 {
103 {
101 m_dataset->removeAllSeries();
104 m_dataset->removeAllSeries();
102 }
105 }
103
106
104 /*!
107 /*!
105 Sets the \a brush that is used for painting the background of the chart area.
108 Sets the \a brush that is used for painting the background of the chart area.
106 */
109 */
107 void QChart::setBackgroundBrush(const QBrush& brush)
110 void QChart::setBackgroundBrush(const QBrush& brush)
108 {
111 {
109 createChartBackgroundItem();
112 createChartBackgroundItem();
110 m_backgroundItem->setBrush(brush);
113 m_backgroundItem->setBrush(brush);
111 m_backgroundItem->update();
114 m_backgroundItem->update();
112 }
115 }
113
116
114 QBrush QChart::backgroundBrush() const
117 QBrush QChart::backgroundBrush() const
115 {
118 {
116 if(!m_backgroundItem) return QBrush();
119 if(!m_backgroundItem) return QBrush();
117 return m_backgroundItem->brush();
120 return m_backgroundItem->brush();
118 }
121 }
119
122
120 /*!
123 /*!
121 Sets the \a pen that is used for painting the background of the chart area.
124 Sets the \a pen that is used for painting the background of the chart area.
122 */
125 */
123 void QChart::setBackgroundPen(const QPen& pen)
126 void QChart::setBackgroundPen(const QPen& pen)
124 {
127 {
125 createChartBackgroundItem();
128 createChartBackgroundItem();
126 m_backgroundItem->setPen(pen);
129 m_backgroundItem->setPen(pen);
127 m_backgroundItem->update();
130 m_backgroundItem->update();
128 }
131 }
129
132
130 QPen QChart::backgroundPen() const
133 QPen QChart::backgroundPen() const
131 {
134 {
132 if(!m_backgroundItem) return QPen();
135 if(!m_backgroundItem) return QPen();
133 return m_backgroundItem->pen();
136 return m_backgroundItem->pen();
134 }
137 }
135
138
136 /*!
139 /*!
137 Sets the chart \a title. The description text that is drawn above the chart.
140 Sets the chart \a title. The description text that is drawn above the chart.
138 */
141 */
139 void QChart::setTitle(const QString& title)
142 void QChart::setTitle(const QString& title)
140 {
143 {
141 createChartTitleItem();
144 createChartTitleItem();
142 m_titleItem->setText(title);
145 m_titleItem->setText(title);
143 updateLayout();
146 updateLayout();
144 }
147 }
145
148
146 /*!
149 /*!
147 Returns the chart title. The description text that is drawn above the chart.
150 Returns the chart title. The description text that is drawn above the chart.
148 */
151 */
149 QString QChart::title() const
152 QString QChart::title() const
150 {
153 {
151 if(m_titleItem)
154 if(m_titleItem)
152 return m_titleItem->text();
155 return m_titleItem->text();
153 else
156 else
154 return QString();
157 return QString();
155 }
158 }
156
159
157 /*!
160 /*!
158 Sets the \a font that is used for rendering the description text that is rendered above the chart.
161 Sets the \a font that is used for rendering the description text that is rendered above the chart.
159 */
162 */
160 void QChart::setTitleFont(const QFont& font)
163 void QChart::setTitleFont(const QFont& font)
161 {
164 {
162 createChartTitleItem();
165 createChartTitleItem();
163 m_titleItem->setFont(font);
166 m_titleItem->setFont(font);
164 updateLayout();
167 updateLayout();
165 }
168 }
166
169
167 /*!
170 /*!
168 Sets the \a brush used for rendering the title text.
171 Sets the \a brush used for rendering the title text.
169 */
172 */
170 void QChart::setTitleBrush(const QBrush &brush)
173 void QChart::setTitleBrush(const QBrush &brush)
171 {
174 {
172 createChartTitleItem();
175 createChartTitleItem();
173 m_titleItem->setBrush(brush);
176 m_titleItem->setBrush(brush);
174 updateLayout();
177 updateLayout();
175 }
178 }
176
179
177 /*!
180 /*!
178 Returns the brush used for rendering the title text.
181 Returns the brush used for rendering the title text.
179 */
182 */
180 QBrush QChart::titleBrush() const
183 QBrush QChart::titleBrush() const
181 {
184 {
182 if(!m_titleItem) return QBrush();
185 if(!m_titleItem) return QBrush();
183 return m_titleItem->brush();
186 return m_titleItem->brush();
184 }
187 }
185
188
186 void QChart::createChartBackgroundItem()
189 void QChart::createChartBackgroundItem()
187 {
190 {
188 if(!m_backgroundItem) {
191 if(!m_backgroundItem) {
189 m_backgroundItem = new ChartBackground(this);
192 m_backgroundItem = new ChartBackground(this);
190 m_backgroundItem->setPen(Qt::NoPen);
193 m_backgroundItem->setPen(Qt::NoPen);
191 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
194 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
192 }
195 }
193 }
196 }
194
197
195 void QChart::createChartTitleItem()
198 void QChart::createChartTitleItem()
196 {
199 {
197 if(!m_titleItem) {
200 if(!m_titleItem) {
198 m_titleItem = new QGraphicsSimpleTextItem(this);
201 m_titleItem = new QGraphicsSimpleTextItem(this);
199 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
202 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
200 }
203 }
201 }
204 }
202
205
203 /*!
206 /*!
204 Sets the \a theme used by the chart for rendering the graphical representation of the data
207 Sets the \a theme used by the chart for rendering the graphical representation of the data
205 \sa ChartTheme, chartTheme()
208 \sa ChartTheme, chartTheme()
206 */
209 */
207 void QChart::setChartTheme(QChart::ChartTheme theme)
210 void QChart::setChartTheme(QChart::ChartTheme theme)
208 {
211 {
209 m_presenter->setChartTheme(theme);
212 m_presenter->setChartTheme(theme);
210 }
213 }
211
214
212 /*!
215 /*!
213 Returns the theme enum used by the chart.
216 Returns the theme enum used by the chart.
214 \sa ChartTheme, setChartTheme()
217 \sa ChartTheme, setChartTheme()
215 */
218 */
216 QChart::ChartTheme QChart::chartTheme() const
219 QChart::ChartTheme QChart::chartTheme() const
217 {
220 {
218 return m_presenter->chartTheme();
221 return m_presenter->chartTheme();
219 }
222 }
220
223
221 /*!
224 /*!
222 Zooms in the view by a factor of 2
225 Zooms in the view by a factor of 2
223 */
226 */
224 void QChart::zoomIn()
227 void QChart::zoomIn()
225 {
228 {
226 m_presenter->zoomIn();
229 m_presenter->zoomIn();
227 }
230 }
228
231
229 /*!
232 /*!
230 Zooms in the view to a maximum level at which \a rect is still fully visible.
233 Zooms in the view to a maximum level at which \a rect is still fully visible.
231 */
234 */
232 void QChart::zoomIn(const QRectF& rect)
235 void QChart::zoomIn(const QRectF& rect)
233 {
236 {
234
237
235 if(!rect.isValid()) return;
238 if(!rect.isValid()) return;
236 m_presenter->zoomIn(rect);
239 m_presenter->zoomIn(rect);
237 }
240 }
238
241
239 /*!
242 /*!
240 Restores the view zoom level to the previous one.
243 Restores the view zoom level to the previous one.
241 */
244 */
242 void QChart::zoomOut()
245 void QChart::zoomOut()
243 {
246 {
244 m_presenter->zoomOut();
247 m_presenter->zoomOut();
245 }
248 }
246
249
247 /*!
250 /*!
248 Returns the pointer to the x axis object of the chart
251 Returns the pointer to the x axis object of the chart
249 */
252 */
250 QChartAxis* QChart::axisX() const
253 QChartAxis* QChart::axisX() const
251 {
254 {
252 return m_dataset->axisX();
255 return m_dataset->axisX();
253 }
256 }
254
257
255 /*!
258 /*!
256 Returns the pointer to the y axis object of the chart
259 Returns the pointer to the y axis object of the chart
257 */
260 */
258 QChartAxis* QChart::axisY() const
261 QChartAxis* QChart::axisY() const
259 {
262 {
260 return m_dataset->axisY();
263 return m_dataset->axisY();
261 }
264 }
262
265
263 /*!
266 /*!
264 Returns the legend object of the chart. Ownership stays in chart.
267 Returns the legend object of the chart. Ownership stays in chart.
265 */
268 */
266 QLegend* QChart::legend() const
269 QLegend* QChart::legend() const
267 {
270 {
268 return m_legend;
271 return m_legend;
269 }
272 }
270
273
271 /*!
274 /*!
272 Resizes and updates the chart area using the \a event data
275 Resizes and updates the chart area using the \a event data
273 */
276 */
274 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
277 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
275 {
278 {
276
279
277 m_rect = QRectF(QPoint(0,0),event->newSize());
280 m_rect = QRectF(QPoint(0,0),event->newSize());
278 updateLayout();
281 updateLayout();
279 QGraphicsWidget::resizeEvent(event);
282 QGraphicsWidget::resizeEvent(event);
280 update();
283 update();
281 }
284 }
282
285
283 /*!
286 /*!
284 Sets animation \a options for the chart
287 Sets animation \a options for the chart
285 */
288 */
286 void QChart::setAnimationOptions(AnimationOptions options)
289 void QChart::setAnimationOptions(AnimationOptions options)
287 {
290 {
288 m_presenter->setAnimationOptions(options);
291 m_presenter->setAnimationOptions(options);
289 }
292 }
290
293
291 /*!
294 /*!
292 Returns animation options for the chart
295 Returns animation options for the chart
293 */
296 */
294 QChart::AnimationOptions QChart::animationOptions() const
297 QChart::AnimationOptions QChart::animationOptions() const
295 {
298 {
296 return m_presenter->animationOptions();
299 return m_presenter->animationOptions();
297 }
300 }
298
301
299 void QChart::scrollLeft()
302 void QChart::scrollLeft()
300 {
303 {
301 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
304 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
302 }
305 }
303
306
304 void QChart::scrollRight()
307 void QChart::scrollRight()
305 {
308 {
306 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
309 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
307 }
310 }
308 void QChart::scrollUp()
311 void QChart::scrollUp()
309 {
312 {
310 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
313 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
311 }
314 }
312 void QChart::scrollDown()
315 void QChart::scrollDown()
313 {
316 {
314 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
317 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
315 }
318 }
316
319
317 void QChart::updateLayout()
320 void QChart::updateLayout()
318 {
321 {
319 if(!m_rect.isValid()) return;
322 if(!m_rect.isValid()) return;
320
323
321 QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
324 QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding);
322
325
323 // recalculate title position
326 // recalculate title position
324 if (m_titleItem) {
327 if (m_titleItem) {
325 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
328 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
326 m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2);
329 m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2);
327 }
330 }
328
331
329 //recalculate background gradient
332 //recalculate background gradient
330 if (m_backgroundItem) {
333 if (m_backgroundItem) {
331 m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding));
334 m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding));
332 }
335 }
333
336
334 // recalculate legend position
337 // recalculate legend position
335 if (m_legend) {
338 if (m_legend) {
336 if (m_legend->parentObject() == this) {
339 if (m_legend->parentObject() == this) {
337 m_legend->setMaximumSize(rect.size());
340 m_legend->setMaximumSize(rect.size());
338 m_legend->setPos(rect.topLeft());
341 m_legend->setPos(rect.topLeft());
339 }
342 }
340 }
343 }
341 }
344 }
342
345
343
346
344 int QChart::padding() const
347 int QChart::padding() const
345 {
348 {
346 return m_padding;
349 return m_padding;
347 }
350 }
348
351
349 void QChart::setPadding(int padding)
352 void QChart::setPadding(int padding)
350 {
353 {
351 if(m_padding==padding){
354 if(m_padding==padding){
352 m_padding = padding;
355 m_padding = padding;
353 m_presenter->handleGeometryChanged();
356 m_presenter->handleGeometryChanged();
354 updateLayout();
357 updateLayout();
355 }
358 }
356 }
359 }
357
360
358 void QChart::setBackgroundPadding(int padding)
361 void QChart::setBackgroundPadding(int padding)
359 {
362 {
360 if(m_backgroundPadding!=padding){
363 if(m_backgroundPadding!=padding){
361 m_backgroundPadding = padding;
364 m_backgroundPadding = padding;
362 updateLayout();
365 updateLayout();
363 }
366 }
364 }
367 }
365
368
366 void QChart::setBackgroundDiameter(int diameter)
369 void QChart::setBackgroundDiameter(int diameter)
367 {
370 {
368 createChartBackgroundItem();
371 createChartBackgroundItem();
369 m_backgroundItem->setDimeter(diameter);
372 m_backgroundItem->setDimeter(diameter);
370 m_backgroundItem->update();
373 m_backgroundItem->update();
371 }
374 }
372
375
373 void QChart::setBackgroundVisible(bool visible)
376 void QChart::setBackgroundVisible(bool visible)
374 {
377 {
375 createChartBackgroundItem();
378 createChartBackgroundItem();
376 m_backgroundItem->setVisible(visible);
379 m_backgroundItem->setVisible(visible);
377 }
380 }
378
381
379 bool QChart::isBackgroundVisible() const
382 bool QChart::isBackgroundVisible() const
380 {
383 {
381 if(!m_backgroundItem) return false;
384 if(!m_backgroundItem) return false;
382 return m_backgroundItem->isVisible();
385 return m_backgroundItem->isVisible();
383 }
386 }
384
387
385
388
386 #include "moc_qchart.cpp"
389 #include "moc_qchart.cpp"
387
390
388 QTCOMMERCIALCHART_END_NAMESPACE
391 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now