|
- /****************************************************************************
- **
- ** Copyright (C) 2016 The Qt Company Ltd.
- ** Contact: https://www.qt.io/licensing/
- **
- ** This file is part of the Qt Charts module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:GPL$
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and The Qt Company. For licensing terms
- ** and conditions see https://www.qt.io/terms-conditions. For further
- ** information use the contact form at https://www.qt.io/contact-us.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3 or (at your option) any later version
- ** approved by the KDE Free Qt Foundation. The licenses are as published by
- ** the Free Software Foundation and appearing in the file LICENSE.GPL3
- ** included in the packaging of this file. Please review the following
- ** information to ensure the GNU General Public License requirements will
- ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- #ifndef CHART_H
- #define CHART_H
- #include <QtCharts/QChart>
- QT_BEGIN_NAMESPACE
- class QGestureEvent;
- QT_END_NAMESPACE
- QT_CHARTS_USE_NAMESPACE
- //![1]
- class Chart : public QChart
- //![1]
- {
- public:
- explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
- ~Chart();
- protected:
- bool sceneEvent(QEvent *event);
- private:
- bool gestureEvent(QGestureEvent *event);
- private:
- };
- #endif // CHART_H
- /****************************************************************************
- **
- ** Copyright (C) 2016 The Qt Company Ltd.
- ** Contact: https://www.qt.io/licensing/
- **
- ** This file is part of the Qt Charts module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:GPL$
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and The Qt Company. For licensing terms
- ** and conditions see https://www.qt.io/terms-conditions. For further
- ** information use the contact form at https://www.qt.io/contact-us.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3 or (at your option) any later version
- ** approved by the KDE Free Qt Foundation. The licenses are as published by
- ** the Free Software Foundation and appearing in the file LICENSE.GPL3
- ** included in the packaging of this file. Please review the following
- ** information to ensure the GNU General Public License requirements will
- ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- #ifndef CHARTVIEW_H
- #define CHARTVIEW_H
- #include <QtCharts/QChartView>
- #include <QtWidgets/QRubberBand>
- QT_CHARTS_USE_NAMESPACE
- //![1]
- class ChartView : public QChartView
- //![1]
- {
- public:
- ChartView(QChart *chart, QWidget *parent = 0);
- //![2]
- protected:
- bool viewportEvent(QEvent *event);
- void mousePressEvent(QMouseEvent *event);
- void mouseMoveEvent(QMouseEvent *event);
- void mouseReleaseEvent(QMouseEvent *event);
- void keyPressEvent(QKeyEvent *event);
- void wheelEvent(QWheelEvent*event);
- //![2]
- private:
- bool m_isTouching;
- };
- #endif
- /****************************************************************************
- **
- ** Copyright (C) 2016 The Qt Company Ltd.
- ** Contact: https://www.qt.io/licensing/
- **
- ** This file is part of the Qt Charts module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:GPL$
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and The Qt Company. For licensing terms
- ** and conditions see https://www.qt.io/terms-conditions. For further
- ** information use the contact form at https://www.qt.io/contact-us.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3 or (at your option) any later version
- ** approved by the KDE Free Qt Foundation. The licenses are as published by
- ** the Free Software Foundation and appearing in the file LICENSE.GPL3
- ** included in the packaging of this file. Please review the following
- ** information to ensure the GNU General Public License requirements will
- ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- #include "chart.h"
- #include <QtWidgets/QGesture>
- #include <QtWidgets/QGraphicsScene>
- #include <QtWidgets/QGraphicsView>
- Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
- : QChart(QChart::ChartTypeCartesian, parent, wFlags)
- {
- // Seems that QGraphicsView (QChartView) does not grab gestures.
- // They can only be grabbed here in the QGraphicsWidget (QChart).
- grabGesture(Qt::PanGesture);
- grabGesture(Qt::PinchGesture);
- }
- Chart::~Chart()
- {
- }
- //![1]
- bool Chart::sceneEvent(QEvent *event)
- {
- if (event->type() == QEvent::Gesture)
- return gestureEvent(static_cast<QGestureEvent *>(event));
- return QChart::event(event);
- }
- bool Chart::gestureEvent(QGestureEvent *event)
- {
- if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
- QPanGesture *pan = static_cast<QPanGesture *>(gesture);
- QChart::scroll(-(pan->delta().x()), pan->delta().y());
- }
- if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
- QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
- if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
- QChart::zoom(pinch->scaleFactor());
- }
- return true;
- }
- //![1]
- /****************************************************************************
- **
- ** Copyright (C) 2016 The Qt Company Ltd.
- ** Contact: https://www.qt.io/licensing/
- **
- ** This file is part of the Qt Charts module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:GPL$
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and The Qt Company. For licensing terms
- ** and conditions see https://www.qt.io/terms-conditions. For further
- ** information use the contact form at https://www.qt.io/contact-us.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3 or (at your option) any later version
- ** approved by the KDE Free Qt Foundation. The licenses are as published by
- ** the Free Software Foundation and appearing in the file LICENSE.GPL3
- ** included in the packaging of this file. Please review the following
- ** information to ensure the GNU General Public License requirements will
- ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- #include "chartview.h"
- #include <QtGui/QMouseEvent>
- ChartView::ChartView(QChart *chart, QWidget *parent) :
- QChartView(chart, parent),
- m_isTouching(false)
- {
- setRubberBand(QChartView::RectangleRubberBand);
- }
- bool ChartView::viewportEvent(QEvent *event)
- {
- if (event->type() == QEvent::TouchBegin) {
- // By default touch events are converted to mouse events. So
- // after this event we will get a mouse event also but we want
- // to handle touch events as gestures only. So we need this safeguard
- // to block mouse events that are actually generated from touch.
- m_isTouching = true;
- // Turn off animations when handling gestures they
- // will only slow us down.
- chart()->setAnimationOptions(QChart::NoAnimation);
- }
- return QChartView::viewportEvent(event);
- }
- void ChartView::mousePressEvent(QMouseEvent *event)
- {
- if (m_isTouching)
- return;
- QChartView::mousePressEvent(event);
- }
- void ChartView::mouseMoveEvent(QMouseEvent *event)
- {
- if (m_isTouching)
- return;
- QChartView::mouseMoveEvent(event);
- }
- void ChartView::mouseReleaseEvent(QMouseEvent *event)
- {
- if (m_isTouching)
- m_isTouching = false;
- // Because we disabled animations when touch event was detected
- // we must put them back on.
- chart()->setAnimationOptions(QChart::SeriesAnimations);
- QChartView::mouseReleaseEvent(event);
- }
- //![1]
- void ChartView::keyPressEvent(QKeyEvent *event)
- {
- switch (event->key()) {
- case Qt::Key_Plus:
- chart()->zoomIn();
- break;
- case Qt::Key_Minus:
- chart()->zoomOut();
- break;
- //![1]
- case Qt::Key_Left:
- chart()->scroll(-10, 0);
- break;
- case Qt::Key_Right:
- chart()->scroll(10, 0);
- break;
- case Qt::Key_Up:
- chart()->scroll(0, 10);
- break;
- case Qt::Key_Down:
- chart()->scroll(0, -10);
- break;
- default:
- QGraphicsView::keyPressEvent(event);
- break;
- }
- }
- void ChartView::wheelEvent(QWheelEvent*event)
- {
- qreal factor;//缩放因子
- if ( event->delta() > 0 )
- factor = 2.0;
- else
- factor = 0.5;
- //橡皮筋为水平方向时才缩放
- if(event->delta()>0)
- {
- //鼠标的当前位置
- chart()->zoomIn();
- }
- else
- {
- chart()->zoomOut();
- }
- QChartView::wheelEvent(event);
- }
- /****************************************************************************
- **
- ** Copyright (C) 2016 The Qt Company Ltd.
- ** Contact: https://www.qt.io/licensing/
- **
- ** This file is part of the Qt Charts module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:GPL$
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and The Qt Company. For licensing terms
- ** and conditions see https://www.qt.io/terms-conditions. For further
- ** information use the contact form at https://www.qt.io/contact-us.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3 or (at your option) any later version
- ** approved by the KDE Free Qt Foundation. The licenses are as published by
- ** the Free Software Foundation and appearing in the file LICENSE.GPL3
- ** included in the packaging of this file. Please review the following
- ** information to ensure the GNU General Public License requirements will
- ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- #include "chart.h"
- #include "chartview.h"
- #include <QtWidgets/QApplication>
- #include <QtWidgets/QMainWindow>
- #include <QtCore/QtMath>
- #include <QtCore/QRandomGenerator>
- #include <QtCharts/QLineSeries>
- #include <QtCharts/QValueAxis>
- QT_CHARTS_USE_NAMESPACE
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- //![1]
- QLineSeries *series = new QLineSeries();
- for (int i = 0; i < 500; i++) {
- QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100);
- p.ry() += QRandomGenerator::global()->bounded(20);
- *series << p;
- }
- //![1]
- Chart *chart = new Chart();
- chart->addSeries(series);
- chart->setTitle("Zoom in/out example");
- chart->setAnimationOptions(QChart::SeriesAnimations);
- chart->legend()->hide();
- chart->createDefaultAxes();
- ChartView *chartView = new ChartView(chart);
- chartView->setRenderHint(QPainter::Antialiasing);
- QMainWindow window;
- window.setCentralWidget(chartView);
- window.resize(400, 300);
- window.grabGesture(Qt::PanGesture);
- window.grabGesture(Qt::PinchGesture);
- window.show();
- return a.exec();
- }
复制代码
|
|