OpenCores
URL https://opencores.org/ocsvn/modular_oscilloscope/modular_oscilloscope/trunk

Subversion Repositories modular_oscilloscope

[/] [modular_oscilloscope/] [trunk/] [sw/] [src/] [data_plot.cpp] - Blame information for rev 60

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 60 budinero
#include <stdlib.h>
2
#include <QFont>
3
#include <qwt_painter.h>
4
#include <qwt_plot_canvas.h>
5
#include <qwt_plot_marker.h>
6
#include <qwt_plot_curve.h>
7
#include <qwt_scale_widget.h>
8
#include <qwt_legend.h>
9
#include <qwt_scale_draw.h>
10
#include <qwt_plot_grid.h>
11
#include <qwt_math.h>
12
#include "data_plot.h"
13
 
14
//
15
//  Initialize main window
16
//
17
DataPlot::DataPlot(QWidget *parent):
18
    QwtPlot(parent)
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
    canvas()->setFocusIndicator(QwtPlotCanvas::ItemFocusIndicator);
28
 
29
    //canvas()->setPaintAttribute(QwtPlotCanvas::FocusIndicator, true);
30
#if QT_VERSION >= 0x040000
31
//#ifdef 1
32
    /*
33
       Qt::WA_PaintOnScreen is only supported for X11, but leads
34
       to substantial bugs with Qt 4.2.x/Windows
35
     */
36
    //canvas()->setAttribute(Qt::WA_PaintOnScreen, true);
37
//#endif
38
#endif
39
 
40
    alignScales();
41
 
42
 
43
 
44
    // Insert new curves
45
    curveA = new QwtPlotCurve("Channel A");
46
    curveA->attach(this);
47
    curveA->setData(0,0,0);
48
    curveA->setAxis(QwtPlot::xBottom, QwtPlot::yLeft);
49
 
50
    curveB = new QwtPlotCurve("Channel B");
51
    curveB->attach(this);
52
    curveB->setData(0,0,0);
53
    curveB->setAxis(QwtPlot::xBottom, QwtPlot::yRight);
54
 
55
    triggerLine = new QwtPlotMarker;
56
    triggerLine->attach(this);
57
    triggerLine->setYAxis(QwtPlot::yLeft);
58
    triggerLine->setYValue(0);
59
    triggerLine->setLineStyle(QwtPlotMarker::HLine);
60
    triggerLine->setLinePen(QPen(Qt::white, 0, Qt::SolidLine));
61
    enableTriggerLine(false, 0);
62
 
63
    // Axis 
64
    // setAxisTitle(QwtPlot::xBottom, "Time/seconds");
65
    setAxisScale(QwtPlot::xBottom, 0, 10);
66
    enableAxis(QwtPlot::xBottom, true);
67
    //setAxisTitle(QwtPlot::xBottom, "Time [s]");
68
    //setAxisMaxMinor(QwtPlot::yLeft, 20);
69
    //setAxisMaxMajor(QwtPlot::yLeft, 10);
70
    enableAxis(QwtPlot::yLeft, true);
71
    setAxisScale(QwtPlot::yLeft, -2, 2);
72
    setAxisTitle(QwtPlot::yLeft, tr("A"));
73
    axisTitle(QwtPlot::yLeft).setFont(QFont("Serif", 8));
74
 
75
    setAxisMaxMajor(QwtPlot::xBottom, 13);
76
    setAxisMaxMinor(QwtPlot::xBottom, 10);
77
 
78
    enableAxis(QwtPlot::yRight, true);
79
    setAxisScale(QwtPlot::yRight, -2, 2);
80
    setAxisTitle(QwtPlot::yRight, "B");
81
 
82
 
83
    setAxisFont(QwtPlot::xBottom, QFont("Serif", 8));
84
    setAxisFont(QwtPlot::yRight, QFont("Serif", 8));
85
    setAxisFont(QwtPlot::yLeft, QFont("Serif", 8));
86
 
87
    // grid
88
    gridA = new QwtPlotGrid();
89
    gridA->setAxis(QwtPlot::xBottom, QwtPlot::yLeft);
90
 
91
    gridA->setMajPen(QPen(Qt::white, 0, Qt::DotLine));
92
    gridA->setMinPen(QPen(Qt::white, 0 , Qt::DotLine));
93
    gridA->attach(this);
94
 
95
 
96
    gridB = new QwtPlotGrid();
97
    gridB->setAxis(QwtPlot::xBottom, QwtPlot::yRight);
98
    //gridA->enableXMin(true);
99
    gridB->enableX(false);
100
    gridB->setMajPen(QPen(Qt::gray, 0, Qt::DotLine));
101
    gridB->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
