Skip to content
Snippets Groups Projects
qwt_magnifier.cpp 11.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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_magnifier.h"
    #include "qwt_math.h"
    
    pixhawk's avatar
    pixhawk committed
    #include <qevent.h>
    #include <qwidget.h>
    
    class QwtMagnifier::PrivateData
    {
    public:
        PrivateData():
    
    Bryant's avatar
    Bryant committed
            isEnabled( false ),
            wheelFactor( 0.9 ),
            wheelModifiers( Qt::NoModifier ),
            mouseFactor( 0.95 ),
            mouseButton( Qt::RightButton ),
            mouseButtonModifiers( Qt::NoModifier ),
            keyFactor( 0.9 ),
            zoomInKey( Qt::Key_Plus ),
            zoomInKeyModifiers( Qt::NoModifier ),
            zoomOutKey( Qt::Key_Minus ),
            zoomOutKeyModifiers( Qt::NoModifier ),
            mousePressed( false )
        {
    
    pixhawk's avatar
    pixhawk committed
        }
    
        bool isEnabled;
    
        double wheelFactor;
    
    Bryant's avatar
    Bryant committed
        Qt::KeyboardModifiers wheelModifiers;
    
    pixhawk's avatar
    pixhawk committed
    
        double mouseFactor;
    
    Bryant's avatar
    Bryant committed
    
        Qt::MouseButton mouseButton;
        Qt::KeyboardModifiers mouseButtonModifiers;
    
    pixhawk's avatar
    pixhawk committed
    
        double keyFactor;
    
    Bryant's avatar
    Bryant committed
    
    
    pixhawk's avatar
    pixhawk committed
        int zoomInKey;
    
    Bryant's avatar
    Bryant committed
        Qt::KeyboardModifiers zoomInKeyModifiers;
    
    
    pixhawk's avatar
    pixhawk committed
        int zoomOutKey;
    
    Bryant's avatar
    Bryant committed
        Qt::KeyboardModifiers  zoomOutKeyModifiers;
    
    pixhawk's avatar
    pixhawk committed
    
        bool mousePressed;
        bool hasMouseTracking;
        QPoint mousePos;
    };
    
    
    pixhawk's avatar
    pixhawk committed
       Constructor
       \param parent Widget to be magnified
    */
    
    Bryant's avatar
    Bryant committed
    QwtMagnifier::QwtMagnifier( QWidget *parent ):
        QObject( parent )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data = new PrivateData();
    
    Bryant's avatar
    Bryant committed
        setEnabled( true );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    //! Destructor
    QwtMagnifier::~QwtMagnifier()
    {
        delete d_data;
    }
    
    /*!
      \brief En/disable the magnifier
    
      When enabled is true an event filter is installed for
      the observed widget, otherwise the event filter is removed.
    
      \param on true or false
      \sa isEnabled(), eventFilter()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setEnabled( bool on )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        if ( d_data->isEnabled != on )
        {
    
    pixhawk's avatar
    pixhawk committed
            d_data->isEnabled = on;
    
            QObject *o = parent();
    
    Bryant's avatar
    Bryant committed
            if ( o )
            {
    
    pixhawk's avatar
    pixhawk committed
                if ( d_data->isEnabled )
    
    Bryant's avatar
    Bryant committed
                    o->installEventFilter( this );
    
    pixhawk's avatar
    pixhawk committed
                else
    
    Bryant's avatar
    Bryant committed
                    o->removeEventFilter( this );
    
    pixhawk's avatar
    pixhawk committed
            }
        }
    }
    
    /*!
      \return true when enabled, false otherwise
    
    Bryant's avatar
    Bryant committed
      \sa setEnabled(), eventFilter()
    
    pixhawk's avatar
    pixhawk committed
    */
    bool QwtMagnifier::isEnabled() const
    {
        return d_data->isEnabled;
    }
    
    /*!
       \brief Change the wheel factor
    
       The wheel factor defines the ratio between the current range
       on the parent widget and the zoomed range for each step of the wheel.
    
    Bryant's avatar
    Bryant committed
    
       Use values > 1 for magnification (i.e. 2.0) and values < 1 for
       scaling down (i.e. 1/2.0 = 0.5). You can use this feature for
       inverting the direction of the wheel.
    
    
    pixhawk's avatar
    pixhawk committed
       The default value is 0.9.
    
    pixhawk's avatar
    pixhawk committed
       \param factor Wheel factor
    
       \sa wheelFactor(), setWheelButtonState(),
    
    pixhawk's avatar
    pixhawk committed
           setMouseFactor(), setKeyFactor()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setWheelFactor( double factor )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data->wheelFactor = factor;
    }
    
    /*!
       \return Wheel factor
       \sa setWheelFactor()
    */
    double QwtMagnifier::wheelFactor() const
    {
        return d_data->wheelFactor;
    }
    
    /*!
    
    Bryant's avatar
    Bryant committed
       Assign keyboard modifiers for zooming in/out using the wheel.
       The default modifiers are Qt::NoModifiers.
    
    pixhawk's avatar
    pixhawk committed
    
    
    Bryant's avatar
    Bryant committed
       \param modifiers Keyboard modifiers
       \sa wheelModifiers()
    
    pixhawk's avatar
    pixhawk committed
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        d_data->wheelModifiers = modifiers;
    
    pixhawk's avatar
    pixhawk committed
    }
    
    /*!
    
    Bryant's avatar
    Bryant committed
       \return Wheel modifiers
       \sa setWheelModifiers()
    
    pixhawk's avatar
    pixhawk committed
    */
    
    Bryant's avatar
    Bryant committed
    Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        return d_data->wheelModifiers;
    
    pixhawk's avatar
    pixhawk committed
    }
    
    
    pixhawk's avatar
    pixhawk committed
       \brief Change the mouse factor
    
       The mouse factor defines the ratio between the current range
       on the parent widget and the zoomed range for each vertical mouse movement.
       The default value is 0.95.
    
    pixhawk's avatar
    pixhawk committed
       \param factor Wheel factor
       \sa mouseFactor(), setMouseButton(), setWheelFactor(), setKeyFactor()
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setMouseFactor( double factor )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data->mouseFactor = factor;
    }
    
    /*!
       \return Mouse factor
       \sa setMouseFactor()
    */
    double QwtMagnifier::mouseFactor() const
    {
        return d_data->mouseFactor;
    }
    
    /*!
       Assign the mouse button, that is used for zooming in/out.
       The default value is Qt::RightButton.
    
       \param button Button
    
    Bryant's avatar
    Bryant committed
       \param modifiers Keyboard modifiers
    
       \sa getMouseButton()
    
    pixhawk's avatar
    pixhawk committed
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setMouseButton( 
        Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data->mouseButton = button;
    
    Bryant's avatar
    Bryant committed
        d_data->mouseButtonModifiers = modifiers;
    
    pixhawk's avatar
    pixhawk committed
    }
    
    
    Bryant's avatar
    Bryant committed
    //! \sa setMouseButton()
    
    pixhawk's avatar
    pixhawk committed
    void QwtMagnifier::getMouseButton(
    
    Bryant's avatar
    Bryant committed
        Qt::MouseButton &button, Qt::KeyboardModifiers &modifiers ) const
    
    pixhawk's avatar
    pixhawk committed
    {
        button = d_data->mouseButton;
    
    Bryant's avatar
    Bryant committed
        modifiers = d_data->mouseButtonModifiers;
    
    pixhawk's avatar
    pixhawk committed
    }
    
    /*!
       \brief Change the key factor
    
       The key factor defines the ratio between the current range
       on the parent widget and the zoomed range for each key press of
       the zoom in/out keys. The default value is 0.9.
    
    pixhawk's avatar
    pixhawk committed
       \param factor Key factor
       \sa keyFactor(), setZoomInKey(), setZoomOutKey(),
           setWheelFactor, setMouseFactor()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setKeyFactor( double factor )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data->keyFactor = factor;
    }
    
    /*!
       \return Key factor
       \sa setKeyFactor()
    */
    double QwtMagnifier::keyFactor() const
    {
        return d_data->keyFactor;
    }
    
    /*!
       Assign the key, that is used for zooming in.
       The default combination is Qt::Key_Plus + Qt::NoModifier.
    
       \param key
       \param modifiers
       \sa getZoomInKey(), setZoomOutKey()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setZoomInKey( int key, 
        Qt::KeyboardModifiers modifiers )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data->zoomInKey = key;
        d_data->zoomInKeyModifiers = modifiers;
    }
    
    
    Bryant's avatar
    Bryant committed
    /*! 
       \brief Retrieve the settings of the zoom in key
    
       \param key Key code, see Qt::Key
       \param modifiers Keyboard modifiers
    
       \sa setZoomInKey()
    */
    void QwtMagnifier::getZoomInKey( int &key, 
        Qt::KeyboardModifiers &modifiers ) const
    
    pixhawk's avatar
    pixhawk committed
    {
        key = d_data->zoomInKey;
        modifiers = d_data->zoomInKeyModifiers;
    }
    
    /*!
       Assign the key, that is used for zooming out.
       The default combination is Qt::Key_Minus + Qt::NoModifier.
    
       \param key
       \param modifiers
       \sa getZoomOutKey(), setZoomOutKey()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::setZoomOutKey( int key, 
        Qt::KeyboardModifiers modifiers )
    
    pixhawk's avatar
    pixhawk committed
    {
        d_data->zoomOutKey = key;
        d_data->zoomOutKeyModifiers = modifiers;
    }
    
    
    Bryant's avatar
    Bryant committed
    /*! 
       \brief Retrieve the settings of the zoom out key
    
       \param key Key code, see Qt::Key
       \param modifiers Keyboard modifiers
    
       \sa setZoomOutKey()
    */
    void QwtMagnifier::getZoomOutKey( int &key, 
        Qt::KeyboardModifiers &modifiers ) const
    
    pixhawk's avatar
    pixhawk committed
    {
        key = d_data->zoomOutKey;
        modifiers = d_data->zoomOutKeyModifiers;
    }
    
    /*!
      \brief Event filter
    
    
    Bryant's avatar
    Bryant committed
      When isEnabled() is true, the mouse events of the
      observed widget are filtered.
    
      \param object Object to be filtered
      \param event Event
    
      \return Forwarded to QObject::eventFilter()
    
    pixhawk's avatar
    pixhawk committed
    
      \sa widgetMousePressEvent(), widgetMouseReleaseEvent(),
          widgetMouseMoveEvent(), widgetWheelEvent(), widgetKeyPressEvent()
          widgetKeyReleaseEvent()
    */
    
    Bryant's avatar
    Bryant committed
    bool QwtMagnifier::eventFilter( QObject *object, QEvent *event )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        if ( object && object == parent() )
        {
            switch ( event->type() )
            {
                case QEvent::MouseButtonPress:
                {
                    widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
                    break;
                }
                case QEvent::MouseMove:
                {
                    widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
                    break;
                }
                case QEvent::MouseButtonRelease:
                {
                    widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
                    break;
                }
                case QEvent::Wheel:
                {
                    widgetWheelEvent( static_cast<QWheelEvent *>( event ) );
                    break;
                }
                case QEvent::KeyPress:
                {
                    widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
                    break;
                }
                case QEvent::KeyRelease:
                {
                    widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
                    break;
                }
                default:;
    
    pixhawk's avatar
    pixhawk committed
            }
        }
    
    Bryant's avatar
    Bryant committed
        return QObject::eventFilter( object, event );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    /*!
      Handle a mouse press event for the observed widget.
    
    
    Bryant's avatar
    Bryant committed
      \param mouseEvent Mouse event
    
      \sa eventFilter(), widgetMouseReleaseEvent(), widgetMouseMoveEvent()
    
    pixhawk's avatar
    pixhawk committed
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::widgetMousePressEvent( QMouseEvent *mouseEvent )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        if ( parentWidget() == NULL )
    
    pixhawk's avatar
    pixhawk committed
            return;
    
    
    Bryant's avatar
    Bryant committed
        if ( ( mouseEvent->button() != d_data->mouseButton ) ||
            ( mouseEvent->modifiers() != d_data->mouseButtonModifiers ) )
    
    pixhawk's avatar
    pixhawk committed
        {
            return;
        }
    
        d_data->hasMouseTracking = parentWidget()->hasMouseTracking();
    
    Bryant's avatar
    Bryant committed
    
        parentWidget()->setMouseTracking( true );
        d_data->mousePos = mouseEvent->pos();
    
    pixhawk's avatar
    pixhawk committed
        d_data->mousePressed = true;
    }
    
    /*!
      Handle a mouse release event for the observed widget.
    
    Bryant's avatar
    Bryant committed
    
      \param mouseEvent Mouse event
    
    
    pixhawk's avatar
    pixhawk committed
      \sa eventFilter(), widgetMousePressEvent(), widgetMouseMoveEvent(),
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        Q_UNUSED( mouseEvent );
    
        if ( d_data->mousePressed && parentWidget() )
        {
    
    pixhawk's avatar
    pixhawk committed
            d_data->mousePressed = false;
    
    Bryant's avatar
    Bryant committed
            parentWidget()->setMouseTracking( d_data->hasMouseTracking );
    
    pixhawk's avatar
    pixhawk committed
        }
    }
    
    /*!
      Handle a mouse move event for the observed widget.
    
    Bryant's avatar
    Bryant committed
      \param mouseEvent Mouse event
    
    pixhawk's avatar
    pixhawk committed
      \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
    
    pixhawk's avatar
    pixhawk committed
    {
        if ( !d_data->mousePressed )
            return;
    
    
    Bryant's avatar
    Bryant committed
        const int dy = mouseEvent->pos().y() - d_data->mousePos.y();
        if ( dy != 0 )
        {
    
    pixhawk's avatar
    pixhawk committed
            double f = d_data->mouseFactor;
            if ( dy < 0 )
                f = 1 / f;
    
    
    Bryant's avatar
    Bryant committed
            rescale( f );
    
    pixhawk's avatar
    pixhawk committed
        }
    
    
    Bryant's avatar
    Bryant committed
        d_data->mousePos = mouseEvent->pos();
    
    pixhawk's avatar
    pixhawk committed
    }
    
    /*!
      Handle a wheel event for the observed widget.
    
    
    Bryant's avatar
    Bryant committed
      \param wheelEvent Wheel event
    
    pixhawk's avatar
    pixhawk committed
      \sa eventFilter()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::widgetWheelEvent( QWheelEvent *wheelEvent )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        if ( wheelEvent->modifiers() != d_data->wheelModifiers )
    
    pixhawk's avatar
    pixhawk committed
        {
            return;
        }
    
    
    Bryant's avatar
    Bryant committed
        if ( d_data->wheelFactor != 0.0 )
        {
    
            /*
                A positive delta indicates that the wheel was
                rotated forwards away from the user; a negative
                value indicates that the wheel was rotated
                backwards toward the user.
                Most mouse types work in steps of 15 degrees,
                in which case the delta value is a multiple
                of 120 (== 15 * 8).
             */
    
    Bryant's avatar
    Bryant committed
            double f = qPow( d_data->wheelFactor, 
                qAbs( wheelEvent->delta() / 120.0 ) );
    
            if ( wheelEvent->delta() > 0 )
    
    pixhawk's avatar
    pixhawk committed
                f = 1 / f;
    
    
    Bryant's avatar
    Bryant committed
            rescale( f );
    
    pixhawk's avatar
    pixhawk committed
        }
    }
    
    /*!
      Handle a key press event for the observed widget.
    
    
    Bryant's avatar
    Bryant committed
      \param keyEvent Key event
    
    pixhawk's avatar
    pixhawk committed
      \sa eventFilter(), widgetKeyReleaseEvent()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::widgetKeyPressEvent( QKeyEvent *keyEvent )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        if ( keyEvent->key() == d_data->zoomInKey &&
            keyEvent->modifiers() == d_data->zoomInKeyModifiers )
        {
            rescale( d_data->keyFactor );
        }
        else if ( keyEvent->key() == d_data->zoomOutKey &&
            keyEvent->modifiers() == d_data->zoomOutKeyModifiers )
        {
            rescale( 1.0 / d_data->keyFactor );
    
    pixhawk's avatar
    pixhawk committed
        }
    }
    
    /*!
      Handle a key release event for the observed widget.
    
    
    Bryant's avatar
    Bryant committed
      \param keyEvent Key event
    
    pixhawk's avatar
    pixhawk committed
      \sa eventFilter(), widgetKeyReleaseEvent()
    */
    
    Bryant's avatar
    Bryant committed
    void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
    
    pixhawk's avatar
    pixhawk committed
    {
    
    Bryant's avatar
    Bryant committed
        Q_UNUSED( keyEvent );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    
    Bryant's avatar
    Bryant committed
    //! \return Parent widget, where the rescaling happens
    
    pixhawk's avatar
    pixhawk committed
    QWidget *QwtMagnifier::parentWidget()
    {
    
    Bryant's avatar
    Bryant committed
        return qobject_cast<QWidget *>( parent() );
    
    pixhawk's avatar
    pixhawk committed
    }
    
    
    Bryant's avatar
    Bryant committed
    //! \return Parent widget, where the rescaling happens
    
    pixhawk's avatar
    pixhawk committed
    const QWidget *QwtMagnifier::parentWidget() const
    {
    
    Bryant's avatar
    Bryant committed
        return qobject_cast<const QWidget *>( parent() );
    
    pixhawk's avatar
    pixhawk committed
    }