Ran update_copyright.py
[hooke.git] / hooke / ui / gui / plotcommands.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 #PLOT INTERACTION COMMANDS
20 #-------------------------------
21     def help_plot(self):
22         print '''
23 PLOT
24 Plots the current force curve
25 -------
26 Syntax: plot
27         '''
28     def do_plot(self,args):
29         if self.current.identify(self.drivers) == False:
30             return
31         self.plots=self.current.curve.default_plots()
32         try:
33             self.plots=self.current.curve.default_plots()
34         except Exception, e:
35             print 'Unexpected error occurred in do_plot().'
36             print e
37             return
38
39         #apply the plotmanip functions eventually present
40         nplots=len(self.plots)
41         c=0
42         while c<nplots:
43             for function in self.plotmanip: #FIXME: something strange happens about self.plotmanip[0]
44                 self.plots[c]=function(self.plots[c], self.current)
45
46             self.plots[c].xaxes=self.config['xaxes'] #FIXME: in the future, xaxes and yaxes should be set per-plot
47             self.plots[c].yaxes=self.config['yaxes']
48
49             c+=1
50
51         self._send_plot(self.plots)
52
53     def _delta(self, set=1):
54         '''
55         calculates the difference between two clicked points
56         '''
57         print 'Click two points'
58         points=self._measure_N_points(N=2, whatset=set)
59         dx=abs(points[0].graph_coords[0]-points[1].graph_coords[0])
60         dy=abs(points[0].graph_coords[1]-points[1].graph_coords[1])
61         unitx=self.plots[points[0].dest].units[0]
62         unity=self.plots[points[0].dest].units[1]
63         return dx,unitx,dy,unity
64
65     def do_delta(self,args):
66         '''
67         DELTA
68
69         Measures the delta X and delta Y between two points.
70         ----
71         Syntax: delta
72         '''
73         dx,unitx,dy,unity=self._delta()
74         print str(dx)+' '+unitx
75         print str(dy)+' '+unity
76
77     def _point(self, set=1):
78         '''calculates the coordinates of a single clicked point'''
79
80         print 'Click one point'
81         point=self._measure_N_points(N=1, whatset=set)
82
83         x=point[0].graph_coords[0]
84         y=point[0].graph_coords[1]
85         unitx=self.plots[point[0].dest].units[0]
86         unity=self.plots[point[0].dest].units[1]
87         return x,unitx,y,unity
88
89     def do_point(self,args):
90         '''
91         POINT
92
93         Returns the coordinates of a point on the graph.
94         ----
95         Syntax: point
96         '''
97         x,unitx,y,unity=self._point()
98         print str(x)+' '+unitx
99         print str(y)+' '+unity
100         to_dump='point '+self.current.path+' '+str(x)+' '+unitx+', '+str(y)+' '+unity
101         self.outlet.push(to_dump)
102
103
104     def do_close(self,args=None):
105         '''
106         CLOSE
107         Closes one of the two plots. If no arguments are given, the bottom plot is closed.
108         ------
109         Syntax: close [top,bottom]
110         '''
111         if args=='top':
112             to_close=0
113         elif args=='bottom':
114             to_close=1
115         else:
116             to_close=1
117
118         close_plot=self.list_of_events['close_plot']
119         wx.PostEvent(self.frame, close_plot(to_close=to_close))
120
121     def do_show(self,args=None):
122         '''
123         SHOW
124         Shows both plots.
125         '''
126         show_plots=self.list_of_events['show_plots']
127         wx.PostEvent(self.frame, show_plots())
128
129 #HELPER FUNCTIONS
130 #Everything sending an event should be here
131     def _measure_N_points(self, N, whatset=1):
132         '''
133         general helper function for N-points measures
134         '''
135         wx.PostEvent(self.frame,self.list_of_events['measure_points'](num_of_points=N, set=whatset))
136         while 1:
137             try:
138                 points=self.frame.events_from_gui.get()
139                 break
140             except Empty:
141                 pass
142         return points
143
144     def _get_displayed_plot(self,dest=0):
145         '''
146         returns the currently displayed plot.
147         '''
148         wx.PostEvent(self.frame, self.list_of_events['get_displayed_plot'](dest=dest))
149         while 1:
150             try:
151                 displayed_plot=self.events_from_gui.get()
152             except Empty:
153                 pass
154             if displayed_plot:
155                 break
156         return displayed_plot
157
158     def _send_plot(self,plots):
159         '''
160         sends a plot to the GUI
161         '''
162         wx.PostEvent(self.frame, self.list_of_events['plot_graph'](plots=plots))
163         return
164
165     def _find_plotmanip(self, name):
166         '''
167         returns a plot manipulator function from its name
168         '''
169         return self.plotmanip[self.config['plotmanips'].index(name)]
170
171     def _clickize(self, xvector, yvector, index):
172         '''
173         returns a ClickedPoint() object from an index and vectors of x, y coordinates
174         '''
175         point=ClickedPoint()
176         point.index=index
177         point.absolute_coords=xvector[index],yvector[index]
178         point.find_graph_coords(xvector,yvector)
179         return point