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
16class 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
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
79{
80 delete m_data;
81}
82
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
114{
115 return m_data->isEnabled;
116}
117
134void QwtMagnifier::setWheelFactor( double factor )
135{
136 m_data->wheelFactor = factor;
137}
138
144{
145 return m_data->wheelFactor;
146}
147
155void 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
179void QwtMagnifier::setMouseFactor( double factor )
180{
181 m_data->mouseFactor = factor;
182}
183
189{
190 return m_data->mouseFactor;
191}
192
203 Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
204{
205 m_data->mouseButton = button;
206 m_data->mouseButtonModifiers = modifiers;
207}
208
211 Qt::MouseButton& button, Qt::KeyboardModifiers& modifiers ) const
212{
213 button = m_data->mouseButton;
214 modifiers = m_data->mouseButtonModifiers;
215}
216
228void QwtMagnifier::setKeyFactor( double factor )
229{
230 m_data->keyFactor = factor;
231}
232
238{
239 return m_data->keyFactor;
240}
241
251 Qt::KeyboardModifiers modifiers )
252{
253 m_data->zoomInKey = key;
254 m_data->zoomInKeyModifiers = modifiers;
255}
256
266 Qt::KeyboardModifiers& modifiers ) const
267{
268 key = m_data->zoomInKey;
269 modifiers = m_data->zoomInKeyModifiers;
270}
271
281 Qt::KeyboardModifiers modifiers )
282{
283 m_data->zoomOutKey = key;
284 m_data->zoomOutKeyModifiers = modifiers;
285}
286
296 Qt::KeyboardModifiers& modifiers ) const
297{
298 key = m_data->zoomOutKey;
299 modifiers = m_data->zoomOutKeyModifiers;
300}
301
317bool 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
365void 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
390void 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
407void 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
431void 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
473void 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
493void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent* keyEvent )
494{
495 Q_UNUSED( keyEvent );
496}
497
500{
501 return qobject_cast< QWidget* >( parent() );
502}
503
505const QWidget* QwtMagnifier::parentWidget() const
506{
507 return qobject_cast< const QWidget* >( parent() );
508}
509
510#include "moc_qwt_magnifier.cpp"
void setZoomInKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
void setEnabled(bool)
En/disable the magnifier.
void setZoomOutKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
void getZoomInKey(int &key, Qt::KeyboardModifiers &) const
Retrieve the settings of the zoom in key.
virtual void widgetWheelEvent(QWheelEvent *)
double mouseFactor() const
virtual void widgetKeyPressEvent(QKeyEvent *)
Qt::KeyboardModifiers wheelModifiers() const
virtual void widgetMouseMoveEvent(QMouseEvent *)
QWidget * parentWidget()
virtual void widgetMousePressEvent(QMouseEvent *)
virtual void rescale(double factor)=0
bool isEnabled() const
void getMouseButton(Qt::MouseButton &, Qt::KeyboardModifiers &) const
double wheelFactor() const
QwtMagnifier(QWidget *)
virtual void widgetMouseReleaseEvent(QMouseEvent *)
void setWheelModifiers(Qt::KeyboardModifiers)
void setKeyFactor(double)
Change the key factor.
virtual ~QwtMagnifier()
Destructor.
void getZoomOutKey(int &key, Qt::KeyboardModifiers &) const
Retrieve the settings of the zoom out key.
void setMouseButton(Qt::MouseButton, Qt::KeyboardModifiers=Qt::NoModifier)
void setMouseFactor(double)
Change the mouse factor.
double keyFactor() const
void setWheelFactor(double)
Change the wheel factor.
virtual void widgetKeyReleaseEvent(QKeyEvent *)
virtual bool eventFilter(QObject *, QEvent *) override
Event filter.