aboutsummaryrefslogtreecommitdiff
path: root/src/aoclocklabel.cpp
blob: afd4d1e31d72f9af246242262055021e6c32a1bd (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "aoclocklabel.h"

AOClockLabel::AOClockLabel(QWidget *parent)
    : QLabel(parent)
{}

void AOClockLabel::start()
{
  m_timer.start(1000 / 60, this);
}

void AOClockLabel::start(qint64 msecs)
{
  this->set(msecs);
  this->start();
}

void AOClockLabel::set(qint64 msecs, bool update_text)
{
  m_target_time = QDateTime::currentDateTime().addMSecs(msecs);
  if (update_text)
  {
    if (QDateTime::currentDateTime() >= m_target_time)
    {
      this->setText("00:00:00.000");
    }
    else
    {
      qint64 ms_left = QDateTime::currentDateTime().msecsTo(m_target_time);
      QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
      QString timestring = timeleft.toString("hh:mm:ss.zzz");
      this->setText(timestring);
    }
  }
}

void AOClockLabel::pause()
{
  m_timer.stop();
}

void AOClockLabel::stop()
{
  this->setText("00:00:00.000");
  m_timer.stop();
}

void AOClockLabel::skip(qint64 msecs)
{
  qint64 ms_left = QDateTime::currentDateTime().msecsTo(m_target_time);
  this->set(ms_left - msecs, true);
}

bool AOClockLabel::active()
{
  return m_timer.isActive();
}

void AOClockLabel::timerEvent(QTimerEvent *event)
{
  if (event->timerId() == m_timer.timerId())
  {
    if (QDateTime::currentDateTime() >= m_target_time)
    {
      this->stop();
      return;
    }
    qint64 ms_left = QDateTime::currentDateTime().msecsTo(m_target_time);
    QTime timeleft = QTime(0, 0).addMSecs(ms_left % (1000 * 3600 * 24));
    QString timestring = timeleft.toString("hh:mm:ss.zzz");
    this->setText(timestring);
  }
  else
  {
    QWidget::timerEvent(event);
  }
}