File: scalepicker.cpp

package info (click to toggle)
libqwt 4.2.0-4.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,832 kB
  • ctags: 5,512
  • sloc: cpp: 22,973; ansic: 244; makefile: 66
file content (114 lines) | stat: -rw-r--r-- 2,995 bytes parent folder | download | duplicates (5)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <qevent.h>
#include <qwt_plot.h>
#include <qwt_scale.h>
#include "scalepicker.h"

ScalePicker::ScalePicker(QwtPlot *plot):
    QObject(plot)
{
    for ( uint i = 0; i < QwtPlot::axisCnt; i++ )
    {
        QwtScale *scale = (QwtScale *)plot->axis(i);
        if ( scale )
            scale->installEventFilter(this);
    }
}

bool ScalePicker::eventFilter(QObject *object, QEvent *e)
{
    if ( object->inherits("QwtScale") && 
        e->type() == QEvent::MouseButtonPress )
    {
        mouseClicked((const QwtScale *)object,
            ((QMouseEvent *)e)->pos());
        return TRUE;
    }

    return QObject::eventFilter(object, e);
}

void ScalePicker::mouseClicked(const QwtScale *scale, const QPoint &pos) 
{
    QRect rect = scaleRect(scale);

    int margin = 10; // 10 pixels tolerance
    rect.setRect(rect.x() - margin, rect.y() - margin,
        rect.width() + 2 * margin, rect.height() +  2 * margin);

    if ( rect.contains(pos) ) // No click on the title
    {
        // translate the position in a value on the scale 

        double value = 0.0;
        int axis = -1;

        const QwtScaleDraw *sd = scale->scaleDraw();
        switch(scale->position())   
        {
            case QwtScale::Left:
            {
                value = sd->invTransform(pos.y());
                axis = QwtPlot::yLeft;
                break;
            }
            case QwtScale::Right:
            {
                value = sd->invTransform(pos.y());
                axis = QwtPlot::yRight;
                break;
            }
            case QwtScale::Bottom:
            {
                value = sd->invTransform(pos.x());
                axis = QwtPlot::xBottom;
                break;
            }
            case QwtScale::Top:
            {
                value = sd->invTransform(pos.x());
                axis = QwtPlot::xTop;
                break;
            }
        }
        emit clicked(axis, value);
    }
}

// The rect of a scale without the title
QRect ScalePicker::scaleRect(const QwtScale *scale) const
{
    const int bld = scale->baseLineDist();
    const int mjt = scale->scaleDraw()->majTickLength();
    const int sbd = scale->startBorderDist();
    const int ebd = scale->endBorderDist();

    QRect rect;
    switch(scale->position())   
    {
        case QwtScale::Left:
        {
            rect.setRect(scale->width() - bld - mjt, sbd,
                mjt, scale->height() - sbd - ebd);
            break;
        }
        case QwtScale::Right:
        {
            rect.setRect(bld, sbd,
                mjt, scale->height() - sbd - ebd);
            break;
        }
        case QwtScale::Bottom:
        {
            rect.setRect(sbd, bld, 
                scale->width() - sbd - ebd, mjt);
            break;
        }
        case QwtScale::Top:
        {
            rect.setRect(sbd, scale->height() - bld - mjt, 
                scale->width() - sbd - ebd, mjt);
            break;
        }
    }
    return rect;
}