Skip to content
qwt_wheel.cpp 29.4 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

Bryant's avatar
Bryant committed
#include "qwt_wheel.h"
#include "qwt_math.h"
#include "qwt_painter.h"
pixhawk's avatar
pixhawk committed
#include <qevent.h>
#include <qdrawutil.h>
#include <qpainter.h>
#include <qstyle.h>
Bryant's avatar
Bryant committed
#include <qstyleoption.h>
#include <qapplication.h>
#include <qdatetime.h>

#if QT_VERSION < 0x040601
#define qFabs(x) ::fabs(x)
#define qFastSin(x) ::sin(x)
#define qExp(x) ::exp(x)
#endif
pixhawk's avatar
pixhawk committed

class QwtWheel::PrivateData
{
public:
Bryant's avatar
Bryant committed
    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 )
    {
pixhawk's avatar
pixhawk committed
    };

Bryant's avatar
Bryant committed
    Qt::Orientation orientation;
pixhawk's avatar
pixhawk committed
    double viewAngle;
    double totalAngle;
Bryant's avatar
Bryant committed
    int tickCount;
    int wheelBorderWidth;
pixhawk's avatar
pixhawk committed
    int borderWidth;
    int wheelWidth;
Bryant's avatar
Bryant committed

    bool isScrolling;
    double mouseOffset;

    bool tracking;
    bool pendingValueChanged; // when not tracking

    int updateInterval;
    double mass;

    // for the flying wheel effect
    int timerId;
    QTime time;
    double speed;
    double mouseValue;
    double flyingValue;

    double minimum;
    double maximum;

    double singleStep;
    int pageStepCount;
    bool stepAlignment;

    double value;

    bool inverted;
    bool wrapping;
pixhawk's avatar
pixhawk committed
};

//! Constructor
Bryant's avatar
Bryant committed
QwtWheel::QwtWheel( QWidget *parent ):
    QWidget( parent )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    d_data = new PrivateData;

    setFocusPolicy( Qt::StrongFocus );
    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
    setAttribute( Qt::WA_WState_OwnSizePolicy, false );
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
//! Destructor
QwtWheel::~QwtWheel()
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    delete d_data;
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
/*!
  \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 id tracking is enabled or not.

  \param enable On/Off
  \sa isTracking()
 */
void QwtWheel::setTracking( bool enable )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    d_data->tracking = enable;
}
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
/*!
  \return True, when tracking is enabled
  \sa setTracking(), valueChanged(), wheelMoved()
*/
bool QwtWheel::isTracking() const
{
    return d_data->tracking;
}
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
/*!
  \brief Specify the update interval when the wheel is flying
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  Default and minimum value is 50 ms.
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  \param interval Interval in milliseconds
  \sa updateInterval(), setMass(), setTracking()
*/
void QwtWheel::setUpdateInterval( int interval )
{
    d_data->updateInterval = qMax( interval, 50 );
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
/*!
  \return Update interval when the wheel is flying
  \sa setUpdateInterval(), mass(), isTracking()
 */
int QwtWheel::updateInterval() const
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    return d_data->updateInterval;
}

/*!
   \brief Mouse press event handler

   Start movement of the wheel. 

   \param event Mouse event
*/
void QwtWheel::mousePressEvent( QMouseEvent *event )
{
    stopFlying();

    d_data->isScrolling = wheelRect().contains( event->pos() );

    if ( d_data->isScrolling )
    {
        d_data->time.start();
        d_data->speed = 0.0;
        d_data->mouseValue = valueAt( event->pos() );
        d_data->mouseOffset = d_data->mouseValue - d_data->value;
        d_data->pendingValueChanged = false;

        Q_EMIT wheelPressed();
    }
pixhawk's avatar
pixhawk committed
}

Bryant's avatar
Bryant committed
/*!
   \brief Mouse Move Event handler

   Turn the wheel according to the mouse position

   \param event Mouse event
*/
void QwtWheel::mouseMoveEvent( QMouseEvent *event )
pixhawk's avatar
pixhawk committed
{
Bryant's avatar
Bryant committed
    if ( !d_data->isScrolling )
pixhawk's avatar
pixhawk committed
        return;

Bryant's avatar
Bryant committed
    double mouseValue = valueAt( event->pos() );

    if ( d_data->mass > 0.0 )
Loading
Loading full blame...