Changeset - 11522eba8b9b
[Not reviewed]
longmem
0 3 0
Hasan Yavuz ÖZDERYA - 6 years ago 2020-02-18 16:25:04
hy@ozderya.net
modify scrollzoomer so that it tries to maintain zoom window when buffer size changes
3 files changed with 64 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/plot.cpp
Show inline comments
 
/*
 
  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);
src/scrollzoomer.cpp
Show inline comments
 
@@ -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);
 
}
src/scrollzoomer.h
Show inline comments
 
@@ -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;
0 comments (0 inline, 0 general)