Changeset - 6c305182bff3
[Not reviewed]
scalezoomer
0 2 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-08-26 10:47:31
hy@ozderya.net
make sure pick size is not zero
2 files changed with 37 insertions and 9 deletions:
0 comments (0 inline, 0 general)
scalepicker.cpp
Show inline comments
 
@@ -19,51 +19,67 @@
 

	
 
#include <QEvent>
 
#include <QMouseEvent>
 
#include <qwt_scale_widget.h>
 
#include <qwt_scale_map.h>
 
#include <QtDebug>
 
#include <math.h>
 

	
 
#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)
 
{
 
    if (event->type() == QEvent::MouseButtonPress ||
 
        event->type() == QEvent::MouseButtonRelease ||
 
        event->type() == QEvent::MouseMove)
 
    {
 
        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);
 
            }
 
        }
 
        else // event->type() == QEvent::MouseButtonRelease
 
        {
 
            if (started)
 
            {
 
                // finalize
 
                started = false;
 
                pressed = false;
 
                qDebug() << "Picked:" << firstPos << pos;
 
                emit picked(firstPos, pos);
 
            }
 
        }
 
        return true;
 
    }
 
@@ -72,20 +88,27 @@ bool ScalePicker::eventFilter(QObject* o
 
        return QObject::eventFilter(object, event);
 
    }
 
}
 

	
 
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)
 
    {
 
        pos = mouseEvent->pos().x();
 
    }
 
    else // left or right scale
 
    {
 
        pos = mouseEvent->pos().y();
 
    }
 
    pos = _scaleWidget->scaleDraw()->scaleMap().invTransform(pos);
 
    return pos;
 
}
scalepicker.h
Show inline comments
 
@@ -19,29 +19,34 @@
 

	
 
#ifndef SCALEPICKER_H
 
#define SCALEPICKER_H
 

	
 
#include <QObject>
 
#include <QMouseEvent>
 
#include <QPointF>
 
#include <qwt_scale_widget.h>
 

	
 
class ScalePicker : public QObject
 
{
 
    Q_OBJECT
 

	
 
public:
 
    ScalePicker(QwtScaleWidget* scaleWidget);
 
    virtual bool eventFilter(QObject*, QEvent*);
 

	
 
signals:
 
    void pickStarted(double pos);
 
    void picking(double firstPos, double lastPos);
 
    void picked(double firstPos, double lastPos);
 

	
 
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
0 comments (0 inline, 0 general)