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