~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.0/examples/event_filter/scalepicker.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-05-26 10:26:31 UTC
  • mfrom: (1.1.3 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526102631-bp95mfccnrb957nx
Tags: 5.1.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <qevent.h>
2
 
#include <qwt_plot.h>
3
 
#include <qwt_scale_widget.h>
4
 
#include "scalepicker.h"
5
 
 
6
 
ScalePicker::ScalePicker(QwtPlot *plot):
7
 
    QObject(plot)
8
 
{
9
 
    for ( uint i = 0; i < QwtPlot::axisCnt; i++ )
10
 
    {
11
 
        QwtScaleWidget *scaleWidget = (QwtScaleWidget *)plot->axisWidget(i);
12
 
        if ( scaleWidget )
13
 
            scaleWidget->installEventFilter(this);
14
 
    }
15
 
}
16
 
 
17
 
bool ScalePicker::eventFilter(QObject *object, QEvent *e)
18
 
{
19
 
    if ( object->inherits("QwtScaleWidget") && 
20
 
        e->type() == QEvent::MouseButtonPress )
21
 
    {
22
 
        mouseClicked((const QwtScaleWidget *)object,
23
 
            ((QMouseEvent *)e)->pos());
24
 
        return true;
25
 
    }
26
 
 
27
 
    return QObject::eventFilter(object, e);
28
 
}
29
 
 
30
 
void ScalePicker::mouseClicked(const QwtScaleWidget *scale, const QPoint &pos) 
31
 
{
32
 
    QRect rect = scaleRect(scale);
33
 
 
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);
37
 
 
38
 
    if ( rect.contains(pos) ) // No click on the title
39
 
    {
40
 
        // translate the position in a value on the scale 
41
 
 
42
 
        double value = 0.0;
43
 
        int axis = -1;
44
 
 
45
 
        const QwtScaleDraw *sd = scale->scaleDraw();
46
 
        switch(scale->alignment())   
47
 
        {
48
 
            case QwtScaleDraw::LeftScale:
49
 
            {
50
 
                value = sd->map().invTransform(pos.y());
51
 
                axis = QwtPlot::yLeft;
52
 
                break;
53
 
            }
54
 
            case QwtScaleDraw::RightScale:
55
 
            {
56
 
                value = sd->map().invTransform(pos.y());
57
 
                axis = QwtPlot::yRight;
58
 
                break;
59
 
            }
60
 
            case QwtScaleDraw::BottomScale:
61
 
            {
62
 
                value = sd->map().invTransform(pos.x());
63
 
                axis = QwtPlot::xBottom;
64
 
                break;
65
 
            }
66
 
            case QwtScaleDraw::TopScale:
67
 
            {
68
 
                value = sd->map().invTransform(pos.x());
69
 
                axis = QwtPlot::xTop;
70
 
                break;
71
 
            }
72
 
        }
73
 
        emit clicked(axis, value);
74
 
    }
75
 
}
76
 
 
77
 
// The rect of a scale without the title
78
 
QRect ScalePicker::scaleRect(const QwtScaleWidget *scale) const
79
 
{
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();
84
 
 
85
 
    QRect rect;
86
 
    switch(scale->alignment())   
87
 
    {
88
 
        case QwtScaleDraw::LeftScale:
89
 
        {
90
 
            rect.setRect(scale->width() - bld - mjt, sbd,
91
 
                mjt, scale->height() - sbd - ebd);
92
 
            break;
93
 
        }
94
 
        case QwtScaleDraw::RightScale:
95
 
        {
96
 
            rect.setRect(bld, sbd,
97
 
                mjt, scale->height() - sbd - ebd);
98
 
            break;
99
 
        }
100
 
        case QwtScaleDraw::BottomScale:
101
 
        {
102
 
            rect.setRect(sbd, bld, 
103
 
                scale->width() - sbd - ebd, mjt);
104
 
            break;
105
 
        }
106
 
        case QwtScaleDraw::TopScale:
107
 
        {
108
 
            rect.setRect(sbd, scale->height() - bld - mjt, 
109
 
                scale->width() - sbd - ebd, mjt);
110
 
            break;
111
 
        }
112
 
    }
113
 
    return rect;
114
 
}