-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditable_rectangle_.py
316 lines (235 loc) · 10 KB
/
editable_rectangle_.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 16 23:25:03 2018
@author: evgeniy
"""
# draggable rectangle with the animation blit techniques; see
# http://www.scipy.org/Cookbook/Matplotlib/Animations
from matplotlib.patches import Rectangle
class RectangleBasic(object):
def __init__(self, rect_big, mu, nu,
color_mu=None, color_nu=None, alpha=0.5):
self.rect = rect_big
self.axes = self.rect.axes
# self.rect.axes.set_ylim([0,1])
x, y = rect_big.get_xy()
height = rect_big.get_height()
width = rect_big.get_width()
color_mu = "blue" if color_mu is None else color_mu
self.rect_mu = Rectangle((x,y), width, mu, color=color_mu, alpha=alpha)
self.axes.add_patch(self.rect_mu)
color_nu = "green" if color_nu is None else color_nu
self.rect_nu = Rectangle((x,height - nu), width, nu, color=color_nu, alpha=alpha)
self.rect.axes.add_patch(self.rect_nu)
class EditableRectangle(RectangleBasic):
lock = None # only one can be animated at a time
def __init__(self, rect_big, mu, nu,
color_mu=None, color_nu=None, alpha=0.5,
companion=None):
super(EditableRectangle, self).__init__(rect_big, mu, nu,
color_mu, color_nu, alpha)
self.companion = companion
self.press_xy = None
self.mu_data = None
self.nu_data = None
self.background = \
self.rect.figure.canvas.copy_from_bbox(self.axes.figure.bbox)
@property
def get_idx(self):
return self.rect.get_x()
@property
def get_mu(self):
return self.rect_mu.get_y()
@property
def get_nu(self):
return self.rect.get_height() - self.rect_nu.get_y()
@property
def get_pi(self):
return self.rect.get_height() - self.get_mu - self.get_mu
## Setters
def set_mu(self, mu):
self.rect_mu.set_height(mu)
# if self.companion is not None:
# self.companion.set_mu(mu)
def set_nu(self, nu):
self.rect_nu.set_y(self.rect.get_height() - nu)
self.rect_nu.set_height(nu)
# if self.companion is not None:
# self.companion.set_nu(nu)
def set_munu(self, munu):
self.set_mu(munu[0])
self.set_nu(munu[1])
def set_data(self, mu, nu):
self.set_munu((mu, nu))
def set_animated(self, value):
self.rect_mu.set_animated(value)
self.rect_nu.set_animated(value)
def disconnect(self):
'disconnect all the stored connection ids'
self.rect.figure.canvas.mpl_disconnect(self.cidpress)
# self.rect.figure.canvas.mpl_disconnect(self.ciddraw)
self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
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.ciddraw = self.rect.figure.canvas.mpl_connect(
# 'draw_event', self.draw_callback)
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 EditableRectangle.lock is not None:
return
contains, attrd = self.rect.contains(event)
if not contains:
return
if self.rect_mu.contains(event)[0]:
self.update_flag = "update_mu"
elif self.rect_nu.contains(event)[0]:
self.update_flag = "update_nu"
else:
self.update_flag = "update_pi"
self.mu_data = self.rect_mu.get_y(), self.rect_mu.get_height()
self.nu_data = self.rect_nu.get_y(), self.rect_nu.get_height()
self.press_xy = event.xdata, event.ydata
EditableRectangle.lock = self
# draw everything but the selected rectangle and store the pixel buffer
canvas = self.rect.figure.canvas
axes = self.rect.axes
self.set_animated(True)
if self.companion is not None:
print("companion of rectangle set animated True")
# self.companion.background = \
# canvas.copy_from_bbox(self.companion.axes.figure.bbox)
self.companion.set_animated(True)
canvas.draw()
self.background = canvas.copy_from_bbox(self.axes.figure.bbox)
# now redraw just the rectangle
axes.draw_artist(self.rect_mu)
axes.draw_artist(self.rect_nu)
# and blit just the redrawn area
canvas.blit(axes.bbox)
if self.companion is not None:
self.companion.background = \
canvas.copy_from_bbox(self.companion.axes.figure.bbox)
self.update_companion()
def draw_callback(self, event):
# self.draw_holder_annotations(self.axes)
self.rect.axes.draw_artist(self.rect_mu)
self.rect.axes.draw_artist(self.rect_nu)
canvas = self.rect.axes.figure.canvas
canvas.blit(self.rect.axes.bbox)
self.background = canvas.copy_from_bbox(self.rect.axes.figure.bbox)
def update_companion(self):
# self.companion.set_mu(self.rect_mu.get_height())
# self.companion.set_nu(1-self.rect_nu.get_y())
self.companion.set_data(self.rect_mu.get_height(),
1-self.rect_nu.get_y())
self.companion.draw_object()
self.companion.axes.figure.canvas.blit(self.companion.axes.bbox)
def update_nu(self, event):
xpress, ypress = self.press_xy
dy = event.ydata - ypress
nu_y, nu_height = self.nu_data
nu_y = max(min(1.0, nu_y + dy), 0.0)
# self.rect_nu.set_y(nu_y)
# self.rect_nu.set_height(self.rect.get_height() - nu_y)
self.set_nu(self.rect.get_height() - nu_y)
mu_height = self.rect_mu.get_height()
if mu_height > nu_y:
self.set_mu(nu_y)
# self.rect_mu.set_height(nu_y)
def update_mu(self, event):
xpress, ypress = self.press_xy
dy = event.ydata - ypress
mu_y, mu_height = self.mu_data
self.set_mu(min(max(0.0, mu_height + dy),
self.rect.get_height()))
nu_y = self.rect_nu.get_y()
mu_height = self.rect_mu.get_height()
if mu_height > nu_y:
self.set_nu(self.rect.get_height() - mu_height)
# self.rect_nu.set_y(mu_height)
# self.rect_nu.set_height(self.rect.get_height() - mu_height)
def update_pi(self, event):
xpress, ypress = self.press_xy
dx = event.xdata - xpress
dy = event.ydata - ypress
mu_y, mu_height = self.mu_data
self.set_mu(max(0.0, mu_height + dy))
# self.rect_mu.set_height(max(0.0, mu_height + dy))
nu_y, nu_height = self.nu_data
# self.rect_nu.set_y(min(1.0, nu_y + dy))
self.set_nu(max(0.0, nu_height - dy))
# self.rect_nu.set_height(max(0.0, nu_height - dy))
def on_motion(self, event):
'on motion we will move the rect if the mouse is over us'
if EditableRectangle.lock is not self:
return
if event.inaxes != self.axes:
return
if self.update_flag == "update_pi":
self.update_pi(event)
elif self.update_flag == "update_mu":
self.update_mu(event)
else:
self.update_nu(event)
canvas = self.rect.figure.canvas
# restore the background region
canvas.restore_region(self.background)
self.draw_object()
# blit just the redrawn area
canvas.blit(self.axes.bbox)
if self.companion is not None:
canvas.restore_region(self.companion.background)
self.update_companion()
def draw_blit(self):
canvas = self.rect.figure.canvas
# restore the background region
canvas.restore_region(self.background)
# blit just the redrawn area
canvas.blit(self.axes.bbox)
def blit(self):
canvas = self.rect.figure.canvas
# restore the background region
canvas.restore_region(self.background)
# blit just the redrawn area
canvas.blit(self.axes.bbox)
def draw_object(self):
self.axes.draw_artist(self.rect_mu)
self.axes.draw_artist(self.rect_nu)
def on_release(self, event):
'on release we reset the press data'
if EditableRectangle.lock is not self:
return
self.press_xy = None
self.mu_xy = None
self.nu_xy = None
EditableRectangle.lock = None
# turn off the rect animation property and reset the background
self.set_animated(False)
if self.companion is not None:
# self.rect.figure.canvas.restore_region(self.companion.background)
# self.update_companion()
self.companion.set_animated(False)
# self.companion.background = None
# self.background = None
# redraw the full figure
self.rect.figure.canvas.draw()
###############################################################################
class EditableRectangleRadius(EditableRectangle):
def update_companion(self):
# self.companion.set_mu(self.rect_mu.get_height())
# self.companion.set_nu(1-self.rect_nu.get_y())
# self.companion.set_data(self.rect_mu.get_height(),
# 1-self.rect_nu.get_y())
self.companion.set_radius(self.rect_mu.get_height())
self.companion.draw_object()
self.companion.axes.figure.canvas.blit(self.companion.axes.bbox)