1 /*
2  * DQt - D bindings for the Qt Toolkit
3  *
4  * GNU Lesser General Public License Usage
5  * This file may be used under the terms of the GNU Lesser
6  * General Public License version 3 as published by the Free Software
7  * Foundation and appearing in the file LICENSE.LGPL3 included in the
8  * packaging of this file. Please review the following information to
9  * ensure the GNU Lesser General Public License version 3 requirements
10  * will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
11  */
12 module qt.core.timer;
13 extern(C++):
14 
15 import qt.config;
16 import qt.core.coreevent;
17 import qt.core.namespace;
18 import qt.core.object;
19 import qt.core.property;
20 import qt.helpers;
21 
22 /+ #ifndef QT_NO_QOBJECT +/
23 
24 /+ #if __has_include(<chrono>)
25 #endif +/
26 
27 
28 extern(C++, class) struct QTimerPrivate;
29 /// Binding for C++ class [QTimer](https://doc.qt.io/qt-6/qtimer.html).
30 class /+ Q_CORE_EXPORT +/ QTimer : QObject
31 {
32     mixin(Q_OBJECT);
33     /+ Q_PROPERTY(bool singleShot READ isSingleShot WRITE setSingleShot BINDABLE bindableSingleShot)
34     Q_PROPERTY(int interval READ interval WRITE setInterval BINDABLE bindableInterval)
35     Q_PROPERTY(int remainingTime READ remainingTime)
36     Q_PROPERTY(Qt::TimerType timerType READ timerType WRITE setTimerType BINDABLE bindableTimerType)
37     Q_PROPERTY(bool active READ isActive STORED false BINDABLE bindableActive) +/
38 public:
39     /+ explicit +/this(QObject parent = null);
40     ~this();
41 
42     final bool isActive() const;
43     final QBindable!(bool) bindableActive();
44     final int timerId() const;
45 
46     final void setInterval(int msec);
47     final int interval() const;
48     final QBindable!(int) bindableInterval();
49 
50     final int remainingTime() const;
51 
52     final void setTimerType(/+ Qt:: +/qt.core.namespace.TimerType atype);
53     final /+ Qt:: +/qt.core.namespace.TimerType timerType() const;
54     final QBindable!(/+ Qt:: +/qt.core.namespace.TimerType) bindableTimerType();
55 
56     final void setSingleShot(bool singleShot);
57     final bool isSingleShot() const;
58     final QBindable!(bool) bindableSingleShot();
59 
60     mixin(changeWindowsMangling(q{mangleClassesTailConst}, q{
61     static void singleShot(int msec, const(QObject) receiver, const(char)* member);
62     }));
63     mixin(changeWindowsMangling(q{mangleClassesTailConst}, q{
64     static void singleShot(int msec, /+ Qt:: +/qt.core.namespace.TimerType timerType, const(QObject) receiver, const(char)* member);
65     }));
66 
67 /+ #ifdef Q_CLANG_QDOC
68     template<typename PointerToMemberFunction>
69     static void singleShot(int msec, const QObject *receiver, PointerToMemberFunction method);
70     template<typename PointerToMemberFunction>
71     static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method);
72     template<typename Functor>
73     static void singleShot(int msec, Functor functor);
74     template<typename Functor>
75     static void singleShot(int msec, Qt::TimerType timerType, Functor functor);
76     template<typename Functor, int>
77     static void singleShot(int msec, const QObject *context, Functor functor);
78     template<typename Functor, int>
79     static void singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor);
80     template <typename Functor>
81     QMetaObject::Connection callOnTimeout(Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
82     template <typename Functor>
83     QMetaObject::Connection callOnTimeout(const QObject *context, Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
84     template <typename MemberFunction>
85     QMetaObject::Connection callOnTimeout(const QObject *receiver, MemberFunction *slot, Qt::ConnectionType connectionType = Qt::AutoConnection);
86 #else +/
87     // singleShot to a QObject slot
88     /+ template <typename Duration, typename Func1> +/
89     /+ static inline void singleShot(Duration interval, const typename QtPrivate::FunctionPointer<Func1>::Object *receiver, Func1 slot)
90     {
91         singleShot(interval, defaultTypeFor(interval), receiver, slot);
92     } +/
93     /+ template <typename Duration, typename Func1> +/
94     /+ static inline void singleShot(Duration interval, Qt::TimerType timerType, const typename QtPrivate::FunctionPointer<Func1>::Object *receiver,
95                                   Func1 slot)
96     {
97         typedef QtPrivate::FunctionPointer<Func1> SlotType;
98 
99         //compilation error if the slot has arguments.
100         static_assert(int(SlotType::ArgumentCount) == 0,
101                           "The slot must not have any arguments.");
102 
103         singleShotImpl(interval, timerType, receiver,
104                        new QtPrivate::QSlotObject<Func1, typename SlotType::Arguments, void>(slot));
105     } +/
106     // singleShot to a functor or function pointer (without context)
107     /+ template <typename Duration, typename Func1> +/
108     /+ static inline typename std::enable_if<!QtPrivate::FunctionPointer<Func1>::IsPointerToMemberFunction &&
109                                           !std::is_same<const char*, Func1>::value, void>::type
110             singleShot(Duration interval, Func1 slot)
111     {
112         singleShot(interval, defaultTypeFor(interval), nullptr, std::move(slot));
113     } +/
114     /+ template <typename Duration, typename Func1> +/
115     /+ static inline typename std::enable_if<!QtPrivate::FunctionPointer<Func1>::IsPointerToMemberFunction &&
116                                           !std::is_same<const char*, Func1>::value, void>::type
117             singleShot(Duration interval, Qt::TimerType timerType, Func1 slot)
118     {
119         singleShot(interval, timerType, nullptr, std::move(slot));
120     } +/
121     // singleShot to a functor or function pointer (with context)
122     /+ template <typename Duration, typename Func1> +/
123     /+ static inline typename std::enable_if<!QtPrivate::FunctionPointer<Func1>::IsPointerToMemberFunction &&
124                                           !std::is_same<const char*, Func1>::value, void>::type
125             singleShot(Duration interval, const QObject *context, Func1 slot)
126     {
127         singleShot(interval, defaultTypeFor(interval), context, std::move(slot));
128     } +/
129     /+ template <typename Duration, typename Func1> +/
130     /+ static inline typename std::enable_if<!QtPrivate::FunctionPointer<Func1>::IsPointerToMemberFunction &&
131                                           !std::is_same<const char*, Func1>::value, void>::type
132             singleShot(Duration interval, Qt::TimerType timerType, const QObject *context, Func1 slot)
133     {
134         //compilation error if the slot has arguments.
135         typedef QtPrivate::FunctionPointer<Func1> SlotType;
136         static_assert(int(SlotType::ArgumentCount) <= 0,  "The slot must not have any arguments.");
137 
138         singleShotImpl(interval, timerType, context,
139                        new QtPrivate::QFunctorSlotObject<Func1, 0,
140                             typename QtPrivate::List_Left<void, 0>::Value, void>(std::move(slot)));
141     } +/
142 
143     /+ template <typename ... Args> +/
144     /+ QMetaObject::Connection callOnTimeout(Args && ...args)
145     {
146         return QObject::connect(this, &QTimer::timeout, std::forward<Args>(args)... );
147     } +/
148 
149 /+ #endif +/
150 
151 public /+ Q_SLOTS +/:
152     @QSlot final void start(int msec);
153 
154     @QSlot final void start();
155     @QSlot final void stop();
156 
157 /+ Q_SIGNALS +/public:
158     @QSignal final void timeout(QPrivateSignal);
159 
160 public:
161 /+ #if __has_include(<chrono>) || defined(Q_QDOC) +/
162     /+ void setInterval(std::chrono::milliseconds value)
163     {
164         setInterval(int(value.count()));
165     } +/
166 
167     /+ std::chrono::milliseconds intervalAsDuration() const
168     {
169         return std::chrono::milliseconds(interval());
170     } +/
171 
172     /+ std::chrono::milliseconds remainingTimeAsDuration() const
173     {
174         return std::chrono::milliseconds(remainingTime());
175     } +/
176 
177     /+ static void singleShot(std::chrono::milliseconds value, const QObject *receiver, const char *member)
178     {
179         singleShot(int(value.count()), receiver, member);
180     } +/
181 
182     /+ static void singleShot(std::chrono::milliseconds value, Qt::TimerType timerType, const QObject *receiver, const char *member)
183     {
184         singleShot(int(value.count()), timerType, receiver, member);
185     } +/
186 
187     /+ void start(std::chrono::milliseconds value)
188     {
189         start(int(value.count()));
190     } +/
191 /+ #endif +/
192 
193 protected:
194     override void timerEvent(QTimerEvent );
195 
196 private:
197     /+ Q_DISABLE_COPY(QTimer) +/
198     /+ Q_DECLARE_PRIVATE(QTimer) +/
199 
200     pragma(inline, true) final int startTimer(int){ return -1;}
201     pragma(inline, true) final void killTimer(int){}
202 
203     static /+ Qt:: +/qt.core.namespace.TimerType defaultTypeFor(int msecs)/+ noexcept+/
204     { return msecs >= 2000 ? /+ Qt:: +/qt.core.namespace.TimerType.CoarseTimer : /+ Qt:: +/qt.core.namespace.TimerType.PreciseTimer; }
205     mixin(changeWindowsMangling(q{mangleClassesTailConst}, q{
206     static void singleShotImpl(int msec, /+ Qt:: +/qt.core.namespace.TimerType timerType,
207                                    const(QObject) receiver, /+ QtPrivate:: +/dqtimported!q{qt.core.objectdefs_impl}.QSlotObjectBase* slotObj);
208     }));
209 
210 /+ #if __has_include(<chrono>) +/
211     /+ static Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval)
212     { return defaultTypeFor(int(interval.count())); } +/
213 
214     /+ static void singleShotImpl(std::chrono::milliseconds interval, Qt::TimerType timerType,
215                                const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
216     {
217         singleShotImpl(int(interval.count()),
218                        timerType, receiver, slotObj);
219     } +/
220 /+ #endif +/
221     mixin(CREATE_CONVENIENCE_WRAPPERS);
222 }
223 
224 
225 /+ #endif +/ // QT_NO_QOBJECT
226