File: simple.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 (111 lines) | stat: -rw-r--r-- 2,443 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
#ifdef QWT_QTOPIA
#include <qpe/qpeapplication.h>
#endif
#include <qapplication.h>
#include <qwt_plot.h>
#include <qwt_data.h>
#include <math.h>

//-----------------------------------------------------------------
//              simple.cpp
//
//      A simple example which shows how to use QwtPlot and QwtData
//-----------------------------------------------------------------

class SimpleData: public QwtData
{
    // The x values depend on its index and the y values
    // can be calculated from the corresponding x value. 
    // So we dont need to store the values.
    // Such an implementation is slower because every point 
    // has to be recalculated for every replot, but it demonstrates how
    // QwtData can be used.

public:
    SimpleData(double(*y)(double), size_t size):
        d_size(size),
        d_y(y)
    {
    }

    virtual QwtData *copy() const
    {
        return new SimpleData(d_y, d_size);
    }

    virtual size_t size() const
    {
        return d_size;
    }

    virtual double x(size_t i) const
    {
        return 0.1 * i;
    }

    virtual double y(size_t i) const
    {
        return d_y(x(i));
    }
private:
    size_t d_size;
    double(*d_y)(double);
};

class Plot : public QwtPlot
{
public:
    Plot();
};


Plot::Plot()
{
    setTitle("A Simple QwtPlot Demonstration");
    setAutoLegend(TRUE); // We want a legend
    setLegendPosition(QwtPlot::Right);

    // Set axis titles
    setAxisTitle(xBottom, "x -->");
    setAxisTitle(yLeft, "y -->");
    
    // Insert new curves
    long cSin = insertCurve("y = sin(x)");
    long cCos = insertCurve("y = cos(x)");

    // Set curve styles
    setCurvePen(cSin, QPen(Qt::red));
    setCurvePen(cCos, QPen(Qt::blue));

    // Create sin and cos data
    const int nPoints = 100;
    setCurveData(cSin, SimpleData(::sin, nPoints));
    setCurveData(cCos, SimpleData(::cos, nPoints));

    // Insert markers
    
    //  ...a horizontal line at y = 0...
    long mY = insertLineMarker("y = 0", QwtPlot::yLeft);     
    setMarkerYPos(mY, 0.0);

    //  ...a vertical line at x = 2 * pi
    long mX = insertLineMarker("x = 2 pi", QwtPlot::xBottom);
    setMarkerXPos(mX, 6.284);

    replot();
}

int main(int argc, char **argv)
{
#ifdef QWT_QTOPIA
    QPEApplication a(argc, argv);
#else
    QApplication a(argc, argv);
#endif

    Plot plot;
    a.setMainWidget(&plot);
    plot.resize(500,300);
    plot.show();
    return a.exec(); 
}