##// 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,55 +1,55
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>
7 7
8 8 QTCOMMERCIALCHART_USE_NAMESPACE
9 9
10 10 #define PI 3.14159265358979
11 11
12 12 int main(int argc, char *argv[])
13 13 {
14 14 QApplication a(argc, argv);
15 15
16 16 QMainWindow window;
17 17
18 18 QXYChartSeries* series0 = QXYChartSeries::create();
19 19 series0->setColor(Qt::blue);
20 20 QXYChartSeries* series1 = QXYChartSeries::create();
21 21 series1->setColor(Qt::red);
22 22 QXYChartSeries* series2 = QXYChartSeries::create();
23 23 series2->setColor(Qt::gray);
24 24 QXYChartSeries* series3 = QXYChartSeries::create();
25 25 series3->setColor(Qt::green);
26 26
27 27 int numPoints = 100;
28 28
29 29 for (int x = 0; x < numPoints; ++x) {
30 30 series0->add(x,0);
31 31 series1->add(x, abs(sin(PI/50*x)*100));
32 32 series2->add(x, abs(cos(PI/50*x)*100));
33 33 series3->add(x,100);
34 34 }
35 35
36 36 QList<QXYChartSeries*> dataset;
37 37
38 38 //qDebug()<<"Series 1:" << *series1;
39 39 //qDebug()<<"Series 2:" << *series2;
40 40
41 41 dataset << series0;
42 42 dataset << series1;
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
54 54 return a.exec();
55 55 }
@@ -1,52 +1,51
1 1 #include "qchartwidget.h"
2 2 #include "qchartseries.h"
3 3 #include <QGraphicsView>
4 4 #include <QGraphicsScene>
5 5 #include <QResizeEvent>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 QChartWidget::QChartWidget(QWidget *parent) :
10 10 QGraphicsView(parent)
11 11 {
12 12 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
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);
13 m_scene = new QGraphicsScene();
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);
21 20 show();
22 21 }
23 22
24 23 QChartWidget::~QChartWidget()
25 24 {
26 25 }
27 26
28 27 void QChartWidget::resizeEvent(QResizeEvent *event)
29 28 {
30 29 m_scene->setSceneRect(0,0,size().width(),size().height());
31 30 m_chart->setSize(size());
32 31 QWidget::resizeEvent(event);
33 32 }
34 33
35 34 QSize QChartWidget::sizeHint() const
36 35 {
37 36 // TODO: calculate size hint based on contents?
38 37 return QSize(100, 100);
39 38 }
40 39
41 40 void QChartWidget::addSeries(QChartSeries* series)
42 41 {
43 42 m_chart->addSeries(series);
44 43 }
45 44
46 45 QChartSeries* QChartWidget::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
47 46 {
48 47 return m_chart->createSeries(x, y, type);
49 48 }
50 49 #include "moc_qchartwidget.cpp"
51 50
52 51 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,89
1 1 !include( ../common.pri ) {
2 2 error( Couldn't find the common.pri file! )
3 3 }
4 4
5 5 TARGET = QtCommercialChart
6 6 DESTDIR = $$CHART_BUILD_LIB_DIR
7 7 TEMPLATE = lib
8 8 QT += core \
9 9 gui
10 10
11 11 CONFIG += debug_and_release
12 12 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
13 13
14 14 SOURCES += \
15 15 barchart/barchartseries.cpp \
16 16 barchart/bargroup.cpp \
17 17 barchart/bar.cpp \
18 18 xylinechart/qxychartseries.cpp \
19 19 xylinechart/xylinechartitem.cpp \
20 20 xylinechart/xygrid.cpp \
21 21 xylinechart/xyplotdomain.cpp \
22 22 qscatterseries.cpp \
23 23 qpieseries.cpp \
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 \
31 32 xylinechart/xyplotdomain_p.h \
32 33 xylinechart/xygrid_p.h \
33 34 qscatterseries_p.h \
34 35 pieslice.h \
35 36 axis_p.h
36 37
37 38 PUBLIC_HEADERS += \
38 39 qchartseries.h \
39 40 qscatterseries.h \
40 41 qpieseries.h \
41 42 qchart.h \
42 43 qchartwidget.h \
43 44 qchartglobal.h \
44 45 xylinechart/qxychartseries.h \
45 46 barchart/barchartseries.h \
46 barchart/bargroup.h
47
47 barchart/bargroup.h \
48 qchartview.h
49
48 50 HEADERS += $$PUBLIC_HEADERS
49 51 HEADERS += $$PRIVATE_HEADERS
50 52
51 53 INCLUDEPATH += xylinechart \
52 54 barchart \
53 55 .
54 56
55 57 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
56 58 MOC_DIR = $$CHART_BUILD_DIR/lib
57 59 UI_DIR = $$CHART_BUILD_DIR/lib
58 60 RCC_DIR = $$CHART_BUILD_DIR/lib
59 61
60 62
61 63 DEFINES += QTCOMMERCIALCHART_LIBRARY
62 64
63 65 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
64 66 public_headers.files = $$PUBLIC_HEADERS
65 67 target.path = $$[QT_INSTALL_LIBS]
66 68 INSTALLS += target \
67 69 public_headers
68 70
69 71
70 72 install_build_headers.name = bild_headers
71 73 install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h
72 74 install_build_headers.input = PUBLIC_HEADERS
73 75 install_build_headers.commands = $$QMAKE_COPY ${QMAKE_FILE_NAME} $$CHART_BUILD_HEADER_DIR
74 76 install_build_headers.CONFIG += target_predeps no_link
75 77 QMAKE_EXTRA_COMPILERS += install_build_headers
76 78
77 79 chartversion.target = qchartversion_p.h
78 80 chartversion.commands = @echo "build_time" > $$chartversion.target;
79 81 chartversion.depends = $$HEADERS $$SOURCES
80 82 PRE_TARGETDEPS += qchartversion_p.h
81 83 QMAKE_CLEAN+= qchartversion_p.h
82 84 QMAKE_EXTRA_TARGETS += chartversion
83 85
84 86 unix:QMAKE_DISTCLEAN += -r $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
85 87 win32:QMAKE_DISTCLEAN += /Q $$CHART_BUILD_HEADER_DIR $$CHART_BUILD_LIB_DIR
86 88
87 89
General Comments 0
You need to be logged in to leave comments. Login now