source: ntrip/trunk/BNC/qwt/qwt_magnifier.cpp@ 10126

Last change on this file since 10126 was 9383, checked in by stoecker, 4 years ago

update to qwt verion 6.1.1 to fix build with newer Qt5

File size: 11.5 KB
Line 
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
15class QwtMagnifier::PrivateData
16{
17public:
18 PrivateData():
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 {
32 }
33
34 bool isEnabled;
35
36 double wheelFactor;
37 Qt::KeyboardModifiers wheelModifiers;
38
39 double mouseFactor;
40
41 Qt::MouseButton mouseButton;
42 Qt::KeyboardModifiers mouseButtonModifiers;
43
44 double keyFactor;
45
46 int zoomInKey;
47 Qt::KeyboardModifiers zoomInKeyModifiers;
48
49 int zoomOutKey;
50 Qt::KeyboardModifiers zoomOutKeyModifiers;
51
52 bool mousePressed;
53 bool hasMouseTracking;
54 QPoint mousePos;
55};
56
57/*!
58 Constructor
59 \param parent Widget to be magnified
60*/
61QwtMagnifier::QwtMagnifier( QWidget *parent ):
62 QObject( parent )
63{
64 d_data = new PrivateData();
65 setEnabled( true );
66}
67
68//! Destructor
69QwtMagnifier::~QwtMagnifier()
70{
71 delete d_data;
72}
73
74/*!
75 \brief En/disable the magnifier
76
77 When enabled is true an event filter is installed for
78 the observed widget, otherwise the event filter is removed.
79
80 \param on true or false
81 \sa isEnabled(), eventFilter()
82*/
83void QwtMagnifier::setEnabled( bool on )
84{
85 if ( d_data->isEnabled != on )
86 {
87 d_data->isEnabled = on;
88
89 QObject *o = parent();
90 if ( o )
91 {
92 if ( d_data->isEnabled )
93 o->installEventFilter( this );
94 else
95 o->removeEventFilter( this );
96 }
97 }
98}
99
100/*!
101 \return true when enabled, false otherwise
102 \sa setEnabled(), eventFilter()
103*/
104bool QwtMagnifier::isEnabled() const
105{
106 return d_data->isEnabled;
107}
108
109/*!
110 \brief Change the wheel factor
111
112 The wheel factor defines the ratio between the current range
113 on the parent widget and the zoomed range for each step of the wheel.
114
115 Use values > 1 for magnification (i.e. 2.0) and values < 1 for
116 scaling down (i.e. 1/2.0 = 0.5). You can use this feature for
117 inverting the direction of the wheel.
118
119 The default value is 0.9.
120
121 \param factor Wheel factor
122 \sa wheelFactor(), setWheelButtonState(),
123 setMouseFactor(), setKeyFactor()
124*/
125void QwtMagnifier::setWheelFactor( double factor )
126{
127 d_data->wheelFactor = factor;
128}
129
130/*!
131 \return Wheel factor
132 \sa setWheelFactor()
133*/
134double QwtMagnifier::wheelFactor() const
135{
136 return d_data->wheelFactor;
137}
138
139/*!
140 Assign keyboard modifiers for zooming in/out using the wheel.
141 The default modifiers are Qt::NoModifiers.
142
143 \param modifiers Keyboard modifiers
144 \sa wheelModifiers()
145*/
146void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
147{
148 d_data->wheelModifiers = modifiers;
149}
150
151/*!
152 \return Wheel modifiers
153 \sa setWheelModifiers()
154*/
155Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
156{
157 return d_data->wheelModifiers;
158}
159
160/*!
161 \brief Change the mouse factor
162
163 The mouse factor defines the ratio between the current range
164 on the parent widget and the zoomed range for each vertical mouse movement.
165 The default value is 0.95.
166
167 \param factor Wheel factor
168 \sa mouseFactor(), setMouseButton(), setWheelFactor(), setKeyFactor()
169*/
170void QwtMagnifier::setMouseFactor( double factor )
171{
172 d_data->mouseFactor = factor;
173}
174
175/*!
176 \return Mouse factor
177 \sa setMouseFactor()
178*/
179double QwtMagnifier::mouseFactor() const
180{
181 return d_data->mouseFactor;
182}
183
184/*!
185 Assign the mouse button, that is used for zooming in/out.
186 The default value is Qt::RightButton.
187
188 \param button Button
189 \param modifiers Keyboard modifiers
190
191 \sa getMouseButton()
192*/
193void QwtMagnifier::setMouseButton(
194 Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
195{
196 d_data->mouseButton = button;
197 d_data->mouseButtonModifiers = modifiers;
198}
199
200//! \sa setMouseButton()
201void QwtMagnifier::getMouseButton(
202 Qt::MouseButton &button, Qt::KeyboardModifiers &modifiers ) const
203{
204 button = d_data->mouseButton;
205 modifiers = d_data->mouseButtonModifiers;
206}
207
208/*!
209 \brief Change the key factor
210
211 The key factor defines the ratio between the current range
212 on the parent widget and the zoomed range for each key press of
213 the zoom in/out keys. The default value is 0.9.
214
215 \param factor Key factor
216 \sa keyFactor(), setZoomInKey(), setZoomOutKey(),
217 setWheelFactor, setMouseFactor()
218*/
219void QwtMagnifier::setKeyFactor( double factor )
220{
221 d_data->keyFactor = factor;
222}
223
224/*!
225 \return Key factor
226 \sa setKeyFactor()
227*/
228double QwtMagnifier::keyFactor() const
229{
230 return d_data->keyFactor;
231}
232
233/*!
234 Assign the key, that is used for zooming in.
235 The default combination is Qt::Key_Plus + Qt::NoModifier.
236
237 \param key
238 \param modifiers
239 \sa getZoomInKey(), setZoomOutKey()
240*/
241void QwtMagnifier::setZoomInKey( int key,
242 Qt::KeyboardModifiers modifiers )
243{
244 d_data->zoomInKey = key;
245 d_data->zoomInKeyModifiers = modifiers;
246}
247
248/*!
249 \brief Retrieve the settings of the zoom in key
250
251 \param key Key code, see Qt::Key
252 \param modifiers Keyboard modifiers
253
254 \sa setZoomInKey()
255*/
256void QwtMagnifier::getZoomInKey( int &key,
257 Qt::KeyboardModifiers &modifiers ) const
258{
259 key = d_data->zoomInKey;
260 modifiers = d_data->zoomInKeyModifiers;
261}
262
263/*!
264 Assign the key, that is used for zooming out.
265 The default combination is Qt::Key_Minus + Qt::NoModifier.
266
267 \param key
268 \param modifiers
269 \sa getZoomOutKey(), setZoomOutKey()
270*/
271void QwtMagnifier::setZoomOutKey( int key,
272 Qt::KeyboardModifiers modifiers )
273{
274 d_data->zoomOutKey = key;
275 d_data->zoomOutKeyModifiers = modifiers;
276}
277
278/*!
279 \brief Retrieve the settings of the zoom out key
280
281 \param key Key code, see Qt::Key
282 \param modifiers Keyboard modifiers
283
284 \sa setZoomOutKey()
285*/
286void QwtMagnifier::getZoomOutKey( int &key,
287 Qt::KeyboardModifiers &modifiers ) const
288{
289 key = d_data->zoomOutKey;
290 modifiers = d_data->zoomOutKeyModifiers;
291}
292
293/*!
294 \brief Event filter
295
296 When isEnabled() is true, the mouse events of the
297 observed widget are filtered.
298
299 \param object Object to be filtered
300 \param event Event
301
302 \return Forwarded to QObject::eventFilter()
303
304 \sa widgetMousePressEvent(), widgetMouseReleaseEvent(),
305 widgetMouseMoveEvent(), widgetWheelEvent(), widgetKeyPressEvent()
306 widgetKeyReleaseEvent()
307*/
308bool QwtMagnifier::eventFilter( QObject *object, QEvent *event )
309{
310 if ( object && object == parent() )
311 {
312 switch ( event->type() )
313 {
314 case QEvent::MouseButtonPress:
315 {
316 widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
317 break;
318 }
319 case QEvent::MouseMove:
320 {
321 widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
322 break;
323 }
324 case QEvent::MouseButtonRelease:
325 {
326 widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
327 break;
328 }
329 case QEvent::Wheel:
330 {
331 widgetWheelEvent( static_cast<QWheelEvent *>( event ) );
332 break;
333 }
334 case QEvent::KeyPress:
335 {
336 widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
337 break;
338 }
339 case QEvent::KeyRelease:
340 {
341 widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
342 break;
343 }
344 default:;
345 }
346 }
347 return QObject::eventFilter( object, event );
348}
349
350/*!
351 Handle a mouse press event for the observed widget.
352
353 \param mouseEvent Mouse event
354 \sa eventFilter(), widgetMouseReleaseEvent(), widgetMouseMoveEvent()
355*/
356void QwtMagnifier::widgetMousePressEvent( QMouseEvent *mouseEvent )
357{
358 if ( parentWidget() == NULL )
359 return;
360
361 if ( ( mouseEvent->button() != d_data->mouseButton ) ||
362 ( mouseEvent->modifiers() != d_data->mouseButtonModifiers ) )
363 {
364 return;
365 }
366
367 d_data->hasMouseTracking = parentWidget()->hasMouseTracking();
368
369 parentWidget()->setMouseTracking( true );
370 d_data->mousePos = mouseEvent->pos();
371 d_data->mousePressed = true;
372}
373
374/*!
375 Handle a mouse release event for the observed widget.
376
377 \param mouseEvent Mouse event
378
379 \sa eventFilter(), widgetMousePressEvent(), widgetMouseMoveEvent(),
380*/
381void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
382{
383 Q_UNUSED( mouseEvent );
384
385 if ( d_data->mousePressed && parentWidget() )
386 {
387 d_data->mousePressed = false;
388 parentWidget()->setMouseTracking( d_data->hasMouseTracking );
389 }
390}
391
392/*!
393 Handle a mouse move event for the observed widget.
394
395 \param mouseEvent Mouse event
396 \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
397*/
398void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
399{
400 if ( !d_data->mousePressed )
401 return;
402
403 const int dy = mouseEvent->pos().y() - d_data->mousePos.y();
404 if ( dy != 0 )
405 {
406 double f = d_data->mouseFactor;
407 if ( dy < 0 )
408 f = 1 / f;
409
410 rescale( f );
411 }
412
413 d_data->mousePos = mouseEvent->pos();
414}
415
416/*!
417 Handle a wheel event for the observed widget.
418
419 \param wheelEvent Wheel event
420 \sa eventFilter()
421*/
422void QwtMagnifier::widgetWheelEvent( QWheelEvent *wheelEvent )
423{
424 if ( wheelEvent->modifiers() != d_data->wheelModifiers )
425 {
426 return;
427 }
428
429 if ( d_data->wheelFactor != 0.0 )
430 {
431 /*
432 A positive delta indicates that the wheel was
433 rotated forwards away from the user; a negative
434 value indicates that the wheel was rotated
435 backwards toward the user.
436 Most mouse types work in steps of 15 degrees,
437 in which case the delta value is a multiple
438 of 120 (== 15 * 8).
439 */
440 double f = qPow( d_data->wheelFactor,
441 qAbs( wheelEvent->delta() / 120.0 ) );
442
443 if ( wheelEvent->delta() > 0 )
444 f = 1 / f;
445
446 rescale( f );
447 }
448}
449
450/*!
451 Handle a key press event for the observed widget.
452
453 \param keyEvent Key event
454 \sa eventFilter(), widgetKeyReleaseEvent()
455*/
456void QwtMagnifier::widgetKeyPressEvent( QKeyEvent *keyEvent )
457{
458 if ( keyEvent->key() == d_data->zoomInKey &&
459 keyEvent->modifiers() == d_data->zoomInKeyModifiers )
460 {
461 rescale( d_data->keyFactor );
462 }
463 else if ( keyEvent->key() == d_data->zoomOutKey &&
464 keyEvent->modifiers() == d_data->zoomOutKeyModifiers )
465 {
466 rescale( 1.0 / d_data->keyFactor );
467 }
468}
469
470/*!
471 Handle a key release event for the observed widget.
472
473 \param keyEvent Key event
474 \sa eventFilter(), widgetKeyReleaseEvent()
475*/
476void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
477{
478 Q_UNUSED( keyEvent );
479}
480
481//! \return Parent widget, where the rescaling happens
482QWidget *QwtMagnifier::parentWidget()
483{
484 return qobject_cast<QWidget *>( parent() );
485}
486
487//! \return Parent widget, where the rescaling happens
488const QWidget *QwtMagnifier::parentWidget() const
489{
490 return qobject_cast<const QWidget *>( parent() );
491}
492
Note: See TracBrowser for help on using the repository browser.