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

« back to all changes in this revision

Viewing changes to qwt-5.1.0/examples/curvdemo2/curvdemo2.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 <qapplication.h>
2
 
#include <qpainter.h>
3
 
#include <qwt_math.h>
4
 
#include <qwt_symbol.h>
5
 
#include <qwt_curve_fitter.h>
6
 
#include "curvdemo2.h"
7
 
 
8
 
 
9
 
//------------------------------------------------------------
10
 
//      curvdemo2
11
 
//
12
 
//  This example shows a simple animation featuring
13
 
//  with several QwtPlotCurves
14
 
//
15
 
//------------------------------------------------------------
16
 
 
17
 
//
18
 
//   Array Sizes
19
 
//
20
 
const int Size = 15;
21
 
const int USize = 13;
22
 
 
23
 
//
24
 
//   Arrays holding the values
25
 
//
26
 
double xval[Size];
27
 
double yval[Size];
28
 
double zval[Size];
29
 
double uval[USize];
30
 
double vval[USize];
31
 
 
32
 
 
33
 
//
34
 
//  CONSTRUCT MAIN WINDOW
35
 
//
36
 
MainWin::MainWin(): 
37
 
    QFrame()
38
 
{
39
 
    setFrameStyle(QFrame::Box|QFrame::Raised);
40
 
    setLineWidth(2);
41
 
    setMidLineWidth(3);
42
 
    
43
 
    const QColor bgColor(30,30,50);
44
 
#if QT_VERSION < 0x040000
45
 
    setPaletteBackgroundColor(bgColor);
46
 
#else
47
 
    QPalette p = palette();
48
 
    p.setColor(backgroundRole(), bgColor);
49
 
    setPalette(p);
50
 
#endif
51
 
 
52
 
    QwtSplineCurveFitter* curveFitter; 
53
 
 
54
 
    //
55
 
    //  curve 1
56
 
    // 
57
 
    int i = 0;
58
 
    xMap[i].setScaleInterval(-1.5, 1.5);
59
 
    yMap[i].setScaleInterval(0.0, 6.28);
60
 
 
61
 
    curve[i].setPen(QPen(QColor(150,150,200),2));
62
 
    curve[i].setCurveType(QwtPlotCurve::Xfy);
63
 
    curve[i].setStyle(QwtPlotCurve::Lines);
64
 
    curve[i].setCurveAttribute(QwtPlotCurve::Fitted, true);
65
 
    curveFitter = new QwtSplineCurveFitter();
66
 
    curveFitter->setSplineSize(150);
67
 
    curve[i].setCurveFitter(curveFitter);
68
 
 
69
 
    QwtSymbol sym;
70
 
    sym.setStyle(QwtSymbol::XCross);
71
 
    sym.setPen(QPen(Qt::yellow,2));
72
 
    sym.setSize(7);
73
 
    
74
 
    curve[i].setSymbol(sym);
75
 
 
76
 
    curve[i].setRawData(yval,xval,Size);
77
 
    
78
 
    //
79
 
    // curve 2
80
 
    //
81
 
    i++;
82
 
    xMap[i].setScaleInterval(0.0, 6.28);
83
 
    yMap[i].setScaleInterval(-3.0, 1.1);
84
 
    curve[i].setPen(QPen(QColor(200,150,50), 1, Qt::DashDotDotLine));
85
 
    curve[i].setStyle(QwtPlotCurve::Sticks);
86
 
    curve[i].setSymbol(QwtSymbol(QwtSymbol::Ellipse,
87
 
        QColor(Qt::blue), QColor(Qt::yellow), QSize(5,5)));
88
 
 
89
 
    curve[i].setRawData(xval,zval,Size);
90
 
 
91
 
    
92
 
    //
93
 
    //  curve 3
94
 
    // 
95
 
    i++;
96
 
    xMap[i].setScaleInterval(-1.1, 3.0);
97
 
    yMap[i].setScaleInterval(-1.1, 3.0);
98
 
    curve[i].setStyle(QwtPlotCurve::Lines);
99
 
    curve[i].setCurveAttribute(QwtPlotCurve::Fitted, true);
100
 
    curve[i].setPen(QColor(100,200,150));
101
 
    curveFitter = new QwtSplineCurveFitter();
102
 
    curveFitter->setFitMode(QwtSplineCurveFitter::ParametricSpline);
103
 
    curveFitter->setSplineSize(200);
104
 
    curve[i].setCurveFitter(curveFitter);
105
 
 
106
 
    curve[i].setRawData(yval,zval,Size);
107
 
 
108
 
 
109
 
    //
110
 
    //  curve 4
111
 
    //
112
 
    i++;
113
 
    xMap[i].setScaleInterval(-5, 1.1);
114
 
    yMap[i].setScaleInterval(-1.1, 5.0);
115
 
    curve[i].setStyle(QwtPlotCurve::Lines);
116
 
    curve[i].setCurveAttribute(QwtPlotCurve::Fitted, true);
117
 
    curve[i].setPen(QColor(Qt::red));
118
 
    curveFitter = new QwtSplineCurveFitter();
119
 
    curveFitter->setSplineSize(200);
120
 
    curve[i].setCurveFitter(curveFitter);
121
 
 
122
 
    curve[i].setRawData(uval,vval,USize);
123
 
 
124
 
    //
125
 
    //  initialize values
126
 
    //
127
 
    double base = 2.0 * M_PI / double(USize - 1);
128
 
    double toggle = 1.0; 
129
 
    for (i = 0; i < USize; i++)
130
 
    {
131
 
        uval[i] =  toggle * cos( double(i) * base);
132
 
        vval[i] =  toggle * sin( double(i) * base);
133
 
            
134
 
        if (toggle == 1.0)
135
 
           toggle = 0.5;
136
 
        else
137
 
           toggle = 1.0;
138
 
    }
139
 
 
140
 
    newValues();
141
 
 
142
 
    //
143
 
    // start timer
144
 
    //
145
 
    (void)startTimer(250);  
146
 
}
147
 
 
148
 
