Changeset - d09252498ce0
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 8 years ago 2017-08-16 09:06:32
hy@ozderya.net
don't move the x axis when moving vertical scroll

problem happens when x-axis is out of limit, extended zoom selection with rectangle tool
2 files changed with 14 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/scrollzoomer.cpp
Show inline comments
 
@@ -10,209 +10,214 @@
 
#include <qevent.h>
 
#include <qwt_plot_canvas.h>
 
#include <qwt_plot_layout.h>
 
#include <qwt_scale_engine.h>
 
#include <qwt_scale_widget.h>
 
#include "scrollbar.h"
 
#include "scrollzoomer.h"
 

	
 
class ScrollData
 
{
 
public:
 
    ScrollData():
 
        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)
 
{
 
    xMin = min;
 
    xMax = max;
 
    setZoomBase();
 
    // TODO: should we update zoom base here?
 
}
 

	
 
void ScrollZoomer::setHViewSize(double size)
 
{
 
    hViewSize = size;
 
}
 

	
 
void ScrollZoomer::setZoomBase(bool doReplot)
 
{
 
    QwtPlotZoomer::setZoomBase(doReplot);
 
    auto zb = zoomBase();
 
    auto zs = zoomStack();
 
    zb.setRight(xMax);
 
    if ((xMax - xMin) < hViewSize)
 
    {
 
        zb.setLeft(xMin);
 
    }
 
    else
 
    {
 
        zb.setLeft(xMax-hViewSize);
 
    }
 
    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] = 
 
                    layout->alignCanvasToScale( axis );
 
            }
 

	
 
            layout->setAlignCanvasToScales( false );
 

	
 
            d_inZoom = true;
 
        }
 
    }
 

	
 
    // NOTE: Below snippet is copied from QwtPlotZoomer::rescale() just so that
 
    // we can refrain from updating y axis when moving horizontal scrollbar, so
 
    // that auto-scale isn't disrupted.
 
    // that auto-scale isn't disrupted. Also we don't want to jump around in
 
    // x-axis when moving vertical scroll.
 
    {
 
        QwtPlot *plt = plot();
 
        if ( !plt )
 
            return;
 

	
 
        const QRectF &rect = zoomStack()[zoomRectIndex()];
 
        if ( rect != scaleRect() )
 
        {
 
            const bool doReplot = plt->autoReplot();
 
            plt->setAutoReplot( false );
 

	
 
            double x1 = rect.left();
 
            double x2 = rect.right();
 
            if ( !plt->axisScaleDiv( xAxis() ).isIncreasing() )
 
                qSwap( x1, x2 );
 
            if (!vscrollmove)
 
            {
 
                double x1 = rect.left();
 
                double x2 = rect.right();
 
                if ( !plt->axisScaleDiv( xAxis() ).isIncreasing() )
 
                    qSwap( x1, x2 );
 

	
 
            plt->setAxisScale( xAxis(), x1, x2 );
 
                plt->setAxisScale( xAxis(), x1, x2 );
 
            }
 

	
 
            if (!hscrollmove)
 
            {
 
                double y1 = rect.top();
 
                double y2 = rect.bottom();
 
                if ( !plt->axisScaleDiv( yAxis() ).isIncreasing() )
 
                    qSwap( y1, y2 );
 

	
 
                plt->setAxisScale( yAxis(), y1, y2 );
 

	
 
                plt->setAutoReplot( doReplot );
 
            }
 
            plt->replot();
 
        }
 
    }
 
    updateScrollBars();
 
}
 

	
 
ScrollBar *ScrollZoomer::scrollBar( Qt::Orientation orientation )
 
{
 
    ScrollBar *&sb = ( orientation == Qt::Vertical )
 
        ? d_vScrollData->scrollBar : d_hScrollData->scrollBar;
 

	
 
    if ( sb == NULL )
 
    {
 
        sb = new ScrollBar( orientation, canvas() );
 
        sb->hide();
 
        connect( sb,
 
            SIGNAL( valueChanged( Qt::Orientation, double, double ) ),
 
            SLOT( scrollBarMoved( Qt::Orientation, double, double ) ) );
 
    }
 
    return sb;
 
}
 

	
 
