1 | import os
|
---|
2 | import matplotlib, numpy
|
---|
3 | from asap.logging import asaplog, asaplog_post_dec
|
---|
4 | from matplotlib.patches import Rectangle
|
---|
5 | from asap.parameters import rcParams
|
---|
6 | from asap._asap import stmath
|
---|
7 | from asap.customgui_base import *
|
---|
8 |
|
---|
9 | import Tkinter as Tk
|
---|
10 | import tkMessageBox
|
---|
11 |
|
---|
12 | ######################################
|
---|
13 | ## Add CASA custom toolbar ##
|
---|
14 | ######################################
|
---|
15 | class CustomToolbarTkAgg(CustomToolbarCommon, Tk.Frame):
|
---|
16 | def __init__(self,parent):
|
---|
17 | from asap.asapplotter import asapplotter
|
---|
18 | if not isinstance(parent,asapplotter):
|
---|
19 | return False
|
---|
20 | if not parent._plotter:
|
---|
21 | return False
|
---|
22 | self._p = parent._plotter
|
---|
23 | self.figmgr = self._p.figmgr
|
---|
24 | self.canvas = self.figmgr.canvas
|
---|
25 | self.mode = ''
|
---|
26 | self.button = True
|
---|
27 | self.pagecount = None
|
---|
28 | CustomToolbarCommon.__init__(self,parent)
|
---|
29 | self.notewin = NotationWindowTkAgg(master=self.canvas)
|
---|
30 | self._add_custom_toolbar()
|
---|
31 |
|
---|
32 | def _add_custom_toolbar(self):
|
---|
33 | Tk.Frame.__init__(self,master=self.figmgr.window)
|
---|
34 | #self.bSpec = self._NewButton(master=self,
|
---|
35 | # text='spec value',
|
---|
36 | # command=self.spec_show)
|
---|
37 | self.bNote = self._NewButton(master=self,
|
---|
38 | text='notation',
|
---|
39 | command=self.modify_note)
|
---|
40 |
|
---|
41 | self.bStat = self._NewButton(master=self,
|
---|
42 | text='statistics',
|
---|
43 | command=self.stat_cal)
|
---|
44 | self.bQuit = self._NewButton(master=self,
|
---|
45 | text='Quit',
|
---|
46 | command=self.quit,
|
---|
47 | side=Tk.RIGHT)
|
---|
48 |
|
---|
49 | # page change oparations
|
---|
50 | frPage = Tk.Frame(master=self,borderwidth=2,relief=Tk.GROOVE)
|
---|
51 | frPage.pack(ipadx=2,padx=10,side=Tk.RIGHT)
|
---|
52 | self.lPagetitle = Tk.Label(master=frPage,text='Page:',padx=5)
|
---|
53 | self.lPagetitle.pack(side=Tk.LEFT)
|
---|
54 | self.pagecount = Tk.StringVar(master=frPage)
|
---|
55 | self.lPagecount = Tk.Label(master=frPage,
|
---|
56 | textvariable=self.pagecount,
|
---|
57 | padx=5,bg='white')
|
---|
58 | self.lPagecount.pack(side=Tk.LEFT,padx=3)
|
---|
59 |
|
---|
60 | self.bNext = self._NewButton(master=frPage,
|
---|
61 | text=' + ',
|
---|
62 | command=self.next_page)
|
---|
63 | self.bPrev = self._NewButton(master=frPage,
|
---|
64 | text=' - ',
|
---|
65 | command=self.prev_page)
|
---|
66 |
|
---|
67 | if os.uname()[0] != 'Darwin':
|
---|
68 | self.bPrev.config(padx=5)
|
---|
69 | self.bNext.config(padx=5)
|
---|
70 |
|
---|
71 | self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
|
---|
72 | self.pagecount.set(' '*4)
|
---|
73 |
|
---|
74 | self.disable_button()
|
---|
75 | return #self
|
---|
76 |
|
---|
77 | def _NewButton(self, master, text, command, side=Tk.LEFT):
|
---|
78 | if os.uname()[0] == 'Darwin':
|
---|
79 | b = Tk.Button(master=master, text=text, command=command)
|
---|
80 | else:
|
---|
81 | b = Tk.Button(master=master, text=text, padx=2, pady=2,
|
---|
82 | command=command)
|
---|
83 | b.pack(side=side)
|
---|
84 | return b
|
---|
85 |
|
---|
86 | def show_pagenum(self,pagenum,formatstr):
|
---|
87 | self.pagecount.set(formatstr % (pagenum))
|
---|
88 |
|
---|
89 | def spec_show(self):
|
---|
90 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
91 | self.figmgr.toolbar.set_message('spec value: drag on a spec')
|
---|
92 | if self.mode == 'spec': return
|
---|
93 | #self.bStat.config(relief='raised')
|
---|
94 | #self.bSpec.config(relief='sunken')
|
---|
95 | #self.bNote.config(relief='raised')
|
---|
96 | self.mode = 'spec'
|
---|
97 | self.notewin.close_widgets()
|
---|
98 | self.__disconnect_event()
|
---|
99 | self._p.register('button_press',self._select_spectrum)
|
---|
100 |
|
---|
101 | def stat_cal(self):
|
---|
102 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
103 | self.figmgr.toolbar.set_message('statistics: select a region')
|
---|
104 | if self.mode == 'stat':
|
---|
105 | # go back to spec mode
|
---|
106 | self.bStat.config(relief='raised')
|
---|
107 | self.spec_show()
|
---|
108 | return
|
---|
109 | #self.bSpec.config(relief='raised')
|
---|
110 | self.bStat.config(relief='sunken')
|
---|
111 | self.bNote.config(relief='raised')
|
---|
112 | self.mode = 'stat'
|
---|
113 | self.notewin.close_widgets()
|
---|
114 | self.__disconnect_event()
|
---|
115 | self._p.register('button_press',self._single_mask)
|
---|
116 |
|
---|
117 | def modify_note(self):
|
---|
118 | if not self.figmgr.toolbar.mode == '': return
|
---|
119 | if self.mode == 'note':
|
---|
120 | self.bNote.config(relief='raised')
|
---|
121 | self.mode = 'none'
|
---|
122 | self.spec_show()
|
---|
123 | if not self.button:
|
---|
124 | self.notewin.close_widgets()
|
---|
125 | self.__disconnect_event()
|
---|
126 | return
|
---|
127 | self.figmgr.toolbar.set_message('text: select a position/text')
|
---|
128 | #self.bSpec.config(relief='raised')
|
---|
129 | self.bStat.config(relief='raised')
|
---|
130 | self.bNote.config(relief='sunken')
|
---|
131 | self.mode = 'note'
|
---|
132 | self.__disconnect_event()
|
---|
133 | self._p.register('button_press',self._mod_note)
|
---|
134 |
|
---|
135 | def quit(self):
|
---|
136 | self.__disconnect_event()
|
---|
137 | #self.delete_bar()
|
---|
138 | self.disable_button()
|
---|
139 | self.figmgr.window.wm_withdraw()
|
---|
140 | self._p.quit()
|
---|
141 |
|
---|
142 | def enable_button(self):
|
---|
143 | if self.button: return
|
---|
144 | #self.bSpec.config(state=Tk.NORMAL)
|
---|
145 | self.bStat.config(state=Tk.NORMAL)
|
---|
146 | self.button = True
|
---|
147 | self.spec_show()
|
---|
148 |
|
---|
149 | def disable_button(self):
|
---|
150 | if not self.button: return
|
---|
151 | #self.bSpec.config(relief='raised', state=Tk.DISABLED)
|
---|
152 | self.bStat.config(relief='raised', state=Tk.DISABLED)
|
---|
153 | self.bNext.config(state=Tk.DISABLED)
|
---|
154 | self.bPrev.config(state=Tk.DISABLED)
|
---|
155 | self.button = False
|
---|
156 | self.mode = ''
|
---|
157 | self.__disconnect_event()
|
---|
158 |
|
---|
159 | def enable_next(self):
|
---|
160 | self.bNext.config(state=Tk.NORMAL)
|
---|
161 |
|
---|
162 | def disable_next(self):
|
---|
163 | self.bNext.config(state=Tk.DISABLED)
|
---|
164 |
|
---|
165 | def enable_prev(self):
|
---|
166 | self.bPrev.config(state=Tk.NORMAL)
|
---|
167 |
|
---|
168 | def disable_prev(self):
|
---|
169 | self.bPrev.config(state=Tk.DISABLED)
|
---|
170 |
|
---|
171 | # pause buttons for slow operations
|
---|
172 | def _pause_buttons(self,operation="end",msg=""):
|
---|
173 | buttons = ["bStat","bNote","bQuit"]
|
---|
174 | if operation == "start":
|
---|
175 | state = Tk.DISABLED
|
---|
176 | else:
|
---|
177 | state = Tk.NORMAL
|
---|
178 | for btn in buttons:
|
---|
179 | getattr(self,btn).config(state=state)
|
---|
180 | self.figmgr.toolbar.set_message(msg)
|
---|
181 |
|
---|
182 | def delete_bar(self):
|
---|
183 | self.__disconnect_event()
|
---|
184 | self.destroy()
|
---|
185 |
|
---|
186 | def __disconnect_event(self):
|
---|
187 | self._p.register('button_press',None)
|
---|
188 | self._p.register('button_release',None)
|
---|
189 |
|
---|
190 | def _draw_span(self,axes,x0,x1,**kwargs):
|
---|
191 | height = self._p.figure.bbox.height
|
---|
192 | y0 = height - axes.bbox.y0
|
---|
193 | y1 = height - axes.bbox.y1
|
---|
194 | return self._p.canvas._tkcanvas.create_rectangle(x0,y0,x1,y1,**kwargs)
|
---|
195 |
|
---|
196 | def _remove_span(self,span):
|
---|
197 | self._p.canvas._tkcanvas.delete(span)
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | ######################################
|
---|
202 | ## Notation box window ##
|
---|
203 | ######################################
|
---|
204 | class NotationWindowTkAgg(NotationWindowCommon):
|
---|
205 | """
|
---|
206 | Backend based class to create widgets to add, modify, or delete
|
---|
207 | note on the plot.
|
---|
208 |
|
---|
209 | Note:
|
---|
210 | Press LEFT-mouse button on the plot to ADD a note on the canvas.
|
---|
211 | A notation window will be loaded for specifying note string and
|
---|
212 | anchor. The note will be anchored on a position in whether figure-
|
---|
213 | (0-1 relative in a figure), panel- (0-1 relative in a plot axes),
|
---|
214 | or data-coordinate (data value in a plot axes).
|
---|
215 | Press RIGHT-mouse button on a note to MODIFY/DELETE it. A cascade
|
---|
216 | menu will be displayed and you can select an operation.
|
---|
217 | """
|
---|
218 | def __init__(self,master=None):
|
---|
219 | self.parent = master._tkcanvas
|
---|
220 | NotationWindowCommon.__init__(self,master=master)
|
---|
221 | self.anchval = None
|
---|
222 | self.textwin = self._create_textwindow(master=None)
|
---|
223 | self.menu = self._create_modmenu(master=self.parent)
|
---|
224 |
|
---|
225 | ### Notation window widget
|
---|
226 | def _create_textwindow(self,master=None):
|
---|
227 | """Create notation window widget and iconfy it"""
|
---|
228 | twin = Tk.Toplevel(padx=3,pady=3)
|
---|
229 | twin.title("Notation")
|
---|
230 | twin.resizable(width=True,height=True)
|
---|
231 | self.textbox = self._NotationBox(parent=twin)
|
---|
232 | self.radio = self._AnchorRadio(parent=twin)
|
---|
233 | self.actionbs = self._ActionButtons(parent=twin)
|
---|
234 |
|
---|
235 | self.textbox.pack(side=Tk.TOP,fill=Tk.BOTH,expand=True)
|
---|
236 | self.actionbs.pack(side=Tk.BOTTOM)
|
---|
237 | self.radio.pack(side=Tk.BOTTOM)
|
---|
238 | #twin.deiconify()
|
---|
239 | #twin.minsize(width=twin.winfo_width(),height=twin.winfo_height())
|
---|
240 | #twin.lift()
|
---|
241 | twin.withdraw()
|
---|
242 | return twin
|
---|
243 |
|
---|
244 | def _NotationBox(self,parent=None):
|
---|
245 | textbox = Tk.Text(master=parent,background='white',
|
---|
246 | height=2,width=20,cursor="xterm",
|
---|
247 | padx=2,pady=2,undo=True,maxundo=10,
|
---|
248 | relief='sunken',borderwidth=3,
|
---|
249 | state=Tk.NORMAL,takefocus=1)
|
---|
250 | return textbox
|
---|
251 |
|
---|
252 | def _AnchorRadio(self,parent=None):
|
---|
253 | radio = Tk.LabelFrame(master=parent,text="anchor",
|
---|
254 | labelanchor="nw",padx=5,pady=3)
|
---|
255 | self.anchval = Tk.IntVar(master=radio,value=0)
|
---|
256 | self.rFig = self._NewRadioButton(radio,"figure",state=Tk.NORMAL,
|
---|
257 | variable=self.anchval,value=0,
|
---|
258 | side=Tk.LEFT)
|
---|
259 | self.rAxis = self._NewRadioButton(radio,"panel",state=Tk.DISABLED,
|
---|
260 | variable=self.anchval,value=1,
|
---|
261 | side=Tk.LEFT)
|
---|
262 | self.rData = self._NewRadioButton(radio,"data",state=Tk.DISABLED,
|
---|
263 | variable=self.anchval,value=2,
|
---|
264 | side=Tk.LEFT)
|
---|
265 | # set initial selection "figure"
|
---|
266 | self.anchval.set(0)
|
---|
267 | return radio
|
---|
268 |
|
---|
269 | def _NewRadioButton(self,parent,text,state=Tk.NORMAL,variable=None,value=None,side=Tk.LEFT):
|
---|
270 | rb = Tk.Radiobutton(master=parent,text=text,state=state,
|
---|
271 | variable=variable,value=value)
|
---|
272 | rb.pack(side=side)
|
---|
273 | return rb
|
---|
274 |
|
---|
275 | def _enable_radio(self):
|
---|
276 | """Enable 'panel' and 'data' radio button"""
|
---|
277 | self.rAxis.config(state=Tk.NORMAL)
|
---|
278 | self.rData.config(state=Tk.NORMAL)
|
---|
279 | #self.rFig.config(state=Tk.NORMAL)
|
---|
280 | self.rFig.select()
|
---|
281 |
|
---|
282 | def _reset_radio(self):
|
---|
283 | """Disable 'panel' and 'data' radio button"""
|
---|
284 | self.rAxis.config(state=Tk.DISABLED)
|
---|
285 | self.rData.config(state=Tk.DISABLED)
|
---|
286 | self.rFig.config(state=Tk.NORMAL)
|
---|
287 | self.rFig.select()
|
---|
288 |
|
---|
289 | def _select_radio(self,selection):
|
---|
290 | """Select a specified radio button"""
|
---|
291 | if not selection in self.anchors:
|
---|
292 | return
|
---|
293 | if selection == "data":
|
---|
294 | self.rData.select()
|
---|
295 | elif selection == "axes":
|
---|
296 | self.rAxis.select()
|
---|
297 | else:
|
---|
298 | self.rFig.select()
|
---|
299 |
|
---|
300 | def _get_anchval(self):
|
---|
301 | """Returns a integer of a selected radio button"""
|
---|
302 | return self.anchval.get()
|
---|
303 |
|
---|
304 | def _get_note(self):
|
---|
305 | """Returns a note string specified in the text box"""
|
---|
306 | # A WORKAROUND for OSX 10.7 (Tk.Text returns unicode but asaplog doesn't accept it)
|
---|
307 | #return self.textbox.get("1.0",Tk.END)
|
---|
308 | return str(self.textbox.get("1.0",Tk.END))
|
---|
309 |
|
---|
310 | def _clear_textbox(self):
|
---|
311 | """Clear the text box"""
|
---|
312 | self.textbox.delete("1.0",Tk.END)
|
---|
313 |
|
---|
314 | def _set_note(self,note=None):
|
---|
315 | """Set a note string to the text box"""
|
---|
316 | self._clear_textbox()
|
---|
317 | if len(note) >0:
|
---|
318 | self.textbox.insert("1.0",note)
|
---|
319 |
|
---|
320 | def _ActionButtons(self,parent=None):
|
---|
321 | actbuts = Tk.Frame(master=parent)
|
---|
322 | bCancel = self._NewButton(actbuts,"cancel",self._cancel_text,side=Tk.LEFT)
|
---|
323 | bPrint = self._NewButton(actbuts,"print", self._print_text,side=Tk.LEFT)
|
---|
324 | return actbuts
|
---|
325 |
|
---|
326 | def _NewButton(self, parent, text, command, side=Tk.LEFT):
|
---|
327 | if(os.uname()[0] == 'Darwin'):
|
---|
328 | b = Tk.Button(master=parent, text=text, command=command)
|
---|
329 | else:
|
---|
330 | b = Tk.Button(master=parent, text=text, padx=2, pady=2, command=command)
|
---|
331 | b.pack(side=side)
|
---|
332 | return b
|
---|
333 |
|
---|
334 | def _cancel_text(self):
|
---|
335 | """
|
---|
336 | Cancel adding/modifying a note and close notaion window.
|
---|
337 | called when 'cancel' is selected.
|
---|
338 | """
|
---|
339 | self.close_textwindow()
|
---|
340 |
|
---|
341 | def _print_text(self):
|
---|
342 | """
|
---|
343 | Add/Modify a note. Called when 'print' is selected on the
|
---|
344 | notation window.
|
---|
345 | """
|
---|
346 | self.print_text()
|
---|
347 | self.close_textwindow()
|
---|
348 |
|
---|
349 | def load_textwindow(self,event):
|
---|
350 | """
|
---|
351 | Load text window at a event position to add a note on a plot.
|
---|
352 | Parameter:
|
---|
353 | event: an even object to specify the position to load
|
---|
354 | text window.
|
---|
355 | """
|
---|
356 | self.close_modmenu()
|
---|
357 | if event.canvas._tkcanvas != self.parent:
|
---|
358 | raise RuntimeError, "Got invalid event!"
|
---|
359 |
|
---|
360 | self.event = event
|
---|
361 | is_ax = (event.inaxes != None)
|
---|
362 | (xpix, ypix) = self._disppix2screen(event.x, event.y)
|
---|
363 | offset = 5
|
---|
364 | self.show_textwindow(xpix+offset,ypix+offset,enableaxes=is_ax)
|
---|
365 |
|
---|
366 | def show_textwindow(self,xpix,ypix,basetext=None,enableaxes=False):
|
---|
367 | """
|
---|
368 | Load text window at a position of screen to add a note on a plot.
|
---|
369 | Parameters:
|
---|
370 | xpix, ypix: a pixel position from Upper-left corner
|
---|
371 | of the screen.
|
---|
372 | basetext: None (default) or any string.
|
---|
373 | A string to be printed on text box when loaded.
|
---|
374 | enable axes: False (default) or True.
|
---|
375 | If True, 'panel' & 'data' radio button is enabled.
|
---|
376 | """
|
---|
377 | if not self.textwin: return
|
---|
378 | self._reset_radio()
|
---|
379 | if enableaxes:
|
---|
380 | self._enable_radio()
|
---|
381 | self.textwin.deiconify()
|
---|
382 | (w,h) = self.textwin.minsize()
|
---|
383 | # WORKAROUND for too small default minsize on OSX 10.7
|
---|
384 | #if w*h <= 1:
|
---|
385 | if w*h <= 1500:
|
---|
386 | self.textwin.minsize(width=self.textwin.winfo_width(),
|
---|
387 | height=self.textwin.winfo_height())
|
---|
388 | (w,h) = self.textwin.minsize()
|
---|
389 | self.textwin.geometry("%sx%s+%s+%s"%(w,h,xpix,ypix))
|
---|
390 | self.textwin.lift()
|
---|
391 |
|
---|
392 | def close_textwindow(self):
|
---|
393 | """Close text window."""
|
---|
394 | self.seltext = {}
|
---|
395 | self._reset_radio()
|
---|
396 | self._clear_textbox()
|
---|
397 | self.textwin.withdraw()
|
---|
398 |
|
---|
399 |
|
---|
400 | ### Modify/Delete menu widget
|
---|
401 | def _create_modmenu(self,master=None):
|
---|
402 | """Create modify/delete menu widget"""
|
---|
403 | if master:
|
---|
404 | self.parent = master
|
---|
405 | if not self.parent:
|
---|
406 | return False
|
---|
407 | menu = Tk.Menu(master=self.parent,tearoff=False)
|
---|
408 | menu.add_command(label="Modify",command=self._modify_note)
|
---|
409 | menu.add_command(label="Delete",command=self._delnote_dialog)
|
---|
410 | return menu
|
---|
411 |
|
---|
412 | def load_modmenu(self,event):
|
---|
413 | """
|
---|
414 | Load cascade menu at a event position to modify or delete
|
---|
415 | selected text.
|
---|
416 | Parameter:
|
---|
417 | event: an even object to specify the position to load
|
---|
418 | text window.
|
---|
419 | """
|
---|
420 | self.close_textwindow()
|
---|
421 | self.seltext = self._get_selected_text(event)
|
---|
422 | if len(self.seltext) == 3:
|
---|
423 | tkcanvas = event.canvas._tkcanvas
|
---|
424 | xpos = tkcanvas.winfo_rootx() + int(event.x)
|
---|
425 | ypos = tkcanvas.winfo_rooty() \
|
---|
426 | + tkcanvas.winfo_height() - int(event.y)
|
---|
427 | self.menu.post(xpos,ypos)
|
---|
428 |
|
---|
429 | def close_modmenu(self):
|
---|
430 | """Close cascade menu."""
|
---|
431 | self.seltext = {}
|
---|
432 | self.menu.unpost()
|
---|
433 |
|
---|
434 | ### load text window for modification
|
---|
435 | def _modify_note(self):
|
---|
436 | """helper function to load text window to modify selected note"""
|
---|
437 | #print "Modify selected!!"
|
---|
438 | textobj = self.seltext['textobj']
|
---|
439 | (xtx, ytx) = textobj._get_xy_display()
|
---|
440 | is_ax = (self.seltext['anchor'] != 'figure')
|
---|
441 | if not is_ax:
|
---|
442 | # previous anchor is figure
|
---|
443 | pos = textobj.get_position()
|
---|
444 | is_ax = (self._get_axes_from_pos(pos,self.canvas) != None)
|
---|
445 |
|
---|
446 | (xpix, ypix) = self._disppix2screen(xtx,ytx)
|
---|
447 | offset = int(textobj.get_size())*2
|
---|
448 | self.show_textwindow(xpix,ypix+offset,basetext=textobj.get_text(),\
|
---|
449 | enableaxes=is_ax)
|
---|
450 | self._select_radio(self.seltext['anchor'])
|
---|
451 | self._set_note(textobj.get_text())
|
---|
452 |
|
---|
453 | ### close all widgets
|
---|
454 | def close_widgets(self):
|
---|
455 | """Close note window and menu"""
|
---|
456 | self.close_textwindow()
|
---|
457 | self.close_modmenu()
|
---|
458 |
|
---|
459 | ### dialog to confirm deleting note
|
---|
460 | def _delnote_dialog(self):
|
---|
461 | """Load dialog to confirm deletion of the text"""
|
---|
462 | remind = "Delete text?\n '"+self.seltext['textobj'].get_text()+"'"
|
---|
463 | answer = tkMessageBox.askokcancel(parent=self.parent,title="Delete?",
|
---|
464 | message=remind,
|
---|
465 | default=tkMessageBox.CANCEL)
|
---|
466 | if answer:
|
---|
467 | self.delete_note()
|
---|
468 | else:
|
---|
469 | self.cancel_delete()
|
---|
470 |
|
---|
471 | ### helper functions
|
---|
472 | def _disppix2screen(self,xpixd,ypixd):
|
---|
473 | """
|
---|
474 | helper function to calculate a pixel position form Upper-left
|
---|
475 | corner of the SCREEN from a pixel position (xpixd, ypixd)
|
---|
476 | from Lower-left of the CANVAS (which, e.g., event.x/y returns)
|
---|
477 |
|
---|
478 | Returns:
|
---|
479 | (x, y): pixel position from Upper-left corner of the SCREEN.
|
---|
480 | """
|
---|
481 | xpixs = self.parent.winfo_rootx() + xpixd
|
---|
482 | ypixs = self.parent.winfo_rooty() + self.parent.winfo_height() \
|
---|
483 | - ypixd
|
---|
484 | return (int(xpixs), int(ypixs))
|
---|
485 |
|
---|
486 |
|
---|
487 |
|
---|
488 |
|
---|
489 |
|
---|
490 |
|
---|
491 | ###########################################
|
---|
492 | ## Add CASA custom Flag toolbar ##
|
---|
493 | ###########################################
|
---|
494 | class CustomFlagToolbarTkAgg(CustomFlagToolbarCommon, Tk.Frame):
|
---|
495 | def __init__(self,parent):
|
---|
496 | from asap.asapplotter import asapplotter
|
---|
497 | if not isinstance(parent,asapplotter):
|
---|
498 | return False
|
---|
499 | if not parent._plotter:
|
---|
500 | return False
|
---|
501 | self._p = parent._plotter
|
---|
502 | self.figmgr = self._p.figmgr
|
---|
503 | self.canvas = self.figmgr.canvas
|
---|
504 | self.mode = ''
|
---|
505 | self.button = True
|
---|
506 | self.pagecount = None
|
---|
507 | CustomFlagToolbarCommon.__init__(self,parent)
|
---|
508 | self.notewin=NotationWindowTkAgg(master=self.canvas)
|
---|
509 | self._add_custom_toolbar()
|
---|
510 |
|
---|
511 | def _add_custom_toolbar(self):
|
---|
512 | Tk.Frame.__init__(self,master=self.figmgr.window)
|
---|
513 | #self.bSpec = self._NewButton(master=self,
|
---|
514 | # text='spec value',
|
---|
515 | # command=self.spec_show)
|
---|
516 |
|
---|
517 | self.bRegion = self._NewButton(master=self,
|
---|
518 | text='region',
|
---|
519 | command=self.select_region)
|
---|
520 | self.bPanel = self._NewButton(master=self,
|
---|
521 | text='panel',
|
---|
522 | command=self.select_panel)
|
---|
523 | self.bClear = self._NewButton(master=self,
|
---|
524 | text='clear',
|
---|
525 | command=self.cancel_select)
|
---|
526 | self.bFlag = self._NewButton(master=self,
|
---|
527 | text='flag',
|
---|
528 | command=self.flag)
|
---|
529 | self.bUnflag = self._NewButton(master=self,
|
---|
530 | text='unflag',
|
---|
531 | command=self.unflag)
|
---|
532 |
|
---|
533 | self.bStat = self._NewButton(master=self,
|
---|
534 | text='statistics',
|
---|
535 | command=self.stat_cal)
|
---|
536 |
|
---|
537 | self.bNote = self._NewButton(master=self,
|
---|
538 | text='notation',
|
---|
539 | command=self.modify_note)
|
---|
540 |
|
---|
541 | self.bQuit = self._NewButton(master=self,
|
---|
542 | text='Quit',
|
---|
543 | #file="stock_close.ppm",
|
---|
544 | command=self.quit,
|
---|
545 | side=Tk.RIGHT)
|
---|
546 |
|
---|
547 | # page change oparations
|
---|
548 | frPage = Tk.Frame(master=self,borderwidth=2,relief=Tk.GROOVE)
|
---|
549 | frPage.pack(ipadx=2,padx=10,side=Tk.RIGHT)
|
---|
550 | self.lPagetitle = Tk.Label(master=frPage,text='Page:',padx=5)
|
---|
551 | #width=8,anchor=Tk.E,padx=5)
|
---|
552 | self.lPagetitle.pack(side=Tk.LEFT)
|
---|
553 | self.pagecount = Tk.StringVar(master=frPage)
|
---|
554 | self.lPagecount = Tk.Label(master=frPage,
|
---|
555 | textvariable=self.pagecount,
|
---|
556 | padx=5,bg='white')
|
---|
557 | self.lPagecount.pack(side=Tk.LEFT,padx=3)
|
---|
558 |
|
---|
559 | self.bNext=self._NewButton(master=frPage,
|
---|
560 | text=' + ',
|
---|
561 | #file="hand.ppm",
|
---|
562 | command=self.next_page)
|
---|
563 | self.bPrev=self._NewButton(master=frPage,
|
---|
564 | text=' - ',
|
---|
565 | command=self.prev_page)
|
---|
566 |
|
---|
567 | if os.uname()[0] != 'Darwin':
|
---|
568 | self.bPrev.config(padx=5)
|
---|
569 | self.bNext.config(padx=5)
|
---|
570 |
|
---|
571 | self.pack(side=Tk.BOTTOM,fill=Tk.BOTH)
|
---|
572 | self.pagecount.set(' '*4)
|
---|
573 |
|
---|
574 | self.disable_button()
|
---|
575 | return #self
|
---|
576 |
|
---|
577 | def _NewButton(self, master, text, command, side=Tk.LEFT,file=None):
|
---|
578 | img = None
|
---|
579 | if file:
|
---|
580 | file = os.path.join(matplotlib.rcParams['datapath'], 'images', file)
|
---|
581 | img = Tk.PhotoImage(master=master, file=file)
|
---|
582 |
|
---|
583 | if os.uname()[0] == 'Darwin':
|
---|
584 | b = Tk.Button(master=master, text=text, image=img,
|
---|
585 | command=command)
|
---|
586 | if img: b.image = img
|
---|
587 | else:
|
---|
588 | b = Tk.Button(master=master, text=text, image=img, padx=2, pady=2,
|
---|
589 | command=command)
|
---|
590 | if img: b.image = img
|
---|
591 | b.pack(side=side)
|
---|
592 | return b
|
---|
593 |
|
---|
594 | def show_pagenum(self,pagenum,formatstr):
|
---|
595 | self.pagecount.set(formatstr % (pagenum))
|
---|
596 |
|
---|
597 | def spec_show(self):
|
---|
598 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
599 | self.figmgr.toolbar.set_message('spec value: drag on a spec')
|
---|
600 | if self.mode == 'spec': return
|
---|
601 | self.mode = 'spec'
|
---|
602 | self.notewin.close_widgets()
|
---|
603 | self.__disconnect_event()
|
---|
604 | self._p.register('button_press',self._select_spectrum)
|
---|
605 |
|
---|
606 | def modify_note(self):
|
---|
607 | if not self.figmgr.toolbar.mode == '': return
|
---|
608 | if self.mode == 'note':
|
---|
609 | self.bNote.config(relief='raised')
|
---|
610 | self.mode = 'none'
|
---|
611 | self.spec_show()
|
---|
612 | if not self.button:
|
---|
613 | self.notewin.close_widgets()
|
---|
614 | self.__disconnect_event()
|
---|
615 | return
|
---|
616 | self.figmgr.toolbar.set_message('text: select a position/text')
|
---|
617 | self.bNote.config(relief='sunken')
|
---|
618 | self.bRegion.config(relief='raised')
|
---|
619 | self.bPanel.config(relief='raised')
|
---|
620 | self.mode = 'note'
|
---|
621 | self.__disconnect_event()
|
---|
622 | self._p.register('button_press',self._mod_note)
|
---|
623 |
|
---|
624 | def select_region(self):
|
---|
625 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
626 | if self.mode == 'region':
|
---|
627 | self.bRegion.config(relief='raised')
|
---|
628 | self.mode = 'none'
|
---|
629 | self.spec_show()
|
---|
630 | return
|
---|
631 | self.figmgr.toolbar.set_message('select regions: click at start and end channels')
|
---|
632 | self.bNote.config(relief='raised')
|
---|
633 | self.bRegion.config(relief='sunken')
|
---|
634 | self.bPanel.config(relief='raised')
|
---|
635 | self.mode = 'region'
|
---|
636 | self.notewin.close_widgets()
|
---|
637 | self.__disconnect_event()
|
---|
638 | self._p.register('button_press',self._add_region)
|
---|
639 |
|
---|
640 | def select_panel(self):
|
---|
641 | if not self.figmgr.toolbar.mode == '' or not self.button: return
|
---|
642 | if self.mode == 'panel':
|
---|
643 | self.bPanel.config(relief='raised')
|
---|
644 | self.mode = 'none'
|
---|
645 | self.spec_show()
|
---|
646 | return
|
---|
647 | self.figmgr.toolbar.set_message('select spectra: click on subplots')
|
---|
648 | self.bNote.config(relief='raised')
|
---|
649 | self.bRegion.config(relief='raised')
|
---|
650 | self.bPanel.config(relief='sunken')
|
---|
651 | self.mode = 'panel'
|
---|
652 | self.notewin.close_widgets()
|
---|
653 | self.__disconnect_event()
|
---|
654 | self._p.register('button_press',self._add_panel)
|
---|
655 |
|
---|
656 | def quit(self):
|
---|
657 | self.__disconnect_event()
|
---|
658 | self.disable_button()
|
---|
659 | self.figmgr.window.wm_withdraw()
|
---|
660 | self._p.quit()
|
---|
661 |
|
---|
662 | def enable_button(self):
|
---|
663 | if self.button: return
|
---|
664 | self.bRegion.config(state=Tk.NORMAL)
|
---|
665 | self.bPanel.config(state=Tk.NORMAL)
|
---|
666 | self.bClear.config(state=Tk.NORMAL)
|
---|
667 | self.bFlag.config(state=Tk.NORMAL)
|
---|
668 | self.bUnflag.config(state=Tk.NORMAL)
|
---|
669 | self.bStat.config(state=Tk.NORMAL)
|
---|
670 | self.button = True
|
---|
671 | self.spec_show()
|
---|
672 |
|
---|
673 | def disable_button(self):
|
---|
674 | ## disable buttons which don't work for plottp
|
---|
675 | if not self.button: return
|
---|
676 | self.bRegion.config(relief='raised')
|
---|
677 | self.bPanel.config(relief='raised')
|
---|
678 | self.bRegion.config(state=Tk.DISABLED)
|
---|
679 | self.bPanel.config(state=Tk.DISABLED)
|
---|
680 | self.bClear.config(state=Tk.DISABLED)
|
---|
681 | self.bFlag.config(state=Tk.DISABLED)
|
---|
682 | self.bUnflag.config(state=Tk.DISABLED)
|
---|
683 | self.bStat.config(state=Tk.DISABLED)
|
---|
684 | self.bNext.config(state=Tk.DISABLED)
|
---|
685 | self.bPrev.config(state=Tk.DISABLED)
|
---|
686 | self.button = False
|
---|
687 | self.mode = ''
|
---|
688 | self.notewin.close_widgets()
|
---|
689 | self.__disconnect_event()
|
---|
690 |
|
---|
691 | def enable_next(self):
|
---|
692 | self.bNext.config(state=Tk.NORMAL)
|
---|
693 |
|
---|
694 | def disable_next(self):
|
---|
695 | self.bNext.config(state=Tk.DISABLED)
|
---|
696 |
|
---|
697 | def enable_prev(self):
|
---|
698 | self.bPrev.config(state=Tk.NORMAL)
|
---|
699 |
|
---|
700 | def disable_prev(self):
|
---|
701 | self.bPrev.config(state=Tk.DISABLED)
|
---|
702 |
|
---|
703 | # pause buttons for slow operations
|
---|
704 | def _pause_buttons(self,operation="end",msg=""):
|
---|
705 | buttons = ["bRegion","bPanel","bClear","bFlag","bUnflag","bStat",
|
---|
706 | "bNote","bQuit"]
|
---|
707 | if operation == "start":
|
---|
708 | state=Tk.DISABLED
|
---|
709 | else:
|
---|
710 | state=Tk.NORMAL
|
---|
711 | for btn in buttons:
|
---|
712 | getattr(self,btn).config(state=state)
|
---|
713 | self.figmgr.toolbar.set_message(msg)
|
---|
714 |
|
---|
715 | def delete_bar(self):
|
---|
716 | self.__disconnect_event()
|
---|
717 | self.destroy()
|
---|
718 |
|
---|
719 | def __disconnect_event(self):
|
---|
720 | self._p.register('button_press',None)
|
---|
721 | self._p.register('button_release',None)
|
---|
722 |
|
---|
723 | def _draw_span(self,axes,x0,x1,**kwargs):
|
---|
724 | height = self._p.figure.bbox.height
|
---|
725 | y0 = height - axes.bbox.y0
|
---|
726 | y1 = height - axes.bbox.y1
|
---|
727 | return self._p.canvas._tkcanvas.create_rectangle(x0,y0,x1,y1,**kwargs)
|
---|
728 |
|
---|
729 | def _remove_span(self,span):
|
---|
730 | self._p.canvas._tkcanvas.delete(span)
|
---|