| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include "chartview.h" |
| 31 | #include <QtGui/QMouseEvent> |
| 32 | |
| 33 | ChartView::ChartView(QChart *chart, QWidget *parent) : |
| 34 | QChartView(chart, parent), |
| 35 | m_isTouching(false) |
| 36 | { |
| 37 | setRubberBand(QChartView::RectangleRubberBand); |
| 38 | } |
| 39 | |
| 40 | bool ChartView::viewportEvent(QEvent *event) |
| 41 | { |
| 42 | if (event->type() == QEvent::TouchBegin) { |
| 43 | // By default touch events are converted to mouse events. So |
| 44 | // after this event we will get a mouse event also but we want |
| 45 | // to handle touch events as gestures only. So we need this safeguard |
| 46 | // to block mouse events that are actually generated from touch. |
| 47 | m_isTouching = true; |
| 48 | |
| 49 | // Turn off animations when handling gestures they |
| 50 | // will only slow us down. |
| 51 | chart()->setAnimationOptions(QChart::NoAnimation); |
| 52 | } |
| 53 | return QChartView::viewportEvent(event); |
| 54 | } |
| 55 | |
| 56 | void ChartView::mousePressEvent(QMouseEvent *event) |
| 57 | { |
| 58 | if (m_isTouching) |
| 59 | return; |
| 60 | QChartView::mousePressEvent(event); |
| 61 | } |
| 62 | |
| 63 | void ChartView::mouseMoveEvent(QMouseEvent *event) |
| 64 | { |
| 65 | if (m_isTouching) |
| 66 | return; |
| 67 | QChartView::mouseMoveEvent(event); |
| 68 | } |
| 69 | |
| 70 | void ChartView::mouseReleaseEvent(QMouseEvent *event) |
| 71 | { |
| 72 | if (m_isTouching) |
| 73 | m_isTouching = false; |
| 74 | |
| 75 | // Because we disabled animations when touch event was detected |
| 76 | // we must put them back on. |
| 77 | chart()->setAnimationOptions(QChart::SeriesAnimations); |
| 78 | |
| 79 | QChartView::mouseReleaseEvent(event); |
| 80 | } |
| 81 | |
| 82 | //![1] |
| 83 | void ChartView::keyPressEvent(QKeyEvent *event) |
| 84 | { |
| 85 | switch (event->key()) { |
| 86 | case Qt::Key_Plus: |
| 87 | chart()->zoomIn(); |
| 88 | break; |
| 89 | case Qt::Key_Minus: |
| 90 | chart()->zoomOut(); |
| 91 | break; |
| 92 | //![1] |
| 93 | case Qt::Key_Left: |
| 94 | chart()->scroll(dx: -10, dy: 0); |
| 95 | break; |
| 96 | case Qt::Key_Right: |
| 97 | chart()->scroll(dx: 10, dy: 0); |
| 98 | break; |
| 99 | case Qt::Key_Up: |
| 100 | chart()->scroll(dx: 0, dy: 10); |
| 101 | break; |
| 102 | case Qt::Key_Down: |
| 103 | chart()->scroll(dx: 0, dy: -10); |
| 104 | break; |
| 105 | default: |
| 106 | QGraphicsView::keyPressEvent(event); |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |