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