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.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QRectF>
 
#include <QKeySequence>
 
#include <QColor>
 
#include <qwt_symbol.h>
 
#include <qwt_plot_curve.h>
 
#include <math.h>
 
#include <algorithm>
 

	
 
#include "plot.h"
 
#include "utils.h"
 

	
 
static const int SYMBOL_SHOW_AT_WIDTH = 5;
 
static const int SYMBOL_SIZE_MAX = 7;
 

	
 
Plot::Plot(QWidget* parent) :
 
    QwtPlot(parent),
 
    zoomer(this->canvas(), false),
 
    sZoomer(this, &zoomer)
 
{
 
    isAutoScaled = true;
 
    symbolSize = 0;
 
    numOfSamples = 1;
 
    plotWidth = 1;
 
    showSymbols = Plot::ShowSymbolsAuto;
 

	
 
    QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed);
 

	
 
    zoomer.setZoomBase();
 
    grid.attach(this);
 
    legend.attach(this);
 

	
 
@@ -71,97 +71,97 @@ Plot::Plot(QWidget* parent) :
 
    demoText.setBackgroundBrush(Qt::darkRed);
 
    demoText.setBorderRadius(4);
 
    demoText.setRenderFlags(Qt::AlignLeft | Qt::AlignBottom);
 
    demoIndicator.setText(demoText);
 
    demoIndicator.hide();
 
    demoIndicator.attach(this);
 

	
 
    // init no channels are visible indicator
 
    QwtText noChannelText(" No Visible Channels ");
 
    noChannelText.setColor(QColor("white"));
 
    noChannelText.setBackgroundBrush(Qt::darkBlue);
 
    noChannelText.setBorderRadius(4);
 
    noChannelText.setRenderFlags(Qt::AlignHCenter | Qt::AlignVCenter);
 
    noChannelIndicator.setText(noChannelText);
 
    noChannelIndicator.hide();
 
    noChannelIndicator.attach(this);
 
}
 

	
 
Plot::~Plot()
 
{
 
    if (snapshotOverlay != NULL) delete snapshotOverlay;
 
}
 

	
 
void Plot::setDispChannels(QVector<const StreamChannel*> channels)
 
{
 
    zoomer.setDispChannels(channels);
 
}
 

	
 
void Plot::setYAxis(bool autoScaled, double yAxisMin, double yAxisMax)
 
{
 
    this->isAutoScaled = autoScaled;
 

	
 
    if (!autoScaled)
 
    {
 
        yMin = yAxisMin;
 
        yMax = yAxisMax;
 
    }
 

	
 
    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();
 
}
 

	
 
void Plot::resetAxes()
 
{
 
    // reset y axis
 
    if (isAutoScaled)
 
    {
 
        setAxisAutoScale(QwtPlot::yLeft);
 
    }
 
    else
 
    {
 
        setAxisScale(QwtPlot::yLeft, yMin, yMax);
 
    }
 

	
 
    zoomer.setZoomBase();
 

	
 
    replot();
 
}
 

	
 
void Plot::unzoomed()
 
{
 
    resetAxes();
 
    onXScaleChanged();
 
}
 

	
 
void Plot::showGrid(bool show)
 
{
 
    grid.enableX(show);
 
    grid.enableY(show);
 
    replot();
 
}
 

	
 
void Plot::showMinorGrid(bool show)
 
{
 
    grid.enableXMin(show);
 
    grid.enableYMin(show);
src/scrollzoomer.cpp
Show inline comments
 
@@ -22,123 +22,174 @@ public:
 
        scrollBar( NULL ),
 
        position( ScrollZoomer::OppositeToScale ),
 
        mode( Qt::ScrollBarAsNeeded )
 
    {
 
    }
 

	
 
    ~ScrollData()
 
    {
 
        delete scrollBar;
 
    }
 

	
 
    ScrollBar *scrollBar;
 
    ScrollZoomer::ScrollBarPosition position;
 
    Qt::ScrollBarPolicy mode;
 
};
 

	
 
ScrollZoomer::ScrollZoomer( QWidget *canvas ):
 
    QwtPlotZoomer( canvas ),
 
    d_cornerWidget( NULL ),
 
    d_hScrollData( NULL ),
 
    d_vScrollData( NULL ),
 
    d_inZoom( false )
 
{
 
    xMin = 0.;
 
    xMax = 10000.;
 
    hViewSize = 10000;
 

	
 
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
        d_alignCanvasToScales[ axis ] = false;
 

	
 
    if ( !canvas )
 
        return;
 

	
 
    d_hScrollData = new ScrollData;
 
    d_vScrollData = new ScrollData;
 
    hscrollmove = false;
 
    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 )
 
        {
 
            xScale->setMinBorderDist( 0, 0 );
 
            yScale->setMinBorderDist( 0, 0 );
 

	
 
            QwtPlotLayout *layout = plot()->plotLayout();
 

	
 
            for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
                layout->setAlignCanvasToScale( axis, d_alignCanvasToScales[ axis ] );
 

	
 
            d_inZoom = false;
 
        }
 
    }
 
    else
 
    {
 
        if ( !d_inZoom )
 
        {
 
            /*
 
             We set a minimum border distance.
 
             Otherwise the canvas size changes when scrolling,
 
             between situations where the major ticks are at
 
             the canvas borders (requiring extra space for the label)
 
             and situations where all labels can be painted below/top
 
             or left/right of the canvas.
 
             */
 
            int start, end;
 

	
 
            xScale->getBorderDistHint( start, end );
 
            xScale->setMinBorderDist( start, end );
 

	
 
            yScale->getBorderDistHint( start, end );
 
            yScale->setMinBorderDist( start, end );
 

	
 
            QwtPlotLayout *layout = plot()->plotLayout();
 
            for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
            {
 
                d_alignCanvasToScales[axis] = 
src/scrollzoomer.h
Show inline comments
 
@@ -22,67 +22,74 @@ class ScrollZoomer: public QwtPlotZoomer
 
    Q_OBJECT
 
public:
 
    enum ScrollBarPosition
 
    {
 
        AttachedToScale,
 
        OppositeToScale
 
    };
 

	
 
    ScrollZoomer( QWidget * );
 
    virtual ~ScrollZoomer();
 

	
 
    ScrollBar *horizontalScrollBar() const;
 
    ScrollBar *verticalScrollBar() const;
 

	
 
    void setHScrollBarMode( Qt::ScrollBarPolicy );
 
    void setVScrollBarMode( Qt::ScrollBarPolicy );
 

	
 
    Qt::ScrollBarPolicy vScrollBarMode () const;
 
    Qt::ScrollBarPolicy hScrollBarMode () const;
 

	
 
    void setHScrollBarPosition( ScrollBarPosition );
 
    void setVScrollBarPosition( ScrollBarPosition );
 

	
 
    ScrollBarPosition hScrollBarPosition() const;
 
    ScrollBarPosition vScrollBarPosition() const;
 

	
 
    QWidget* cornerWidget() const;
 
    virtual void setCornerWidget( QWidget * );
 

	
 
    virtual bool eventFilter( QObject *, QEvent * );
 

	
 
    void setXLimits(double min, double max);
 
    void setHViewSize(double size);
 
    virtual void setZoomBase(bool doReplot = true);
 
    virtual void rescale();
 

	
 
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;
 
    bool vscrollmove;
 
};
 

	
 
#endif
0 comments (0 inline, 0 general)