delayed initialization macro
Tue Apr 7 22:07:00 CEST 2009 Tobias Rautenkranz <tobias@rautenkranz.ch>
* delayed initialization macro
diff -rN -u old-qt.gui/qt.mbd new-qt.gui/qt.mbd
--- old-qt.gui/qt.mbd 2014-10-30 07:50:43.000000000 +0100
+++ new-qt.gui/qt.mbd 2014-10-30 07:50:43.000000000 +0100
@@ -48,6 +48,7 @@
("qstring" (:needs "qt"))
("msg-handler" (:needs "lib"))
("painter" (:needs "qt"))
+ ("timer" (:needs "qt"))
("i18n" (:needs "qt"))
("lisp-object" (:needs "qt" "lib"))
("signal-slot" module
diff -rN -u old-qt.gui/src/package.lisp new-qt.gui/src/package.lisp
--- old-qt.gui/src/package.lisp 2014-10-30 07:50:43.000000000 +0100
+++ new-qt.gui/src/package.lisp 2014-10-30 07:50:43.000000000 +0100
@@ -10,6 +10,8 @@
#:with-core-app
#:with-painter
+ #:single-shot
+ #:do-delayed-initialize
#:tr
#:with-translator
diff -rN -u old-qt.gui/src/timer.lisp new-qt.gui/src/timer.lisp
--- old-qt.gui/src/timer.lisp 1970-01-01 01:00:00.000000000 +0100
+++ new-qt.gui/src/timer.lisp 2014-10-30 07:50:43.000000000 +0100
@@ -0,0 +1,29 @@
+(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*))