source: sasview/src/sas/sasgui/plottools/BaseInteractor.py @ ebfdf4b

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 ebfdf4b was d7bb526, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Refactored plottools into sasgui

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