aboutsummaryrefslogtreecommitdiff
path: root/src/aoclocklabel.cpp
blob: 4c9c48198c90a5b8feeb94e9768f2b15b79f594b (plain)
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
#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);
  }
}