-
Notifications
You must be signed in to change notification settings - Fork 1
/
showwave.cpp
472 lines (419 loc) · 13.3 KB
/
showwave.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "showwave.h"
#include "ui_showwave.h"
#include "common.h"
#include <qwt_symbol.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_magnifier.h>
#include <qwt_legend.h>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QVector>
#include <QMessageBox>
#include <QDebug>
ShowWave::ShowWave(QWidget *parent) :QMainWindow(parent),ui(new Ui::ShowWave),
id_timer(0),time(0),
_flag(FLAG_CLEAR),
recv_data(new QString()),
ch1_curve(NULL),
ch2_curve(NULL),
ch3_curve(NULL),
ch4_curve(NULL),
grid(NULL)
{
ui->setupUi(this);
this->setWindowTitle("Show Wave");
ui->ch1CheckBox->setChecked(true);
initWavePlot();
initCurves(CH1_VALUE);
connect(ui->ch1CheckBox, SIGNAL(clicked()), this, SLOT(showOrHideCurve1()));
connect(ui->ch2CheckBox, SIGNAL(clicked()), this, SLOT(showOrHideCurve2()));
connect(ui->ch3CheckBox, SIGNAL(clicked()), this, SLOT(showOrHideCurve3()));
connect(ui->ch4CheckBox, SIGNAL(clicked()), this, SLOT(showOrHideCurve4()));
}
ShowWave::~ShowWave()
{
delete recv_data;
delete ui;
#ifdef USE_DEBUG
qDebug()<<"The showwave window is destoryed!!!";
#endif
}
void ShowWave::on_showWaveBtn_clicked()
{ //如果之前的操作是清除操作,则重新启动一个定时器
if(_flag == FLAG_CLEAR){
//启动定时器,100ms响应,用于模拟产生实时数据
id_timer = this->startTimer(100);
if(ch1_curve && ui->ch1CheckBox->isChecked())
ch1_curve->setVisible(true);
if(ch2_curve && ui->ch2CheckBox->isChecked())
ch2_curve->setVisible(true);
if(ch3_curve && ui->ch3CheckBox->isChecked())
ch3_curve->setVisible(true);
if(ch4_curve && ui->ch4CheckBox->isChecked())
ch4_curve->setVisible(true);
ui->wavePlot->replot();
}
_flag = FLAG_SHOW;
}
void ShowWave::on_clearWaveBtn_clicked()
{
ch_x_data.clear();
ch1_y_data.clear();
ch2_y_data.clear();
ch3_y_data.clear();
ch4_y_data.clear();
if(ch1_curve && ch1_curve->isVisible())
ch1_curve->setVisible(false);
if(ch2_curve && ch2_curve->isVisible())
ch2_curve->setVisible(false);
if(ch3_curve && ch3_curve->isVisible())
ch3_curve->setVisible(false);
if(ch4_curve && ch4_curve->isVisible())
ch4_curve->setVisible(false);
ui->wavePlot->replot();
ui->ch1CheckBox->setChecked(false);
ui->ch2CheckBox->setChecked(false);
ui->ch3CheckBox->setChecked(false);
ui->ch4CheckBox->setChecked(false);
//如果前一个操作不是清零操作,则停掉定时器并清零计数
if(_flag != FLAG_CLEAR){
this->killTimer(id_timer);
time = 0;
}
_flag = FLAG_CLEAR;
}
void ShowWave::on_drawGridBtn_clicked()
{
if(!grid){
grid = new QwtPlotGrid();
grid->setPen(QColor(0,0,0),1, Qt::DotLine);
grid->attach(ui->wavePlot);
ui->wavePlot->replot();
}
else{
if(grid->isVisible())
grid->setVisible(false);
else
grid->setVisible(true);
ui->wavePlot->replot();
}
}
void ShowWave::on_pauseWaveBtn_clicked()
{
_flag = FLAG_PAUSE;
}
void ShowWave::on_frame_ch1_editingFinished()
{
frame_ch1_str = ui->frame_ch1->text();
if(frame_ch1_str.isEmpty())
{
QMessageBox::critical(this, "Error",
tr("帧首不能为空"));
}
#ifdef USE_DEBUG
qDebug()<<"frame_1"<<frame_ch1_str;
#endif
}
void ShowWave::on_frame_ch2_editingFinished()
{
frame_ch2_str = ui->frame_ch2->text();
if(frame_ch2_str.isEmpty())
{
QMessageBox::critical(this, "Error",
tr("帧首不能为空"));
}
#ifdef USE_DEBUG
qDebug()<<"frame_2"<<frame_ch2_str;
#endif
}
void ShowWave::on_frame_ch3_editingFinished()
{
frame_ch3_str = ui->frame_ch3->text();
if(frame_ch3_str.isEmpty())
{
QMessageBox::critical(this, "Error",
tr("帧首不能为空"));
}
#ifdef USE_DEBUG
qDebug()<<"frame_3"<<frame_ch3_str;
#endif
}
void ShowWave::on_frame_ch4_editingFinished()
{
frame_ch4_str = ui->frame_ch4->text();
if(frame_ch4_str.isEmpty())
{
QMessageBox::critical(this, "Error",
tr("帧首不能为空"));
}
#ifdef USE_DEBUG
qDebug()<<"frame_4"<<frame_ch4_str;
#endif
}
void ShowWave::showOrHideCurve1()
{
if(ch1_curve){
if(ui->ch1CheckBox->isChecked())
ch1_curve->setVisible(true);
else
ch1_curve->setVisible(false);
ui->wavePlot->replot();
}
else{
initCurves(CH1_VALUE);
}
}
void ShowWave::showOrHideCurve2()
{
if(ch2_curve){
if(ui->ch2CheckBox->isChecked())
ch2_curve->setVisible(true);
else
ch2_curve->setVisible(false);
ui->wavePlot->replot();
}
else{
initCurves(CH2_VALUE);
}
}
void ShowWave::showOrHideCurve3()
{
if(ch3_curve){
if(ui->ch3CheckBox->isChecked())
ch3_curve->setVisible(true);
else
ch3_curve->setVisible(false);
ui->wavePlot->replot();
}
else{
initCurves(CH3_VALUE);
}
}
void ShowWave::showOrHideCurve4()
{
if(ch4_curve){
if(ui->ch4CheckBox->isChecked())
ch4_curve->setVisible(true);
else
ch4_curve->setVisible(false);
ui->wavePlot->replot();
}
else{
initCurves(CH4_VALUE);
}
}
//时间更新事件
void ShowWave::timerEvent(QTimerEvent *)
{
if(_flag == FLAG_SHOW){
time += 0.1;
ch_x_data.append(time);
//重新加载数据
if(ch1_curve)
ch1_curve->setSamples(ch_x_data, ch1_y_data);
if(ch2_curve)
ch2_curve->setSamples(ch_x_data, ch2_y_data);
if(ch3_curve)
ch3_curve->setSamples(ch_x_data, ch3_y_data);
if(ch4_curve)
ch4_curve->setSamples(ch_x_data, ch4_y_data);
ui->wavePlot->replot();
}
#ifdef USE_DEBUG
// qDebug()<<"Enter timer event";
#endif
}
void ShowWave::closeEvent(QCloseEvent *)
{
//reset all configuration
this->killTimer(id_timer);
time = 0;
_flag = FLAG_CLEAR;
ch_x_data.clear();
ch1_y_data.clear();
ch2_y_data.clear();
ch3_y_data.clear();
ch4_y_data.clear();
ui->ch1CheckBox->setChecked(false);
ui->ch2CheckBox->setChecked(false);
ui->ch3CheckBox->setChecked(false);
ui->ch4CheckBox->setChecked(false);
#ifdef USE_DEBUG
qDebug()<<"The waveshow window is closed.";
#endif
}
//初始化plot
void ShowWave::initWavePlot(){
ui->wavePlot->setTitle("MultiChannels Plot");
ui->wavePlot->setCanvasBackground( Qt::white );
// ui->wavePlot->setAutoDelete(true);
//设置坐标轴的名称
ui->wavePlot->setAxisTitle(QwtPlot::xBottom, "X->");
ui->wavePlot->setAxisTitle(QwtPlot::yLeft, "Y->");
//设置X轴自动调节,Y轴自动调节
ui->wavePlot->setAxisAutoScale(QwtPlot::xBottom,true);
ui->wavePlot->setAxisAutoScale(QwtPlot::yLeft, true);
//设置图例在右边
ui->wavePlot->insertLegend(new QwtLegend, QwtPlot::RightLegend);
ui->wavePlot->insertLegend(new QwtLegend, QwtPlot::RightLegend);
ui->wavePlot->insertLegend(new QwtLegend, QwtPlot::RightLegend);
ui->wavePlot->insertLegend(new QwtLegend, QwtPlot::RightLegend);
//使用滚轮放大/缩小
(void) new QwtPlotMagnifier( ui->wavePlot->canvas());
//使用鼠标左键平移
(void) new QwtPlotPanner( ui->wavePlot->canvas());
}
//构造曲线数据
void ShowWave::initCurves(quint8 chx){
switch(chx)
{
case CH1_VALUE:
ch1_curve = new QwtPlotCurve("CH1");
ch1_curve->setStyle(QwtPlotCurve::Lines);//直线形式
ch1_curve->setCurveFitter(NULL);//不设置曲线插值,就使用原生直线段
ch1_curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
// ch1_curve->setCurveAttribute(QwtPlotCurve::Fitted, true);//使曲线更光滑
ch1_curve->setPen(QPen(CH1_CURVE_COLOR, CH_PEN_WIDTH));//设置画笔
ch1_curve->setSamples(ch_x_data, ch1_y_data);
ch1_curve->attach(ui->wavePlot);//把曲线附加到plot上
break;
case CH2_VALUE:
ch2_curve = new QwtPlotCurve("CH2");
ch2_curve->setStyle(QwtPlotCurve::Lines);
ch2_curve->setCurveFitter(NULL);
ch2_curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
// ch2_curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
ch2_curve->setPen(QPen(CH2_CURVE_COLOR, CH_PEN_WIDTH));
ch2_curve->setSamples(ch_x_data, ch2_y_data);
ch2_curve->attach(ui->wavePlot);
break;
case CH3_VALUE:
ch3_curve = new QwtPlotCurve("CH3");
ch3_curve->setStyle(QwtPlotCurve::Lines);
ch3_curve->setCurveFitter(NULL);
ch3_curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
// ch3_curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
ch3_curve->setPen(QPen(CH3_CURVE_COLOR, CH_PEN_WIDTH));
ch3_curve->setSamples(ch_x_data, ch3_y_data);
ch3_curve->attach(ui->wavePlot);
break;
case CH4_VALUE:
ch4_curve = new QwtPlotCurve("CH4");
ch4_curve->setStyle(QwtPlotCurve::Lines);
ch4_curve->setCurveFitter(NULL);
ch4_curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
// ch4_curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
ch4_curve->setPen(QPen(CH4_CURVE_COLOR, CH_PEN_WIDTH));
ch4_curve->setSamples(ch_x_data, ch4_y_data);
ch4_curve->attach(ui->wavePlot);
break;
}
}
//接收从串口得到的一行数据
void ShowWave::putData(const QByteArray &data)
{
*recv_data = QString(data).trimmed();
qDebug()<<*recv_data;
if(!frame_ch1_str.isEmpty() && recv_data->startsWith(frame_ch1_str, Qt::CaseInsensitive))
{
recv_data->remove(frame_ch1_str, Qt::CaseInsensitive);
qDebug()<<*recv_data;
ch1_y_data.append(recv_data->toDouble());
}
else{
if(!frame_ch2_str.isEmpty()&& recv_data->startsWith(frame_ch2_str, Qt::CaseInsensitive))
{
recv_data->remove(frame_ch2_str, Qt::CaseInsensitive);
ch2_y_data.append(recv_data->toDouble());
}
else{
if(!frame_ch3_str.isEmpty()&& recv_data->startsWith(frame_ch3_str, Qt::CaseInsensitive))
{
recv_data->remove(frame_ch3_str, Qt::CaseInsensitive);
ch3_y_data.append(recv_data->toDouble());
}
else{
if(!frame_ch4_str.isEmpty() && recv_data->startsWith(frame_ch4_str, Qt::CaseInsensitive))
{
recv_data->remove(frame_ch4_str, Qt::CaseInsensitive);
ch4_y_data.append(recv_data->toDouble());
}
}
}
}
// if(_flag == FLAG_SHOW){
// time ++;
// ch_x_data.append(time);
// //重新加载数据
// if(ch1_curve)
// ch1_curve->setSamples(ch_x_data, ch1_y_data);
// if(ch2_curve)
// ch2_curve->setSamples(ch_x_data, ch2_y_data);
// if(ch3_curve)
// ch3_curve->setSamples(ch_x_data, ch3_y_data);
// if(ch4_curve)
// ch4_curve->setSamples(ch_x_data, ch4_y_data);
// ui->wavePlot->replot();
// }
}
void ShowWave::on_loadFileBtn_clicked()
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("打开文件"),
".",
("Text File(*.txt)"));
if(fileName.isEmpty()){
return ;
}
else{
QFile fileOut(fileName);
if(!fileOut.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(this,"Error",tr("打开文件失败!"));
}
else{
QTextStream ts(&fileOut);
QString strLine;
while(!ts.atEnd()){
ts>>strLine;//读取打头的单词
if(strLine == "")
{
ts.skipWhiteSpace();
continue;
}
if(strLine.startsWith(frame_ch1_str, Qt::CaseInsensitive))
{
strLine.remove(frame_ch1_str,Qt::CaseInsensitive);
ch1_y_data.append(strLine.toDouble());
ts.readLine();
continue;
}
if(strLine.startsWith(frame_ch2_str, Qt::CaseInsensitive))
{
strLine.remove(frame_ch2_str,Qt::CaseInsensitive);
ch2_y_data.append(strLine.toDouble());
ts.readLine();
continue;
}
if(strLine.startsWith(frame_ch3_str, Qt::CaseInsensitive))
{
strLine.remove(frame_ch3_str,Qt::CaseInsensitive);
ch3_y_data.append(strLine.toDouble());
ts.readLine();
continue;
}
if(strLine.startsWith(frame_ch4_str, Qt::CaseInsensitive))
{
strLine.remove(frame_ch4_str,Qt::CaseInsensitive);
ch4_y_data.append(strLine.toDouble());
ts.readLine();
continue;
}
}
fileOut.close();
QMessageBox::information(this, "Information", tr("加载成功"));
}
}
}