| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QCHRONOTIMER_H |
| 5 | #define QCHRONOTIMER_H |
| 6 | |
| 7 | #ifndef QT_NO_QOBJECT |
| 8 | |
| 9 | #include <QtCore/qcoreevent.h> |
| 10 | #include <QtCore/qnamespace.h> |
| 11 | #include <QtCore/qobject.h> |
| 12 | #include <QtCore/qproperty.h> |
| 13 | #include <QtCore/qtimer.h> |
| 14 | |
| 15 | #include <chrono> |
| 16 | |
| 17 | QT_BEGIN_NAMESPACE |
| 18 | |
| 19 | class QTimerPrivate; |
| 20 | class Q_CORE_EXPORT QChronoTimer : public QObject |
| 21 | { |
| 22 | Q_OBJECT |
| 23 | Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot |
| 24 | BINDABLE bindableSingleShot FINAL) |
| 25 | Q_PROPERTY(std::chrono::nanoseconds interval READ interval WRITE setInterval |
| 26 | BINDABLE bindableInterval FINAL) |
| 27 | Q_PROPERTY(std::chrono::nanoseconds remainingTime READ remainingTime FINAL) |
| 28 | Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType |
| 29 | BINDABLE bindableTimerType FINAL) |
| 30 | Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive FINAL) |
| 31 | |
| 32 | template <typename Functor> |
| 33 | using FunctorContext = typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType; |
| 34 | |
| 35 | public: |
| 36 | explicit QChronoTimer(std::chrono::nanoseconds nsec, QObject *parent = nullptr); |
| 37 | explicit QChronoTimer(QObject *parent = nullptr); |
| 38 | ~QChronoTimer() override; |
| 39 | |
| 40 | bool isActive() const; |
| 41 | QBindable<bool> bindableActive(); |
| 42 | Qt::TimerId id() const; |
| 43 | |
| 44 | void setInterval(std::chrono::nanoseconds nsec); |
| 45 | std::chrono::nanoseconds interval() const; |
| 46 | QBindable<std::chrono::nanoseconds> bindableInterval(); |
| 47 | |
| 48 | std::chrono::nanoseconds remainingTime() const; |
| 49 | |
| 50 | void setTimerType(Qt::TimerType atype); |
| 51 | Qt::TimerType timerType() const; |
| 52 | QBindable<Qt::TimerType> bindableTimerType(); |
| 53 | |
| 54 | void setSingleShot(bool singleShot); |
| 55 | bool isSingleShot() const; |
| 56 | QBindable<bool> bindableSingleShot(); |
| 57 | |
| 58 | #ifdef Q_QDOC |
| 59 | template <typename Functor> |
| 60 | QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, |
| 61 | Qt::ConnectionType connectionType = Qt::AutoConnection); |
| 62 | #else |
| 63 | template <typename ... Args> |
| 64 | QMetaObject::Connection callOnTimeout(Args && ...args) |
| 65 | { |
| 66 | return QObject::connect(this, &QChronoTimer::timeout, std::forward<Args>(args)... ); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | public Q_SLOTS: |
| 71 | void start(); |
| 72 | void stop(); |
| 73 | |
| 74 | Q_SIGNALS: |
| 75 | void timeout(QPrivateSignal); |
| 76 | |
| 77 | protected: |
| 78 | void timerEvent(QTimerEvent *) override; |
| 79 | |
| 80 | private: |
| 81 | Q_DISABLE_COPY(QChronoTimer) |
| 82 | |
| 83 | // QChronoTimer uses QTimerPrivate |
| 84 | inline QTimerPrivate *d_func() noexcept |
| 85 | { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<QTimerPrivate *>(qGetPtrHelper(d_ptr));) } |
| 86 | inline const QTimerPrivate *d_func() const noexcept |
| 87 | { Q_CAST_IGNORE_ALIGN(return reinterpret_cast<const QTimerPrivate *>(qGetPtrHelper(d_ptr));) } |
| 88 | |
| 89 | // These two functions are inherited from QObject |
| 90 | int startTimer(std::chrono::nanoseconds) = delete; |
| 91 | void killTimer(int) = delete; |
| 92 | }; |
| 93 | |
| 94 | QT_END_NAMESPACE |
| 95 | |
| 96 | #endif // QT_NO_QOBJECT |
| 97 | |
| 98 | #endif // QCHRONOTIMER_H |
| 99 | |