Changeset View
Changeset View
Standalone View
Standalone View
libs/global/kis_relaxed_timer.h
Context not available. | |||||
30 | * time guarantees and minimizes internal timer restarts by keeping one long-running | 30 | * time guarantees and minimizes internal timer restarts by keeping one long-running | ||
---|---|---|---|---|---|
31 | * repeating timer. | 31 | * repeating timer. | ||
32 | * | 32 | * | ||
33 | * Users can use this just like a QTimer. The difference is that KisRelaxedTimer will | 33 | * Users can use this mostly like a QTimer. The difference is that KisRelaxedTimer will | ||
34 | * relax the callback guarantee time as follows: timeouts will never happen earlier | 34 | * relax how precisely the callback happens. Using setInterval(), users of this class | ||
35 | * than \p interval ms, but may well happen only after 2 * \p interval ms (whereas | 35 | * configure how much the emitted signals are allowed to be "off", i.e. up to how many | ||
36 | * QTimer guarantees a fixed interval of \p interval ms). | 36 | * ms a signal may be emitted too early (\p maxOffEarly), and up to how many ms a signal | ||
37 | * may be emitted too late (\p maxOffLate). | ||||
38 | * | ||||
39 | * Setting \p maxOffLate to \p interval + 1 will allow KisRelaxedTimer to always use the | ||||
40 | * long running timer. | ||||
37 | * | 41 | * | ||
38 | * The rationale for using this is that stopping and starting timers can produce a | 42 | * The rationale for using this is that stopping and starting timers can produce a | ||
39 | * measurable performance overhead. KisRelaxedTimer removes that overhead. | 43 | * measurable performance overhead. KisRelaxedTimer can reduce that overhead. | ||
40 | */ | 44 | */ | ||
41 | class KRITAGLOBAL_EXPORT KisRelaxedTimer : public QObject | 45 | class KRITAGLOBAL_EXPORT KisRelaxedTimer : public QObject | ||
42 | { | 46 | { | ||
Context not available. | |||||
51 | m_emitOnTimeTick = 0; | 55 | m_emitOnTimeTick = 0; | ||
52 | } | 56 | } | ||
53 | 57 | | |||
54 | void setInterval(int interval); | 58 | void setInterval(int interval, int maxOffEarly, int maxOffLate); | ||
55 | void setSingleShot(bool singleShot); | 59 | void setSingleShot(bool singleShot); | ||
56 | 60 | | |||
57 | inline bool isActive() const { | 61 | inline bool isActive() const { | ||
Context not available. | |||||
64 | void timeout(); | 68 | void timeout(); | ||
65 | 69 | | |||
66 | protected: | 70 | protected: | ||
71 | void resync(); | ||||
72 | | ||||
67 | void timerEvent(QTimerEvent *event) override; | 73 | void timerEvent(QTimerEvent *event) override; | ||
68 | 74 | | |||
69 | private: | 75 | private: | ||
70 | int m_interval; | 76 | int m_interval; | ||
77 | int m_maxOffEarly; | ||||
78 | int m_maxOffLate; | ||||
71 | bool m_singleShot; | 79 | bool m_singleShot; | ||
72 | 80 | | |||
73 | QBasicTimer m_timer; | 81 | QBasicTimer m_timer; | ||
Context not available. | |||||
75 | qint64 m_emitOnTimeTick; | 83 | qint64 m_emitOnTimeTick; | ||
76 | 84 | | |||
77 | QElapsedTimer m_elapsed; | 85 | QElapsedTimer m_elapsed; | ||
86 | QElapsedTimer m_tick; | ||||
78 | 87 | | |||
79 | protected: | 88 | protected: | ||
80 | class IsEmitting { | 89 | class IsEmitting { | ||
Context not available. |