This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cell.lisp
95 lines (78 loc) · 3.27 KB
/
cell.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#|
This file is a part of Qtools-UI
(c) 2015 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.qtools.ui)
(in-readtable :qtools)
(defgeneric padding (cell))
(defgeneric (setf padding) (padding cell))
(define-widget cell (QWidget selectable-item draggable mouse-propagator)
((padding :initarg :padding :accessor padding)
(draw-item :initarg :draw-item :accessor draw-item))
(:default-initargs :padding 3 :draw-item T))
(define-initializer (cell setup)
(setf (widget-item cell) (widget-item cell)))
(defmethod (setf widget-item) :before (item (cell cell))
(when (typep (widget-item cell) 'qobject)
(setf (parent (widget-item cell)) NIL)
(q+:remove-event-filter (widget-item cell) cell)))
(defmethod (setf widget-item) :after ((item qobject) (cell cell))
(q+:install-event-filter item cell))
(define-override (cell paint-event) (ev)
(with-finalizing ((painter (q+:make-qpainter cell)))
(when (active-p cell)
(q+:fill-rect painter (q+:rect cell) (q+:highlight (q+:palette cell))))
(when (and draw-item (not (typep (widget-item cell) 'qobject)))
(q+:draw-text painter (q+:adjusted (q+:rect cell) padding padding (- padding) (- padding))
(logior (q+:qt.align-left)
(q+:qt.align-vcenter))
(princ-to-string (widget-item cell)))))
(stop-overriding))
(define-override (cell resize-event) (ev)
(update cell)
(stop-overriding))
(define-override (cell event) (ev)
(when (= (enum-value (q+:type ev)) (q+:qevent.layout-request))
(update cell))
(stop-overriding))
(define-override (cell minimum-height) ()
(if (typep (widget-item cell) 'qobject)
(max 20 (+ padding (q+:minimum-height (widget-item cell)) padding))
30))
(defun padded-hint (cell hint)
(let ((padding (padding cell)))
(if (q+:is-valid hint)
(q+:make-qsize (+ padding (q+:width hint) padding)
(+ padding (q+:height hint) padding))
hint)))
(define-override (cell size-hint) ()
(cond ((typep (widget-item cell) 'qobject)
(padded-hint cell (q+:size-hint (widget-item cell))))
(T (call-next-qmethod))))
(define-override (cell minimum-size-hint) ()
(cond ((typep (widget-item cell) 'qobject)
(padded-hint cell (q+:minimum-size-hint (widget-item cell))))
(T (call-next-qmethod))))
(define-override (cell set-geometry) (&rest args)
(declare (ignore args))
(update cell)
(call-next-qmethod))
(defmethod drag-start ((cell cell) x y)
(declare (ignore x y))
(setf (active-widget (container cell)) cell))
(defmethod drag ((cell cell) px py nx ny)
(declare (ignore px py))
(let* ((pos (q+:map-to-parent cell (q+:make-qpoint nx ny)))
(widget (widget-at-point pos (container cell))))
(when (and (typep widget 'cell)
(eql (container widget) (container cell))
(not (eql widget cell)))
(swap-widgets widget cell (container cell)))))
(defmethod update ((cell cell))
(when (typep (widget-item cell) 'qobject)
(let ((padding (padding cell)))
(setf (q+:geometry (widget-item cell))
(values padding padding
(- (q+:width cell) padding padding)
(- (q+:height cell) padding padding))))))