source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/binder.py @ c65a265

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c65a265 was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Remaining modules refactored

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