support packages for symbols as property names.
Annotate for file src/object.lisp
2010-01-10 tobias 1 (in-package :cl-smoke.qt-impl)
08:52:49 ' 2
2009-08-02 tobias 3 (smoke:eval-startup (:load-toplevel :compile-toplevel :execute)
2009-08-27 tobias 4 (let ((object (make-instance 'qt:object)))
08:37:36 ' 5 (defmethod cxx:static-meta-object ((class (eql (find-class 'qt:object))))
' 6 "No OBJECT.STATIC-META-OBJECT (r558420)."
' 7 (cxx:meta-object object))))
' 8
2010-01-10 tobias 9 (defmethod cxx:static-meta-object ((class cxx:class))
08:52:49 ' 10 (cxx:static-meta-object (smoke::find-smoke-class class)))
' 11
' 12 (defmethod documentation :around ((class smoke::smoke-standard-class)
' 13 (doc-type (eql 't)))
' 14 (if (and (subtypep class (find-class 'qt:object))
' 15 (not (subtypep class (find-class 'cxx:class))))
' 16 (format nil "~@[~A~%~]Properties:~%~T~{~<~%~T~0,75:; ~(~A~)~>~}
' 17
' 18 Signals:
' 19 ~{~T~A~%~}
' 20 Slots:
' 21 ~{~T~A~%~}"
' 22 (call-next-method) (sort (qt:class-direct-properties class) #'string<=)
' 23 (sort (class-signals class) #'string<=)
' 24 (sort (class-slots class) #'string<=))
' 25 (call-next-method)))
' 26
' 27 (defmethod print-object ((object qt:object) stream)
' 28 (if (or (not (slot-boundp object 'pointer))
' 29 (null-pointer-p (pointer object)))
' 30 (call-next-method)
2009-08-02 tobias 31 (print-unreadable-object (object stream :type t :identity t)
11:29:13 ' 32 (princ (cxx:object-name object) stream))))
2010-01-10 tobias 33
08:52:49 ' 34 (defun meta-object-methods (meta-object &optional (direct-only nil))
' 35 (loop for index from (if direct-only (cxx:method-offset meta-object) 0)
' 36 below (cxx:method-count meta-object)
' 37 collect (cxx:method meta-object index)))
' 38
' 39
' 40 (defun meta-object-signals (meta-object &key all)
' 41 (mapcar #'cxx:signature
' 42 (remove-if-not #'(lambda (m) (enum= qt:meta-method.+signal+
' 43 (cxx:method-type m)))
' 44 (meta-object-methods meta-object (not all)))))
' 45
' 46 (defun class-signals (class &key all)
' 47 (meta-object-signals (cxx:static-meta-object class) :all all))
' 48
' 49 (defun meta-object-slots (meta-object &key all)
' 50 (mapcar #'cxx:signature
' 51 (remove-if-not #'(lambda (m) (enum= qt:meta-method.+slot+
' 52 (cxx:method-type m)))
' 53 (meta-object-methods meta-object (not all)))))
' 54
' 55
' 56 (defun class-slots (class &key all)
' 57 (meta-object-slots (cxx:static-meta-object class) :all all))
' 58
' 59 (defun parent-p (object)
2009-08-02 tobias 60 (not (null-pointer-p (smoke::pointer-call (smoke::make-smoke-method-from-name (find-class 'qt:object)
11:15:21 ' 61 "parent")
' 62 (smoke::pointer object)))))
2010-01-10 tobias 63
2009-08-02 tobias 64 (defun find-smoke-parent (object)
11:15:21 ' 65 "Returns the first parent of OBJECT or OBJECT that is a Smoke class.
' 66 (the destructed callback is called when the object is freed.)"
' 67 ;; FIXME allow usage of non smoke objects by connecting to the
' 68 ;; destroyed() signal.
2010-01-10 tobias 69 (declare (optimize (speed 3)))
08:52:49 ' 70 (let ((parent (cxx:parent object)))
2009-08-02 tobias 71 (if (not (null-pointer-p (smoke::pointer parent)))
11:15:21 ' 72 (if (smoke::has-pointer-p (smoke::pointer parent))
' 73 parent
' 74 (find-smoke-parent parent))
' 75 (error "No smoke parent found for ~A." object))))
2010-01-10 tobias 76
08:52:49 ' 77 (defmethod initialize-instance :after ((object qt:object)
' 78 &key (pointer nil pointer-p) &allow-other-keys)
' 79 "Registers the object to the parent when a parent was set in the constructor
' 80 and the objects metaclass is SMOKE-WRAPPER-CLASS."
' 81 (declare (optimize (speed 3)))
' 82 (when (and (not pointer-p)
' 83 (null-pointer-p (smoke::pointer object)))
' 84 (error "Object ~A has not been constructed" object))
' 85 (when (and (null pointer)
' 86 (not (null-pointer-p (smoke::pointer object)))
' 87 (parent-p object))
' 88 (smoke::transfer-ownership-to object
2009-08-02 tobias 89 (find-smoke-parent object))))
2010-01-10 tobias 90
08:52:49 ' 91 (define-condition wrapper-gc (storage-condition)
2009-08-02 tobias 92 ((class-name :initarg :class-name
11:15:21 ' 93 :documentation "The class name of the gc'ed object.")
2010-01-10 tobias 94 (pointer :initarg :pointer))
08:52:49 ' 95 (:report (lambda (condition stream)
2009-08-02 tobias 96 (format stream "The object ~A ~A of type cxx:class
11:15:21 ' 97 has a parent but got garbage collected."
' 98 (slot-value condition 'class-name)
' 99 (slot-value condition 'pointer)))))
2010-01-10 tobias 100
2009-08-02 tobias 101 (smoke:eval-startup (:compile-toplevel :execute)
2009-07-22 tobias 102 (defparameter *get-parent*
22:21:01 ' 103 (smoke::make-smoke-method-from-name (find-class 'qt:object) "parent"))
' 104
' 105 ;; FIXME this leaks memory when QCoreApplication::exec() is never
' 106 ;; called, beause then, deleteLater() has no effect.
' 107 (defparameter *delete-later*
' 108 (smoke::make-smoke-method-from-name (find-class 'qt:object) "deleteLater")))
2010-01-10 tobias 109
08:52:49 ' 110 (defmethod smoke::make-finalize ((object qt:object))
' 111 "Delete the qt:object OBJECT,
' 112 by calling cxx:delete-later iff it has no parent."
' 113 (let ((pointer (pointer object))
' 114 (class (class-of object))
' 115 (next (call-next-method)))
' 116 (declare (function next))
' 117 (if (typep (class-of object) 'cxx:class)
' 118 #'(lambda ()
' 119 (declare (optimize (speed 3)))
' 120 (handler-case
' 121 (if (null-pointer-p (smoke::pointer-call *get-parent* pointer))
' 122 (smoke::pointer-call *delete-later* pointer)
' 123 (error (make-condition 'wrapper-gc
2009-08-02 tobias 124 :class-name (name class)
2010-01-10 tobias 125 :pointer pointer)))
08:52:49 ' 126 (error (condition)
' 127 (smoke::report-finalize-error condition "qt:object wrap"
' 128 (name class) pointer))))
' 129 #'(lambda ()
' 130 (declare (optimize (speed 3)))
' 131 (handler-case
' 132 (if (null-pointer-p (smoke::pointer-call *get-parent* pointer))
' 133 (funcall next)
' 134 (cerror "Ignore" "Finalizer for object with a parent called."))
' 135 (error (condition)
' 136 (smoke::report-finalize-error condition "qt:object"
' 137 (name class) pointer)))))))
' 138
' 139
' 140 ;;;
2009-07-22 tobias 141 ;;; The event-notify callback get called by QCoreApplication, on
22:21:01 ' 142 ;;; notification of an event.
' 143 ;;;
' 144 ;;; The DATA argument is an array of size three, containing the
' 145 ;;; pointers:
2010-01-10 tobias 146 ;;;
08:52:49 ' 147 ;;; void* receiver
' 148 ;;; void* event
' 149 ;;; void* result
' 150 ;;; in that order.
' 151 ;;;
' 152 ;;; Returning true marks the event as handled; false on the other hand
' 153 ;;; leaves the event processing unchanged.
' 154 ;;;
2009-07-22 tobias 155 ;;; See: QCoreApplication::notifyInternal(QObject *receiver, QEvent
22:21:01 ' 156 ;;; *event)
2010-01-10 tobias 157
08:52:49 ' 158 (cffi:defcallback event-notify smoke:cxx-bool
' 159 ((data :pointer))
' 160 (declare (optimize (speed 3)))
' 161 (let ((receiver (smoke::get-object (cffi:mem-aref data :pointer 0)))
2009-08-02 tobias 162 (event (make-instance 'qt:event
11:29:02 ' 163 :pointer (cffi:mem-aref data :pointer 1))))
2010-01-10 tobias 164 (enum-case (cxx:type event)
08:52:49 ' 165 (qt:event.+child-added+
2009-08-02 tobias 166 (let ((child-event (make-instance 'qt:child-event
11:29:02 ' 167 :pointer
' 168 (smoke::upcast event (find-class 'qt:child-event)))))
' 169 (tg:cancel-finalization (cxx:child child-event))
' 170 (when (smoke::has-pointer-p (smoke::pointer (cxx:child child-event)))
' 171 (unless receiver
2009-08-02 tobias 172 (setf receiver (find-smoke-parent (cxx:child child-event))))
2009-08-02 tobias 173 (smoke::transfer-ownership-to (cxx:child child-event) receiver))))
2010-01-10 tobias 174 (qt:event.+child-removed+
2009-08-02 tobias 175 (let* ((child-event (make-instance 'qt:child-event
11:29:02 ' 176 :pointer (smoke::upcast event
' 177 (find-class 'qt:child-event)))))
' 178 ;; We receive child removed events for any QObject, whether
2009-07-22 tobias 179 ;; it was constructed by Smoke or not. Only take ownership of
22:21:01 ' 180 ;; objects that have been constructed by Smoke.
2009-08-02 tobias 181 (when (smoke::has-pointer-p (smoke::pointer (cxx:child child-event)))
11:29:02 ' 182 (assert receiver)
' 183 (smoke::take-ownership (cxx:child child-event) receiver))))))
2010-01-10 tobias 184 nil)
08:52:49 ' 185
' 186 (eval-when (:compile-toplevel :load-toplevel :execute)
' 187 (cffi:defcfun qt-smoke-register-event-notify :boolean
' 188 (event-notify :pointer)))
' 189
' 190 (defun register-event-notify ()
' 191 (let ((ret (qt-smoke-register-event-notify (cffi:callback event-notify))))
' 192 (unless ret
2009-08-02 tobias 193 (error "The Qt event-notify callback table is full."))))
2010-01-10 tobias 194
2009-08-02 tobias 195 (smoke:eval-startup ()
2010-01-10 tobias 196 (register-event-notify))