forked from commonqt/commonqt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qvariant.lisp
49 lines (43 loc) · 1.76 KB
/
qvariant.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(in-package :qt)
(named-readtables:in-readtable :qt)
(defmarshal (value :|QVariant|)
(qvariant value))
(defmarshal (value :|const QVariant&|)
(qvariant value))
(defun qvariant-ptr-types ()
(with-cache ()
(iter (for (code . type) in (list (cons (#_QVariant::Color) "QColor")
(cons (#_QVariant::Pixmap) "QPixmap")
(cons (#_QVariant::Icon) "QIcon")))
(collect (cons (primitive-value code)
(find-qclass type))))))
(defun qvariant (value)
(etypecase value
(string (#_new QVariant :|const QString&| value))
(integer (#_new QVariant :|int| value))
((or single-float double-float) (#_new QVariant :|double| value))
(qobject
(iter (for (code . type) in (qvariant-ptr-types))
(when (qtypep value type)
(return (#_new QVariant code (qobject-pointer value))))
(finally (return value))))))
(defun unvariant (variant &optional (type (find-qtype "QVariant")))
(let* ((qobject (%qobject (qtype-class type) variant))
(code (primitive-value (#_type qobject))))
(case code
(2 (#_toInt qobject))
(10 (#_toString qobject))
(6 (#_toDouble qobject))
(t
(alexandria:if-let ((qclass (cdr (assoc code (qvariant-ptr-types)))))
(%qobject qclass (#_constData qobject))
qobject)))))
(define-marshalling-test (value :|QVariant|)
;; FIXME: this belongs to qvariant.lisp but we need it here (and qvariant.lisp needs call stuff)
(typecase value
((or string integer single-float double-float) t)
(qobject
(or (qtypep value "QVariant")
(iter (for (code . type) in (qvariant-ptr-types))
(thereis (qtypep value type)))))
(t nil)))