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