qwt_magnifier.cpp 11.5 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
2
3
4
5
6
7
8
9
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

Bryant's avatar
Bryant committed
10
11
#include "qwt_magnifier.h"
#include "qwt_math.h"
pixhawk's avatar
pixhawk committed
12
13
14
15
16
17
18
#include <qevent.h>
#include <qwidget.h>

class QwtMagnifier::PrivateData
{
public:
    PrivateData():
Bryant's avatar
Bryant committed
19
20
21
22
23
24
25
26
27
28
29
30
31
        isEnabled( false ),
        wheelFactor( 0.9 ),
        wheelModifiers( Qt::NoModifier ),
        mouseFactor( 0.95 ),
        mouseButton( Qt::RightButton ),
        mouseButtonModifiers( Qt::NoModifier ),
        keyFactor( 0.9 ),
        zoomInKey( Qt::Key_Plus ),
        zoomInKeyModifiers( Qt::NoModifier ),
        zoomOutKey( Qt::Key_Minus ),
        zoomOutKeyModifiers( Qt::NoModifier ),
        mousePressed( false )
    {
pixhawk's avatar
pixhawk committed
32
33
34
35
36
    }

    bool isEnabled;

    double wheelFactor;
Bryant's avatar
Bryant committed
37
    Qt::KeyboardModifiers wheelModifiers;
pixhawk's avatar
pixhawk committed
38
39

    double mouseFactor;
Bryant's avatar
Bryant committed
40
41
42

    Qt::MouseButton mouseButton;
    Qt::KeyboardModifiers mouseButtonModifiers;
pixhawk's avatar
pixhawk committed
43
44

    double keyFactor;
Bryant's avatar
Bryant committed
45

pixhawk's avatar
pixhawk committed
46
    int zoomInKey;
Bryant's avatar
Bryant committed
47
48
    Qt::KeyboardModifiers zoomInKeyModifiers;

pixhawk's avatar
pixhawk committed
49
    int zoomOutKey;
Bryant's avatar
Bryant committed
50
    Qt::KeyboardModifiers  zoomOutKeyModifiers;
pixhawk's avatar
pixhawk committed
51
52
53
54
55
56

    bool mousePressed;
    bool hasMouseTracking;
    QPoint mousePos;
};

57
/*!
pixhawk's avatar
pixhawk committed
58
59
60
   Constructor
   \param parent Widget to be magnified
*/
Bryant's avatar
Bryant committed
61
62
QwtMagnifier::QwtMagnifier( QWidget *parent ):
    QObject( parent )
pixhawk's avatar
pixhawk committed
63
64
{
    d_data = new PrivateData();
Bryant's avatar
Bryant committed
65
    setEnabled( true );
pixhawk's avatar
pixhawk committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
}

//! Destructor
QwtMagnifier::~QwtMagnifier()
{
    delete d_data;
}

/*!
  \brief En/disable the magnifier

  When enabled is true an event filter is installed for
  the observed widget, otherwise the event filter is removed.

  \param on true or false
  \sa isEnabled(), eventFilter()
*/
Bryant's avatar
Bryant committed
83
void QwtMagnifier::setEnabled( bool on )
pixhawk's avatar
pixhawk committed
84
{
Bryant's avatar
Bryant committed
85
86
    if ( d_data->isEnabled != on )
    {
pixhawk's avatar
pixhawk committed
87
88
89
        d_data->isEnabled = on;

        QObject *o = parent();
Bryant's avatar
Bryant committed
90
91
        if ( o )
        {
pixhawk's avatar
pixhawk committed
92
            if ( d_data->isEnabled )
Bryant's avatar
Bryant committed
93
                o->installEventFilter( this );
pixhawk's avatar
pixhawk committed
94
            else
Bryant's avatar
Bryant committed
95
                o->removeEventFilter( this );
pixhawk's avatar
pixhawk committed
96
97
98
99
100
101
        }
    }
}

/*!
  \return true when enabled, false otherwise
Bryant's avatar
Bryant committed
102
  \sa setEnabled(), eventFilter()
pixhawk's avatar
pixhawk committed
103
104
105
106
107
108
109
110
111
112
113
*/
bool QwtMagnifier::isEnabled() const
{
    return d_data->isEnabled;
}

