~l3on/ubuntu/oneiric/qwt/fix-921430

« back to all changes in this revision

Viewing changes to qwt-5.0.2/examples/spectrogram/plot.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2007-10-05 15:20:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071005152041-qmybqh4fj9jejyo2
Tags: 5.0.2-2
* Handle nostrip build option. (Closes: #437877)
* Build libqwt5-doc package in binary-indep target. (Closes: #443110)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <qwt_color_map.h>
 
2
#include <qwt_plot_spectrogram.h>
 
3
#include <qwt_scale_widget.h>
 
4
#include <qwt_scale_draw.h>
 
5
#include <qwt_plot_zoomer.h>
 
6
#include <qwt_plot_panner.h>
 
7
#include <qwt_plot_layout.h>
 
8
#include "plot.h"
 
9
 
 
10
class SpectrogramData: public QwtRasterData
 
11
{
 
12
public:
 
13
    SpectrogramData():
 
14
        QwtRasterData(QwtDoubleRect(-1.5, -1.5, 3.0, 3.0))
 
15
    {
 
16
    }
 
17
 
 
18
    virtual QwtRasterData *copy() const
 
19
    {
 
20
        return new SpectrogramData();
 
21
    }
 
22
 
 
23
    virtual QwtDoubleInterval range() const
 
24
    {
 
25
        return QwtDoubleInterval(0.0, 10.0);
 
26
    }
 
27
 
 
28
    virtual double value(double x, double y) const
 
29
    {
 
30
        const double c = 0.842;
 
31
 
 
32
        const double v1 = x * x + (y-c) * (y+c);
 
33
        const double v2 = x * (y+c) + x * (y+c);
 
34
 
 
35
        return 1.0 / (v1 * v1 + v2 * v2);
 
36
    }
 
37
};
 
38
 
 
39
class MyZoomer: public QwtPlotZoomer
 
40
{
 
41
public:
 
42
    MyZoomer(QwtPlotCanvas* canvas):
 
43
        QwtPlotZoomer(canvas)
 
44
    {
 
45
        setTrackerMode(QwtPicker::AlwaysOn);
 
46
    }
 
47
 
 
48
protected:
 
49
    virtual QwtText trackerText( const QwtDoublePoint& p ) const 
 
50
    {
 
51
        QwtText t( QwtPlotPicker::trackerText( p ));
 
52
 
 
53
#if QT_VERSION >= 0x040300
 
54
        QColor c(Qt::white);
 
55
        c.setAlpha(180);
 
56
        t.setBackgroundBrush( QBrush(c) );
 
57
#endif
 
58
        return t;
 
59
    }
 
60
};
 
61
 
 
62
 
 
63
Plot::Plot(QWidget *parent):
 
64
    QwtPlot(parent)
 
65
{
 
66
    d_spectrogram = new QwtPlotSpectrogram();
 
67
 
 
68
    QwtLinearColorMap colorMap(Qt::darkCyan, Qt::red);
 
69
    colorMap.addColorStop(0.1, Qt::cyan);
 
70
    colorMap.addColorStop(0.6, Qt::green);
 
71
    colorMap.addColorStop(0.95, Qt::yellow);
 
72
 
 
73
    d_spectrogram->setColorMap(colorMap);
 
74
 
 
75
    d_spectrogram->setData(SpectrogramData());
 
76
    d_spectrogram->attach(this);
 
77
 
 
78
    QwtValueList contourLevels;
 
79
    for ( double level = 0.5; level < 10.0; level += 1.0 )
 
80
        contourLevels += level;
 
81
    d_spectrogram->setContourLevels(contourLevels);
 
82
 
 
83
    QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
 
84
    rightAxis->setTitle("Intensity");
 
85
    rightAxis->setColorBarEnabled(true);
 
86
    rightAxis->setColorMap(d_spectrogram->data().range(),
 
87
        d_spectrogram->colorMap());
 
88
 
 
89
    setAxisScale(QwtPlot::yRight, 
 
90
        d_spectrogram->data().range().minValue(),
 
91
        d_spectrogram->data().range().maxValue() );
 
92
    enableAxis(QwtPlot::yRight);
 
93
 
 
94
    plotLayout()->setAlignCanvasToScales(true);
 
95
    replot();
 
96
 
 
97
    // LeftButton for the zooming
 
98
    // MidButton for the panning
 
99
    // RightButton: zoom out by 1
 
100
    // Ctrl+RighButton: zoom out to full size
 
101
 
 
102
    QwtPlotZoomer* zoomer = new MyZoomer(canvas());
 
103
#if QT_VERSION < 0x040000
 
104
    zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
 
105
        Qt::RightButton, Qt::ControlButton);
 
106
#else
 
107
    zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
 
108
        Qt::RightButton, Qt::ControlModifier);
 
109
#endif
 
110
    zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
 
111
        Qt::RightButton);
 
112
 
 
113
    QwtPlotPanner *panner = new QwtPlotPanner(canvas());
 
114
    panner->setAxisEnabled(QwtPlot::yRight, false);
 
115
    panner->setMouseButton(Qt::MidButton);
 
116
 
 
117
    // Avoid jumping when labels with more/less digits
 
118
    // appear/disappear when scrolling vertically
 
119
 
 
120
    const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
 
121
    QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
 
122
    sd->setMinimumExtent( fm.width("100.00") );
 
123
 
 
124
    const QColor c(Qt::darkBlue);
 
125
    zoomer->setRubberBandPen(c);
 
126
    zoomer->setTrackerPen(c);
 
127
}
 
128
 
 
129
void Plot::showContour(bool on)
 
130
{
 
131
    d_spectrogram->setDisplayMode(QwtPlotSpectrogram::ContourMode, on);
 
132
    replot();
 
133
}
 
134
 
 
135
void Plot::showSpectrogram(bool on)
 
136
{
 
137
    d_spectrogram->setDisplayMode(QwtPlotSpectrogram::ImageMode, on);
 
138
    d_spectrogram->setDefaultContourPen(on ? QPen() : QPen(Qt::NoPen));
 
139
    replot();
 
140
}