forked from SirJohnFranklin/TraitsMatplotlibWidget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraggableResizableRectangle.py
433 lines (343 loc) · 14.3 KB
/
DraggableResizableRectangle.py
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import numpy as np
from traits.api import HasTraits, Instance, Any, Str, on_trait_change, Int, Event
import matplotlib.patches as mpatches
import matplotlib
class DraggableResizeableLine(HasTraits):
"""
Resizable Lines based on the DraggabelResizableRectangle. Draggable is yet not implemented
Author: KingKarl, April 2017
"""
lock = None # only one can be animated at a time
updateXY = Int(0)
updateText = Int(0)
released = Int(0)
def __init__(self, line, border_tol=0.15, allow_resize=True,
fixed_aspect_ratio=False):
super(DraggableResizeableLine,self).__init__()
self.line = line
self.border_tol = border_tol
self.press = None
def connect(self):
'connect to all the events we need'
self.cidpress = self.line.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.cidrelease = self.line.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.cidmotion = self.line.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
def on_press(self, event):
'on button press we will see if the mouse is over us and store some data'
if event.inaxes != self.line.axes: return
if DraggableResizeableLine.lock is not None: return
x,y = self.line.get_data()
x0, x1 = x
y0, y1 = y
if not self.within_border_tol([x0, y0],[x1, y1],event): return
DraggableResizeableLine.lock = self
self.press = x0, y0, x1, y1, event.xdata, event.ydata
canvas = self.line.figure.canvas
axes = self.line.axes
self.line.set_animated(True)
canvas.draw()
self.background = canvas.copy_from_bbox(self.line.axes.bbox)
# now redraw just the rectangle
axes.draw_artist(self.line)
# and blit just the redrawn area
canvas.blit(axes.bbox)
def within_border_tol(self,pos_0, pos_1, event):
x0, y0 = pos_0
x1, y1 = pos_1
xpress, ypress = event.xdata, event.ydata
bt = self.border_tol * (abs(x0-x1)**2 + abs(y0-y1)**2)**0.5
if (abs(x0-xpress)**2+abs(y0-ypress)**2)**0.5<2**0.5*abs(bt) or (abs(x1-xpress)**2+abs(y1-ypress)**2)**0.5<2**0.5*abs(bt) or (abs((x0+x1)/2-xpress)**2+abs((y0+y1)/2-ypress)**2)**0.5<2**0.5*abs(bt):
return True
else:
return False
def on_motion(self, event):
'on motion we will move the rect if the mouse is over us'
if DraggableResizeableLine.lock is not self:
return
if event.inaxes != self.line.axes: return
x0, y0, x1, y1, xpress, ypress = self.press
self.dx = event.xdata - xpress
self.dy = event.ydata - ypress
self.update_line()
canvas = self.line.figure.canvas
axes = self.line.axes
# restore the background region
canvas.restore_region(self.background)
# redraw just the current line
axes.draw_artist(self.line)
# blit just the redrawn area
canvas.blit(axes.bbox)
self.updateXY += 1
def on_release(self, event):
'on release we reset the press data'
if DraggableResizeableLine.lock is not self:
return
self.press = None
DraggableResizeableLine.lock = None
# turn off the rect animation property and reset the background
self.line.set_animated(False)
self.background = None
self.updateText +=1
# redraw the full figure
self.line.figure.canvas.draw()
self.released += 1
def disconnect(self):
'disconnect all the stored connection ids'
self.line.figure.canvas.mpl_disconnect(self.cidpress)
self.line.figure.canvas.mpl_disconnect(self.cidrelease)
self.line.figure.canvas.mpl_disconnect(self.cidmotion)
def update_line(self):
x0, y0, x1, y1, xpress, ypress = self.press
bt = self.border_tol * (abs(x0-x1)**2 + abs(y0-y1)**2)**0.5
if (abs(x0-xpress)**2+abs(y0-ypress)**2)**0.5<2**0.5*abs(bt): # Check for if mouse close to start (pos 0) of line
self.line.set_data([x0+self.dx,x1],[y0+self.dy,y1])
elif (abs(x1-xpress)**2+abs(y1-ypress)**2)**0.5<2**0.5*abs(bt): # Check for if mouse close to start (pos 1) of line
self.line.set_data([x0,x1+self.dx],[y0,y1+self.dy])
elif (abs((x0+x1)/2-xpress)**2+abs((y0+y1)/2-ypress)**2)**0.5<2**0.5*abs(bt): # Make line draggable at center
self.line.set_data([x0+self.dx,x1+self.dx],[y0+self.dy,y1+self.dy])
class AnnotatedLine(HasTraits):
axes = Instance(matplotlib.axes.Axes)
annotext = Instance(matplotlib.text.Text)
text = Str()
drl = Instance(DraggableResizeableLine)
lineUpdated = Int(0)
lineReleased = Int(0)
def __init__(self, axes, x0, y0, x1, y1,text, color = 'k'):#text, color='c', ecolor='k', alpha=0.7):
print("View: Line created")
super(AnnotatedLine, self).__init__()
self.pos_0 = [x0,y0]
self.pos_1 = [x1,y1]
self.axes = axes
self.text = text
line_handle = self.axes.plot([x0,x1],[y0,y1],color = color)[0]
self.line = line_handle
self.drl = DraggableResizeableLine(line_handle)
self.drl.connect()
def disconnect(self):
self.drl.disconnect()
def connect(self):
self.drl.connect()
@on_trait_change('drl.updateText')
def updateText(self):
print('Draw_Text')
try:
self.annotext.remove()
except AttributeError:
print("AnnotatedRectangle: Found no annotated text")
x, y = self.line.get_data()
self.pos_0 = np.array([x[0],y[0]])
self.pos_1 = np.array([x[1],y[1]])
self.annotext = self.axes.annotate(self.text, self.pos_1+(self.pos_0-self.pos_1)/2, color='w', weight='bold',fontsize=6, ha='center', va='center')
@on_trait_change('drl.updateXY')
def xyLineUpdated(self):
self.lineUpdated += 1
@on_trait_change('drl.released')
def released(self):
print("AnnotatedRectangle: Rectangle released")
x, y = self.line.get_data()
self.pos_0 = np.array([x[0],y[0]])
self.pos_1 = np.array([x[1],y[1]])
self.lineReleased += 1
def get_pos(self):
return self.pos_0, self.pos_1
def remove(self):
self.line.remove()
self.annotext.remove()
del self
class DraggableResizeableRectangle(HasTraits):
"""
Draggable and resizeable rectangle with the animation blit techniques.
Based on example code at
http://matplotlib.sourceforge.net/users/event_handling.html
If *allow_resize* is *True* the recatngle can be resized by dragging its
lines. *border_tol* specifies how close the pointer has to be to a line for
the drag to be considered a resize operation. Dragging is still possible by
clicking the interior of the rectangle. *fixed_aspect_ratio* determines if
the recatngle keeps its aspect ratio during resize operations.
"""
updateText = Int(0)
updateXY = Int(0)
released = Int(0)
lock = None # only one can be animated at a time
def __init__(self, rect, border_tol=.15, allow_resize=True,
fixed_aspect_ratio=False):
self.rect = rect
self.border_tol = border_tol
self.allow_resize = allow_resize
self.fixed_aspect_ratio = fixed_aspect_ratio
self.press = None
self.background = None
def connect(self):
'connect to all the events we need'
self.cidpress = self.rect.figure.canvas.mpl_connect(
'button_press_event', self.on_press)
self.cidrelease = self.rect.figure.canvas.mpl_connect(
'button_release_event', self.on_release)
self.cidmotion = self.rect.figure.canvas.mpl_connect(
'motion_notify_event', self.on_motion)
def on_press(self, event):
'on button press we will see if the mouse is over us and store some data'
if event.inaxes != self.rect.axes: return
if DraggableResizeableRectangle.lock is not None: return
contains, attrd = self.rect.contains(event)
if not contains: return
#print 'event contains', self.rect.xy
x0, y0 = self.rect.xy
w0, h0 = self.rect.get_width(), self.rect.get_height()
aspect_ratio = np.true_divide(w0, h0)
self.press = x0, y0, w0, h0, aspect_ratio, event.xdata, event.ydata
DraggableResizeableRectangle.lock = self
# draw everything but the selected rectangle and store the pixel buffer
canvas = self.rect.figure.canvas
axes = self.rect.axes
self.rect.set_animated(True)
canvas.draw()
self.background = canvas.copy_from_bbox(self.rect.axes.bbox)
# now redraw just the rectangle
axes.draw_artist(self.rect)
# and blit just the redrawn area
canvas.blit(axes.bbox)
def on_motion(self, event):
'on motion we will move the rect if the mouse is over us'
if DraggableResizeableRectangle.lock is not self:
return
if event.inaxes != self.rect.axes: return
x0, y0, w0, h0, aspect_ratio, xpress, ypress = self.press
self.dx = event.xdata - xpress
self.dy = event.ydata - ypress
self.update_rect()
canvas = self.rect.figure.canvas
axes = self.rect.axes
# restore the background region
canvas.restore_region(self.background)
# redraw just the current rectangle
axes.draw_artist(self.rect)
# blit just the redrawn area
canvas.blit(axes.bbox)
self.updateXY += 1
def on_release(self, event):
'on release we reset the press data'
if DraggableResizeableRectangle.lock is not self:
return
self.press = None
DraggableResizeableRectangle.lock = None
# turn off the rect animation property and reset the background
self.rect.set_animated(False)
self.background = None
self.updateText += 1
# redraw the full figure
self.rect.figure.canvas.draw()
self.released += 1
def disconnect(self):
'disconnect all the stored connection ids'
self.rect.figure.canvas.mpl_disconnect(self.cidpress)
self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
def update_rect(self):
x0, y0, w0, h0, aspect_ratio, xpress, ypress = self.press
dx, dy = self.dx, self.dy
bt = self.border_tol
fixed_ar = self.fixed_aspect_ratio
if (not self.allow_resize or
(abs(x0+np.true_divide(w0,2)-xpress)<np.true_divide(w0,2)-bt*w0 and
abs(y0+np.true_divide(h0,2)-ypress)<np.true_divide(h0,2)-bt*h0)):
self.rect.set_x(x0+dx)
self.rect.set_y(y0+dy)
elif abs(x0-xpress)<bt*w0:
self.rect.set_x(x0+dx)
self.rect.set_width(w0-dx)
if fixed_ar:
dy = np.true_divide(dx, aspect_ratio)
self.rect.set_y(y0+dy)
self.rect.set_height(h0-dy)
elif abs(x0+w0-xpress)<bt*w0:
self.rect.set_width(w0+dx)
if fixed_ar:
dy = np.true_divide(dx, aspect_ratio)
self.rect.set_height(h0+dy)
elif abs(y0-ypress)<bt*h0:
self.rect.set_y(y0+dy)
self.rect.set_height(h0-dy)
if fixed_ar:
dx = dy*aspect_ratio
self.rect.set_x(x0+dx)
self.rect.set_width(w0-dx)
elif abs(y0+h0-ypress)<bt*h0:
self.rect.set_height(h0+dy)
if fixed_ar:
dx = dy*aspect_ratio
self.rect.set_width(w0+dx)
class AnnotatedRectangle(HasTraits):
rectangle = Instance(mpatches.Rectangle)
axes = Instance(matplotlib.axes.Axes)
annotext = Instance(matplotlib.text.Text)
text = Str()
drr = Instance(DraggableResizeableRectangle)
rectUpdated = Int(0)
rectReleased = Int(0)
def __init__(self, axes, x1, y1, x2, y2, text, color='c', ecolor='k', alpha=0.7):
print("View: AnnotatedRectangle created")
super(AnnotatedRectangle, self).__init__()
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
# Rectangle Workaround, because it's not draggable when selecting from top
if x1 > x2:
temp = x2
x2 = x1
x1 = temp
if y1 > y2:
temp = y2
y2 = y1
y1 = temp
xto = x2 - x1
yto = y2 - y1
rectangle = mpatches.Rectangle((x1, y1), xto, yto, ec=ecolor, color=color, alpha=alpha)
self.text = text
self.axes = axes
xtext = self.x1 + (self.x2 - self.x1) / 2.
ytext = self.y1 + (self.y2 - self.y1) / 2.
self.axes.add_patch(rectangle)
self.drr = DraggableResizeableRectangle(rectangle)
self.drr.connect()
self.rectangle = self.drr.rect
@on_trait_change('drr.updateText')
def updateText(self):
try:
self.annotext.remove()
except AttributeError:
print("AnnotatedRectangle: Found no annotated text")
x1, y1 = self.drr.rect.get_xy()
x2 = x1 + self.drr.rect.get_width()/2.0
y2 = y1 + self.drr.rect.get_height()/2.0
self.annotext = self.axes.annotate(self.text, (x2, y2), color='w', weight='bold',
fontsize=6, ha='center', va='center')
@on_trait_change('drr.updateXY')
def xyRectUpdated(self):
self.rectUpdated += 1
@on_trait_change('drr.released')
def released(self):
print("AnnotatedRectangle: Rectangle released")
self.x1, self.y1 = self.drr.rect.get_xy()
self.x2 = self.x1+self.drr.rect.get_width()
self.y2 = self.y1+self.drr.rect.get_width()
self.rectReleased += 1
def remove(self):
self.rectangle.remove()
self.annotext.remove()
del self
def get_rect_xy(self):
return self.drr.rect.get_xy()
def get_rect_width(self):
return self.drr.rect.get_width()
def get_rect_height(self):
return self.drr.rect.get_height()
def disconnect(self):
self.drr.disconnect()
def connect(self):
self.drr.connect()
if __name__ == '__main__':
p = mpatches.Rectangle((0.5, 0.5), 0.2, 0.2, ec="k", color='c', alpha=0.7)
d = DraggableResizeableRectangle(p)
d.configure_traits()