Changeset - 80498f7fdebc
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 9 years ago 2017-02-25 11:00:43
hy@ozderya.net
show correct precise values in tracker text when snapped
2 files changed with 31 insertions and 9 deletions:
0 comments (0 inline, 0 general)
src/scalepicker.cpp
Show inline comments
 
@@ -179,15 +179,14 @@ void ScalePicker::drawPlotOverlay(QPaint
 
    {
 
        QColor color = _pen.color();
 
        color.setAlphaF(FILL_ALPHA);
 
        painter->setBrush(color);
 

	
 
        QRect rect;
 
        QwtText text(QString("%1").arg(position(currentPosPx)));
 
        QwtText text = trackerText();
 
        auto tSize = text.textSize(painter->font());
 
        QPointF tTopLeft;
 

	
 
        if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
            _scaleWidget->alignment() == QwtScaleDraw::TopScale)
 
        {
 
            int canvasHeight = painter->device()->height();
 
            int pickWidth = currentPosPx-firstPosPx;
 
@@ -202,19 +201,35 @@ void ScalePicker::drawPlotOverlay(QPaint
 
        painter->drawRect(rect);
 
        text.draw(painter, pickTrackerTextRect(painter, rect, tSize));
 
    }
 
    else if (_scaleWidget->underMouse())
 
    {
 
        // draw tracker text centered on cursor
 
        QwtText text(QString("%1").arg(position(currentPosPx)));
 
        QwtText text = trackerText();
 
        auto tsize = text.textSize(painter->font());
 
        text.draw(painter, trackerTextRect(painter, currentPosPx, tsize));
 
    }
 
    painter->restore();
 
}
 

	
 
QwtText ScalePicker::trackerText() const
 
{
 
    double pos;
 
    // use stored value if snapped to restore precision
 
    if (snapPointMap.contains(currentPosPx))
 
    {
 
        pos = snapPointMap[currentPosPx];
 
    }
 
    else
 
    {
 
        pos = position(currentPosPx);
 
    }
 

	
 
    return QwtText(QString("%1").arg(pos));
 
}
 

	
 
QRectF ScalePicker::trackerTextRect(QPainter* painter, int posPx, QSizeF textSize) const
 
{
 
    int canvasPosPx = posCanvasPx(posPx);
 
    QPointF topLeft;
 

	
 
    if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
@@ -374,13 +389,13 @@ void ScalePicker::drawTriangle(QPainter*
 
void ScalePicker::setPen(QPen pen)
 
{
 
    _pen = pen;
 
}
 

	
 
// convert the position of the click to the plot coordinates
 
double ScalePicker::position(double posPx)
 
double ScalePicker::position(double posPx) const
 
{
 
    return _scaleWidget->scaleDraw()->scaleMap().invTransform(posPx);
 
}
 

	
 
int ScalePicker::positionPx(QMouseEvent* mouseEvent)
 
{
 
@@ -422,12 +437,15 @@ 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();
 
    snapPointMap.clear();
 
    for(auto t : allTicks)
 
    {
 
        // `round` is used because `allTicks` is double but `snapPoints` is int
 
        snapPoints << round(_scaleWidget->scaleDraw()->scaleMap().transform(t));
 
        int p = round(_scaleWidget->scaleDraw()->scaleMap().transform(t));
 
        snapPoints << p;
 
        snapPointMap[p] = t;
 
    }
 
}
src/scalepicker.h
Show inline comments
 
@@ -22,12 +22,13 @@
 

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

	
 
class ScalePicker : public QObject
 
{
 
    Q_OBJECT
 
@@ -55,21 +56,24 @@ private:
 
    bool pressed;
 
    bool started;
 
    double firstPos; // converted to plot coordinates
 
    double firstPosPx; // pixel coordinates
 
    double currentPosPx; // current position in pixel coordinates
 
    QList<int> snapPoints;
 
    /// used to restore precision of snappoints that is lost due to rounding
 
    QMap<int, double> snapPointMap;
 

	
 
    double position(double); // returns the axis mouse position relative to plot coordinates
 
    double position(double) const; // returns the axis mouse position relative to plot coordinates
 
    int positionPx(QMouseEvent*); // returns the axis mouse position in pixels
 
    double posCanvasPx(double pos) const; // returns the given position in canvas coordinates
 
    void drawTriangle(QPainter* painter, int position);
 

	
 
private slots:
 
    /// Returns tracker text position
 
    QwtText trackerText() const;
 
     /// Returns tracker text position
 
    QRectF trackerTextRect(QPainter* painter, int posPx, QSizeF textSize) const;
 
    /// Returns the text position for tracker text shown during picking
 
    QRectF pickTrackerTextRect(QPainter* painter, QRect pickRect, QSizeF textSize) const;
 

	
 
private slots:
 
    void updateSnapPoints();
 
};
 

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