Changeset - 02317c2e271d
[Not reviewed]
scrollbar
0 4 0
Hasan Yavuz ÖZDERYA - 9 years ago 2016-10-29 15:16:31
hy@ozderya.net
add copyright header to the files copied from qwt
4 files changed with 36 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/scrollbar.cpp
Show inline comments
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#include <qstyle.h>
 
#include <qstyleoption.h>
 
#include "scrollbar.h"
 

	
 
ScrollBar::ScrollBar( QWidget * parent ):
 
    QScrollBar( parent )
 
{
 
    init();
 
}
 

	
 
ScrollBar::ScrollBar( Qt::Orientation o,
 
        QWidget *parent ):
 
    QScrollBar( o, parent )
 
{
 
    init();
 
}
 

	
 
ScrollBar::ScrollBar( double minBase, double maxBase,
 
        Qt::Orientation o, QWidget *parent ):
 
    QScrollBar( o, parent )
 
{
 
    init();
 
    setBase( minBase, maxBase );
 
    moveSlider( minBase, maxBase );
 
}
 

	
 
void ScrollBar::init()
 
{
 
    d_inverted = orientation() == Qt::Vertical;
 
    d_baseTicks = 1000000;
 
    d_minBase = 0.0;
 
    d_maxBase = 1.0;
 
    moveSlider( d_minBase, d_maxBase );
 

	
 
    connect( this, SIGNAL( sliderMoved( int ) ), SLOT( catchSliderMoved( int ) ) );
 
    connect( this, SIGNAL( valueChanged( int ) ), SLOT( catchValueChanged( int ) ) );
 
}
 

	
 
void ScrollBar::setInverted( bool inverted )
 
{
 
    if ( d_inverted != inverted )
 
    {
 
        d_inverted = inverted;
 
        moveSlider( minSliderValue(), maxSliderValue() );
 
    }
 
}
 

	
 
bool ScrollBar::isInverted() const
 
{
 
    return d_inverted;
 
}
 

	
 
void ScrollBar::setBase( double min, double max )
 
{
 
    if ( min != d_minBase || max != d_maxBase )
 
    {
 
        d_minBase = min;
 
        d_maxBase = max;
 

	
 
        moveSlider( minSliderValue(), maxSliderValue() );
 
    }
 
}
 

	
 
void ScrollBar::moveSlider( double min, double max )
 
{
 
    const int sliderTicks = qRound( ( max - min ) /
 
        ( d_maxBase - d_minBase ) * d_baseTicks );
 

	
 
    // setRange initiates a valueChanged of the scrollbars
 
    // in some situations. So we block
 
    // and unblock the signals.
 

	
 
    blockSignals( true );
 

	
 
    setRange( sliderTicks / 2, d_baseTicks - sliderTicks / 2 );
 
    int steps = sliderTicks / 200;
 
    if ( steps <= 0 )
 
        steps = 1;
 

	
 
    setSingleStep( steps );
 
    setPageStep( sliderTicks );
 

	
 
    int tick = mapToTick( min + ( max - min ) / 2 );
 
    if ( isInverted() )
 
        tick = d_baseTicks - tick;
 

	
 
    setSliderPosition( tick );
 
    blockSignals( false );
 
}
 

	
 
double ScrollBar::minBaseValue() const
 
{
 
    return d_minBase;
 
}
 

	
 
double ScrollBar::maxBaseValue() const
 
{
 
    return d_maxBase;
 
}
 

	
 
void ScrollBar::sliderRange( int value, double &min, double &max ) const
 
{
 
    if ( isInverted() )
 
        value = d_baseTicks - value;
 

	
 
    const int visibleTicks = pageStep();
 

	
 
    min = mapFromTick( value - visibleTicks / 2 );
 
    max = mapFromTick( value + visibleTicks / 2 );
 
}
 

	
 
double ScrollBar::minSliderValue() const
 
{
 
    double min, dummy;
 
    sliderRange( value(), min, dummy );
 

	
 
    return min;
 
}
 

	
 
double ScrollBar::maxSliderValue() const
 
{
 
    double max, dummy;
 
    sliderRange( value(), dummy, max );
 

	
 
    return max;
 
}
 

	
 
int ScrollBar::mapToTick( double v ) const
 
{
 
    const double pos = ( v - d_minBase ) / ( d_maxBase - d_minBase ) * d_baseTicks;
 
    return static_cast<int>( pos );
 
}
 

	
 
double ScrollBar::mapFromTick( int tick ) const
 
{
 
    return d_minBase + ( d_maxBase - d_minBase ) * tick / d_baseTicks;
 
}
 

	
 
void ScrollBar::catchValueChanged( int value )
 
{
 
    double min, max;
 
    sliderRange( value, min, max );
 
    Q_EMIT valueChanged( orientation(), min, max );
 
}
 

	
 
void ScrollBar::catchSliderMoved( int value )
 
{
 
    double min, max;
 
    sliderRange( value, min, max );
 
    Q_EMIT sliderMoved( orientation(), min, max );
 
}
 

	
 
