-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimerControlBox.cpp
420 lines (356 loc) · 12.2 KB
/
TimerControlBox.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (c) 2019 Stephen Williams ([email protected])
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
# include "TimerControlBox.h"
# include "ui_TimerControlBox.h"
# include "TimerMain.h"
# include <QtMultimedia/QSound>
# include <cstdio>
# include <string>
# include <cassert>
using namespace std;
TimerControlBox::TimerControlBox(QWidget*parent)
: QMainWindow(parent), timer_window_(0)
{
ui = new Ui::TimerControlBox;
ui->setupUi(this);
sound_callup_ = new QSound("sounds/whistle-2.wav");
sound_start_ = new QSound("sounds/whistle-1.wav");
sound_clear_ = new QSound("sounds/whistle-3.wav");
sound_emergency_stop_ = new QSound("sounds/emergency-stop.wav");
timer_callup_ = 0;
timer_end_ = 0;
timer_end_warn_ = 0;
timer_running_flag_ = false;
timer_next_line_ = false;
// The timer...
connect(&timer_,
SIGNAL(timeout()),
SLOT(timer_timeout()));
// The control box buttons...
connect(ui->go_button,
SIGNAL(clicked()),
SLOT(go_button()));
connect(ui->prev_button,
SIGNAL(clicked()),
SLOT(prev_button()));
connect(ui->next_button,
SIGNAL(clicked()),
SLOT(next_button()));
connect(ui->line_cd_check,
SIGNAL(stateChanged(int)),
SLOT(line_cd_check(int)));
connect(ui->reset_ends_button,
SIGNAL(clicked()),
SLOT(reset_ends_button()));
// Cannot go until an end is set up.
ui->go_button->setEnabled(false);
}
TimerControlBox::~TimerControlBox()
{
delete sound_clear_;
delete sound_start_;
delete sound_callup_;
delete sound_emergency_stop_;
delete ui;
}
void TimerControlBox::set_timer_window(TimerMain*timer)
{
assert(timer_window_ == 0);
timer_window_ = timer;
}
int TimerControlBox::special_end_command(const QString&label_txt)
{
if (timer_running_flag_) {
return -1;
}
timer_window_->set_line_text(label_txt);
timer_callup_ = ui->callup_time_text->text().toInt();
timer_end_ = ui->end_time_text->text().toInt() + 1;
timer_end_warn_ = ui->warn_time_text->text().toInt();
timer_next_line_ = 0;
timer_window_->set_time_value(timer_callup_, TimerMain::TIMER_CALLUP);
ui->go_button->setEnabled(true);
return 0;
}
/*
* Advance to the specified end. Set the displays for the end, select
* the starting line, and prepare for everything to start
* happening. Return the number for the next end, or <0 for an error.
*
* If the timer is still running for the current end, then return an
* error.
*/
int TimerControlBox::next_end_command(int end_number, bool practice_flag)
{
if (timer_running_flag_) {
return -1;
}
// Count and display the end number.
bool toggle_order_flag = ui->toggle_order_check->checkState()? true : false;
timer_window_->set_end_number(end_number, practice_flag);
// This is true if there are multiple lines.
bool dual_line_flag = ui->line_cd_check->checkState()? true : false;
if (dual_line_flag && toggle_order_flag && (end_number%2 == 0)) {
// If there are 2 lines, and this is an even end, then
// the cd line is first up, followed by the ab line.
timer_window_->set_line_text(ui->line_cd_text->text());
timer_next_line_txt_ = ui->line_ab_text->text();
} else {
// Starting with the ab line, set the line in the display,
// and stash the cd line name for later use.
timer_window_->set_line_text(ui->line_ab_text->text());
timer_next_line_txt_ = ui->line_cd_text->text();
}
// Load the state machine with the phase times.
timer_callup_ = ui->callup_time_text->text().toInt();
timer_end_ = ui->end_time_text->text().toInt() + 1;
timer_end_warn_ = ui->warn_time_text->text().toInt();
timer_next_line_ = dual_line_flag? timer_end_ : 0;
// State the callup time in the timer display, and enable the
// GO button.
timer_window_->set_time_value(timer_callup_, TimerMain::TIMER_PREP);
ui->go_button->setEnabled(true);
// Return the number for the next end.
return end_number + 1;
}
int TimerControlBox::next_end_command(void)
{
int end_counter = ui->end_count_box->value();
bool practice_flag = ui->practice_check->checkState()? true : false;
int next_end = next_end_command(end_counter, practice_flag);
if (next_end >= 0) ui->end_count_box->setValue(next_end);
return next_end;
}
int TimerControlBox::start_timer_command(void)
{
// If the timer is already running, then do nothing.
if (timer_running_flag_) return -1;
set_running_state_();
ui->go_button->setText("PAUSE");
timer_.start(1000);
// start with phase appropriate sound
if (timer_callup_ >= 1) {
// in callup phase, resume with callup signal
// restore numeric value and background colour (may have been emergency stopped)
timer_window_->set_time_value(timer_callup_, TimerMain::TIMER_CALLUP);
sound_callup_->play();
} else if (timer_end_ >= 1) {
// in the middle of an end, resume with start signal
// restore numeric value and background colour (may have been emergency stopped)
// Don't bother detecting whether we're in the 'Warning' phase: it'll fix itself in 1 second
timer_window_->set_time_value(timer_end_, TimerMain::TIMER_END);
sound_start_->play();
}
return 0;
}
int TimerControlBox::emergency_stop_command(void)
{
// If the timer is not running, then do nothing.
if (!timer_running_flag_) return -1;
// play the emergency stop sound, set the display to stop
timer_window_->set_time_value(0, TimerMain::TIMER_STOP);
sound_emergency_stop_->play();
// pause the timer
pause_timer_command();
return 0;
}
int TimerControlBox::fast_forward_command(void)
{
// If the timer is not running, then do nothing.
if (!timer_running_flag_) return -1;
if (timer_callup_ >= 1) {
timer_callup_ = 1;
} else if (timer_end_ >= 1) {
timer_end_ = 1;
} else {
return -1;
}
timer_timeout();
return 0;
}
int TimerControlBox::pause_timer_command(void)
{
// If the timer is not running, then do nothing.
if (!timer_running_flag_) return -1;
// Stop the timer, and enable the "RUN" button.
timer_.stop();
set_paused_state_();
ui->go_button->setText("RUN");
return 0;
}
int TimerControlBox::query_settings_command(QString&text)
{
int callup_time = ui->callup_time_text->text().toInt();
int end_time = ui->end_time_text ->text().toInt();
int warn_time = ui->warn_time_text ->text().toInt();
string line_two_enabled;
if (ui->line_cd_check->isChecked()){
line_two_enabled = "true";
} else {
line_two_enabled = "false";
}
string toggle_lines;
if (ui->toggle_order_check->isChecked()){
toggle_lines = "true";
} else {
toggle_lines = "false";
}
text = QString::asprintf("callup-time=%d end-time=%d warn-time=%d line-one=%s line-two=%s line-two-enabled=%s toggle-lines=%s",
callup_time, end_time, warn_time, ui->line_ab_text->text().toUtf8().constData(), ui->line_cd_text->text().toUtf8().constData(),
line_two_enabled.c_str(), toggle_lines.c_str());
return 0;
}
int TimerControlBox::set_command(const QString&text)
{
QStringList pair = text.split('=');
if (pair.size() != 2) return -1;
if (pair[0] == "callup-time") {
int val = pair[1].toInt();
if (val == 0) return 0;
QString txt;
txt.setNum(val);
ui->callup_time_text->setText(txt);
} else if (pair[0] == "end-time") {
int val = pair[1].toInt();
if (val == 0) return 0;
QString txt;
txt.setNum(val);
ui->end_time_text->setText(txt);
} else if (pair[0] == "warn-time") {
int val = pair[1].toInt();
if (val == 0) return 0;
QString txt;
txt.setNum(val);
ui->warn_time_text->setText(txt);
} else if (pair[0] == "line-one") {
ui->line_ab_text->setText(pair[1]);
} else if (pair[0] == "line-two-enabled") {
ui->line_cd_check->setChecked(QVariant(pair[1]).toBool());
} else if (pair[0] == "line-two") {
ui->line_cd_text->setText(pair[1]);
} else if (pair[0] == "toggle-lines") {
ui->toggle_order_check->setChecked(QVariant(pair[1]).toBool());
}
return 0;
}
/*
* The remote control sends the toggle-fullscreen command. In
* response, toggle fullscreen mode. If that causes the display to not
* be fullscreen, then also raise the control box.
*/
int TimerControlBox::toggle_fullscreen_command(void)
{
bool flag = timer_window_->toggle_fullscreen();
if (!flag) this->show();
return flag? 1 : 0;
}
void TimerControlBox::network_service_port(const QString&port_text)
{
ui->remote_port_text->setText(port_text);
if (timer_window_) timer_window_->network_service_port(port_text);
}
void TimerControlBox::network_interfaces(const QStringList&if_list)
{
ui->ip_interface_list->clear();
ui->ip_interface_list->addItems(if_list);
if (timer_window_) timer_window_->network_interfaces(if_list);
}
void TimerControlBox::set_running_state_(void)
{
timer_running_flag_ = true;
ui->prev_button->setEnabled(false);
ui->next_button->setEnabled(false);
ui->reset_ends_button->setEnabled(false);
}
void TimerControlBox::set_paused_state_(void)
{
ui->prev_button->setEnabled(true);
ui->next_button->setEnabled(true);
ui->reset_ends_button->setEnabled(true);
timer_running_flag_ = false;
}
void TimerControlBox::line_cd_check(int state)
{
if (state) {
ui->line_cd_text->setEnabled(true);
} else {
ui->line_cd_text->setEnabled(false);
}
}
void TimerControlBox::go_button(void)
{
if (timer_running_flag_) {
pause_timer_command();
} else {
start_timer_command();
}
}
void TimerControlBox::prev_button(void)
{
fprintf(stdout, "XXXX NOT IMPLEMENTED\n");
fflush(stdout);
}
void TimerControlBox::next_button(void)
{
next_end_command();
}
void TimerControlBox::reset_ends_button(void)
{
ui->end_count_box->setValue(1);
}
void TimerControlBox::timer_timeout(void)
{
if (timer_callup_ > 0) {
// Still calling archers to the line.
timer_callup_ -= 1;
timer_window_->set_time_value(timer_callup_, TimerMain::TIMER_CALLUP);
// If time's up for calling archers to the line, then
// signal line's hot.
if (timer_callup_ == 0) {
sound_start_->play();
}
} else if (timer_end_ > 0) {
// Counting down the end.
timer_end_ -= 1;
if (timer_end_ > timer_end_warn_)
timer_window_->set_time_value(timer_end_, TimerMain::TIMER_END);
else
timer_window_->set_time_value(timer_end_, TimerMain::TIMER_END_WARN);
// If time's up...
if (timer_end_ == 0) {
if (timer_next_line_) {
// If the other line has to shoot, reset the
// state machine, set the new line text, and restar.
timer_next_line_ = false;
timer_callup_ = ui->callup_time_text->text().toInt();
timer_end_ = ui->end_time_text->text().toInt() + 1;
timer_window_->set_line_text(timer_next_line_txt_);
sound_callup_->play();
} else {
sound_clear_->play();
}
}
} else {
// All the steps ran out, so stop the timer by clicking
// the go_button toggle switch.
timer_window_->set_time_value(0, TimerMain::TIMER_STOP);
pause_timer_command();
ui->go_button->setEnabled(false);
}
}