Changeset - a710fe7af2f0
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-11-07 16:27:54
hy@ozderya.net
added snapping for scale zoom picker
2 files changed with 39 insertions and 8 deletions:
0 comments (0 inline, 0 general)
scalepicker.cpp
Show inline comments
 
@@ -19,19 +19,22 @@
 

	
 
#include <QEvent>
 
#include <QMouseEvent>
 
#include <QPainter>
 
#include <qwt_scale_widget.h>
 
#include <qwt_scale_map.h>
 
#include <qwt_scale_div.h>
 
#include <math.h>
 

	
 
#include "scalepicker.h"
 

	
 
// minimum size for pick (in pixels)
 
#define MIN_PICK_SIZE (2)
 

	
 
#define SNAP_DISTANCE (5)
 

	
 
class PlotOverlay : public QwtWidgetOverlay
 
{
 
public:
 
    PlotOverlay(QWidget* widget, ScalePicker* picker);
 

	
 
protected:
 
@@ -91,15 +94,28 @@ ScalePicker::ScalePicker(QwtScaleWidget*
 
bool ScalePicker::eventFilter(QObject* object, QEvent* event)
 
{
 
    if (event->type() == QEvent::MouseButtonPress ||
 
        event->type() == QEvent::MouseButtonRelease ||
 
        event->type() == QEvent::MouseMove)
 
    {
 
        updateSnapPoints();
 

	
 
        QMouseEvent* mouseEvent = (QMouseEvent*) event;
 
        double pos = this->position(mouseEvent);
 
        double posPx = this->positionPx(mouseEvent);
 

	
 
        // do snapping
 
        for (double sp : snapPoints)
 
        {
 
            if (fabs(posPx-sp) <= SNAP_DISTANCE)
 
            {
 
                posPx = sp;
 
                break;
 
            }
 
        }
 

	
 
        double pos = this->position(posPx);
 
        currentPosPx = posPx;
 

	
 
        if (event->type() == QEvent::MouseButtonPress &&
 
            mouseEvent->button() == Qt::LeftButton)
 
        {
 
            pressed = true; // not yet started
 
@@ -201,19 +217,16 @@ void ScalePicker::drawScaleOverlay(QPain
 

	
 
void ScalePicker::setPen(QPen pen)
 
{
 
    _pen = pen;
 
}
 

	
 
double ScalePicker::position(QMouseEvent* mouseEvent)
 
// convert the position of the click to the plot coordinates
 
double ScalePicker::position(double posPx)
 
{
 
    double pos;
 
    pos = positionPx(mouseEvent);
 
    // convert the position of the click to the plot coordinates
 
    pos = _scaleWidget->scaleDraw()->scaleMap().invTransform(pos);
 
    return pos;
 
    return _scaleWidget->scaleDraw()->scaleMap().invTransform(posPx);
 
}
 

	
 
double ScalePicker::positionPx(QMouseEvent* mouseEvent)
 
{
 
    double pos;
 
    if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
@@ -245,6 +258,19 @@ double ScalePicker::posCanvasPx(double p
 
    else // left or right scale
 
    {
 
        return pos + (_scaleWidget->y() - _canvas->y());
 
    }
 
    return pos;
 
}
 

	
 
void ScalePicker::updateSnapPoints()
 
{
 
    auto allTicks = _scaleWidget->scaleDraw()->scaleDiv().ticks(QwtScaleDiv::MajorTick) +
 
        _scaleWidget->scaleDraw()->scaleDiv().ticks(QwtScaleDiv::MediumTick) +
 
        _scaleWidget->scaleDraw()->scaleDiv().ticks(QwtScaleDiv::MinorTick);
 

	
 
    snapPoints.clear();
 
    for(auto t : allTicks)
 
    {
 
        snapPoints << _scaleWidget->scaleDraw()->scaleMap().transform(t);
 
    }
 
}
scalepicker.h
Show inline comments
 
@@ -21,12 +21,13 @@
 
#define SCALEPICKER_H
 

	
 
#include <QObject>
 
#include <QMouseEvent>
 
#include <QPen>
 
#include <QWidget>
 
#include <QList>
 
#include <qwt_scale_widget.h>
 
#include <qwt_widget_overlay.h>
 

	
 
class ScalePicker : public QObject
 
{
 
    Q_OBJECT
 
@@ -53,13 +54,17 @@ private:
 

	
 
    bool pressed;
 
    bool started;
 
    double firstPos; // converted to plot coordinates
 
    double firstPosPx; // pixel coordinates
 
    double currentPosPx; // current position in pixel coordinates
 
    QList<double> snapPoints;
 

	
 
    double position(QMouseEvent*); // returns the axis mouse position relative to plot coordinates
 
    double position(double); // returns the axis mouse position relative to plot coordinates
 
    double positionPx(QMouseEvent*); // returns the axis mouse position in pixels
 
    double posCanvasPx(double pos); // returns the given position in canvas coordinates
 

	
 
private slots:
 
    void updateSnapPoints();
 
};
 

	
 
#endif // SCALEPICKER_H
0 comments (0 inline, 0 general)