##// END OF EJS Templates
Add stub from minimum size
Michal Klocek -
r782:55ca9199fd76
parent child
Show More
@@ -1,456 +1,458
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qchart.h"
21 #include "qchart.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include <QGraphicsScene>
23 #include <QGraphicsScene>
24 #include <QGraphicsSceneResizeEvent>
24 #include <QGraphicsSceneResizeEvent>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 /*!
28 /*!
29 \enum QChart::ChartTheme
29 \enum QChart::ChartTheme
30
30
31 This enum describes the theme used by the chart.
31 This enum describes the theme used by the chart.
32
32
33 \value ChartThemeDefault Follows the GUI style of the Operating System
33 \value ChartThemeDefault Follows the GUI style of the Operating System
34 \value ChartThemeLight
34 \value ChartThemeLight
35 \value ChartThemeBlueCerulean
35 \value ChartThemeBlueCerulean
36 \value ChartThemeDark
36 \value ChartThemeDark
37 \value ChartThemeBrownSand
37 \value ChartThemeBrownSand
38 \value ChartThemeBlueNcs
38 \value ChartThemeBlueNcs
39 \value ChartThemeHighContrast
39 \value ChartThemeHighContrast
40 \value ChartThemeBlueIcy
40 \value ChartThemeBlueIcy
41 \value ChartThemeCount Not really a theme; the total count of themes.
41 \value ChartThemeCount Not really a theme; the total count of themes.
42 */
42 */
43
43
44 /*!
44 /*!
45 \enum QChart::AnimationOption
45 \enum QChart::AnimationOption
46
46
47 For enabling/disabling animations. Defaults to NoAnimation.
47 For enabling/disabling animations. Defaults to NoAnimation.
48
48
49 \value NoAnimation
49 \value NoAnimation
50 \value GridAxisAnimations
50 \value GridAxisAnimations
51 \value SeriesAnimations
51 \value SeriesAnimations
52 \value AllAnimations
52 \value AllAnimations
53 */
53 */
54
54
55 /*!
55 /*!
56 \class QChart
56 \class QChart
57 \brief QtCommercial chart API.
57 \brief QtCommercial chart API.
58
58
59 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
59 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
60 representation of different types of QChartSeries and other chart related objects like
60 representation of different types of QChartSeries and other chart related objects like
61 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
61 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
62 convenience class QChartView instead of QChart.
62 convenience class QChartView instead of QChart.
63 \sa QChartView
63 \sa QChartView
64 */
64 */
65
65
66 /*!
66 /*!
67 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
67 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
68 */
68 */
69 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
69 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
70 d_ptr(new QChartPrivate(this))
70 d_ptr(new QChartPrivate(this))
71 {
71 {
72 //setMinimumSize(200,200);
72 d_ptr->m_legend = new QLegend(this);
73 d_ptr->m_legend = new QLegend(this);
73 d_ptr->m_dataset = new ChartDataSet(this);
74 d_ptr->m_dataset = new ChartDataSet(this);
74 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
75 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
75
76 int padding = d_ptr->m_presenter->padding();
77 setMinimumSize(3*padding,3*padding);
76 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
78 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
77 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
79 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
78 }
80 }
79
81
80 /*!
82 /*!
81 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
83 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
82 */
84 */
83 QChart::~QChart()
85 QChart::~QChart()
84 {
86 {
85 //delete first presenter , since this is a root of all the graphical items
87 //delete first presenter , since this is a root of all the graphical items
86 delete d_ptr->m_presenter;
88 delete d_ptr->m_presenter;
87 d_ptr->m_presenter=0;
89 d_ptr->m_presenter=0;
88 }
90 }
89
91
90 /*!
92 /*!
91 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
93 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
92 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
94 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
93 the y axis).
95 the y axis).
94 */
96 */
95 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
97 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
96 {
98 {
97 d_ptr->m_dataset->addSeries(series, axisY);
99 d_ptr->m_dataset->addSeries(series, axisY);
98 }
100 }
99
101
100 /*!
102 /*!
101 Removes the \a series specified in a perameter from the QChartView.
103 Removes the \a series specified in a perameter from the QChartView.
102 It releses its ownership of the specified QChartSeries object.
104 It releses its ownership of the specified QChartSeries object.
103 It does not delete the pointed QChartSeries data object
105 It does not delete the pointed QChartSeries data object
104 \sa addSeries(), removeAllSeries()
106 \sa addSeries(), removeAllSeries()
105 */
107 */
106 void QChart::removeSeries(QSeries* series)
108 void QChart::removeSeries(QSeries* series)
107 {
109 {
108 d_ptr->m_dataset->removeSeries(series);
110 d_ptr->m_dataset->removeSeries(series);
109 }
111 }
110
112
111 /*!
113 /*!
112 Removes all the QChartSeries that have been added to the QChartView
114 Removes all the QChartSeries that have been added to the QChartView
113 It also deletes the pointed QChartSeries data objects
115 It also deletes the pointed QChartSeries data objects
114 \sa addSeries(), removeSeries()
116 \sa addSeries(), removeSeries()
115 */
117 */
116 void QChart::removeAllSeries()
118 void QChart::removeAllSeries()
117 {
119 {
118 d_ptr->m_dataset->removeAllSeries();
120 d_ptr->m_dataset->removeAllSeries();
119 }
121 }
120
122
121 /*!
123 /*!
122 Sets the \a brush that is used for painting the background of the chart area.
124 Sets the \a brush that is used for painting the background of the chart area.
123 */
125 */
124 void QChart::setBackgroundBrush(const QBrush& brush)
126 void QChart::setBackgroundBrush(const QBrush& brush)
125 {
127 {
126 d_ptr->createChartBackgroundItem();
128 d_ptr->createChartBackgroundItem();
127 d_ptr->m_backgroundItem->setBrush(brush);
129 d_ptr->m_backgroundItem->setBrush(brush);
128 d_ptr->m_backgroundItem->update();
130 d_ptr->m_backgroundItem->update();
129 }
131 }
130
132
131 QBrush QChart::backgroundBrush() const
133 QBrush QChart::backgroundBrush() const
132 {
134 {
133 if (!d_ptr->m_backgroundItem) return QBrush();
135 if (!d_ptr->m_backgroundItem) return QBrush();
134 return (d_ptr->m_backgroundItem)->brush();
136 return (d_ptr->m_backgroundItem)->brush();
135 }
137 }
136
138
137 /*!
139 /*!
138 Sets the \a pen that is used for painting the background of the chart area.
140 Sets the \a pen that is used for painting the background of the chart area.
139 */
141 */
140 void QChart::setBackgroundPen(const QPen& pen)
142 void QChart::setBackgroundPen(const QPen& pen)
141 {
143 {
142 d_ptr->createChartBackgroundItem();
144 d_ptr->createChartBackgroundItem();
143 d_ptr->m_backgroundItem->setPen(pen);
145 d_ptr->m_backgroundItem->setPen(pen);
144 d_ptr->m_backgroundItem->update();
146 d_ptr->m_backgroundItem->update();
145 }
147 }
146
148
147 QPen QChart::backgroundPen() const
149 QPen QChart::backgroundPen() const
148 {
150 {
149 if (!d_ptr->m_backgroundItem) return QPen();
151 if (!d_ptr->m_backgroundItem) return QPen();
150 return d_ptr->m_backgroundItem->pen();
152 return d_ptr->m_backgroundItem->pen();
151 }
153 }
152
154
153 /*!
155 /*!
154 Sets the chart \a title. The description text that is drawn above the chart.
156 Sets the chart \a title. The description text that is drawn above the chart.
155 */
157 */
156 void QChart::setTitle(const QString& title)
158 void QChart::setTitle(const QString& title)
157 {
159 {
158 d_ptr->createChartTitleItem();
160 d_ptr->createChartTitleItem();
159 d_ptr->m_titleItem->setText(title);
161 d_ptr->m_titleItem->setText(title);
160 d_ptr->updateLayout();
162 d_ptr->updateLayout();
161 }
163 }
162
164
163 /*!
165 /*!
164 Returns the chart title. The description text that is drawn above the chart.
166 Returns the chart title. The description text that is drawn above the chart.
165 */
167 */
166 QString QChart::title() const
168 QString QChart::title() const
167 {
169 {
168 if (d_ptr->m_titleItem)
170 if (d_ptr->m_titleItem)
169 return d_ptr->m_titleItem->text();
171 return d_ptr->m_titleItem->text();
170 else
172 else
171 return QString();
173 return QString();
172 }
174 }
173
175
174 /*!
176 /*!
175 Sets the \a font that is used for rendering the description text that is rendered above the chart.
177 Sets the \a font that is used for rendering the description text that is rendered above the chart.
176 */
178 */
177 void QChart::setTitleFont(const QFont& font)
179 void QChart::setTitleFont(const QFont& font)
178 {
180 {
179 d_ptr->createChartTitleItem();
181 d_ptr->createChartTitleItem();
180 d_ptr->m_titleItem->setFont(font);
182 d_ptr->m_titleItem->setFont(font);
181 d_ptr->updateLayout();
183 d_ptr->updateLayout();
182 }
184 }
183
185
184 /*!
186 /*!
185 Sets the \a brush used for rendering the title text.
187 Sets the \a brush used for rendering the title text.
186 */
188 */
187 void QChart::setTitleBrush(const QBrush &brush)
189 void QChart::setTitleBrush(const QBrush &brush)
188 {
190 {
189 d_ptr->createChartTitleItem();
191 d_ptr->createChartTitleItem();
190 d_ptr->m_titleItem->setBrush(brush);
192 d_ptr->m_titleItem->setBrush(brush);
191 d_ptr->updateLayout();
193 d_ptr->updateLayout();
192 }
194 }
193
195
194 /*!
196 /*!
195 Returns the brush used for rendering the title text.
197 Returns the brush used for rendering the title text.
196 */
198 */
197 QBrush QChart::titleBrush() const
199 QBrush QChart::titleBrush() const
198 {
200 {
199 if (!d_ptr->m_titleItem) return QBrush();
201 if (!d_ptr->m_titleItem) return QBrush();
200 return d_ptr->m_titleItem->brush();
202 return d_ptr->m_titleItem->brush();
201 }
203 }
202
204
203 /*!
205 /*!
204 Sets the \a theme used by the chart for rendering the graphical representation of the data
206 Sets the \a theme used by the chart for rendering the graphical representation of the data
205 \sa ChartTheme, chartTheme()
207 \sa ChartTheme, chartTheme()
206 */
208 */
207 void QChart::setTheme(QChart::ChartTheme theme)
209 void QChart::setTheme(QChart::ChartTheme theme)
208 {
210 {
209 d_ptr->m_presenter->setTheme(theme);
211 d_ptr->m_presenter->setTheme(theme);
210 }
212 }
211
213
212 /*!
214 /*!
213 Returns the theme enum used by the chart.
215 Returns the theme enum used by the chart.
214 \sa ChartTheme, setChartTheme()
216 \sa ChartTheme, setChartTheme()
215 */
217 */
216 QChart::ChartTheme QChart::theme() const
218 QChart::ChartTheme QChart::theme() const
217 {
219 {
218 return d_ptr->m_presenter->theme();
220 return d_ptr->m_presenter->theme();
219 }
221 }
220
222
221 /*!
223 /*!
222 Zooms in the view by a factor of 2
224 Zooms in the view by a factor of 2
223 */
225 */
224 void QChart::zoomIn()
226 void QChart::zoomIn()
225 {
227 {
226 d_ptr->m_presenter->zoomIn();
228 d_ptr->m_presenter->zoomIn();
227 }
229 }
228
230
229 /*!
231 /*!
230 Zooms in the view to a maximum level at which \a rect is still fully visible.
232 Zooms in the view to a maximum level at which \a rect is still fully visible.
231 */
233 */
232 void QChart::zoomIn(const QRectF& rect)
234 void QChart::zoomIn(const QRectF& rect)
233 {
235 {
234 if (!rect.isValid()) return;
236 if (!rect.isValid()) return;
235 d_ptr->m_presenter->zoomIn(rect);
237 d_ptr->m_presenter->zoomIn(rect);
236 }
238 }
237
239
238 /*!
240 /*!
239 Restores the view zoom level to the previous one.
241 Restores the view zoom level to the previous one.
240 */
242 */
241 void QChart::zoomOut()
243 void QChart::zoomOut()
242 {
244 {
243 d_ptr->m_presenter->zoomOut();
245 d_ptr->m_presenter->zoomOut();
244 }
246 }
245
247
246 /*!
248 /*!
247 Returns the pointer to the x axis object of the chart
249 Returns the pointer to the x axis object of the chart
248 */
250 */
249 QChartAxis* QChart::axisX() const
251 QChartAxis* QChart::axisX() const
250 {
252 {
251 return d_ptr->m_dataset->axisX();
253 return d_ptr->m_dataset->axisX();
252 }
254 }
253
255
254 /*!
256 /*!
255 Returns the pointer to the y axis object of the chart
257 Returns the pointer to the y axis object of the chart
256 */
258 */
257 QChartAxis* QChart::axisY() const
259 QChartAxis* QChart::axisY() const
258 {
260 {
259 return d_ptr->m_dataset->axisY();
261 return d_ptr->m_dataset->axisY();
260 }
262 }
261
263
262 /*!
264 /*!
263 Returns the legend object of the chart. Ownership stays in chart.
265 Returns the legend object of the chart. Ownership stays in chart.
264 */
266 */
265 QLegend& QChart::legend() const
267 QLegend& QChart::legend() const
266 {
268 {
267 return *d_ptr->m_legend;
269 return *d_ptr->m_legend;
268 }
270 }
269
271
270 /*!
272 /*!
271 Gives ownership of legend to user.
273 Gives ownership of legend to user.
272 */
274 */
273 QLegend* QChart::takeLegend()
275 QLegend* QChart::takeLegend()
274 {
276 {
275 QLegend* l = d_ptr->m_legend;
277 QLegend* l = d_ptr->m_legend;
276 d_ptr->m_legend = 0;
278 d_ptr->m_legend = 0;
277 return l;
279 return l;
278 }
280 }
279
281
280 /*!
282 /*!
281 Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one
283 Gives ownership of legend back to chart. QChart takes ownership of \a legend and deletes existing one
282 */
284 */
283 void QChart::giveLegend(QLegend *legend)
285 void QChart::giveLegend(QLegend *legend)
284 {
286 {
285 if (d_ptr->m_legend) {
287 if (d_ptr->m_legend) {
286 // Should not happen.
288 // Should not happen.
287 qDebug() << "Warning! Giving more than one legend to chart.";
289 qDebug() << "Warning! Giving more than one legend to chart.";
288 delete d_ptr->m_legend;
290 delete d_ptr->m_legend;
289 }
291 }
290
292
291 d_ptr->m_legend = legend;
293 d_ptr->m_legend = legend;
292
294
293 // Reconnect legend, in case not already connected.
295 // Reconnect legend, in case not already connected.
294 disconnect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
296 disconnect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
295 disconnect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
297 disconnect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
296 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
298 connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
297 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
299 connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*)));
298 }
300 }
299
301
300 /*!
302 /*!
301 Resizes and updates the chart area using the \a event data
303 Resizes and updates the chart area using the \a event data
302 */
304 */
303 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
305 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
304 {
306 {
305 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
307 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
306 d_ptr->updateLayout();
308 d_ptr->updateLayout();
307 QGraphicsWidget::resizeEvent(event);
309 QGraphicsWidget::resizeEvent(event);
308 update();
310 update();
309 }
311 }
310
312
311 /*!
313 /*!
312 Sets animation \a options for the chart
314 Sets animation \a options for the chart
313 */
315 */
314 void QChart::setAnimationOptions(AnimationOptions options)
316 void QChart::setAnimationOptions(AnimationOptions options)
315 {
317 {
316 d_ptr->m_presenter->setAnimationOptions(options);
318 d_ptr->m_presenter->setAnimationOptions(options);
317 }
319 }
318
320
319 /*!
321 /*!
320 Returns animation options for the chart
322 Returns animation options for the chart
321 */
323 */
322 QChart::AnimationOptions QChart::animationOptions() const
324 QChart::AnimationOptions QChart::animationOptions() const
323 {
325 {
324 return d_ptr->m_presenter->animationOptions();
326 return d_ptr->m_presenter->animationOptions();
325 }
327 }
326
328
327 void QChart::scrollLeft()
329 void QChart::scrollLeft()
328 {
330 {
329 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
331 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
330 }
332 }
331
333
332 void QChart::scrollRight()
334 void QChart::scrollRight()
333 {
335 {
334 d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
336 d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
335 }
337 }
336
338
337 void QChart::scrollUp()
339 void QChart::scrollUp()
338 {
340 {
339 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
341 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
340 }
342 }
341
343
342 void QChart::scrollDown()
344 void QChart::scrollDown()
343 {
345 {
344 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
346 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
345 }
347 }
346
348
347 void QChart::setBackgroundVisible(bool visible)
349 void QChart::setBackgroundVisible(bool visible)
348 {
350 {
349 d_ptr->createChartBackgroundItem();
351 d_ptr->createChartBackgroundItem();
350 d_ptr->m_backgroundItem->setVisible(visible);
352 d_ptr->m_backgroundItem->setVisible(visible);
351 }
353 }
352
354
353 bool QChart::isBackgroundVisible() const
355 bool QChart::isBackgroundVisible() const
354 {
356 {
355 if (!d_ptr->m_backgroundItem) return false;
357 if (!d_ptr->m_backgroundItem) return false;
356 return d_ptr->m_backgroundItem->isVisible();
358 return d_ptr->m_backgroundItem->isVisible();
357 }
359 }
358
360
359 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
361 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
360
362
361 QChartPrivate::QChartPrivate(QChart *parent):
363 QChartPrivate::QChartPrivate(QChart *parent):
362 q_ptr(parent),
364 q_ptr(parent),
363 m_backgroundItem(0),
365 m_backgroundItem(0),
364 m_titleItem(0),
366 m_titleItem(0),
365 m_legend(0),
367 m_legend(0),
366 m_dataset(0),
368 m_dataset(0),
367 m_presenter(0)
369 m_presenter(0)
368 {
370 {
369
371
370 }
372 }
371
373
372 QChartPrivate::~QChartPrivate()
374 QChartPrivate::~QChartPrivate()
373 {
375 {
374
376
375 }
377 }
376
378
377 void QChartPrivate::createChartBackgroundItem()
379 void QChartPrivate::createChartBackgroundItem()
378 {
380 {
379 if (!m_backgroundItem) {
381 if (!m_backgroundItem) {
380 m_backgroundItem = new ChartBackground(q_ptr);
382 m_backgroundItem = new ChartBackground(q_ptr);
381 m_backgroundItem->setPen(Qt::NoPen);
383 m_backgroundItem->setPen(Qt::NoPen);
382 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
384 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
383 }
385 }
384 }
386 }
385
387
386 void QChartPrivate::createChartTitleItem()
388 void QChartPrivate::createChartTitleItem()
387 {
389 {
388 if (!m_titleItem) {
390 if (!m_titleItem) {
389 m_titleItem = new QGraphicsSimpleTextItem(q_ptr);
391 m_titleItem = new QGraphicsSimpleTextItem(q_ptr);
390 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
392 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
391 }
393 }
392 }
394 }
393
395
394 void QChartPrivate::updateLegendLayout()
396 void QChartPrivate::updateLegendLayout()
395 {
397 {
396 int padding = m_presenter->padding();
398 int padding = m_presenter->padding();
397 QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding);
399 QRectF plotRect = m_rect.adjusted(padding,padding,-padding,-padding);
398 QRectF legendRect;
400 QRectF legendRect;
399
401
400 switch (m_legend->alignment())
402 switch (m_legend->alignment())
401 {
403 {
402 case QLegend::LayoutTop: {
404 case QLegend::LayoutTop: {
403 legendRect = plotRect.adjusted(0,0,0,-padding - plotRect.height());
405 legendRect = plotRect.adjusted(0,0,0,-padding - plotRect.height());
404 break;
406 break;
405 }
407 }
406 case QLegend::LayoutBottom: {
408 case QLegend::LayoutBottom: {
407 legendRect = plotRect.adjusted(padding,padding + plotRect.height(),-padding,0);
409 legendRect = plotRect.adjusted(padding,padding + plotRect.height(),-padding,0);
408 break;
410 break;
409 }
411 }
410 case QLegend::LayoutLeft: {
412 case QLegend::LayoutLeft: {
411 legendRect = plotRect.adjusted(0,padding,-padding - plotRect.width(),-padding);
413 legendRect = plotRect.adjusted(0,padding,-padding - plotRect.width(),-padding);
412 break;
414 break;
413 }
415 }
414 case QLegend::LayoutRight: {
416 case QLegend::LayoutRight: {
415 legendRect = plotRect.adjusted(padding + plotRect.width(),padding,0,-padding);
417 legendRect = plotRect.adjusted(padding + plotRect.width(),padding,0,-padding);
416 break;
418 break;
417 }
419 }
418 default: {
420 default: {
419 legendRect = plotRect;
421 legendRect = plotRect;
420 break;
422 break;
421 }
423 }
422 }
424 }
423
425
424 m_legend->setMaximumSize(legendRect.size());
426 m_legend->setMaximumSize(legendRect.size());
425 m_legend->setPos(legendRect.topLeft());
427 m_legend->setPos(legendRect.topLeft());
426 }
428 }
427
429
428 void QChartPrivate::updateLayout()
430 void QChartPrivate::updateLayout()
429 {
431 {
430 if (!m_rect.isValid()) return;
432 if (!m_rect.isValid()) return;
431
433
432 int padding = m_presenter->padding();
434 int padding = m_presenter->padding();
433 int backgroundPadding = m_presenter->backgroundPadding();
435 int backgroundPadding = m_presenter->backgroundPadding();
434
436
435 // recalculate title position
437 // recalculate title position
436 if (m_titleItem) {
438 if (m_titleItem) {
437 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
439 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
438 m_titleItem->setPos(center.x(),m_rect.top()/2 + padding/2);
440 m_titleItem->setPos(center.x(),m_rect.top()/2 + padding/2);
439 }
441 }
440
442
441 //recalculate background gradient
443 //recalculate background gradient
442 if (m_backgroundItem) {
444 if (m_backgroundItem) {
443 m_backgroundItem->setRect(m_rect.adjusted(backgroundPadding,backgroundPadding, -backgroundPadding, -backgroundPadding));
445 m_backgroundItem->setRect(m_rect.adjusted(backgroundPadding,backgroundPadding, -backgroundPadding, -backgroundPadding));
444 }
446 }
445
447
446 // recalculate legend position
448 // recalculate legend position
447 if (m_legend) {
449 if (m_legend) {
448 if (m_legend->parentObject() == q_ptr) {
450 if (m_legend->parentObject() == q_ptr) {
449 updateLegendLayout();
451 updateLegendLayout();
450 }
452 }
451 }
453 }
452 }
454 }
453
455
454 #include "moc_qchart.cpp"
456 #include "moc_qchart.cpp"
455
457
456 QTCOMMERCIALCHART_END_NAMESPACE
458 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,233 +1,233
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qchartview.h"
21 #include "qchartview.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include "qchartview_p.h"
23 #include "qchartview_p.h"
24 #include <QGraphicsScene>
24 #include <QGraphicsScene>
25 #include <QRubberBand>
25 #include <QRubberBand>
26
26
27
27
28 /*!
28 /*!
29 \enum QChartView::RubberBandPolicy
29 \enum QChartView::RubberBandPolicy
30
30
31 This enum describes the different types of rubber bands that can be used for zoom rect selection
31 This enum describes the different types of rubber bands that can be used for zoom rect selection
32
32
33 \value NoRubberBand
33 \value NoRubberBand
34 \value VerticalRubberBand
34 \value VerticalRubberBand
35 \value HorizonalRubberBand
35 \value HorizonalRubberBand
36 \value RectangleRubberBand
36 \value RectangleRubberBand
37 */
37 */
38
38
39 /*!
39 /*!
40 \class QChartView
40 \class QChartView
41 \brief Standalone charting widget.
41 \brief Standalone charting widget.
42
42
43 QChartView is a standalone widget that can display charts. It does not require separate
43 QChartView is a standalone widget that can display charts. It does not require separate
44 QGraphicsScene to work. It manages the graphical representation of different types of
44 QGraphicsScene to work. It manages the graphical representation of different types of
45 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
45 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
46 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
46 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
47
47
48 \sa QChart
48 \sa QChart
49 */
49 */
50
50
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
51 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52
52
53 /*!
53 /*!
54 Constructs a chartView object which is a child of a\a parent.
54 Constructs a chartView object which is a child of a\a parent.
55 */
55 */
56 QChartView::QChartView(QChart *chart,QWidget *parent) :
56 QChartView::QChartView(QChart *chart,QWidget *parent) :
57 QGraphicsView(parent),
57 QGraphicsView(parent),
58 d_ptr(new QChartViewPrivate())
58 d_ptr(new QChartViewPrivate())
59 {
59 {
60 d_ptr->m_scene = new QGraphicsScene(this);
60 d_ptr->m_scene = new QGraphicsScene(this);
61 d_ptr->m_chart = chart;
61 d_ptr->m_chart = chart;
62 d_ptr->m_presenter = chart->d_ptr->m_presenter;
62 d_ptr->m_presenter = chart->d_ptr->m_presenter;
63
64 setFrameShape(QFrame::NoFrame);
63 setFrameShape(QFrame::NoFrame);
65 setBackgroundRole(QPalette::Window);
64 setBackgroundRole(QPalette::Window);
66 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
68 setScene(d_ptr->m_scene);
67 setScene(d_ptr->m_scene);
69 d_ptr->m_scene->addItem(chart);
68 d_ptr->m_scene->addItem(chart);
70 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
69 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
71 }
70 }
72
71
73
72
74 /*!
73 /*!
75 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
74 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
76 */
75 */
77 QChartView::~QChartView()
76 QChartView::~QChartView()
78 {
77 {
79 }
78 }
80
79
81 QChart* QChartView::chart() const
80 QChart* QChartView::chart() const
82 {
81 {
83 return d_ptr->m_chart;
82 return d_ptr->m_chart;
84 }
83 }
85
84
86 /*!
85 /*!
87 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
86 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
88 */
87 */
89 void QChartView::setRubberBand(const RubberBands& rubberBand)
88 void QChartView::setRubberBand(const RubberBands& rubberBand)
90 {
89 {
91 d_ptr->m_rubberBandFlags=rubberBand;
90 d_ptr->m_rubberBandFlags=rubberBand;
92
91
93 if (!d_ptr->m_rubberBandFlags) {
92 if (!d_ptr->m_rubberBandFlags) {
94 delete d_ptr->m_rubberBand;
93 delete d_ptr->m_rubberBand;
95 d_ptr->m_rubberBand=0;
94 d_ptr->m_rubberBand=0;
96 return;
95 return;
97 }
96 }
98
97
99 if (!d_ptr->m_rubberBand) {
98 if (!d_ptr->m_rubberBand) {
100 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
99 d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
101 d_ptr->m_rubberBand->setEnabled(true);
100 d_ptr->m_rubberBand->setEnabled(true);
102 }
101 }
103 }
102 }
104
103
105 /*!
104 /*!
106 Returns the RubberBandPolicy that is currently being used by the widget.
105 Returns the RubberBandPolicy that is currently being used by the widget.
107 */
106 */
108 QChartView::RubberBands QChartView::rubberBand() const
107 QChartView::RubberBands QChartView::rubberBand() const
109 {
108 {
110 return d_ptr->m_rubberBandFlags;
109 return d_ptr->m_rubberBandFlags;
111 }
110 }
112
111
113 /*!
112 /*!
114 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
113 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
115 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
114 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
116 */
115 */
117 void QChartView::mousePressEvent(QMouseEvent *event)
116 void QChartView::mousePressEvent(QMouseEvent *event)
118 {
117 {
119 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
118 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
120
119
121 int padding = d_ptr->m_presenter->padding();
120 int padding = d_ptr->m_presenter->padding();
122 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
121 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
123
122
124 if (rect.contains(event->pos())) {
123 if (rect.contains(event->pos())) {
125 d_ptr->m_rubberBandOrigin = event->pos();
124 d_ptr->m_rubberBandOrigin = event->pos();
126 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
125 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
127 d_ptr->m_rubberBand->show();
126 d_ptr->m_rubberBand->show();
128 event->accept();
127 event->accept();
129 }
128 }
130 }
129 }
131 else {
130 else {
132 QGraphicsView::mousePressEvent(event);
131 QGraphicsView::mousePressEvent(event);
133 }
132 }
134 }
133 }
135
134
136 /*!
135 /*!
137 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
136 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
138 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
137 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
139 */
138 */
140 void QChartView::mouseMoveEvent(QMouseEvent *event)
139 void QChartView::mouseMoveEvent(QMouseEvent *event)
141 {
140 {
142 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
141 if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
143 int padding = d_ptr->m_presenter->padding();
142 int padding = d_ptr->m_presenter->padding();
144 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
143 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
145 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
144 int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x();
146 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
145 int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y();
147 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
146 if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) {
148 d_ptr->m_rubberBandOrigin.setY(rect.top());
147 d_ptr->m_rubberBandOrigin.setY(rect.top());
149 height = rect.height();
148 height = rect.height();
150 }
149 }
151 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
150 if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) {
152 d_ptr->m_rubberBandOrigin.setX(rect.left());
151 d_ptr->m_rubberBandOrigin.setX(rect.left());
153 width= rect.width();
152 width= rect.width();
154 }
153 }
155 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
154 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized());
156 }
155 }
157 else {
156 else {
158 QGraphicsView::mouseMoveEvent(event);
157 QGraphicsView::mouseMoveEvent(event);
159 }
158 }
160 }
159 }
161
160
162 /*!
161 /*!
163 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
162 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
164 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
163 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
165 */
164 */
166 void QChartView::mouseReleaseEvent(QMouseEvent *event)
165 void QChartView::mouseReleaseEvent(QMouseEvent *event)
167 {
166 {
168 if(d_ptr->m_rubberBand) {
167 if(d_ptr->m_rubberBand) {
169 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
168 if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) {
170 d_ptr->m_rubberBand->hide();
169 d_ptr->m_rubberBand->hide();
171 QRect rect = d_ptr->m_rubberBand->geometry();
170 QRect rect = d_ptr->m_rubberBand->geometry();
172 d_ptr->m_chart->zoomIn(rect);
171 d_ptr->m_chart->zoomIn(rect);
173 event->accept();
172 event->accept();
174 }
173 }
175
174
176 if(event->button()==Qt::RightButton){
175 if(event->button()==Qt::RightButton){
177 d_ptr->m_chart->zoomOut();
176 d_ptr->m_chart->zoomOut();
178 event->accept();
177 event->accept();
179 }
178 }
180 }
179 }
181 else {
180 else {
182 QGraphicsView::mouseReleaseEvent(event);
181 QGraphicsView::mouseReleaseEvent(event);
183 }
182 }
184 }
183 }
185
184
186 /*!
185 /*!
187 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
186 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
188 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
187 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
189 */
188 */
190 void QChartView::keyPressEvent(QKeyEvent *event)
189 void QChartView::keyPressEvent(QKeyEvent *event)
191 {
190 {
192 switch (event->key()) {
191 switch (event->key()) {
193 case Qt::Key_Plus:
192 case Qt::Key_Plus:
194 d_ptr->m_chart->zoomIn();
193 d_ptr->m_chart->zoomIn();
195 break;
194 break;
196 case Qt::Key_Minus:
195 case Qt::Key_Minus:
197 d_ptr->m_chart->zoomOut();
196 d_ptr->m_chart->zoomOut();
198 break;
197 break;
199 default:
198 default:
200 QGraphicsView::keyPressEvent(event);
199 QGraphicsView::keyPressEvent(event);
201 break;
200 break;
202 }
201 }
203 }
202 }
204
203
205 /*!
204 /*!
206 Resizes and updates the chart area using the \a event data
205 Resizes and updates the chart area using the \a event data
207 */
206 */
208 void QChartView::resizeEvent(QResizeEvent *event)
207 void QChartView::resizeEvent(QResizeEvent *event)
209 {
208 {
210 d_ptr->m_chart->resize(size());
211 QGraphicsView::resizeEvent(event);
209 QGraphicsView::resizeEvent(event);
210 d_ptr->m_chart->resize(size());
211 setSceneRect(d_ptr->m_chart->geometry());
212 }
212 }
213
213
214 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
214 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
215
215
216 QChartViewPrivate::QChartViewPrivate():
216 QChartViewPrivate::QChartViewPrivate():
217 m_scene(0),
217 m_scene(0),
218 m_chart(0),
218 m_chart(0),
219 m_presenter(0),
219 m_presenter(0),
220 m_rubberBand(0),
220 m_rubberBand(0),
221 m_rubberBandFlags(QChartView::NoRubberBand)
221 m_rubberBandFlags(QChartView::NoRubberBand)
222 {
222 {
223
223
224 }
224 }
225
225
226 QChartViewPrivate::~QChartViewPrivate()
226 QChartViewPrivate::~QChartViewPrivate()
227 {
227 {
228
228
229 }
229 }
230
230
231 #include "moc_qchartview.cpp"
231 #include "moc_qchartview.cpp"
232
232
233 QTCOMMERCIALCHART_END_NAMESPACE
233 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now