| 1 | /* |
| 2 | * Copyright 2015 Canonical Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; version 3. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #ifndef TIMER_P_H |
| 18 | #define TIMER_P_H |
| 19 | |
| 20 | #include <QtCore/QObject> |
| 21 | #include <QtCore/QPointer> |
| 22 | #include <QtCore/QTimer> |
| 23 | |
| 24 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 25 | #include <LomiriGestures-Qt6/lomirigesturesglobal.h> |
| 26 | #include <LomiriGestures-Qt6/private/timesource_p.h> |
| 27 | #else |
| 28 | #include <LomiriGestures/lomirigesturesglobal.h> |
| 29 | #include <LomiriGestures/private/timesource_p.h> |
| 30 | #endif |
| 31 | |
| 32 | UG_NAMESPACE_BEGIN |
| 33 | |
| 34 | /* Defines an interface for a Timer. */ |
| 35 | class LOMIRIGESTURES_EXPORT AbstractTimer : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | public: |
| 39 | AbstractTimer(QObject *parent) : QObject(parent), m_isRunning(false) {} |
| 40 | virtual int interval() const = 0; |
| 41 | virtual void setInterval(int msecs) = 0; |
| 42 | virtual void start() { m_isRunning = true; } |
| 43 | virtual void stop() { m_isRunning = false; } |
| 44 | bool isRunning() const { return m_isRunning; } |
| 45 | virtual bool isSingleShot() const = 0; |
| 46 | virtual void setSingleShot(bool value) = 0; |
| 47 | Q_SIGNALS: |
| 48 | void timeout(); |
| 49 | private: |
| 50 | bool m_isRunning; |
| 51 | }; |
| 52 | |
| 53 | /* Essentially a QTimer wrapper */ |
| 54 | class LOMIRIGESTURES_EXPORT Timer : public AbstractTimer |
| 55 | { |
| 56 | Q_OBJECT |
| 57 | public: |
| 58 | Timer(QObject *parent = nullptr); |
| 59 | |
| 60 | int interval() const override; |
| 61 | void setInterval(int msecs) override; |
| 62 | void start() override; |
| 63 | void stop() override; |
| 64 | bool isSingleShot() const override; |
| 65 | void setSingleShot(bool value) override; |
| 66 | private: |
| 67 | QTimer m_timer; |
| 68 | }; |
| 69 | |
| 70 | /* For tests */ |
| 71 | class LOMIRIGESTURES_EXPORT FakeTimer : public AbstractTimer |
| 72 | { |
| 73 | Q_OBJECT |
| 74 | public: |
| 75 | FakeTimer(const SharedTimeSource &timeSource, QObject *parent = nullptr); |
| 76 | |
| 77 | void update(); |
| 78 | qint64 nextTimeoutTime() const { return m_nextTimeoutTime; } |
| 79 | |
| 80 | int interval() const override; |
| 81 | void setInterval(int msecs) override; |
| 82 | void start() override; |
| 83 | bool isSingleShot() const override; |
| 84 | void setSingleShot(bool value) override; |
| 85 | private: |
| 86 | int m_interval; |
| 87 | bool m_singleShot; |
| 88 | SharedTimeSource m_timeSource; |
| 89 | qint64 m_nextTimeoutTime; |
| 90 | }; |
| 91 | |
| 92 | class LOMIRIGESTURES_EXPORT AbstractTimerFactory |
| 93 | { |
| 94 | public: |
| 95 | virtual ~AbstractTimerFactory() {} |
| 96 | virtual AbstractTimer *createTimer(QObject *parent = nullptr) = 0; |
| 97 | }; |
| 98 | |
| 99 | class LOMIRIGESTURES_EXPORT TimerFactory : public AbstractTimerFactory |
| 100 | { |
| 101 | public: |
| 102 | AbstractTimer *createTimer(QObject *parent = nullptr) override { return new Timer(parent); } |
| 103 | }; |
| 104 | |
| 105 | class LOMIRIGESTURES_EXPORT FakeTimerFactory : public AbstractTimerFactory |
| 106 | { |
| 107 | public: |
| 108 | FakeTimerFactory(); |
| 109 | virtual ~FakeTimerFactory() {} |
| 110 | |
| 111 | void updateTime(qint64 msecsSinceReference); |
| 112 | QSharedPointer<TimeSource> timeSource() { return m_timeSource; } |
| 113 | |
| 114 | AbstractTimer *createTimer(QObject *parent = nullptr) override; |
| 115 | QList<QPointer<FakeTimer>> timers; |
| 116 | private: |
| 117 | QSharedPointer<FakeTimeSource> m_timeSource; |
| 118 | }; |
| 119 | |
| 120 | UG_NAMESPACE_END |
| 121 | |
| 122 | #endif // TIMER_P_H |