source: sasview/sansguiframe/src/sans/guiframe/local_perspectives/plotting/Arc.py @ 9e91546

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 9e91546 was bc2db41, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Fixing code style problems

  • Property mode set to 100644
File size: 4.9 KB
Line 
1"""
2    Arc slicer for 2D data
3"""
4import math
5
6from BaseInteractor import _BaseInteractor
7from sans.guiframe.events import SlicerParameterEvent
8 
9class ArcInteractor(_BaseInteractor):
10    """
11    Select an annulus through a 2D plot
12    """
13    def __init__(self, base, axes, color='black', zorder=5, r=1.0, 
14                 theta1=math.pi/8, theta2=math.pi/4):
15       
16        _BaseInteractor.__init__(self, base, axes, color=color)
17        self.markers = []
18        self.axes = axes
19        self._mouse_x = r
20        self._mouse_y = 0
21       
22        self._save_x  = r
23        self._save_y  = 0
24       
25        self.scale = 10.0
26       
27        self.theta1 = theta1
28        self.theta2 = theta2
29        self.radius = r
30        [self.arc] = self.axes.plot([], [],
31                                      linestyle='-', marker='',
32                                      color=self.color)
33        self.npts = 20
34        self.has_move = False   
35        self.connect_markers([self.arc])
36        self.update()
37
38    def set_layer(self, n):
39        """
40            Allow adding plot to the same panel
41            :param n: the number of layer
42        """
43        self.layernum = n
44        self.update()
45       
46    def clear(self):
47        """
48            Clear this slicer and its markers
49        """
50        self.clear_markers()
51        try:
52            self.marker.remove()
53            self.arc.remove()
54        except:
55            # Old version of matplotlib
56            for item in range(len(self.axes.lines)):
57                del self.axes.lines[0]
58       
59    def get_radius(self):
60        """
61            Return arc radius
62        """
63        radius = math.sqrt(math.pow(self._mouse_x, 2) + \
64                           math.pow(self._mouse_y, 2))
65        return radius
66       
67    def update(self, theta1=None, theta2=None, nbins=None, r=None):
68        """
69            Update the plotted arc
70            :param theta1: starting angle of the arc
71            :param theta2: ending angle of the arc
72            :param nbins: number of points along the arc
73            :param r: radius of the arc
74        """
75        # Plot inner circle
76        x = []
77        y = []
78        if theta1 != None:
79            self.theta1 = theta1
80        if theta2 != None:
81            self.theta2 = theta2
82        while self.theta2 < self.theta1:
83            self.theta2 += (2 * math.pi)
84        while self.theta2 >= (self.theta1 + 2 * math.pi):
85            self.theta2 -= (2 * math.pi)
86        npts = int((self.theta2 - self.theta1)/(math.pi/120)) 
87             
88        if r == None:
89            self.radius =  math.sqrt(math.pow(self._mouse_x, 2) + \
90                                     math.pow(self._mouse_y, 2))
91        else:
92            self.radius = r
93        for i in range(self.npts):
94            phi = (self.theta2 - self.theta1)/(self.npts - 1) * i + self.theta1
95            xval = 1.0 * self.radius * math.cos(phi) 
96            yval = 1.0 * self.radius * math.sin(phi) 
97           
98            x.append(xval)
99            y.append(yval)
100        #self.marker.set(xdata=[self._mouse_x],ydata=[0])
101        self.arc.set_data(x, y) 
102       
103    def save(self, ev):
104        """
105        Remember the roughness for this layer and the next so that we
106        can restore on Esc.
107        """
108        self._save_x = self._mouse_x
109        self._save_y = self._mouse_y
110        #self._save_x = ev.xdata
111        #self._save_y = ev.ydata
112        self.base.freeze_axes()
113
114    def moveend(self, ev):
115        """
116            After a dragging motion reset the flag self.has_move to False
117            :param ev: event
118        """
119        self.has_move = False
120       
121        event = SlicerParameterEvent()
122        event.type = self.__class__.__name__
123        event.params = self.get_params()
124        self.base.moveend(ev)
125           
126    def restore(self):
127        """
128        Restore the roughness for this layer.
129        """
130        self._mouse_x = self._save_x
131        self._mouse_y = self._save_y
132       
133    def move(self, x, y, ev):
134        """
135        Process move to a new position, making sure that the move is allowed.
136        """
137        #print "ring move x, y", x,y
138        self._mouse_x = x
139        self._mouse_y = y
140        self.has_move = True
141        self.base.base.update()
142       
143    def set_cursor(self, radius, phi_min, phi_max, nbins):
144        """
145        """
146        self.theta1 = phi_min
147        self.theta2 = phi_max
148        self.update(nbins=nbins, r=radius)
149
150    def get_params(self):
151        """
152        """
153        params = {}
154        params["radius"] = self.radius
155        params["theta1"] = self.theta1
156        params["theta2"] = self.theta2
157        return params
158   
159    def set_params(self, params):
160        """
161        """
162        x = params["radius"] 
163        self.set_cursor(x, self._mouse_y)
164       
165   
Note: See TracBrowser for help on using the repository browser.