#if QT_VERSION >= 0x040000
149
 
void MainWin::paintEvent(QPaintEvent *event)
150
 
{
151
 
    QFrame::paintEvent(event);
152
 
 
153
 
    QPainter painter(this);
154
 
    painter.setClipRect(contentsRect());
155
 
    drawContents(&painter);
156
 
}
157
 
#endif
158
 
 
159
 
void MainWin::drawContents(QPainter *painter)
160
 
{
161
 
    const QRect &r = contentsRect();
162
 
 
163
 
    for ( int i = 0; i < curveCount; i++ )
164
 
    {
165
 
        xMap[i].setPaintInterval(r.left(), r.right());
166
 
        yMap[i].setPaintInterval(r.top(), r.bottom());
167
 
        curve[i].draw(painter, xMap[i], yMap[i], r);
168
 
    }
169
 
}
170
 
 
171
 
//
172
 
//  TIMER EVENT
173
 
//
174
 
void MainWin::timerEvent(QTimerEvent *)
175
 
{
176
 
    newValues();
177
 
    repaint();
178
 
}
179
 
 
180
 
//
181
 
//  RE-CALCULATE VALUES
182
 
//
183
 
void MainWin::newValues()
184
 
{
185
 
    int i;
186
 
    static double phs = 0.0;
187
 
    double s,c,u;
188
 
    
189
 
    for (i=0;i<Size;i++)
190
 
    {
191
 
        xval[i] = 6.28 * double(i) / double(Size -1);
192
 
        yval[i] = sin(xval[i] - phs);
193
 
        zval[i] = cos(3.0 * (xval[i] + phs));
194
 
    }
195
 
    
196
 
    s = 0.25 * sin(phs);
197
 
    c = sqrt(1.0 - s*s);
198
 
    for (i=0; i<USize;i++)
199
 
    {
200
 
        u = uval[i];
201
 
        uval[i] = uval[i] * c - vval[i] * s;
202
 
        vval[i] = vval[i] * c + u * s;
203
 
    }
204
 
    
205
 
    phs += 0.0628;
206
 
    if (phs > 6.28)
207
 
       phs = 0.0;
208
 
    
209
 
}
210
 
 
211
 
int main (int argc, char **argv)
212
 
{
213
 
    QApplication a(argc, argv);
214
 
 
215
 
    MainWin w;
216
 
 
217
 
#if QT_VERSION < 0x040000
218
 
    a.setMainWidget(&w);
219
 
#endif
220
 
    w.resize(300,300);
221
 
    w.show();
222
 
 
223
 
    return a.exec();
224
 
}