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.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 
@@ -107,25 +107,25 @@ void Plot::setYAxis(bool autoScaled, dou
 
    }
 

	
 
    zoomer.zoom(0);
 
    resetAxes();
 
}
 

	
 
void Plot::setXAxis(double xMin, double xMax)
 
{
 
    _xMin = xMin;
 
    _xMax = xMax;
 

	
 
    zoomer.setXLimits(xMin, xMax);
 
    zoomer.zoom(0); // unzoom
 
    zoomer.setZoomBase();
 

	
 
    // set axis
 
    // setAxisScale(QwtPlot::xBottom, xMin, xMax);
 
    replot(); // Note: if we don't replot here scale at startup isn't set correctly
 

	
 
    // reset zoom base
 
    // auto base = zoomer.zoomBase();
 
    // base.setLeft(xMin);
 
    // base.setRight(xMax);
 
    // zoomer.setZoomBase(base);
 

	
 
    onXScaleChanged();
src/scrollzoomer.cpp
Show inline comments
 
@@ -58,51 +58,102 @@ ScrollZoomer::ScrollZoomer( QWidget *can
 
    vscrollmove = false;
 
}
 

	
 
ScrollZoomer::~ScrollZoomer()
 
{
 
    delete d_cornerWidget;
 
    delete d_vScrollData;
 
    delete d_hScrollData;
 
}
 

	
 
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)
 
{
 
    hscrollmove = true;
 
    hViewSize = size;
 
    setZoomBase();
 
    hscrollmove = false;
 
}
 

	
 
void ScrollZoomer::setZoomBase(bool doReplot)
 
{
 
    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);
 
}
 

	
 
void ScrollZoomer::rescale()
 
{
 
    QwtScaleWidget *xScale = plot()->axisWidget( xAxis() );
 
    QwtScaleWidget *yScale = plot()->axisWidget( yAxis() );
 

	
 
    if ( zoomRectIndex() <= 0 )
 
    {
 
        if ( d_inZoom )
src/scrollzoomer.h
Show inline comments
 
@@ -58,27 +58,34 @@ public:
 
public Q_SLOTS:
 
    virtual void moveTo( const QPointF & );
 

	
 
protected:
 
    virtual ScrollBar *scrollBar( Qt::Orientation );
 
    virtual void updateScrollBars();
 
    virtual void layoutScrollBars( const QRect & );
 

	
 
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;
 

	
 
    QWidget *d_cornerWidget;
 

	
 
    ScrollData *d_hScrollData;
 
    ScrollData *d_vScrollData;
 

	
 
    bool d_inZoom;
 
    bool d_alignCanvasToScales[ QwtPlot::axisCnt ];
 
    bool hscrollmove;
0 comments (0 inline, 0 general)