From adef258de74bec2815bfedcfd95e1a3b70abf39c 2012-02-08 09:31:47 From: Michal Klocek Date: 2012-02-08 09:31:47 Subject: [PATCH] Removes QChartWidget * uses QChartView instead * fixs examples * adds missing rubberband theme hadnling to QChartView --- diff --git a/example/piechart/main.cpp b/example/piechart/main.cpp index 49200a7..3a70ac3 100644 --- a/example/piechart/main.cpp +++ b/example/piechart/main.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include QTCOMMERCIALCHART_USE_NAMESPACE @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); // Create widget and scatter series - QChartWidget *chartWidget = new QChartWidget(); + QChartView *chartWidget = new QChartView(); QPieSeries *series = qobject_cast(chartWidget->createSeries(QChartSeries::SeriesTypePie)); Q_ASSERT(series); diff --git a/example/scatter/main.cpp b/example/scatter/main.cpp index d508c10..c5260d9 100644 --- a/example/scatter/main.cpp +++ b/example/scatter/main.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include QTCOMMERCIALCHART_USE_NAMESPACE @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); // Create widget and scatter series - QChartWidget *chartWidget = new QChartWidget(); + QChartView *chartWidget = new QChartView(); QScatterSeries *scatter = qobject_cast(chartWidget->createSeries(QChartSeries::SeriesTypeScatter)); Q_ASSERT(scatter); diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 753c4b3..b17d4fc 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -11,7 +11,10 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChartView::QChartView(QWidget *parent) : QGraphicsView(parent), m_scene(new QGraphicsScene()), - m_chart(new QChart()) + m_chart(new QChart()), + m_rubberBand(0), + m_verticalRubberBand(false), + m_horizonalRubberBand(false) { setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -82,4 +85,118 @@ void QChartView::setChartBackgroundPen(const QPen& pen) { m_chart->setChartBackgroundPen(pen); } + + +void QChartView::setRubberBandPolicy(const RubberBandPolicy policy) +{ + switch(policy){ + case VerticalRubberBand: + m_verticalRubberBand = true; + m_horizonalRubberBand = false; + break; + case HorizonalRubberBand: + m_verticalRubberBand = false; + m_horizonalRubberBand = true; + break; + case RectangleRubberBand: + m_verticalRubberBand = true; + m_horizonalRubberBand = true; + break; + case NoRubberBand: + default: + delete m_rubberBand; + m_rubberBand=0; + m_horizonalRubberBand = false; + m_verticalRubberBand = false; + return; + } + + m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); + m_rubberBand->setEnabled(true); +} + +QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const +{ + if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand; + if(m_horizonalRubberBand) return HorizonalRubberBand; + if(m_verticalRubberBand) return VerticalRubberBand; + return NoRubberBand; +} + + +void QChartView::mousePressEvent(QMouseEvent *event) +{ + if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { + + int margin = m_chart->margin(); + QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); + + if (rect.contains(event->pos())) { + m_rubberBandOrigin = event->pos(); + m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize())); + m_rubberBand->show(); + event->accept(); + } + } +} + +void QChartView::mouseMoveEvent(QMouseEvent *event) +{ + if(m_rubberBand && m_rubberBand->isVisible()){ + int margin = m_chart->margin(); + QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); + int width = event->pos().x() - m_rubberBandOrigin.x(); + int height = event->pos().y() - m_rubberBandOrigin.y(); + if(!m_verticalRubberBand) { + m_rubberBandOrigin.setY(rect.top()); + height = rect.height(); + } + if(!m_horizonalRubberBand) { + m_rubberBandOrigin.setX(rect.left()); + width= rect.width(); + } + m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized()); + } else { + QGraphicsView::mouseMoveEvent(event); + } +} + +void QChartView::mouseReleaseEvent(QMouseEvent *event) +{ + if(m_rubberBand){ + if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { + m_rubberBand->hide(); + QRect rect = m_rubberBand->geometry(); + m_chart->zoomInToRect(rect); + event->accept(); + } + + if(event->button()==Qt::RightButton) + m_chart->zoomReset(); + }else{ + QGraphicsView::mouseReleaseEvent(event); + } +} + +void QChartView::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Plus: + zoomIn(); + break; + case Qt::Key_Minus: + zoomOut(); + break; + default: + QGraphicsView::keyPressEvent(event); + break; + } +} + +void QChartView::setTheme(QChart::ChartThemeId theme) +{ + m_chart->setTheme(theme); +} + + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h index 04f2de6..2becc04 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -7,6 +7,7 @@ #include class QGraphicsScene; +class QRubberBand; QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -15,6 +16,8 @@ class QChart; class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView { public: + enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand }; + explicit QChartView(QWidget *parent = 0); ~QChartView(); @@ -22,16 +25,17 @@ public: void resizeEvent(QResizeEvent *event); void addSeries(QChartSeries* series); + // Convenience function QChartSeries* createSeries(QChartSeries::QChartSeriesType type); int margin() const; + void setTitle(const QString& title); //Obsolete interface void setBackground(const QColor& startColor, const QColor& endColor = Qt::white, QChart::GradientOrientation orientation = QChart::VerticalGradientOrientation); - void setChartBackgroundBrush(const QBrush& brush); void setChartBackgroundPen(const QPen& pen); @@ -39,10 +43,25 @@ public: void zoomIn(); void zoomOut(); + void setRubberBandPolicy(const RubberBandPolicy ); + RubberBandPolicy rubberBandPolicy() const; + + void setTheme(QChart::ChartThemeId theme); + +protected: + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void keyPressEvent(QKeyEvent *event); + + private: QGraphicsScene *m_scene; QChart* m_chart; - QPoint m_origin; + QPoint m_rubberBandOrigin; + QRubberBand* m_rubberBand; + bool m_verticalRubberBand; + bool m_horizonalRubberBand; Q_DISABLE_COPY(QChartView) diff --git a/src/qchartwidget.cpp b/src/qchartwidget.cpp deleted file mode 100644 index c13ebed..0000000 --- a/src/qchartwidget.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "qchartwidget.h" -#include "qchartseries.h" -#include -#include -#include - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -QChartWidget::QChartWidget(QWidget *parent) : - QGraphicsView(parent), - m_rubberBand(QRubberBand::Rectangle, this), - m_originX(0) -{ - setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - m_scene = new QGraphicsScene(); - setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - setScene(m_scene); - - m_chart = new QChart(); - // TODO: currently setting the margins explicitly is required, if you wish to have axis labels visible - m_chart->setMargin(25); - m_scene->addItem(m_chart); - m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default? - show(); -} - -QChartWidget::~QChartWidget() -{ -} - -void QChartWidget::resizeEvent(QResizeEvent *event) -{ - m_scene->setSceneRect(0,0,size().width(),size().height()); - m_chart->resize(size()); - QWidget::resizeEvent(event); -} - -QSize QChartWidget::sizeHint() const -{ - // TODO: calculate size hint based on contents? - return QSize(100, 100); -} - -void QChartWidget::mousePressEvent(QMouseEvent *event) -{ - if(m_rubberBand.isEnabled() && event->button() == Qt::LeftButton) { - int margin = m_chart->margin(); - QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); - - if (rect.contains(event->pos())) { - m_originX = event->pos().x(); - m_rubberBand.setGeometry(QRect(m_originX, 0, 0, height())); - m_rubberBand.show(); - event->accept(); - } - } -} - -void QChartWidget::mouseMoveEvent(QMouseEvent *event) -{ - if(m_rubberBand.isVisible()) - m_rubberBand.setGeometry(QRect(m_originX, 0, - event->pos().x() - m_originX, height()).normalized()); -} - -void QChartWidget::mouseReleaseEvent(QMouseEvent *event) -{ - if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) { - m_rubberBand.hide(); - QRect rect = m_rubberBand.geometry(); - m_chart->zoomInToRect(rect); - event->accept(); - } - - if(event->button()==Qt::RightButton) - m_chart->zoomReset(); -} - -void QChartWidget::addSeries(QChartSeries* series) -{ - m_chart->addSeries(series); -} - -QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type) -{ - return m_chart->createSeries(type); -} - -void QChartWidget::setTheme(QChart::ChartThemeId theme) -{ - m_chart->setTheme(theme); -} - -void QChartWidget::setZoomEnabled(bool enabled) -{ - m_rubberBand.setEnabled(enabled); -} - -#include "moc_qchartwidget.cpp" - -QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartwidget.h b/src/qchartwidget.h deleted file mode 100644 index 47a0ca9..0000000 --- a/src/qchartwidget.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef QCHARTWIDGET_H -#define QCHARTWIDGET_H - -#include "qchartglobal.h" -#include "qchart.h" -#include -#include - -class QGraphicsScene; -class QRubberBand; - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -class QChartSeries; -class QChartWidgetPrivate; - -class QTCOMMERCIALCHART_EXPORT QChartWidget : public QGraphicsView -{ - Q_OBJECT -public: - explicit QChartWidget(QWidget *parent = 0); - ~QChartWidget(); - - // TODO: addSeries and createSeries are optional solutions - // TODO: currently createSeries assumes x, y value pairs. This isn't case with all charts. So is there another createSeries for other types (for example one list of ints)? -public Q_SLOTS: - void addSeries(QChartSeries* series); - QChartSeries* createSeries(QChartSeries::QChartSeriesType type); - - /*! - * Set color theme for the chart. Themes define harmonic colors for the graphical elements of - * the chart. - */ - void setTheme(QChart::ChartThemeId theme); - - void setZoomEnabled(bool enabled); - -private: // From QWidget TODO: should these be protected instead? Is QChartWidget meant to be extened by the user? - void resizeEvent(QResizeEvent *event); - QSize sizeHint() const; - void mousePressEvent(QMouseEvent *event); - void mouseMoveEvent(QMouseEvent *event); - void mouseReleaseEvent(QMouseEvent *event); - -private: - Q_DISABLE_COPY(QChartWidget) - // TODO: move the following to pimpl - QGraphicsScene *m_scene; - QChart* m_chart; - QRubberBand m_rubberBand; - int m_originX; -}; - -QTCOMMERCIALCHART_END_NAMESPACE - -#endif // QCHARTWIDGET_H diff --git a/src/src.pro b/src/src.pro index faf7e7c..735187b 100644 --- a/src/src.pro +++ b/src/src.pro @@ -28,7 +28,6 @@ SOURCES += \ qpieseries.cpp \ qchart.cpp \ axisitem.cpp \ - qchartwidget.cpp \ pieslice.cpp \ qchartview.cpp \ qchartseries.cpp \ @@ -63,7 +62,6 @@ PUBLIC_HEADERS += \ qscatterseries.h \ qpieseries.h \ qchart.h \ - qchartwidget.h \ qchartglobal.h \ xylinechart/qxychartseries.h \ barchart/barchartseries.h \ diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 4ce0e03..89d3386 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -23,7 +23,8 @@ QTCOMMERCIALCHART_USE_NAMESPACE MainWidget::MainWidget(QWidget *parent) : QWidget(parent) { - m_chartWidget = new QChartWidget(this); + m_chartWidget = new QChartView(this); + m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand); // Grid layout for the controls for configuring the chart widget QGridLayout *grid = new QGridLayout(); diff --git a/test/chartwidgettest/mainwidget.h b/test/chartwidgettest/mainwidget.h index ca4660b..4567400 100644 --- a/test/chartwidgettest/mainwidget.h +++ b/test/chartwidgettest/mainwidget.h @@ -2,7 +2,7 @@ #define MAINWIDGET_H #include -#include +#include #include class QSpinBox; @@ -41,7 +41,7 @@ private slots: void setPiePosition(int position); private: - QChartWidget *m_chartWidget; + QChartView *m_chartWidget; QCheckBox *m_autoScaleCheck; QSpinBox *m_xMinSpin; QSpinBox *m_xMaxSpin;