##// END OF EJS Templates
Use window's palette for chart view by default
Tero Ahola -
r664:bbf0ad86a73c
parent child
Show More
@@ -1,397 +1,398
1 #include "qchartview.h"
1 #include "qchartview.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qchartaxis.h"
3 #include "qchartaxis.h"
4 #include <QGraphicsView>
4 #include <QGraphicsView>
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QRubberBand>
6 #include <QRubberBand>
7 #include <QResizeEvent>
7 #include <QResizeEvent>
8 #include <QDebug>
8 #include <QDebug>
9
9
10 /*!
10 /*!
11 \enum QChartView::RubberBandPolicy
11 \enum QChartView::RubberBandPolicy
12
12
13 This enum describes the different types of rubber bands that can be used for zoom rect selection
13 This enum describes the different types of rubber bands that can be used for zoom rect selection
14
14
15 \value NoRubberBand
15 \value NoRubberBand
16 \value VerticalRubberBand
16 \value VerticalRubberBand
17 \value HorizonalRubberBand
17 \value HorizonalRubberBand
18 \value RectangleRubberBand
18 \value RectangleRubberBand
19 */
19 */
20
20
21 /*!
21 /*!
22 \class QChartView
22 \class QChartView
23 \brief Standalone charting widget.
23 \brief Standalone charting widget.
24
24
25 QChartView is a standalone widget that can display charts. It does not require separate
25 QChartView is a standalone widget that can display charts. It does not require separate
26 QGraphicsScene to work. It manages the graphical representation of different types of
26 QGraphicsScene to work. It manages the graphical representation of different types of
27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
29
29
30 \sa QChart
30 \sa QChart
31 */
31 */
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 /*!
35 /*!
36 Constructs a chartView object which is a child of a\a parent.
36 Constructs a chartView object which is a child of a\a parent.
37 */
37 */
38 QChartView::QChartView(QWidget *parent) :
38 QChartView::QChartView(QWidget *parent) :
39 QGraphicsView(parent),
39 QGraphicsView(parent),
40 m_scene(new QGraphicsScene(this)),
40 m_scene(new QGraphicsScene(this)),
41 m_chart(new QChart()),
41 m_chart(new QChart()),
42 m_rubberBand(0),
42 m_rubberBand(0),
43 m_verticalRubberBand(false),
43 m_verticalRubberBand(false),
44 m_horizonalRubberBand(false)
44 m_horizonalRubberBand(false)
45 {
45 {
46 setBackgroundRole(QPalette::Window);
46 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 setScene(m_scene);
49 setScene(m_scene);
49 m_scene->addItem(m_chart);
50 m_scene->addItem(m_chart);
50 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
51 }
52 }
52
53
53
54
54 /*!
55 /*!
55 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
56 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
56 */
57 */
57 QChartView::~QChartView()
58 QChartView::~QChartView()
58 {
59 {
59 }
60 }
60
61
61 /*!
62 /*!
62 Resizes and updates the chart area using the \a event data
63 Resizes and updates the chart area using the \a event data
63 */
64 */
64 void QChartView::resizeEvent(QResizeEvent *event)
65 void QChartView::resizeEvent(QResizeEvent *event)
65 {
66 {
66 m_scene->setSceneRect(0,0,size().width(),size().height());
67 m_scene->setSceneRect(0,0,size().width(),size().height());
67 m_chart->resize(size());
68 m_chart->resize(size());
68 QWidget::resizeEvent(event);
69 QWidget::resizeEvent(event);
69 }
70 }
70
71
71 /*!
72 /*!
72 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
73 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
74 the y axis).
75 the y axis).
75 \sa removeSeries(), removeAllSeries()
76 \sa removeSeries(), removeAllSeries()
76 */
77 */
77 void QChartView::addSeries(QSeries* series,QChartAxis *axisY)
78 void QChartView::addSeries(QSeries* series,QChartAxis *axisY)
78 {
79 {
79 m_chart->addSeries(series,axisY);
80 m_chart->addSeries(series,axisY);
80 }
81 }
81
82
82 /*!
83 /*!
83 Removes the \a series specified in a perameter from the QChartView.
84 Removes the \a series specified in a perameter from the QChartView.
84 It releses its ownership of the specified QChartSeries object.
85 It releses its ownership of the specified QChartSeries object.
85 It does not delete the pointed QChartSeries data object
86 It does not delete the pointed QChartSeries data object
86 \sa addSeries(), removeAllSeries()
87 \sa addSeries(), removeAllSeries()
87 */
88 */
88 void QChartView::removeSeries(QSeries* series)
89 void QChartView::removeSeries(QSeries* series)
89 {
90 {
90 m_chart->removeSeries(series);
91 m_chart->removeSeries(series);
91 }
92 }
92
93
93 /*!
94 /*!
94 Removes all the QChartSeries that have been added to the QChartView
95 Removes all the QChartSeries that have been added to the QChartView
95 It also deletes the pointed QChartSeries data objects
96 It also deletes the pointed QChartSeries data objects
96 \sa addSeries(), removeSeries()
97 \sa addSeries(), removeSeries()
97 */
98 */
98 void QChartView::removeAllSeries()
99 void QChartView::removeAllSeries()
99 {
100 {
100 m_chart->removeAllSeries();
101 m_chart->removeAllSeries();
101 }
102 }
102
103
103 /*!
104 /*!
104 Zooms in the view by a factor of 2
105 Zooms in the view by a factor of 2
105 */
106 */
106 void QChartView::zoomIn()
107 void QChartView::zoomIn()
107 {
108 {
108 m_chart->zoomIn();
109 m_chart->zoomIn();
109 }
110 }
110
111
111 /*!
112 /*!
112 Zooms in the view to a maximum level at which \a rect is still fully visible.
113 Zooms in the view to a maximum level at which \a rect is still fully visible.
113 */
114 */
114 void QChartView::zoomIn(const QRect& rect)
115 void QChartView::zoomIn(const QRect& rect)
115 {
116 {
116 m_chart->zoomIn(rect);
117 m_chart->zoomIn(rect);
117 }
118 }
118
119
119 /*!
120 /*!
120 Restores the view zoom level to the previous one.
121 Restores the view zoom level to the previous one.
121 */
122 */
122 void QChartView::zoomOut()
123 void QChartView::zoomOut()
123 {
124 {
124 m_chart->zoomOut();
125 m_chart->zoomOut();
125 }
126 }
126
127
127 /*!
128 /*!
128 Sets the chart \a title. A description text that is drawn above the chart.
129 Sets the chart \a title. A description text that is drawn above the chart.
129 */
130 */
130 void QChartView::setChartTitle(const QString& title)
131 void QChartView::setChartTitle(const QString& title)
131 {
132 {
132 m_chart->setTitle(title);
133 m_chart->setTitle(title);
133 }
134 }
134
135
135 /*!
136 /*!
136 Returns the chart's title. A description text that is drawn above the chart.
137 Returns the chart's title. A description text that is drawn above the chart.
137 */
138 */
138 QString QChartView::chartTitle() const
139 QString QChartView::chartTitle() const
139 {
140 {
140 return m_chart->title();
141 return m_chart->title();
141 }
142 }
142
143
143 /*!
144 /*!
144 Sets the \a font that is used for rendering the description text that is rendered above the chart.
145 Sets the \a font that is used for rendering the description text that is rendered above the chart.
145 */
146 */
146 void QChartView::setChartTitleFont(const QFont& font)
147 void QChartView::setChartTitleFont(const QFont& font)
147 {
148 {
148 m_chart->setTitleFont(font);
149 m_chart->setTitleFont(font);
149 }
150 }
150
151
151 /*!
152 /*!
152 Sets the \a brush used for rendering the title text.
153 Sets the \a brush used for rendering the title text.
153 */
154 */
154 void QChartView::setChartTitleBrush(const QBrush &brush)
155 void QChartView::setChartTitleBrush(const QBrush &brush)
155 {
156 {
156 m_chart->setTitleBrush(brush);
157 m_chart->setTitleBrush(brush);
157 }
158 }
158
159
159 /*!
160 /*!
160 Returns the brush used for rendering the title text.
161 Returns the brush used for rendering the title text.
161 */
162 */
162 QBrush QChartView::chartTitleBrush()
163 QBrush QChartView::chartTitleBrush()
163 {
164 {
164 return m_chart->titleBrush();
165 return m_chart->titleBrush();
165 }
166 }
166
167
167 /*!
168 /*!
168 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
169 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
169 */
170 */
170 void QChartView::setChartBackgroundBrush(const QBrush& brush)
171 void QChartView::setChartBackgroundBrush(const QBrush& brush)
171 {
172 {
172 m_chart->setBackgroundBrush(brush);
173 m_chart->setBackgroundBrush(brush);
173 }
174 }
174
175
175 /*!
176 /*!
176 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
177 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
177 */
178 */
178 void QChartView::setChartBackgroundPen(const QPen& pen)
179 void QChartView::setChartBackgroundPen(const QPen& pen)
179 {
180 {
180 m_chart->setBackgroundPen(pen);
181 m_chart->setBackgroundPen(pen);
181 }
182 }
182
183
183 /*!
184 /*!
184 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
185 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
185 */
186 */
186 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
187 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
187 {
188 {
188 switch(policy) {
189 switch(policy) {
189 case VerticalRubberBand:
190 case VerticalRubberBand:
190 m_verticalRubberBand = true;
191 m_verticalRubberBand = true;
191 m_horizonalRubberBand = false;
192 m_horizonalRubberBand = false;
192 break;
193 break;
193 case HorizonalRubberBand:
194 case HorizonalRubberBand:
194 m_verticalRubberBand = false;
195 m_verticalRubberBand = false;
195 m_horizonalRubberBand = true;
196 m_horizonalRubberBand = true;
196 break;
197 break;
197 case RectangleRubberBand:
198 case RectangleRubberBand:
198 m_verticalRubberBand = true;
199 m_verticalRubberBand = true;
199 m_horizonalRubberBand = true;
200 m_horizonalRubberBand = true;
200 break;
201 break;
201 case NoRubberBand:
202 case NoRubberBand:
202 default:
203 default:
203 delete m_rubberBand;
204 delete m_rubberBand;
204 m_rubberBand=0;
205 m_rubberBand=0;
205 m_horizonalRubberBand = false;
206 m_horizonalRubberBand = false;
206 m_verticalRubberBand = false;
207 m_verticalRubberBand = false;
207 return;
208 return;
208 }
209 }
209 if(!m_rubberBand) {
210 if(!m_rubberBand) {
210 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
211 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
211 m_rubberBand->setEnabled(true);
212 m_rubberBand->setEnabled(true);
212 }
213 }
213 }
214 }
214
215
215 /*!
216 /*!
216 Returns the RubberBandPolicy that is currently being used by the widget.
217 Returns the RubberBandPolicy that is currently being used by the widget.
217 */
218 */
218 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
219 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
219 {
220 {
220 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
221 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
221 if(m_horizonalRubberBand) return HorizonalRubberBand;
222 if(m_horizonalRubberBand) return HorizonalRubberBand;
222 if(m_verticalRubberBand) return VerticalRubberBand;
223 if(m_verticalRubberBand) return VerticalRubberBand;
223 return NoRubberBand;
224 return NoRubberBand;
224 }
225 }
225
226
226 /*!
227 /*!
227 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.
228 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.
228 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
229 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
229 */
230 */
230 void QChartView::mousePressEvent(QMouseEvent *event)
231 void QChartView::mousePressEvent(QMouseEvent *event)
231 {
232 {
232 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
233 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
233
234
234 int padding = m_chart->padding();
235 int padding = m_chart->padding();
235 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
236 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
236
237
237 if (rect.contains(event->pos())) {
238 if (rect.contains(event->pos())) {
238 m_rubberBandOrigin = event->pos();
239 m_rubberBandOrigin = event->pos();
239 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
240 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
240 m_rubberBand->show();
241 m_rubberBand->show();
241 event->accept();
242 event->accept();
242 }
243 }
243 }
244 }
244 else {
245 else {
245 QGraphicsView::mousePressEvent(event);
246 QGraphicsView::mousePressEvent(event);
246 }
247 }
247 }
248 }
248
249
249 /*!
250 /*!
250 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
251 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
251 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
252 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
252 */
253 */
253 void QChartView::mouseMoveEvent(QMouseEvent *event)
254 void QChartView::mouseMoveEvent(QMouseEvent *event)
254 {
255 {
255 if(m_rubberBand && m_rubberBand->isVisible()) {
256 if(m_rubberBand && m_rubberBand->isVisible()) {
256 int padding = m_chart->padding();
257 int padding = m_chart->padding();
257 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
258 QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding);
258 int width = event->pos().x() - m_rubberBandOrigin.x();
259 int width = event->pos().x() - m_rubberBandOrigin.x();
259 int height = event->pos().y() - m_rubberBandOrigin.y();
260 int height = event->pos().y() - m_rubberBandOrigin.y();
260 if(!m_verticalRubberBand) {
261 if(!m_verticalRubberBand) {
261 m_rubberBandOrigin.setY(rect.top());
262 m_rubberBandOrigin.setY(rect.top());
262 height = rect.height();
263 height = rect.height();
263 }
264 }
264 if(!m_horizonalRubberBand) {
265 if(!m_horizonalRubberBand) {
265 m_rubberBandOrigin.setX(rect.left());
266 m_rubberBandOrigin.setX(rect.left());
266 width= rect.width();
267 width= rect.width();
267 }
268 }
268 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
269 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
269 }
270 }
270 else {
271 else {
271 QGraphicsView::mouseMoveEvent(event);
272 QGraphicsView::mouseMoveEvent(event);
272 }
273 }
273 }
274 }
274
275
275 /*!
276 /*!
276 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
277 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
277 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
278 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
278 */
279 */
279 void QChartView::mouseReleaseEvent(QMouseEvent *event)
280 void QChartView::mouseReleaseEvent(QMouseEvent *event)
280 {
281 {
281 if(m_rubberBand) {
282 if(m_rubberBand) {
282 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
283 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
283 m_rubberBand->hide();
284 m_rubberBand->hide();
284 QRect rect = m_rubberBand->geometry();
285 QRect rect = m_rubberBand->geometry();
285 m_chart->zoomIn(rect);
286 m_chart->zoomIn(rect);
286 event->accept();
287 event->accept();
287 }
288 }
288
289
289 if(event->button()==Qt::RightButton)
290 if(event->button()==Qt::RightButton)
290 m_chart->zoomReset();
291 m_chart->zoomReset();
291 }
292 }
292 else {
293 else {
293 QGraphicsView::mouseReleaseEvent(event);
294 QGraphicsView::mouseReleaseEvent(event);
294 }
295 }
295 }
296 }
296
297
297 /*!
298 /*!
298 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
299 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
299 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
300 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
300 */
301 */
301 void QChartView::keyPressEvent(QKeyEvent *event)
302 void QChartView::keyPressEvent(QKeyEvent *event)
302 {
303 {
303 switch (event->key()) {
304 switch (event->key()) {
304 case Qt::Key_Plus:
305 case Qt::Key_Plus:
305 zoomIn();
306 zoomIn();
306 break;
307 break;
307 case Qt::Key_Minus:
308 case Qt::Key_Minus:
308 zoomOut();
309 zoomOut();
309 break;
310 break;
310 default:
311 default:
311 QGraphicsView::keyPressEvent(event);
312 QGraphicsView::keyPressEvent(event);
312 break;
313 break;
313 }
314 }
314 }
315 }
315
316
316 /*!
317 /*!
317 Sets the \a theme used by the chart for rendering the graphical representation of the data
318 Sets the \a theme used by the chart for rendering the graphical representation of the data
318 \sa QChart::ChartTheme, chartTheme()
319 \sa QChart::ChartTheme, chartTheme()
319 */
320 */
320 void QChartView::setChartTheme(QChart::ChartTheme theme)
321 void QChartView::setChartTheme(QChart::ChartTheme theme)
321 {
322 {
322 m_chart->setChartTheme(theme);
323 m_chart->setChartTheme(theme);
323 }
324 }
324
325
325 /*!
326 /*!
326 Returns the theme enum used by the chart.
327 Returns the theme enum used by the chart.
327 \sa setChartTheme()
328 \sa setChartTheme()
328 */
329 */
329 QChart::ChartTheme QChartView::chartTheme() const
330 QChart::ChartTheme QChartView::chartTheme() const
330 {
331 {
331 return m_chart->chartTheme();
332 return m_chart->chartTheme();
332 }
333 }
333
334
334 /*!
335 /*!
335 Returns the pointer to the x axis object of the chart
336 Returns the pointer to the x axis object of the chart
336 */
337 */
337 QChartAxis* QChartView::axisX() const
338 QChartAxis* QChartView::axisX() const
338 {
339 {
339 return m_chart->axisX();
340 return m_chart->axisX();
340 }
341 }
341
342
342 /*!
343 /*!
343 Returns the pointer to the y axis object of the chart
344 Returns the pointer to the y axis object of the chart
344 */
345 */
345 QChartAxis* QChartView::axisY() const
346 QChartAxis* QChartView::axisY() const
346 {
347 {
347 return m_chart->axisY();
348 return m_chart->axisY();
348 }
349 }
349
350
350 /*!
351 /*!
351 Returns the pointer to legend object of the chart
352 Returns the pointer to legend object of the chart
352 */
353 */
353 QLegend* QChartView::legend() const
354 QLegend* QChartView::legend() const
354 {
355 {
355 return m_chart->legend();
356 return m_chart->legend();
356 }
357 }
357
358
358 /*!
359 /*!
359 Sets animation \a options for the chart
360 Sets animation \a options for the chart
360 */
361 */
361 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
362 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
362 {
363 {
363 m_chart->setAnimationOptions(options);
364 m_chart->setAnimationOptions(options);
364 }
365 }
365
366
366 /*!
367 /*!
367 Returns animation options for the chart
368 Returns animation options for the chart
368 */
369 */
369 QChart::AnimationOptions QChartView::animationOptions() const
370 QChart::AnimationOptions QChartView::animationOptions() const
370 {
371 {
371 return m_chart->animationOptions();
372 return m_chart->animationOptions();
372 }
373 }
373
374
374 void QChartView::scrollLeft()
375 void QChartView::scrollLeft()
375 {
376 {
376 m_chart->scrollLeft();
377 m_chart->scrollLeft();
377 }
378 }
378
379
379 void QChartView::scrollRight()
380 void QChartView::scrollRight()
380 {
381 {
381 m_chart->scrollRight();
382 m_chart->scrollRight();
382 }
383 }
383
384
384 void QChartView::scrollUp()
385 void QChartView::scrollUp()
385 {
386 {
386 m_chart->scrollUp();
387 m_chart->scrollUp();
387 }
388 }
388
389
389 void QChartView::scrollDown()
390 void QChartView::scrollDown()
390 {
391 {
391 m_chart->scrollDown();
392 m_chart->scrollDown();
392 }
393 }
393
394
394
395
395 #include "moc_qchartview.cpp"
396 #include "moc_qchartview.cpp"
396
397
397 QTCOMMERCIALCHART_END_NAMESPACE
398 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now