##// END OF EJS Templates
Add gestures support for zoomlinechart example...
Jani Honkonen -
r1187:5f42a207d0d0
parent child
Show More
@@ -0,0 +1,61
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "chart.h"
22 #include <QGesture>
23 #include <QGraphicsScene>
24 #include <QGraphicsView>
25
26 Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 :QChart(parent, wFlags)
28 {
29 // Seems that QGraphicsView (QChartView) does not grab gestures.
30 // They can only be grabbed here in the QGraphicsWidget (QChart).
31 grabGesture(Qt::PanGesture);
32 grabGesture(Qt::PinchGesture);
33 }
34
35 Chart::~Chart()
36 {
37
38 }
39
40 bool Chart::sceneEvent(QEvent *event)
41 {
42 if (event->type() == QEvent::Gesture)
43 return gestureEvent(static_cast<QGestureEvent*>(event));
44 return QChart::event(event);
45 }
46
47 bool Chart::gestureEvent(QGestureEvent* event)
48 {
49 if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
50 QPanGesture *pan = static_cast<QPanGesture *>(gesture);
51 scroll(pan->delta());
52 }
53
54 if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
55 QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
56 if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
57 zoom(pinch->scaleFactor());
58 }
59
60 return true;
61 }
@@ -0,0 +1,44
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef CHART_H
22 #define CHART_H
23
24 #include <QChart>
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
28 class Chart : public QChart
29 {
30 public:
31 explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
32 ~Chart();
33
34 protected:
35 bool sceneEvent(QEvent *event);
36
37 private:
38 bool gestureEvent(QGestureEvent* event);
39
40 private:
41
42 };
43
44 #endif // CHART_H
@@ -22,41 +22,52
22 #include <QMouseEvent>
22 #include <QMouseEvent>
23
23
24 ChartView::ChartView(QChart *chart, QWidget *parent) :
24 ChartView::ChartView(QChart *chart, QWidget *parent) :
25 QChartView(chart, parent), m_rubberBand(QRubberBand::Rectangle, this), m_chart(chart)
25 QChartView(chart, parent),
26 m_isTouching(false)
26 {
27 {
28 setRubberBand(QChartView::RectangleRubberBand);
27 }
29 }
28
30
29 void ChartView::mousePressEvent(QMouseEvent *event)
31 bool ChartView::viewportEvent(QEvent *event)
30 {
32 {
31 if (event->button() != Qt::LeftButton)
33 if (event->type() == QEvent::TouchBegin) {
32 return;
34 // By default touch events are converted to mouse events. So
35 // after this event we will get a mouse event also but we want
36 // to handle touch events as gestures only. So we need this safeguard
37 // to block mouse events that are actually generated from touch.
38 m_isTouching = true;
33
39
34 m_origin = event->pos();
40 // Turn off animations when handling gestures they
35 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
41 // will only slow us down.
36 m_rubberBand.show();
42 chart()->setAnimationOptions(QChart::NoAnimation);
43 }
44 return QChartView::viewportEvent(event);
45 }
37
46
38 event->accept();
47 void ChartView::mousePressEvent(QMouseEvent *event)
48 {
49 if (m_isTouching)
50 return;
51 QChartView::mousePressEvent(event);
39 }
52 }
40
53
41 void ChartView::mouseMoveEvent(QMouseEvent *event)
54 void ChartView::mouseMoveEvent(QMouseEvent *event)
42 {
55 {
43 if (m_rubberBand.isVisible())
56 if (m_isTouching)
44 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
57 return;
58 QChartView::mouseMoveEvent(event);
45 }
59 }
46
60
47 void ChartView::mouseReleaseEvent(QMouseEvent *event)
61 void ChartView::mouseReleaseEvent(QMouseEvent *event)
48 {
62 {
49 if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) {
63 if (m_isTouching)
50 m_rubberBand.hide();
64 m_isTouching = false;
51
65
52 QRect rect = m_rubberBand.geometry();
66 // Because we disabled animations when touch event was detected
53 m_chart->zoomIn(rect);
67 // we must put them back on.
54 event->accept();
68 chart()->setAnimationOptions(QChart::SeriesAnimations);
55 }
56
69
57 if (event->button() == Qt::RightButton) {
70 QChartView::mouseReleaseEvent(event);
58 m_chart->zoomOut();
59 }
60 }
71 }
61
72
62 //![1]
73 //![1]
@@ -64,23 +75,23 void ChartView::keyPressEvent(QKeyEvent *event)
64 {
75 {
65 switch (event->key()) {
76 switch (event->key()) {
66 case Qt::Key_Plus:
77 case Qt::Key_Plus:
67 m_chart->zoomIn();
78 chart()->zoomIn();
68 break;
79 break;
69 case Qt::Key_Minus:
80 case Qt::Key_Minus:
70 m_chart->zoomOut();
81 chart()->zoomOut();
71 break;
82 break;
72 //![1]
83 //![1]
73 case Qt::Key_Left:
84 case Qt::Key_Left:
74 m_chart->scrollLeft();
85 chart()->scrollLeft();
75 break;
86 break;
76 case Qt::Key_Right:
87 case Qt::Key_Right:
77 m_chart->scrollRight();
88 chart()->scrollRight();
78 break;
89 break;
79 case Qt::Key_Up:
90 case Qt::Key_Up:
80 m_chart->scrollUp();
91 chart()->scrollUp();
81 break;
92 break;
82 case Qt::Key_Down:
93 case Qt::Key_Down:
83 m_chart->scrollDown();
94 chart()->scrollDown();
84 break;
95 break;
85 default:
96 default:
86 QGraphicsView::keyPressEvent(event);
97 QGraphicsView::keyPressEvent(event);
@@ -20,6 +20,7
20
20
21 #ifndef CHARTVIEW_H
21 #ifndef CHARTVIEW_H
22 #define CHARTVIEW_H
22 #define CHARTVIEW_H
23
23 #include <QChartView>
24 #include <QChartView>
24 #include <QRubberBand>
25 #include <QRubberBand>
25
26
@@ -34,6 +35,7 public:
34
35
35 //![2]
36 //![2]
36 protected:
37 protected:
38 bool viewportEvent(QEvent *event);
37 void mousePressEvent(QMouseEvent *event);
39 void mousePressEvent(QMouseEvent *event);
38 void mouseMoveEvent(QMouseEvent *event);
40 void mouseMoveEvent(QMouseEvent *event);
39 void mouseReleaseEvent(QMouseEvent *event);
41 void mouseReleaseEvent(QMouseEvent *event);
@@ -41,9 +43,7 protected:
41 //![2]
43 //![2]
42
44
43 private:
45 private:
44 QRubberBand m_rubberBand;
46 bool m_isTouching;
45 QPoint m_origin;
46 QChart* m_chart;
47 };
47 };
48
48
49 #endif
49 #endif
@@ -18,6 +18,7
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chart.h"
21 #include "chartview.h"
22 #include "chartview.h"
22 #include <QApplication>
23 #include <QApplication>
23 #include <QMainWindow>
24 #include <QMainWindow>
@@ -39,10 +40,10 int main(int argc, char *argv[])
39 }
40 }
40 //![1]
41 //![1]
41
42
42 QChart* chart = new QChart();
43 Chart* chart = new Chart();
43 chart->addSeries(series);
44 chart->addSeries(series);
44 chart->setTitle("Zoom in/out example");
45 chart->setTitle("Zoom in/out example");
45 chart->setAnimationOptions(QChart::AllAnimations);
46 chart->setAnimationOptions(QChart::SeriesAnimations);
46
47
47 ChartView* chartView = new ChartView(chart);
48 ChartView* chartView = new ChartView(chart);
48 chartView->setRenderHint(QPainter::Antialiasing);
49 chartView->setRenderHint(QPainter::Antialiasing);
@@ -50,6 +51,8 int main(int argc, char *argv[])
50 QMainWindow window;
51 QMainWindow window;
51 window.setCentralWidget(chartView);
52 window.setCentralWidget(chartView);
52 window.resize(400, 300);
53 window.resize(400, 300);
54 window.grabGesture(Qt::PanGesture);
55 window.grabGesture(Qt::PinchGesture);
53 window.show();
56 window.show();
54
57
55 return a.exec();
58 return a.exec();
@@ -2,7 +2,8
2 error( "Couldn't find the examples.pri file!" )
2 error( "Couldn't find the examples.pri file!" )
3 }
3 }
4 TARGET = zoomlinechart
4 TARGET = zoomlinechart
5 HEADERS += chartview.h
5 HEADERS += chart.h chartview.h
6 SOURCES += main.cpp chartview.cpp
6
7 SOURCES += main.cpp chart.cpp chartview.cpp
7
8
8 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
9 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
@@ -228,11 +228,11 void ChartPresenter::resetAllElements()
228 }
228 }
229 }
229 }
230
230
231 void ChartPresenter::zoomIn()
231 void ChartPresenter::zoomIn(qreal factor)
232 {
232 {
233 QRectF rect = chartGeometry();
233 QRectF rect = chartGeometry();
234 rect.setWidth(rect.width()/2);
234 rect.setWidth(rect.width()/factor);
235 rect.setHeight(rect.height()/2);
235 rect.setHeight(rect.height()/factor);
236 rect.moveCenter(chartGeometry().center());
236 rect.moveCenter(chartGeometry().center());
237 zoomIn(rect);
237 zoomIn(rect);
238 }
238 }
@@ -241,35 +241,38 void ChartPresenter::zoomIn(const QRectF& rect)
241 {
241 {
242 QRectF r = rect.normalized();
242 QRectF r = rect.normalized();
243 r.translate(-m_chartMargins.topLeft());
243 r.translate(-m_chartMargins.topLeft());
244 if(!r.isValid()) return;
244 if (!r.isValid())
245 if(m_animator) {
245 return;
246
246
247 if (m_animator) {
247 QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
248 QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
248 m_animator->setState(ChartAnimator::ZoomInState,point);
249 m_animator->setState(ChartAnimator::ZoomInState,point);
249 }
250 }
251
250 m_dataset->zoomInDomain(r,chartGeometry().size());
252 m_dataset->zoomInDomain(r,chartGeometry().size());
251 if(m_animator) {
253
254 if (m_animator)
252 m_animator->setState(ChartAnimator::ShowState);
255 m_animator->setState(ChartAnimator::ShowState);
253 }
254 }
256 }
255
257
256 void ChartPresenter::zoomOut()
258 void ChartPresenter::zoomOut(qreal factor)
257 {
259 {
258 if(m_animator)
260 if (m_animator)
259 {
260 m_animator->setState(ChartAnimator::ZoomOutState);
261 m_animator->setState(ChartAnimator::ZoomOutState);
261 }
262
262
263 QSizeF size = chartGeometry().size();
263 QRectF chartRect;
264 QRectF rect = chartGeometry();
264 chartRect.setSize(chartGeometry().size());
265 rect.translate(-m_chartMargins.topLeft());
266 if(!rect.isValid()) return;
267 m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size);
268 //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
269
265
270 if(m_animator){
266 QRectF rect;
267 rect.setSize(chartRect.size()/factor);
268 rect.moveCenter(chartRect.center());
269 if (!rect.isValid())
270 return;
271
272 m_dataset->zoomOutDomain(rect, chartRect.size());
273
274 if (m_animator)
271 m_animator->setState(ChartAnimator::ShowState);
275 m_animator->setState(ChartAnimator::ShowState);
272 }
273 }
276 }
274
277
275 void ChartPresenter::scroll(int dx,int dy)
278 void ChartPresenter::scroll(int dx,int dy)
@@ -66,9 +66,9 public:
66 void setAnimationOptions(QChart::AnimationOptions options);
66 void setAnimationOptions(QChart::AnimationOptions options);
67 QChart::AnimationOptions animationOptions() const;
67 QChart::AnimationOptions animationOptions() const;
68
68
69 void zoomIn();
69 void zoomIn(qreal factor);
70 void zoomIn(const QRectF& rect);
70 void zoomIn(const QRectF& rect);
71 void zoomOut();
71 void zoomOut(qreal factor);
72 void scroll(int dx,int dy);
72 void scroll(int dx,int dy);
73
73
74 void setGeometry(const QRectF& rect);
74 void setGeometry(const QRectF& rect);
@@ -252,7 +252,7 QChart::ChartTheme QChart::theme() const
252 */
252 */
253 void QChart::zoomIn()
253 void QChart::zoomIn()
254 {
254 {
255 d_ptr->m_presenter->zoomIn();
255 d_ptr->m_presenter->zoomIn(2.0);
256 }
256 }
257
257
258 /*!
258 /*!
@@ -269,7 +269,29 void QChart::zoomIn(const QRectF& rect)
269 */
269 */
270 void QChart::zoomOut()
270 void QChart::zoomOut()
271 {
271 {
272 d_ptr->m_presenter->zoomOut();
272 d_ptr->m_presenter->zoomOut(2.0);
273 }
274
275 /*!
276 Zooms in the view by a \a factor.
277
278 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
279 */
280 void QChart::zoom(qreal factor)
281 {
282 if (qFuzzyIsNull(factor))
283 return;
284
285 if (qFuzzyCompare(factor, 1.0))
286 return;
287
288 if (factor < 0)
289 return;
290
291 if (factor > 1.0)
292 d_ptr->m_presenter->zoomIn(factor);
293 else
294 d_ptr->m_presenter->zoomOut(1.0 / factor);
273 }
295 }
274
296
275 /*!
297 /*!
@@ -366,6 +388,14 void QChart::scrollDown()
366 }
388 }
367
389
368 /*!
390 /*!
391 Scrolls the visible area of the chart by the distance defined in the \a delta.
392 */
393 void QChart::scroll(const QPointF &delta)
394 {
395 d_ptr->m_presenter->scroll(-delta.x(), delta.y());
396 }
397
398 /*!
369 Sets the chart background visibility state to \a visible
399 Sets the chart background visibility state to \a visible
370 */
400 */
371 void QChart::setBackgroundVisible(bool visible)
401 void QChart::setBackgroundVisible(bool visible)
@@ -94,10 +94,12 public:
94 void zoomIn();
94 void zoomIn();
95 void zoomIn(const QRectF &rect);
95 void zoomIn(const QRectF &rect);
96 void zoomOut();
96 void zoomOut();
97 void zoom(qreal factor);
97 void scrollLeft();
98 void scrollLeft();
98 void scrollRight();
99 void scrollRight();
99 void scrollUp();
100 void scrollUp();
100 void scrollDown();
101 void scrollDown();
102 void scroll(const QPointF &delta);
101
103
102 QAxis* axisX() const;
104 QAxis* axisX() const;
103 QAxis* axisY(QAbstractSeries* series = 0) const;
105 QAxis* axisY(QAbstractSeries* series = 0) const;
General Comments 0
You need to be logged in to leave comments. Login now