source: trunk/python/casatoolbar.py@ 1896

Last change on this file since 1896 was 1890, checked in by Kana Sugimoto, 14 years ago

New Development: No

JIRA Issue: Yes (CAS-1801)

Ready for Test: Yes

Interface Changes: No

Test Programs: try notation after sdplot with plottype='totalpower'

Put in Release Notes: No

Module(s): sdplot with plottype='totalpower'

Description: enabled notation with plottype='totalpower'.


File size: 10.5 KB
Line 
1import os
2import matplotlib
3
4######################################
5## Add CASA custom toolbar ##
6######################################
7class CustomToolbarCommon:
8 def __init__(self,parent):
9 self.plotter=parent
10 #self.figmgr=self.plotter._plotter.figmgr
11
12 ### select the nearest spectrum in pick radius
13 ### and display spectral value on the toolbar.
14 def _select_spectrum(self,event):
15 # Do not fire event when in zooming/panning mode
16 mode = self.figmgr.toolbar.mode
17 if not mode == '':
18 return
19 # When selected point is out of panels
20 if event.inaxes == None:
21 return
22 # If not left button
23 if event.button != 1:
24 return
25
26 xclick=event.xdata
27 yclick=event.ydata
28 dist2=1000.
29 pickline=None
30 # If the pannel has picable objects
31 pflag=False
32 for lin in event.inaxes.lines:
33 if not lin.pickable():
34 continue
35 pflag=True
36 flag,pind = lin.contains(event)
37 if not flag:
38 continue
39 # Get nearest point
40 inds = pind['ind']
41 xlin = lin.get_xdata()
42 ylin = lin.get_ydata()
43 for i in inds:
44 d2=(xlin[i]-xclick)**2+(ylin[i]-yclick)**2
45 if dist2 >= d2:
46 dist2 = d2
47 pickline = lin
48 # No pickcable line in the pannel
49 if not pflag:
50 return
51 # Pickable but too far from mouse position
52 elif pickline is None:
53 picked='No line selected.'
54 self.figmgr.toolbar.set_message(picked)
55 return
56 del pind, inds, xlin, ylin
57 # Spectra are Picked
58 theplot = self.plotter._plotter
59 thetoolbar = self.figmgr.toolbar
60 thecanvas = self.figmgr.canvas
61 # Disconnect the default motion notify event
62 # Notice! the other buttons are also diabled!!!
63 thecanvas.mpl_disconnect(thetoolbar._idDrag)
64 # Get picked spectrum
65 xdata = pickline.get_xdata()
66 ydata = pickline.get_ydata()
67 titl=pickline.get_label()
68 titp=event.inaxes.title.get_text()
69 panel0=event.inaxes
70 picked="Selected: '"+titl+"' in panel '"+titp+"'."
71 thetoolbar.set_message(picked)
72 # Generate a navigation window
73 #naviwin=Navigationwindow(titp,titl)
74 #------------------------------------------------------#
75 # Show spectrum data at mouse position
76 def spec_data(event):
77 # Getting spectrum data of neiboring point
78 xclick=event.xdata
79 if event.inaxes != panel0:
80 return
81 ipoint=len(xdata)-1
82 for i in range(len(xdata)-1):
83 xl=xclick-xdata[i]
84 xr=xclick-xdata[i+1]
85 if xl*xr <= 0.:
86 ipoint = i
87 break
88 # Output spectral value on the navigation window
89 posi='[ %s, %s ]: x = %.2f value = %.2f'\
90 %(titl,titp,xdata[ipoint],ydata[ipoint])
91 #naviwin.posi.set(posi)
92 thetoolbar.set_message(posi)
93 #------------------------------------------------------#
94 # Disconnect from mouse events
95 def discon(event):
96 #naviwin.window.destroy()
97 theplot.register('motion_notify',None)
98 # Re-activate the default motion_notify_event
99 thetoolbar._idDrag=thecanvas.mpl_connect('motion_notify_event',
100 thetoolbar.mouse_move)
101 theplot.register('button_release',None)
102 return
103 #------------------------------------------------------#
104 # Show data value along with mouse movement
105 theplot.register('motion_notify',spec_data)
106 # Finish events when mouse button is released
107 theplot.register('button_release',discon)
108
109
110 ### Calculate statistics of the selected area.
111 def _single_mask(self,event):
112 # Do not fire event when in zooming/panning mode
113 if not self.figmgr.toolbar.mode == '':
114 return
115 # When selected point is out of panels
116 if event.inaxes == None:
117 return
118 if event.button ==1:
119 baseinv=True
120 elif event.button == 3:
121 baseinv=False
122 else:
123 return
124
125 def _calc_stats():
126 msk=mymask.get_mask()
127 mymask.scan.stats(stat='max',mask=msk)
128 mymask.scan.stats(stat='min',mask=msk)
129 mymask.scan.stats(stat='sum',mask=msk)
130 mymask.scan.stats(stat='mean',mask=msk)
131 mymask.scan.stats(stat='median',mask=msk)
132 mymask.scan.stats(stat='rms',mask=msk)
133 mymask.scan.stats(stat='stddev',mask=msk)
134
135 # Interactive mask definition
136 from asap.interactivemask import interactivemask
137 mymask=interactivemask(plotter=self.plotter,scan=self.plotter._data)
138 # Create initial mask
139 mymask.set_basemask(invert=baseinv)
140 # Inherit event
141 mymask.set_startevent(event)
142 # Set callback func
143 mymask.set_callback(_calc_stats)
144 # Selected mask
145 mymask.select_mask(once=True,showmask=False)
146
147 def _mod_note(self,event):
148 # Do not fire event when in zooming/panning mode
149 if not self.figmgr.toolbar.mode == '':
150 return
151 if event.button ==1:
152 self.notewin.load_textwindow(event)
153 elif event.button == 3 and self._note_picked(event):
154 self.notewin.load_modmenu(event)
155 return
156
157 def _note_picked(self,event):
158 # just briefly check if any texts are picked
159 for textobj in self.canvas.figure.texts:
160 if textobj.contains(event)[0]:
161 return True
162 for ax in self.canvas.figure.axes:
163 for textobj in ax.texts:
164 if textobj.contains(event)[0]:
165 return True
166 #print "No text picked"
167 return False
168
169#####################################
170## Backend dependent Classes ##
171#####################################
172### TkAgg
173if matplotlib.get_backend() == 'TkAgg':
174 import Tkinter as Tk
175 from notationwindow import NotationWindowTkAgg
176
177class CustomToolbarTkAgg(CustomToolbarCommon, Tk.Frame):
178 def __init__(self,parent):
179 from asap.asapplotter import asapplotter
180 if not isinstance(parent,asapplotter):
181 return False
182 if not parent._plotter:
183 return False
184 self._p=parent._plotter
185 self.figmgr=self._p.figmgr
186 self.canvas=self.figmgr.canvas
187 self.mode=''
188 self.button=True
189 self._add_custom_toolbar()
190 self.notewin=NotationWindowTkAgg(master=self.canvas)
191 CustomToolbarCommon.__init__(self,parent)
192
193 def _add_custom_toolbar(self):
194 Tk.Frame.__init__(self,master=self.figmgr.window)
195 self.bSpec=self._NewButton(master=self,
196 text='spec value',
197 command=self.spec_show)
198 self.bStat=self._NewButton(master=self,
199 text='statistics',
200 command=self.stat_cal)
201 self.bNote=self._NewButton(master=self,
202 text='notation',
203 command=self.modify_note)
204 self.bQuit=self._NewButton(master=self,
205 text='Quit',
206 command=self.quit,
207 side=Tk.RIGHT)
208 self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
209
210 self.disable_button()
211 return #self
212
213 def _NewButton(self, master, text, command, side=Tk.LEFT):
214 if os.uname()[0] == 'Darwin':
215 b = Tk.Button(master=master, text=text, command=command)
216 else:
217 b = Tk.Button(master=master, text=text, padx=2, pady=2,
218 command=command)
219 b.pack(side=side)
220 return b
221
222 def spec_show(self):
223 if not self.figmgr.toolbar.mode == '' or not self.button: return
224 self.figmgr.toolbar.set_message('spec value: drag on a spec')
225 if self.mode == 'spec': return
226 self.bStat.config(relief='raised')
227 self.bSpec.config(relief='sunken')
228 self.bNote.config(relief='raised')
229 self.mode='spec'
230 self.notewin.close_widgets()
231 self.__disconnect_event()
232 #self.canvas.mpl_connect('button_press_event',self._select_spectrum)
233 self._p.register('button_press',self._select_spectrum)
234
235 def stat_cal(self):
236 if not self.figmgr.toolbar.mode == '' or not self.button: return
237 self.figmgr.toolbar.set_message('statistics: select a region')
238 if self.mode == 'stat': return
239 self.bSpec.config(relief='raised')
240 self.bStat.config(relief='sunken')
241 self.bNote.config(relief='raised')
242 self.mode='stat'
243 self.notewin.close_widgets()
244 self.__disconnect_event()
245 self._p.register('button_press',self._single_mask)
246
247 def modify_note(self):
248 if not self.figmgr.toolbar.mode == '': return
249 self.figmgr.toolbar.set_message('text: select a position/text')
250 if self.mode == 'note': return
251 self.bSpec.config(relief='raised')
252 self.bStat.config(relief='raised')
253 self.bNote.config(relief='sunken')
254 self.mode='note'
255 self.__disconnect_event()
256 self._p.register('button_press',self._mod_note)
257
258 def quit(self):
259 self.__disconnect_event()
260 #self.delete_bar()
261 self.disable_button()
262 self.figmgr.window.wm_withdraw()
263
264 def enable_button(self):
265 if self.button: return
266 self.bSpec.config(state=Tk.NORMAL)
267 self.bStat.config(state=Tk.NORMAL)
268 self.button=True
269 self.spec_show()
270
271 def disable_button(self):
272 if not self.button: return
273 self.bStat.config(relief='raised', state=Tk.DISABLED)
274 self.bSpec.config(relief='raised', state=Tk.DISABLED)
275 self.button=False
276 self.mode=''
277 self.__disconnect_event()
278
279 def delete_bar(self):
280 self.__disconnect_event()
281 self.destroy()
282
283 def __disconnect_event(self):
284 #idP=self.figmgr.toolbar._idPress
285 #idR=self.figmgr.toolbar._idRelease
286 #if idP is not None:
287 # self.canvas.mpl_disconnect(idP)
288 # self.figmgr.toolbar._idPress=None
289 #if idR is not None:
290 # self.canvas.mpl_disconnect(idR)
291 # self.figmgr.toolbar._idRelease=None
292 self._p.register('button_press',None)
293 self._p.register('button_release',None)
Note: See TracBrowser for help on using the repository browser.