Changeset View
Changeset View
Standalone View
Standalone View
libs/global/kis_relaxed_timer.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2017 Bernhard Liebl <poke1024@gmx.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "kis_relaxed_timer.h" | ||||
20 | | ||||
21 | KisRelaxedTimer::KisRelaxedTimer(QObject *parent) | ||||
22 | : QObject(parent) | ||||
23 | , m_interval(0) | ||||
24 | , m_singleShot(false) | ||||
25 | , m_nextTimerTickSeqNo(1) | ||||
26 | , m_emitOnTimeTick(0) | ||||
27 | , m_isEmitting(false) | ||||
28 | { | ||||
29 | } | ||||
30 | | ||||
31 | void KisRelaxedTimer::setInterval(int interval) | ||||
32 | { | ||||
33 | Q_ASSERT(!isActive()); | ||||
34 | m_interval = interval; | ||||
35 | } | ||||
36 | | ||||
37 | void KisRelaxedTimer::setSingleShot(bool singleShot) | ||||
38 | { | ||||
39 | m_singleShot = singleShot; | ||||
40 | } | ||||
41 | | ||||
42 | int KisRelaxedTimer::remainingTime() const | ||||
43 | { | ||||
44 | // in contrast to normal QTimers, the remaining time is calculated in | ||||
45 | // terms of 2 * m_interval as this is the worst case interval. | ||||
46 | | ||||
47 | if (!isActive()) { | ||||
48 | return -1; | ||||
49 | } else { | ||||
50 | return qMax(qint64(0), 2 * m_interval - qint64(m_elapsed.elapsed())); | ||||
51 | } | ||||
52 | } | ||||
53 | | ||||
54 | void KisRelaxedTimer::start() | ||||
55 | { | ||||
56 | m_elapsed.start(); | ||||
57 | | ||||
58 | // cancels any previously scheduled timer and schedules a new timer to be | ||||
59 | // triggered as soon as possible, but never sooner than \p m_interval ms. | ||||
60 | | ||||
61 | if (!m_timer.isActive()) { | ||||
62 | // no internal timer is running. start one, and configure it to send | ||||
63 | // us a timeout event on the next possible tick which will be exactly | ||||
64 | // \p m_interval ms in the future. | ||||
65 | | ||||
66 | m_emitOnTimeTick = m_nextTimerTickSeqNo; | ||||
67 | m_timer.start(m_interval, this); | ||||
68 | } else if (m_isEmitting) { | ||||
69 | // an internal timer is running and we are actually called from a | ||||
70 | // timeout event. so we know the next tick will happen in exactly | ||||
71 | // \p m_interval ms. | ||||
72 | | ||||
73 | m_emitOnTimeTick = m_nextTimerTickSeqNo; | ||||
74 | } else { | ||||
75 | // an internal timer is already running, but we do not know when | ||||
76 | // the next tick will happen. we need to skip next tick as it | ||||
77 | // will be sooner than m_delay. the one after that will be good as | ||||
78 | // it will be m_interval * (1 + err) in the future. | ||||
79 | | ||||
80 | m_emitOnTimeTick = m_nextTimerTickSeqNo + 1; | ||||
81 | } | ||||
82 | } | ||||
83 | | ||||
84 | void KisRelaxedTimer::timerEvent(QTimerEvent *event) | ||||
85 | { | ||||
86 | Q_UNUSED(event); | ||||
87 | | ||||
88 | const int ticksStopThreshold = 5; | ||||
89 | | ||||
90 | const qint64 timerTickSeqNo = m_nextTimerTickSeqNo; | ||||
91 | | ||||
92 | // from this point on, if this is an emit tick, we are no longer active. | ||||
93 | m_nextTimerTickSeqNo++; | ||||
94 | | ||||
95 | if (timerTickSeqNo == m_emitOnTimeTick) { | ||||
96 | if (m_singleShot) { | ||||
97 | stop(); | ||||
98 | } | ||||
99 | const IsEmitting emitting(*this); | ||||
100 | emit timeout(); | ||||
101 | } else if (timerTickSeqNo - m_emitOnTimeTick > ticksStopThreshold) { | ||||
102 | m_timer.stop(); | ||||
103 | } | ||||
104 | } |