Changeset - 092cfd95f0ce
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 8 years ago 2017-08-13 12:09:25
hy@ozderya.net
retain y axis autoscale when moving horizontal scrollbar
2 files changed with 43 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/scrollzoomer.cpp
Show inline comments
 
@@ -33,48 +33,49 @@ public:
 
    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 )
 
{
 
    d_limits = QRectF(0, -10, 10000, 20);
 
    xMin = 0.;
 
    xMax = 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;
 
}
 

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

	
 
void ScrollZoomer::rescale()
 
{
 
    qDebug() << "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();
 
@@ -97,49 +98,83 @@ void ScrollZoomer::rescale()
 
             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;
 
        }
 
    }
 

	
 
    QwtPlotZoomer::rescale();
 
    // NOTE: Below snipped 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 distrupted.
 
    {
 
        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 );
 

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

	
 
@@ -457,51 +492,57 @@ void ScrollZoomer::layoutScrollBars( con
 
            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
 
    {
 
        moveTo( QPointF( zoomRect().left(), min ) );
 
    }
 

	
 
    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 )
src/scrollzoomer.h
Show inline comments
 
@@ -58,27 +58,28 @@ 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;
 

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

	
 
#endif
0 comments (0 inline, 0 general)