Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee3bad4

Browse files
Implement clock pausing
Implement clock setting w/o starting or stopping Both of these should make it possible for the server to start/stop/pause/resume the clock with perfect synchronization to the true time.
1 parent 7e9c572 commit ee3bad4

5 files changed

Lines changed: 65 additions & 26 deletions

File tree

include/aoclocklabel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class AOClockLabel : public QLabel {
1414
AOClockLabel(QWidget *parent);
1515
void start();
1616
void start(int msecs);
17+
void set(int msecs, bool update_text=false);
1718
void pause();
18-
void resume();
1919
void stop();
2020

2121
protected:

include/courtroom.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,11 @@ class Courtroom : public QMainWindow {
251251

252252
void check_connection_received();
253253

254+
void start_clock();
254255
void start_clock(qint64 msecs);
255256

257+
void set_clock(qint64 msecs);
258+
256259
void stop_clock();
257260

258261
qint64 get_ping() { return ping_timer.elapsed(); }

src/aoclocklabel.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,35 @@ AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {}
44

55
void AOClockLabel::start()
66
{
7-
this->resume();
7+
timer.start(100, this);
88
}
99

1010
void AOClockLabel::start(int msecs)
11+
{
12+
this->set(msecs);
13+
this->start();
14+
}
15+
16+
void AOClockLabel::set(int msecs, bool update_text)
1117
{
1218
QTime time = QTime::currentTime();
1319
if (msecs > time.msec())
1420
{
1521
target_time = time.addMSecs(msecs);
16-
timer.start(100, this);
1722
}
18-
}
19-
20-
void AOClockLabel::pause()
21-
{
22-
timer.stop();
23-
}
24-
25-
void AOClockLabel::resume()
26-
{
27-
timer.start(100, this);
23+
if (update_text)
24+
{
25+
if (QTime::currentTime() >= target_time)
26+
{
27+
this->setText("00:00:00.000");
28+
}
29+
else
30+
{
31+
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
32+
QString timestring = timeleft.toString("hh:mm:ss.zzz");
33+
this->setText(timestring);
34+
}
35+
}
2836
}
2937

3038
void AOClockLabel::stop()
@@ -35,16 +43,16 @@ void AOClockLabel::stop()
3543

3644
void AOClockLabel::timerEvent(QTimerEvent *event)
3745
{
38-
if (event->timerId() == timer.timerId()) {
39-
if (QTime::currentTime() >= target_time)
40-
{
41-
this->stop();
42-
return;
43-
}
44-
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
45-
QString timestring = timeleft.toString("hh:mm:ss.zzz");
46-
this->setText(timestring);
47-
} else {
48-
QWidget::timerEvent(event);
46+
if (event->timerId() == timer.timerId()) {
47+
if (QTime::currentTime() >= target_time)
48+
{
49+
this->stop();
50+
return;
4951
}
52+
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
53+
QString timestring = timeleft.toString("hh:mm:ss.zzz");
54+
this->setText(timestring);
55+
} else {
56+
QWidget::timerEvent(event);
57+
}
5058
}

src/courtroom.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4715,11 +4715,26 @@ void Courtroom::announce_case(QString title, bool def, bool pro, bool jud,
47154715
}
47164716
}
47174717

4718+
void Courtroom::start_clock()
4719+
{
4720+
ui_clock->start();
4721+
}
4722+
47184723
void Courtroom::start_clock(qint64 msecs)
47194724
{
47204725
ui_clock->start(static_cast<int>(msecs));
47214726
}
47224727

4728+
void Courtroom::set_clock(qint64 msecs)
4729+
{
4730+
ui_clock->set(static_cast<int>(msecs), true);
4731+
}
4732+
4733+
void Courtroom::pause_clock()
4734+
{
4735+
ui_clock->pause();
4736+
}
4737+
47234738
void Courtroom::stop_clock()
47244739
{
47254740
ui_clock->stop();

src/packet_distribution.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,12 +729,25 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
729729
f_contents.at(5) == "1");
730730
}
731731
else if (header == "TI") { // Timer packet
732-
if (courtroom_constructed && f_contents.size() >= 1) {
732+
if (courtroom_constructed && f_contents.size() > 0) {
733733
qint64 resolution = f_contents.at(0).toInt();
734+
//Type 0 = start/stop timer
735+
//Type 1 = pause timer
736+
int type = 0;
737+
if (f_contents.size() > 1)
738+
type = f_contents.at(1).toInt();
734739
qDebug() << "timer" << resolution << last_ping << resolution - last_ping;
735740
resolution = resolution - last_ping;
736741
if (resolution > 0)
737-
w_courtroom->start_clock(resolution);
742+
{
743+
if (type == 1)
744+
{
745+
w_courtroom->pause_clock();
746+
w_courtroom->set_clock(resolution);
747+
}
748+
else
749+
w_courtroom->start_clock(resolution);
750+
}
738751
else
739752
w_courtroom->stop_clock();
740753
}

0 commit comments

Comments
 (0)