source: sasview/src/sas/qtgui/Plotting/Slicers/BaseInteractor.py @ c71b20a

Last change on this file since c71b20a was aae5f4c, checked in by Adam Washington <adam.washington@…>, 7 years ago

Fix line endings

  • Property mode set to 100755
File size: 5.1 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(object):
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    """
42    def __init__(self, base, axes, color='black'):
43        """
44        """
45        self.base = base
46        self.axes = axes
47        self.color = color
48        self.clickx = None
49        self.clicky = None
50        self.markers = []
51
52    def clear_markers(self):
53        """
54        Clear old markers and interfaces.
55        """
56        for h in self.markers: 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
86        for h in markers:
87            connect = self.base.connect
88            connect('enter', h, self.onHilite)
89            connect('leave', h, self.onLeave)
90            connect('click', h, self.onClick)
91            connect('release', h, self.onRelease)
92            connect('drag', h, self.onDrag)
93            connect('key', h, self.onKey)
94
95    def onHilite(self, ev):
96        """
97        Hilite the artist reporting the event, indicating that it is
98        ready to receive a click.
99        """
100        ev.artist.set_color(active_color)
101        self.base.draw()
102        return True
103
104    def onLeave(self, ev):
105        """
106        Restore the artist to the original colour when the cursor leaves.
107        """
108        ev.artist.set_color(self.color)
109        self.base.draw()
110        return True
111
112    def onClick(self, ev):
113        """
114        Prepare to move the artist.  Calls save() to preserve the state for
115        later restore().
116        """
117        self.clickx, self.clicky = ev.xdata, ev.ydata
118        self.save(ev)
119        return True
120
121    def onRelease(self, ev):
122        """
123        """
124        self.moveend(ev)
125        return True
126
127    def onDrag(self, ev):
128        """
129        Move the artist.  Calls move() to update the state, or restore() if
130        the mouse leaves the window.
131        """
132        inside, _ = self.axes.contains(ev)
133        if inside:
134            self.clickx, self.clicky = ev.xdata, ev.ydata
135            self.move(ev.xdata, ev.ydata, ev)
136        else:
137            self.restore()
138        self.base.update()
139        return True
140
141    def onKey(self, ev):
142        """
143        Respond to keyboard events.  Arrow keys move the widget.  Escape
144        restores it to the position before the last click.
145
146        Calls move() to update the state.  Calls restore() on escape.
147        """
148        if ev.key == 'escape':
149            self.restore()
150        elif ev.key in ['up', 'down', 'right', 'left']:
151            dx, dy = self.dpixel(self.clickx, self.clicky, nudge=ev.control)
152            if ev.key == 'up':
153                self.clicky += dy
154            elif ev.key == 'down':
155                self.clicky -= dy
156            elif ev.key == 'right':
157                self.clickx += dx
158            else: 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.0, py + 1.0))
178        dx, dy = nx - x, ny - y
179        return dx, dy
180
Note: See TracBrowser for help on using the repository browser.