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

« back to all changes in this revision

Viewing changes to qwt-5.0.2/examples/curvdemo1/curvdemo1.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 <qapplication.h>
 
2
#include <qframe.h>
 
3
#include <qwt_scale_map.h>
 
4
#include <qwt_plot_curve.h>
 
5
#include <qwt_symbol.h>
 
6
#include <qcolor.h>
 
7
#include <qpainter.h>
 
8
#if QT_VERSION >= 0x040000
 
9
#include <qpaintengine.h>
 
10
#endif
 
11
#include <math.h>
 
12
 
 
13
//------------------------------------------------------------
 
14
//      curvdemo1
 
15
//
 
16
//  This example program features some of the different
 
17
//  display styles of the QwtPlotCurve class
 
18
//------------------------------------------------------------
 
19
 
 
20
 
 
21
//
 
22
//   Array Sizes
 
23
//
 
24
const int Size = 27;
 
25
#if QT_VERSION >= 0x040000
 
26
const int CurvCnt = 6;
 
27
#else
 
28
const int CurvCnt = 5;
 
29
#endif
 
30
 
 
31
//
 
32
//   Arrays holding the values
 
33
//
 
34
double xval[Size];
 
35
double yval[Size];
 
36
QwtScaleMap xMap; 
 
37
QwtScaleMap yMap;
 
38
 
 
39
class MainWin : public QFrame 
 
40
{
 
41
public:
 
42
    MainWin();
 
43
    
 
44
protected:
 
45
#if QT_VERSION >= 0x040000
 
46
    virtual void paintEvent(QPaintEvent *);
 
47
#endif
 
48
    void drawContents(QPainter *p);
 
49
 
 
50
private:
 
51
    void shiftDown(QRect &rect, int offset) const;
 
52
 
 
53
    QwtPlotCurve crv[CurvCnt];
 
54
};
 
55
 
 
56
MainWin::MainWin() 
 
57
{
 
58
    int i;
 
59
 
 
60
    xMap.setScaleInterval(-0.5, 10.5);
 
61
    yMap.setScaleInterval(-1.1, 1.1);
 
62
 
 
63
    //
 
64
    //  Frame style
 
65
    //  
 
66
    setFrameStyle(QFrame::Box|QFrame::Raised);
 
67
    setLineWidth(2);
 
68
    setMidLineWidth(3);
 
69
 
 
70
    //
 
71
    // Calculate values
 
72
    //
 
73
    for(i=0; i<Size;i++)
 
74
    {   xval[i] = double(i) * 10.0 / double(Size - 1);
 
75
        yval[i] = sin(xval[i]) * cos(2.0 * xval[i]);
 
76
    }
 
77
    
 
78
    //
 
79
    //  define curve styles
 
80
    // 
 
81
    QwtSymbol sym;
 
82
    i = 0;
 
83
 
 
84
    sym.setStyle(QwtSymbol::Cross);
 
85
    sym.setPen(QColor(Qt::black));
 
86
    sym.setSize(5);
 
87
    crv[i].setSymbol(sym);
 
88
    crv[i].setPen(QColor(Qt::darkGreen));
 
89
    crv[i].setStyle(QwtPlotCurve::Lines);
 
90
    crv[i].setCurveAttribute(QwtPlotCurve::Fitted);
 
91
    i++;
 
92
 
 
93
    sym.setStyle(QwtSymbol::Ellipse);
 
94
    sym.setPen(QColor(Qt::blue));
 
95
    sym.setBrush(QColor(Qt::yellow));
 
96
    sym.setSize(5);
 
97
    crv[i].setSymbol(sym);
 
98
    crv[i].setPen(QColor(Qt::red));
 
99
    crv[i].setStyle(QwtPlotCurve::Sticks);
 
100
    i++;
 
101
 
 
102
    crv[i].setPen(QColor(Qt::darkBlue));
 
103
    crv[i].setStyle(QwtPlotCurve::Lines);
 
104
    i++;
 
105
 
 
106
#if QT_VERSION >= 0x040000
 
107
    crv[i].setPen(QColor(Qt::darkBlue));
 
108
    crv[i].setStyle(QwtPlotCurve::Lines);
 
109
    crv[i].setRenderHint(QwtPlotItem::RenderAntialiased);
 
110
    i++;
 
111
#endif
 
112
 
 
113
 
 
114
    crv[i].setPen(QColor(Qt::darkCyan));
 
115
    crv[i].setStyle(QwtPlotCurve::Steps);
 
116
    i++;
 
117
 
 
118
    sym.setStyle(QwtSymbol::XCross);
 
119
    sym.setPen(QColor(Qt::darkMagenta));
 
120
    crv[i].setSymbol(sym);
 
121
    crv[i].setStyle(QwtPlotCurve::NoCurve);
 
122
    i++;
 
123
 
 
124
 
 
125
    //
 
126
    // attach data
 
127
    //
 
128
    for(i=0;i<CurvCnt;i++)
 
129
        crv[i].setRawData(xval,yval,Size);
 
130
}
 
