##// END OF EJS Templates
Adds rubberband for zooming...
Michal Klocek -
r58:1c099f3b0fd1
parent child
Show More
@@ -0,0 +1,81
1 #include "qchartview.h"
2 #include "qchart.h"
3 #include <QGraphicsView>
4 #include <QGraphicsScene>
5 #include <QRubberBand>
6 #include <QResizeEvent>
7 #include <QDebug>
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11 QChartView::QChartView(QWidget *parent) :
12 QGraphicsView(parent),
13 m_scene(new QGraphicsScene()),
14 m_chart(new QChart()),
15 m_rubberBand(0),
16 m_showRubberBand(false)
17 {
18 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
19 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
20 setScene(m_scene);
21 m_chart->setMargin(50);
22 m_scene->addItem(m_chart);
23 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
24 }
25
26 QChartView::~QChartView()
27 {
28 }
29
30 void QChartView::resizeEvent(QResizeEvent *event)
31 {
32 m_scene->setSceneRect(0,0,size().width(),size().height());
33 m_chart->setSize(size());
34 QWidget::resizeEvent(event);
35 }
36
37
38 void QChartView::addSeries(QChartSeries* series)
39 {
40 m_chart->addSeries(series);
41 }
42
43 QChartSeries* QChartView::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
44 {
45
46 return m_chart->createSeries(x, y, type);
47 }
48
49 void QChartView::mousePressEvent(QMouseEvent *event)
50 {
51 int margin = m_chart->margin();
52
53 QRect rect(margin,margin,width()-2*margin,height()-2*margin);
54
55 m_origin = event->pos();
56
57 if (!rect.contains(m_origin)) return;
58
59 if (!m_rubberBand)
60 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
61 m_rubberBand->setGeometry(QRect(m_origin, QSize()));
62 m_showRubberBand=true;
63 m_rubberBand->show();
64
65 }
66
67 void QChartView::mouseMoveEvent(QMouseEvent *event)
68 {
69 if(m_showRubberBand)
70 m_rubberBand->setGeometry(QRect(m_origin, event->pos()).normalized());
71 }
72
73 void QChartView::mouseReleaseEvent(QMouseEvent *event)
74 {
75 if(m_showRubberBand) {
76 m_rubberBand->hide();
77 m_showRubberBand=false;
78 }
79 }
80
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,46
1 #ifndef QCHARTWIDGET_H
2 #define QCHARTWIDGET_H
3
4 #include "qchartglobal.h"
5 #include "qchartseries.h"
6 #include <QGraphicsView>
7
8 class QGraphicsScene;
9 class QRubberBand;
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13 class QChart;
14
15 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
16 {
17 public:
18 explicit QChartView(QWidget *parent = 0);
19 ~QChartView();
20
21 //implement from QWidget
22 void resizeEvent(QResizeEvent *event);
23
24 // TODO: addSeries and createSeries are optional solutions
25 void addSeries(QChartSeries* series);
26 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
27
28 protected:
29 void mouseMoveEvent (QMouseEvent *event);
30 void mousePressEvent (QMouseEvent *event);
31 void mouseReleaseEvent (QMouseEvent *event);
32
33 private:
34 QGraphicsScene *m_scene;
35 QChart* m_chart;
36 QRubberBand *m_rubberBand;
37 QPoint m_origin;
38 bool m_showRubberBand;
39 Q_DISABLE_COPY(QChartView)
40
41
42 };
43
44 QTCOMMERCIALCHART_END_NAMESPACE
45
46 #endif // QCHARTWIDGET_H
@@ -1,6 +1,6
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 #include <qchartwidget.h>
3 #include <qchartview.h>
4 4 #include <qxychartseries.h>
5 5 #include <qchart.h>
6 6 #include <cmath>
@@ -43,11 +43,11 int main(int argc, char *argv[])
43 43 dataset << series2;
44 44 dataset << series3;
45 45
46 QChartWidget* chartWidget = new QChartWidget(&window);
47 chartWidget->addSeries(series1);
48 chartWidget->addSeries(series2);
46 QChartView* chartView = new QChartView(&window);
47 chartView->addSeries(series1);
48 chartView->addSeries(series2);
49 49
50 window.setCentralWidget(chartWidget);
50 window.setCentralWidget(chartView);
51 51 window.resize(400, 300);
52 52 window.show();
53 53
@@ -11,10 +11,9 QGraphicsView(parent)
11 11 {
12 12 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
13 13 m_scene = new QGraphicsScene();
14 m_view = new QGraphicsView(parent);
15 m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
16 m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
17 m_view->setScene(m_scene);
14 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
15 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
16 setScene(m_scene);
18 17
19 18 m_chart = new QChart();
20 19 m_scene->addItem(m_chart);
@@ -24,7 +24,8 SOURCES += \
24 24 qchart.cpp \
25 25 axis.cpp \
26 26 qchartwidget.cpp \
27 pieslice.cpp
27 pieslice.cpp \
28 qchartview.cpp
28 29
29 30 PRIVATE_HEADERS += \
30 31 xylinechart/xylinechartitem_p.h \
@@ -43,7 +44,8 PUBLIC_HEADERS += \
43 44 qchartglobal.h \
44 45 xylinechart/qxychartseries.h \
45 46 barchart/barchartseries.h \
46 barchart/bargroup.h
47 barchart/bargroup.h \
48 qchartview.h
47 49
48 50 HEADERS += $$PUBLIC_HEADERS
49 51 HEADERS += $$PRIVATE_HEADERS
General Comments 0
You need to be logged in to leave comments. Login now