forked from dbmcclain/LispPlotter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotter-classes.lisp
408 lines (385 loc) · 10.6 KB
/
plotter-classes.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
(in-package :plotter)
(defclass <line-style> ()
((line-thick
:initarg :thick
:accessor line-thick)
(line-dashing
:initarg :dashing
:accessor line-dashing)
(line-color
:initarg :color
:accessor line-color)
(line-alpha
:initarg :alpha
:accessor line-alpha)
(line-type
:initarg :type
:accessor line-type))
(:default-initargs
:thick 1
:dashing nil
:color :darkgreen
:alpha nil
:type :interpolated))
(defclass <symbol-style> ()
((plot-symbol
:initarg :symbol
:accessor plot-symbol)
(fill-color
:initarg :fill-color
:accessor fill-color)
(fill-alpha
:initarg :fill-alpha
:accessor fill-alpha)
(border-color
:initarg :border-color
:accessor border-color)
(border-alpha
:initarg :border-alpha
:accessor border-alpha)
(border-thick
:initarg :border-thick
:accessor border-thick)
(bar-width
:initarg :bar-width
:accessor bar-width)
(bar-offset
:initarg :bar-offset
:accessor bar-offset))
(:default-initargs
:symbol :circle
:fill-color nil
:fill-alpha nil
:border-color :black
:border-alpha nil
:border-thick 1
:bar-width nil
:bar-offset nil))
(defclass <plot-style> ()
((line-style
:initarg :line-style
:accessor line-style)
(symbol-style
:initarg :symbol-style
:accessor symbol-style)
(legend
:initarg :legend
:accessor legend))
(:default-initargs
:line-style (make-instance '<line-style>)
:symbol-style nil
:legend nil))
(defclass <legend> ()
((activep
:initform t
:accessor activep)
(has-content
:initform nil
:accessor has-content)
(highlighted
:initform nil
:accessor highlighted)
(x
:initform 0
:accessor x)
(y
:initform 0
:accessor y)
(width
:initform 0
:accessor width)
(height
:initform 0
:accessor height)
(dragging
:initform nil
:accessor dragging)
(dx
:initform 0
:accessor dx)
(dy
:initform 0
:accessor dy)))
(defclass <plotter-mixin> ()
;; stuff used by 2-D plot scaling and plotting
;; The mixin has all the information needed to produce plots
;; but has nothing to draw on...
((lock
:initform (mp:make-lock)
:accessor plotter-lock)
(xlog
:initform nil
:accessor plotter-xlog)
(xmin
:initform 0D0
:accessor plotter-xmin)
(xmax
:initform 1D0
:accessor plotter-xmax)
(ylog
:initform nil
:accessor plotter-ylog)
(ymin
:initform 0D0
:accessor plotter-ymin)
(ymax
:initform 1D0
:accessor plotter-ymax)
(box :accessor plotter-box)
(xform
:initform (list 1 0 0 1 0 0)
:accessor plotter-xform)
(inv-xform
:initform (list 1 0 0 1 0 0)
:accessor plotter-inv-xform)
(dlist
:initform (make-mpsafe-monitored-collector)
:accessor plotter-display-list)
(delayed
:initform 0
:accessor plotter-delayed-update)
;; info for nice looking zooming
(def-wd
:initarg :nominal-width
:accessor plotter-nominal-width)
(def-ht
:initarg :nominal-height
:accessor plotter-nominal-height)
(sf
:initform 1
:accessor plotter-sf)
(magn
:initform 1
:accessor plotter-magn)
(legend-info
:initform (make-collector)
:accessor plotter-legend-info)
(legend-x
:initform (list :frac 0.95)
:accessor plotter-legend-x)
(legend-y
:initform (list :frac 0.95)
:accessor plotter-legend-y)
(legend-anchor
:initform :auto
:accessor plotter-legend-anchor)
(legend
:initform (make-instance '<legend>)
:accessor plotter-legend)
(preferred-x
:initform nil
:accessor preferred-x)
(preferred-y
:initform nil
:accessor preferred-y)
(dirty
:initform nil
:accessor plotter-dirty)
(needs-legend
:initform nil
:accessor plotter-needs-legend)
(reply-mbox
:initform nil
:accessor reply-mbox))
(:default-initargs
:nominal-width 400
:nominal-height 300))
(defclass <plotter-pane> (<plotter-mixin> capi:output-pane)
;; stuff used by 2-D plot scaling and plotting
;; The pane adds something to draw on...
;; And it also adds some user gestures and any display related items
;; like cross hairs, cursors, backing images, etc.
((backing-image
:initform nil
:accessor plotter-backing-image)
(timer
:initform nil
:accessor plotter-resize-timer)
(backing-pixmap
:initform nil
:accessor plotter-backing-pixmap)
(delay-backing
:initform nil
:accessor plotter-delay-backing)
(full-crosshair
:initarg :full-crosshair
:accessor plotter-full-crosshair)
(prev-x
:initform nil
:accessor plotter-prev-x)
(prev-y
:initform nil
:accessor plotter-prev-y)
(x-ro-hook
:initform #'identity
:accessor plotter-x-readout-hook)
(y-ro-hook
:initform #'identity
:accessor plotter-y-readout-hook)
(plotter-valid
:initform t
:accessor plotter-valid)
(mark-x
:initform nil
:accessor mark-x)
(mark-y
:initform nil
:accessor mark-y)
(mark-x-raw
:initform nil
:accessor mark-x-raw)
(mark-y-raw
:initform nil
:accessor mark-y-raw))
(:default-initargs
:full-crosshair nil
:display-callback 'display-callback
:resize-callback 'resize-callback
:destroy-callback 'destroy-callback
:pane-menu 'popup-menu
:input-model
'((:motion mouse-move)
((:button-1 :motion) drag-legend)
((:button-1 :press) show-x-y-at-cursor)
((:button-1 :release) undrag-legend)
((:gesture-spec "Backspace") maybe-remove-legend)
((:gesture-spec "Delete") maybe-remove-legend)
((:gesture-spec "Control-c") copy-image-to-clipboard)
((:gesture-spec "Control-p") print-plotter-pane)
((:gesture-spec "Control-s") save-image-from-menu)
((:gesture-spec "C") toggle-full-crosshair)
((:gesture-spec "c") toggle-full-crosshair)
((:gesture-spec "x") mark-x-at-cursor)
((:gesture-spec "y") mark-y-at-cursor)
((:gesture-spec "m") mark-x-y-at-cursor)
((:gesture-spec "u") unmark-x-y))
:cursor :crosshair
:visible-min-width 200
:visible-min-height 150
:visible-max-width 800
:visible-max-height 600
:background :white
:foreground :black))
#+:WIN32
(defmethod initialize-instance :after
((pane <plotter-pane>) &rest initargs)
(when (slot-value pane 'full-crosshair)
(let ((color (slot-value pane 'full-crosshair)))
(setf
(slot-value pane 'full-crosshair)
(complementary-color
pane color (capi:simple-pane-background pane))))))
(defun destroy-callback (pane)
(setf (plotter-valid pane) nil)
(discard-backing-image pane)
(discard-backing-pixmap pane))
;;; FIXME : Replace the backquoted code in popup-menu
(defun popup-menu (pane selection x y)
(declare (ignore selection))
(make-instance
'capi:menu :items
`(,(make-instance
'capi:menu-component :items
`(,(make-instance
'capi:menu-item
:data :toggle-cursor
:text "Toggle crosshair")))
,(make-instance
'capi:menu-component :items
`(,(make-instance
'capi:menu-item
:data :copy-image
:text "Copy to clipboard")
,(make-instance
'capi:menu-item
:data :print-image
:text "Print image")
,(make-instance
'capi:menu-item
:data :save-image
:text "Save image")))
,@(when (or (on-legend pane x y)
(not (activep (plotter-legend pane))))
`(,(make-instance
'capi:menu-component :items
`(,@(when (on-legend pane x y)
`(,(make-instance
'capi:menu-item
:data :remove-legend
:text "Remove Legend")))
,@(when (not (activep (plotter-legend pane)))
`(,(make-instance
'capi:menu-item
:data :restore-legend
:text "Restore Legend")))))))
,(make-instance
'capi:menu-component :items
`(,(make-instance 'capi:menu-item :data :mark-x :text "Mark X")
,(make-instance 'capi:menu-item :data :mark-y :text "Mark Y")
,(make-instance
'capi:menu-item :data :mark-x-y :text "Mark X & Y")
,@(when (or (mark-x pane) (mark-y pane))
`(,(make-instance
'capi:menu-item
:data :remove-mark
:text "Remove marker"))))))
:callback
(lambda (key intf)
(declare (ignore intf))
(case key
(:toggle-cursor
(toggle-full-crosshair pane))
(:copy-image
(copy-image-to-clipboard pane))
(:print-image
(print-plotter-pane pane))
(:save-image
(save-image-from-menu pane))
(:remove-legend
(let ((legend (plotter-legend pane)))
(setf (activep legend) nil)
(restore-legend-background pane legend)))
(:restore-legend
(let ((legend (plotter-legend pane)))
(setf (activep legend) t)
(draw-existing-legend pane pane)))
(:mark-x
(mark-x-at-cursor pane x y))
(:mark-y
(mark-y-at-cursor pane x y))
(:mark-x-y
(mark-x-y-at-cursor pane x y))
(:remove-mark
(unmark-x-y pane))))))
(defun maybe-remove-legend (pane x y &rest args)
(declare (ignore args))
(let ((legend (plotter-legend pane)))
(when (and (activep legend) (on-legend pane x y))
(setf (activep legend) nil)
(restore-legend-background pane legend))))
(defgeneric plotter-mixin-of (pane-rep &optional args)
(:documentation
"pane-rep might be a <plotter-pane>, a subclass of a
capi:interface, or a symbolic name of a window."))
(defmethod plotter-mixin-of ((pane <plotter-mixin>) &optional args)
(declare (ignore args))
pane)
(defmethod display-pane-of (pane)
pane)
(defmethod display-pane-of ((obj capi:pinboard-object))
(display-pane-of (capi:element-parent obj)))
(defun append-display-list (pane item)
(collector-append-item (plotter-display-list pane) item))
(defun discard-display-list (pane)
(collector-discard-contents (plotter-display-list pane))
(collector-discard-contents (plotter-legend-info pane)))
(defun display-list-items (pane &key discard)
(collector-contents (plotter-display-list pane) :discard discard))
(defun display-list-empty-p (pane)
(collector-empty-p (plotter-display-list pane)))
(defun append-legend (pane item)
(collector-append-item (plotter-legend-info pane) item))
(defun all-legends (pane &key discard)
(collector-contents (plotter-legend-info pane) :discard discard))
(defun discard-legends (pane)
(collector-discard-contents (plotter-legend-info pane)))