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

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 9ccb7e1 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
RevLine 
[bc2db41]1"""
2    Arc slicer for 2D data
3"""
[b06ef8c]4import math
5
[0d9dae8]6from BaseInteractor import _BaseInteractor
[55a0dc1]7from sans.guiframe.events import SlicerParameterEvent
[0d9dae8]8 
[b06ef8c]9class ArcInteractor(_BaseInteractor):
10    """
[83f4445]11    Select an annulus through a 2D plot
[b06ef8c]12    """
[32c0841]13    def __init__(self, base, axes, color='black', zorder=5, r=1.0, 
14                 theta1=math.pi/8, theta2=math.pi/4):
[b06ef8c]15       
16        _BaseInteractor.__init__(self, base, axes, color=color)
17        self.markers = []
18        self.axes = axes
[cd84dca]19        self._mouse_x = r
20        self._mouse_y = 0
[b06ef8c]21       
[cd84dca]22        self._save_x  = r
23        self._save_y  = 0
[b06ef8c]24       
25        self.scale = 10.0
26       
[32c0841]27        self.theta1 = theta1
28        self.theta2 = theta2
29        self.radius = r
30        [self.arc] = self.axes.plot([], [],
[b06ef8c]31                                      linestyle='-', marker='',
32                                      color=self.color)
33        self.npts = 20
[32c0841]34        self.has_move = False   
[cd84dca]35        self.connect_markers([self.arc])
[b06ef8c]36        self.update()
37
38    def set_layer(self, n):
[83f4445]39        """
[bc2db41]40            Allow adding plot to the same panel
41            :param n: the number of layer
[83f4445]42        """
[b06ef8c]43        self.layernum = n
44        self.update()
45       
46    def clear(self):
[83f4445]47        """
[bc2db41]48            Clear this slicer and its markers
[83f4445]49        """
[b06ef8c]50        self.clear_markers()
51        try:
[cd84dca]52            self.marker.remove()
53            self.arc.remove()
[b06ef8c]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):
[83f4445]60        """
[bc2db41]61            Return arc radius
[83f4445]62        """
[32c0841]63        radius = math.sqrt(math.pow(self._mouse_x, 2) + \
64                           math.pow(self._mouse_y, 2))
[b06ef8c]65        return radius
66       
[32c0841]67    def update(self, theta1=None, theta2=None, nbins=None, r=None):
[b06ef8c]68        """
[bc2db41]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
[b06ef8c]74        """
75        # Plot inner circle
76        x = []
77        y = []
[32c0841]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)
[cd84dca]86        npts = int((self.theta2 - self.theta1)/(math.pi/120)) 
87             
[32c0841]88        if r == None:
89            self.radius =  math.sqrt(math.pow(self._mouse_x, 2) + \
90                                     math.pow(self._mouse_y, 2))
[cd84dca]91        else:
[32c0841]92            self.radius = r
[b06ef8c]93        for i in range(self.npts):
[32c0841]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) 
[e8c96f5]97           
[b06ef8c]98            x.append(xval)
99            y.append(yval)
[cd84dca]100        #self.marker.set(xdata=[self._mouse_x],ydata=[0])
101        self.arc.set_data(x, y) 
[b06ef8c]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        """
[cd84dca]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
[b06ef8c]112        self.base.freeze_axes()
113
114    def moveend(self, ev):
[83f4445]115        """
[bc2db41]116            After a dragging motion reset the flag self.has_move to False
117            :param ev: event
[83f4445]118        """
[32c0841]119        self.has_move = False
[e8c96f5]120       
[0d9dae8]121        event = SlicerParameterEvent()
[e8c96f5]122        event.type = self.__class__.__name__
123        event.params = self.get_params()
[b06ef8c]124        self.base.moveend(ev)
125           
126    def restore(self):
127        """
128        Restore the roughness for this layer.
129        """
[cd84dca]130        self._mouse_x = self._save_x
131        self._mouse_y = self._save_y
[b06ef8c]132       
133    def move(self, x, y, ev):
134        """
135        Process move to a new position, making sure that the move is allowed.
136        """
[e8c96f5]137        #print "ring move x, y", x,y
[cd84dca]138        self._mouse_x = x
139        self._mouse_y = y
[32c0841]140        self.has_move = True
[b06ef8c]141        self.base.base.update()
142       
[32c0841]143    def set_cursor(self, radius, phi_min, phi_max, nbins):
[83f4445]144        """
145        """
[32c0841]146        self.theta1 = phi_min
147        self.theta2 = phi_max
[e8c96f5]148        self.update(nbins=nbins, r=radius)
[32c0841]149
[b06ef8c]150    def get_params(self):
[83f4445]151        """
152        """
[b06ef8c]153        params = {}
[e8c96f5]154        params["radius"] = self.radius
155        params["theta1"] = self.theta1
156        params["theta2"] = self.theta2
[b06ef8c]157        return params
158   
159    def set_params(self, params):
[83f4445]160        """
161        """
[b06ef8c]162        x = params["radius"] 
[cd84dca]163        self.set_cursor(x, self._mouse_y)
[b06ef8c]164       
165   
Note: See TracBrowser for help on using the repository browser.