source: sasview/guiframe/local_perspectives/plotting/BaseInteractor.py @ 383eeaa

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 383eeaa was b06ef8c, checked in by Gervaise Alina <gervyh@…>, 16 years ago

sector averaging

  • Property mode set to 100644
File size: 5.0 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       
68        for h in markers:
69            connect = self.base.connect
70            connect('enter', h, self.onHilite)
71            connect('leave', h, self.onLeave)
72            connect('click', h, self.onClick)
73            connect('release', h, self.onRelease)
74            connect('drag', h, self.onDrag)
75            connect('key', h, self.onKey)
76
77    def onHilite(self, ev):
78        """
79        Hilite the artist reporting the event, indicating that it is
80        ready to receive a click.
81        """
82        ev.artist.set_color(active_color)
83        self.base.draw()
84        return True
85
86    def onLeave(self, ev):
87        """
88        Restore the artist to the original colour when the cursor leaves.
89        """
90        ev.artist.set_color(self.color)
91        self.base.draw()
92        return True
93           
94    def onClick(self, ev):
95        """
96        Prepare to move the artist.  Calls save() to preserve the state for
97        later restore().
98        """
99        self.clickx,self.clicky = ev.xdata,ev.ydata
100        self.save(ev)
101        return True
102
103    def onRelease(self, ev):
104        self.moveend(ev)
105        return True
106
107    def onDrag(self, ev):
108        """
109        Move the artist.  Calls move() to update the state, or restore() if
110        the mouse leaves the window.
111        """
112        inside,prop = self.axes.contains(ev)
113        if inside:
114            self.clickx,self.clicky = ev.xdata,ev.ydata
115            self.move(ev.xdata,ev.ydata,ev)
116        else:
117            self.restore()
118        self.base.update()
119        return True
120   
121    def onKey(self, ev):
122        '''
123        Respond to keyboard events.  Arrow keys move the widget.  Escape
124        restores it to the position before the last click.
125       
126        Calls move() to update the state.  Calls restore() on escape.
127        '''
128        if ev.key == 'escape':
129            self.restore()
130        elif ev.key in ['up', 'down', 'right', 'left']:
131            dx,dy = self.dpixel(self.clickx,self.clicky,nudge=ev.control)
132            if ev.key == 'up': self.clicky += dy
133            elif ev.key == 'down': self.clicky -= dy
134            elif ev.key == 'right': self.clickx += dx
135            else: self.clickx -= dx
136            self.move(self.clickx,self.clicky,ev)
137        else:
138            return False
139        self.base.update()
140        return True
141
142    def dpixel(self,x,y,nudge=False):
143        '''
144        Return the step size in data coordinates for a small
145        step in screen coordinates.  If nudge is False (default)
146        the step size is one pixel.  If nudge is True, the step
147        size is 0.2 pixels.
148        '''
149        ax = self.axes
150        px,py = ax.transData.inverse_xy_tup((x,y))
151        if nudge:
152            nx,ny = ax.transData.xy_tup((px+0.2,py+0.2))
153        else:
154            nx,ny = ax.transData.xy_tup((px+1.,py+1.))
155        dx,dy = nx-x,ny-y
156        return dx,dy
157
Note: See TracBrowser for help on using the repository browser.