7e5579bf4144d8a255c5232576ce839f95361d79
[hooke.git] / hooke / ui / gui / plotcommands.py
1 # Copyright
2
3 #PLOT INTERACTION COMMANDS
4 #-------------------------------
5     def help_plot(self):
6         print '''
7 PLOT
8 Plots the current force curve
9 -------
10 Syntax: plot
11         '''
12     def do_plot(self,args):
13         if self.current.identify(self.drivers) == False:
14             return
15         self.plots=self.current.curve.default_plots()
16         try:
17             self.plots=self.current.curve.default_plots()
18         except Exception, e:
19             print 'Unexpected error occurred in do_plot().'
20             print e
21             return
22
23         #apply the plotmanip functions eventually present
24         nplots=len(self.plots)
25         c=0
26         while c<nplots:
27             for function in self.plotmanip: #FIXME: something strange happens about self.plotmanip[0]
28                 self.plots[c]=function(self.plots[c], self.current)
29
30             self.plots[c].xaxes=self.config['xaxes'] #FIXME: in the future, xaxes and yaxes should be set per-plot
31             self.plots[c].yaxes=self.config['yaxes']
32
33             c+=1
34
35         self._send_plot(self.plots)
36
37     def _delta(self, set=1):
38         '''
39         calculates the difference between two clicked points
40         '''
41         print 'Click two points'
42         points=self._measure_N_points(N=2, whatset=set)
43         dx=abs(points[0].graph_coords[0]-points[1].graph_coords[0])
44         dy=abs(points[0].graph_coords[1]-points[1].graph_coords[1])
45         unitx=self.plots[points[0].dest].units[0]
46         unity=self.plots[points[0].dest].units[1]
47         return dx,unitx,dy,unity
48
49     def do_delta(self,args):
50         '''
51         DELTA
52
53         Measures the delta X and delta Y between two points.
54         ----
55         Syntax: delta
56         '''
57         dx,unitx,dy,unity=self._delta()
58         print str(dx)+' '+unitx
59         print str(dy)+' '+unity
60
61     def _point(self, set=1):
62         '''calculates the coordinates of a single clicked point'''
63
64         print 'Click one point'
65         point=self._measure_N_points(N=1, whatset=set)
66
67         x=point[0].graph_coords[0]
68         y=point[0].graph_coords[1]
69         unitx=self.plots[point[0].dest].units[0]
70         unity=self.plots[point[0].dest].units[1]
71         return x,unitx,y,unity
72
73     def do_point(self,args):
74         '''
75         POINT
76
77         Returns the coordinates of a point on the graph.
78         ----
79         Syntax: point
80         '''
81         x,unitx,y,unity=self._point()
82         print str(x)+' '+unitx
83         print str(y)+' '+unity
84         to_dump='point '+self.current.path+' '+str(x)+' '+unitx+', '+str(y)+' '+unity
85         self.outlet.push(to_dump)
86
87
88     def do_close(self,args=None):
89         '''
90         CLOSE
91         Closes one of the two plots. If no arguments are given, the bottom plot is closed.
92         ------
93         Syntax: close [top,bottom]
94         '''
95         if args=='top':
96             to_close=0
97         elif args=='bottom':
98             to_close=1
99         else:
100             to_close=1
101
102         close_plot=self.list_of_events['close_plot']
103         wx.PostEvent(self.frame, close_plot(to_close=to_close))
104
105     def do_show(self,args=None):
106         '''
107         SHOW
108         Shows both plots.
109         '''
110         show_plots=self.list_of_events['show_plots']
111         wx.PostEvent(self.frame, show_plots())
112
113 #HELPER FUNCTIONS
114 #Everything sending an event should be here
115     def _measure_N_points(self, N, whatset=1):
116         '''
117         general helper function for N-points measures
118         '''
119         wx.PostEvent(self.frame,self.list_of_events['measure_points'](num_of_points=N, set=whatset))
120         while 1:
121             try:
122                 points=self.frame.events_from_gui.get()
123                 break
124             except Empty:
125                 pass
126         return points
127
128     def _get_displayed_plot(self,dest=0):
129         '''
130         returns the currently displayed plot.
131         '''
132         wx.PostEvent(self.frame, self.list_of_events['get_displayed_plot'](dest=dest))
133         while 1:
134             try:
135                 displayed_plot=self.events_from_gui.get()
136             except Empty:
137                 pass
138             if displayed_plot:
139                 break
140         return displayed_plot
141
142     def _send_plot(self,plots):
143         '''
144         sends a plot to the GUI
145         '''
146         wx.PostEvent(self.frame, self.list_of_events['plot_graph'](plots=plots))
147         return
148
149     def _find_plotmanip(self, name):
150         '''
151         returns a plot manipulator function from its name
152         '''
153         return self.plotmanip[self.config['plotmanips'].index(name)]
154
155     def _clickize(self, xvector, yvector, index):
156         '''
157         returns a ClickedPoint() object from an index and vectors of x, y coordinates
158         '''
159         point=ClickedPoint()
160         point.index=index
161         point.absolute_coords=xvector[index],yvector[index]
162         point.find_graph_coords(xvector,yvector)
163         return point