/*!
   \brief Change the wheel factor

   The wheel factor defines the ratio between the current range
   on the parent widget and the zoomed range for each step of the wheel.
Bryant's avatar
Bryant committed
114
115
116
117
118

   Use values > 1 for magnification (i.e. 2.0) and values < 1 for
   scaling down (i.e. 1/2.0 = 0.5). You can use this feature for
   inverting the direction of the wheel.

pixhawk's avatar
pixhawk committed
119
   The default value is 0.9.
120

pixhawk's avatar
pixhawk committed
121
   \param factor Wheel factor
122
   \sa wheelFactor(), setWheelButtonState(),
pixhawk's avatar
pixhawk committed
123
124
       setMouseFactor(), setKeyFactor()
*/
Bryant's avatar
Bryant committed
125
void QwtMagnifier::setWheelFactor( double factor )
pixhawk's avatar
pixhawk committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
    d_data->wheelFactor = factor;
}

/*!
   \return Wheel factor
   \sa setWheelFactor()
*/
double QwtMagnifier::wheelFactor() const
{
    return d_data->wheelFactor;
}

/*!
Bryant's avatar
Bryant committed
140
141
   Assign keyboard modifiers for zooming in/out using the wheel.
   The default modifiers are Qt::NoModifiers.
pixhawk's avatar
pixhawk committed
142

Bryant's avatar
Bryant committed
143
144
   \param modifiers Keyboard modifiers
   \sa wheelModifiers()
pixhawk's avatar
pixhawk committed
145
*/
Bryant's avatar
Bryant committed
146
void QwtMagnifier::setWheelModifiers( Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
147
{
Bryant's avatar
Bryant committed
148
    d_data->wheelModifiers = modifiers;
pixhawk's avatar
pixhawk committed
149
150
151
}

/*!
Bryant's avatar
Bryant committed
152
153
   \return Wheel modifiers
   \sa setWheelModifiers()
pixhawk's avatar
pixhawk committed
154
*/
Bryant's avatar
Bryant committed
155
Qt::KeyboardModifiers QwtMagnifier::wheelModifiers() const
pixhawk's avatar
pixhawk committed
156
{
Bryant's avatar
Bryant committed
157
    return d_data->wheelModifiers;
pixhawk's avatar
pixhawk committed
158
159
}

160
/*!
pixhawk's avatar
pixhawk committed
161
162
163
164
165
   \brief Change the mouse factor

   The mouse factor defines the ratio between the current range
   on the parent widget and the zoomed range for each vertical mouse movement.
   The default value is 0.95.
166

pixhawk's avatar
pixhawk committed
167
168
   \param factor Wheel factor
   \sa mouseFactor(), setMouseButton(), setWheelFactor(), setKeyFactor()
169
*/
Bryant's avatar
Bryant committed
170
void QwtMagnifier::setMouseFactor( double factor )
pixhawk's avatar
pixhawk committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{
    d_data->mouseFactor = factor;
}

/*!
   \return Mouse factor
   \sa setMouseFactor()
*/
double QwtMagnifier::mouseFactor() const
{
    return d_data->mouseFactor;
}

/*!
   Assign the mouse button, that is used for zooming in/out.
   The default value is Qt::RightButton.

   \param button Button
Bryant's avatar
Bryant committed
189
190
191
   \param modifiers Keyboard modifiers

   \sa getMouseButton()
pixhawk's avatar
pixhawk committed
192
*/
Bryant's avatar
Bryant committed
193
194
void QwtMagnifier::setMouseButton( 
    Qt::MouseButton button, Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
195
196
{
    d_data->mouseButton = button;
Bryant's avatar
Bryant committed
197
    d_data->mouseButtonModifiers = modifiers;
pixhawk's avatar
pixhawk committed
198
199
}

Bryant's avatar
Bryant committed
200
//! \sa setMouseButton()
pixhawk's avatar
pixhawk committed
201
void QwtMagnifier::getMouseButton(
Bryant's avatar
Bryant committed
202
    Qt::MouseButton &button, Qt::KeyboardModifiers &modifiers ) const
pixhawk's avatar
pixhawk committed
203
204
{
    button = d_data->mouseButton;
Bryant's avatar
Bryant committed
205
    modifiers = d_data->mouseButtonModifiers;
pixhawk's avatar
pixhawk committed
206
207
208
209
210
211
212
213
}

/*!
   \brief Change the key factor

   The key factor defines the ratio between the current range
   on the parent widget and the zoomed range for each key press of
   the zoom in/out keys. The default value is 0.9.
214

pixhawk's avatar
pixhawk committed
215
216
217
218
   \param factor Key factor
   \sa keyFactor(), setZoomInKey(), setZoomOutKey(),
       setWheelFactor, setMouseFactor()
*/
Bryant's avatar
Bryant committed
219
void QwtMagnifier::setKeyFactor( double factor )
pixhawk's avatar
pixhawk committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
{
    d_data->keyFactor = factor;
}

/*!
   \return Key factor
   \sa setKeyFactor()
*/
double QwtMagnifier::keyFactor() const
{
    return d_data->keyFactor;
}

/*!
   Assign the key, that is used for zooming in.
   The default combination is Qt::Key_Plus + Qt::NoModifier.

   \param key
   \param modifiers
   \sa getZoomInKey(), setZoomOutKey()
*/
Bryant's avatar
Bryant committed
241
242
void QwtMagnifier::setZoomInKey( int key, 
    Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
243
244
245
246
247
{
    d_data->zoomInKey = key;
    d_data->zoomInKeyModifiers = modifiers;
}

Bryant's avatar
Bryant committed
248
249
250
251
252
253
254
255
256
257
/*! 
   \brief Retrieve the settings of the zoom in key

   \param key Key code, see Qt::Key
   \param modifiers Keyboard modifiers

   \sa setZoomInKey()
*/
void QwtMagnifier::getZoomInKey( int &key, 
    Qt::KeyboardModifiers &modifiers ) const
pixhawk's avatar
pixhawk committed
258
259
260
261
262
263
264
265
266
267
268
269
270
{
    key = d_data->zoomInKey;
    modifiers = d_data->zoomInKeyModifiers;
}

/*!
   Assign the key, that is used for zooming out.
   The default combination is Qt::Key_Minus + Qt::NoModifier.

   \param key
   \param modifiers
   \sa getZoomOutKey(), setZoomOutKey()
*/
Bryant's avatar
Bryant committed
271
272
void QwtMagnifier::setZoomOutKey( int key, 
    Qt::KeyboardModifiers modifiers )
pixhawk's avatar
pixhawk committed
273
274
275
276
277
{
    d_data->zoomOutKey = key;
    d_data->zoomOutKeyModifiers = modifiers;
}

Bryant's avatar
Bryant committed
278
279
280
281
282
283
284
285
286
287
/*! 
   \brief Retrieve the settings of the zoom out key

   \param key Key code, see Qt::Key
   \param modifiers Keyboard modifiers

   \sa setZoomOutKey()
*/
void QwtMagnifier::getZoomOutKey( int &key, 
    Qt::KeyboardModifiers &modifiers ) const
pixhawk's avatar
pixhawk committed
288
289
290
291
292
293
294
295
{
    key = d_data->zoomOutKey;
    modifiers = d_data->zoomOutKeyModifiers;
}

/*!
  \brief Event filter

Bryant's avatar
Bryant committed
296
297
298
299
300
301
302
  When isEnabled() is true, the mouse events of the
  observed widget are filtered.

  \param object Object to be filtered
  \param event Event

  \return Forwarded to QObject::eventFilter()
pixhawk's avatar
pixhawk committed
303
304
305
306
307

  \sa widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseMoveEvent(), widgetWheelEvent(), widgetKeyPressEvent()
      widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
308
bool QwtMagnifier::eventFilter( QObject *object, QEvent *event )
pixhawk's avatar
pixhawk committed
309
{
Bryant's avatar
Bryant committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    if ( object && object == parent() )
    {
        switch ( event->type() )
        {
            case QEvent::MouseButtonPress:
            {
                widgetMousePressEvent( static_cast<QMouseEvent *>( event ) );
                break;
            }
            case QEvent::MouseMove:
            {
                widgetMouseMoveEvent( static_cast<QMouseEvent *>( event ) );
                break;
            }
            case QEvent::MouseButtonRelease:
            {
                widgetMouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
                break;
            }
            case QEvent::Wheel:
            {
                widgetWheelEvent( static_cast<QWheelEvent *>( event ) );
                break;
            }
            case QEvent::KeyPress:
            {
                widgetKeyPressEvent( static_cast<QKeyEvent *>( event ) );
                break;
            }
            case QEvent::KeyRelease:
            {
                widgetKeyReleaseEvent( static_cast<QKeyEvent *>( event ) );
                break;
            }
            default:;
pixhawk's avatar
pixhawk committed
345
346
        }
    }
Bryant's avatar
Bryant committed
347
    return QObject::eventFilter( object, event );
pixhawk's avatar
pixhawk committed
348
349
350
351
352
}

/*!
  Handle a mouse press event for the observed widget.

Bryant's avatar
Bryant committed
353
  \param mouseEvent Mouse event
354
  \sa eventFilter(), widgetMouseReleaseEvent(), widgetMouseMoveEvent()
pixhawk's avatar
pixhawk committed
355
*/
Bryant's avatar
Bryant committed
356
void QwtMagnifier::widgetMousePressEvent( QMouseEvent *mouseEvent )
pixhawk's avatar
pixhawk committed
357
{
Bryant's avatar
Bryant committed
358
    if ( parentWidget() == NULL )
pixhawk's avatar
pixhawk committed
359
360
        return;

Bryant's avatar
Bryant committed
361
362
    if ( ( mouseEvent->button() != d_data->mouseButton ) ||
        ( mouseEvent->modifiers() != d_data->mouseButtonModifiers ) )
pixhawk's avatar
pixhawk committed
363
364
365
366
367
    {
        return;
    }

    d_data->hasMouseTracking = parentWidget()->hasMouseTracking();
Bryant's avatar
Bryant committed
368
369
370

    parentWidget()->setMouseTracking( true );
    d_data->mousePos = mouseEvent->pos();
pixhawk's avatar
pixhawk committed
371
372
373
374
375
    d_data->mousePressed = true;
}

/*!
  Handle a mouse release event for the observed widget.
Bryant's avatar
Bryant committed
376
377
378

  \param mouseEvent Mouse event

pixhawk's avatar
pixhawk committed
379
380
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseMoveEvent(),
*/
Bryant's avatar
Bryant committed
381
void QwtMagnifier::widgetMouseReleaseEvent( QMouseEvent *mouseEvent )
pixhawk's avatar
pixhawk committed
382
{
Bryant's avatar
Bryant committed
383
384
385
386
    Q_UNUSED( mouseEvent );

    if ( d_data->mousePressed && parentWidget() )
    {
pixhawk's avatar
pixhawk committed
387
        d_data->mousePressed = false;
Bryant's avatar
Bryant committed
388
        parentWidget()->setMouseTracking( d_data->hasMouseTracking );
pixhawk's avatar
pixhawk committed
389
390
391
392
393
    }
}

/*!
  Handle a mouse move event for the observed widget.
394

Bryant's avatar
Bryant committed
395
  \param mouseEvent Mouse event
pixhawk's avatar
pixhawk committed
396
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
397
*/
Bryant's avatar
Bryant committed
398
void QwtMagnifier::widgetMouseMoveEvent( QMouseEvent *mouseEvent )
pixhawk's avatar
pixhawk committed
399
400
401
402
{
    if ( !d_data->mousePressed )
        return;

Bryant's avatar
Bryant committed
403
404
405
    const int dy = mouseEvent->pos().y() - d_data->mousePos.y();
    if ( dy != 0 )
    {
pixhawk's avatar
pixhawk committed
406
407
408
409
        double f = d_data->mouseFactor;
        if ( dy < 0 )
            f = 1 / f;

Bryant's avatar
Bryant committed
410
        rescale( f );
pixhawk's avatar
pixhawk committed
411
412
    }

Bryant's avatar
Bryant committed
413
    d_data->mousePos = mouseEvent->pos();
pixhawk's avatar
pixhawk committed
414
415
416
417
418
}

/*!
  Handle a wheel event for the observed widget.

Bryant's avatar
Bryant committed
419
  \param wheelEvent Wheel event
pixhawk's avatar
pixhawk committed
420
421
  \sa eventFilter()
*/
Bryant's avatar
Bryant committed
422
void QwtMagnifier::widgetWheelEvent( QWheelEvent *wheelEvent )
pixhawk's avatar
pixhawk committed
423
{
Bryant's avatar
Bryant committed
424
    if ( wheelEvent->modifiers() != d_data->wheelModifiers )
pixhawk's avatar
pixhawk committed
425
426
427
428
    {
        return;
    }

Bryant's avatar
Bryant committed
429
430
    if ( d_data->wheelFactor != 0.0 )
    {
431
432
433
434
435
436
437
438
439
        /*
            A positive delta indicates that the wheel was
            rotated forwards away from the user; a negative
            value indicates that the wheel was rotated
            backwards toward the user.
            Most mouse types work in steps of 15 degrees,
            in which case the delta value is a multiple
            of 120 (== 15 * 8).
         */
Bryant's avatar
Bryant committed
440
441
442
443
        double f = qPow( d_data->wheelFactor, 
            qAbs( wheelEvent->delta() / 120.0 ) );

        if ( wheelEvent->delta() > 0 )
pixhawk's avatar
pixhawk committed
444
445
            f = 1 / f;

Bryant's avatar
Bryant committed
446
        rescale( f );
pixhawk's avatar
pixhawk committed
447
448
449
450
451
452
    }
}

/*!
  Handle a key press event for the observed widget.

Bryant's avatar
Bryant committed
453
  \param keyEvent Key event
pixhawk's avatar
pixhawk committed
454
455
  \sa eventFilter(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
456
void QwtMagnifier::widgetKeyPressEvent( QKeyEvent *keyEvent )
pixhawk's avatar
pixhawk committed
457
{
Bryant's avatar
Bryant committed
458
459
460
461
462
463
464
465
466
    if ( keyEvent->key() == d_data->zoomInKey &&
        keyEvent->modifiers() == d_data->zoomInKeyModifiers )
    {
        rescale( d_data->keyFactor );
    }
    else if ( keyEvent->key() == d_data->zoomOutKey &&
        keyEvent->modifiers() == d_data->zoomOutKeyModifiers )
    {
        rescale( 1.0 / d_data->keyFactor );
pixhawk's avatar
pixhawk committed
467
468
469
470
471
472
    }
}

/*!
  Handle a key release event for the observed widget.

Bryant's avatar
Bryant committed
473
  \param keyEvent Key event
pixhawk's avatar
pixhawk committed
474
475
  \sa eventFilter(), widgetKeyReleaseEvent()
*/
Bryant's avatar
Bryant committed
476
void QwtMagnifier::widgetKeyReleaseEvent( QKeyEvent *keyEvent )
pixhawk's avatar
pixhawk committed
477
{
Bryant's avatar
Bryant committed
478
    Q_UNUSED( keyEvent );
pixhawk's avatar
pixhawk committed
479
480
}

Bryant's avatar
Bryant committed
481
//! \return Parent widget, where the rescaling happens
pixhawk's avatar
pixhawk committed
482
483
QWidget *QwtMagnifier::parentWidget()
{
Bryant's avatar
Bryant committed
484
    return qobject_cast<QWidget *>( parent() );
pixhawk's avatar
pixhawk committed
485
486
}

Bryant's avatar
Bryant committed
487
//! \return Parent widget, where the rescaling happens
pixhawk's avatar
pixhawk committed
488
489
const QWidget *QwtMagnifier::parentWidget() const
{
Bryant's avatar
Bryant committed
490
    return qobject_cast<const QWidget *>( parent() );
pixhawk's avatar
pixhawk committed
491
492
}