2019-04-12 00:09:45 +02:00
|
|
|
#include <LibCore/CTimer.h>
|
2019-03-30 21:40:27 +01:00
|
|
|
|
2019-04-12 00:09:45 +02:00
|
|
|
CTimer::CTimer(CObject* parent)
|
2019-04-10 17:01:54 +02:00
|
|
|
: CObject(parent)
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-14 05:44:15 +02:00
|
|
|
CTimer::CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent)
|
|
|
|
: CObject(parent)
|
|
|
|
, on_timeout(move(timeout_handler))
|
|
|
|
{
|
|
|
|
start(interval);
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:09:45 +02:00
|
|
|
CTimer::~CTimer()
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:09:45 +02:00
|
|
|
void CTimer::start()
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
start(m_interval);
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:09:45 +02:00
|
|
|
void CTimer::start(int interval)
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
if (m_active)
|
|
|
|
return;
|
2019-04-14 05:44:15 +02:00
|
|
|
m_interval = interval;
|
2019-03-30 21:40:27 +01:00
|
|
|
start_timer(interval);
|
|
|
|
m_active = true;
|
|
|
|
}
|
|
|
|
|
2019-04-18 04:38:04 +02:00
|
|
|
void CTimer::restart(int interval)
|
|
|
|
{
|
|
|
|
if (m_active)
|
|
|
|
stop();
|
|
|
|
start(interval);
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:09:45 +02:00
|
|
|
void CTimer::stop()
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
if (!m_active)
|
|
|
|
return;
|
|
|
|
stop_timer();
|
|
|
|
m_active = false;
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:09:45 +02:00
|
|
|
void CTimer::timer_event(CTimerEvent&)
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
if (m_single_shot)
|
|
|
|
stop();
|
2019-04-18 04:38:04 +02:00
|
|
|
else {
|
|
|
|
if (m_interval_dirty) {
|
|
|
|
stop();
|
|
|
|
start(m_interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 21:40:27 +01:00
|
|
|
if (on_timeout)
|
|
|
|
on_timeout();
|
|
|
|
}
|