qwt_magnifier.cpp 10.7 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* -*- 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
 *****************************************************************************/

// vim: expandtab

#include <math.h>
#include <qevent.h>
#include <qwidget.h>
#include "qwt_math.h"
#include "qwt_magnifier.h"

class QwtMagnifier::PrivateData
{
public:
    PrivateData():
        isEnabled(false),
        wheelFactor(0.9),
        wheelButtonState(Qt::NoButton),
        mouseFactor(0.95),
        mouseButton(Qt::RightButton),
        mouseButtonState(Qt::NoButton),
        keyFactor(0.9),
        zoomInKey(Qt::Key_Plus),
        zoomOutKey(Qt::Key_Minus),
#if QT_VERSION < 0x040000
        zoomInKeyModifiers(Qt::NoButton),
        zoomOutKeyModifiers(Qt::NoButton),
#else
        zoomInKeyModifiers(Qt::NoModifier),
        zoomOutKeyModifiers(Qt::NoModifier),
#endif
38
        mousePressed(false) {
pixhawk's avatar
pixhawk committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    }

    bool isEnabled;

    double wheelFactor;
    int wheelButtonState;

    double mouseFactor;
    int mouseButton;
    int mouseButtonState;

    double keyFactor;
    int zoomInKey;
    int zoomOutKey;
    int zoomInKeyModifiers;
    int zoomOutKeyModifiers;

    bool mousePressed;
    bool hasMouseTracking;
    QPoint mousePos;
};

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

//! 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()
*/
void QwtMagnifier::setEnabled(bool on)
{
89
    if ( d_data->isEnabled != on ) {
pixhawk's avatar
pixhawk committed
90 91 92
        d_data->isEnabled = on;

        QObject *o = parent();
93
        if ( o ) {
pixhawk's avatar
pixhawk committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            if ( d_data->isEnabled )
                o->installEventFilter(this);
            else
                o->removeEventFilter(this);
        }
    }
}

/*!
  \return true when enabled, false otherwise
  \sa setEnabled, eventFilter()
*/
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.
   The default value is 0.9.
117

pixhawk's avatar
pixhawk committed
118
   \param factor Wheel factor
119
   \sa wheelFactor(), setWheelButtonState(),
pixhawk's avatar
pixhawk committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
       setMouseFactor(), setKeyFactor()
*/
void QwtMagnifier::setWheelFactor(double factor)
{
    d_data->wheelFactor = factor;
}

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

/*!
   Assign a mandatory button state for zooming in/out using the wheel.
   The default button state is Qt::NoButton.

   \param buttonState Button state
   \sa wheelButtonState
*/
void QwtMagnifier::setWheelButtonState(int buttonState)
{
    d_data->wheelButtonState = buttonState;
}

/*!
   \return Wheel button state
   \sa setWheelButtonState
*/
int QwtMagnifier::wheelButtonState() const
{
    return d_data->wheelButtonState;
}

