# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2015-08-26 10:47:31 # Node ID 6c305182bff32f6bbc7a6773d8114afbe9c2fb2d # Parent 6548ba64c950e515afa995dde3aec566bea41f03 make sure pick size is not zero diff --git a/scalepicker.cpp b/scalepicker.cpp --- a/scalepicker.cpp +++ b/scalepicker.cpp @@ -22,15 +22,20 @@ #include #include #include +#include #include "scalepicker.h" +// minimum size for pick (in pixels) +#define MIN_PICK_SIZE (2) + ScalePicker::ScalePicker(QwtScaleWidget* scaleWidget) : QObject(scaleWidget) { scaleWidget->installEventFilter(this); _scaleWidget = scaleWidget; started = false; + pressed = false; } bool ScalePicker::eventFilter(QObject* object, QEvent* event) @@ -41,17 +46,26 @@ bool ScalePicker::eventFilter(QObject* o { QMouseEvent* mouseEvent = (QMouseEvent*) event; double pos = this->position(mouseEvent); + double posPx = this->positionPx(mouseEvent); - if (event->type() == QEvent::MouseButtonPress) + if (event->type() == QEvent::MouseButtonPress && + mouseEvent->button() == Qt::LeftButton) { - started = true; + pressed = true; // not yet started firstPos = pos; - qDebug() << "Pick started:" << firstPos; - emit pickStarted(pos); + firstPosPx = posPx; } else if (event->type() == QEvent::MouseMove) { - if (started) + // make sure pick size is big enough, so that just + // clicking won't trigger pick + if (!started && pressed && (fabs(posPx-firstPosPx) > MIN_PICK_SIZE)) + { + started = true; + qDebug() << "Pick started:" << firstPos; + emit pickStarted(pos); + } + else if (started) { emit picking(firstPos, pos); } @@ -60,7 +74,9 @@ bool ScalePicker::eventFilter(QObject* o { if (started) { + // finalize started = false; + pressed = false; qDebug() << "Picked:" << firstPos << pos; emit picked(firstPos, pos); } @@ -75,7 +91,15 @@ bool ScalePicker::eventFilter(QObject* o double ScalePicker::position(QMouseEvent* mouseEvent) { - // capture and convert the position of the click to the plot coordinates + double pos; + pos = positionPx(mouseEvent); + // convert the position of the click to the plot coordinates + pos = _scaleWidget->scaleDraw()->scaleMap().invTransform(pos); + return pos; +} + +double ScalePicker::positionPx(QMouseEvent* mouseEvent) +{ double pos; if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale || _scaleWidget->alignment() == QwtScaleDraw::TopScale) @@ -86,6 +110,5 @@ double ScalePicker::position(QMouseEvent { pos = mouseEvent->pos().y(); } - pos = _scaleWidget->scaleDraw()->scaleMap().invTransform(pos); return pos; } diff --git a/scalepicker.h b/scalepicker.h --- a/scalepicker.h +++ b/scalepicker.h @@ -22,11 +22,13 @@ #include #include +#include #include class ScalePicker : public QObject { Q_OBJECT + public: ScalePicker(QwtScaleWidget* scaleWidget); virtual bool eventFilter(QObject*, QEvent*); @@ -38,10 +40,13 @@ signals: private: QwtScaleWidget* _scaleWidget; - double position(QMouseEvent*); // returns the mouse position relative to plot coordinates + double position(QMouseEvent*); // returns the axis mouse position relative to plot coordinates + double positionPx(QMouseEvent*); // returns the axis mouse position in pixels + bool pressed; bool started; - double firstPos; + double firstPos; // converted to plot coordinates + double firstPosPx; // pixel coordinates }; #endif // SCALEPICKER_H