-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.h
212 lines (185 loc) · 6.03 KB
/
MainWindow.h
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
#pragma once
#include <QtWebSockets/qwebsocket.h>
#include <qdir.h>
#include <qfile.h>
#include <qtimer.h>
#include <QMessageBox>
#include <QtWidgets/QMainWindow>
#include <functional>
#include <memory>
#include "LmType.h"
#include "ui_MainWindow.h"
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget* parent = Q_NULLPTR) : QMainWindow(parent) {
ui.setupUi(this);
resetUrl();
connect(&wsocket, &QWebSocket::textMessageReceived, this,
&MainWindow::onFrame);
connect(ui.btnConnect, &QPushButton::clicked, this, &MainWindow::lmOpen);
connect(ui.btnDisconnect, &QPushButton::clicked, this,
&MainWindow::lmClose);
connect(ui.btnReset, &QPushButton::clicked, this, &MainWindow::resetUrl);
connect(ui.editAction, &QLineEdit::textChanged, this,
&MainWindow::filenameChanged);
connect(ui.spinRepeat, SIGNAL(valueChanged(int)), this,
SLOT(filenameChanged()));
setEditsEnabled(true);
connect(ui.btnStart, &QPushButton::clicked, this, &MainWindow::start);
connect(ui.btnStop, &QPushButton::clicked, this, &MainWindow::stop);
connect(ui.btnClear, &QPushButton::clicked,
[this]() { ui.textLog->clear(); });
connect(&timer, &QTimer::timeout, this, &MainWindow::onTimeOut);
ui.pgrTime->setRange(0, 100);
csv.reserve(50000);
}
private:
Ui::MainWindowClass ui;
QWebSocket wsocket;
QTimer timer;
enum State { Stop, Prepare, Collect } state;
QString csv;
void resetUrl() { ui.editUrl->setText("ws://127.0.0.1:6437/v6.json"); }
void setEditsEnabled(bool enabled) {
ui.editUrl->setEnabled(enabled);
ui.btnReset->setEnabled(enabled);
ui.btnConnect->setEnabled(enabled);
ui.btnDisconnect->setEnabled(enabled);
ui.editAction->setEnabled(enabled);
ui.spinRepeat->setEnabled(enabled);
ui.spinPrepare->setEnabled(enabled);
ui.spinTime->setEnabled(enabled);
ui.btnStart->setEnabled(enabled);
ui.btnStop->setEnabled(!enabled);
ui.listFilename->setDisabled(!enabled);
}
private slots:
void start() {
if (ui.editAction->text().isEmpty()) {
QMessageBox::critical(
this, QStringLiteral("错误"),
ui.editAction->toolTip() + QStringLiteral("不能为空"));
return;
}
setEditsEnabled(false);
changeState(State::Prepare);
}
void changeState(State s) {
switch (s) {
case State::Stop:
ui.textLog->append(QStringLiteral("已停止....\n\n"));
timerStop();
setEditsEnabled(true);
break;
case State::Prepare:
ui.textLog->append(QStringLiteral("请准备....\n\n"));
timerStart(ui.spinPrepare->value());
break;
case State::Collect:
ui.textLog->append(QStringLiteral("数据收集开始 ->") +
ui.listFilename->currentItem()->text() + "\n\n");
csv.clear();
timerStart(ui.spinTime->value());
break;
default:
break;
}
state = s;
}
void stop() { changeState(Stop); }
void timerStart(double s) {
if (timer.isActive()) {
timer.stop();
}
timer.start(s * 10);
ui.pgrTime->setValue(0);
}
void timerStop() {
if (timer.isActive()) {
timer.stop();
ui.pgrTime->setValue(0);
}
}
void onTimeOut() {
if (state != Stop) {
int v = ui.pgrTime->value();
if (v < 100) {
ui.pgrTime->setValue(v + 1);
} else {
switch (state) {
case State::Prepare:
changeState(Collect);
break;
case State::Collect: {
{
auto filename = ui.listFilename->currentItem()->text();
QDir().mkpath(QApplication::applicationDirPath() +
QFileInfo(filename).dir().path());
QFile file(QApplication::applicationDirPath() + filename);
if (file.open(QFile::WriteOnly | QIODevice::Truncate)) {
QTextStream out(&file);
out << csv;
file.close();
ui.textLog->append(QStringLiteral("已保存到 ") +
file.fileName() + "\n\n");
} else {
ui.textLog->append(QStringLiteral("保存失败, ") +
file.errorString() + "\n\n");
}
}
auto index = ui.listFilename->currentRow();
if (index < ui.listFilename->count() - 1) {
ui.listFilename->setCurrentRow(index + 1);
changeState(Prepare);
} else {
changeState(Stop);
}
} break;
default:
break;
}
}
}
}
void lmOpen() {
if (ui.editUrl->text().isEmpty()) {
QMessageBox::critical(this, QStringLiteral("错误"),
ui.editUrl->toolTip() + QStringLiteral("不能为空"));
return;
}
wsocket.close();
wsocket.open(QUrl{ui.editUrl->text()});
}
void filenameChanged() {
QString actionName = ui.editAction->text();
if (actionName.isEmpty()) return;
int repeatTime = ui.spinRepeat->value();
ui.listFilename->clear();
QStringList filenames;
for (int i = 0; i < repeatTime; i++)
filenames.append("/data/" + actionName + "/" + QString::number(i) +
".csv");
ui.listFilename->addItems(filenames);
ui.listFilename->setCurrentRow(0);
}
void lmClose() { wsocket.close(); }
void onFrame(QString const& msg) {
auto doc = QJsonDocument::fromJson(msg.toLatin1());
mxt::Frame frame(doc.object());
if (frame.hands.size() != 0) {
ui.labelCurrentFrame->setText(
QStringLiteral("当前帧:id: %1, timestamp:%2, Palm:(%3)")
.arg(frame.id)
.arg(frame.timestamp)
.arg(mxt::detail::makeXyzValue(
frame.hands.begin()->second.palmPosition)));
if (state == Collect) {
if (csv.isEmpty()) csv.append(frame.toCsvLine(true));
csv.append(frame.toCsvLine());
}
} else if (frame.id == 0) {
ui.textLog->append(msg + "\n\n");
}
}
};