3
#include <qwt_scale_widget.h>
4
#include "scalepicker.h"
6
ScalePicker::ScalePicker(QwtPlot *plot):
9
for ( uint i = 0; i < QwtPlot::axisCnt; i++ )
11
QwtScaleWidget *scaleWidget = (QwtScaleWidget *)plot->axisWidget(i);
13
scaleWidget->installEventFilter(this);
17
bool ScalePicker::eventFilter(QObject *object, QEvent *e)
19
if ( object->inherits("QwtScaleWidget") &&
20
e->type() == QEvent::MouseButtonPress )
22
mouseClicked((const QwtScaleWidget *)object,
23
((QMouseEvent *)e)->pos());
27
return QObject::eventFilter(object, e);
30
void ScalePicker::mouseClicked(const QwtScaleWidget *scale, const QPoint &pos)
32
QRect rect = scaleRect(scale);
34
int margin = 10; // 10 pixels tolerance
35
rect.setRect(rect.x() - margin, rect.y() - margin,
36
rect.width() + 2 * margin, rect.height() + 2 * margin);
38
if ( rect.contains(pos) ) // No click on the title
40
// translate the position in a value on the scale
45
const QwtScaleDraw *sd = scale->scaleDraw();
46
switch(scale->alignment())
48
case QwtScaleDraw::LeftScale:
50
value = sd->map().invTransform(pos.y());
51
axis = QwtPlot::yLeft;
54
case QwtScaleDraw::RightScale:
56
value = sd->map().invTransform(pos.y());
57
axis = QwtPlot::yRight;
60
case QwtScaleDraw::BottomScale:
62
value = sd->map().invTransform(pos.x());
63
axis = QwtPlot::xBottom;
66
case QwtScaleDraw::TopScale:
68
value = sd->map().invTransform(pos.x());
73
emit clicked(axis, value);
77
// The rect of a scale without the title
78
QRect ScalePicker::scaleRect(const QwtScaleWidget *scale) const
80
const int bld = scale->margin();
81
const int mjt = scale->scaleDraw()->majTickLength();
82
const int sbd = scale->startBorderDist();
83
const int ebd = scale->endBorderDist();
86
switch(scale->alignment())
88
case QwtScaleDraw::LeftScale:
90
rect.setRect(scale->width() - bld - mjt, sbd,
91
mjt, scale->height() - sbd - ebd);
94
case QwtScaleDraw::RightScale:
96
rect.setRect(bld, sbd,
97
mjt, scale->height() - sbd - ebd);
100
case QwtScaleDraw::BottomScale:
102
rect.setRect(sbd, bld,
103
scale->width() - sbd - ebd, mjt);
106
case QwtScaleDraw::TopScale:
108
rect.setRect(sbd, scale->height() - bld - mjt,
109
scale->width() - sbd - ebd, mjt);