diff --git a/scalepicker.cpp b/scalepicker.cpp --- a/scalepicker.cpp +++ b/scalepicker.cpp @@ -19,9 +19,9 @@ #include #include +#include #include #include -#include #include #include "scalepicker.h" @@ -29,11 +29,35 @@ // minimum size for pick (in pixels) #define MIN_PICK_SIZE (2) -ScalePicker::ScalePicker(QwtScaleWidget* scaleWidget) : +class ScalePickerOverlay : public QwtWidgetOverlay +{ +public: + ScalePickerOverlay(QWidget* widget, ScalePicker* picker); + +protected: + virtual void drawOverlay(QPainter*) const; + +private: + ScalePicker* _picker; +}; + +ScalePickerOverlay::ScalePickerOverlay(QWidget* widget, ScalePicker* picker) : + QwtWidgetOverlay(widget) +{ + _picker = picker; +} + +void ScalePickerOverlay::drawOverlay(QPainter* painter) const +{ + _picker->drawOverlay(painter); +} + +ScalePicker::ScalePicker(QwtScaleWidget* scaleWidget, QWidget* canvas) : QObject(scaleWidget) { scaleWidget->installEventFilter(this); _scaleWidget = scaleWidget; + pickerOverlay = new ScalePickerOverlay(canvas, this); started = false; pressed = false; } @@ -47,6 +71,7 @@ bool ScalePicker::eventFilter(QObject* o QMouseEvent* mouseEvent = (QMouseEvent*) event; double pos = this->position(mouseEvent); double posPx = this->positionPx(mouseEvent); + currentPosPx = posPx; if (event->type() == QEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton) @@ -62,11 +87,12 @@ bool ScalePicker::eventFilter(QObject* o if (!started && pressed && (fabs(posPx-firstPosPx) > MIN_PICK_SIZE)) { started = true; - qDebug() << "Pick started:" << firstPos; + // pickerOverlay->updateOverlay(); emit pickStarted(pos); } else if (started) { + pickerOverlay->updateOverlay(); emit picking(firstPos, pos); } } @@ -77,7 +103,6 @@ bool ScalePicker::eventFilter(QObject* o // finalize started = false; pressed = false; - qDebug() << "Picked:" << firstPos << pos; emit picked(firstPos, pos); } } @@ -89,6 +114,26 @@ bool ScalePicker::eventFilter(QObject* o } } +void ScalePicker::drawOverlay(QPainter* painter) +{ + if (started) + { + QRect rect; + if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale || + _scaleWidget->alignment() == QwtScaleDraw::TopScale) + { + int height = painter->device()->height(); + rect = QRect(firstPosPx, 0, (currentPosPx-firstPosPx), height); + } + else // vertical + { + int width = painter->device()->width(); + rect = QRect(0, firstPosPx, width, (currentPosPx-firstPosPx)); + } + painter->drawRect(rect); + } +} + double ScalePicker::position(QMouseEvent* mouseEvent) { double pos; diff --git a/scalepicker.h b/scalepicker.h --- a/scalepicker.h +++ b/scalepicker.h @@ -22,17 +22,19 @@ #include #include -#include #include +#include class ScalePicker : public QObject { Q_OBJECT public: - ScalePicker(QwtScaleWidget* scaleWidget); + ScalePicker(QwtScaleWidget* scaleWidget, QWidget* canvas); virtual bool eventFilter(QObject*, QEvent*); + void drawOverlay(QPainter*); // called from ScalePickerOverlay + signals: void pickStarted(double pos); void picking(double firstPos, double lastPos); @@ -40,6 +42,7 @@ signals: private: QwtScaleWidget* _scaleWidget; + QwtWidgetOverlay* pickerOverlay; // ScalePickerOverlay double position(QMouseEvent*); // returns the axis mouse position relative to plot coordinates double positionPx(QMouseEvent*); // returns the axis mouse position in pixels @@ -47,6 +50,7 @@ private: bool started; double firstPos; // converted to plot coordinates double firstPosPx; // pixel coordinates + double currentPosPx; // current position in pixel coordinates }; #endif // SCALEPICKER_H diff --git a/scalezoomer.cpp b/scalezoomer.cpp --- a/scalezoomer.cpp +++ b/scalezoomer.cpp @@ -18,45 +18,20 @@ */ #include -#include + #include "scalezoomer.h" ScaleZoomer::ScaleZoomer(QwtPlot* plot, QwtPlotZoomer* zoomer) : QObject(plot), - bottomPicker(plot->axisWidget(QwtPlot::xBottom)), - leftPicker(plot->axisWidget(QwtPlot::yLeft)) + bottomPicker(plot->axisWidget(QwtPlot::xBottom), plot->canvas()), + leftPicker(plot->axisWidget(QwtPlot::yLeft), plot->canvas()) { _plot = plot; _zoomer = zoomer; - connect(&bottomPicker, &ScalePicker::pickStarted, this, &ScaleZoomer::bottomPickStarted); - connect(&bottomPicker, &ScalePicker::picking, this, &ScaleZoomer::bottomPicking); connect(&bottomPicker, &ScalePicker::picked, this, &ScaleZoomer::bottomPicked); connect(&leftPicker, &ScalePicker::picked, this, &ScaleZoomer::leftPicked); } -void ScaleZoomer::bottomPickStarted(double firstPos) -{ - double yMin = _plot->axisScaleDiv(QwtPlot::yLeft).lowerBound(); - double yMax = _plot->axisScaleDiv(QwtPlot::yLeft).upperBound(); - rectShape.setRect(QRectF(firstPos, yMin, 2, yMax-yMin)); - rectShape.attach(_plot); - _plot->replot(); -} - -void ScaleZoomer::bottomPicking(double firstPos, double lastPos) -{ - double yMin = _plot->axisScaleDiv(QwtPlot::yLeft).lowerBound(); - double yMax = _plot->axisScaleDiv(QwtPlot::yLeft).upperBound(); - if (lastPos > firstPos) { - rectShape.setRect(QRectF(firstPos, yMin, lastPos-firstPos, yMax-yMin)); - } - else - { - rectShape.setRect(QRectF(lastPos, yMin, firstPos-lastPos, yMax-yMin)); - } - _plot->replot(); -} - void ScaleZoomer::bottomPicked(double firstPos, double lastPos) { QRectF zRect; @@ -74,8 +49,6 @@ void ScaleZoomer::bottomPicked(double fi zRect.setBottom(_plot->axisScaleDiv(QwtPlot::yLeft).lowerBound()); zRect.setTop(_plot->axisScaleDiv(QwtPlot::yLeft).upperBound()); _zoomer->zoom(zRect); - rectShape.detach(); - _plot->replot(); } void ScaleZoomer::leftPicked(double firstPos, double lastPos) diff --git a/scalezoomer.h b/scalezoomer.h --- a/scalezoomer.h +++ b/scalezoomer.h @@ -23,7 +23,6 @@ #include #include #include -#include #include "scalepicker.h" @@ -40,12 +39,8 @@ private: ScalePicker bottomPicker; ScalePicker leftPicker; - QwtPlotShapeItem rectShape; - private slots: - void bottomPickStarted(double firstPos); void bottomPicked(double firstPos, double lastPos); - void bottomPicking(double firstPos, double lastPos); void leftPicked(double firstPos, double lastPos); };