Serenity Operating System
1/*
2 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#define AK_DONT_REPLACE_STD
8
9#include "TimerQt.h"
10#include <AK/NonnullRefPtr.h>
11#include <QTimer>
12
13namespace Ladybird {
14
15NonnullRefPtr<TimerQt> TimerQt::create()
16{
17 return adopt_ref(*new TimerQt);
18}
19
20TimerQt::TimerQt()
21{
22 m_timer = new QTimer;
23 QObject::connect(m_timer, &QTimer::timeout, [this] {
24 if (on_timeout)
25 on_timeout();
26 });
27}
28
29TimerQt::~TimerQt()
30{
31 delete m_timer;
32}
33
34void TimerQt::start()
35{
36 m_timer->start();
37}
38
39void TimerQt::start(int interval_ms)
40{
41 m_timer->start(interval_ms);
42}
43
44void TimerQt::restart()
45{
46 restart(interval());
47}
48
49void TimerQt::restart(int interval_ms)
50{
51 if (is_active())
52 stop();
53 start(interval_ms);
54}
55
56void TimerQt::stop()
57{
58 m_timer->stop();
59}
60
61void TimerQt::set_active(bool active)
62{
63 if (active)
64 m_timer->start();
65 else
66 m_timer->stop();
67}
68
69bool TimerQt::is_active() const
70{
71 return m_timer->isActive();
72}
73
74int TimerQt::interval() const
75{
76 return m_timer->interval();
77}
78
79void TimerQt::set_interval(int interval_ms)
80{
81 m_timer->setInterval(interval_ms);
82}
83
84bool TimerQt::is_single_shot() const
85{
86 return m_timer->isSingleShot();
87}
88
89void TimerQt::set_single_shot(bool single_shot)
90{
91 m_timer->setSingleShot(single_shot);
92}
93
94}