# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2020-02-18 16:25:04 # Node ID 11522eba8b9bd57ec76be4e00086cf9153bd7a1a # Parent 6b388219977e596d55144ffeb253d8dacc68d88e modify scrollzoomer so that it tries to maintain zoom window when buffer size changes diff --git a/src/plot.cpp b/src/plot.cpp --- a/src/plot.cpp +++ b/src/plot.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2018 Hasan Yavuz Özderya + Copyright © 2020 Hasan Yavuz Özderya This file is part of serialplot. @@ -116,7 +116,7 @@ void Plot::setXAxis(double xMin, double _xMax = xMax; zoomer.setXLimits(xMin, xMax); - zoomer.zoom(0); // unzoom + zoomer.setZoomBase(); // set axis // setAxisScale(QwtPlot::xBottom, xMin, xMax); diff --git a/src/scrollzoomer.cpp b/src/scrollzoomer.cpp --- a/src/scrollzoomer.cpp +++ b/src/scrollzoomer.cpp @@ -67,9 +67,26 @@ ScrollZoomer::~ScrollZoomer() void ScrollZoomer::setXLimits(double min, double max) { + // store current align state to be used when setting zoom base + if ((zoomRect().right() > xMax) || (abs(zoomRect().right() - xMax) < 0.001)) + { + zbAlign = AlignZoomBase::Right; + } + else if (zoomRect().left() < xMin || (zoomRect().left() - xMin < 0.001)) + { + zbAlign = AlignZoomBase::Left; + } + else + { + zbAlign = AlignZoomBase::Center; + } + xMin = min; xMax = max; + setZoomBase(); + zbAlign = AlignZoomBase::Center; + rescale(); } void ScrollZoomer::setHViewSize(double size) @@ -85,15 +102,49 @@ void ScrollZoomer::setZoomBase(bool doRe QwtPlotZoomer::setZoomBase(doReplot); auto zb = zoomBase(); auto zs = zoomStack(); - zb.setRight(xMax); - if ((xMax - xMin) < hViewSize) + + double zbWidth; // final zoom base width + if (hViewSize > (xMax - xMin)) { - zb.setLeft(xMin); + zbWidth = xMax - xMin; } else { - zb.setLeft(xMax-hViewSize); + zbWidth = hViewSize; + } + + // TODO: fix snapping, xlimits is already changed we should compare to previous values + // keep right + // if (abs(zb.right() - xMax) < 0.001) + if (zbAlign == AlignZoomBase::Right) + { + zb.setWidth(zbWidth); + zb.moveRight(xMax); + } // keep left + // else if (zb.left() == xMin) + else if (zbAlign == AlignZoomBase::Left) + { + zb.setWidth(zbWidth); + zb.moveRight(xMin); } + else // keep center + { + // resize the zoom base + double widthDiff = zb.width() - zbWidth; + zb.setLeft(zb.left() + widthDiff / 2.); + zb.setRight(zb.right() - widthDiff / 2.); + + // don't let it go out of limits + if (zb.right() > xMax) + { + zb.moveRight(xMax); + } + else if (zb.left() < xMin) + { + zb.moveLeft(xMin); + } + } + zs[0] = zb; setZoomStack(zs); } diff --git a/src/scrollzoomer.h b/src/scrollzoomer.h --- a/src/scrollzoomer.h +++ b/src/scrollzoomer.h @@ -67,9 +67,16 @@ private Q_SLOTS: void scrollBarMoved( Qt::Orientation o, double min, double max ); private: + enum class AlignZoomBase + { + Right, Center, Left + }; + QRectF d_limits; double xMin, xMax; double hViewSize; + /// used for aligning zoom base during xlimits change + AlignZoomBase zbAlign; bool needScrollBar( Qt::Orientation ) const; int oppositeAxis( int ) const;