[a9d5684] | 1 | """ |
---|
| 2 | Extension to MPL to support the binding of artists to key/mouse events. |
---|
| 3 | """ |
---|
[a1b8fee] | 4 | from __future__ import print_function |
---|
| 5 | |
---|
[a9d5684] | 6 | import sys |
---|
[2df0b74] | 7 | import logging |
---|
[a9d5684] | 8 | |
---|
[463e7ffc] | 9 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 10 | |
---|
[2df0b74] | 11 | class Selection(object): |
---|
[a9d5684] | 12 | """ |
---|
| 13 | Store and compare selections. |
---|
| 14 | """ |
---|
| 15 | # TODO: We need some way to check in prop matches, preferably |
---|
| 16 | # TODO: without imposing structure on prop. |
---|
[2df0b74] | 17 | |
---|
[a9d5684] | 18 | artist = None |
---|
| 19 | prop = {} |
---|
[2df0b74] | 20 | |
---|
[a9d5684] | 21 | def __init__(self, artist=None, prop={}): |
---|
| 22 | self.artist, self.prop = artist, self.prop |
---|
[2df0b74] | 23 | |
---|
[a9d5684] | 24 | def __eq__(self, other): |
---|
| 25 | return self.artist is other.artist |
---|
[2df0b74] | 26 | |
---|
[a9d5684] | 27 | def __ne__(self, other): |
---|
| 28 | return self.artist is not other.artist |
---|
[2df0b74] | 29 | |
---|
[5251ec6] | 30 | def __bool__(self): |
---|
[a9d5684] | 31 | return self.artist is not None |
---|
| 32 | |
---|
[5251ec6] | 33 | __nonzero__ = __bool__ |
---|
| 34 | |
---|
[a9d5684] | 35 | |
---|
[2df0b74] | 36 | class BindArtist(object): |
---|
[a9d5684] | 37 | """ |
---|
[5251ec6] | 38 | Track keyboard modifiers for events. |
---|
[a9d5684] | 39 | """ |
---|
| 40 | # TODO: Move keyboard modifier support into the backend. We cannot |
---|
| 41 | # TODO: properly support it from outside the windowing system since there |
---|
| 42 | # TODO: is no way to recognized whether shift is held down when the mouse |
---|
| 43 | # TODO: first clicks on the the application window. |
---|
| 44 | control, shift, alt, meta = False, False, False, False |
---|
| 45 | |
---|
| 46 | # Track doubleclick |
---|
| 47 | dclick_threshhold = 0.25 |
---|
| 48 | _last_button, _last_time = None, 0 |
---|
[2df0b74] | 49 | |
---|
[a9d5684] | 50 | # Mouse/keyboard events we can bind to |
---|
| 51 | events = ['enter', 'leave', 'motion', 'click', 'dclick', 'drag', 'release', |
---|
[2df0b74] | 52 | 'scroll', 'key', 'keyup'] |
---|
[a9d5684] | 53 | # TODO: Need our own event structure |
---|
[2df0b74] | 54 | |
---|
[a9d5684] | 55 | def __init__(self, figure): |
---|
| 56 | canvas = figure.canvas |
---|
| 57 | |
---|
| 58 | # Link to keyboard/mouse |
---|
| 59 | try: |
---|
| 60 | self._connections = [ |
---|
| 61 | canvas.mpl_connect('motion_notify_event', self._onMotion), |
---|
| 62 | canvas.mpl_connect('button_press_event', self._onClick), |
---|
| 63 | canvas.mpl_connect('button_release_event', self._onRelease), |
---|
| 64 | canvas.mpl_connect('key_press_event', self._onKey), |
---|
| 65 | canvas.mpl_connect('key_release_event', self._onKeyRelease), |
---|
| 66 | canvas.mpl_connect('scroll_event', self._onScroll) |
---|
| 67 | ] |
---|
| 68 | except: |
---|
[5251ec6] | 69 | logger.warn("bypassing scroll_event: wrong matplotlib version") |
---|
[a9d5684] | 70 | self._connections = [ |
---|
| 71 | canvas.mpl_connect('motion_notify_event', self._onMotion), |
---|
| 72 | canvas.mpl_connect('button_press_event', self._onClick), |
---|
| 73 | canvas.mpl_connect('button_release_event', self._onRelease), |
---|
| 74 | canvas.mpl_connect('key_press_event', self._onKey), |
---|
| 75 | canvas.mpl_connect('key_release_event', self._onKeyRelease), |
---|
| 76 | ] |
---|
[2df0b74] | 77 | |
---|
[5251ec6] | 78 | # Turn off picker if it hasn't already been done |
---|
| 79 | try: |
---|
| 80 | canvas.mpl_disconnect(canvas.button_pick_id) |
---|
| 81 | canvas.mpl_disconnect(canvas.scroll_pick_id) |
---|
| 82 | except Exception as exc: |
---|
| 83 | logger.error(exc) |
---|
| 84 | |
---|
[a9d5684] | 85 | self._current = None |
---|
| 86 | self._actions = {} |
---|
| 87 | self.canvas = canvas |
---|
| 88 | self.figure = figure |
---|
| 89 | self.clearall() |
---|
[2df0b74] | 90 | |
---|
[a9d5684] | 91 | def clear(self, *artists): |
---|
| 92 | """ |
---|
| 93 | self.clear(h1,h2,...) |
---|
| 94 | Remove connections for artists h1, h2, ... |
---|
[2df0b74] | 95 | |
---|
[a9d5684] | 96 | Use clearall() to reset all connections. |
---|
| 97 | """ |
---|
| 98 | for h in artists: |
---|
| 99 | for a in self.events: |
---|
| 100 | if h in self._actions[a]: |
---|
| 101 | del self._actions[a][h] |
---|
| 102 | if h in self._artists: |
---|
| 103 | self._artists.remove(h) |
---|
| 104 | if self._current.artist in artists: |
---|
| 105 | self._current = Selection() |
---|
| 106 | if self._hasclick.artist in artists: |
---|
| 107 | self._hasclick = Selection() |
---|
| 108 | if self._haskey.artist in artists: |
---|
| 109 | self._haskey = Selection() |
---|
[2df0b74] | 110 | |
---|
[a9d5684] | 111 | def clearall(self): |
---|
| 112 | """ |
---|
| 113 | Clear connections to all artists. |
---|
[2df0b74] | 114 | |
---|
[a9d5684] | 115 | Use clear(h1,h2,...) to reset specific artists. |
---|
| 116 | """ |
---|
| 117 | # Don't monitor any actions |
---|
| 118 | self._actions = {} |
---|
| 119 | for action in self.events: |
---|
| 120 | self._actions[action] = {} |
---|
| 121 | |
---|
| 122 | # Need activity state |
---|
| 123 | self._artists = [] |
---|
| 124 | self._current = Selection() |
---|
| 125 | self._hasclick = Selection() |
---|
| 126 | self._haskey = Selection() |
---|
| 127 | |
---|
| 128 | def disconnect(self): |
---|
| 129 | """ |
---|
| 130 | In case we need to disconnect from the canvas... |
---|
| 131 | """ |
---|
| 132 | try: |
---|
[5251ec6] | 133 | for cid in self._connections: |
---|
| 134 | self.canvas.mpl_disconnect(cid) |
---|
| 135 | except Exception as exc: |
---|
| 136 | logger.error("Error disconnection canvas: %s" % exc) |
---|
[a9d5684] | 137 | self._connections = [] |
---|
| 138 | |
---|
| 139 | def __del__(self): |
---|
| 140 | self.disconnect() |
---|
| 141 | |
---|
| 142 | def __call__(self, trigger, artist, action): |
---|
| 143 | """Register a callback for an artist to a particular trigger event. |
---|
[2df0b74] | 144 | |
---|
[a9d5684] | 145 | usage: |
---|
| 146 | self.connect(eventname,artist,action) |
---|
[2df0b74] | 147 | |
---|
[a9d5684] | 148 | where: |
---|
| 149 | eventname is a string |
---|
| 150 | artist is the particular graph object to respond to the event |
---|
| 151 | action(event,**kw) is called when the event is triggered |
---|
| 152 | |
---|
| 153 | The action callback is associated with particular artists. |
---|
| 154 | Different artists will have different kwargs. See documentation |
---|
| 155 | on the contains() method for each artist. One common properties |
---|
| 156 | are ind for the index of the item under the cursor, which is |
---|
| 157 | returned by Line2D and by collections. |
---|
| 158 | |
---|
| 159 | The following events are supported: |
---|
| 160 | enter: mouse cursor moves into the artist or to a new index |
---|
| 161 | leave: mouse cursor leaves the artist |
---|
| 162 | click: mouse button pressed on the artist |
---|
| 163 | drag: mouse button pressed on the artist and cursor moves |
---|
| 164 | release: mouse button released for the artist |
---|
| 165 | key: key pressed when mouse is on the artist |
---|
| 166 | keyrelease: key released for the artist |
---|
[2df0b74] | 167 | |
---|
[a9d5684] | 168 | The event received by action has a number of attributes: |
---|
| 169 | name is the event name which was triggered |
---|
| 170 | artist is the object which triggered the event |
---|
| 171 | x,y are the screen coordinates of the mouse |
---|
| 172 | xdata,ydata are the graph coordinates of the mouse |
---|
| 173 | button is the mouse button being pressed/released |
---|
| 174 | key is the key being pressed/released |
---|
| 175 | shift,control,alt,meta are flags which are true if the |
---|
| 176 | corresponding key is pressed at the time of the event. |
---|
| 177 | details is a dictionary of artist specific details, such as the |
---|
| 178 | id(s) of the point that were clicked. |
---|
[2df0b74] | 179 | |
---|
[a9d5684] | 180 | When receiving an event, first check the modifier state to be |
---|
| 181 | sure it applies. E.g., the callback for 'press' might be: |
---|
| 182 | if event.button == 1 and event.shift: process Shift-click |
---|
| 183 | """ |
---|
[5251ec6] | 184 | #TODO: Only receive events with the correct modifiers (e.g., S-click, |
---|
| 185 | #TODO: or *-click for any modifiers). |
---|
| 186 | #TODO: Only receive button events for the correct button (e.g., click1 |
---|
| 187 | #TODO: release3, or dclick* for any button) |
---|
| 188 | #TODO: Support virtual artist, so that and artist can be flagged as |
---|
| 189 | #TODO: having a tag list and receive the correct events |
---|
| 190 | #TODO: Support virtual events for binding to button-3 vs shift button-1 |
---|
| 191 | #TODO: without changing callback code |
---|
| 192 | #TODO: Attach multiple callbacks to the same event? |
---|
| 193 | #TODO: Clean up interaction with toolbar modes |
---|
| 194 | #TODO: push/pushclear/pop context so that binding changes for the duration |
---|
| 195 | #TODO: e.g., to support ? context sensitive help |
---|
| 196 | |
---|
[a9d5684] | 197 | # Check that the trigger is valid |
---|
| 198 | if trigger not in self._actions: |
---|
[5251ec6] | 199 | raise ValueError("%s invalid --- valid triggers are %s" |
---|
| 200 | % (trigger, ", ".join(self.events))) |
---|
[a9d5684] | 201 | |
---|
| 202 | # Register the trigger callback |
---|
| 203 | self._actions[trigger][artist] = action |
---|
| 204 | |
---|
| 205 | # Maintain a list of all artists |
---|
| 206 | if artist not in self._artists: |
---|
| 207 | self._artists.append(artist) |
---|
| 208 | |
---|
| 209 | def trigger(self, actor, action, ev): |
---|
| 210 | """ |
---|
| 211 | Trigger a particular event for the artist. Fallback to axes, |
---|
| 212 | to figure, and to 'all' if the event is not processed. |
---|
| 213 | """ |
---|
| 214 | if action not in self.events: |
---|
[5251ec6] | 215 | raise ValueError("Trigger expects " + ", ".join(self.events)) |
---|
[2df0b74] | 216 | |
---|
[a9d5684] | 217 | # Tag the event with modifiers |
---|
| 218 | for mod in ('alt', 'control', 'shift', 'meta'): |
---|
| 219 | setattr(ev, mod, getattr(self, mod)) |
---|
| 220 | setattr(ev, 'artist', None) |
---|
| 221 | setattr(ev, 'action', action) |
---|
| 222 | setattr(ev, 'prop', {}) |
---|
[2df0b74] | 223 | |
---|
[5251ec6] | 224 | # Fallback scheme. If the event does not return false, pass to parent. |
---|
[a9d5684] | 225 | processed = False |
---|
| 226 | artist, prop = actor.artist, actor.prop |
---|
| 227 | if artist in self._actions[action]: |
---|
| 228 | ev.artist, ev.prop = artist, prop |
---|
| 229 | processed = self._actions[action][artist](ev) |
---|
| 230 | if not processed and ev.inaxes in self._actions[action]: |
---|
| 231 | ev.artist, ev.prop = ev.inaxes, {} |
---|
| 232 | processed = self._actions[action][ev.inaxes](ev) |
---|
| 233 | if not processed and self.figure in self._actions[action]: |
---|
| 234 | ev.artist, ev.prop = self.figure, {} |
---|
| 235 | processed = self._actions[action][self.figure](ev) |
---|
| 236 | if not processed and 'all' in self._actions[action]: |
---|
| 237 | ev.artist, ev.prop = None, {} |
---|
| 238 | processed = self._actions[action]['all'](ev) |
---|
| 239 | return processed |
---|
| 240 | |
---|
| 241 | def _find_current(self, event): |
---|
| 242 | """ |
---|
| 243 | Find the artist who will receive the event. Only search |
---|
| 244 | registered artists. All others are invisible to the mouse. |
---|
| 245 | """ |
---|
| 246 | # TODO: sort by zorder of axes then by zorder within axes |
---|
[5251ec6] | 247 | self._artists.sort(key=lambda x: x.zorder, reverse=True) |
---|
[a9d5684] | 248 | found = Selection() |
---|
| 249 | for artist in self._artists: |
---|
| 250 | # TODO: should contains() return false if invisible? |
---|
| 251 | if not artist.get_visible(): |
---|
| 252 | continue |
---|
| 253 | # TODO: optimization - exclude artists not inaxes |
---|
| 254 | try: |
---|
| 255 | inside, prop = artist.contains(event) |
---|
| 256 | except: |
---|
| 257 | # Probably an old version of matplotlib |
---|
| 258 | inside = False |
---|
| 259 | if inside: |
---|
| 260 | found.artist, found.prop = artist, prop |
---|
| 261 | break |
---|
[2df0b74] | 262 | |
---|
[a9d5684] | 263 | # TODO: how to check if prop is equal? |
---|
| 264 | if found != self._current: |
---|
| 265 | self.trigger(self._current, 'leave', event) |
---|
| 266 | self.trigger(found, 'enter', event) |
---|
| 267 | self._current = found |
---|
| 268 | |
---|
| 269 | return found |
---|
[2df0b74] | 270 | |
---|
[a9d5684] | 271 | def _onMotion(self, event): |
---|
| 272 | """ |
---|
| 273 | Track enter/leave/motion through registered artists; all |
---|
| 274 | other artists are invisible. |
---|
| 275 | """ |
---|
[2df0b74] | 276 | # # Can't kill double-click on motion since Windows produces |
---|
| 277 | # # spurious motion events. |
---|
| 278 | # self._last_button = None |
---|
| 279 | |
---|
[a9d5684] | 280 | # Dibs on the motion event for the clicked artist |
---|
| 281 | if self._hasclick: |
---|
| 282 | # Make sure the x,y data use the coordinate system of the |
---|
| 283 | # artist rather than the default axes coordinates. |
---|
[2df0b74] | 284 | |
---|
[a9d5684] | 285 | transform = self._hasclick.artist.get_transform() |
---|
[5251ec6] | 286 | #x,y = event.xdata,event.ydata |
---|
[a9d5684] | 287 | x, y = event.x, event.y |
---|
| 288 | try: |
---|
| 289 | if transform.__class__.__name__ == "IdentityTransform": |
---|
| 290 | x, y = transform.inverted().transform((x, y)) |
---|
| 291 | else: |
---|
[2df0b74] | 292 | # # For interactive plottable apply transform is not working |
---|
| 293 | # # don't know why maybe marker definition |
---|
| 294 | # #transform ="CompositeGenericTransform" crash |
---|
[a9d5684] | 295 | pass |
---|
[5251ec6] | 296 | except: # CRUFT: matplotlib-0.91 support |
---|
[2df0b74] | 297 | # # exception for transform ="CompositeGenericTransform" |
---|
| 298 | # # crashes also here |
---|
[a9d5684] | 299 | x, y = transform.inverse_xy_tup((x, y)) |
---|
| 300 | |
---|
[5251ec6] | 301 | #TODO: why compute (x,y) if it isn't being used? |
---|
| 302 | #event.xdata, event.ydata = x, y |
---|
[a9d5684] | 303 | self.trigger(self._hasclick, 'drag', event) |
---|
| 304 | else: |
---|
| 305 | found = self._find_current(event) |
---|
| 306 | self.trigger(found, 'motion', event) |
---|
| 307 | |
---|
| 308 | def _onClick(self, event): |
---|
| 309 | """ |
---|
| 310 | Process button click |
---|
| 311 | """ |
---|
| 312 | import time |
---|
[2df0b74] | 313 | |
---|
[a9d5684] | 314 | # Check for double-click |
---|
| 315 | event_time = time.time() |
---|
| 316 | if (event.button != self._last_button) or \ |
---|
| 317 | (event_time > self._last_time + self.dclick_threshhold): |
---|
| 318 | action = 'click' |
---|
| 319 | else: |
---|
| 320 | action = 'dclick' |
---|
| 321 | self._last_button = event.button |
---|
| 322 | self._last_time = event_time |
---|
[2df0b74] | 323 | |
---|
[a9d5684] | 324 | # If an artist is already dragging, feed any additional button |
---|
| 325 | # presses to that artist. |
---|
| 326 | # TODO: do we want to force a single button model on the user? |
---|
| 327 | # TODO: that is, once a button is pressed, no other buttons |
---|
| 328 | # TODO: can come through? I think this belongs in canvas, not here. |
---|
| 329 | if self._hasclick: |
---|
| 330 | found = self._hasclick |
---|
| 331 | else: |
---|
| 332 | found = self._find_current(event) |
---|
[2df0b74] | 333 | # print "button %d pressed"%event.button |
---|
[a9d5684] | 334 | # Note: it seems like if "click" returns False then hasclick should |
---|
| 335 | # not be set. The problem is that there are two reasons it can |
---|
| 336 | # return false: because there is no click action for this artist |
---|
| 337 | # or because the click action returned false. A related problem |
---|
| 338 | # is that click actions will go to the canvas if there is no click |
---|
| 339 | # action for the artist, even if the artist has a drag. I'll leave |
---|
| 340 | # it to future maintainers to sort out this problem. For now the |
---|
| 341 | # recommendation is that users should define click if they have |
---|
| 342 | # drag or release on the artist. |
---|
| 343 | self.trigger(found, action, event) |
---|
| 344 | self._hasclick = found |
---|
| 345 | |
---|
| 346 | def _onDClick(self, event): |
---|
| 347 | """ |
---|
| 348 | Process button double click |
---|
| 349 | """ |
---|
| 350 | # If an artist is already dragging, feed any additional button |
---|
| 351 | # presses to that artist. |
---|
| 352 | # TODO: do we want to force a single button model on the user? |
---|
| 353 | # TODO: that is, once a button is pressed, no other buttons |
---|
| 354 | # TODO: can come through? I think this belongs in canvas, not here. |
---|
| 355 | if self._hasclick: |
---|
| 356 | found = self._hasclick |
---|
| 357 | else: |
---|
| 358 | found = self._find_current(event) |
---|
| 359 | self.trigger(found, 'dclick', event) |
---|
| 360 | self._hasclick = found |
---|
| 361 | |
---|
| 362 | def _onRelease(self, event): |
---|
| 363 | """ |
---|
| 364 | Process release release |
---|
| 365 | """ |
---|
| 366 | self.trigger(self._hasclick, 'release', event) |
---|
| 367 | self._hasclick = Selection() |
---|
[2df0b74] | 368 | |
---|
[a9d5684] | 369 | def _onKey(self, event): |
---|
| 370 | """ |
---|
| 371 | Process key click |
---|
| 372 | """ |
---|
| 373 | # TODO: Do we really want keyboard focus separate from mouse focus? |
---|
| 374 | # TODO: Do we need an explicit focus command for keyboard? |
---|
| 375 | # TODO: Can we tab between items? |
---|
[20fa5fe] | 376 | # TODO: How do unhandled events get propagated to axes, figure and |
---|
[a9d5684] | 377 | # TODO: finally to application? Do we need to implement a full tags |
---|
| 378 | # TODO: architecture a la Tk? |
---|
| 379 | # TODO: Do modifiers cause a grab? Does the artist see the modifiers? |
---|
| 380 | if event.key in ('alt', 'meta', 'control', 'shift'): |
---|
| 381 | setattr(self, event.key, True) |
---|
| 382 | return |
---|
| 383 | |
---|
| 384 | if self._haskey: |
---|
| 385 | found = self._haskey |
---|
| 386 | else: |
---|
| 387 | found = self._find_current(event) |
---|
| 388 | self.trigger(found, 'key', event) |
---|
| 389 | self._haskey = found |
---|
[2df0b74] | 390 | |
---|
[a9d5684] | 391 | def _onKeyRelease(self, event): |
---|
| 392 | """ |
---|
| 393 | Process key release |
---|
| 394 | """ |
---|
| 395 | if event.key in ('alt', 'meta', 'control', 'shift'): |
---|
| 396 | setattr(self, event.key, False) |
---|
| 397 | return |
---|
[2df0b74] | 398 | |
---|
[a9d5684] | 399 | if self._haskey: |
---|
| 400 | self.trigger(self._haskey, 'keyup', event) |
---|
| 401 | self._haskey = Selection() |
---|
| 402 | |
---|
| 403 | def _onScroll(self, event): |
---|
| 404 | """ |
---|
| 405 | Process scroll event |
---|
| 406 | """ |
---|
| 407 | found = self._find_current(event) |
---|
| 408 | self.trigger(found, 'scroll', event) |
---|