##// END OF EJS Templates
Tuning zoom example (key zooming was removed from QChartView)
Tero Ahola -
r966:69613711a0c3
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
NO CONTENT: new file 100644, binary diff hidden
@@ -1,7 +1,22
1 /*!
1 /*!
2 \example examples/zoomlinechart
2 \example examples/zoomlinechart
3 \title Zoom line example
3 \title Zoom line example
4 \subtitle
4 \subtitle
5
5
6 The example shows how to create your own custom zooming effect with QRubberBand.
6 The example shows how to create your own custom zooming effect with QRubberBand.
7 \image example_zoomlinechart1.png
8 \image example_zoomlinechart2.png
9
10 Let's first create a line series with some example data.
11 \snippet ../examples/zoomlinechart/main.cpp 1
12
13 Then we create a custom chart view by deriving from QChartView:
14 \snippet ../examples/zoomlinechart/chartview.h 1
15
16 We override mouse and key event handling
17 \snippet ../examples/zoomlinechart/chartview.h 2
18
19 Then we implement a custom logic for mouse and key events. For example pressing '+' key will zoom in and pressing
20 the '-' key will zoom out.
21 \snippet ../examples/zoomlinechart/chartview.cpp 1
7 */
22 */
@@ -1,87 +1,89
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartview.h"
21 #include "chartview.h"
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), m_rubberBand(QRubberBand::Rectangle, this), m_chart(chart)
26 {
26 {
27 }
27 }
28
28
29 void ChartView::mousePressEvent(QMouseEvent *event)
29 void ChartView::mousePressEvent(QMouseEvent *event)
30 {
30 {
31 if (event->button() != Qt::LeftButton)
31 if (event->button() != Qt::LeftButton)
32 return;
32 return;
33
33
34 m_origin = event->pos();
34 m_origin = event->pos();
35 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
35 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
36 m_rubberBand.show();
36 m_rubberBand.show();
37
37
38 event->accept();
38 event->accept();
39 }
39 }
40
40
41 void ChartView::mouseMoveEvent(QMouseEvent *event)
41 void ChartView::mouseMoveEvent(QMouseEvent *event)
42 {
42 {
43 if (m_rubberBand.isVisible())
43 if (m_rubberBand.isVisible())
44 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
44 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
45 }
45 }
46
46
47 void ChartView::mouseReleaseEvent(QMouseEvent *event)
47 void ChartView::mouseReleaseEvent(QMouseEvent *event)
48 {
48 {
49 if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) {
49 if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) {
50 m_rubberBand.hide();
50 m_rubberBand.hide();
51
51
52 QRect rect = m_rubberBand.geometry();
52 QRect rect = m_rubberBand.geometry();
53 m_chart->zoomIn(rect);
53 m_chart->zoomIn(rect);
54 event->accept();
54 event->accept();
55 }
55 }
56
56
57 if (event->button() == Qt::RightButton) {
57 if (event->button() == Qt::RightButton) {
58 m_chart->zoomOut();
58 m_chart->zoomOut();
59 }
59 }
60 }
60 }
61
61
62 //![1]
62 void ChartView::keyPressEvent(QKeyEvent *event)
63 void ChartView::keyPressEvent(QKeyEvent *event)
63 {
64 {
64 switch (event->key()) {
65 switch (event->key()) {
65 case Qt::Key_Plus:
66 case Qt::Key_Plus:
66 m_chart->zoomIn();
67 m_chart->zoomIn();
67 break;
68 break;
68 case Qt::Key_Minus:
69 case Qt::Key_Minus:
69 m_chart->zoomOut();
70 m_chart->zoomOut();
70 break;
71 break;
72 //![1]
71 case Qt::Key_Left:
73 case Qt::Key_Left:
72 m_chart->scrollLeft();
74 m_chart->scrollLeft();
73 break;
75 break;
74 case Qt::Key_Right:
76 case Qt::Key_Right:
75 m_chart->scrollRight();
77 m_chart->scrollRight();
76 break;
78 break;
77 case Qt::Key_Up:
79 case Qt::Key_Up:
78 m_chart->scrollUp();
80 m_chart->scrollUp();
79 break;
81 break;
80 case Qt::Key_Down:
82 case Qt::Key_Down:
81 m_chart->scrollDown();
83 m_chart->scrollDown();
82 break;
84 break;
83 default:
85 default:
84 QGraphicsView::keyPressEvent(event);
86 QGraphicsView::keyPressEvent(event);
85 break;
87 break;
86 }
88 }
87 }
89 }
@@ -1,47 +1,49
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef CHARTVIEW_H
21 #ifndef CHARTVIEW_H
22 #define CHARTVIEW_H
22 #define CHARTVIEW_H
23 #include <QChartView>
23 #include <QChartView>
24 #include <QRubberBand>
24 #include <QRubberBand>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 //![1]
28 class ChartView : public QChartView
29 class ChartView : public QChartView
30 //![1]
29 {
31 {
30 public:
32 public:
31 ChartView(QChart *chart, QWidget *parent = 0);
33 ChartView(QChart *chart, QWidget *parent = 0);
32
34
35 //![2]
33 protected:
36 protected:
34 void mousePressEvent(QMouseEvent *event);
37 void mousePressEvent(QMouseEvent *event);
35 void mouseMoveEvent(QMouseEvent *event);
38 void mouseMoveEvent(QMouseEvent *event);
36 void mouseReleaseEvent(QMouseEvent *event);
39 void mouseReleaseEvent(QMouseEvent *event);
37 void keyPressEvent(QKeyEvent *event);
40 void keyPressEvent(QKeyEvent *event);
41 //![2]
38
42
39 private:
43 private:
40 bool rubberBandIsShown;
41 QRubberBand m_rubberBand;
44 QRubberBand m_rubberBand;
42 QPoint m_origin;
45 QPoint m_origin;
43 QChart* m_chart;
46 QChart* m_chart;
44
45 };
47 };
46
48
47 #endif
49 #endif
@@ -1,71 +1,56
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartview.h"
21 #include "chartview.h"
22 #include <QApplication>
22 #include <QApplication>
23 #include <QMainWindow>
23 #include <QMainWindow>
24 #include <QLineSeries>
24 #include <QLineSeries>
25
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
27
28 int main(int argc, char *argv[])
28 int main(int argc, char *argv[])
29 {
29 {
30 QApplication a(argc, argv);
30 QApplication a(argc, argv);
31
31
32 //![1]
32 //![1]
33 QLineSeries* series0 = new QLineSeries();
33 QLineSeries* series = new QLineSeries();
34 QPen blue(Qt::blue);
34 qreal yValue = 0.0;
35 blue.setWidth(3);
35 for (int i(0); i < 500; i++) {
36 series0->setPen(blue);
36 yValue = yValue + (qreal) (qrand() % 10) / 500.0;
37
37 QPointF value((i + (qreal) rand() / (qreal) RAND_MAX) * (10.0 / 500.0), yValue);
38 QLineSeries* series1 = new QLineSeries();
38 *series << value;
39 QPen red(Qt::red);
39 }
40 red.setWidth(3);
41 series1->setPen(red);
42 //![1]
40 //![1]
43
41
44 //![2]
45 *series0 << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10, 5);
46 *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2);
47 //![2]
48
49 //![3]
50 QChart* chart = new QChart();
42 QChart* chart = new QChart();
51
43 chart->addSeries(series);
52 chart->addSeries(series0);
44 chart->setTitle("Zoom in/out example");
53 chart->addSeries(series1);
54 chart->setTitle("Zoom in/out chart example");
55 chart->setAnimationOptions(QChart::AllAnimations);
45 chart->setAnimationOptions(QChart::AllAnimations);
56 //![3]
57
46
58 //![4]
47 ChartView* chartView = new ChartView(chart);
59 ChartView* chartView = new ChartView(chart);
48 chartView->setRenderHint(QPainter::Antialiasing);
60 chartView->setRenderHint(QPainter::Antialiasing);
61 //![4]
62
49
63 //![5]
50 QMainWindow window;
64 QMainWindow window;
51 window.setCentralWidget(chartView);
65 window.setCentralWidget(chartView);
52 window.resize(400, 300);
66 window.resize(400, 300);
53 window.show();
67 window.show();
68 //![5]
69
54
70 return a.exec();
55 return a.exec();
71 }
56 }
General Comments 0
You need to be logged in to leave comments. Login now