Changeset View
Changeset View
Standalone View
Standalone View
libs/global/kis_relaxed_timer.h
- 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 | #ifndef __KIS_RELAXED_TIMER_H | ||||
20 | #define __KIS_RELAXED_TIMER_H | ||||
21 | | ||||
22 | #include <QObject> | ||||
23 | #include <QBasicTimer> | ||||
24 | #include <QElapsedTimer> | ||||
25 | | ||||
26 | #include "kritaglobal_export.h" | ||||
27 | | ||||
28 | /** | ||||
29 | * A timer using an interface like QTimer that relaxes the given interval callback | ||||
30 | * time guarantees and minimizes internal timer restarts by keeping one long-running | ||||
31 | * repeating timer. | ||||
32 | * | ||||
33 | * Users can use this just like a QTimer. The difference is that KisRelaxedTimer will | ||||
34 | * relax the callback guarantee time as follows: timeouts will never happen earlier | ||||
35 | * than \p interval ms, but may well happen only after 2 * \p interval ms (whereas | ||||
36 | * QTimer guarantees a fixed interval of \p interval ms). | ||||
37 | * | ||||
38 | * The rationale for using this is that stopping and starting timers can produce a | ||||
39 | * measurable performance overhead. KisRelaxedTimer removes that overhead. | ||||
40 | */ | ||||
41 | class KRITAGLOBAL_EXPORT KisRelaxedTimer : public QObject | ||||
42 | { | ||||
43 | Q_OBJECT | ||||
44 | | ||||
45 | public: | ||||
46 | KisRelaxedTimer(QObject *parent = nullptr); | ||||
47 | | ||||
48 | void start(); | ||||
49 | | ||||
50 | inline void stop() { | ||||
51 | m_emitOnTimeTick = 0; | ||||
52 | } | ||||
53 | | ||||
54 | void setInterval(int interval); | ||||
55 | void setSingleShot(bool singleShot); | ||||
56 | | ||||
57 | inline bool isActive() const { | ||||
58 | return m_emitOnTimeTick >= m_nextTimerTickSeqNo; | ||||
59 | } | ||||
60 | | ||||
61 | int remainingTime() const; | ||||
62 | | ||||
63 | Q_SIGNALS: | ||||
64 | void timeout(); | ||||
65 | | ||||
66 | protected: | ||||
67 | void timerEvent(QTimerEvent *event) override; | ||||
68 | | ||||
69 | private: | ||||
70 | int m_interval; | ||||
71 | bool m_singleShot; | ||||
72 | | ||||
73 | QBasicTimer m_timer; | ||||
74 | qint64 m_nextTimerTickSeqNo; | ||||
75 | qint64 m_emitOnTimeTick; | ||||
76 | | ||||
77 | QElapsedTimer m_elapsed; | ||||
78 | | ||||
79 | protected: | ||||
80 | class IsEmitting { | ||||
81 | public: | ||||
82 | IsEmitting(KisRelaxedTimer &timer) : m_timer(timer) { | ||||
83 | timer.m_isEmitting = true; | ||||
84 | } | ||||
85 | | ||||
86 | ~IsEmitting() { | ||||
87 | m_timer.m_isEmitting = false; | ||||
88 | } | ||||
89 | | ||||
90 | private: | ||||
91 | KisRelaxedTimer &m_timer; | ||||
92 | }; | ||||
93 | | ||||
94 | friend class IsEmitting; | ||||
95 | | ||||
96 | bool m_isEmitting; | ||||
97 | }; | ||||
98 | | ||||
99 | #endif /* __KIS_RELAXED_TIMER_H */ |