source: sasview/plottools/src/danse/common/plottools/BaseInteractor.py @ 82a54b8

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 82a54b8 was 82a54b8, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

adding plottools Part 2

  • Property mode set to 100644
File size: 5.2 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
10
11class _BaseInteractor:
12    """
13    Share some functions between the interface interactor and various layer
14    interactors.
15   
16    Individual interactors need the following functions:
17   
18        save(ev)  - save the current state for later restore
19        restore() - restore the old state
20        move(x,y,ev) - move the interactor to position x,y
21        moveend(ev) - end the drag event
22        update() - draw the interactors
23       
24    The following are provided by the base class:
25   
26        connect_markers(markers) - register callbacks for all markers
27        clear_markers() - remove all items in self.markers
28        onHilite(ev) - enter/leave event processing
29        onLeave(ev) - enter/leave event processing
30        onClick(ev) - mouse click: calls save()
31        onRelease(ev) - mouse click ends: calls moveend()
32        onDrag(ev) - mouse move: calls move() or restore()
33        onKey(ev) - keyboard move: calls move() or restore()
34       
35    Interactor attributes:
36   
37        base  - model we are operating on
38        axes  - axes holding the interactor
39        color - color of the interactor in non-active state
40        markers - list of handles for the interactor
41       
42    """
43    def __init__(self, base, axes, color='black'):
44        """
45        """
46        self.base = base
47        self.axes = axes
48        self.color = color
49        self.markers = []
50       
51    def clear_markers(self):
52        '''
53        Clear old markers and interfaces.
54        '''
55        for h in self.markers: h.remove()
56        if self.markers: self.base.connect.clear(*self.markers)
57        self.markers = []
58
59    def save(self, ev):
60        """
61        """
62        pass
63   
64    def restore(self, ev):
65        """
66        """
67        pass
68   
69    def move(self, x, y, ev):
70        """
71        """
72        pass
73   
74    def moveend(self, ev):
75        """
76        """
77        pass
78
79    def connect_markers(self, markers):
80        """
81        Connect markers to callbacks
82        """
83        for h in markers:
84            connect = self.base.connect
85            connect('enter', h, self.onHilite)
86            connect('leave', h, self.onLeave)
87            connect('click', h, self.onClick)
88            connect('release', h, self.onRelease)
89            connect('drag', h, self.onDrag)
90            connect('key', h, self.onKey)
91
92    def onHilite(self, ev):
93        """
94        Hilite the artist reporting the event, indicating that it is
95        ready to receive a click.
96        """
97        ev.artist.set_color(active_color)
98        self.base.draw()
99        return True
100
101    def onLeave(self, ev):
102        """
103        Restore the artist to the original colour when the cursor leaves.
104        """
105        ev.artist.set_color(self.color)
106        self.base.draw()
107        return True
108           
109    def onClick(self, ev):
110        """
111        Prepare to move the artist.  Calls save() to preserve the state for
112        later restore().
113        """
114        self.clickx, self.clicky = ev.xdata, ev.ydata
115        self.save(ev)
116        return True
117
118    def onRelease(self, ev):
119        """
120        """
121        self.moveend(ev)
122        return True
123
124    def onDrag(self, ev):
125        """
126        Move the artist.  Calls move() to update the state, or restore() if
127        the mouse leaves the window.
128        """
129        inside, prop = self.axes.contains(ev)
130        if inside:
131            self.clickx, self.clicky = ev.xdata, ev.ydata
132            self.move(ev.xdata, ev.ydata, ev)
133        else:
134            self.restore()
135        self.base.update()
136        return True
137   
138    def onKey(self, ev):
139        """
140        Respond to keyboard events.  Arrow keys move the widget.  Escape
141        restores it to the position before the last click.
142       
143        Calls move() to update the state.  Calls restore() on escape.
144        """
145        if ev.key == 'escape':
146            self.restore()
147        elif ev.key in ['up', 'down', 'right', 'left']:
148            dx, dy = self.dpixel(self.clickx, self.clicky, nudge=ev.control)
149            if ev.key == 'up': self.clicky += dy
150            elif ev.key == 'down': self.clicky -= dy
151            elif ev.key == 'right': self.clickx += dx
152            else: self.clickx -= dx
153            self.move(self.clickx, self.clicky, ev)
154        else:
155            return False
156        self.base.update()
157        return True
158
159    def dpixel(self, x, y, nudge=False):
160        """
161        Return the step size in data coordinates for a small
162        step in screen coordinates.  If nudge is False (default)
163        the step size is one pixel.  If nudge is True, the step
164        size is 0.2 pixels.
165        """
166        ax = self.axes
167        px, py = ax.transData.inverse_xy_tup((x, y))
168        if nudge:
169            nx, ny = ax.transData.xy_tup((px+0.2, py+0.2))
170        else:
171            nx, ny = ax.transData.xy_tup((px+1., py+1.))
172        dx, dy = nx-x, ny-y
173        return dx, dy
174
Note: See TracBrowser for help on using the repository browser.