1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
15
#include "qwt_plot_canvas.h"
16
#include "qwt_scale_div.h"
17
#include "qwt_plot_magnifier.h"
19
class QwtPlotMagnifier::PrivateData
25
wheelButtonState(Qt::NoButton),
27
mouseButton(Qt::RightButton),
28
mouseButtonState(Qt::NoButton),
30
zoomInKey(Qt::Key_Plus),
31
zoomOutKey(Qt::Key_Minus),
32
#if QT_VERSION < 0x040000
33
zoomInKeyButtonState(Qt::NoButton),
34
zoomOutKeyButtonState(Qt::NoButton),
36
zoomInKeyButtonState(Qt::NoModifier),
37
zoomOutKeyButtonState(Qt::NoModifier),
41
for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
42
isAxisEnabled[axis] = true;
57
int zoomInKeyButtonState;
58
int zoomOutKeyButtonState;
60
bool isAxisEnabled[QwtPlot::axisCnt];
63
bool hasMouseTracking;
67
QwtPlotMagnifier::QwtPlotMagnifier(QwtPlotCanvas *canvas):
70
d_data = new PrivateData();
74
QwtPlotMagnifier::~QwtPlotMagnifier()
79
void QwtPlotMagnifier::setEnabled(bool on)
81
if ( d_data->isEnabled != on )
83
d_data->isEnabled = on;
85
QObject *o = parent();
88
if ( d_data->isEnabled )
89
o->installEventFilter(this);
91
o->removeEventFilter(this);
96
bool QwtPlotMagnifier::isEnabled() const
98
return d_data->isEnabled;
101
void QwtPlotMagnifier::setWheelFactor(double factor)
103
d_data->wheelFactor = factor;
106
double QwtPlotMagnifier::wheelFactor() const
108
return d_data->wheelFactor;
111
void QwtPlotMagnifier::setWheelButtonState(int buttonState)
113
d_data->wheelButtonState = buttonState;
116
int QwtPlotMagnifier::wheelButtonState() const
118
return d_data->wheelButtonState;
121
void QwtPlotMagnifier::setMouseFactor(double factor)
123
d_data->mouseFactor = factor;
126
double QwtPlotMagnifier::mouseFactor() const
128
return d_data->mouseFactor;
131
void QwtPlotMagnifier::setMouseButton(int button, int buttonState)
133
d_data->mouseButton = button;
134
d_data->mouseButtonState = buttonState;
137
void QwtPlotMagnifier::getMouseButton(
138
int &button, int &buttonState) const
140
button = d_data->mouseButton;
141
buttonState = d_data->mouseButtonState;
144
void QwtPlotMagnifier::setKeyFactor(double factor)
146
d_data->keyFactor = factor;
149
double QwtPlotMagnifier::keyFactor() const
151
return d_data->keyFactor;
154
void QwtPlotMagnifier::setZoomInKey(int key, int buttonState)
156
d_data->zoomInKey = key;
157
d_data->zoomInKeyButtonState = buttonState;
160
void QwtPlotMagnifier::getZoomInKey(int &key, int &buttonState)
162
key = d_data->zoomInKey;
163
buttonState = d_data->zoomInKeyButtonState;
166
void QwtPlotMagnifier::setZoomOutKey(int key, int buttonState)
168
d_data->zoomOutKey = key;
169
d_data->zoomOutKeyButtonState = buttonState;
172
void QwtPlotMagnifier::getZoomOutKey(int &key, int &buttonState)
174
key = d_data->zoomOutKey;
175
buttonState = d_data->zoomOutKeyButtonState;
178
void QwtPlotMagnifier::setAxisEnabled(int axis, bool on)
180
if ( axis >= 0 && axis <= QwtPlot::axisCnt )
181
d_data->isAxisEnabled[axis] = on;
184
bool QwtPlotMagnifier::isAxisEnabled(int axis) const
186
if ( axis >= 0 && axis <= QwtPlot::axisCnt )
187
return d_data->isAxisEnabled[axis];
192
//! Return observed plot canvas
193
QwtPlotCanvas *QwtPlotMagnifier::canvas()
195
QObject *w = parent();
196
if ( w && w->inherits("QwtPlotCanvas") )
197
return (QwtPlotCanvas *)w;
202
//! Return Observed plot canvas
203
const QwtPlotCanvas *QwtPlotMagnifier::canvas() const
205
return ((QwtPlotMagnifier *)this)->canvas();
208
//! Return plot widget, containing the observed plot canvas
209
QwtPlot *QwtPlotMagnifier::plot()
211
QObject *w = canvas();
215
if ( w && w->inherits("QwtPlot") )
222
//! Return plot widget, containing the observed plot canvas
223
const QwtPlot *QwtPlotMagnifier::plot() const
225
return ((QwtPlotMagnifier *)this)->plot();
228
bool QwtPlotMagnifier::eventFilter(QObject *o, QEvent *e)
230
if ( o && o == parent() )
234
case QEvent::MouseButtonPress:
236
widgetMousePressEvent((QMouseEvent *)e);
239
case QEvent::MouseMove:
241
widgetMouseMoveEvent((QMouseEvent *)e);
244
case QEvent::MouseButtonRelease:
246
widgetMouseReleaseEvent((QMouseEvent *)e);
251
widgetWheelEvent((QWheelEvent *)e);
254
case QEvent::KeyPress:
256
widgetKeyPressEvent((QKeyEvent *)e);
259
case QEvent::KeyRelease:
261
widgetKeyReleaseEvent((QKeyEvent *)e);
267
return QObject::eventFilter(o, e);
270
void QwtPlotMagnifier::widgetMousePressEvent(QMouseEvent *me)
272
if ( me->button() != d_data->mouseButton )
275
#if QT_VERSION < 0x040000
276
if ( (me->state() & Qt::KeyButtonMask) !=
277
(d_data->mouseButtonState & Qt::KeyButtonMask) )
279
if ( (me->modifiers() & Qt::KeyboardModifierMask) !=
280
(int)(d_data->mouseButtonState & Qt::KeyboardModifierMask) )
286
d_data->hasMouseTracking = canvas()->hasMouseTracking();
287
canvas()->setMouseTracking(true);
288
d_data->mousePos = me->pos();
289
d_data->mousePressed = true;
292
void QwtPlotMagnifier::widgetMouseReleaseEvent(QMouseEvent *)
294
d_data->mousePressed = false;
295
canvas()->setMouseTracking(d_data->hasMouseTracking);
298
void QwtPlotMagnifier::widgetMouseMoveEvent(QMouseEvent *me)
300
if ( !d_data->mousePressed )
303
const int dy = me->pos().y() - d_data->mousePos.y();
306
double f = d_data->mouseFactor;
313
d_data->mousePos = me->pos();
316
void QwtPlotMagnifier::widgetWheelEvent(QWheelEvent *we)
318
#if QT_VERSION < 0x040000
319
if ( (we->state() & Qt::KeyButtonMask) !=
320
(d_data->wheelButtonState & Qt::KeyButtonMask) )
322
if ( (we->modifiers() & Qt::KeyboardModifierMask) !=
323
(int)(d_data->wheelButtonState & Qt::KeyboardModifierMask) )
329
if ( d_data->wheelFactor != 0.0 )
332
A positive delta indicates that the wheel was
333
rotated forwards away from the user; a negative
334
value indicates that the wheel was rotated
335
backwards toward the user.
336
Most mouse types work in steps of 15 degrees,
337
in which case the delta value is a multiple
340
double f = ::pow(d_data->wheelFactor,
341
qwtAbs(we->delta() / 120));
342
if ( we->delta() > 0 )
349
void QwtPlotMagnifier::widgetKeyPressEvent(QKeyEvent *ke)
351
const int key = ke->key();
352
#if QT_VERSION < 0x040000
353
const int state = ke->state();
355
const int state = ke->modifiers();
358
if ( key == d_data->zoomInKey &&
359
state == d_data->zoomInKeyButtonState )
361
rescale(d_data->keyFactor);
363
else if ( key == d_data->zoomOutKey &&
364
state == d_data->zoomOutKeyButtonState )
366
rescale(1.0 / d_data->keyFactor);
370
void QwtPlotMagnifier::widgetKeyReleaseEvent(QKeyEvent *)
374
void QwtPlotMagnifier::rescale(double factor)
376
if ( factor == 1.0 || factor == 0.0 )
379
bool doReplot = false;
380
QwtPlot* plt = plot();
382
const bool autoReplot = plt->autoReplot();
383
plt->setAutoReplot(false);
385
for ( int axisId = 0; axisId < QwtPlot::axisCnt; axisId++ )
387
const QwtScaleDiv *scaleDiv = plt->axisScaleDiv(axisId);
388
if ( isAxisEnabled(axisId) && scaleDiv->isValid() )
390
const double center =
391
scaleDiv->lBound() + scaleDiv->range() / 2;
392
const double width_2 = scaleDiv->range() / 2 * factor;
394
plt->setAxisScale(axisId, center - width_2, center + width_2);
399
plt->setAutoReplot(autoReplot);