Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_magnifier.cpp
1/******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_magnifier.h"
11#include "qwt_math.h"
12
13#include <qevent.h>
14#include <qwidget.h>
15
16 class QwtMagnifier::PrivateData
17{
18 public:
19 PrivateData()
20 : isEnabled( false )
21 , wheelFactor( 0.9 )
22 , wheelModifiers( Qt::NoModifier )
23 , mouseFactor( 0.95 )
24 , mouseButton( Qt::RightButton )
25 , mouseButtonModifiers( Qt::NoModifier )
26 , keyFactor( 0.9 )
27 , zoomInKey( Qt::Key_Plus )
28 , zoomInKeyModifiers( Qt::NoModifier )
29 , zoomOutKey( Qt::Key_Minus )
30 , zoomOutKeyModifiers( Qt::NoModifier )
31 , mousePressed( false )
32 , hasMouseTracking( false )
33 {
34 }
35
36 bool isEnabled;
37
38 double wheelFactor;
39 Qt::KeyboardModifiers wheelModifiers;
40
41 double mouseFactor;
42
43 Qt::MouseButton mouseButton;
44 Qt::KeyboardModifiers mouseButtonModifiers;
45
46 double keyFactor;
47
48 int zoomInKey;
49 Qt::KeyboardModifiers zoomInKeyModifiers;
50
51 int zoomOutKey;
52 Qt::KeyboardModifiers zoomOutKeyModifiers;
53
54 bool mousePressed;
55 bool hasMouseTracking;
56 QPoint mousePos;
57};
58
63 QwtMagnifier::QwtMagnifier( QWidget* parent )
64 : QObject( parent )
65{
66 m_data = new PrivateData();
67
68 if ( parent )
69 {
70 if ( parent->focusPolicy() == Qt::NoFocus )
71 parent->setFocusPolicy( Qt::WheelFocus );
72 }
73
74 setEnabled( true );
75}
76
78 QwtMagnifier::~QwtMagnifier()
79{
80 delete m_data;
81}
82
92 void QwtMagnifier::setEnabled( bool on )
93{
94 if ( m_data->isEnabled != on )
95 {
96 m_data->isEnabled = on;
97
98 QObject* o = parent();
99 if ( o )
100 {
101 if ( m_data->isEnabled )
102 o->installEventFilter( this );
103 else
104 o->removeEventFilter( this );
105 }
106 }
107}
108
113 bool QwtMagnifier::isEnabled() const
114{
115 return m_data->isEnabled;
116}
117
134 void QwtMagnifier::setWheelFactor( double factor )
135{
136 m_data->wheelFactor = factor;
137}
138
143 double QwtMagnifier::wheelFactor() const
144{
145 return m_data->wheelFactor;
146}
147
155 void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
156{
157 m_data->wheelModifiers = modifiers;
158}
159
164Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
165{
166 return m_data->wheelModifiers;
167}
168
179 void QwtMagnifier::setMouseFactor( double factor )
180{
181 m_data->mouseFactor = factor;
182}
183
188 double QwtMagnifier::mouseFactor() const
189{
190 return m_data->mouseFactor;
191}
192
202 void QwtMagnifier::setMouseButton(
203 Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
204{
205 m_data->mouseButton = button;
206 m_data->mouseButtonModifiers = modifiers;
207}
208
210 void QwtMagnifier::getMouseButton(
211 Qt::MouseButton& button, Qt::KeyboardModifiers& modifiers ) const
212{
213 button = m_data->mouseButton;
214 modifiers = m_data->mouseButtonModifiers;
215}
216
228 void QwtMagnifier::setKeyFactor( double factor )
229{
230 m_data->keyFactor = factor;
231}
232
237 double QwtMagnifier::keyFactor() const
238{
239 return m_data->keyFactor;
240}
241
250 void QwtMagnifier::setZoomInKey( int key,
251 Qt::KeyboardModifiers modifiers )
252{
253 m_data->zoomInKey = key;
254 m_data->zoomInKeyModifiers = modifiers;
255}
256
265 void QwtMagnifier::getZoomInKey( int& key,
266 Qt::KeyboardModifiers& modifiers ) const
267{
268 key = m_data->zoomInKey;
269 modifiers = m_data->zoomInKeyModifiers;
270}
271
280 void QwtMagnifier::setZoomOutKey( int key,
281 Qt::KeyboardModifiers modifiers )
282{
283 m_data->zoomOutKey = key;
284 m_data->zoomOutKeyModifiers = modifiers;
285}
286
295 void QwtMagnifier::getZoomOutKey( int& key,
296 Qt::KeyboardModifiers& modifiers ) const
297{
298 key = m_data->zoomOutKey;
299 modifiers = m_data->zoomOutKeyModifiers;
300}
301
317 bool QwtMagnifier::eventFilter( QObject* object, QEvent* event )
318{
319 if ( object && object == parent() )
320 {
321 switch ( event->type() )
322 {
323 case QEvent::MouseButtonPress:
324 {
325 widgetMousePressEvent( static_cast< QMouseEvent* >( event ) );
326 break;
327 }
328 case QEvent::MouseMove:
329 {
330 widgetMouseMoveEvent( static_cast< QMouseEvent* >( event ) );
331 break;
332 }
333 case QEvent::MouseButtonRelease:
334 {
335 widgetMouseReleaseEvent( static_cast< QMouseEvent* >( event ) );
336 break;
337 }
338 case QEvent::Wheel:
339 {
340 widgetWheelEvent( static_cast< QWheelEvent* >( event ) );
341 break;
342 }
343 case QEvent::KeyPress:
344 {
345 widgetKeyPressEvent( static_cast< QKeyEvent* >( event ) );
346 break;
347 }
348 case QEvent::KeyRelease:
349 {
350 widgetKeyReleaseEvent( static_cast< QKeyEvent* >( event ) );
351 break;
352 }
353 default:;
354 }
355 }
356 return QObject::eventFilter( object, event );
357}
358
365 void QwtMagnifier::widgetMousePressEvent( QMouseEvent* mouseEvent )
366{
367 if ( parentWidget() == NULL )
368 return;
369
370 if ( ( mouseEvent->button() != m_data->mouseButton ) ||
371 ( mouseEvent->modifiers() != m_data->mouseButtonModifiers ) )
372 {
373 return;
374 }
375
376 m_data->hasMouseTracking = parentWidget()->hasMouseTracking();
377
378 parentWidget()->setMouseTracking( true );
379 m_data->mousePos = mouseEvent->pos();
380 m_data->mousePressed = true;
381}
382
390 void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent* mouseEvent )
391{
392 Q_UNUSED( mouseEvent );
393
394 if ( m_data->mousePressed && parentWidget() )
395 {
396 m_data->mousePressed = false;
397 parentWidget()->setMouseTracking( m_data->hasMouseTracking );
398 }
399}
400
407 void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent* mouseEvent )
408{
409 if ( !m_data->mousePressed )
410 return;
411
412 const int dy = mouseEvent->pos().y() - m_data->mousePos.y();
413 if ( dy != 0 )
414 {
415 double f = m_data->mouseFactor;
416 if ( dy < 0 )
417 f = 1 / f;
418
419 rescale( f );
420 }
421
422 m_data->mousePos = mouseEvent->pos();
423}
424
431 void QwtMagnifier::widgetWheelEvent( QWheelEvent* wheelEvent )
432{
433 if ( wheelEvent->modifiers() != m_data->wheelModifiers )
434 {
435 return;
436 }
437
438 if ( m_data->wheelFactor != 0.0 )
439 {
440#if QT_VERSION < 0x050000
441 const int wheelDelta = wheelEvent->delta();
442#else
443 const QPoint delta = wheelEvent->angleDelta();
444 const int wheelDelta = ( qAbs( delta.x() ) > qAbs( delta.y() ) )
445 ? delta.x() : delta.y();
446#endif
447
448 /*
449 A positive delta indicates that the wheel was
450 rotated forwards away from the user; a negative
451 value indicates that the wheel was rotated
452 backwards toward the user.
453 Most mouse types work in steps of 15 degrees,
454 in which case the delta value is a multiple
455 of 120 (== 15 * 8).
456 */
457 double f = std::pow( m_data->wheelFactor,
458 qAbs( wheelDelta / 120.0 ) );
459
460 if ( wheelDelta > 0 )
461 f = 1 / f;
462
463 rescale( f );
464 }
465}
466
473 void QwtMagnifier::widgetKeyPressEvent( QKeyEvent* keyEvent )
474{
475 if ( keyEvent->key() == m_data->zoomInKey &&
476 keyEvent->modifiers() == m_data->zoomInKeyModifiers )
477 {
478 rescale( m_data->keyFactor );
479 }
480 else if ( keyEvent->key() == m_data->zoomOutKey &&
481 keyEvent->modifiers() == m_data->zoomOutKeyModifiers )
482 {
483 rescale( 1.0 / m_data->keyFactor );
484 }
485}
486
493 void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent* keyEvent )
494{
495 Q_UNUSED( keyEvent );
496}
497
499QWidget* QwtMagnifier::parentWidget()
500{
501 return qobject_cast< QWidget* >( parent() );
502}
503
505 const QWidget* QwtMagnifier::parentWidget() const
506{
507 return qobject_cast< const QWidget* >( parent() );
508}
509
510#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