diff --git a/src/qchart.cpp b/src/qchart.cpp index 92b17d9..d0f21ea 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -351,7 +351,7 @@ void QChart::zoomIn() foreach (ChartItem* item ,m_chartItems) item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); update(); - }else{ + } else { QRect rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); rect.setWidth(rect.width()/2); rect.setHeight(rect.height()/2); @@ -370,6 +370,16 @@ void QChart::zoomOut() } } +void QChart::zoomReset() +{ + if (m_plotDataIndex > 0) { + m_plotDataIndex = 0; + foreach (ChartItem* item ,m_chartItems) + item->setPlotDomain(m_plotDomainList[m_plotDataIndex]); + update(); + } +} + void QChart::setAxisX(const QChartAxis& axis) { setAxis(m_axisXItem,axis); diff --git a/src/qchart.h b/src/qchart.h index 7f897d3..f1501fb 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -66,6 +66,7 @@ public: void zoomInToRect(const QRect& rectangle); void zoomIn(); void zoomOut(); + void zoomReset(); void setAxisX(const QChartAxis& axis); void setAxisY(const QChartAxis& axis); diff --git a/src/qchartwidget.cpp b/src/qchartwidget.cpp index dc87270..c601b89 100644 --- a/src/qchartwidget.cpp +++ b/src/qchartwidget.cpp @@ -8,7 +8,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChartWidget::QChartWidget(QWidget *parent) : QGraphicsView(parent), - m_rubberBand(QRubberBand::Rectangle, this) + m_rubberBand(QRubberBand::Rectangle, this), + m_originX(0) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_scene = new QGraphicsScene(); @@ -44,10 +45,10 @@ 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); - m_origin = event->pos(); - if (rect.contains(m_origin)) { - m_rubberBand.setGeometry(QRect(m_origin, QSize())); + if (rect.contains(event->pos())) { + m_originX = event->pos().x(); + m_rubberBand.setGeometry(QRect(m_originX, 0, 0, height())); m_rubberBand.show(); event->accept(); } @@ -57,7 +58,8 @@ void QChartWidget::mousePressEvent(QMouseEvent *event) void QChartWidget::mouseMoveEvent(QMouseEvent *event) { if(m_rubberBand.isVisible()) - m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized()); + m_rubberBand.setGeometry(QRect(m_originX, 0, + event->pos().x() - m_originX, height()).normalized()); } void QChartWidget::mouseReleaseEvent(QMouseEvent *event) @@ -69,9 +71,8 @@ void QChartWidget::mouseReleaseEvent(QMouseEvent *event) event->accept(); } - if(event->button()==Qt::RightButton) { - m_chart->zoomOut(); - } + if(event->button()==Qt::RightButton) + m_chart->zoomReset(); } void QChartWidget::addSeries(QChartSeries* series) diff --git a/src/qchartwidget.h b/src/qchartwidget.h index d0cbf0d..47a0ca9 100644 --- a/src/qchartwidget.h +++ b/src/qchartwidget.h @@ -48,7 +48,7 @@ private: QGraphicsScene *m_scene; QChart* m_chart; QRubberBand m_rubberBand; - QPoint m_origin; + int m_originX; }; QTCOMMERCIALCHART_END_NAMESPACE