int ScrollBar::extent() const
 
{
 
    QStyleOptionSlider opt;
 
    opt.init( this );
 
    opt.subControls = QStyle::SC_None;
 
    opt.activeSubControls = QStyle::SC_None;
 
    opt.orientation = orientation();
 
    opt.minimum = minimum();
 
    opt.maximum = maximum();
 
    opt.sliderPosition = sliderPosition();
 
    opt.sliderValue = value();
 
    opt.singleStep = singleStep();
 
    opt.pageStep = pageStep();
 
    opt.upsideDown = invertedAppearance();
 
    if ( orientation() == Qt::Horizontal )
 
        opt.state |= QStyle::State_Horizontal;
 
    return style()->pixelMetric( QStyle::PM_ScrollBarExtent, &opt, this );
 
}
src/scrollbar.h
Show inline comments
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#ifndef _SCROLLBAR_H
 
#define _SCROLLBAR_H 1
 

	
 
#include <qscrollbar.h>
 

	
 
class ScrollBar: public QScrollBar
 
{
 
    Q_OBJECT
 

	
 
public:
 
    ScrollBar( QWidget *parent = NULL );
 
    ScrollBar( Qt::Orientation, QWidget *parent = NULL );
 
    ScrollBar( double minBase, double maxBase,
 
        Qt::Orientation o, QWidget *parent = NULL );
 

	
 
    void setInverted( bool );
 
    bool isInverted() const;
 

	
 
    double minBaseValue() const;
 
    double maxBaseValue() const;
 

	
 
    double minSliderValue() const;
 
    double maxSliderValue() const;
 

	
 
    int extent() const;
 

	
 
Q_SIGNALS:
 
    void sliderMoved( Qt::Orientation, double, double );
 
    void valueChanged( Qt::Orientation, double, double );
 

	
 
public Q_SLOTS:
 
    virtual void setBase( double min, double max );
 
    virtual void moveSlider( double min, double max );
 

	
 
protected:
 
    void sliderRange( int value, double &min, double &max ) const;
 
    int mapToTick( double ) const;
 
    double mapFromTick( int ) const;
 

	
 
private Q_SLOTS:
 
    void catchValueChanged( int value );
 
    void catchSliderMoved( int value );
 

	
 
private:
 
    void init();
 

	
 
    bool d_inverted;
 
    double d_minBase;
 
    double d_maxBase;
 
    int d_baseTicks;
 
};
 

	
 
#endif
src/scrollzoomer.cpp
Show inline comments
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#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 )
 
{
 
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
        d_alignCanvasToScales[ axis ] = false;
 

	
 
    if ( !canvas )
 
        return;
 

	
 
    d_hScrollData = new ScrollData;
 
    d_vScrollData = new ScrollData;
 
}
 

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

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

	
 
    QwtPlotZoomer::rescale();
 
    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 )
 
    {
 
        d_hScrollData->mode = mode;
 
        updateScrollBars();
 
    }
 
}
 

	
 
void ScrollZoomer::setVScrollBarMode( Qt::ScrollBarPolicy mode )
 
{
 
    if ( vScrollBarMode() != mode )
 
    {
 
        d_vScrollData->mode = mode;
 
        updateScrollBars();
 
    }
 
}
 

	
 
Qt::ScrollBarPolicy ScrollZoomer::hScrollBarMode() const
 
{
 
    return d_hScrollData->mode;
 
}
 

	
 
Qt::ScrollBarPolicy ScrollZoomer::vScrollBarMode() const
 
{
 
    return d_vScrollData->mode;
 
}
 

	
 
void ScrollZoomer::setHScrollBarPosition( ScrollBarPosition pos )
 
{
 
    if ( d_hScrollData->position != pos )
 
    {
 
        d_hScrollData->position = pos;
 
        updateScrollBars();
 
    }
 
}
 

	
 
void ScrollZoomer::setVScrollBarPosition( ScrollBarPosition pos )
 
{
 
    if ( d_vScrollData->position != pos )
 
    {
 
        d_vScrollData->position = pos;
 
        updateScrollBars();
 
    }
 
}
 

	
 
ScrollZoomer::ScrollBarPosition ScrollZoomer::hScrollBarPosition() const
 
{
 
    return d_hScrollData->position;
 
}
 

	
 
ScrollZoomer::ScrollBarPosition ScrollZoomer::vScrollBarPosition() const
 
{
 
    return d_vScrollData->position;
 
}
 

	
 
void ScrollZoomer::setCornerWidget( QWidget *w )
src/scrollzoomer.h
Show inline comments
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#ifndef _SCROLLZOOMER_H
 
#define _SCROLLZOOMER_H
 

	
 
#include <qglobal.h>
 
#include <qwt_plot_zoomer.h>
 
#include <qwt_plot.h>
 

	
 
class ScrollData;
 
class ScrollBar;
 

	
 
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 * );
 

	
 
    virtual void rescale();
 

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

	
 
#endif
0 comments (0 inline, 0 general)