qwt_magnifier.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_magnifier.h"
00011 #include "qwt_math.h"
00012 #include <qevent.h>
00013 #include <qwidget.h>
00014 
00015 class QwtMagnifier::PrivateData
00016 {
00017 public:
00018     PrivateData():
00019         isEnabled( false ),
00020         wheelFactor( 0.9 ),
00021         wheelModifiers( Qt::NoModifier ),
00022         mouseFactor( 0.95 ),
00023         mouseButton( Qt::RightButton ),
00024         mouseButtonModifiers( Qt::NoModifier ),
00025         keyFactor( 0.9 ),
00026         zoomInKey( Qt::Key_Plus ),
00027         zoomInKeyModifiers( Qt::NoModifier ),
00028         zoomOutKey( Qt::Key_Minus ),
00029         zoomOutKeyModifiers( Qt::NoModifier ),
00030         mousePressed( false ),
00031         hasMouseTracking( false )
00032     {
00033     }
00034 
00035     bool isEnabled;
00036 
00037     double wheelFactor;
00038     Qt::KeyboardModifiers wheelModifiers;
00039 
00040     double mouseFactor;
00041 
00042     Qt::MouseButton mouseButton;
00043     Qt::KeyboardModifiers mouseButtonModifiers;
00044 
00045     double keyFactor;
00046 
00047     int zoomInKey;
00048     Qt::KeyboardModifiers zoomInKeyModifiers;
00049 
00050     int zoomOutKey;
00051     Qt::KeyboardModifiers  zoomOutKeyModifiers;
00052 
00053     bool mousePressed;
00054     bool hasMouseTracking;
00055     QPoint mousePos;
00056 };
00057 
00062 QwtMagnifier::QwtMagnifier( QWidget *parent ):
00063     QObject( parent )
00064 {
00065     d_data = new PrivateData();
00066 
00067     if ( parent )
00068     {
00069         if ( parent->focusPolicy() == Qt::NoFocus )
00070             parent->setFocusPolicy( Qt::WheelFocus );
00071     }
00072 
00073     setEnabled( true );
00074 }
00075 
00077 QwtMagnifier::~QwtMagnifier()
00078 {
00079     delete d_data;
00080 }
00081 
00091 void QwtMagnifier::setEnabled( bool on )
00092 {
00093     if ( d_data->isEnabled != on )
00094     {
00095         d_data->isEnabled = on;
00096 
00097         QObject *o = parent();
00098         if ( o )
00099         {
00100             if ( d_data->isEnabled )
00101                 o->installEventFilter( this );
00102             else
00103                 o->removeEventFilter( this );
00104         }
00105     }
00106 }
00107 
00112 bool QwtMagnifier::isEnabled() const
00113 {
00114     return d_data->isEnabled;
00115 }
00116 
00133 void QwtMagnifier::setWheelFactor( double factor )
00134 {
00135     d_data->wheelFactor = factor;
00136 }
00137 
00142 double QwtMagnifier::wheelFactor() const
00143 {
00144     return d_data->wheelFactor;
00145 }
00146 
00154 void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
00155 {
00156     d_data->wheelModifiers = modifiers;
00157 }
00158 
00163 Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
00164 {
00165     return d_data->wheelModifiers;
00166 }
00167 
00178 void QwtMagnifier::setMouseFactor( double factor )
00179 {
00180     d_data->mouseFactor = factor;
00181 }
00182 
00187 double QwtMagnifier::mouseFactor() const
00188 {
00189     return d_data->mouseFactor;
00190 }
00191 
00201 void QwtMagnifier::setMouseButton( 
00202     Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
00203 {
00204     d_data->mouseButton = button;
00205     d_data->mouseButtonModifiers = modifiers;
00206 }
00207 
00209 void QwtMagnifier::getMouseButton(
00210     Qt::MouseButton &button, Qt::KeyboardModifiers &modifiers ) const
00211 {
00212     button = d_data->mouseButton;
00213     modifiers = d_data->mouseButtonModifiers;
00214 }
00215 
00227 void QwtMagnifier::setKeyFactor( double factor )
00228 {
00229     d_data->keyFactor = factor;
00230 }
00231 
00236 double QwtMagnifier::keyFactor() const
00237 {
00238     return d_data->keyFactor;
00239 }
00240 
00249 void QwtMagnifier::setZoomInKey( int key, 
00250     Qt::KeyboardModifiers modifiers )
00251 {
00252     d_data->zoomInKey = key;
00253     d_data->zoomInKeyModifiers = modifiers;
00254 }
00255 
00264 void QwtMagnifier::getZoomInKey( int &key, 
00265     Qt::KeyboardModifiers &modifiers ) const
00266 {
00267     key = d_data->zoomInKey;
00268     modifiers = d_data->zoomInKeyModifiers;
00269 }
00270 
00279 void QwtMagnifier::setZoomOutKey( int key, 
00280     Qt::KeyboardModifiers modifiers )
00281 {
00282     d_data->zoomOutKey = key;
00283     d_data->zoomOutKeyModifiers = modifiers;
00284 }
00285 
00294 void QwtMagnifier::getZoomOutKey( int &key, 
00295     Qt::KeyboardModifiers &modifiers ) const
00296 {
00297     key = d_data->zoomOutKey;
00298     modifiers = d_data->zoomOutKeyModifiers;
00299 }
00300 
00316 bool QwtMagnifier::eventFilter( QObject *object, QEvent *event )
00317 {
00318     if ( object && object == parent() )
00319     {
00320         switch ( event->type() )
00321         {
00322             case QEvent::MouseButtonPress:
00323             {
00324                 widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
00325                 break;
00326             }
00327             case QEvent::MouseMove:
00328             {
00329                 widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
00330                 break;
00331             }
00332             case QEvent::MouseButtonRelease:
00333             {
00334                 widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
00335                 break;
00336             }
00337             case QEvent::Wheel:
00338             {
00339                 widgetWheelEvent( static_cast<QWheelEvent *>( event ) );
00340                 break;
00341             }
00342             case QEvent::KeyPress:
00343             {
00344                 widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
00345                 break;
00346             }
00347             case QEvent::KeyRelease:
00348             {
00349                 widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
00350                 break;
00351             }
00352             default:;
00353         }
00354     }
00355     return QObject::eventFilter( object, event );
00356 }
00357 
00364 void QwtMagnifier::widgetMousePressEvent( QMouseEvent *mouseEvent )
00365 {
00366     if ( parentWidget() == NULL )
00367         return;
00368 
00369     if ( ( mouseEvent->button() != d_data->mouseButton ) ||
00370         ( mouseEvent->modifiers() != d_data->mouseButtonModifiers ) )
00371     {
00372         return;
00373     }
00374 
00375     d_data->hasMouseTracking = parentWidget()->hasMouseTracking();
00376 
00377     parentWidget()->setMouseTracking( true );
00378     d_data->mousePos = mouseEvent->pos();
00379     d_data->mousePressed = true;
00380 }
00381 
00389 void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
00390 {
00391     Q_UNUSED( mouseEvent );
00392 
00393     if ( d_data->mousePressed && parentWidget() )
00394     {
00395         d_data->mousePressed = false;
00396         parentWidget()->setMouseTracking( d_data->hasMouseTracking );
00397     }
00398 }
00399 
00406 void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
00407 {
00408     if ( !d_data->mousePressed )
00409         return;
00410 
00411     const int dy = mouseEvent->pos().y() - d_data->mousePos.y();
00412     if ( dy != 0 )
00413     {
00414         double f = d_data->mouseFactor;
00415         if ( dy < 0 )
00416             f = 1 / f;
00417 
00418         rescale( f );
00419     }
00420 
00421     d_data->mousePos = mouseEvent->pos();
00422 }
00423 
00430 void QwtMagnifier::widgetWheelEvent( QWheelEvent *wheelEvent )
00431 {
00432     if ( wheelEvent->modifiers() != d_data->wheelModifiers )
00433     {
00434         return;
00435     }
00436 
00437     if ( d_data->wheelFactor != 0.0 )
00438     {
00439         /*
00440             A positive delta indicates that the wheel was
00441             rotated forwards away from the user; a negative
00442             value indicates that the wheel was rotated
00443             backwards toward the user.
00444             Most mouse types work in steps of 15 degrees,
00445             in which case the delta value is a multiple
00446             of 120 (== 15 * 8).
00447          */
00448         double f = qPow( d_data->wheelFactor, 
00449             qAbs( wheelEvent->delta() / 120.0 ) );
00450 
00451         if ( wheelEvent->delta() > 0 )
00452             f = 1 / f;
00453 
00454         rescale( f );
00455     }
00456 }
00457 
00464 void QwtMagnifier::widgetKeyPressEvent( QKeyEvent *keyEvent )
00465 {
00466     if ( keyEvent->key() == d_data->zoomInKey &&
00467         keyEvent->modifiers() == d_data->zoomInKeyModifiers )
00468     {
00469         rescale( d_data->keyFactor );
00470     }
00471     else if ( keyEvent->key() == d_data->zoomOutKey &&
00472         keyEvent->modifiers() == d_data->zoomOutKeyModifiers )
00473     {
00474         rescale( 1.0 / d_data->keyFactor );
00475     }
00476 }
00477 
00484 void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
00485 {
00486     Q_UNUSED( keyEvent );
00487 }
00488 
00490 QWidget *QwtMagnifier::parentWidget()
00491 {
00492     return qobject_cast<QWidget *>( parent() );
00493 }
00494 
00496 const QWidget *QwtMagnifier::parentWidget() const
00497 {
00498     return qobject_cast<const QWidget *>( parent() );
00499 }
00500 


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56