-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
406 lines (367 loc) · 13.8 KB
/
Copy pathmainwindow.cpp
File metadata and controls
406 lines (367 loc) · 13.8 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
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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QChart>
#include <QLineEdit>
#include <QLineSeries>
#include <QLogValueAxis>
#include <QStatusBar>
#include <QThread>
#include <QValueAxis>
#include "config.h"
#include "exerciserowwidget.h"
#include "exercisesmodel.h"
#include "libp8020/libp8020.h"
#include "protocol.h"
#include "protocolsmodel.h"
#include "testworker.h"
static const qsizetype sRawSampleRange = 180;
static const qsizetype sAmbientSampleRange = 40;
static const qsizetype sSpecimenSampleRange = 60;
void
MainWindow::device_callback(const P8020DeviceNotification* notification,
void* cb_data)
{
auto mw = static_cast<MainWindow*>(cb_data);
switch (notification->tag) {
case P8020DeviceNotification::Tag::Sample: {
const double sample = notification->sample.particle_conc;
emit mw->receivedRawSample(notification->sample.device_id, sample);
if (sample < 100) {
emit mw->renderRawSample(QString::number(sample, 'f', 2));
} else {
emit mw->renderRawSample(QString::number(sample, 'f', 0));
}
break;
}
case P8020DeviceNotification::Tag::ConnectionClosed:
// TODO: handle loss of connection.
break;
case P8020DeviceNotification::Tag::DevicePropertiesAvailable:
emit mw->refreshStatusBar();
break;
case P8020DeviceNotification::Tag::TestStarted:
emit mw->testStarted();
break;
case P8020DeviceNotification::Tag::TestCompleted:
emit mw->testCompleted();
break;
case P8020DeviceNotification::Tag::TestCancelled:
emit mw->testCancelled();
break;
}
}
void
MainWindow::test_callback(const TestNotification* notification, void* cb_data)
{
auto mw = static_cast<MainWindow*>(cb_data);
switch (notification->tag) {
case TestNotification::Tag::StateChange: {
const TestState ts = notification->state_change.test_state;
switch (ts.tag) {
case TestState::Tag::StartedExercise:
emit mw->exerciseChanged(ts.started_exercise.exercise);
break;
default:
// TODO: handle these.
break;
}
break;
}
case TestNotification::Tag::ExerciseResult: {
auto& result = notification->exercise_result;
emit mw->ffUpdated(result.device_id, result.exercise, result.fit_factor, result.error);
break;
};
case TestNotification::Tag::Sample: {
auto& sample = notification->sample.data;
switch (sample.sample_type) {
case SampleType::AmbientSample:
case SampleType::SpecimenSample:
emit mw->receivedSample(sample.device_id,
sample.sample_type,
sample.exercise,
sample.value);
break;
case SampleType::AmbientPurge:
case SampleType::SpecimenPurge:
break;
}
break;
}
case TestNotification::Tag::LiveFF: {
auto& ff = notification->live_ff;
emit mw->receivedLiveFF(
ff.device_id, ff.exercise, ff.index, ff.fit_factor);
break;
}
case TestNotification::Tag::InterimFF:
auto& ff = notification->interim_ff;
emit mw->receivedInterimFF(ff.device_id, ff.exercise, ff.fit_factor);
break;
}
}
void
MainWindow::processRawSample(const size_t aDeviceIndex, const double aSample)
{
mUI->rawChartView->addDatapoint(aDeviceIndex, /*seriesIndex*/ 0, aSample);
}
void
MainWindow::processSample(const size_t aDeviceIndex,
const SampleType aSampleType,
const size_t aExercise,
const double aValue)
{
switch (aSampleType) {
case SampleType::AmbientSample:
mUI->ambientSampleGraph->addDatapoint(aDeviceIndex, aExercise, aValue);
break;
case SampleType::SpecimenSample:
mUI->specimenSampleGraph->addDatapoint(aDeviceIndex, aExercise, aValue);
break;
default:
// TODO: handle remaining cases.
break;
}
}
void
MainWindow::processLiveFF(const size_t aDeviceIndex,
const size_t aExercise,
const size_t /* aIndex */,
const double aFitFactor)
{
mUI->liveFFGraph->addDatapoint(aDeviceIndex, aExercise, aFitFactor);
}
void
MainWindow::subjectOrSpecimenEntryChanged(const QString&)
{
bool enableTestStartPanel =
mUI->subjectSelector->currentText().length() > 0 &&
mUI->specimenSelector->currentText().length() > 0;
mUI->startTestGroupBox->setEnabled(enableTestStartPanel);
}
void
MainWindow::refreshStatusBar()
{
QString message = QString("Incolata v") + INCOLATA_VERSION;
if (mDevice != nullptr) {
P8020DeviceProperties* deviceProperties =
p8020_device_get_properties(mDevice);
if (deviceProperties != nullptr) {
message = QString("%1 8020(A): Last service: %2-%3, runtime since "
"last service: %4h.")
.arg(message,
QString::number(deviceProperties->last_service_year),
QString::number(deviceProperties->last_service_month),
QString::number(size_t(
deviceProperties->run_time_since_last_service_hours)));
p8020_device_properties_free(deviceProperties);
}
}
statusBar()->showMessage(message);
}
void
MainWindow::doTestCompleted()
{
mUI->testControlGroupBox->setEnabled(true);
}
MainWindow::MainWindow(const QString& aDevice, QWidget* const parent)
: QMainWindow(parent)
, mUI(new Ui::MainWindow)
, mModel(new ExercisesModel)
, mProtocolsModel(new ProtocolsModel)
, mDevice(nullptr)
, mWorkerThread(new QThread)
{
mUI->setupUi(this);
setWindowState(Qt::WindowMaximized);
// There doesn't appear to be away to set this in xml layout.
mUI->mainAreaSplitter->setSizes({400, 1000});
mUI->protocolSelector->setModel(mProtocolsModel.get());
// https://github.com/ahunt/incolata/issues/14 - Qt makes the dropdown too
// narrow, the workaround is to set a min width. 800 was picked arbitrarily,
// a dynamic calculation might be better but this works well enough for me.
mUI->protocolSelector->view()->setMinimumWidth(800);
mUI->exercisesList->setItemDelegate(new ExerciseRowDelegate(this));
mUI->exercisesList->setModel(mModel.get());
mModel->setCurrentExerciseLabel(mUI->currentExerciseLabel);
mModel->setFailLabel(mUI->failLabel);
mUI->rawChartView->setTitle("Raw Particle Conc. (#/cm³)");
mUI->rawChartView->setXRange(sRawSampleRange);
mUI->rawChartView->enableFixedXAxis();
mUI->rawChartView->xAxis()->setTickInterval(30);
mUI->rawChartView->yAxisLeft()->setRange(0, 2000);
mUI->rawChartView->yAxisRight()->setRange(0, 20);
mUI->ambientSampleGraph->setTitle("Ambient Particle Conc. (#/cm³)");
mUI->ambientSampleGraph->setXRange(sAmbientSampleRange);
mUI->ambientSampleGraph->setYAxisScalingMode(YAxisScalingMode::Floating);
mUI->ambientSampleGraph->yAxisLeft()->setRange(1000, 10000);
mUI->ambientSampleGraph->yAxisRight()->setRange(1000, 10000);
mUI->specimenSampleGraph->setTitle("Specimen Particle Conc. (#/cm³)");
mUI->specimenSampleGraph->setXRange(sSpecimenSampleRange);
mUI->specimenSampleGraph->yAxisLeft()->setRange(0, 20);
mUI->specimenSampleGraph->yAxisRight()->setRange(0, 2);
mUI->liveFFGraph->setTitle("Live Fit Factor");
mUI->liveFFGraph->enableLogYAxis();
// Intentionally use the same range as for specimen samples - the two graphs
// should mirror each other:
mUI->liveFFGraph->setXRange(sSpecimenSampleRange);
mUI->specimenIconLabel->setPixmap(
QIcon(":/specimen.svg").pixmap(QSize(32, 32)));
mUI->subjectIconLabel->setPixmap(
QIcon(":/subject.svg").pixmap(QSize(32, 32)));
// The QComboBox.placeHolder is ignored for editable QComboBoxes - the docs
// explicitly state that you need to this instead...
mUI->subjectSelector->lineEdit()->setPlaceholderText("Subject");
mUI->specimenSelector->lineEdit()->setPlaceholderText("Specimen");
refreshStatusBar();
QObject::connect(mUI->startTest1,
&QAbstractButton::pressed,
this,
&MainWindow::startTestPressed);
QObject::connect(mUI->startTest2,
&QAbstractButton::pressed,
this,
&MainWindow::startTestPressed);
QObject::connect(mUI->startTest3,
&QAbstractButton::pressed,
this,
&MainWindow::startTestPressed);
QObject::connect(mUI->startCustomProtocol,
&QAbstractButton::pressed,
this,
&MainWindow::startTestPressed);
QObject::connect(this,
&MainWindow::testStarted,
mModel.get(),
&ExercisesModel::doTestStarted);
QObject::connect(this,
&MainWindow::testCompleted,
mModel.get(),
&ExercisesModel::doTestCompleted);
QObject::connect(this,
&MainWindow::testCancelled,
mModel.get(),
&ExercisesModel::doTestCancelled);
QObject::connect(this,
&MainWindow::exerciseChanged,
mModel.get(),
&ExercisesModel::updateCurrentExercise);
QObject::connect(
this, &MainWindow::ffUpdated, mModel.get(), &ExercisesModel::updateFF);
QObject::connect(this,
&MainWindow::receivedInterimFF,
mModel.get(),
&ExercisesModel::renderInterimFF);
QObject::connect(this,
&MainWindow::ffUpdated,
mUI->ffGraph,
&FitFactorChartView::addDatapoint);
QObject::connect(this,
&MainWindow::renderRawSample,
mUI->rawCountLCD,
QOverload<const QString&>::of(&QLCDNumber::display));
QObject::connect(mUI->specimenSelector,
&QComboBox::currentTextChanged,
this,
&MainWindow::subjectOrSpecimenEntryChanged);
QObject::connect(mUI->subjectSelector,
&QComboBox::currentTextChanged,
this,
&MainWindow::subjectOrSpecimenEntryChanged);
// Use signals+slots here as it deals with cross-thread dispatch for us.
QObject::connect(
this, &MainWindow::receivedRawSample, this, &MainWindow::processRawSample);
QObject::connect(
this, &MainWindow::receivedSample, this, &MainWindow::processSample);
QObject::connect(
this, &MainWindow::receivedLiveFF, this, &MainWindow::processLiveFF);
const std::string deviceStdString = aDevice.toStdString();
assert(deviceStdString.find_first_of('\0') == std::string::npos &&
"device name must not contain nulls");
mDevice =
p8020_device_connect(deviceStdString.c_str(), &device_callback, this);
auto testWorker = new TestWorker(mDevice);
testWorker->moveToThread(mWorkerThread);
connect(this, &MainWindow::triggerTest, testWorker, &TestWorker::runTest);
connect(
this, &MainWindow::testCompleted, this, &MainWindow::doTestCompleted);
mWorkerThread->start();
}
void
MainWindow::startTest(QSharedPointer<Protocol> protocol)
{
mUI->testControlGroupBox->setEnabled(false);
// TODO: move most of this into Protocol.
const TestConfig* config;
switch (protocol->tag) {
case Protocol::BUILTIN_CONFIG:
config = protocol->builtinConfig;
break;
case Protocol::BUILTIN_CONFIG_ID:
const std::string shortNameStdString =
protocol->builtinConfigID->toStdString();
assert(shortNameStdString.find_first_of('\0') == std::string::npos &&
"short name must not contain nulls");
config = p8020_test_config_builtin_get(shortNameStdString.c_str());
protocol.reset(new Protocol{
.tag = Protocol::BUILTIN_CONFIG,
.builtinConfig = config,
});
break;
}
const size_t exerciseCount = p8020_test_config_exercise_count(config);
QStringList exercises;
for (size_t i = 0; i < exerciseCount; i++) {
auto name = p8020_test_config_exercise_name(config, i);
exercises << name;
p8020_string_free(name);
}
mUI->ffGraph->wipeData();
mUI->ffGraph->setExerciseCount(exerciseCount);
mModel->setExercises(exercises);
mUI->ambientSampleGraph->wipeData();
mUI->specimenSampleGraph->wipeData();
mUI->liveFFGraph->wipeData();
qInfo() << "Starting test: " << protocol->id() << " (" << exercises.length()
<< " exercises)";
emit triggerTest(protocol,
&test_callback,
this,
mUI->specimenSelector->currentText(),
mUI->subjectSelector->currentText(),
mUI->commentEntry->text());
}
void
MainWindow::startTestPressed()
{
auto sndr(sender());
if (sndr == mUI->startTest1) {
startTest(QSharedPointer<Protocol>(new Protocol{
.tag = Protocol::BUILTIN_CONFIG_ID,
.builtinConfigID = new QString("osha_legacy"),
}));
} else if (sndr == mUI->startTest2) {
startTest(QSharedPointer<Protocol>(new Protocol{
.tag = Protocol::BUILTIN_CONFIG_ID,
.builtinConfigID = new QString("crash2.5"),
}));
} else if (sndr == mUI->startTest3) {
startTest(QSharedPointer<Protocol>(new Protocol{
.tag = Protocol::BUILTIN_CONFIG_ID,
.builtinConfigID = new QString("live_mode_1h"),
}));
} else if (sndr == mUI->startCustomProtocol) {
startTest(mProtocolsModel->protocol(mUI->protocolSelector->currentIndex()));
} else {
qWarning() << "Bad sender for startExercisesPressed()";
}
}
MainWindow::~MainWindow() {
// There is no special reason to connect these here... but there's no good
// reason to connect them earlier anyway (we may have crashed in the meantime
// ¯\_(ツ)_/¯.
connect(mWorkerThread, &QThread::finished, mWorkerThread, &QThread::deleteLater);
// TODO: make sure any ongoing work (in the worker) is interrupted too.
// That's not possible yet because tests are still synchronous.
mWorkerThread->quit();
}