Skip to content
qwt_dial.cpp 28.3 KiB
Newer Older
pixhawk's avatar
pixhawk committed
    return 360.0 - angle * 180.0 / M_PI;
}

/*!
  Find the value for a given position

  \param pos Position
  \return Value
*/
double QwtDial::getValue(const QPoint &pos)
{
    if ( d_data->maxScaleArc == d_data->minScaleArc || maxValue() == minValue() )
        return minValue();

    double dir = line2Radians(rect().center(), pos) - d_data->origin;
    if ( dir < 0.0 )
        dir += 360.0;

    if ( mode() == RotateScale )
        dir = 360.0 - dir;

    // The position might be in the area that is outside the scale arc.
    // We need the range of the scale if it was a complete circle.

    const double completeCircle = 360.0 / (d_data->maxScaleArc - d_data->minScaleArc)
                                  * (maxValue() - minValue());
pixhawk's avatar
pixhawk committed

    double posValue = minValue() + completeCircle * dir / 360.0;

    if ( scrollMode() == ScrMouse ) {
        if ( d_data->previousDir >= 0.0 ) { // valid direction
pixhawk's avatar
pixhawk committed
            // We have to find out whether the mouse is moving
            // clock or counter clockwise

            bool clockWise = false;

            const double angle = dir - d_data->previousDir;
            if ( (angle >= 0.0 && angle <= 180.0) || angle < -180.0 )
                clockWise = true;

            if ( clockWise ) {
                if ( dir < d_data->previousDir && mouseOffset() > 0.0 ) {
pixhawk's avatar
pixhawk committed
                    // We passed 360 -> 0
                    setMouseOffset(mouseOffset() - completeCircle);
                }

                if ( wrapping() ) {
                    if ( posValue - mouseOffset() > maxValue() ) {
pixhawk's avatar
pixhawk committed
                        // We passed maxValue and the value will be set
                        // to minValue. We have to adjust the mouseOffset.

                        setMouseOffset(posValue - minValue());
                    }
pixhawk's avatar
pixhawk committed
                    if ( posValue - mouseOffset() > maxValue() ||
                            value() == maxValue() ) {
pixhawk's avatar
pixhawk committed
                        // We fix the value at maxValue by adjusting
                        // the mouse offset.

                        setMouseOffset(posValue - maxValue());
                    }
                }
            } else {
                if ( dir > d_data->previousDir && mouseOffset() < 0.0 ) {
                    // We passed 0 -> 360
                    setMouseOffset(mouseOffset() + completeCircle);
pixhawk's avatar
pixhawk committed
                }

                if ( wrapping() ) {
                    if ( posValue - mouseOffset() < minValue() ) {
pixhawk's avatar
pixhawk committed
                        // We passed minValue and the value will be set
                        // to maxValue. We have to adjust the mouseOffset.

                        setMouseOffset(posValue - maxValue());
                    }
pixhawk's avatar
pixhawk committed
                    if ( posValue - mouseOffset() < minValue() ||
                            value() == minValue() ) {
pixhawk's avatar
pixhawk committed
                        // We fix the value at minValue by adjusting
                        // the mouse offset.

                        setMouseOffset(posValue - minValue());
                    }
                }
            }
        }
        d_data->previousDir = dir;
    }

    return posValue;
}

/*!
  \sa QwtAbstractSlider::getScrollMode
*/
void QwtDial::getScrollMode(const QPoint &p, int &scrollMode, int &direction)
{
    direction = 0;
    scrollMode = ScrNone;

    const QRegion region(contentsRect(), QRegion::Ellipse);
    if ( region.contains(p) && p != rect().center() ) {
pixhawk's avatar
pixhawk committed
        scrollMode = ScrMouse;
        d_data->previousDir = -1.0;
    }
}

pixhawk's avatar
pixhawk committed
  Handles key events

  - Key_Down, KeyLeft\n
    Decrement by 1
  - Key_Prior\n
    Decrement by pageSize()
  - Key_Home\n
    Set the value to minValue()

  - Key_Up, KeyRight\n
    Increment by 1
  - Key_Next\n
    Increment by pageSize()
  - Key_End\n
    Set the value to maxValue()

  \sa isReadOnly()
*/
void QwtDial::keyPressEvent(QKeyEvent *e)
{
    if ( isReadOnly() ) {
pixhawk's avatar
pixhawk committed
        e->ignore();
        return;
    }

    if ( !isValid() )
        return;

    double previous = prevValue();
    switch ( e->key() ) {
    case Qt::Key_Down:
    case Qt::Key_Left:
        QwtDoubleRange::incValue(-1);
        break;
pixhawk's avatar
pixhawk committed
#if QT_VERSION < 0x040000
    case Qt::Key_Prior:
pixhawk's avatar
pixhawk committed
#else
    case Qt::Key_PageUp:
pixhawk's avatar
pixhawk committed
#endif
        QwtDoubleRange::incValue(-pageSize());
        break;
    case Qt::Key_Home:
        setValue(minValue());
        break;

    case Qt::Key_Up:
    case Qt::Key_Right:
        QwtDoubleRange::incValue(1);
        break;
pixhawk's avatar
pixhawk committed
#if QT_VERSION < 0x040000
    case Qt::Key_Next:
pixhawk's avatar
pixhawk committed
#else
    case Qt::Key_PageDown:
pixhawk's avatar
pixhawk committed
#endif
        QwtDoubleRange::incValue(pageSize());
        break;
    case Qt::Key_End:
        setValue(maxValue());
        break;
    default:
        ;
        e->ignore();
pixhawk's avatar
pixhawk committed
    }

    if (value() != previous)
        emit sliderMoved(value());
}

/*!
   \brief Update the mask of the dial

   In case of "hasVisibleBackground() == false", the backgound is
   transparent by a mask.

   \sa showBackground(), hasVisibleBackground()
*/
void QwtDial::updateMask()
{
    if ( d_data->visibleBackground )
        clearMask();
    else
        setMask(QRegion(boundingRect(), QRegion::Ellipse));
}