-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
executable file
·530 lines (478 loc) · 13.5 KB
/
timer.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Qt Timer application.
// Written by Russell Auld <[email protected]>
#include "timer.hxx"
#include "ui_timer.h"
#include "ui_newValueDialog.h"
#include <QDebug>
#include <QMessageBox>
Timer::Timer(QWidget *parent)
: QMainWindow(parent), ui(new Ui::Timer), newTimeDialogUi(new Ui::SetValueDialog), newNameDialogUi(new Ui::SetValueDialog)
{
ui->setupUi(this);
name = "Timer";
started = false;
off = false;
countUp = false;
showSecs = false;
showBar = true;
secs = 0;
minutes = 5;
hours = 0;
firstHide = false;
newTimeDialog = NULL;
newNameDialog = NULL;
timer = new QTimer(this);
timer->start(1000); // one second
connect( ui->startButton, SIGNAL( clicked() ), this, SLOT( start() ) );
connect( ui->stopButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
connect( ui->actionExit, SIGNAL(triggered()), qApp, SLOT( quit() ) );
connect( ui->actionShowSeconds, SIGNAL( toggled(bool) ), this, SLOT(showSeconds(bool) ) );
// Calling this function should cause the toggled() signal to be emitted:
connect( ui->actionShowProgressBar, SIGNAL( toggled(bool) ), this, SLOT( showProgressBar(bool) ) );
connect( ui->actionSetTime, SIGNAL( triggered() ), this, SLOT( showSetTimeDialog() ) );
connect( ui->actionSetName, SIGNAL( triggered() ), this, SLOT( showSetNameDialog() ) );
connect( ui->actionCountUp, SIGNAL( toggled(bool) ), this, SLOT( setCountUp(bool) ) );
signalMapper = new QSignalMapper(this);
connect( ui->hUpButton, SIGNAL( clicked() ), signalMapper, SLOT( map() ) );
connect( ui->mUpButton, SIGNAL( clicked() ), signalMapper, SLOT( map() ) );
connect( ui->sUpButton, SIGNAL( clicked() ), signalMapper, SLOT( map() ) );
connect( ui->hDownButton, SIGNAL( clicked() ), signalMapper, SLOT( map() ) );
connect( ui->mDownButton, SIGNAL( clicked() ), signalMapper, SLOT( map() ) );
connect( ui->sDownButton, SIGNAL( clicked() ), signalMapper, SLOT( map() ) );
signalMapper->setMapping(ui->hUpButton, 0);
signalMapper->setMapping(ui->mUpButton, 1);
signalMapper->setMapping(ui->sUpButton, 2);
signalMapper->setMapping(ui->hDownButton, 3);
signalMapper->setMapping(ui->mDownButton, 4);
signalMapper->setMapping(ui->sDownButton, 5);
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(timeCtrlClicked(int)));
connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT( setTime(const QString &) ));
trayMenu = new QMenu(this);
fiveMinAction = new QAction("5:00",this);
connect(fiveMinAction, SIGNAL(triggered()), signalMapper, SLOT( map() ) );
signalMapper->setMapping(fiveMinAction, "5:00");
trayMenu->addAction(fiveMinAction);
tenMinAction = new QAction("10:00",this);
connect(tenMinAction, SIGNAL( triggered() ), signalMapper, SLOT( map() ) );
signalMapper->setMapping(tenMinAction, "10:00");
trayMenu->addAction(tenMinAction);
fifteenMinAction = new QAction("15:00",this);
connect(fifteenMinAction, SIGNAL( triggered() ), signalMapper, SLOT( map() ) );
signalMapper->setMapping(fifteenMinAction, "15:00");
trayMenu->addAction(fifteenMinAction);
halfHourAction = new QAction("30:00",this);
connect(halfHourAction, SIGNAL( triggered() ), signalMapper, SLOT( map() ) );
signalMapper->setMapping(halfHourAction, "30:00");
trayMenu->addAction(halfHourAction);
oneHourAction = new QAction("1:00:00",this);
connect(oneHourAction, SIGNAL( triggered() ), signalMapper, SLOT( map() ) );
signalMapper->setMapping(oneHourAction, "1:00:00");
trayMenu->addAction(oneHourAction);
trayMenu->addSeparator();
trayMenu->addAction(ui->actionSetName);
trayMenu->setDefaultAction(ui->actionSetName);
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayMenu->addAction(quitAction);
redIcon = new QIcon(":/Classic-Timer-Red-icon_32x32.png");
greyIcon = new QIcon(":/Classic-Timer-icon_32x32.png");
trayIcon = new QSystemTrayIcon(*greyIcon,this);
trayIcon->setContextMenu(trayMenu);
connect(trayIcon, SIGNAL( activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
connect(trayIcon, SIGNAL( messageClicked() ), this, SLOT( messageClicked() ) );
trayIcon->show();
ui->actionShowSeconds->setChecked(showSecs);
ui->actionShowProgressBar->setChecked(showBar);
//showTime(false);
}
Timer::~Timer()
{
delete ui;
delete timer;
delete signalMapper;
delete trayMenu;
delete trayIcon;
delete redIcon;
delete greyIcon;
delete fiveMinAction;
delete tenMinAction;
delete fifteenMinAction;
delete halfHourAction;
delete oneHourAction;
delete quitAction;
delete newTimeDialog;
delete newNameDialog;
}
void Timer::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
// QMessageBox::information(this, tr("Systray"),
// tr("The program will keep running in the "
// "system tray. To terminate the program, "
// "choose <b>Quit</b> in the context menu "
// "of the system tray entry."));
if (this->isVisible()) {
hide();
if (!firstHide) {
trayIcon->showMessage(tr("Timer is still running"),tr("Choose Quit to terminte the program"), QSystemTrayIcon::NoIcon);
firstHide = true;
}
event->ignore();
}
}
}
void Timer::start()
{
if (started) return;
if (hours == 0 && minutes == 0 && secs == 0) {
countUp=true;
ui->actionCountUp->setChecked(countUp);
}
// else{
// countUp = false;
// }
started = true;
// disable gui elements:
ui->startButton->setEnabled(false);
ui->hUpButton->setEnabled(false);
ui->mUpButton->setEnabled(false);
ui->sUpButton->setEnabled(false);
ui->hDownButton->setEnabled(false);
ui->mDownButton->setEnabled(false);
ui->sDownButton->setEnabled(false);
connect(timer, SIGNAL( timeout() ), this, SLOT( updateTime() ) );
//connect(timer, SIGNAL( timeout() ), this, SLOT( showTimeWithToggle() ) );
off = true;
//showTime(true);
trayIcon->setIcon(*greyIcon);
}
void Timer::stop()
{
// Dual function button. If already stopped, reset the timer value:
if (!started) {
//reset:
hours = 0;
minutes = 0;
secs = 0;
//off = false;
showTime(false);
trayIcon->setIcon(*greyIcon);
return;
}
started = false;
//disconnect(countdownTimer, SIGNAL( timeout() ), this, SLOT( updateTime() ) );
disconnect(timer, SIGNAL( timeout() ), this, SLOT( updateTime() ) );
//disconnect(timer, SIGNAL( timeout() ), this, SLOT( showTimeWithToggle() ) );
//off = false;
//showTime(false);
ui->startButton->setEnabled(true);
ui->hUpButton->setEnabled(true);
ui->mUpButton->setEnabled(true);
ui->sUpButton->setEnabled(true);
ui->hDownButton->setEnabled(true);
ui->mDownButton->setEnabled(true);
ui->sDownButton->setEnabled(true);
}
void Timer::updateTime()
{
bool done=false;
if (countUp) {
secs++;
if (secs > 59 ) {
secs = 0;
minutes++;
}
if (minutes > 59) {
minutes = 0;
hours++;
}
// if (hours > 23) {
// hours = 0;
// }
}
else{
// Count down:
secs--;
if (hours == 0 && minutes == 0 && secs == 0) {
stop();
done=true;
}
if (secs < 0 ) {
secs = 59;
minutes--;
}
if (minutes < 0) {
minutes = 59;
hours--;
}
}
showTime(true);
if (done) timesUp();
}
void Timer::showTime(bool toggle_colon)
{
QString time;
if (showSecs) {
//digits = 8;
ui->lcdNumber->setDigitCount(8);
//ui->lcdNumber->display(QString("0:05:00"));
time = QString("%1:%2:%3").arg(hours,2).arg(minutes,2,10,QChar('0')).arg(secs,2,10,QChar('0'));
// if (toggle_colon) {
// if (off) {
// time[2]=' ';
// time[5]=' ';
// off = false;
// }else{
// off = true;
// }
// }
}else{
//digits = 5;
ui->lcdNumber->setDigitCount(5);
time = QString("%1:%2").arg(hours,2).arg(minutes,2,10,QChar('0'));
// if (toggle_colon) {
// if (off) {
// time[2]=' ';
// off = false;
// }else{
// off = true;
// }
// }
}
ui->lcdNumber->display(time);
trayIcon->setToolTip(name+": "+time);
ui->progressBar->setValue(secs);
}
void Timer::timesUp()
{
if (isVisible()) {
QMessageBox::information(this,"Time's up!", "Hey! Time's Up!");
} else {
trayIcon->showMessage("Hey! Time's up!", "The timer has expired", QSystemTrayIcon::Critical, 60000);
}
trayIcon->setIcon(*redIcon);
}
void Timer::timeCtrlClicked(int b)
{
if (started) return;
switch (b) {
case 0:
hours++;
//if (hours > 23) hours=0; // let's be honest here
break;
case 1:
minutes++;
if (minutes > 59) minutes=0;
break;
case 2:
secs++;
if (secs > 59) secs=0;
break;
case 3:
hours--;
if (hours < 0) hours=0;
break;
case 4:
minutes--;
if (minutes < 0) minutes=0;
break;
case 5:
secs--;
if (secs < 0) secs=0;
break;
//default:
}
//off=false;
showTime(false);
}
void Timer::showSeconds(bool val)
{
showSecs = val;
if (val) {
ui->sUpButton->show();
ui->sDownButton->show();
}
else{
ui->sUpButton->hide();
ui->sDownButton->hide();
}
showTime(started);
}
void Timer::showProgressBar(bool val)
{
showBar = val;
if (val) {
ui->progressBar->show();
}else{
ui->progressBar->hide();
}
showTime(started);
}
void Timer::showSetTimeDialog()
{
if (!newTimeDialog) {
newTimeDialog = new QDialog(this);
newTimeDialogUi->setupUi(newTimeDialog);
connect(newTimeDialog, SIGNAL( accepted() ), this, SLOT(setTimeFromDialog()) );
newTimeDialogUi->lineEdit->setWhatsThis(tr("Enter new time value"));
}
newTimeDialogUi->lineEdit->setFocus(Qt::PopupFocusReason);
newTimeDialog->exec();
}
void Timer::showSetNameDialog()
{
if (!newNameDialog) {
newNameDialog = new QDialog(this);
newNameDialogUi->setupUi(newNameDialog);
connect(newNameDialog, SIGNAL( accepted() ), this, SLOT(setNameFromDialog()) );
newNameDialogUi->lineEdit->setWhatsThis(tr("Enter new name"));
}
newNameDialogUi->lineEdit->setText(name);
newNameDialogUi->lineEdit->selectAll();
newNameDialogUi->lineEdit->setFocus(Qt::PopupFocusReason);
newNameDialog->exec();
}
void Timer::setTimeFromDialog()
{
setTime(newTimeDialogUi->lineEdit->text());
}
void Timer::setNameFromDialog()
{
setName(newNameDialogUi->lineEdit->text());
}
void Timer::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
QString time;
switch (reason) {
case QSystemTrayIcon::Context:
// the context menu shows normally under this "reason"
//trayIcon->showMessage("Title", "Context");
break;
// Left click
case QSystemTrayIcon::Trigger:
// if (showSecs)
// time = QString("%1:%2:%3").arg(hours,2).arg(minutes,2,10,QChar('0')).arg(secs,2,10,QChar('0'));
// else
// time = QString("%1:%2").arg(hours,2).arg(minutes,2,10,QChar('0'));
// trayIcon->showMessage("Timer", time);
if (isHidden()) {
show();
}else{
hide();
}
break;
case QSystemTrayIcon::DoubleClick:
//trayIcon->showMessage("Title", "DoubleClick");
break;
case QSystemTrayIcon::MiddleClick:
//trayIcon->showMessage("Title", "MiddleClick");
break;
default:
;
}
}
void Timer::setTime(const QString &val)
{
// validate the input value using a regex.
// This will match anything other than numbers and colons
QRegExp rx("[^\\d:]");
// If no match, the return value will be -1
if (rx.indexIn(val) != -1) {
QMessageBox::warning(this,"Timer","Invalid value");
return;
}
if (started) {
if (! isVisible()) {
show();
}
return;
}
QStringList list = val.split(":");
switch(list.size()) {
case 1:
hours = 0;
minutes = list[0].toInt();
secs = 0;
break;
case 2:
hours = 0;
minutes = list[0].toInt();
secs = list[1].toInt();
break;
case 3:
hours = list[0].toInt();
minutes = list[1].toInt();
secs = list[2].toInt();
}
minutes += secs / 60;
secs %= 60;
hours += minutes / 60;
minutes %= 60;
// update display
showTime(false);
if (! isVisible()) {
//show();
// go ahead and start counting down if called from tray icon:
start();
}
}
void Timer::setName(const QString &val)
{
name = val;
setWindowTitle(val);
showTime(false);
}
void Timer::messageClicked()
{
trayIcon->setIcon(*greyIcon);
}
void Timer::setCountUp(bool b)
{
countUp = b;
}
// void Timer::resetHourSlider()
// {
// ui->hourSlider->setValue(0);
// }
// void Timer::resetMinuteSlider()
// {
// ui->minuteSlider->setValue(0);
// }
// void Timer::hourSliderMoved(int val)
// {
// hours += val;
// off=false;
// showTime();
// }
// void Timer::on_pushButton_clicked()
// {
// if (!started) {
// randomStopTime();
// time->start();
// connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
// started = true;
// ui->pushButton->setText("Stop");
// return;
// }
// else{
// disconnect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
// started = false;
// ui->pushButton->setText("Start");
// checkResult();
// }
// }
// void Timer::randomStopTime()
// {
// targetSecs = rand() % 10;
// targetKsecs = (rand() % 1000) / 10;
// ui->targetNumber->display(QString("%1:%2").arg(targetSecs,2).arg(targetKsecs,2));
// //qDebug() << targetSecs << targetKsecs;
// }
// void Timer::checkResult()
// {
// if (stopSecs == targetSecs && abs(targetKsecs - stopKsecs) < 10) {
// QMessageBox::information(this, "STP", "Winner!");
// }else{
// QMessageBox::information(this, "STP", "meh");
// }
// }