-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathampplot.cpp
226 lines (180 loc) · 6.05 KB
/
ampplot.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
#include "ampplot.h"
#include <QDebug>
AmpPlot::AmpPlot(QwtPlot *plot)
{
//TODO
// _picker = new QwtPicker(canvas());
// _picker->setStateMachine(new QwtPickerDragRectMachine());
// _picker->setTrackerMode(QwtPicker::ActiveOnly);
// _picker->setRubberBand(QwtPicker::RectRubberBand);
_plot = plot;
_panner = new QwtPlotPanner(_plot->canvas()); //Panning with the mouse
_panner->setOrientations(Qt::Horizontal);
//connect(_panner, SIGNAL(moved(int,int)), this, SLOT(pannerMoved(int,int)));
_magnifier = new QwtPlotMagnifier(_plot->canvas()); //Zooming with the wheel
_plot->canvas()->setBorderRadius(5);
_plot->setCanvasBackground(Qt::white);
_plot->plotLayout()->setAlignCanvasToScales(true);
QwtLegend *legend = new QwtLegend;
_plot->insertLegend(legend,QwtPlot::RightLegend);
legend->setItemMode(QwtLegend::CheckableItem);
connect(_plot,SIGNAL(legendChecked(QwtPlotItem*,bool)),SLOT(showCurve(QwtPlotItem*,bool)));
_xMax = DEFAULT_X_MAX;
_yMax = DEFAULT_Y_MAX;
_plot->setAxisTitle(QwtPlot::xBottom, "ms");
_plot->setAxisScale(QwtPlot::xBottom,0,_xMax);
QwtScaleWidget *scaleWidget = _plot->axisWidget(QwtPlot::xBottom);
const int fmh = QFontMetrics(scaleWidget->font()).height();
scaleWidget->setMinBorderDist(0, fmh*2);
_plot->setAxisTitle(QwtPlot::yLeft,"mA");
_plot->setAxisAutoScale(QwtPlot::yLeft, true);
_dataCurve = new QwtPlotCurve("mA");
_dataCurve->attach(_plot);
_dataCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
_dataCurve->setVisible(_plot);
_meanCurve = new QwtPlotCurve("avg mean");
_meanCurve->attach(_plot);
_meanCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
_meanCurve->setPen(QPen(Qt::red));
_currentMeanCurve = new QwtPlotCurve("mean");
_currentMeanCurve->attach(_plot);
_currentMeanCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
_currentMeanCurve->setPen(QPen(Qt::blue));
showCurve(_dataCurve, true);
showCurve(_meanCurve, false);
showCurve(_currentMeanCurve, true);
_time = new QTime();
_time->start();
_meanData.append(0.0);
_meanTime.append(0.0);
_meanData.append(0.0);
_meanTime.append(0.0);
_pauseTime = 0;
_dataSource = NULL;
_loadedCurve = NULL;
}
void AmpPlot::setDataSource(DataSource *source)
{
qDebug() << "Setting Data Source...";
if (_dataSource)
delete _dataSource;
_dataSource = source;
connect(_dataSource, SIGNAL(dataRead(double,double)), this, SLOT(dataRead(double, double)));
}
void AmpPlot::startRead()
{
if (_dataSource)
{
_pauseTime = 0;
_data.clear();
_dataTime.clear();
_dataSource->stopRead();
_dataSource->startRead();
_meanTime.first() = 0.0;
_meanTime.last() = 0.0;
_currentMeanData.clear();
_time->restart();
_plot->setAxisScale(QwtPlot::xBottom,0, _xMax);
}
}
void AmpPlot::pauseRead()
{
if (_dataSource)
_dataSource->stopRead();
_pauseTime = _dataTime.last();
}
void AmpPlot::unpauseRead()
{
if(_dataSource)
_dataSource->startRead();
}
QwtPlotCurve *AmpPlot::getCurve()
{
return _dataCurve;
}
void AmpPlot::dataRead(double value, double time)
{
double size;
_data.append(value);
_dataTime.append(time+_pauseTime);
size = _data.size();
// lastMean = _mean;
_mean = (1 - (1/size))*_mean + (1/size)*value;
// _sd = ((size-1) * _sd + (value -_mean)*(value - lastMean))* (1 / size);
_meanData.first() = _mean;
_meanData.last() = _mean;
_meanTime.last() = _dataTime.last();
_currentMeanData.append(_mean);
_dataCurve->setRawSamples(_dataTime.data(), _data.data(), _data.size());
_meanCurve->setRawSamples(_meanTime.data(), _meanData.data(), _meanTime.size());
_currentMeanCurve->setRawSamples(_dataTime.data(), _currentMeanData.data(), _dataTime.size());
emit meanChanged(_mean);
_plot->setAxisScale(QwtPlot::xBottom,_dataTime.last() - _xMax, _dataTime.last());
_plot->replot();
}
void AmpPlot::pannerMoved(int dx, int dy)
{
qDebug() << dx << " - " << dy;
}
void AmpPlot::showCurve(QwtPlotItem *item, bool on)
{
item->setVisible(on);
QWidget *w = _plot->legend()->find(item);
if (w && w->inherits("QwtLegendItem"))
((QwtLegendItem *)w)->setChecked(on);
_plot->replot();
}
void AmpPlot::saveToCSV(QString fileName)
{
QFileInfo fileInfo(fileName);
if(fileInfo.suffix() == NULL)
fileName += ".csv";
QFile *file = new QFile(fileName);
if(file->open(QIODevice::WriteOnly))
{
QTextStream fileStream(file);
fileStream << "Time,Value\n";
for(int i = 0; i < _data.size(); ++i)
fileStream << QString().number(_dataTime.data()[i]) + "," + QString().number(_data.data()[i]) + "\n";
}
delete(file);
}
void AmpPlot::loadFromCSV(QString fileName)
{
QFile *file;
file = new QFile(fileName);
if(_loadedCurve)
_loadedCurve->detach();
delete(_loadedCurve);
_loadedData.clear();
_loadedTime.clear();
if(file->open(QIODevice::ReadOnly))
{
QTextStream fileStream(file);
QString line;
if((line = fileStream.readLine()) != NULL)
{
QStringList tokens;
tokens = line.split(",");
if (tokens.size() < 2)
return;
while(!fileStream.atEnd())
{
tokens = fileStream.readLine().split(",");
if(tokens.size() < 2)
return;
_loadedTime.append(tokens.first().toDouble());
_loadedData.append(tokens.last().toDouble());
}
}
}
QFileInfo pathInfo(fileName);
_loadedCurve = new QwtPlotCurve(pathInfo.baseName());
_loadedCurve->attach(_plot);
_loadedCurve->setRenderHint(QwtPlotItem::RenderAntialiased);
_loadedCurve->setPen(QPen(Qt::darkGreen));
_loadedCurve->setVisible(_plot);
_loadedCurve->setRawSamples(_loadedTime.data(), _loadedData.data(), _loadedData.size());
showCurve(_loadedCurve, true);
_plot->replot();
}