Ignore:
Timestamp:
Oct 11, 2018 2:20:56 PM (12 months ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1249
Children:
98b9f32
Parents:
67ed543
Message:

improved support for py37 in sasgui

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/guiframe/local_perspectives/plotting/binder.py

    r20fa5fe r5251ec6  
    22Extension to MPL to support the binding of artists to key/mouse events. 
    33""" 
     4from __future__ import print_function 
     5 
     6import sys 
    47import logging 
    5 import sys 
    68 
    79logger = logging.getLogger(__name__) 
     
    1618    artist = None 
    1719    prop = {} 
     20 
    1821    def __init__(self, artist=None, prop={}): 
    1922        self.artist, self.prop = artist, self.prop 
     
    2528        return self.artist is not other.artist 
    2629 
    27     def __nonzero__(self): 
     30    def __bool__(self): 
    2831        return self.artist is not None 
     32 
     33    __nonzero__ = __bool__ 
     34 
    2935 
    3036class BindArtist(object): 
    3137    """ 
    32         Track keyboard modifiers for events. 
    33         TODO: Move keyboard modifier support into the backend.  We cannot 
    34         TODO: properly support it from outside the windowing system since there 
    35         TODO: is no way to recognized whether shift is held down when the mouse 
    36         TODO: first clicks on the the application window. 
     38    Track keyboard modifiers for events. 
    3739    """ 
     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. 
    3844    control, shift, alt, meta = False, False, False, False 
    3945 
     
    4652              'scroll', 'key', 'keyup'] 
    4753    # TODO: Need our own event structure 
     54 
    4855    def __init__(self, figure): 
    4956        canvas = figure.canvas 
     57 
    5058        # Link to keyboard/mouse 
    5159        try: 
     
    5967            ] 
    6068        except: 
    61             # print "bypassing scroll_event: wrong matplotlib version" 
     69            logger.warn("bypassing scroll_event: wrong matplotlib version") 
    6270            self._connections = [ 
    6371                canvas.mpl_connect('motion_notify_event', self._onMotion), 
     
    6775                canvas.mpl_connect('key_release_event', self._onKeyRelease), 
    6876            ] 
     77 
    6978        # Turn off picker if it hasn't already been done 
    7079        try: 
    7180            canvas.mpl_disconnect(canvas.button_pick_id) 
    7281            canvas.mpl_disconnect(canvas.scroll_pick_id) 
    73         except: 
    74             logger.error(sys.exc_value) 
     82        except Exception as exc: 
     83            logger.error(exc) 
     84 
     85        self._current = None 
     86        self._actions = {} 
    7587        self.canvas = canvas 
    7688        self.figure = figure 
     
    8395 
    8496        Use clearall() to reset all connections. 
    85  
    8697        """ 
    8798        for h in artists: 
     
    108119        for action in self.events: 
    109120            self._actions[action] = {} 
     121 
    110122        # Need activity state 
    111123        self._artists = [] 
     
    121133            for cid in self._connections: 
    122134                self.canvas.mpl_disconnect(cid) 
    123         except: 
    124             pass 
     135        except Exception as exc: 
     136            logger.error("Error disconnection canvas: %s" % exc) 
    125137        self._connections = [] 
    126138 
     
    169181        sure it applies.  E.g., the callback for 'press' might be: 
    170182            if event.button == 1 and event.shift: process Shift-click 
    171  
    172         :TODO: Only receive events with the correct modifiers (e.g., S-click, 
    173         :TODO:   or *-click for any modifiers). 
    174         :TODO: Only receive button events for the correct button (e.g., click1 
    175         :TODO:   release3, or dclick* for any button) 
    176         :TODO: Support virtual artist, so that and artist can be flagged as 
    177         :TODO:   having a tag list and receive the correct events 
    178         :TODO: Support virtual events for binding to button-3 vs shift button-1 
    179         :TODO:   without changing callback code 
    180         :TODO: Attach multiple callbacks to the same event? 
    181         :TODO: Clean up interaction with toolbar modes 
    182         :TODO: push/pushclear/pop context so that binding changes 
    183             for the duration 
    184         :TODO:   e.g., to support ? context sensitive help 
    185  
    186         """ 
     183        """ 
     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 
    187197        # Check that the trigger is valid 
    188198        if trigger not in self._actions: 
    189             raise ValueError, "%s invalid --- valid triggers are %s" \ 
    190                 % (trigger, ", ".join(self.events)) 
     199            raise ValueError("%s invalid --- valid triggers are %s" 
     200                % (trigger, ", ".join(self.events))) 
     201 
    191202        # Register the trigger callback 
    192203        self._actions[trigger][artist] = action 
    193         # print "==> added",artist,[artist],"to",trigger,":", 
    194         # self._actions[trigger].keys() 
     204 
    195205        # Maintain a list of all artists 
    196206        if artist not in self._artists: 
     
    203213        """ 
    204214        if action not in self.events: 
    205             raise ValueError, "Trigger expects " + ", ".join(self.events) 
     215            raise ValueError("Trigger expects " + ", ".join(self.events)) 
     216 
    206217        # Tag the event with modifiers 
    207218        for mod in ('alt', 'control', 'shift', 'meta'): 
     
    210221        setattr(ev, 'action', action) 
    211222        setattr(ev, 'prop', {}) 
     223 
    212224        # Fallback scheme. If the event does not return false, pass to parent. 
    213225        processed = False 
     
    233245        """ 
    234246        # TODO: sort by zorder of axes then by zorder within axes 
    235         self._artists.sort(cmp=lambda x, y: cmp(y.zorder, x.zorder)) 
    236         # print "search"," ".join([str(h) for h in self._artists]) 
     247        self._artists.sort(key=lambda x: x.zorder, reverse=True) 
    237248        found = Selection() 
    238         # print "searching in",self._artists 
    239249        for artist in self._artists: 
    240250            # TODO: should contains() return false if invisible? 
     
    250260                found.artist, found.prop = artist, prop 
    251261                break 
    252         # print "found",found.artist 
    253262 
    254263        # TODO: how to check if prop is equal? 
     
    257266            self.trigger(found, 'enter', event) 
    258267        self._current = found 
     268 
    259269        return found 
    260270 
     
    274284 
    275285            transform = self._hasclick.artist.get_transform() 
    276             # x,y = event.xdata,event.ydata 
     286            #x,y = event.xdata,event.ydata 
    277287            x, y = event.x, event.y 
    278288            try: 
    279289                x, y = transform.inverted().transform_point((x, y)) 
    280  
    281             except: 
     290            except: # CRUFT: matplotlib-0.91 support 
    282291                x, y = transform.inverse_xy_tup((x, y)) 
     292 
    283293            event.xdata, event.ydata = x, y 
    284294            self.trigger(self._hasclick, 'drag', event) 
    285295        else: 
    286296            found = self._find_current(event) 
    287             # print "found",found.artist 
    288297            self.trigger(found, 'motion', event) 
    289298 
     
    296305        # Check for double-click 
    297306        event_time = time.time() 
    298         # print event_time,self._last_time,self.dclick_threshhold 
    299         # print (event_time > self._last_time + self.dclick_threshhold) 
    300         # print event.button,self._last_button 
    301307        if (event.button != self._last_button) or \ 
    302308                (event_time > self._last_time + self.dclick_threshhold): 
     
    381387            setattr(self, event.key, False) 
    382388            return 
     389 
    383390        if self._haskey: 
    384391            self.trigger(self._haskey, 'keyup', event) 
     
    391398        found = self._find_current(event) 
    392399        self.trigger(found, 'scroll', event) 
    393  
Note: See TracChangeset for help on using the changeset viewer.