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