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