Qwt源码解读之QwtWheel类

来源:互联网 发布:大连电视直播软件 编辑:程序博客网 时间:2024/04/29 03:56

先看一看QWT文档说明

The Wheel Widget.

The wheel widget can be used to change values over a very large range in very small steps. Using the setMass() member, it can be configured as a flying wheel.(如果设置setMass,则释放鼠标后,旋轮仍然会旋转一会,就像具有一定的惯性)

The default range of the wheel is [0.0, 100.0]

See Also
The radio example.(可以参考这个例子radio)

类继承关系

如下图所示


一、属性数据

class QwtWheel::PrivateData{public:    PrivateData():        orientation( Qt::Horizontal ),        viewAngle( 175.0 ),        totalAngle( 360.0 ),        tickCount( 10 ),        wheelBorderWidth( 2 ),        borderWidth( 2 ),        wheelWidth( 20 ),        isScrolling( false ),        mouseOffset( 0.0 ),        tracking( true ),        pendingValueChanged( false ),        updateInterval( 50 ),        mass( 0.0 ),        timerId( 0 ),        speed( 0.0 ),        mouseValue( 0.0 ),        flyingValue( 0.0 ),        minimum( 0.0 ),        maximum( 100.0 ),        singleStep( 1.0 ),        pageStepCount( 1 ),        stepAlignment( true ),        value( 0.0 ),        inverted( false ),        wrapping( false )    {    };    Qt::Orientation orientation;// 方向,是水平放置还是垂直放置    double viewAngle;// 可见角度,即在界面中可以看见的部分的角度    double totalAngle;// 总共可以旋转的角度    int tickCount;//旋轮表面的凹槽数量,tickCount个凹槽代表旋轮的一圈    int wheelBorderWidth;    int borderWidth;    int wheelWidth;    bool isScrolling;    double mouseOffset;    bool tracking;// 旋轮正在旋转时是否发送valueChanged()信号    bool pendingValueChanged; // when not tracking    int updateInterval;// 鼠标释放后旋轮仍在旋转,旋轮当前值更新的时间间隔,单位:毫秒    double mass;// 旋轮的质量,mass值越大,则表示鼠标释放后的惯性越大,即飞轮效应越明显    // for the flying wheel effect    int timerId;// 定时器ID    QTime time;// 用于计时两次旋轮值改变的时间间隔    double speed;//旋轮值变化的速度,在飞轮效应中该值按指数规律递减    double mouseValue;// 鼠标当前位置相对于旋轮可视的起始位置的旋轮差值,注意,不是相对于旋轮的最小值。    double flyingValue;// 旋轮起飞的起始值    double minimum;// 最小值    double maximum;// 最大值    double singleStep;// 单步长    int pageStepCount;// 单步长的倍数,当按下PageUp或者PageDown时旋轮变化pageStepCount*singleStep    bool stepAlignment;//是否对齐,如果对齐,则修改value为singleStep的倍数    double value;    bool inverted;    bool wrapping;};

二、构造函数

//! ConstructorQwtWheel::QwtWheel( QWidget *parent ):    QWidget( parent ){    d_data = new PrivateData;    setFocusPolicy( Qt::StrongFocus );    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );    setAttribute( Qt::WA_WState_OwnSizePolicy, false );}

三、一些与属性获取/设置相关的函数

旋轮的可视角度

/*!  \return Visible portion of the wheel  \sa setViewAngle(), totalAngle()*/double QwtWheel::viewAngle() const{    return d_data->viewAngle;}
/*!  \brief Specify the visible portion of the wheel.  You may use this function for fine-tuning the appearance of  the wheel. The default value is 175 degrees. The value is  limited from 10 to 175 degrees.  \param angle Visible angle in degrees  \sa viewAngle(), setTotalAngle()*/void QwtWheel::setViewAngle( double angle ){    d_data->viewAngle = qBound( 10.0, angle, 175.0 );    update();}

该函数限制旋轮的可视角度在10~175度之间。

旋轮总共可以旋转的角度

/*!  \return Total angle which the wheel can be turned.  \sa setTotalAngle()*/double QwtWheel::totalAngle() const{    return d_data->totalAngle;}
/*!  \brief Set the total angle which the wheel can be turned.  One full turn of the wheel corresponds to an angle of  360 degrees. A total angle of n*360 degrees means  that the wheel has to be turned n times around its axis  to get from the minimum value to the maximum value.  The default setting of the total angle is 360 degrees.  \param angle total angle in degrees  \sa totalAngle()*/void QwtWheel::setTotalAngle( double angle ){    if ( angle < 0.0 )        angle = 0.0;    d_data->totalAngle = angle;    update();}

angle = n*360,表示从最小值旋转到最大值,需要旋转n圈。默认angle=360,表示只需旋转一圈就可以从最小值旋转到最大值。

旋轮的方向

/*!  \return Orientation  \sa setOrientation()*/Qt::Orientation QwtWheel::orientation() const{    return d_data->orientation;}
/*!  \brief Set the wheel's orientation.  The default orientation is Qt::Horizontal.  \param orientation Qt::Horizontal or Qt::Vertical.  \sa orientation()*/void QwtWheel::setOrientation( Qt::Orientation orientation ){    if ( d_data->orientation == orientation )        return;    if ( !testAttribute( Qt::WA_WState_OwnSizePolicy ) )    {        QSizePolicy sp = sizePolicy();        sp.transpose();        setSizePolicy( sp );        setAttribute( Qt::WA_WState_OwnSizePolicy, false );    }    d_data->orientation = orientation;    update();}

设置旋轮值的范围

/*!  \brief Set the minimum and maximum values  The maximum is adjusted if necessary to ensure that the range remains valid.  The value might be modified to be inside of the range.  \param min Minimum value  \param max Maximum value  \sa minimum(), maximum() */void QwtWheel::setRange( double min, double max ){    max = qMax( min, max );    if ( d_data->minimum == min && d_data->maximum == max )        return;    d_data->minimum = min;    d_data->maximum = max;    if ( d_data->value < min || d_data->value > max )    {        d_data->value = qBound( min, d_data->value, max );        update();        Q_EMIT valueChanged( d_data->value );    }}
为保证值的范围合法,可能会调整max值。

如果当前value不在该范围之内,则会修改当前value的大小,并且发射valueChanged信号。

步长

包括singleStep和pageStepCount。

/*!  \brief Set the page step count        pageStepCount is a multiplicator for the single step size  that typically corresponds to the user pressing PageUp or PageDown.      A value of 0 disables page stepping.   The default value is 1.  \param count Multiplicator for the single step size  \sa pageStepCount(), setSingleStep() */void QwtWheel::setPageStepCount( int count ){    d_data->pageStepCount = qMax( 0, count );}
pageStepCount表示按下PageUp或者PageDown 时旋轮值变化pageStepCount*singleStep。

/*!  \brief En/Disable tracking  If tracking is enabled (the default), the wheel emits the valueChanged()   signal while the wheel is moving. If tracking is disabled, the wheel   emits the valueChanged() signal only when the wheel movement is terminated.  The wheelMoved() signal is emitted regardless if tracking is enabled or not.  \param enable On/Off  \sa isTracking() */void QwtWheel::setTracking( bool enable ){    d_data->tracking = enable;}
使能/禁用tracking属性

如果tracking属性被设置,则当旋轮正在旋转时也发射valueChanged()信号,如果tracking属性==false,则只有在旋轮停止旋转之后才发射valueChanged()信号。

注意:不论tracking取何值,都可以发射信号wheelMoved()。

/*!  \brief Specify the update interval when the wheel is flying  Default and minimum value is 50 ms.  \param interval Interval in milliseconds  \sa updateInterval(), setMass(), setTracking()*/void QwtWheel::setUpdateInterval( int interval ){    d_data->updateInterval = qMax( interval, 50 );}
指定值更新的间隔时间,用于设置旋轮在鼠标释放后仍然在旋转时的值更新频率,间隔时间最短50ms。
/*!  \brief Set the slider's mass for flywheel effect(飞轮效应).  If the slider's mass is greater then 0, it will continue  to move after the mouse button has been released. Its speed  decreases with time at a rate depending on the slider's mass.  A large mass means that it will continue to move for a  long time.  Derived widgets may overload this function to make it public.  \param mass New mass in kg  \bug If the mass is smaller than 1g, it is set to zero.       The maximal mass is limited to 100kg.  \sa mass()*/void QwtWheel::setMass( double mass ){    if ( mass < 0.001 )    {        d_data->mass = 0.0;    }    else    {        d_data->mass = qMin( 100.0, mass );    }    if ( d_data->mass <= 0.0 )        stopFlying();}
mass值关系到当鼠标释放后旋轮还能旋转多长时间。mass越大,则鼠标释放后,旋轮还能持续旋转的时间越久,即飞轮效应越明显。
/*!  \brief Adjust the number of grooves in the wheel's surface.  The number of grooves is limited to 6 <= count <= 50.  Values outside this range will be clipped.  The default value is 10.  \param count Number of grooves per 360 degrees  \sa tickCount()*/void QwtWheel::setTickCount( int count ){    count = qBound( 6, count, 50 );    if ( count != d_data->tickCount )    {        d_data->tickCount = qBound( 6, count, 50 );        update();    }}
设置旋轮表面的凹槽数量。限制凹槽数量范围[6, 50]。当旋轮旋转一圈(即360度)时旋轮表面经过count个凹槽。

四、一些信号

    /*!      \brief Notify a change of value.      When tracking is enabled this signal will be emitted every      time the value changes.       \param value new value      \sa setTracking()    */    void valueChanged( double value );
通知当前值改变。当tracking使能,则每当值改变时都发射信号。
    /*!      This signal is emitted when the user presses the      the wheel with the mouse    */    void wheelPressed();
当用鼠标按下旋轮时发射此信号。参见函数 voidmousePressEvent(QMouseEvent*)

    /*!      This signal is emitted when the user releases the mouse    */    void wheelReleased();
当释放鼠标时发射此信号。参见函数 voidmouseReleaseEvent(QMouseEvent*)

    /*!      This signal is emitted when the user moves the      wheel with the mouse.      \param value new value    */    void wheelMoved( double value );
当用鼠标旋转旋轮时发射此信号。参见函数 voidmouseMoveEvent(QMouseEvent*)



0 0
原创粉丝点击