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 #include <qevent.h>
13 #include <qwidget.h>
14 
16 {
17 public:
19  isEnabled( false ),
20  wheelFactor( 0.9 ),
21  wheelModifiers( Qt::NoModifier ),
22  mouseFactor( 0.95 ),
23  mouseButton( Qt::RightButton ),
24  mouseButtonModifiers( Qt::NoModifier ),
25  keyFactor( 0.9 ),
26  zoomInKey( Qt::Key_Plus ),
27  zoomInKeyModifiers( Qt::NoModifier ),
28  zoomOutKey( Qt::Key_Minus ),
29  zoomOutKeyModifiers( Qt::NoModifier ),
30  mousePressed( false ),
31  hasMouseTracking( false )
32  {
33  }
34 
35  bool isEnabled;
36 
37  double wheelFactor;
38  Qt::KeyboardModifiers wheelModifiers;
39 
40  double mouseFactor;
41 
42  Qt::MouseButton mouseButton;
43  Qt::KeyboardModifiers mouseButtonModifiers;
44 
45  double keyFactor;
46 
47  int zoomInKey;
48  Qt::KeyboardModifiers zoomInKeyModifiers;
49 
51  Qt::KeyboardModifiers zoomOutKeyModifiers;
52 
55  QPoint mousePos;
56 };
57 
62 QwtMagnifier::QwtMagnifier( QWidget *parent ):
63  QObject( parent )
64 {
65  d_data = new PrivateData();
66 
67  if ( parent )
68  {
69  if ( parent->focusPolicy() == Qt::NoFocus )
70  parent->setFocusPolicy( Qt::WheelFocus );
71  }
72 
73  setEnabled( true );
74 }
75 
78 {
79  delete d_data;
80 }
81 
91 void QwtMagnifier::setEnabled( bool on )
92 {
93  if ( d_data->isEnabled != on )
94  {
95  d_data->isEnabled = on;
96 
97  QObject *o = parent();
98  if ( o )
99  {
100  if ( d_data->isEnabled )
101  o->installEventFilter( this );
102  else
103  o->removeEventFilter( this );
104  }
105  }
106 }
107 
113 {
114  return d_data->isEnabled;
115 }
116 
133 void QwtMagnifier::setWheelFactor( double factor )
134 {
135  d_data->wheelFactor = factor;
136 }
137 
143 {
144  return d_data->wheelFactor;
145 }
146 
154 void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
155 {
156  d_data->wheelModifiers = modifiers;
157 }
158 
163 Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
164 {
165  return d_data->wheelModifiers;
166 }
167 
178 void QwtMagnifier::setMouseFactor( double factor )
179 {
180  d_data->mouseFactor = factor;
181 }
182 
188 {
189  return d_data->mouseFactor;
190 }
191 
202  Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
203 {
204  d_data->mouseButton = button;
205  d_data->mouseButtonModifiers = modifiers;
206 }
207 
210  Qt::MouseButton &button, Qt::KeyboardModifiers &modifiers ) const
211 {
212  button = d_data->mouseButton;
213  modifiers = d_data->mouseButtonModifiers;
214 }
215 
227 void QwtMagnifier::setKeyFactor( double factor )
228 {
229  d_data->keyFactor = factor;
230 }
231 
237 {
238  return d_data->keyFactor;
239 }
240 
250  Qt::KeyboardModifiers modifiers )
251 {
252  d_data->zoomInKey = key;
253  d_data->zoomInKeyModifiers = modifiers;
254 }
255 
265  Qt::KeyboardModifiers &modifiers ) const
266 {
267  key = d_data->zoomInKey;
268  modifiers = d_data->zoomInKeyModifiers;
269 }
270 
280  Qt::KeyboardModifiers modifiers )
281 {
282  d_data->zoomOutKey = key;
283  d_data->zoomOutKeyModifiers = modifiers;
284 }
285 
295  Qt::KeyboardModifiers &modifiers ) const
296 {
297  key = d_data->zoomOutKey;
298  modifiers = d_data->zoomOutKeyModifiers;
299 }
300 
316 bool QwtMagnifier::eventFilter( QObject *object, QEvent *event )
317 {
318  if ( object && object == parent() )
319  {
320  switch ( event->type() )
321  {
322  case QEvent::MouseButtonPress:
323  {
324  widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
325  break;
326  }
327  case QEvent::MouseMove:
328  {
329  widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
330  break;
331  }
332  case QEvent::MouseButtonRelease:
333  {
334  widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
335  break;
336  }
337  case QEvent::Wheel:
338  {
339  widgetWheelEvent( static_cast<QWheelEvent *>( event ) );
340  break;
341  }
342  case QEvent::KeyPress:
343  {
344  widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
345  break;
346  }
347  case QEvent::KeyRelease:
348  {
349  widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
350  break;
351  }
352  default:;
353  }
354  }
355  return QObject::eventFilter( object, event );
356 }
357 
364 void QwtMagnifier::widgetMousePressEvent( QMouseEvent *mouseEvent )
365 {
366  if ( parentWidget() == NULL )
367  return;
368 
369  if ( ( mouseEvent->button() != d_data->mouseButton ) ||
370  ( mouseEvent->modifiers() != d_data->mouseButtonModifiers ) )
371  {
372  return;
373  }
374 
375  d_data->hasMouseTracking = parentWidget()->hasMouseTracking();
376 
377  parentWidget()->setMouseTracking( true );
378  d_data->mousePos = mouseEvent->pos();
379  d_data->mousePressed = true;
380 }
381 
389 void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
390 {
391  Q_UNUSED( mouseEvent );
392 
393  if ( d_data->mousePressed && parentWidget() )
394  {
395  d_data->mousePressed = false;
396  parentWidget()->setMouseTracking( d_data->hasMouseTracking );
397  }
398 }
399 
406 void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
407 {
408  if ( !d_data->mousePressed )
409  return;
410 
411  const int dy = mouseEvent->pos().y() - d_data->mousePos.y();
412  if ( dy != 0 )
413  {
414  double f = d_data->mouseFactor;
415  if ( dy < 0 )
416  f = 1 / f;
417 
418  rescale( f );
419  }
420 
421  d_data->mousePos = mouseEvent->pos();
422 }
423 
430 void QwtMagnifier::widgetWheelEvent( QWheelEvent *wheelEvent )
431 {
432  if ( wheelEvent->modifiers() != d_data->wheelModifiers )
433  {
434  return;
435  }
436 
437  if ( d_data->wheelFactor != 0.0 )
438  {
439  /*
440  A positive delta indicates that the wheel was
441  rotated forwards away from the user; a negative
442  value indicates that the wheel was rotated
443  backwards toward the user.
444  Most mouse types work in steps of 15 degrees,
445  in which case the delta value is a multiple
446  of 120 (== 15 * 8).
447  */
448  double f = qPow( d_data->wheelFactor,
449  qAbs( wheelEvent->delta() / 120.0 ) );
450 
451  if ( wheelEvent->delta() > 0 )
452  f = 1 / f;
453 
454  rescale( f );
455  }
456 }
457 
464 void QwtMagnifier::widgetKeyPressEvent( QKeyEvent *keyEvent )
465 {
466  if ( keyEvent->key() == d_data->zoomInKey &&
467  keyEvent->modifiers() == d_data->zoomInKeyModifiers )
468  {
470  }
471  else if ( keyEvent->key() == d_data->zoomOutKey &&
472  keyEvent->modifiers() == d_data->zoomOutKeyModifiers )
473  {
474  rescale( 1.0 / d_data->keyFactor );
475  }
476 }
477 
484 void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
485 {
486  Q_UNUSED( keyEvent );
487 }
488 
491 {
492  return qobject_cast<QWidget *>( parent() );
493 }
494 
496 const QWidget *QwtMagnifier::parentWidget() const
497 {
498  return qobject_cast<const QWidget *>( parent() );
499 }
500 
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.
f
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.
virtual bool eventFilter(QObject *, QEvent *)
Event filter.
PrivateData * d_data
Definition: qwt_magnifier.h:82
virtual void widgetMouseReleaseEvent(QMouseEvent *)
void setZoomOutKey(int key, Qt::KeyboardModifiers=Qt::NoModifier)
double keyFactor() const
UnboundConversion o
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
void setMouseButton(Qt::MouseButton, Qt::KeyboardModifiers=Qt::NoModifier)
bool isEnabled() const


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17