Normalize signal and slot names at compile time.
Annotate for file src/signal-slot/connect.lisp
2009-06-11 tobias 1 (in-package :qt)
2010-01-10 tobias 2
2009-06-11 tobias 3 (defgeneric connect (qsignal slot &optional type)
2010-01-10 tobias 4 (:documentation "Connects a signal to a slot."))
08:52:49 ' 5
2009-06-11 tobias 6 (defgeneric disconnect (qsignal slot)
2010-01-10 tobias 7 (:documentation "Disconnects a connection."))
08:52:49 ' 8
2009-06-11 tobias 9 (defgeneric disconnect-all (qsignal)
2010-01-10 tobias 10 (:documentation "Disconnects all connections of QSIGNAL."))
08:52:49 ' 11
' 12 (defun check-argument-types (signal-arguments slot-arguments)
' 13 (assert (= (length signal-arguments) (length slot-arguments)))
' 14 (loop for signal-arg in signal-arguments
' 15 for slot-arg in slot-arguments do
2009-08-02 tobias 16 (assert (subtypep signal-arg slot-arg))))
2010-01-10 tobias 17
08:52:49 ' 18 ;;FIXME check argument-types
2009-06-11 tobias 19 (defmethod connect ((qsignal qsignal) (qslot qslot) &optional type)
2010-01-10 tobias 20 (assert (or (slot-boundp (signal-object qsignal) 'argument-types)
08:52:49 ' 21 (slot-boundp qslot 'arguments))
' 22 ((slot-value (signal-object qsignal) 'argument-types)
' 23 (slot-value qslot 'arguments))
' 24 "Argument types must be specified for at least on of
' 25 ~A and ~A." qsignal qslot)
' 26 (when (not (slot-boundp (signal-object qsignal) 'argument-types))
' 27 (setf (argument-types (signal-object qsignal))
' 28 (arguments qslot)))
' 29 (when (not (slot-boundp qslot 'arguments))
' 30 (setf (slot-value qslot 'arguments)
' 31 (argument-types (signal-object qsignal))))
' 32 (check-argument-types (argument-types (signal-object qsignal))
' 33 (arguments qslot))
' 34 (unless (connect-id (signal-object qsignal) (id (signal-object qsignal))
' 35 qslot (id qslot)
' 36 type
' 37 (types (arguments qslot)))
' 38 (cerror "Failed to connect ~S to ~S." qsignal qslot)))
' 39
2009-06-11 tobias 40 (defmethod connect ((sender qsignal) (function function) &optional type)
2010-01-10 tobias 41 (let ((slot (make-instance 'qslot
2009-08-02 tobias 42 :args (list (signal-object sender))
2010-01-10 tobias 43 :slot-function function)))
08:52:49 ' 44 (unless (connect-id (signal-object sender) (id (signal-object sender))
' 45 slot (id slot)
' 46 type
2009-06-21 tobias 47 (types (arguments sender)))
2010-01-10 tobias 48 (cerror "Failed to connect the function ~S to the signal ~S."
08:52:49 ' 49 function sender))))
' 50
' 51 (defclass qt-signal-slot-name ()
' 52 ((name :initarg :name
2009-06-04 tobias 53 :reader name)))
2010-01-10 tobias 54
08:52:49 ' 55 (defclass qt-signal (qt-signal-slot-name)
' 56 ((sender :initarg :sender
' 57 :reader qsender))
' 58 (:documentation "Qt C++ signal."))
' 59
' 60 (defclass qt-slot (qt-signal-slot-name)
' 61 ((receiver :initarg :receiver
' 62 :reader receiver))
' 63 (:documentation "Qt C++ slot."))
' 64
2009-06-05 tobias 65 (defun get-slot (receiver name)
07:45:07 ' 66 "Returns the slot of RECEIVER with NAME."
' 67 (make-instance 'qt-slot :receiver receiver :name name))
2010-01-10 tobias 68
2009-06-04 tobias 69 (define-compiler-macro get-slot (&whole form receiver name)
10:58:29 ' 70 "Normalize the slot name."
' 71 (if (stringp name)
' 72 (let ((normalized-name (cxx:data (meta-object.normalized-signature name))))
' 73 (if (string= name normalized-name) ;; Avoid loop
' 74 form
' 75 `(get-slot ,receiver ,normalized-name)))
' 76 form))
' 77
2009-06-11 tobias 78 (defun get-signal (sender name)
2010-01-10 tobias 79 "Returns the signal NAME of SENDER."
08:52:49 ' 80 (make-instance 'qt-signal :sender sender :name name))
2009-06-04 tobias 81
10:58:29 ' 82 (define-compiler-macro get-signal (&whole form sender name)
' 83 "Normalize the signal name."
' 84 (if (stringp name)
' 85 (let ((normalized-name (cxx:data (meta-object.normalized-signature name))))
' 86 (if (string= name normalized-name) ;; Avoid loop
' 87 form
' 88 `(get-signal ,sender ,normalized-name)))
' 89 form))
' 90
2009-06-11 tobias 91 (defmethod connect ((qt-signal qt-signal) (qt-slot qt-slot) &optional type)
14:59:48 ' 92 (unless (object.connect (qsender qt-signal) (qsignal (name qt-signal))
' 93 (receiver qt-slot) (qslot (name qt-slot))
' 94 (or type +auto-connection+))
2010-01-10 tobias 95 (cerror "Failed to connect ~A ~A to ~A ~A."
08:52:49 ' 96 (qsender qt-signal) (name qt-signal)
' 97 (receiver qt-slot) (name qt-slot))))
' 98
2009-06-11 tobias 99 (defmethod disconnect ((qt-signal qt-signal) (qt-slot qt-slot))
14:59:48 ' 100 (unless (object.disconnect (qsender qt-signal) (qsignal (name qt-signal))
' 101 (receiver qt-slot) (qslot (name qt-slot)))
2010-01-10 tobias 102 (cerror "Failed to disconnect ~A ~A from ~A ~A."
08:52:49 ' 103 (receiver qt-slot) (name qt-slot)
' 104 (qsender qt-signal) (name qt-signal))))
' 105
2009-06-11 tobias 106 (defmethod disconnect-all ((sender object))
14:59:48 ' 107 (unless (object.disconnect sender 0 0 0)
2010-01-10 tobias 108 (cerror "Failed to disconnect everything connected to ~A."
08:52:49 ' 109 sender)))
' 110
' 111
2009-06-11 tobias 112 (defmethod connect ((qt-signal qt-signal) (function function) &optional type)
2010-01-10 tobias 113 (let* ((signal-id (find-signal-id (qsender qt-signal) (name qt-signal)))
08:52:49 ' 114 (slot (make-instance 'qslot
2009-07-27 tobias 115 ;; Set the sender as the slots parent,
19:39:43 ' 116 ;; to ensure it does not get gc'ed.
2009-07-22 tobias 117 ;; FIXME: unset parent on disconnect
2009-07-01 tobias 118 ;; this no not critical beause the slot object
2009-07-22 tobias 119 ;; is hidden from the user, who thus can not
22:21:01 ' 120 ;; connect it to other signals.
2009-07-27 tobias 121 :args (list (qsender qt-signal))
2010-01-10 tobias 122 :slot-function function
08:52:49 ' 123 :argument-types
' 124 (method-arguments-type
' 125 (cxx:meta-object (qsender qt-signal))
' 126 signal-id))))
' 127 (if (connect-id (qsender qt-signal) signal-id
' 128 slot (id slot)
' 129 type (types (arguments slot)))
' 130 (cxx:connect-notify (qsender qt-signal)
' 131 (name qt-signal))
' 132 (cerror "Ignore" "Failed to connect the signal ~S of ~S to the function ~S."
' 133 (name qt-signal) (qsender qt-signal) function))))
' 134
2009-06-11 tobias 135 (defmethod connect ((qt-signal qt-signal) (slot qslot) &optional type)
2010-01-10 tobias 136 (let ((signal-id (find-signal-id (qsender qt-signal) (name qt-signal))))
08:52:49 ' 137 (if (slot-boundp slot 'arguments)
' 138 (check-argument-types (method-arguments-type (cxx:meta-object
' 139 (qsender qt-signal))
' 140 signal-id)
' 141 (arguments slot))
' 142 (setf (slot-value slot 'arguments)
' 143 (method-arguments-type (cxx:meta-object (qsender qt-signal))
' 144 signal-id)))
' 145 (if (connect-id (qsender qt-signal) signal-id
' 146 slot (id slot)
' 147 type (types (arguments slot)))
' 148 (cxx:connect-notify (qsender qt-signal)
' 149 (name qt-signal))
' 150 (cerror "Ignore" "Failed to connect the signal ~S of ~S to the slot ~S."
' 151 (name qt-signal) (qsender qt-signal) slot))))
' 152
2009-06-11 tobias 153 (defmethod connect ((qsignal qsignal) (slot qt-slot) &optional type)
2010-01-10 tobias 154 (let ((slot-id (find-slot-id (receiver slot) (name slot))))
08:52:49 ' 155 (if (slot-boundp (signal-object qsignal) 'argument-types)
' 156 (check-argument-types (argument-types (signal-object slot))
' 157 (method-arguments-type (cxx:meta-object
' 158 (receiver slot))
' 159 slot-id))
' 160 (setf (argument-types (signal-object qsignal))
' 161 (method-arguments-type (cxx:meta-object (receiver slot))
' 162 slot-id)))
' 163 (unless (connect-id (signal-object qsignal) (id (signal-object qsignal))
' 164 (receiver slot) slot-id
' 165 type
' 166 (types (argument-types (signal-object qsignal))))
' 167 (cerror "Failed to connect ~S to ~S." qsignal slot))))
' 168
' 169
' 170 (defun connect-id (sender signal-id receiver slot-id type types)
2009-06-11 tobias 171 (meta-object.connect sender signal-id
2010-01-10 tobias 172 receiver slot-id
08:52:49 ' 173 (if (null type)
2009-06-11 tobias 174 (value +auto-connection+)
2009-08-27 tobias 175 (value type))
2010-01-10 tobias 176 types))
08:52:49 ' 177
' 178 (defun disconnect-id (sender signal-id receiver slot-id)
2009-06-11 tobias 179 (meta-object.disconnect sender signal-id receiver slot-id))