1 /*
2     This file is part of the KDE libraries
3     SPDX-FileCopyrightText: 2003 George Staikos <staikos@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "ktimeout.h"
9 #include <QTimerEvent>
10 
KTimeout(QObject * parent)11 KTimeout::KTimeout(QObject *parent)
12     : QObject(parent)
13 {
14 }
15 
~KTimeout()16 KTimeout::~KTimeout()
17 {
18 }
19 
clear()20 void KTimeout::clear()
21 {
22     for (int timerId : std::as_const(_timers)) {
23         killTimer(timerId);
24     }
25     _timers.clear();
26 }
27 
removeTimer(int id)28 void KTimeout::removeTimer(int id)
29 {
30     const int timerId = _timers.value(id, 0);
31     if (timerId != 0) {
32         killTimer(timerId);
33     }
34     _timers.remove(id);
35 }
36 
addTimer(int id,int timeout)37 void KTimeout::addTimer(int id, int timeout)
38 {
39     if (_timers.contains(id)) {
40         return;
41     }
42     _timers.insert(id, startTimer(timeout));
43 }
44 
resetTimer(int id,int timeout)45 void KTimeout::resetTimer(int id, int timeout)
46 {
47     int timerId = _timers.value(id, 0);
48     if (timerId != 0) {
49         killTimer(timerId);
50         _timers.insert(id, startTimer(timeout));
51     }
52 }
53 
timerEvent(QTimerEvent * ev)54 void KTimeout::timerEvent(QTimerEvent *ev)
55 {
56     QHash<int, int>::const_iterator it = _timers.constBegin();
57     for (; it != _timers.constEnd(); ++it) {
58         if (it.value() == ev->timerId()) {
59             Q_EMIT timedOut(it.key());
60             return;
61         }
62     }
63 }
64