ScrollBar *ScrollZoomer::horizontalScrollBar() const
 
{
 
    return d_hScrollData->scrollBar;
 
}
 

	
 
ScrollBar *ScrollZoomer::verticalScrollBar() const
 
{
 
    return d_vScrollData->scrollBar;
 
}
 

	
 
void ScrollZoomer::setHScrollBarMode( Qt::ScrollBarPolicy mode )
 
{
 
    if ( hScrollBarMode() != mode )
 
    {
 
@@ -499,97 +504,99 @@ void ScrollZoomer::layoutScrollBars( con
 
    if ( vScrollBar && vScrollBar->isVisible() )
 
    {
 
        int pos = yAxis();
 
        if ( vScrollBarPosition() == OppositeToScale )
 
            pos = oppositeAxis( pos );
 

	
 
        int x = ( vPos == QwtPlot::yLeft )
 
            ? rect.left() : rect.right() - vdim + 1;
 
        int y = rect.y();
 

	
 
        int h = rect.height();
 

	
 
        if ( hScrollBar && hScrollBar->isVisible() )
 
        {
 
            if ( hPos == QwtPlot::xTop )
 
                y += hdim;
 

	
 
            h -= hdim;
 
        }
 

	
 
        vScrollBar->setGeometry( x, y, vdim, h );
 
    }
 
    if ( hScrollBar && hScrollBar->isVisible() &&
 
        vScrollBar && vScrollBar->isVisible() )
 
    {
 
        if ( d_cornerWidget )
 
        {
 
            QRect cornerRect(
 
                vScrollBar->pos().x(), hScrollBar->pos().y(),
 
                vdim, hdim );
 
            d_cornerWidget->setGeometry( cornerRect );
 
        }
 
    }
 
}
 

	
 
void ScrollZoomer::scrollBarMoved(
 
    Qt::Orientation o, double min, double max )
 
{
 
    Q_UNUSED( max );
 

	
 
    if ( o == Qt::Horizontal )
 
    {
 
        hscrollmove = true;
 
        moveTo( QPointF( min, zoomRect().top() ) );
 
        hscrollmove = false;
 
    }
 
    else
 
    {
 
        vscrollmove = true;
 
        moveTo( QPointF( zoomRect().left(), min ) );
 
        vscrollmove = false;
 
    }
 

	
 
    Q_EMIT zoomed( zoomRect() );
 
}
 

	
 
int ScrollZoomer::oppositeAxis( int axis ) const
 
{
 
    switch( axis )
 
    {
 
        case QwtPlot::xBottom:
 
            return QwtPlot::xTop;
 
        case QwtPlot::xTop:
 
            return QwtPlot::xBottom;
 
        case QwtPlot::yLeft:
 
            return QwtPlot::yRight;
 
        case QwtPlot::yRight:
 
            return QwtPlot::yLeft;
 
        default:
 
            break;
 
    }
 

	
 
    return axis;
 
}
 

	
 
void ScrollZoomer::moveTo( const QPointF &pos )
 
{
 
    // QwtPlotZoomer::moveTo(pos);
 
    // return;
 

	
 
    double x = pos.x();
 
    double y = pos.y();
 

	
 
    if ( x < xMin )
 
        x = xMin;
 
    if ( x > xMax - zoomRect().width() )
 
        x = xMax - zoomRect().width();
 

	
 
    if ( y < zoomBase().top() )
 
        y = zoomBase().top();
 
    if ( y > zoomBase().bottom() - zoomRect().height() )
 
        y = zoomBase().bottom() - zoomRect().height();
 

	
 
    if ( x != zoomRect().left() || y != zoomRect().top() )
 
    {
 
        auto zs = zoomStack();
 
        zs[zoomRectIndex()].moveTo( x, y );
 
        setZoomStack(zs, zoomRectIndex());
 
        rescale();
src/scrollzoomer.h
Show inline comments
 
@@ -37,51 +37,52 @@ public:
 
    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:
 
    QRectF d_limits;
 
    double xMin, xMax;
 
    double hViewSize;
 

	
 
    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)