77#include < utility>
88#include < vector>
99
10+ #include < QCheckBox>
1011#include < QComboBox>
1112#include < QDialog>
1213#include < QDialogButtonBox>
@@ -172,7 +173,11 @@ QString severityLabel(safecrowd::domain::ScenarioElementSeverity severity) {
172173}
173174
174175QString hazardScheduleSummary (const safecrowd::domain::EnvironmentHazardDraft& hazard) {
175- return QString (" %1s - %2s" ).arg (hazard.startSeconds , 0 , ' f' , 1 ).arg (hazard.endSeconds , 0 , ' f' , 1 );
176+ const auto start = std::max (0.0 , hazard.startSeconds );
177+ if (safecrowd::domain::environmentHazardHasOpenEndedSchedule (hazard)) {
178+ return QString (" %1s - open" ).arg (start, 0 , ' f' , 1 );
179+ }
180+ return QString (" %1s - %2s" ).arg (start, 0 , ' f' , 1 ).arg (std::max (start, hazard.endSeconds ), 0 , ' f' , 1 );
176181}
177182
178183QString hazardZoneSummary (
@@ -245,13 +250,35 @@ bool editEnvironmentHazard(
245250 startSpin->setRange (0.0 , 86400.0 );
246251 startSpin->setDecimals (1 );
247252 startSpin->setSuffix (" s" );
248- startSpin->setValue (std::max (0.0 , hazard->startSeconds ));
253+ const auto initialStartSeconds = std::max (0.0 , hazard->startSeconds );
254+ const auto initialOpenEnded = safecrowd::domain::environmentHazardHasOpenEndedSchedule (*hazard);
255+ startSpin->setValue (initialStartSeconds);
249256
250257 auto * endSpin = new QDoubleSpinBox (&dialog);
251258 endSpin->setRange (0.0 , 86400.0 );
252259 endSpin->setDecimals (1 );
253260 endSpin->setSuffix (" s" );
254- endSpin->setValue (std::max (0.0 , hazard->endSeconds ));
261+ endSpin->setValue (initialOpenEnded
262+ ? initialStartSeconds
263+ : std::max (initialStartSeconds, hazard->endSeconds ));
264+
265+ auto * openEndedCheck = new QCheckBox (" Open ended" , &dialog);
266+ openEndedCheck->setChecked (initialOpenEnded);
267+ endSpin->setEnabled (!initialOpenEnded);
268+
269+ QObject::connect (openEndedCheck, &QCheckBox::toggled, &dialog, [=](bool checked) {
270+ endSpin->setEnabled (!checked);
271+ if (checked) {
272+ endSpin->setValue (startSpin->value ());
273+ } else if (endSpin->value () <= startSpin->value ()) {
274+ endSpin->setValue (std::min (86400.0 , startSpin->value () + 60.0 ));
275+ }
276+ });
277+ QObject::connect (startSpin, qOverload<double >(&QDoubleSpinBox::valueChanged), &dialog, [=](double value) {
278+ if (openEndedCheck->isChecked ()) {
279+ endSpin->setValue (value);
280+ }
281+ });
255282
256283 auto * severityCombo = new QComboBox (&dialog);
257284 severityCombo->addItem (" Low" , static_cast <int >(safecrowd::domain::ScenarioElementSeverity::Low));
@@ -270,6 +297,7 @@ bool editEnvironmentHazard(
270297 form->addRow (" Y" , ySpin);
271298 form->addRow (" Start" , startSpin);
272299 form->addRow (" End" , endSpin);
300+ form->addRow (" " , openEndedCheck);
273301 form->addRow (" Severity" , severityCombo);
274302 form->addRow (" Note" , noteEdit);
275303 root->addLayout (form);
@@ -301,6 +329,11 @@ bool editEnvironmentHazard(
301329 xSpin->setFocus ();
302330 return ;
303331 }
332+ if (!openEndedCheck->isChecked () && endSpin->value () <= startSpin->value ()) {
333+ QMessageBox::warning (&dialog, " Edit hazard" , " Set the end time after the start time." );
334+ endSpin->setFocus ();
335+ return ;
336+ }
304337
305338 dialog.accept ();
306339 });
@@ -332,14 +365,18 @@ bool editEnvironmentHazard(
332365 QMessageBox::warning (parent, " Edit hazard" , " The hazard location must stay inside the affected zone." );
333366 return false ;
334367 }
368+ if (!openEndedCheck->isChecked () && endSpin->value () <= startSpin->value ()) {
369+ QMessageBox::warning (parent, " Edit hazard" , " Set the end time after the start time." );
370+ return false ;
371+ }
335372
336373 hazard->kind = static_cast <safecrowd::domain::EnvironmentHazardKind>(kindCombo->currentData ().toInt ());
337374 hazard->name = name.toStdString ();
338375 hazard->affectedZoneId = selectedZoneId;
339376 hazard->floorId = selectedZone->floorId ;
340377 hazard->position = selectedPosition;
341378 hazard->startSeconds = startSpin->value ();
342- hazard->endSeconds = std::max ( hazard->startSeconds , endSpin->value () );
379+ hazard->endSeconds = openEndedCheck-> isChecked () ? hazard->startSeconds : endSpin->value ();
343380 hazard->severity = static_cast <safecrowd::domain::ScenarioElementSeverity>(severityCombo->currentData ().toInt ());
344381 hazard->note = noteEdit->toPlainText ().trimmed ().toStdString ();
345382 return true ;
0 commit comments