source: sasview/guitools/BaseInteractor.py @ 8eeb0b6

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 8eeb0b6 was 4972de2, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Updated for interactive graphs.

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