forked from labstreaminglayer/App-LabRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
324 lines (279 loc) · 11.3 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <string>
#include <vector>
// recording class
#include "recording.h"
MainWindow::MainWindow(QWidget *parent, const char *config_file)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::close);
connect(ui->actionLoadConfig, &QAction::triggered, [this]() {
this->load_config(QFileDialog::getOpenFileName(
this, "Load Configuration File", "", "Configuration Files (*.cfg)"));
});
connect(ui->actionSaveConfig, &QAction::triggered, [this]() {
this->save_config(QFileDialog::getSaveFileName(
this, "Save Configuration File", "", "Configuration Files (*.cfg)"));
});
connect(ui->browseButton, &QPushButton::clicked, [this]() {
this->ui->locationEdit->setText(QFileDialog::getSaveFileName(
this, "Save recordings as...", "untitled.xdf", "XDF recordings (*.xdf)"));
});
connect(ui->blockList, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
this, &MainWindow::blockSelected);
connect(ui->refreshButton, &QPushButton::clicked, this, &MainWindow::refreshStreams);
connect(ui->selectAllButton, &QPushButton::clicked, this, &MainWindow::selectAllStreams);
connect(ui->selectNoneButton, &QPushButton::clicked, this, &MainWindow::selectNoStreams);
connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::startRecording);
connect(ui->stopButton, &QPushButton::clicked, this, &MainWindow::stopRecording);
connect(ui->actionAbout, &QAction::triggered, [this]() {
QString infostr = QStringLiteral("LSL library version: ") +
QString::number(lsl::library_version()) +
"\nLSL library info:" + lsl::lsl_library_info();
QMessageBox::about(this, "About LabRecorder", infostr);
});
load_config(config_file);
timer = std::make_unique<QTimer>(this);
connect(&*timer, &QTimer::timeout, this, &MainWindow::statusUpdate);
timer->start(1000);
// startTime = (int)lsl::local_clock();
}
MainWindow::~MainWindow() noexcept = default;
void MainWindow::statusUpdate() const {
if (currentRecording) {
auto elapsed = static_cast<unsigned int>(lsl::local_clock() - startTime);
auto fileinfo = QFileInfo(recFilename);
fileinfo.refresh();
auto size = fileinfo.size();
QString timeString = QStringLiteral("Recording to %1 (%2); %3kb)")
.arg(fileinfo.fileName(),
QDateTime::fromTime_t(elapsed).toUTC().toString("hh:mm:ss"),
QString::number(size / 1000));
statusBar()->showMessage(timeString);
}
}
void MainWindow::closeEvent(QCloseEvent *ev) {
if (currentRecording) ev->ignore();
}
void MainWindow::blockSelected(const QString &block) {
if (currentRecording)
QMessageBox::information(this, "Still recording",
"Please stop recording before switching blocks.", QMessageBox::Ok);
else {
currentBlock = block;
// scripted action code here...
}
// std::cout << item->text().toStdString() <<std::endl;
}
void MainWindow::load_config(QString filename) {
qInfo() << "loading config file " << filename;
try {
// if (!QFileInfo::exists(filename)) throw std::runtime_error("Settings file doesn't
// exist.");
QSettings pt(filename, QSettings::Format::IniFormat);
// ----------------------------
// required streams
// ----------------------------
requiredStreams = pt.value("RequiredStreams").toStringList();
// ----------------------------
// online sync streams
// ----------------------------
QStringList onlineSyncStreams = pt.value("OnlineSync", "").toStringList();
for (QString &oss : onlineSyncStreams) {
QStringList words = oss.split(' ', QString::SkipEmptyParts);
// The first two words ("StreamName (PC)") are the stream identifier
if (words.length() < 2) {
qInfo() << "Invalid sync stream config: " << oss;
continue;
}
QString key = words.takeFirst() + ' ' + words.takeFirst();
int val = 0;
for (const auto &word : words) {
if (word == "post_clocksync") { val |= lsl::post_clocksync; }
if (word == "post_dejitter") { val |= lsl::post_dejitter; }
if (word == "post_monotonize") { val |= lsl::post_monotonize; }
if (word == "post_threadsafe") { val |= lsl::post_threadsafe; }
if (word == "post_ALL") { val = lsl::post_ALL; }
}
syncOptionsByStreamName[key.toStdString()] = val;
qInfo() << "stream sync options: " << key << ": " << val;
}
// ----------------------------
// recording location
// ----------------------------
ui->blockList->addItems(pt.value("SessionBlocks").toStringList());
if (ui->blockList->count()) currentBlock = ui->blockList->itemText(0);
// get the path as a string
QString str_path =
pt.value("StorageLocation", "C:/Recordings/CurrentStudy/exp%n/untitled.xdf").toString();
ui->locationEdit->setText(str_path);
// replace %n as experiment number placeholder
int pos_n = str_path.indexOf(QStringLiteral("%n"));
if (pos_n != -1) {
str_path[pos_n + 1] = '1';
for (int i = 1; i < 10000; i++) {
if (!QDir(str_path.arg(i)).exists()) {
// update gui
ui->experimentNumberSpin->setValue(i);
break;
}
}
}
} catch (std::exception &e) { qWarning() << "Problem parsing config file: " << e.what(); }
// std::cout << "refreshing streams ..." <<std::endl;
refreshStreams();
}
void MainWindow::save_config(QString filename) {
QSettings settings(filename, QSettings::Format::IniFormat);
settings.setValue("StorageLocation", ui->locationEdit->text());
qInfo() << requiredStreams;
settings.setValue("RequiredStreams", requiredStreams);
// Stub.
}
QSet<QString> MainWindow::getCheckedStreams() const {
QSet<QString> checked;
for (int i = 0; i < ui->streamList->count(); i++) {
QListWidgetItem *item = ui->streamList->item(i);
if (item->checkState() == Qt::Checked) checked.insert(item->text());
}
return checked;
}
/**
* @brief MainWindow::refreshStreams Find streams, generate a list of missing streams
* and fill the UI streamlist.
* @return A vector of found stream_infos
*/
std::vector<lsl::stream_info> MainWindow::refreshStreams() {
std::vector<lsl::stream_info> resolvedStreams = lsl::resolve_streams(1.0);
QSet<QString> foundStreamNames;
for (auto &s : resolvedStreams)
foundStreamNames.insert(QString::fromStdString(s.name() + " (" + s.hostname() + ")"));
const QSet<QString> previouslyChecked = getCheckedStreams();
// Missing streams: all checked or required streams that weren't found
missingStreams = (previouslyChecked + requiredStreams.toSet()) - foundStreamNames;
// (Re-)Populate the UI list
const QBrush good_brush(QColor(0, 128, 0)), bad_brush(QColor(255, 0, 0));
ui->streamList->clear();
for (auto &&streamName : foundStreamNames + missingStreams) {
auto *item = new QListWidgetItem(streamName, ui->streamList);
item->setCheckState(previouslyChecked.contains(streamName) ? Qt::Checked : Qt::Unchecked);
item->setForeground(missingStreams.contains(streamName) ? bad_brush : good_brush);
ui->streamList->addItem(item);
}
return resolvedStreams;
}
void MainWindow::startRecording() {
if (!currentRecording) {
// automatically refresh streams
const std::vector<lsl::stream_info> resolvedStreams = refreshStreams();
const QSet<QString> checked = getCheckedStreams();
// if a checked stream is now missing
// change to "checked.intersects(missingStreams) as soon as Ubuntu 16.04/Qt 5.5 is EOL
QSet<QString> missing = checked;
if (!missing.intersect(missingStreams).isEmpty()) {
// are you sure?
QMessageBox msgBox(QMessageBox::Warning, "Stream not found",
"At least one of the streams that you checked seems to be offline",
QMessageBox::Yes | QMessageBox::No, this);
msgBox.setInformativeText("Do you want to start recording anyway?");
msgBox.setDefaultButton(QMessageBox::No);
if (msgBox.exec() != QMessageBox::Yes) return;
}
if (checked.isEmpty()) {
QMessageBox msgBox(QMessageBox::Warning, "No streams selected",
"You have selected no streams", QMessageBox::Yes | QMessageBox::No, this);
msgBox.setInformativeText("Do you want to start recording anyway?");
msgBox.setDefaultButton(QMessageBox::No);
if (msgBox.exec() != QMessageBox::Yes) return;
}
// determine the experiment number block
// scan the path for %n/%1 and %b/%2
recFilename = ui->locationEdit->text();
if (recFilename.isEmpty()) {
QMessageBox::critical(this, "Filename empty", "Can not record without a file name");
return;
}
int pos_n = recFilename.indexOf("%n");
if (pos_n != -1)
recFilename.replace(pos_n, 2, QString::number(ui->experimentNumberSpin->value()));
int pos_b = recFilename.indexOf("%b");
if (pos_b != -1) // check to make sure it is there
recFilename.replace(pos_b, 2, currentBlock);
QFileInfo recFileInfo(recFilename);
if (recFileInfo.exists()) {
if (recFileInfo.isDir()) {
QMessageBox::warning(
this, "Error", "Recording path already exists and is a directory");
return;
}
QString rename_to = recFileInfo.absolutePath() + '/' + recFileInfo.baseName() +
"_old%1." + recFileInfo.suffix();
// search for highest _oldN
int i = 1;
while (QFileInfo::exists(rename_to.arg(i))) i++;
QString newname = rename_to.arg(i);
if (!QFile::rename(recFileInfo.absoluteFilePath(), newname)) {
QMessageBox::warning(this, "Permissions issue",
"Can not rename the file " + recFilename + " to " + recFileInfo.path() + '/' +
newname);
return;
}
qInfo() << "Moved existing file to " << newname;
recFileInfo.refresh();
}
// regardless, we need to create the directory if it doesn't exist
if (!recFileInfo.dir().mkpath(".")) {
QMessageBox::warning(this, "Permissions issue",
"Can not create the directory " + recFileInfo.dir().path() +
". Please check your permissions.");
return;
}
// go through all the listed streams
std::vector<lsl::stream_info> checkedStreams;
for (const lsl::stream_info &stream : resolvedStreams)
if (checked.contains(
QString::fromStdString(stream.name() + " (" + stream.hostname() + ')')))
checkedStreams.push_back(stream);
std::vector<std::string> watchfor;
for (const QString &missing : missingStreams) watchfor.push_back(missing.toStdString());
qInfo() << "Missing: " << missingStreams;
currentRecording = std::make_unique<recording>(
recFilename.toStdString(), checkedStreams, watchfor, syncOptionsByStreamName, true);
ui->stopButton->setEnabled(true);
ui->startButton->setEnabled(false);
startTime = (int)lsl::local_clock();
} else
QMessageBox::information(
this, "Already recording", "The recording is already running", QMessageBox::Ok);
}
void MainWindow::stopRecording() {
if (!currentRecording)
QMessageBox::information(
this, "Not recording", "There is not ongoing recording", QMessageBox::Ok);
else {
try {
currentRecording = nullptr;
} catch (std::exception &e) { qWarning() << "exception on stop: " << e.what(); }
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(false);
statusBar()->showMessage("Stopped");
}
}
void MainWindow::selectAllStreams() {
for (int i = 0; i < ui->streamList->count(); i++) {
QListWidgetItem *item = ui->streamList->item(i);
item->setCheckState(Qt::Checked);
}
}
void MainWindow::selectNoStreams() {
for (int i = 0; i < ui->streamList->count(); i++) {
QListWidgetItem *item = ui->streamList->item(i);
item->setCheckState(Qt::Unchecked);
}
}