diff --git a/src/scalepicker.cpp b/src/scalepicker.cpp --- a/src/scalepicker.cpp +++ b/src/scalepicker.cpp @@ -182,9 +182,8 @@ void ScalePicker::drawPlotOverlay(QPaint 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) @@ -205,13 +204,29 @@ void ScalePicker::drawPlotOverlay(QPaint 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); @@ -377,7 +392,7 @@ void ScalePicker::setPen(QPen 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); } @@ -425,9 +440,12 @@ void ScalePicker::updateSnapPoints() _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; } } diff --git a/src/scalepicker.h b/src/scalepicker.h --- a/src/scalepicker.h +++ b/src/scalepicker.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -58,17 +59,20 @@ private: double firstPosPx; // pixel coordinates double currentPosPx; // current position in pixel coordinates QList snapPoints; + /// used to restore precision of snappoints that is lost due to rounding + QMap 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(); };