source: branches/newfiller/python/casatoolbar.py@ 2485

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

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs: none

Put in Release Notes: No

Module(s): ASAP plotter and sdplot

Description: removed an attribute, custombar, from the class

CustomToolbarTkAgg and inherit Tkinter.Frame class instead.


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