102
    gridB->attach(this);
103
 
104
    alignScales();
105
 
106
    setPaused(false);
107
 
108
}
109
 
110
//
111
//  Set a plain canvas frame and align the scales to it
112
//
113
void DataPlot::alignScales()
114
{
115
    // The code below shows how to align the scales to
116
    // the canvas frame, but is also a good example demonstrating
117
    // why the spreaded API needs polishing.
118
 
119
    canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
120
    canvas()->setLineWidth(1);
121
 
122
    for ( int i = 0; i < QwtPlot::axisCnt; i++ )
123
    {
124
        QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
125
        if ( scaleWidget )
126
            scaleWidget->setMargin(0);
127
 
128
        QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
129
        if ( scaleDraw )
130
            scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
131
    }
132
}
133
 
134
 
135
 
136
void DataPlot::setCurveAColor(const QColor &color)
137
{
138
    curveA->setPen(QPen(color));
139
    replot();
140
}
141
 
142
 
143
void DataPlot::setCurveBColor(const QColor &color)
144
{
145
    curveB->setPen(QPen(color));
146
    replot();
147
}
148
 
149
void DataPlot::setPaused(const bool &pause)
150
{
151
    paused = pause;
152
}
153
 
154
void DataPlot::curveAShow(const bool &show)
155
{
156
    if (show)
157
        curveA->attach(this);
158
    else
159
        curveA->detach();
160
}
161
 
162
void DataPlot::curveBShow(const bool &show)
163
{
164
    if (show)
165
        curveB->attach(this);
166
    else
167
        curveB->detach();
168
}
169
 
170
 
171
void DataPlot::curveAUpdate(QVector<double> time, QVector<double> chA)
172
{
173
    if (paused == false)
174
    {
175
        curveA->setData(time, chA);
176
        replot();
177
    }
178
 
179
    updateDivs();
180
}
181
 
182
void DataPlot::curveBUpdate(QVector<double> time, QVector<double> chB)
183
{
184
 
185
    if (paused == false)
186
    {
187
        curveB->setData(time, chB);
188
        replot();
189
    }
190
 
191
    updateDivs();
192
}
193
 
194
void DataPlot::updateDivs()
195
{
196
    QwtValueList tiks;
197
    // Get aDiv values
198
    tiks = gridA->yScaleDiv().ticks(QwtScaleDiv::MajorTick);
199
    aDiv = tiks.value(1, 0.0) - tiks.value(0, 0.0);
200
    aDiv = qAbs(aDiv);
201
 
202
    // Get bDiv values
203
    tiks = gridB->yScaleDiv().ticks(QwtScaleDiv::MajorTick);
204
    bDiv = tiks.value(1, 0.0) - tiks.value(0, 0.0);
205
    bDiv = qAbs(bDiv);
206
 
207
    // Get tDiv values
208
    tiks = gridA->xScaleDiv().ticks(QwtScaleDiv::MajorTick);
209
    tDiv = tiks.value(1, 0.0) - tiks.value(0, 0.0);
210
    tDiv = qAbs(tDiv);
211
 
212
    if (aDiv != 0.0)
213
        emit aScaleDivChanged(aDiv);
214
    else
215
        aDiv = 1e-5;
216
 
217
    if (bDiv != 0.0)
218
        emit aScaleDivChanged(bDiv);
219
    else
220
        bDiv = 1e-5;
221
 
222
    if (tDiv != 0.0)
223
        emit tScaleDivChanged(tDiv);
224
    else
225
        tDiv = 1/(20e6);
226
 
227
}
228
 
229
void  DataPlot::curveASetLimits(const double &down, const double &up)
230
{
231
    elementSetLimits(QwtPlot::yLeft, down, up);
232
}
233
 
234
 
235
void  DataPlot::curveAZoom(const int &value)
236
{
237
    elementZoom(QwtPlot::yLeft, value);
238
}
239
 
240
void  DataPlot::curveAMove(const int &value)
241
{
242
    elementZoom(QwtPlot::yLeft, value, true);
243
}
244
 
245
void DataPlot::curveAResetPos()
246
{
247
    double yUp, yDown;
248
    yUp = qAbs(axisScaleDiv(QwtPlot::yLeft)->upperBound());
249
    yDown = qAbs(axisScaleDiv(QwtPlot::yLeft)->lowerBound());
250
    if (yUp >= yDown)
251
        curveASetLimits(-yUp, yUp);
252
    else
253
        curveASetLimits(-yDown, yDown);
254
}
255
 
256
 
257
void  DataPlot::curveBSetLimits(const double &down, const double &up)
258
{
259
    elementSetLimits(QwtPlot::yRight, down, up);
260
}
261
 
