@@ -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 | 22 | #include <QMouseEvent> |
|
23 | 23 | |
|
24 | 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 |
|
|
|
31 | bool ChartView::viewportEvent(QEvent *event) | |
|
30 | 32 | { |
|
31 |
if (event-> |
|
|
32 | return; | |
|
33 | if (event->type() == QEvent::TouchBegin) { | |
|
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(); | |
|
35 | m_rubberBand.setGeometry(QRect(m_origin, QSize())); | |
|
36 | m_rubberBand.show(); | |
|
40 | // Turn off animations when handling gestures they | |
|
41 | // will only slow us down. | |
|
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 | 54 | void ChartView::mouseMoveEvent(QMouseEvent *event) |
|
42 | 55 | { |
|
43 | if (m_rubberBand.isVisible()) | |
|
44 | m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized()); | |
|
56 | if (m_isTouching) | |
|
57 | return; | |
|
58 | QChartView::mouseMoveEvent(event); | |
|
45 | 59 | } |
|
46 | 60 | |
|
47 | 61 | void ChartView::mouseReleaseEvent(QMouseEvent *event) |
|
48 | 62 | { |
|
49 | if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) { | |
|
50 | m_rubberBand.hide(); | |
|
63 | if (m_isTouching) | |
|
64 | m_isTouching = false; | |
|
51 | 65 | |
|
52 | QRect rect = m_rubberBand.geometry(); | |
|
53 | m_chart->zoomIn(rect); | |
|
54 | event->accept(); | |
|
55 | } | |
|
66 | // Because we disabled animations when touch event was detected | |
|
67 | // we must put them back on. | |
|
68 | chart()->setAnimationOptions(QChart::SeriesAnimations); | |
|
56 | 69 | |
|
57 | if (event->button() == Qt::RightButton) { | |
|
58 | m_chart->zoomOut(); | |
|
59 | } | |
|
70 | QChartView::mouseReleaseEvent(event); | |
|
60 | 71 | } |
|
61 | 72 | |
|
62 | 73 | //![1] |
@@ -64,23 +75,23 void ChartView::keyPressEvent(QKeyEvent *event) | |||
|
64 | 75 | { |
|
65 | 76 | switch (event->key()) { |
|
66 | 77 | case Qt::Key_Plus: |
|
67 |
|
|
|
78 | chart()->zoomIn(); | |
|
68 | 79 | break; |
|
69 | 80 | case Qt::Key_Minus: |
|
70 |
|
|
|
81 | chart()->zoomOut(); | |
|
71 | 82 | break; |
|
72 | 83 | //![1] |
|
73 | 84 | case Qt::Key_Left: |
|
74 |
|
|
|
85 | chart()->scrollLeft(); | |
|
75 | 86 | break; |
|
76 | 87 | case Qt::Key_Right: |
|
77 |
|
|
|
88 | chart()->scrollRight(); | |
|
78 | 89 | break; |
|
79 | 90 | case Qt::Key_Up: |
|
80 |
|
|
|
91 | chart()->scrollUp(); | |
|
81 | 92 | break; |
|
82 | 93 | case Qt::Key_Down: |
|
83 |
|
|
|
94 | chart()->scrollDown(); | |
|
84 | 95 | break; |
|
85 | 96 | default: |
|
86 | 97 | QGraphicsView::keyPressEvent(event); |
@@ -20,6 +20,7 | |||
|
20 | 20 | |
|
21 | 21 | #ifndef CHARTVIEW_H |
|
22 | 22 | #define CHARTVIEW_H |
|
23 | ||
|
23 | 24 | #include <QChartView> |
|
24 | 25 | #include <QRubberBand> |
|
25 | 26 | |
@@ -34,6 +35,7 public: | |||
|
34 | 35 | |
|
35 | 36 | //![2] |
|
36 | 37 | protected: |
|
38 | bool viewportEvent(QEvent *event); | |
|
37 | 39 | void mousePressEvent(QMouseEvent *event); |
|
38 | 40 | void mouseMoveEvent(QMouseEvent *event); |
|
39 | 41 | void mouseReleaseEvent(QMouseEvent *event); |
@@ -41,9 +43,7 protected: | |||
|
41 | 43 | //![2] |
|
42 | 44 | |
|
43 | 45 | private: |
|
44 | QRubberBand m_rubberBand; | |
|
45 | QPoint m_origin; | |
|
46 | QChart* m_chart; | |
|
46 | bool m_isTouching; | |
|
47 | 47 | }; |
|
48 | 48 | |
|
49 | 49 | #endif |
@@ -18,6 +18,7 | |||
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | #include "chart.h" | |
|
21 | 22 | #include "chartview.h" |
|
22 | 23 | #include <QApplication> |
|
23 | 24 | #include <QMainWindow> |
@@ -39,10 +40,10 int main(int argc, char *argv[]) | |||
|
39 | 40 | } |
|
40 | 41 | //![1] |
|
41 | 42 | |
|
42 |
|
|
|
43 | Chart* chart = new Chart(); | |
|
43 | 44 | chart->addSeries(series); |
|
44 | 45 | chart->setTitle("Zoom in/out example"); |
|
45 |
chart->setAnimationOptions(QChart:: |
|
|
46 | chart->setAnimationOptions(QChart::SeriesAnimations); | |
|
46 | 47 | |
|
47 | 48 | ChartView* chartView = new ChartView(chart); |
|
48 | 49 | chartView->setRenderHint(QPainter::Antialiasing); |
@@ -50,6 +51,8 int main(int argc, char *argv[]) | |||
|
50 | 51 | QMainWindow window; |
|
51 | 52 | window.setCentralWidget(chartView); |
|
52 | 53 | window.resize(400, 300); |
|
54 | window.grabGesture(Qt::PanGesture); | |
|
55 | window.grabGesture(Qt::PinchGesture); | |
|
53 | 56 | window.show(); |
|
54 | 57 | |
|
55 | 58 | return a.exec(); |
@@ -2,7 +2,8 | |||
|
2 | 2 | error( "Couldn't find the examples.pri file!" ) |
|
3 | 3 | } |
|
4 | 4 | TARGET = zoomlinechart |
|
5 | HEADERS += chartview.h | |
|
6 | SOURCES += main.cpp chartview.cpp | |
|
5 | HEADERS += chart.h chartview.h | |
|
6 | ||
|
7 | SOURCES += main.cpp chart.cpp chartview.cpp | |
|
7 | 8 | |
|
8 | 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 | 233 | QRectF rect = chartGeometry(); |
|
234 |
rect.setWidth(rect.width()/ |
|
|
235 |
rect.setHeight(rect.height()/ |
|
|
234 | rect.setWidth(rect.width()/factor); | |
|
235 | rect.setHeight(rect.height()/factor); | |
|
236 | 236 | rect.moveCenter(chartGeometry().center()); |
|
237 | 237 | zoomIn(rect); |
|
238 | 238 | } |
@@ -241,36 +241,39 void ChartPresenter::zoomIn(const QRectF& rect) | |||
|
241 | 241 | { |
|
242 | 242 | QRectF r = rect.normalized(); |
|
243 | 243 | r.translate(-m_chartMargins.topLeft()); |
|
244 |
if(!r.isValid()) |
|
|
245 | if(m_animator) { | |
|
244 | if (!r.isValid()) | |
|
245 | return; | |
|
246 | 246 | |
|
247 | if (m_animator) { | |
|
247 | 248 | QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); |
|
248 | 249 | m_animator->setState(ChartAnimator::ZoomInState,point); |
|
249 | 250 | } |
|
251 | ||
|
250 | 252 | m_dataset->zoomInDomain(r,chartGeometry().size()); |
|
251 | if(m_animator) { | |
|
253 | ||
|
254 | if (m_animator) | |
|
252 | 255 | m_animator->setState(ChartAnimator::ShowState); |
|
253 | 256 | } |
|
254 | } | |
|
255 | 257 | |
|
256 | void ChartPresenter::zoomOut() | |
|
258 | void ChartPresenter::zoomOut(qreal factor) | |
|
257 | 259 | { |
|
258 | 260 | if(m_animator) |
|
259 | { | |
|
260 | 261 | m_animator->setState(ChartAnimator::ZoomOutState); |
|
261 | } | |
|
262 | 262 | |
|
263 | QSizeF size = chartGeometry().size(); | |
|
264 |
|
|
|
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()); | |
|
263 | QRectF chartRect; | |
|
264 | chartRect.setSize(chartGeometry().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 | 275 | m_animator->setState(ChartAnimator::ShowState); |
|
272 | 276 | } |
|
273 | } | |
|
274 | 277 | |
|
275 | 278 | void ChartPresenter::scroll(int dx,int dy) |
|
276 | 279 | { |
@@ -66,9 +66,9 public: | |||
|
66 | 66 | void setAnimationOptions(QChart::AnimationOptions options); |
|
67 | 67 | QChart::AnimationOptions animationOptions() const; |
|
68 | 68 | |
|
69 | void zoomIn(); | |
|
69 | void zoomIn(qreal factor); | |
|
70 | 70 | void zoomIn(const QRectF& rect); |
|
71 | void zoomOut(); | |
|
71 | void zoomOut(qreal factor); | |
|
72 | 72 | void scroll(int dx,int dy); |
|
73 | 73 | |
|
74 | 74 | void setGeometry(const QRectF& rect); |
@@ -252,7 +252,7 QChart::ChartTheme QChart::theme() const | |||
|
252 | 252 | */ |
|
253 | 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 | 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 | 399 | Sets the chart background visibility state to \a visible |
|
370 | 400 | */ |
|
371 | 401 | void QChart::setBackgroundVisible(bool visible) |
@@ -94,10 +94,12 public: | |||
|
94 | 94 | void zoomIn(); |
|
95 | 95 | void zoomIn(const QRectF &rect); |
|
96 | 96 | void zoomOut(); |
|
97 | void zoom(qreal factor); | |
|
97 | 98 | void scrollLeft(); |
|
98 | 99 | void scrollRight(); |
|
99 | 100 | void scrollUp(); |
|
100 | 101 | void scrollDown(); |
|
102 | void scroll(const QPointF &delta); | |
|
101 | 103 | |
|
102 | 104 | QAxis* axisX() const; |
|
103 | 105 | QAxis* axisY(QAbstractSeries* series = 0) const; |
General Comments 0
You need to be logged in to leave comments.
Login now