Loading...
Searching...
No Matches
qwt_magnifier.cpp
/******************************************************************************
* 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
*****************************************************************************/
#include "qwt_magnifier.h"
#include "qwt_math.h"
#include <qevent.h>
#include <qwidget.h>
class QwtMagnifier::PrivateData
{
public:
PrivateData()
: 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 )
, hasMouseTracking( false )
{
}
bool isEnabled;
double wheelFactor;
Qt::KeyboardModifiers wheelModifiers;
double mouseFactor;
Qt::MouseButton mouseButton;
Qt::KeyboardModifiers mouseButtonModifiers;
double keyFactor;
int zoomInKey;
Qt::KeyboardModifiers zoomInKeyModifiers;
int zoomOutKey;
Qt::KeyboardModifiers zoomOutKeyModifiers;
bool mousePressed;
bool hasMouseTracking;
QPoint mousePos;
};
QwtMagnifier::QwtMagnifier( QWidget* parent )
: QObject( parent )
{
m_data = new PrivateData();
if ( parent )
{
if ( parent->focusPolicy() == Qt::NoFocus )
parent->setFocusPolicy( Qt::WheelFocus );
}
setEnabled( true );
}
QwtMagnifier::~QwtMagnifier()
{
delete m_data;
}
void QwtMagnifier::setEnabled( bool on )
{
if ( m_data->isEnabled != on )
{
m_data->isEnabled = on;
QObject* o = parent();
if ( o )
{
if ( m_data->isEnabled )
o->installEventFilter( this );
else
o->removeEventFilter( this );
}
}
}
bool QwtMagnifier::isEnabled() const
{
return m_data->isEnabled;
}
void QwtMagnifier::setWheelFactor( double factor )
{
m_data->wheelFactor = factor;
}
double QwtMagnifier::wheelFactor() const
{
return m_data->wheelFactor;
}
void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
{
m_data->wheelModifiers = modifiers;
}
Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
{
return m_data->wheelModifiers;
}
void QwtMagnifier::setMouseFactor( double factor )
{
m_data->mouseFactor = factor;
}
double QwtMagnifier::mouseFactor() const
{
return m_data->mouseFactor;
}
void QwtMagnifier::setMouseButton(
Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
{
m_data->mouseButton = button;
m_data->mouseButtonModifiers = modifiers;
}
void QwtMagnifier::getMouseButton(
Qt::MouseButton& button, Qt::KeyboardModifiers& modifiers ) const
{
button = m_data->mouseButton;
modifiers = m_data->mouseButtonModifiers;
}
void QwtMagnifier::setKeyFactor( double factor )
{
m_data->keyFactor = factor;
}
double QwtMagnifier::keyFactor() const
{
return m_data->keyFactor;
}
void QwtMagnifier::setZoomInKey( int key,
Qt::KeyboardModifiers modifiers )
{
m_data->zoomInKey = key;
m_data->zoomInKeyModifiers = modifiers;
}
void QwtMagnifier::getZoomInKey( int& key,
Qt::KeyboardModifiers& modifiers ) const
{
key = m_data->zoomInKey;
modifiers = m_data->zoomInKeyModifiers;
}
void QwtMagnifier::setZoomOutKey( int key,
Qt::KeyboardModifiers modifiers )
{
m_data->zoomOutKey = key;
m_data->zoomOutKeyModifiers = modifiers;
}
void QwtMagnifier::getZoomOutKey( int& key,
Qt::KeyboardModifiers& modifiers ) const
{
key = m_data->zoomOutKey;
modifiers = m_data->zoomOutKeyModifiers;
}
bool QwtMagnifier::eventFilter( QObject* object, QEvent* event )
{
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:;
}
}
return QObject::eventFilter( object, event );
}
void QwtMagnifier::widgetMousePressEvent( QMouseEvent* mouseEvent )
{
if ( parentWidget() == NULL )
return;
if ( ( mouseEvent->button() != m_data->mouseButton ) ||
( mouseEvent->modifiers() != m_data->mouseButtonModifiers ) )
{
return;
}
m_data->hasMouseTracking = parentWidget()->hasMouseTracking();
parentWidget()->setMouseTracking( true );
m_data->mousePos = mouseEvent->pos();
m_data->mousePressed = true;
}
void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent* mouseEvent )
{
Q_UNUSED( mouseEvent );
if ( m_data->mousePressed && parentWidget() )
{
m_data->mousePressed = false;
parentWidget()->setMouseTracking( m_data->hasMouseTracking );
}
}
void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent* mouseEvent )
{
if ( !m_data->mousePressed )
return;
const int dy = mouseEvent->pos().y() - m_data->mousePos.y();
if ( dy != 0 )
{
double f = m_data->mouseFactor;
if ( dy < 0 )
f = 1 / f;
rescale( f );
}
m_data->mousePos = mouseEvent->pos();
}
void QwtMagnifier::widgetWheelEvent( QWheelEvent* wheelEvent )
{
if ( wheelEvent->modifiers() != m_data->wheelModifiers )
{
return;
}
if ( m_data->wheelFactor != 0.0 )
{
#if QT_VERSION < 0x050000
const int wheelDelta = wheelEvent->delta();
#else
const QPoint delta = wheelEvent->angleDelta();
const int wheelDelta = ( qAbs( delta.x() ) > qAbs( delta.y() ) )
? delta.x() : delta.y();
#endif
/*
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).
*/
double f = std::pow( m_data->wheelFactor,
qAbs( wheelDelta / 120.0 ) );
if ( wheelDelta > 0 )
f = 1 / f;
rescale( f );
}
}
void QwtMagnifier::widgetKeyPressEvent( QKeyEvent* keyEvent )
{
if ( keyEvent->key() == m_data->zoomInKey &&
keyEvent->modifiers() == m_data->zoomInKeyModifiers )
{
rescale( m_data->keyFactor );
}
else if ( keyEvent->key() == m_data->zoomOutKey &&
keyEvent->modifiers() == m_data->zoomOutKeyModifiers )
{
rescale( 1.0 / m_data->keyFactor );
}
}
void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent* keyEvent )
{
Q_UNUSED( keyEvent );
}
QWidget* QwtMagnifier::parentWidget()
{
return qobject_cast< QWidget* >( parent() );
}
const QWidget* QwtMagnifier::parentWidget() const
{
return qobject_cast< const QWidget* >( parent() );
}
#include "moc_qwt_magnifier.cpp"
QwtMagnifier::setZoomInKey
void setZoomInKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
Definition qwt_magnifier.cpp:250
QwtMagnifier::setEnabled
void setEnabled(bool)
En/disable the magnifier.
Definition qwt_magnifier.cpp:92
QwtMagnifier::setZoomOutKey
void setZoomOutKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
Definition qwt_magnifier.cpp:280
QwtMagnifier::getZoomInKey
void getZoomInKey(int &key, Qt::KeyboardModifiers &) const
Retrieve the settings of the zoom in key.
Definition qwt_magnifier.cpp:265
QwtMagnifier::widgetWheelEvent
virtual void widgetWheelEvent(QWheelEvent *)
Definition qwt_magnifier.cpp:431
QwtMagnifier::mouseFactor
double mouseFactor() const
Definition qwt_magnifier.cpp:188
QwtMagnifier::widgetKeyPressEvent
virtual void widgetKeyPressEvent(QKeyEvent *)
Definition qwt_magnifier.cpp:473
QwtMagnifier::wheelModifiers
Qt::KeyboardModifiers wheelModifiers() const
Definition qwt_magnifier.cpp:164
QwtMagnifier::widgetMouseMoveEvent
virtual void widgetMouseMoveEvent(QMouseEvent *)
Definition qwt_magnifier.cpp:407
QwtMagnifier::parentWidget
QWidget * parentWidget()
Definition qwt_magnifier.cpp:499
QwtMagnifier::widgetMousePressEvent
virtual void widgetMousePressEvent(QMouseEvent *)
Definition qwt_magnifier.cpp:365
QwtMagnifier::rescale
virtual void rescale(double factor)=0
QwtMagnifier::isEnabled
bool isEnabled() const
Definition qwt_magnifier.cpp:113
QwtMagnifier::getMouseButton
void getMouseButton(Qt::MouseButton &, Qt::KeyboardModifiers &) const
Definition qwt_magnifier.cpp:210
QwtMagnifier::wheelFactor
double wheelFactor() const
Definition qwt_magnifier.cpp:143
QwtMagnifier::QwtMagnifier
QwtMagnifier(QWidget *)
Definition qwt_magnifier.cpp:63
QwtMagnifier::widgetMouseReleaseEvent
virtual void widgetMouseReleaseEvent(QMouseEvent *)
Definition qwt_magnifier.cpp:390
QwtMagnifier::setWheelModifiers
void setWheelModifiers(Qt::KeyboardModifiers)
Definition qwt_magnifier.cpp:155
QwtMagnifier::setKeyFactor
void setKeyFactor(double)
Change the key factor.
Definition qwt_magnifier.cpp:228
QwtMagnifier::~QwtMagnifier
virtual ~QwtMagnifier()
Destructor.
Definition qwt_magnifier.cpp:78
QwtMagnifier::getZoomOutKey
void getZoomOutKey(int &key, Qt::KeyboardModifiers &) const
Retrieve the settings of the zoom out key.
Definition qwt_magnifier.cpp:295
QwtMagnifier::setMouseButton
void setMouseButton(Qt::MouseButton, Qt::KeyboardModifiers=Qt::NoModifier)
Definition qwt_magnifier.cpp:202
QwtMagnifier::setMouseFactor
void setMouseFactor(double)
Change the mouse factor.
Definition qwt_magnifier.cpp:179
QwtMagnifier::keyFactor
double keyFactor() const
Definition qwt_magnifier.cpp:237
QwtMagnifier::setWheelFactor
void setWheelFactor(double)
Change the wheel factor.
Definition qwt_magnifier.cpp:134
QwtMagnifier::widgetKeyReleaseEvent
virtual void widgetKeyReleaseEvent(QKeyEvent *)
Definition qwt_magnifier.cpp:493
QwtMagnifier::eventFilter
virtual bool eventFilter(QObject *, QEvent *) override
Event filter.
Definition qwt_magnifier.cpp:317