262
 
263
void  DataPlot::curveBZoom(const int &value)
264
{
265
    elementZoom(QwtPlot::yRight, value);
266
}
267
 
268
 
269
void  DataPlot::curveBMove(const int &value)
270
{
271
    elementZoom(QwtPlot::yRight, value, true);
272
}
273
 
274
 
275
void DataPlot::curveBResetPos()
276
{
277
    double yUp, yDown;
278
    yUp = qAbs(axisScaleDiv(QwtPlot::yRight)->upperBound());
279
    yDown = qAbs(axisScaleDiv(QwtPlot::yRight)->lowerBound());
280
    if (yUp >= yDown)
281
        curveBSetLimits(-yUp, yUp);
282
    else
283
        curveBSetLimits(-yDown, yDown);
284
}
285
 
286
 
287
void DataPlot::timeSetLimits(const double &min, const double &max)
288
{
289
    elementSetLimits(QwtPlot::xBottom, min, max);
290
}
291
 
292
void DataPlot::timeZoom(const int &value)
293
{
294
    elementZoom(QwtPlot::xBottom, value);
295
}
296
 
297
void DataPlot::timeMove(const int &value)
298
{
299
    elementZoom(QwtPlot::xBottom, value, true);
300
}
301
 
302
void DataPlot::elementZoom(const int &axisId, const int &value, const bool &move)
303
{
304
    double up, down, div;
305
 
306
    switch(axisId)
307
    {
308
        case QwtPlot::yLeft:
309
            div = aDiv;
310
            break;
311
 
312
        case QwtPlot::yRight:
313
            div = bDiv;
314
            break;
315
 
316
        default:
317
            div = tDiv;
318
            break;
319
    }
320
 
321
    if (value == 0)  //auto scale
322
    {
323
        setAxisAutoScale(axisId);
324
    }
325
    else
326
    {
327
        up =  axisScaleDiv(axisId)->upperBound() - div * value/10.0 ;
328
        if (up >= 10.0)
329
            up = axisScaleDiv(axisId)->upperBound();
330
 
331
 
332
        down = move ? axisScaleDiv(axisId)->lowerBound() - div * value/10.0 :
333
               axisScaleDiv(axisId)->lowerBound() + div * value/10.0;
334
 
335
        if (down <= -10.0)
336
            down = axisScaleDiv(axisId)->lowerBound();
337
 
338
        setAxisScale(axisId, down, up);
339
    }
340
 
341
    updateDivs();
342
    replot();
343
}
344
 
345
 
346
void DataPlot::elementSetLimits(const int &axisId, const double &min, const double &max)
347
{
348
    if (min == 0 && max == 0)
349
        setAxisAutoScale(axisId);
350
    else
351
        setAxisScale(axisId,  \
352
                     min, \
353
                     max);
354
    replot();
355
    updateDivs();
356
}
357
 
358
 
359
 
360
void DataPlot::curveAShowGrid(const bool &on)
361
{
362
    elementShowGrid(QwtPlot::yLeft, on);
363
}
364
 
365
 
366
void DataPlot::curveBShowGrid(const bool &on)
367
{
368
    elementShowGrid(QwtPlot::yRight, on);
369
}
370
 
371
 
372
void DataPlot::timeShowGrid(const bool &on)
373
{
374
    elementShowGrid(QwtPlot::xBottom, on);
375
}
376
 
377
void DataPlot::elementShowGrid(const int &axisId, const bool &on)
378
{
379
    switch(axisId)
380
    {
381
        case QwtPlot::yLeft:
382
            gridA->enableY(on);
383
            break;
384
 
385
        case QwtPlot::yRight:
386
            gridB->enableY(on);
387
            break;
388
 
389
        default:
390
            gridA->enableX(on);
391
            break;
392
    }
393
    replot();
394
}
395
 
396
 
397
 
398
void DataPlot::enableTriggerLine(const bool &on, const  int &channel)
399
{
400
    int axisId;
401
    switch(channel)
402
    {
403
        case 1:
404
            axisId = QwtPlot::yRight;
405
            break;
406
 
407
        default:
408
            axisId = QwtPlot::yLeft;
409
            break;
410
    }
411
 
412
    triggerLine->setVisible(on);
413
    if(on)
414
    {
415
        triggerLine->show();
416
        triggerLine->setAxis(QwtPlot::xBottom, axisId);
417
        replot();
418
    }
419
    else
420
    {
421
        triggerLine->hide();
422
        replot();
423
    }
424
}
425
 
426
void DataPlot::setTriggerLineValue(double val)
427
{
428
    triggerLine->setYValue(val);
429
    replot();
430
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.