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
/
toolkit.lisp
213 lines (193 loc) · 7.79 KB
/
toolkit.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#|
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)
(defun stable-sort-into (sequence predicate &rest args &key key)
(declare (ignore key))
(let ((sorted (apply #'stable-sort sequence predicate args)))
(unless (eq sorted sequence)
(map-into sequence #'identity (if (listp sorted) (copy-list sorted) sorted)))
sequence))
(defun swapcar (a b list)
(when (< b a) (rotatef a b))
(let* ((acell (nthcdr a list))
(bcell (nthcdr (- b a) acell))
(first (car acell)))
(setf (car acell) (car bcell)
(car bcell) first))
list)
(defun insert (item pos sequence)
(etypecase sequence
(list
(let ((cell (nthcdr pos sequence)))
(setf (cdr cell) (cons (car cell) (cdr cell))
(car cell) item)))
(vector
(array-utils:vector-push-extend-position item sequence pos)))
sequence)
(defmacro remove-nth (pos sequence)
(let ((n (gensym "POS"))
(cell (gensym "CELL"))
(seq (gensym "SEQ")))
`(let ((,n ,pos)
(,seq ,sequence))
(etypecase ,seq
(list
(if (= 0 ,n)
(pop ,seq)
(let ((,cell (nthcdr (1- ,n) ,seq)))
(prog1 (cadr ,cell)
(setf (cdr ,cell) (cddr ,cell))))))
(vector
(array-utils:vector-pop-position ,seq ,n))))))
(defun rotate-seq (sequence &optional (delta 1))
(etypecase sequence
(list
(when sequence
(flet ((rotate-left ()
(let ((last (last sequence)))
(setf (cdr last) (butlast sequence)
sequence last)))
(rotate-right ()
(let ((first sequence))
(setf (cdr (last sequence)) first
sequence (cdr first)
(cdr first) NIL))))
(if (< 0 delta)
(dotimes (i delta) (rotate-left))
(dotimes (i (- delta)) (rotate-right))))))
(vector
(when (< 0 (length sequence))
(flet ((rotate-left ()
(let ((last (aref sequence (1- (length sequence)))))
(loop for i downfrom (1- (length sequence)) above 0
do (setf (aref sequence i) (aref sequence (1- i)))
finally (setf (aref sequence 0) last))))
(rotate-right ()
(let ((first (aref sequence 0)))
(loop for i from 0 below (1- (length sequence))
do (setf (aref sequence i) (aref sequence (1+ i)))
finally (setf (aref sequence (1- (length sequence))) first)))))
(let ((delta (mod delta (length sequence))))
(if (< 0 delta)
(dotimes (i delta) (rotate-left))
(dotimes (i (- delta)) (rotate-right))))))))
sequence)
(defmacro rotate-seqf (sequence &optional (delta 1))
`(setf ,sequence (rotate-seq ,sequence ,delta)))
(defun clamp (low mid high)
(min (max mid low) high))
(defun default-test (test test-not)
(if (and (not test) (not test-not))
#'eql
test))
(defun call-with-translation (painter target function)
(q+:save painter)
(q+:translate painter target)
(unwind-protect
(funcall function)
(q+:restore painter)))
(defmacro with-translation ((painter target) &body body)
`(call-with-translation ,painter ,target (lambda () ,@body)))
(defun color-to-rgba (r g b &optional (a 255))
(let ((rgba 0))
(setf (ldb (byte 8 0) rgba) (round b)
(ldb (byte 8 8) rgba) (round g)
(ldb (byte 8 16) rgba) (round r)
(ldb (byte 8 24) rgba) (round a))
rgba))
(defun rgba-to-color (rgba)
(values (ldb (byte 8 16) rgba)
(ldb (byte 8 8) rgba)
(ldb (byte 8 0) rgba)
(ldb (byte 8 24) rgba)))
(defvar *color-map* (make-hash-table :test 'eql))
(defun c (r g b &optional (a 255))
(let ((rgba (color-to-rgba r g b a)))
(or (gethash rgba *color-map*)
(setf (gethash rgba *color-map*)
(q+:make-qcolor (round r) (round g) (round b) (round a))))))
(defun coerce-color (color)
(etypecase color
(qobject
color)
(integer
(multiple-value-bind (r g b a) (rgba-to-color color)
(c r g b a)))
(cons
(destructuring-bind (r g b &optional (a 255)) color
(c r g b a)))))
(defun ensure-function (function-ish)
(etypecase function-ish
(cl:function function-ish)
(symbol (fdefinition function-ish))
(list (case (first function-ish)
((cl:lambda) (compile NIL function-ish))
((cl:setf cl+qt:setf) (fdefinition function-ish))
((cl:function cl+qt:function) (eval function-ish))
(T (error "Don't know how to turn ~s into a function." function-ish))))))
(defun read-symbol (string &optional (case :upcase))
(let ((out (make-string-output-stream))
(package NIL)
(name NIL)
(escape NIL))
;; I know this is not entirely right as to how symbols are read, but we are
;; being lenient on purpose.
(loop for c across string
do (cond (escape (write-char c out)
(setf escape NIL))
(T (case c
(#\\ (setf escape T))
(#\: (cond (package (write-char c out))
(T (setf package (get-output-stream-string out)))))
(T (case case
(:upcase (write-char (char-upcase c) out))
(:downcase (write-char (char-downcase c) out))
(:preserve (write-char c out))
(:invert (cond ((upper-case-p c) (write-char (char-downcase c) out))
((lower-case-p c) (write-char (char-upcase c) out))
(T (write-char c out)))))))))
finally (setf name (get-output-stream-string out)))
(cond ((string= package "#")
(make-symbol name))
((string= package "")
(intern name :keyword))
(T
(intern name package)))))
(defun shortest-package-name (package)
(let ((package (find-package package))
(minimal (package-name package)))
(loop for name in (package-nicknames package)
do (when (< (length name) (length minimal))
(setf minimal name)))
minimal))
(defun format-symbol (symbol &optional (case :upcase))
(with-output-to-string (out)
(flet ((format-string (string)
(loop for c across string
do (cond ((char= c #\:)
(write-string "\\:" out))
((upper-case-p c)
(case case
(:upcase (write-char (char-downcase c) out))
(:downcase (write-char #\\ out) (write-char c out))
(:preserve (write-char c out))
(:invert (write-char (char-downcase c) out))))
((lower-case-p c)
(case case
(:upcase (write-char #\\ out) (write-char c out))
(:downcase (write-char c out))
(:preserve (write-char c out))
(:invert (write-char (char-upcase c) out))))
(T (write-char c out))))))
(let ((package (symbol-package symbol)))
(cond ((eql (find-package :keyword) package))
(package
(format-string (shortest-package-name package)))
(T
(write-char #\# out))))
(write-char #\: out)
(format-string (symbol-name symbol)))))