Tue Apr 7 22:07:00 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* delayed initialization macro
hunk ./qt.mbd 51
+ ("timer" (:needs "qt"))
hunk ./src/package.lisp 13
+ #:single-shot
+ #:do-delayed-initialize
addfile ./src/timer.lisp
hunk ./src/timer.lisp 1
+(in-package :qt)
+
+(defclass single-shot-timer (qt:object)
+ ((function :initarg :function)
+ (timer-id))
+ (:metaclass cxx:class))
+
+(defvar *single-shot-timers* nil)
+
+(defun single-shot (function &optional (timeout 0))
+ "Run FUNCTION after TIMEOUT seconds, or as soon as all window events
+have been processed when TIMEOUT is 0."
+ (let ((timer (make-instance 'single-shot-timer
+ :function function)))
+ (setf (slot-value timer 'timer-id)
+ (cxx:start-timer timer (floor timeout 100)))
+ (push timer *single-shot-timers*)))
+
+(defmacro do-delayed-initialize (&body body)
+ "Run body when the event loop starts.
+
+http://techbase.kde.org/Development/Tutorials/Common_Programming_Mistakes#Delayed_Initialization"
+ `(single-shot #'(lambda () ,@body)))
+
+(defmethod cxx:timer-event ((timer single-shot-timer) &rest args)
+ (declare (ignore args))
+ (cxx:kill-timer timer (slot-value timer 'timer-id))
+ (funcall (slot-value timer 'function))
+ (remove timer *single-shot-timers*))