Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_signal_compressor.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2013 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef __KIS_SIGNAL_COMPRESSOR_H
8#define __KIS_SIGNAL_COMPRESSOR_H
9
10#include <QObject>
11#include "kritaglobal_export.h"
12
13#include <QElapsedTimer>
14#include <functional>
15
16class QTimer;
17
48class KRITAGLOBAL_EXPORT KisSignalCompressor : public QObject
49{
50 Q_OBJECT
51
52public:
53 enum Mode {
54 POSTPONE, /* Calling start() resets the timer to \p delay ms */
55 FIRST_ACTIVE_POSTPONE_NEXT, /* emits the first signal and postpones all the next ones */
56 FIRST_ACTIVE, /* Emit timeout() signal immediately. Throttle further timeout() to rate of one per \p delay ms */
57 FIRST_INACTIVE, /* Set a timer \p delay ms, Q_EMIT timeout() when it elapses. Ignore all events meanwhile. */
58 UNDEFINED /* KisSignalCompressor is created without an explicit mode */
59 };
60
62 PRECISE_INTERVAL, /* Interval of timeout is forced to \p delay ms, whatever time the handler of timeout() takes */
63 ADDITIVE_INTERVAL /* When the handler of timeout() is slow, the timeout delay is increased to the (delay + handler_time) */
64 };
65
66public:
68 KisSignalCompressor(int delay, Mode mode, QObject *parent = 0);
69 KisSignalCompressor(int delay, Mode mode, SlowHandlerMode slowHandlerMode, QObject *parent = 0);
70 bool isActive() const;
71 void setMode(Mode mode);
72
73 int delay() const;
74
75 void setDelay(std::function<bool()> idleCallback, int idleDelay, int timeout);
76
77public Q_SLOTS:
78 void setDelay(int delay);
79 void start();
80 void stop();
81
82private Q_SLOTS:
83 void slotTimerExpired();
84
85Q_SIGNALS:
86 void timeout();
87
88private:
89 bool tryEmitOnTick(bool isFromTimer);
90 bool tryEmitSignalSafely();
91 void setDelayImpl(int delay);
92
93private:
94 QTimer *m_timer = 0;
95 Mode m_mode = UNDEFINED;
96 SlowHandlerMode m_slowHandlerMode = PRECISE_INTERVAL;
97 bool m_signalsPending = false;
98 QElapsedTimer m_lastEmittedTimer;
99 int m_isEmitting = 0;
100 int m_timeout = 0;
101 std::function<bool()> m_idleCallback;
102 int m_sanityIsStarting = 0;
103};
104
105#endif /* __KIS_SIGNAL_COMPRESSOR_H */
std::function< bool()> m_idleCallback