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

« back to all changes in this revision

Viewing changes to qwt-5.0.2/examples/data_plot/data_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 <stdlib.h>
 
2
#include <qwt_painter.h>
 
3
#include <qwt_plot_canvas.h>
 
4
#include <qwt_plot_marker.h>
 
5
#include <qwt_plot_curve.h>
 
6
#include <qwt_scale_widget.h>
 
7
#include <qwt_legend.h>
 
8
#include <qwt_scale_draw.h>
 
9
#include <qwt_math.h>
 
10
#include "data_plot.h"
 
11
 
 
12
//
 
13
//  Initialize main window
 
14
//
 
15
DataPlot::DataPlot(QWidget *parent):
 
16
    QwtPlot(parent),
 
17
    d_interval(0),
 
18
    d_timerId(-1)
 
19
{
 
20
    // Disable polygon clipping
 
21
    QwtPainter::setDeviceClipping(false);
 
22
 
 
23
    // We don't need the cache here
 
24
    canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
 
25
    canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
 
26
 
 
27
#if QT_VERSION >= 0x040000
 
28
#ifdef Q_WS_X11
 
29
    /*
 
30
       Qt::WA_PaintOnScreen is only supported for X11, but leads
 
31
       to substantial bugs with Qt 4.2.x/Windows
 
32
     */
 
33
    canvas()->setAttribute(Qt::WA_PaintOnScreen, true);
 
34
#endif
 
35
#endif
 
36
 
 
37
    alignScales();
 
38
    
 
39
    //  Initialize data
 
40
    for (int i = 0; i< PLOT_SIZE; i++)
 
41
    {
 
42
        d_x[i] = 0.5 * i;     // time axis
 
43
        d_y[i] = 0;
 
44
        d_z[i] = 0;
 
45
    }
 
46
 
 
47
    // Assign a title
 
48
    setTitle("A Test for High Refresh Rates");
 
49
    insertLegend(new QwtLegend(), QwtPlot::BottomLegend);
 
50
 
 
51
    // Insert new curves
 
52
    QwtPlotCurve *cRight = new QwtPlotCurve("Data Moving Right");
 
53
    cRight->attach(this);
 
54
 
 
55
    QwtPlotCurve *cLeft = new QwtPlotCurve("Data Moving Left");
 
56
    cLeft->attach(this);
 
57
 
 
58
    // Set curve styles
 
59
    cRight->setPen(QPen(Qt::red));
 
60
    cLeft->setPen(QPen(Qt::blue));
 
61
 
 
62
    // Attach (don't copy) data. Both curves use the same x array.
 
63
    cRight->setRawData(d_x, d_y, PLOT_SIZE);
 
64
    cLeft->setRawData(d_x, d_z, PLOT_SIZE);
 
65
 
 
66
#if 0
 
67
    //  Insert zero line at y = 0
 
68
    QwtPlotMarker *mY = new QwtPlotMarker();
 
69
    mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
 
70
    mY->setLineStyle(QwtPlotMarker::HLine);
 
71
    mY->setYValue(0.0);
 
72
    mY->attach(this);
 
73
#endif
 
74
 
 
75
    // Axis 
 
76
    setAxisTitle(QwtPlot::xBottom, "Time/seconds");
 
77
    setAxisScale(QwtPlot::xBottom, 0, 100);
 
78
 
 
79
    setAxisTitle(QwtPlot::yLeft, "Values");
 
80
    setAxisScale(QwtPlot::yLeft, -1.5, 1.5);
 
81
    
 
82
    setTimerInterval(0.0); 
 
83
}
 
84
 
 
85
//
 
86
//  Set a plain canvas frame and align the scales to it
 
87
//
 
88
void DataPlot::alignScales()
 
89
{
 
90
    // The code below shows how to align the scales to
 
91
    // the canvas frame, but is also a good example demonstrating
 
92
    // why the spreaded API needs polishing.
 
93
 
 
94
    canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
 
95
    canvas()->setLineWidth(1);
 
96
 
 
97
    for ( int i = 0; i < QwtPlot::axisCnt; i++ )
 
98
    {
 
99
        QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
 
100
        if ( scaleWidget )
 
101
            scaleWidget->setMargin(0);
 
102
 
 
103
        QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
 
104
        if ( scaleDraw )
 
105
            scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
 
106
    }
 
107
}
 
108
 
 
109
void DataPlot::setTimerInterval(double ms)
 
110
{
 
111
    d_interval = qRound(ms);
 
112
 
 
113
    if ( d_timerId >= 0 )
 
114
    {
 
115
        killTimer(d_timerId);
 
116
        d_timerId = -1;
 
117
    }
 
118
    if (d_interval >= 0 )
 
119
        d_timerId = startTimer(d_interval);
 
120
}
 
121
 
 
122
//  Generate new values 
 
123
void DataPlot::timerEvent(QTimerEvent *)
 
124
{
 
125
    static double phase = 0.0;
 
126
 
 
127
    if (phase > (M_PI - 0.0001)) 
 
128
        phase = 0.0;
 
129
 
 
130
    // y moves from left to right:
 
131
    // Shift y array right and assign new value to y[0].
 
132
 
 
133
    for ( int i = PLOT_SIZE - 1; i > 0; i-- )
 
134
        d_y[i] = d_y[i-1];
 
135
    d_y[0] = sin(phase) * (-1.0 + 2.0 * double(rand()) / double(RAND_MAX));
 
136
 
 
137
    for ( int j = 0; j < PLOT_SIZE - 1; j++ )
 
138
        d_z[j] = d_z[j+1];
 
139
 
 
140
    d_z[PLOT_SIZE - 1] = 0.8 - (2.0 * phase/M_PI) + 0.4 * 
 
141
        double(rand()) / double(RAND_MAX);
 
142
 
 
143
    // update the display
 
144
    replot();
 
145
 
 
146
    phase += M_PI * 0.02;
 
147
}