FotoBox
countdown.cpp
Go to the documentation of this file.
1 /* countdown.cpp
2  *
3  * Copyright (c) 2019 Thomas Kais
4  *
5  * This file is subject to the terms and conditions defined in
6  * file 'COPYING', which is part of this source code package.
7  */
8 #include "countdown.h"
9 
10 namespace FotoBox {
11 
12 Countdown::Countdown(QObject *parent, const unsigned int i_seconds)
13  : QObject(parent)
14  , m_timer(this)
15  , m_startTime(i_seconds)
16  , m_timeLeft(i_seconds)
17 {
18  //set update intervall to one second
19  m_timer.setInterval(ONE_SECOND);
20 
21  //Every second it is checked if the countdown is elapsed
22  connect(&m_timer, &QTimer::timeout, this, &Countdown::updateTimeLeft);
23 }
24 
26 {
27  //this function always deals with the already expired second
28  //check if countdown isn't elapsed
29  if (m_timeLeft > 1) {
30  //reduce counter before sending the signal
31  --m_timeLeft;
32 
33  //send the signal that one second has passed
34  Q_EMIT update(m_timeLeft);
35 
36  //Because it isn't a single shot time we don't need to start it again
37  return;
38  }
39 
40  //countdown finished/elapsed
41  stop();
42  m_timeLeft = 0;
43  Q_EMIT elapsed();
44 }
45 
46 void Countdown::setStartTime(const unsigned int i_seconds)
47 {
48  m_startTime = i_seconds;
49  reset();
50 }
51 
52 auto Countdown::isActive() const -> bool
53 {
54  return m_isActive;
55 }
56 
57 auto Countdown::start() -> bool
58 {
59  //not running and start time set
60  if (!m_isActive && m_startTime > 0) {
61  //start countdown
62  m_timer.start();
63 
64  //check if really running
65  if (m_timer.isActive()) {
66  //countdown active
67  m_isActive = true;
68 
69  //inform whith current value
70  Q_EMIT update(m_timeLeft);
71  return true;
72  }
73  }
74 
75  return false;
76 }
77 
78 auto Countdown::stop() -> bool
79 {
80  //stop timer
81  m_timer.stop();
82  m_isActive = m_timer.isActive();
83 
84  //if isn't active it's stopped so negate bool
85  return !m_isActive;
86 }
87 
88 auto Countdown::reset() -> bool
89 {
90  //stop countdown
91  if (stop()) {
92  //reset it
93  m_timeLeft = m_startTime;
94  return true;
95  }
96 
97  return false;
98 }
99 
100 } // end namespace FotoBox
void setStartTime(unsigned int i_seconds)
Set the countdown start time.
Definition: countdown.cpp:46
static constexpr int ONE_SECOND
Definition: countdown.h:121
auto reset() -> bool
Reset countdown (reset.
Definition: countdown.cpp:88
auto start() -> bool
Start countdown.
Definition: countdown.cpp:57
unsigned int m_startTime
Definition: countdown.h:112
void elapsed()
Countdown elapsed.
auto stop() -> bool
Stop countdown and check.
Definition: countdown.cpp:78
void updateTimeLeft()
Start the timer until there is no time left, means.
Definition: countdown.cpp:25
Countdown(QObject *parent=nullptr, unsigned int i_seconds=0)
Countdown constructor.
Definition: countdown.cpp:12
auto isActive() const -> bool
Show countdown status.
Definition: countdown.cpp:52
void update(unsigned int)
Update countdown.
unsigned int m_timeLeft
Definition: countdown.h:115