157
/*!
pixhawk's avatar
pixhawk committed
158 159 160 161 162
   \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.
163

pixhawk's avatar
pixhawk committed
164 165
   \param factor Wheel factor
   \sa mouseFactor(), setMouseButton(), setWheelFactor(), setKeyFactor()
166
*/
pixhawk's avatar
pixhawk committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
void QwtMagnifier::setMouseFactor(double factor)
{
    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
   \param buttonState Button state
   \sa getMouseButton
*/
void QwtMagnifier::setMouseButton(int button, int buttonState)
{
    d_data->mouseButton = button;
    d_data->mouseButtonState = buttonState;
}

//! \sa setMouseButton
void QwtMagnifier::getMouseButton(
    int &button, int &buttonState) const
{
    button = d_data->mouseButton;
    buttonState = d_data->mouseButtonState;
}

/*!
   \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.
209

pixhawk's avatar
pixhawk committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
   \param factor Key factor
   \sa keyFactor(), setZoomInKey(), setZoomOutKey(),
       setWheelFactor, setMouseFactor()
*/
void QwtMagnifier::setKeyFactor(double factor)
{
    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()
*/
void QwtMagnifier::setZoomInKey(int key, int modifiers)
{
    d_data->zoomInKey = key;
    d_data->zoomInKeyModifiers = modifiers;
}

//! \sa setZoomInKey
void QwtMagnifier::getZoomInKey(int &key, int &modifiers) const
{
    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()
*/
void QwtMagnifier::setZoomOutKey(int key, int modifiers)
{
    d_data->zoomOutKey = key;
    d_data->zoomOutKeyModifiers = modifiers;
}

//! \sa setZoomOutKey
void QwtMagnifier::getZoomOutKey(int &key, int &modifiers) const
{
    key = d_data->zoomOutKey;
    modifiers = d_data->zoomOutKeyModifiers;
}

/*!
  \brief Event filter

  When isEnabled() the mouse events of the observed widget are filtered.

  \sa widgetMousePressEvent(), widgetMouseReleaseEvent(),
      widgetMouseMoveEvent(), widgetWheelEvent(), widgetKeyPressEvent()
      widgetKeyReleaseEvent()
*/
bool QwtMagnifier::eventFilter(QObject *o, QEvent *e)
{
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    if ( o && o == parent() ) {
        switch(e->type() ) {
        case QEvent::MouseButtonPress: {
            widgetMousePressEvent((QMouseEvent *)e);
            break;
        }
        case QEvent::MouseMove: {
            widgetMouseMoveEvent((QMouseEvent *)e);
            break;
        }
        case QEvent::MouseButtonRelease: {
            widgetMouseReleaseEvent((QMouseEvent *)e);
            break;
        }
        case QEvent::Wheel: {
            widgetWheelEvent((QWheelEvent *)e);
            break;
        }
        case QEvent::KeyPress: {
            widgetKeyPressEvent((QKeyEvent *)e);
            break;
        }
        case QEvent::KeyRelease: {
            widgetKeyReleaseEvent((QKeyEvent *)e);
            break;
        }
        default:
            ;
pixhawk's avatar
pixhawk committed
309 310 311 312 313 314 315 316 317
        }
    }
    return QObject::eventFilter(o, e);
}

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

  \param me Mouse event
318
  \sa eventFilter(), widgetMouseReleaseEvent(), widgetMouseMoveEvent()
pixhawk's avatar
pixhawk committed
319 320 321 322 323 324 325 326
*/
void QwtMagnifier::widgetMousePressEvent(QMouseEvent *me)
{
    if ( me->button() != d_data->mouseButton || parentWidget() == NULL )
        return;

#if QT_VERSION < 0x040000
    if ( (me->state() & Qt::KeyButtonMask) !=
327
            (d_data->mouseButtonState & Qt::KeyButtonMask) )
pixhawk's avatar
pixhawk committed
328 329
#else
    if ( (me->modifiers() & Qt::KeyboardModifierMask) !=
330
            (int)(d_data->mouseButtonState & Qt::KeyboardModifierMask) )
pixhawk's avatar
pixhawk committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
#endif
    {
        return;
    }

    d_data->hasMouseTracking = parentWidget()->hasMouseTracking();
    parentWidget()->setMouseTracking(true);
    d_data->mousePos = me->pos();
    d_data->mousePressed = true;
}

/*!
  Handle a mouse release event for the observed widget.
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseMoveEvent(),
*/
void QwtMagnifier::widgetMouseReleaseEvent(QMouseEvent *)
{
348
    if ( d_data->mousePressed && parentWidget() ) {
pixhawk's avatar
pixhawk committed
349 350 351 352 353 354 355
        d_data->mousePressed = false;
        parentWidget()->setMouseTracking(d_data->hasMouseTracking);
    }
}

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

pixhawk's avatar
pixhawk committed
357 358
  \param me Mouse event
  \sa eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(),
359
*/
pixhawk's avatar
pixhawk committed
360 361 362 363 364 365
void QwtMagnifier::widgetMouseMoveEvent(QMouseEvent *me)
{
    if ( !d_data->mousePressed )
        return;

    const int dy = me->pos().y() - d_data->mousePos.y();
366
    if ( dy != 0 ) {
pixhawk's avatar
pixhawk committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        double f = d_data->mouseFactor;
        if ( dy < 0 )
            f = 1 / f;

        rescale(f);
    }

    d_data->mousePos = me->pos();
}

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

  \param we Wheel event
  \sa eventFilter()
*/
void QwtMagnifier::widgetWheelEvent(QWheelEvent *we)
{
#if QT_VERSION < 0x040000
    if ( (we->state() & Qt::KeyButtonMask) !=
387
            (d_data->wheelButtonState & Qt::KeyButtonMask) )
pixhawk's avatar
pixhawk committed
388 389
#else
    if ( (we->modifiers() & Qt::KeyboardModifierMask) !=
390
            (int)(d_data->wheelButtonState & Qt::KeyboardModifierMask) )
pixhawk's avatar
pixhawk committed
391 392 393 394 395
#endif
    {
        return;
    }

396 397 398 399 400 401 402 403 404 405 406 407
    if ( d_data->wheelFactor != 0.0 ) {
        /*
            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).
         */
        double f = ::pow(d_data->wheelFactor,
                         qwtAbs(we->delta() / 120));
pixhawk's avatar
pixhawk committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        if ( we->delta() > 0 )
            f = 1 / f;

        rescale(f);
    }
}

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

  \param ke Key event
  \sa eventFilter(), widgetKeyReleaseEvent()
*/
void QwtMagnifier::widgetKeyPressEvent(QKeyEvent *ke)
{
    const int key = ke->key();
#if QT_VERSION < 0x040000
    const int state = ke->state();
#else
    const int state = ke->modifiers();
#endif

430 431
    if ( key == d_data->zoomInKey &&
            state == d_data->zoomInKeyModifiers ) {
pixhawk's avatar
pixhawk committed
432
        rescale(d_data->keyFactor);
433 434
    } else if ( key == d_data->zoomOutKey &&
                state == d_data->zoomOutKeyModifiers ) {
pixhawk's avatar
pixhawk committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        rescale(1.0 / d_data->keyFactor);
    }
}

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

  \param ke Key event
  \sa eventFilter(), widgetKeyReleaseEvent()
*/
void QwtMagnifier::widgetKeyReleaseEvent(QKeyEvent *)
{
}

QWidget *QwtMagnifier::parentWidget()
{
    if ( parent()->inherits("QWidget") )
        return (QWidget *)parent();

    return NULL;
}

const QWidget *QwtMagnifier::parentWidget() const
{
    if ( parent()->inherits("QWidget") )
        return (const QWidget *)parent();

    return NULL;
}