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

Last change on this file since 1b9a367 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
RevLine 
[a9d5684]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
[2df0b74]11class _BaseInteractor(object):
[a9d5684]12    """
13    Share some functions between the interface interactor and various layer
14    interactors.
[2df0b74]15
[a9d5684]16    Individual interactors need the following functions:
[2df0b74]17
[a9d5684]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
[2df0b74]23
[a9d5684]24    The following are provided by the base class:
[2df0b74]25
[a9d5684]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()
[2df0b74]34
[a9d5684]35    Interactor attributes:
[2df0b74]36
[a9d5684]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
[2df0b74]41
[a9d5684]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 = []
[2df0b74]50
[a9d5684]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
[2df0b74]65
[a9d5684]66    def restore(self, ev):
67        """
68        """
69        pass
[2df0b74]70
[a9d5684]71    def move(self, x, y, ev):
72        """
73        """
74        pass
[2df0b74]75
[a9d5684]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
[2df0b74]110
[a9d5684]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
[2df0b74]139
[a9d5684]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.
[2df0b74]144
[a9d5684]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:
[2df0b74]175            nx, ny = ax.transData.xy_tup((px + 0.2, py + 0.2))
[a9d5684]176        else:
[2df0b74]177            nx, ny = ax.transData.xy_tup((px + 1., py + 1.))
178        dx, dy = nx - x, ny - y
[a9d5684]179        return dx, dy
Note: See TracBrowser for help on using the repository browser.