-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.cpp
269 lines (240 loc) · 8.9 KB
/
plot.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/***************************************************************************
* Copyright (C) 2011-2012 Fabian Lesniak <[email protected]> *
* Max Bruckner <[email protected]> *
* *
* This file is part of the QLenLab project. *
* *
* QLenLab is free software: you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* QLenLab is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with QLenLab. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#include <QPalette>
#include <QTimerEvent>
#include <QPen>
#include <QColor>
#include <qwt_plot_grid.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include "fftthread.h"
#include "plot.h"
#include "signaldata.h"
#include "storage.h"
plot::plot(meta::plotmode mode, storage *datastorage, QWidget *parent) : QwtPlot(parent), p_mode(mode), interval(0.0, 20.0), p_storage(datastorage), p_dataset(0), p_autoscale(false), p_autoscaleGrid(0.0), p_currentIndex(-2)
{
plotLayout()->setAlignCanvasToScales(true);
zoomer = new plotzoomer(canvas());
zoomer->setTrackerMode(QwtPicker::AlwaysOn);
zoomer->setRubberBand(QwtPicker::RectRubberBand);
grid = new QwtPlotGrid();
grid->enableX(true);
grid->enableXMin(true);
grid->enableY(true);
grid->enableYMin(false);
grid->attach(this);
switch(mode) {
case meta::scope :
setWindowTitle(tr("Plot"));
setAxisTitle(QwtPlot::xBottom, tr("Zeit [ms]"));
setAxisTitle(QwtPlot::yLeft, tr("Spannung [V]"));
zoomer->setUnits("ms","V");
break;
case meta::fft :
setWindowTitle(tr("Fourier-Analyse"));
setAxisTitle(QwtPlot::xBottom, tr("Frequenz [Hz]"));
setAxisTitle(QwtPlot::yLeft, tr("Spannung [V]"));
zoomer->setUnits("Hz","V");
break;
default : qDebug() << "[plot] unhandled meta::plotmode";
}
p_datawrapper[0] = new datawrapper;
p_datawrapper[1] = new datawrapper;
p_datawrapper[2] = new datawrapper;
p_datawrapper[3] = new datawrapper;
curve[0] = new QwtPlotCurve(tr("Kanal 1"));
curve[0]->setData(p_datawrapper[0]);
curve[0]->attach(this);
curve[1] = new QwtPlotCurve(tr("Kanal 2"));
curve[1]->setData(p_datawrapper[1]);
curve[1]->attach(this);
curve[2] = new QwtPlotCurve(tr("Kanal 3"));
curve[2]->setData(p_datawrapper[2]);
curve[2]->attach(this);
curve[3] = new QwtPlotCurve(tr("Kanal 4"));
curve[3]->setData(p_datawrapper[3]);
curve[3]->attach(this);
if( p_mode == meta::fft ) {
p_fftthread = new fftthread(this);
connect(p_fftthread,SIGNAL(finished()),SLOT(showFftDataset()));
}
}
plot::~plot()
{
if( p_mode == meta::fft )
delete p_fftthread; //calls destructor which stops fftthread in case its running
}
int plot::getCurrentIndex() const
{
return p_currentIndex;
}
meta::plotmode plot::getMode() const
{
return p_mode;
}
void plot::showDataset(const int index)
{
p_currentIndex = index;
p_dataset = p_storage->getDataset(index);
p_dataset->flags++;
switch(p_mode) {
case meta::scope :
p_datawrapper[0]->setData(p_dataset->channel[0]);
p_datawrapper[1]->setData(p_dataset->channel[1]);
p_datawrapper[2]->setData(p_dataset->channel[2]);
p_datawrapper[3]->setData(p_dataset->channel[3]);
break;
case meta::fft :
bool allFftsFound = true;
for(unsigned int i = 0; i < 4; i++)
if( p_dataset->channel[i]->size() && p_dataset->channel[i]->getFft() == 0 ) {
allFftsFound = false;
break;
}
if( !allFftsFound ) {
p_fftthread->setDataset(p_dataset);
p_fftthread->start(); //dataset will be shown when thread is finished
p_dataset->flags--; //we don't use it anymore, fftthread sets its own flag
return; //don't start drawing if ffts are not calculated yet
} else {
p_datawrapper[0]->setData(p_dataset->channel[0]->getFft());
p_datawrapper[1]->setData(p_dataset->channel[1]->getFft());
p_datawrapper[2]->setData(p_dataset->channel[2]->getFft());
p_datawrapper[3]->setData(p_dataset->channel[3]->getFft());
}
break;
}
if( p_autoscale )
autoscale();
replot();
p_dataset->flags--;
}
void plot::showFftDataset()
{
p_dataset = p_fftthread->getDataset();
p_datawrapper[0]->setData(p_dataset->channel[0]->getFft());
p_datawrapper[1]->setData(p_dataset->channel[1]->getFft());
p_datawrapper[2]->setData(p_dataset->channel[2]->getFft());
p_datawrapper[3]->setData(p_dataset->channel[3]->getFft());
if( p_autoscale )
autoscale();
replot();
}
void plot::setYAutoscaleGrid(const double grid)
{
p_autoscaleGrid = grid;
replot();
}
void plot::setYAutoscale(bool on)
{
p_autoscale = on;
replot();
}
void plot::autoscale()
{
double lower = 0, upper = 0;
for(int i=0;i<4;i++) {
QRectF rect = p_dataset->channel[i]->boundingRect();
if( rect.bottom() < lower )
lower = rect.bottom();
if( rect.top() > upper )
upper = rect.top();
if( p_autoscaleGrid > 0 ) {
lower = floor(lower/p_autoscaleGrid)*p_autoscaleGrid;
upper = ceil(upper/p_autoscaleGrid)*p_autoscaleGrid;
}
}
setAxisScale(QwtPlot::yLeft, lower, upper);
}
void plot::updateColor(meta::colorindex ci, QColor color)
{
switch( ci ) {
case meta::background : //canvas()->setPalette(QPalette(QPalette::Base,color));
setCanvasBackground(color);
color.setBlueF(1.0-color.blueF());
color.setRedF(1.0-color.redF());
color.setGreenF(1.0-color.greenF());
zoomer->setTrackerPen(QPen(color));
zoomer->setRubberBandPen(QPen(color));
break;
case meta::grid : grid->setPen(QPen(color, 0.0, Qt::DotLine));
break;
case meta::channel1 : curve[0]->setPen(QPen(color));
break;
case meta::channel2 : curve[1]->setPen(QPen(color));
break;
case meta::channel3 : curve[2]->setPen(QPen(color));
break;
case meta::channel4 : curve[3]->setPen(QPen(color));
break;
}
replot();
}
void plot::updateViewportX(const int msecs)
{
interval.setMaxValue(interval.minValue()+msecs);
setAxisScale(QwtPlot::xBottom, interval.minValue(), interval.maxValue());
QRectF zoomBase = zoomer->zoomBase();
zoomBase.setLeft(interval.minValue());
zoomBase.setRight(interval.maxValue());
zoomer->setZoomBase(zoomBase);
replot();
}
void plot::updateViewportY(const double lower, const double upper)
{
p_autoscale = false;
setAxisScale(QwtPlot::yLeft, lower, upper);
QRectF zoomBase = zoomer->zoomBase();
zoomBase.setTop(upper);
zoomBase.setBottom(lower);
zoomer->setZoomBase(zoomBase);
replot();
}
signaldata** plot::getCurrentData()
{
return p_dataset->channel;
}
plotzoomer::plotzoomer(QWidget *canvas, bool doReplot) : QwtPlotZoomer(canvas, doReplot)
{
}
plotzoomer::plotzoomer(int xAxis, int yAxis, QWidget *canvas, bool doReplot) : QwtPlotZoomer(xAxis, yAxis, canvas, doReplot)
{
}
void plotzoomer::setUnits(const QString& hUnit, const QString& vUnit)
{
p_hUnit = hUnit;
p_vUnit = vUnit;
}
QwtText plotzoomer::trackerTextF(const QPointF& pos) const
{
QString text;
switch ( rubberBand() )
{
case HLineRubberBand:
text.sprintf( "%.3f%s", pos.y(), p_vUnit.toStdString().c_str() );
break;
case VLineRubberBand:
text.sprintf( "%.3f%s", pos.x(), p_hUnit.toStdString().c_str() );
break;
default:
text.sprintf( "%.3f%s, %.3f%s", pos.x(), p_hUnit.toStdString().c_str(), pos.y(), p_vUnit.toStdString().c_str() );
}
return QwtText( text );
}