-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimelyaction.cpp
45 lines (40 loc) · 873 Bytes
/
timelyaction.cpp
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
#include "timelyaction.h"
#include <QTimerEvent>
TimelyAction::TimelyAction(QObject *parent)
:QObject(parent)
,m_periodInSeconds(0)
,m_doActImmediately(false)
,m_timerId(0)
,m_lastTriggered()
{
m_timerId = startTimer(50);
}
void TimelyAction::setPeriodInSeconds(int value)
{
m_periodInSeconds = value;
}
int TimelyAction::periodInSeconds() const
{
return m_periodInSeconds;
}
void TimelyAction::actShortly()
{
m_doActImmediately = true;
}
void TimelyAction::timerEvent(QTimerEvent *event)
{
if (event->timerId() == m_timerId)
{
QDateTime current = QDateTime::currentDateTime();
if (
(m_doActImmediately) ||
(!m_lastTriggered.isValid()) ||
((m_periodInSeconds > 0) && (m_lastTriggered.addSecs(m_periodInSeconds) <= current))
)
{
m_lastTriggered = current;
m_doActImmediately = false;
emit triggered();
}
}
}