[b06ef8c] | 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 |
---|
[83f4445] | 40 | |
---|
[b06ef8c] | 41 | """ |
---|
| 42 | def __init__(self,base,axes,color='black'): |
---|
[83f4445] | 43 | """ |
---|
| 44 | """ |
---|
[b06ef8c] | 45 | self.base = base |
---|
| 46 | self.axes = axes |
---|
| 47 | self.color = color |
---|
| 48 | |
---|
| 49 | def clear_markers(self): |
---|
[83f4445] | 50 | """ |
---|
[b06ef8c] | 51 | Clear old markers and interfaces. |
---|
[83f4445] | 52 | """ |
---|
[b06ef8c] | 53 | for h in self.markers: h.remove() |
---|
| 54 | if self.markers: self.base.connect.clear(*self.markers) |
---|
| 55 | self.markers = [] |
---|
| 56 | |
---|
| 57 | def save(self, ev): |
---|
[83f4445] | 58 | """ |
---|
| 59 | """ |
---|
[b06ef8c] | 60 | pass |
---|
[83f4445] | 61 | |
---|
[b06ef8c] | 62 | def restore(self, ev): |
---|
[83f4445] | 63 | """ |
---|
| 64 | """ |
---|
[b06ef8c] | 65 | pass |
---|
[83f4445] | 66 | |
---|
[b06ef8c] | 67 | def move(self, x, y, ev): |
---|
[83f4445] | 68 | """ |
---|
| 69 | """ |
---|
[b06ef8c] | 70 | pass |
---|
[83f4445] | 71 | |
---|
[b06ef8c] | 72 | def moveend(self, ev): |
---|
[83f4445] | 73 | """ |
---|
| 74 | """ |
---|
[b06ef8c] | 75 | pass |
---|
| 76 | |
---|
| 77 | def connect_markers(self,markers): |
---|
| 78 | """ |
---|
| 79 | Connect markers to callbacks |
---|
| 80 | """ |
---|
| 81 | |
---|
| 82 | for h in markers: |
---|
| 83 | connect = self.base.connect |
---|
| 84 | connect('enter', h, self.onHilite) |
---|
| 85 | connect('leave', h, self.onLeave) |
---|
| 86 | connect('click', h, self.onClick) |
---|
| 87 | connect('release', h, self.onRelease) |
---|
| 88 | connect('drag', h, self.onDrag) |
---|
| 89 | connect('key', h, self.onKey) |
---|
| 90 | |
---|
| 91 | def onHilite(self, ev): |
---|
| 92 | """ |
---|
| 93 | Hilite the artist reporting the event, indicating that it is |
---|
| 94 | ready to receive a click. |
---|
| 95 | """ |
---|
| 96 | ev.artist.set_color(active_color) |
---|
| 97 | self.base.draw() |
---|
| 98 | return True |
---|
| 99 | |
---|
| 100 | def onLeave(self, ev): |
---|
| 101 | """ |
---|
| 102 | Restore the artist to the original colour when the cursor leaves. |
---|
| 103 | """ |
---|
| 104 | ev.artist.set_color(self.color) |
---|
| 105 | self.base.draw() |
---|
| 106 | return True |
---|
| 107 | |
---|
| 108 | def onClick(self, ev): |
---|
| 109 | """ |
---|
| 110 | Prepare to move the artist. Calls save() to preserve the state for |
---|
| 111 | later restore(). |
---|
| 112 | """ |
---|
| 113 | self.clickx,self.clicky = ev.xdata,ev.ydata |
---|
| 114 | self.save(ev) |
---|
| 115 | return True |
---|
| 116 | |
---|
| 117 | def onRelease(self, ev): |
---|
[83f4445] | 118 | """ |
---|
| 119 | """ |
---|
[b06ef8c] | 120 | self.moveend(ev) |
---|
| 121 | return True |
---|
| 122 | |
---|
| 123 | def onDrag(self, ev): |
---|
| 124 | """ |
---|
| 125 | Move the artist. Calls move() to update the state, or restore() if |
---|
| 126 | the mouse leaves the window. |
---|
| 127 | """ |
---|
| 128 | inside,prop = self.axes.contains(ev) |
---|
| 129 | if inside: |
---|
| 130 | self.clickx,self.clicky = ev.xdata,ev.ydata |
---|
| 131 | self.move(ev.xdata,ev.ydata,ev) |
---|
| 132 | else: |
---|
| 133 | self.restore() |
---|
| 134 | self.base.update() |
---|
| 135 | return True |
---|
| 136 | |
---|
| 137 | def onKey(self, ev): |
---|
[83f4445] | 138 | """ |
---|
[b06ef8c] | 139 | Respond to keyboard events. Arrow keys move the widget. Escape |
---|
| 140 | restores it to the position before the last click. |
---|
| 141 | |
---|
| 142 | Calls move() to update the state. Calls restore() on escape. |
---|
[83f4445] | 143 | """ |
---|
[b06ef8c] | 144 | if ev.key == 'escape': |
---|
| 145 | self.restore() |
---|
| 146 | elif ev.key in ['up', 'down', 'right', 'left']: |
---|
| 147 | dx,dy = self.dpixel(self.clickx,self.clicky,nudge=ev.control) |
---|
| 148 | if ev.key == 'up': self.clicky += dy |
---|
| 149 | elif ev.key == 'down': self.clicky -= dy |
---|
| 150 | elif ev.key == 'right': self.clickx += dx |
---|
| 151 | else: self.clickx -= dx |
---|
| 152 | self.move(self.clickx,self.clicky,ev) |
---|
| 153 | else: |
---|
| 154 | return False |
---|
| 155 | self.base.update() |
---|
| 156 | return True |
---|
| 157 | |
---|
| 158 | def dpixel(self,x,y,nudge=False): |
---|
[83f4445] | 159 | """ |
---|
[b06ef8c] | 160 | Return the step size in data coordinates for a small |
---|
| 161 | step in screen coordinates. If nudge is False (default) |
---|
| 162 | the step size is one pixel. If nudge is True, the step |
---|
| 163 | size is 0.2 pixels. |
---|
[83f4445] | 164 | """ |
---|
[b06ef8c] | 165 | ax = self.axes |
---|
| 166 | px,py = ax.transData.inverse_xy_tup((x,y)) |
---|
| 167 | if nudge: |
---|
| 168 | nx,ny = ax.transData.xy_tup((px+0.2,py+0.2)) |
---|
| 169 | else: |
---|
| 170 | nx,ny = ax.transData.xy_tup((px+1.,py+1.)) |
---|
| 171 | dx,dy = nx-x,ny-y |
---|
| 172 | return dx,dy |
---|
| 173 | |
---|