1 | interface_color = 'black' |
---|
2 | disable_color = 'gray' |
---|
3 | active_color = 'red' |
---|
4 | rho_color = 'black' |
---|
5 | mu_color = 'green' |
---|
6 | P_color = 'blue' |
---|
7 | theta_color = 'orange' |
---|
8 | profile_colors = [rho_color, mu_color, P_color, theta_color] |
---|
9 | |
---|
10 | class _BaseInteractor: |
---|
11 | """ |
---|
12 | Share some functions between the interface interactor and various layer |
---|
13 | interactors. |
---|
14 | |
---|
15 | Individual interactors need the following functions: |
---|
16 | |
---|
17 | save(ev) - save the current state for later restore |
---|
18 | restore() - restore the old state |
---|
19 | move(x,y,ev) - move the interactor to position x,y |
---|
20 | moveend(ev) - end the drag event |
---|
21 | update() - draw the interactors |
---|
22 | |
---|
23 | The following are provided by the base class: |
---|
24 | |
---|
25 | connect_markers(markers) - register callbacks for all markers |
---|
26 | clear_markers() - remove all items in self.markers |
---|
27 | onHilite(ev) - enter/leave event processing |
---|
28 | onLeave(ev) - enter/leave event processing |
---|
29 | onClick(ev) - mouse click: calls save() |
---|
30 | onRelease(ev) - mouse click ends: calls moveend() |
---|
31 | onDrag(ev) - mouse move: calls move() or restore() |
---|
32 | onKey(ev) - keyboard move: calls move() or restore() |
---|
33 | |
---|
34 | Interactor attributes: |
---|
35 | |
---|
36 | base - model we are operating on |
---|
37 | axes - axes holding the interactor |
---|
38 | color - color of the interactor in non-active state |
---|
39 | markers - list of handles for the interactor |
---|
40 | """ |
---|
41 | def __init__(self,base,axes,color='black'): |
---|
42 | self.base = base |
---|
43 | self.axes = axes |
---|
44 | self.color = color |
---|
45 | |
---|
46 | def clear_markers(self): |
---|
47 | ''' |
---|
48 | Clear old markers and interfaces. |
---|
49 | ''' |
---|
50 | for h in self.markers: h.remove() |
---|
51 | if self.markers: self.base.connect.clear(*self.markers) |
---|
52 | self.markers = [] |
---|
53 | |
---|
54 | def save(self, ev): |
---|
55 | pass |
---|
56 | def restore(self, ev): |
---|
57 | pass |
---|
58 | def move(self, x, y, ev): |
---|
59 | pass |
---|
60 | def moveend(self, ev): |
---|
61 | pass |
---|
62 | |
---|
63 | def connect_markers(self,markers): |
---|
64 | """ |
---|
65 | Connect markers to callbacks |
---|
66 | """ |
---|
67 | |
---|
68 | for h in markers: |
---|
69 | connect = self.base.connect |
---|
70 | connect('enter', h, self.onHilite) |
---|
71 | connect('leave', h, self.onLeave) |
---|
72 | connect('click', h, self.onClick) |
---|
73 | connect('release', h, self.onRelease) |
---|
74 | connect('drag', h, self.onDrag) |
---|
75 | connect('key', h, self.onKey) |
---|
76 | |
---|
77 | def onHilite(self, ev): |
---|
78 | """ |
---|
79 | Hilite the artist reporting the event, indicating that it is |
---|
80 | ready to receive a click. |
---|
81 | """ |
---|
82 | ev.artist.set_color(active_color) |
---|
83 | self.base.draw() |
---|
84 | return True |
---|
85 | |
---|
86 | def onLeave(self, ev): |
---|
87 | """ |
---|
88 | Restore the artist to the original colour when the cursor leaves. |
---|
89 | """ |
---|
90 | ev.artist.set_color(self.color) |
---|
91 | self.base.draw() |
---|
92 | return True |
---|
93 | |
---|
94 | def onClick(self, ev): |
---|
95 | """ |
---|
96 | Prepare to move the artist. Calls save() to preserve the state for |
---|
97 | later restore(). |
---|
98 | """ |
---|
99 | self.clickx,self.clicky = ev.xdata,ev.ydata |
---|
100 | self.save(ev) |
---|
101 | return True |
---|
102 | |
---|
103 | def onRelease(self, ev): |
---|
104 | self.moveend(ev) |
---|
105 | return True |
---|
106 | |
---|
107 | def onDrag(self, ev): |
---|
108 | """ |
---|
109 | Move the artist. Calls move() to update the state, or restore() if |
---|
110 | the mouse leaves the window. |
---|
111 | """ |
---|
112 | inside,prop = self.axes.contains(ev) |
---|
113 | if inside: |
---|
114 | self.clickx,self.clicky = ev.xdata,ev.ydata |
---|
115 | self.move(ev.xdata,ev.ydata,ev) |
---|
116 | else: |
---|
117 | self.restore() |
---|
118 | self.base.update() |
---|
119 | return True |
---|
120 | |
---|
121 | def onKey(self, ev): |
---|
122 | ''' |
---|
123 | Respond to keyboard events. Arrow keys move the widget. Escape |
---|
124 | restores it to the position before the last click. |
---|
125 | |
---|
126 | Calls move() to update the state. Calls restore() on escape. |
---|
127 | ''' |
---|
128 | if ev.key == 'escape': |
---|
129 | self.restore() |
---|
130 | elif ev.key in ['up', 'down', 'right', 'left']: |
---|
131 | dx,dy = self.dpixel(self.clickx,self.clicky,nudge=ev.control) |
---|
132 | if ev.key == 'up': self.clicky += dy |
---|
133 | elif ev.key == 'down': self.clicky -= dy |
---|
134 | elif ev.key == 'right': self.clickx += dx |
---|
135 | else: self.clickx -= dx |
---|
136 | self.move(self.clickx,self.clicky,ev) |
---|
137 | else: |
---|
138 | return False |
---|
139 | self.base.update() |
---|
140 | return True |
---|
141 | |
---|
142 | def dpixel(self,x,y,nudge=False): |
---|
143 | ''' |
---|
144 | Return the step size in data coordinates for a small |
---|
145 | step in screen coordinates. If nudge is False (default) |
---|
146 | the step size is one pixel. If nudge is True, the step |
---|
147 | size is 0.2 pixels. |
---|
148 | ''' |
---|
149 | ax = self.axes |
---|
150 | px,py = ax.transData.inverse_xy_tup((x,y)) |
---|
151 | if nudge: |
---|
152 | nx,ny = ax.transData.xy_tup((px+0.2,py+0.2)) |
---|
153 | else: |
---|
154 | nx,ny = ax.transData.xy_tup((px+1.,py+1.)) |
---|
155 | dx,dy = nx-x,ny-y |
---|
156 | return dx,dy |
---|
157 | |
---|