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

« back to all changes in this revision

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