-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
320 lines (289 loc) · 9.59 KB
/
Copy pathmainwindow.cpp
File metadata and controls
320 lines (289 loc) · 9.59 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QJsonObject>
#include <QJsonParseError>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui -> setupUi(this);
mapWidgetInit();
timer0 = new QTimer(this);
connect(this->timer0,&QTimer::timeout,this,&MainWindow::updateTrack);
this -> ui -> pushButton_start -> setEnabled(false);
this -> ui -> comboBox_times_vel -> setEnabled(false);
help = new Help();
}
MainWindow::~MainWindow()
{
delete help;
channel -> deregisterObject(mapChannel);
delete ui;
}
void MainWindow::mapWidgetInit()
{
channel = new QWebChannel(this);
mapChannel = new MapChannel(this);
channel -> registerObject("passId",mapChannel);
this -> ui -> widget_map -> page() -> setWebChannel(channel);
this -> ui -> widget_map -> load(QUrl("file:///./onlinemap/map.html"));
connect(mapChannel,&MapChannel::reloadMapClicked,this,&MainWindow::reloadMap);
connect(mapChannel,&MapChannel::pointsCome,[](int index, double lng, double lat){
qDebug()<<index<<QString::number(lng,'f',6)<<QString::number(lat,'f',6);
});
}
bool MainWindow::readfromfile()
{
QString path = QFileDialog::getOpenFileName(this,"Open","./","TXT(*.txt *.csv)");
if(path.isEmpty() == false)
{
QFile file(path);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return false;
}
lines.clear();
LineIndex = 0;
this -> mapChannel -> clearTrack();
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
lines.append(line);
}
file.close();
LineLength = lines.length();
QString line = lines.at(0);
QStringList subs = line.split(",");
this -> ui -> label_time_value -> setText(subs.at(timeCol));
this -> ui -> label_lng_value -> setText(subs.at(lngCol));
this -> ui -> label_lat_value -> setText(subs.at(latCol));
this -> ui -> label_yaw_value -> setText(subs.at(yawCol));
mapChannel -> panTo(subs.at(lngCol).toDouble(),subs.at(latCol).toDouble());
return true;
}
else {
return false;
}
}
void MainWindow::updateTrack()
{
if(LineIndex < LineLength)
{
QString line = lines.at(LineIndex);
line = line.trimmed();
QStringList subs = line.split(",");
double lng = subs.at(lngCol).toDouble();
double lat = subs.at(latCol).toDouble();
double yaw = subs.at(yawCol).toDouble();
mapChannel->updateBoatPos(lng,lat,yaw);
this -> ui -> label_time_value -> setText(subs.at(timeCol));
this -> ui -> label_lng_value -> setText(subs.at(lngCol));
this -> ui -> label_lat_value -> setText(subs.at(latCol));
this -> ui -> label_yaw_value -> setText(subs.at(yawCol));
LineIndex++;
}
else
{
this -> timer0 -> stop();
this -> ui -> pushButton_start -> setText("开始回放");
this -> ui -> pushButton_start -> setEnabled(false);
this -> ui -> comboBox_times_vel -> setEnabled(false);
}
}
void MainWindow::on_pushButton_open_file_clicked()
{
timeCol = this -> ui -> spinBox_time_col -> value();
lngCol = this -> ui -> spinBox_lng_col -> value();
latCol = this -> ui -> spinBox_lat_col -> value();
yawCol = this -> ui -> spinBox_yaw_col -> value();
if(readfromfile())
{
QMessageBox::information(this,"成功","文件打开成功,请检查实时数据是否正确!");
this -> ui -> pushButton_start -> setEnabled(true);
this -> ui -> comboBox_times_vel -> setEnabled(true);
}
else {
//this->close();
QMessageBox::critical(this,"错误","文件打开失败,请重试!");
}
}
void MainWindow::on_pushButton_start_clicked() //开始
{
if(this -> ui -> pushButton_start -> text() == "开始回放")
{
switch(this -> ui -> comboBox_times_vel -> currentIndex())
{
case 0:
this -> TimersVel = 1;
break;
case 1:
this -> TimersVel = 1.25;
break;
case 2:
this -> TimersVel = 1.5;
break;
case 3:
this -> TimersVel = 2;
break;
case 4:
this -> TimersVel = 3;
break;
case 5:
this -> TimersVel = 5;
break;
case 6:
this -> TimersVel = 10;
break;
case 7:
this -> TimersVel = 15;
break;
default:
break;
}
this -> timer0 -> start(int(1000/TimersVel));
this -> ui -> pushButton_start -> setText("停止回放");
}
else {
this -> timer0 -> stop();
this -> ui -> pushButton_start -> setText("开始回放");
}
}
void MainWindow::on_comboBox_times_vel_currentIndexChanged(int index)
{
switch(index)
{
case 0:
this -> TimersVel = 1;
break;
case 1:
this -> TimersVel = 1.25;
break;
case 2:
this -> TimersVel = 1.5;
break;
case 3:
this -> TimersVel = 2;
break;
case 4:
this -> TimersVel = 3;
break;
case 5:
this -> TimersVel = 5;
break;
case 6:
this -> TimersVel = 10;
break;
case 7:
this -> TimersVel = 15;
break;
default:
break;
}
if(this -> ui -> pushButton_start -> text() == "停止回放")
this -> timer0 ->start(int(1000/TimersVel));
}
void MainWindow::on_spinBox_time_col_valueChanged(int arg1)
{
timeCol = arg1;
QString line = lines.at(0);
QStringList subs = line.split(",");
this -> ui -> label_time_value -> setText(subs.at(timeCol));
}
void MainWindow::on_spinBox_lng_col_valueChanged(int arg1)
{
lngCol = arg1;
QString line = lines.at(0);
QStringList subs = line.split(",");
this -> ui -> label_lng_value -> setText(subs.at(lngCol));
mapChannel -> panTo(subs.at(lngCol).toDouble(),subs.at(latCol).toDouble());
}
void MainWindow::on_spinBox_lat_col_valueChanged(int arg1)
{
latCol = arg1;
QString line = lines.at(0);
QStringList subs = line.split(",");
this -> ui -> label_lat_value -> setText(subs.at(latCol));
mapChannel -> panTo(subs.at(lngCol).toDouble(),subs.at(latCol).toDouble());
}
void MainWindow::on_spinBox_yaw_col_valueChanged(int arg1)
{
yawCol = arg1;
QString line = lines.at(0);
QStringList subs = line.split(",");
this -> ui -> label_yaw_value -> setText(subs.at(yawCol));
}
void MainWindow::reloadMap()
{
this -> ui -> widget_map -> load(QUrl("file:///./onlinemap/map.html"));
}
void MainWindow::on_pushButton_open_desired_track_file_clicked()
{
QString path = QFileDialog::getOpenFileName(this,"Open Json Task",QDir::currentPath()+"/task","JSON File(*.json)");
QFile jsonFile(path);
if(!jsonFile.open(QIODevice::ReadOnly))
{
QMessageBox::information(this,"提示","Json文件打开失败,请重试");
return;
}
QJsonParseError jsonError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonFile.readAll(),&jsonError));
jsonFile.close();
if(jsonError.error != QJsonParseError::NoError)
{
QMessageBox::information(this,"提示",QString("Json解析失败,错误代码:%1,%2").arg(jsonError.error).arg(jsonError.errorString()));
return;
}
QJsonObject rootObj = jsonDoc.object();
mapChannel->clearWaypoints();
int pointsNum=0;
if(rootObj.contains("pointsNum"))
{
QJsonValue pointsNumValue = rootObj.value("pointsNum");
if(pointsNumValue.isDouble())
pointsNum = pointsNumValue.toInt();
else{
QMessageBox::information(this,"提示","Json中pointsNum类型错误!");
return;
}
}else{
QMessageBox::information(this,"提示","Json中无pointsNum!");
return;
}
if(rootObj.contains("waypoints"))
{
QVector<double> lng(pointsNum);
QVector<double> lat(pointsNum);
QJsonValue waypointsValue = rootObj.value("waypoints");
if(waypointsValue.isObject())
{
QJsonObject waypointsObject = waypointsValue.toObject();
for (int i=0; i<pointsNum; i++)
{
QString waypointIndex = QString::number(i);
if(waypointsObject.contains(waypointIndex))
{
QJsonObject waypointObject = waypointsObject.value(waypointIndex).toObject();
lng[i] = waypointObject.value("lng").toDouble();
lat[i] = waypointObject.value("lat").toDouble();
qDebug()<<waypointIndex<< lng[i]<< lat[i];
}else{
QMessageBox::information(this,"提示","Json中pointsNum和waypoints内的航点个数不同!");
return;
}
}
for(int i=0; i<pointsNum; i++)
mapChannel->addPoint(lng[i],lat[i]);
}else{
QMessageBox::information(this,"提示","Json中waypoints不是JsonObject!");
return;
}
}else{
QMessageBox::information(this,"提示","Json中无waypoints!");
return;
}
}
void MainWindow::on_pushButton_help_clicked()
{
help->show();
}