support packages for symbols as property names.
Annotate for file src/signal-slot/connect.lisp
2010-01-10 tobias 1 (in-package :cl-smoke.qt-impl)
08:52:49 ' 2
' 3 (defgeneric qt:connect (qsignal slot &optional type)
' 4 (:documentation "Connects a signal to a slot."))
' 5
' 6 (defgeneric qt:disconnect (qsignal slot)
' 7 (:documentation "Disconnects a connection."))
' 8
' 9 (defgeneric qt:disconnect-all (qsignal)
' 10 (:documentation "Disconnects all connections of QSIGNAL."))
' 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
' 19 (defmethod qt:connect ((qsignal qsignal) (qslot qslot) &optional type)
' 20 (assert (or (slot-boundp (signal-object qsignal) 'argument-types)
' 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
' 40 (defmethod qt:connect ((sender qsignal) (function function) &optional type)
' 41 (let ((slot (make-instance 'qslot
2009-08-02 tobias 42 :args (list (signal-object sender))
2010-01-10 tobias 43 :argument-types (argument-types (signal-object sender))
08:52:49 ' 44 :slot-function function)))
' 45 (unless (connect-id (signal-object sender) (id (signal-object sender))
' 46 slot (id slot)
' 47 type
' 48 (types (argument-types (signal-object sender))))
' 49 (cerror "Failed to connect the function ~S to the signal ~S."
' 50 function sender))))
' 51
' 52 (defclass qt-signal-slot-name ()
' 53 ((name :initarg :name
' 54 :reader name)))
' 55
' 56 (defclass qt-signal (qt-signal-slot-name)
' 57 ((sender :initarg :sender
' 58 :reader qsender))
' 59 (:documentation "Qt C++ signal."))
' 60
' 61 (defclass qt-slot (qt-signal-slot-name)
' 62 ((receiver :initarg :receiver
' 63 :reader receiver))
' 64 (:documentation "Qt C++ slot."))
' 65
' 66 (defgeneric qt:get-slot (receiver name)
' 67 (:documentation "Returns the slot of RECEIVER with NAME.")
' 68 (:method (receiver name)
' 69 (make-instance 'qt-slot :receiver receiver :name name))
' 70 (:method (receiver (function function))
' 71 "Returns a slot for RECEIVER that calls function
' 72 with RECEIVER as the first argument."
2009-08-02 tobias 73 (let ((slot (qt:make-slot #'(lambda (&rest args)
11:15:21 ' 74 (apply function (cxx:parent *this*)
' 75 args)))))
' 76 (cxx:set-parent slot receiver)
' 77 slot)))
2010-01-10 tobias 78
08:52:49 ' 79 (define-compiler-macro qt:get-slot (&whole form receiver name)
' 80 "Normalize the slot name."
' 81 (if (stringp name)
' 82 (let ((normalized-name (cxx:data
' 83 (qt:meta-object.normalized-signature name))))
2009-08-02 tobias 84 (if (string= name normalized-name) ;; Avoid loop
2010-01-10 tobias 85 form
08:52:49 ' 86 `(qt:get-slot ,receiver ,normalized-name)))
' 87 form))
' 88
' 89 (defun qt:get-signal (sender name)
' 90 "Returns the signal NAME of SENDER."
' 91 (make-instance 'qt-signal :sender sender :name name))
' 92
' 93 (define-compiler-macro qt:get-signal (&whole form sender name)
' 94 "Normalize the signal name."
' 95 (if (stringp name)
' 96 (let ((normalized-name (cxx:data
' 97 (qt:meta-object.normalized-signature name))))
2009-08-02 tobias 98 (if (string= name normalized-name) ;; Avoid loop
2010-01-10 tobias 99 form
08:52:49 ' 100 `(qt:get-signal ,sender ,normalized-name)))
' 101 form))
' 102
' 103 (defmethod qt:connect ((qt-signal qt-signal) (qt-slot qt-slot) &optional type)
' 104 (unless (qt:object.connect (qsender qt-signal) (qt:qsignal (name qt-signal))
' 105 (receiver qt-slot) (qt:qslot (name qt-slot))
2009-08-27 tobias 106 (or type qt:+auto-connection+))
2010-01-10 tobias 107 (cerror "Failed to connect ~A ~A to ~A ~A."
08:52:49 ' 108 (qsender qt-signal) (name qt-signal)
' 109 (receiver qt-slot) (name qt-slot))))
' 110
' 111 (defmethod qt:disconnect ((qt-signal qt-signal) (qt-slot qt-slot))
' 112 (unless (qt:object.disconnect (qsender qt-signal) (qt:qsignal (name qt-signal))
' 113 (receiver qt-slot) (qt:qslot (name qt-slot)))
' 114 (cerror "Failed to disconnect ~A ~A from ~A ~A."
' 115 (receiver qt-slot) (name qt-slot)
' 116 (qsender qt-signal) (name qt-signal))))
' 117
' 118 (defmethod qt:disconnect-all ((sender qt:object))
' 119 (unless (qt:object.disconnect sender 0 0 0)
' 120 (cerror "Failed to disconnect everything connected to ~A."
' 121 sender)))
' 122
' 123
' 124 (defmethod qt:connect ((qt-signal qt-signal) (function function) &optional type)
' 125 (let* ((signal-id (find-signal-id (qsender qt-signal) (name qt-signal)))
' 126 (slot (make-instance 'qslot
2009-07-27 tobias 127 ;; Set the sender as the slots parent,
19:39:43 ' 128 ;; to ensure it does not get gc'ed.
2009-07-22 tobias 129 ;;
22:21:01 ' 130 ;; FIXME: unset parent on disconnect.
' 131 ;; This no not critical because the slot
' 132 ;; object is not accessible to the user,
' 133 ;; who thus can not connect it to other
' 134 ;; signals.
2009-07-27 tobias 135 :args (list (qsender qt-signal))
2010-01-10 tobias 136 :slot-function function
08:52:49 ' 137 :argument-types
' 138 (method-arguments-type
' 139 (cxx:meta-object (qsender qt-signal))
' 140 signal-id))))
' 141 (if (connect-id (qsender qt-signal) signal-id
' 142 slot (id slot)
' 143 type (types (arguments slot)))
' 144 (cxx:connect-notify (qsender qt-signal)
' 145 (name qt-signal))
' 146 (cerror "Ignore" "Failed to connect the signal ~S of ~S to the function ~S."
' 147 (name qt-signal) (qsender qt-signal) function))))
' 148
' 149 (defmethod qt:connect ((qt-signal qt-signal) (slot qslot) &optional type)
' 150 (let ((signal-id (find-signal-id (qsender qt-signal) (name qt-signal))))
' 151 (if (slot-boundp slot 'arguments)
' 152 (check-argument-types (method-arguments-type (cxx:meta-object
' 153 (qsender qt-signal))
' 154 signal-id)
' 155 (arguments slot))
' 156 (setf (slot-value slot 'arguments)
' 157 (method-arguments-type (cxx:meta-object (qsender qt-signal))
' 158 signal-id)))
' 159 (if (connect-id (qsender qt-signal) signal-id
' 160 slot (id slot)
' 161 type (types (arguments slot)))
' 162 (cxx:connect-notify (qsender qt-signal)
' 163 (name qt-signal))
' 164 (cerror "Ignore" "Failed to connect the signal ~S of ~S to the slot ~S."
' 165 (name qt-signal) (qsender qt-signal) slot))))
' 166
' 167 (defmethod qt:connect ((qsignal qsignal) (slot qt-slot) &optional type)
' 168 (let ((slot-id (find-slot-id (receiver slot) (name slot))))
' 169 (if (slot-boundp (signal-object qsignal) 'argument-types)
' 170 (check-argument-types (argument-types (signal-object slot))
' 171 (method-arguments-type (cxx:meta-object
' 172 (receiver slot))
' 173 slot-id))
' 174 (setf (argument-types (signal-object qsignal))
' 175 (method-arguments-type (cxx:meta-object (receiver slot))
' 176 slot-id)))
' 177 (unless (connect-id (signal-object qsignal) (id (signal-object qsignal))
' 178 (receiver slot) slot-id
' 179 type
' 180 (types (argument-types (signal-object qsignal))))
' 181 (cerror "Failed to connect ~S to ~S." qsignal slot))))
' 182
' 183
' 184 (defun connect-id (sender signal-id receiver slot-id type types)
' 185 (qt:meta-object.connect sender signal-id
' 186 receiver slot-id
' 187 (if (null type)
2009-08-27 tobias 188 (value qt:+auto-connection+)
08:37:36 ' 189 (value type))
2010-01-10 tobias 190 types))
08:52:49 ' 191
' 192 (defun disconnect-id (sender signal-id receiver slot-id)
' 193 (qt:meta-object.disconnect sender signal-id receiver slot-id))