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