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