131
 
 
132
void MainWin::shiftDown(QRect &rect, int offset) const
 
133
{
 
134
#if QT_VERSION < 0x040000
 
135
        rect.moveBy(0, offset);     
 
136
#else
 
137
        rect.translate(0, offset);     
 
138
#endif
 
139
}
 
140
 
 
141
#if QT_VERSION >= 0x040000
 
142
void MainWin::paintEvent(QPaintEvent *event)
 
143
{
 
144
    QFrame::paintEvent(event);
 
145
 
 
146
    QPainter painter(this);
 
147
    painter.setClipRect(contentsRect());
 
148
    drawContents(&painter);
 
149
}
 
150
#endif
 
151
 
 
152
 
 
153
//
 
154
//  REDRAW CONTENTS
 
155
//
 
156
void MainWin::drawContents(QPainter *painter)
 
157
{
 
158
    int deltay,i;
 
159
 
 
160
    QRect r = contentsRect();
 
161
 
 
162
    deltay = r.height() / CurvCnt - 1;
 
163
 
 
164
    r.setHeight(deltay);
 
165
 
 
166
    //
 
167
    //  draw curves
 
168
    //
 
169
    for (i=0;i<CurvCnt;i++)
 
170
    {
 
171
        xMap.setPaintInterval(r.left(), r.right());
 
172
        yMap.setPaintInterval(r.top(), r.bottom());
 
173
 
 
174
#if QT_VERSION >= 0x040000
 
175
        const QPaintEngine *pe = painter->device()->paintEngine();
 
176
        if (pe->hasFeature(QPaintEngine::Antialiasing) )
 
177
        {
 
178
            painter->setRenderHint(QPainter::Antialiasing,
 
179
                crv[i].testRenderHint(QwtPlotItem::RenderAntialiased) );
 
180
        }
 
181
#endif
 
182
        crv[i].draw(painter, xMap, yMap, r);
 
183
 
 
184
        shiftDown(r, deltay);
 
185
    }
 
186
 
 
187
    //
 
188
    // draw titles
 
189
    //
 
190
    r = contentsRect();     // reset r
 
191
    painter->setFont(QFont("Helvetica", 8));
 
192
    
 
193
    const int alignment = Qt::AlignTop|Qt::AlignHCenter;
 
194
 
 
195
    painter->setPen(Qt::black);
 
196
 
 
197
    painter->drawText(0,r.top(),r.width(), painter->fontMetrics().height(),
 
198
        alignment, "Style: Line/Fitted, Symbol: Cross");
 
199
    shiftDown(r, deltay);
 
200
 
 
201
    painter->drawText(0,r.top(),r.width(), painter->fontMetrics().height(),
 
202
        alignment, "Style: Sticks, Symbol: Ellipse");
 
203
    shiftDown(r, deltay);
 
204
    
 
205
    painter->drawText(0 ,r.top(),r.width(), painter->fontMetrics().height(),
 
206
        alignment, "Style: Lines, Symbol: None");
 
207
    shiftDown(r, deltay);
 
208
 
 
209
#if QT_VERSION >= 0x040000
 
210
    painter->drawText(0 ,r.top(),r.width(), painter->fontMetrics().height(),
 
211
        alignment, "Style: Lines, Symbol: None, Antialiased");
 
212
    shiftDown(r, deltay);
 
213
#endif
 
214
    
 
215
    
 
216
    painter->drawText(0, r.top(),r.width(), painter->fontMetrics().height(),
 
217
        alignment, "Style: Steps, Symbol: None");
 
218
    shiftDown(r, deltay);
 
219
    
 
220
    painter->drawText(0,r.top(),r.width(), painter->fontMetrics().height(),
 
221
        alignment, "Style: NoCurve, Symbol: XCross");
 
222
}
 
223
 
 
224
int main (int argc, char **argv)
 
225
{
 
226
    QApplication a(argc, argv);
 
227
 
 
228
    MainWin w;
 
229
 
 
230
#if QT_VERSION < 0x040000
 
231
    a.setMainWidget(&w);
 
232
#endif
 
233
    w.resize(300,600);
 
234
    w.show();
 
235
 
 
236
    return a.exec();
 
237
}