qwt_magnifier.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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 
17 {
18 public:
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 
52  Qt::KeyboardModifiers zoomOutKeyModifiers;
53 
56  QPoint mousePos;
57 };
58 
63 QwtMagnifier::QwtMagnifier( QWidget *parent ):
64  QObject( parent )
65 {
66  d_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 d_data;
81 }
82 
92 void QwtMagnifier::setEnabled( bool on )
93 {
94  if ( d_data->isEnabled != on )
95  {
96  d_data->isEnabled = on;
97 
98  QObject *o = parent();
99  if ( o )
100  {
101  if ( d_data->isEnabled )
102  o->installEventFilter( this );
103  else
104  o->removeEventFilter( this );
105  }
106  }
107 }
108 
114 {
115  return d_data->isEnabled;
116 }
117 
134 void QwtMagnifier::setWheelFactor( double factor )
135 {
136  d_data->wheelFactor = factor;
137 }
138 
144 {
145  return d_data->wheelFactor;
146 }
147 
155 void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
156 {
157  d_data->wheelModifiers = modifiers;
158 }
159 
164 Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
165 {
166  return d_data->wheelModifiers;
167 }
168 
179 void QwtMagnifier::setMouseFactor( double factor )
180 {
181  d_data->mouseFactor = factor;
182 }
183 
189 {
190  return d_data->mouseFactor;
191 }
192 
203  Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
204 {
205  d_data->mouseButton = button;
206  d_data->mouseButtonModifiers = modifiers;
207 }
208 
211  Qt::MouseButton &button, Qt::KeyboardModifiers &modifiers ) const
212 {
213  button = d_data->mouseButton;
214  modifiers = d_data->mouseButtonModifiers;
215 }
216 
228 void QwtMagnifier::setKeyFactor( double factor )
229 {
230  d_data->keyFactor = factor;
231 }
232 
238 {
239  return d_data->keyFactor;
240 }
241 
251  Qt::KeyboardModifiers modifiers )
252 {
253  d_data->zoomInKey = key;
254  d_data->zoomInKeyModifiers = modifiers;
255 }
256 
266  Qt::KeyboardModifiers &modifiers ) const
267 {
268  key = d_data->zoomInKey;
269  modifiers = d_data->zoomInKeyModifiers;
270 }
271 
281  Qt::KeyboardModifiers modifiers )
282 {
283  d_data->zoomOutKey = key;
284  d_data->zoomOutKeyModifiers = modifiers;
285 }
286 
296  Qt::KeyboardModifiers &modifiers ) const
297 {
298  key = d_data->zoomOutKey;
299  modifiers = d_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() != d_data->mouseButton ) ||
371  ( mouseEvent->modifiers() != d_data->mouseButtonModifiers ) )
372  {
373  return;
374  }
375 
376  d_data->hasMouseTracking = parentWidget()->hasMouseTracking();
377 
378  parentWidget()->setMouseTracking( true );
379  d_data->mousePos = mouseEvent->pos();
380  d_data->mousePressed = true;
381 }
382 
390 void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
391 {
392  Q_UNUSED( mouseEvent );
393 
394  if ( d_data->mousePressed && parentWidget() )
395  {
396  d_data->mousePressed = false;
397  parentWidget()->setMouseTracking( d_data->hasMouseTracking );
398  }
399 }
400 
407 void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
408 {
409  if ( !d_data->mousePressed )
410  return;
411 
412  const int dy = mouseEvent->pos().y() - d_data->mousePos.y();
413  if ( dy != 0 )
414  {
415  double f = d_data->mouseFactor;
416  if ( dy < 0 )
417  f = 1 / f;
418 
419  rescale( f );
420  }
421 
422  d_data->mousePos = mouseEvent->pos();
423 }
424 
431 void QwtMagnifier::widgetWheelEvent( QWheelEvent *wheelEvent )
432 {
433  if ( wheelEvent->modifiers() != d_data->wheelModifiers )
434  {
435  return;
436  }
437 
438  if ( d_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( d_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() == d_data->zoomInKey &&
476  keyEvent->modifiers() == d_data->zoomInKeyModifiers )
477  {
479  }
480  else if ( keyEvent->key() == d_data->zoomOutKey &&
481  keyEvent->modifiers() == d_data->zoomOutKeyModifiers )
482  {
483  rescale( 1.0 / d_data->keyFactor );
484  }
485 }
486 
493 void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
494 {
495  Q_UNUSED( keyEvent );
496 }
497 
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 #if QWT_MOC_INCLUDE
511 #include "moc_qwt_magnifier.cpp"
512 #endif
double mouseFactor() const
virtual void widgetMousePressEvent(QMouseEvent *)
virtual void rescale(double factor)=0
void setWheelFactor(double)
Change the wheel factor.
void setKeyFactor(double)
Change the key factor.
virtual void widgetWheelEvent(QWheelEvent *)
QwtMagnifier(QWidget *)
void setMouseFactor(double)
Change the mouse factor.
Qt::KeyboardModifiers wheelModifiers() const
void setWheelModifiers(Qt::KeyboardModifiers)
Qt::KeyboardModifiers wheelModifiers
Qt::MouseButton mouseButton
virtual ~QwtMagnifier()
Destructor.
QWidget * parentWidget()
void setEnabled(bool)
En/disable the magnifier.
void setZoomInKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
void getZoomOutKey(int &key, Qt::KeyboardModifiers &) const
Retrieve the settings of the zoom out key.
PrivateData * d_data
Definition: qwt_magnifier.h:82
virtual void widgetMouseReleaseEvent(QMouseEvent *)
void setZoomOutKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
double keyFactor() const
Qt::KeyboardModifiers zoomInKeyModifiers
double wheelFactor() const
virtual void widgetMouseMoveEvent(QMouseEvent *)
Qt::KeyboardModifiers mouseButtonModifiers
virtual void widgetKeyReleaseEvent(QKeyEvent *)
Qt::KeyboardModifiers zoomOutKeyModifiers
virtual void widgetKeyPressEvent(QKeyEvent *)
void getZoomInKey(int &key, Qt::KeyboardModifiers &) const
Retrieve the settings of the zoom in key.
void getMouseButton(Qt::MouseButton &, Qt::KeyboardModifiers &) const
virtual bool eventFilter(QObject *, QEvent *) QWT_OVERRIDE
Event filter.
void setMouseButton(Qt::MouseButton, Qt::KeyboardModifiers=Qt::NoModifier)
bool isEnabled() const


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10