atrooney-online-2/src/aoclocklabel.cpp
oldmud0 de3533fbf2 Rework timer and ping logic
The timer's time as received by the server is clarified to be the actual
numerical time, in milliseconds, to be shown on the clock.
2021-01-09 01:18:19 -06:00

60 lines
1.2 KiB
C++

#include "aoclocklabel.h"
AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {}
void AOClockLabel::start()
{
timer.start(1000 / 60, this);
}
void AOClockLabel::start(int msecs)
{
this->set(msecs);
this->start();
}
void AOClockLabel::set(int msecs, bool update_text)
{
target_time = QTime::currentTime().addMSecs(msecs);
if (update_text)
{
if (QTime::currentTime() >= target_time)
{
this->setText("00:00:00.000");
}
else
{
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
QString timestring = timeleft.toString("hh:mm:ss.zzz");
this->setText(timestring);
}
}
}
void AOClockLabel::pause()
{
timer.stop();
}
void AOClockLabel::stop()
{
this->setText("00:00:00.000");
timer.stop();
}
void AOClockLabel::timerEvent(QTimerEvent *event)
{
if (event->timerId() == timer.timerId()) {
if (QTime::currentTime() >= target_time)
{
this->stop();
return;
}
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
QString timestring = timeleft.toString("hh:mm:ss.zzz");
this->setText(timestring);
} else {
QWidget::timerEvent(event);
}
}