From 1c099f3b0fd1b89fd4487e85549df252c976f46a 2012-01-23 16:01:38 From: Michal Klocek Date: 2012-01-23 16:01:38 Subject: [PATCH] Adds rubberband for zooming * adds qchartview class * implements mouse events for handling rubberband --- diff --git a/example/linechart/main.cpp b/example/linechart/main.cpp index 41e1bde..b9b914f 100644 --- a/example/linechart/main.cpp +++ b/example/linechart/main.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -43,11 +43,11 @@ int main(int argc, char *argv[]) dataset << series2; dataset << series3; - QChartWidget* chartWidget = new QChartWidget(&window); - chartWidget->addSeries(series1); - chartWidget->addSeries(series2); + QChartView* chartView = new QChartView(&window); + chartView->addSeries(series1); + chartView->addSeries(series2); - window.setCentralWidget(chartWidget); + window.setCentralWidget(chartView); window.resize(400, 300); window.show(); diff --git a/src/qchartview.cpp b/src/qchartview.cpp new file mode 100644 index 0000000..bf493c6 --- /dev/null +++ b/src/qchartview.cpp @@ -0,0 +1,81 @@ +#include "qchartview.h" +#include "qchart.h" +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QChartView::QChartView(QWidget *parent) : + QGraphicsView(parent), + m_scene(new QGraphicsScene()), + m_chart(new QChart()), + m_rubberBand(0), + m_showRubberBand(false) +{ + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setScene(m_scene); + m_chart->setMargin(50); + m_scene->addItem(m_chart); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); +} + +QChartView::~QChartView() +{ +} + +void QChartView::resizeEvent(QResizeEvent *event) +{ + m_scene->setSceneRect(0,0,size().width(),size().height()); + m_chart->setSize(size()); + QWidget::resizeEvent(event); +} + + +void QChartView::addSeries(QChartSeries* series) +{ + m_chart->addSeries(series); +} + +QChartSeries* QChartView::createSeries(QList x, QList y, QChartSeries::QChartSeriesType type) +{ + + return m_chart->createSeries(x, y, type); +} + +void QChartView::mousePressEvent(QMouseEvent *event) +{ + int margin = m_chart->margin(); + + QRect rect(margin,margin,width()-2*margin,height()-2*margin); + + m_origin = event->pos(); + + if (!rect.contains(m_origin)) return; + + if (!m_rubberBand) + m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); + m_rubberBand->setGeometry(QRect(m_origin, QSize())); + m_showRubberBand=true; + m_rubberBand->show(); + +} + + void QChartView::mouseMoveEvent(QMouseEvent *event) +{ + if(m_showRubberBand) + m_rubberBand->setGeometry(QRect(m_origin, event->pos()).normalized()); +} + + void QChartView::mouseReleaseEvent(QMouseEvent *event) +{ + if(m_showRubberBand) { + m_rubberBand->hide(); + m_showRubberBand=false; + } +} + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h new file mode 100644 index 0000000..e08daa0 --- /dev/null +++ b/src/qchartview.h @@ -0,0 +1,46 @@ +#ifndef QCHARTWIDGET_H +#define QCHARTWIDGET_H + +#include "qchartglobal.h" +#include "qchartseries.h" +#include + +class QGraphicsScene; +class QRubberBand; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QChart; + +class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView +{ +public: + explicit QChartView(QWidget *parent = 0); + ~QChartView(); + + //implement from QWidget + void resizeEvent(QResizeEvent *event); + + // TODO: addSeries and createSeries are optional solutions + void addSeries(QChartSeries* series); + QChartSeries* createSeries(QList x, QList y, QChartSeries::QChartSeriesType type); + +protected: + void mouseMoveEvent (QMouseEvent *event); + void mousePressEvent (QMouseEvent *event); + void mouseReleaseEvent (QMouseEvent *event); + +private: + QGraphicsScene *m_scene; + QChart* m_chart; + QRubberBand *m_rubberBand; + QPoint m_origin; + bool m_showRubberBand; + Q_DISABLE_COPY(QChartView) + + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QCHARTWIDGET_H diff --git a/src/qchartwidget.cpp b/src/qchartwidget.cpp index 1ef1127..214d765 100644 --- a/src/qchartwidget.cpp +++ b/src/qchartwidget.cpp @@ -10,11 +10,10 @@ QChartWidget::QChartWidget(QWidget *parent) : QGraphicsView(parent) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - m_scene = new QGraphicsScene(); - m_view = new QGraphicsView(parent); - m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - m_view->setScene(m_scene); + m_scene = new QGraphicsScene(); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setScene(m_scene); m_chart = new QChart(); m_scene->addItem(m_chart); diff --git a/src/src.pro b/src/src.pro index f9b78f4..3fc88db 100644 --- a/src/src.pro +++ b/src/src.pro @@ -24,7 +24,8 @@ SOURCES += \ qchart.cpp \ axis.cpp \ qchartwidget.cpp \ - pieslice.cpp + pieslice.cpp \ + qchartview.cpp PRIVATE_HEADERS += \ xylinechart/xylinechartitem_p.h \ @@ -43,8 +44,9 @@ PUBLIC_HEADERS += \ qchartglobal.h \ xylinechart/qxychartseries.h \ barchart/barchartseries.h \ - barchart/bargroup.h - + barchart/bargroup.h \ + qchartview.h + HEADERS += $$PUBLIC_HEADERS HEADERS += $$PRIVATE_HEADERS