Changeset 1885 for trunk/python
- Timestamp:
- 08/21/10 12:03:00 (14 years ago)
- Location:
- trunk/python
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/casatoolbar.py
r1884 r1885 152 152 self.notewin.load_textwindow(event) 153 153 elif event.button == 3 and self._note_picked(event): 154 self.notewin. show_modmenu(event)154 self.notewin.load_modmenu(event) 155 155 return 156 156 … … 228 228 self.bNote.config(relief='raised') 229 229 self.mode='spec' 230 self.notewin.close_widgets() 230 231 self.__disconnect_event() 231 232 #self.canvas.mpl_connect('button_press_event',self._select_spectrum) … … 240 241 self.bNote.config(relief='raised') 241 242 self.mode='stat' 243 self.notewin.close_widgets() 242 244 self.__disconnect_event() 243 245 self._p.register('button_press',self._single_mask) -
trunk/python/notationwindow.py
r1884 r1885 7 7 ###################################### 8 8 class NotationWindowCommon: 9 """ 10 A base class to define the functions that the backend-based 11 GUI notation class must implement to print/modify/delete notes on a canvas. 12 13 The following methods *must* be implemented in the backend-based 14 parent class: 15 _get_note : get text in text box 16 _get_anchval : get anchor value selected 17 """ 9 18 def __init__(self,master=None): 10 19 #self.parent = master … … 12 21 self.event = None 13 22 self.note = None 14 self.ancval = None15 23 self.anchors = ["figure","axes","data"] 16 24 self.seltext = {} … … 19 27 @asaplog_post_dec 20 28 def print_text(self): 21 anchor = self.anchors[self.ancval.get()] 29 """ 30 """ 31 anchor = self.anchors[self._get_anchval()] 22 32 notestr = self._get_note().rstrip("\n") 23 33 if len(notestr.strip()) == 0: 24 self._clear_textbox()34 #self._clear_textbox() 25 35 #print "Empty string!" 26 36 return … … 28 38 myaxes = None 29 39 calcpos = True 30 xpos =None31 ypos =None40 xpos = None 41 ypos = None 32 42 if self.seltext: 33 43 # You are modifying a text … … 40 50 # printing text in the same coord. 41 51 # you don't have to recalc position 52 parent = self.seltext['parent'] 53 transform = self.seltext['textobj'].get_transform() 42 54 (xpos, ypos) = self.seltext['textobj'].get_position() 43 transform = self.seltext['textobj'].get_transform()44 parent = self.seltext['parent']45 55 elif anchor == "figure": 46 56 # converting from "axes"/"data" -> "figure" … … 79 89 # alignment of the text: ha (horizontal), va (vertical) 80 90 ha = 'left' 81 va = 'center' 91 #va = 'center' 92 va = 'top' 82 93 if not calcpos: 83 94 # you aready know parent, tansform, xpos and ypos … … 110 121 self.numnote += 1 111 122 112 self._clear_textbox()113 msg = "A note added to figure: str ='"+notestr+"'"114 msg += " at ["+str(xpos)+", "+str(ypos)+"] in "+anchor+"-coordinate"115 msg += "\n Total number of notes are "+str(self.numnote)123 #self._clear_textbox() 124 msg = "Added note: '"+notestr+"'" 125 msg += " @["+str(xpos)+", "+str(ypos)+"] ("+anchor+"-coord)" 126 msg += "\ntotal number of notes are "+str(self.numnote) 116 127 asaplog.push( msg ) 117 128 118 129 def _get_axes_from_pos(self,pos,canvas): 130 """helper function to get axes of a position in a plot (fig-coord)""" 119 131 if len(pos) != 2: 120 132 raise ValueError, "pixel position should have 2 elements" … … 130 142 131 143 def _convert_pix2dat(self,pos,axes): 144 """ 145 helper function to convert a position in figure-coord (0-1) to 146 data-coordinate of the axes 147 """ 132 148 # convert a relative position from lower-left of the canvas 133 149 # to a data in axes … … 152 168 153 169 @asaplog_post_dec 154 def _selected_text(self,event): 170 def _get_selected_text(self,event): 171 """helper function to return a dictionary of the nearest note to the event.""" 155 172 (w,h) = event.canvas.get_width_height() 156 173 dist2 = w*w+h*h … … 160 177 d2 = self._get_text_dist2(event,textobj) 161 178 if dist2 >= d2: 162 dist2 =d2163 selected ={'anchor': 'figure', \164 'parent': event.canvas.figure, 'textobj': textobj}179 dist2 = d2 180 selected = {'anchor': 'figure', \ 181 'parent': event.canvas.figure, 'textobj': textobj} 165 182 msg = "Fig loop: a text, '"+textobj.get_text()+"', at " 166 183 msg += str(textobj.get_position())+" detected" 167 asaplog.push(msg)184 print msg 168 185 for ax in self.canvas.figure.axes: 169 186 for textobj in ax.texts: … … 173 190 anchor='axes' 174 191 if ax.transData == textobj.get_transform(): 175 anchor='data' 176 selected={'anchor': anchor, 'parent': ax, 'textobj': textobj} 192 anchor = 'data' 193 selected = {'anchor': anchor, \ 194 'parent': ax, 'textobj': textobj} 177 195 msg = "Ax loop: a text, '"+textobj.get_text()+"', at " 178 196 msg += str(textobj.get_position())+" detected" 179 asaplog.push(msg) 197 print msg 198 199 if selected: 200 msg = "Selected (modify/delete): '"+textobj.get_text() 201 msg += "' @"+str(textobj.get_position()) 202 msg += " ("+selected['anchor']+"-coord)" 203 asaplog.push(msg) 180 204 181 205 return selected 182 206 183 207 def _get_text_dist2(self,event,textobj): 208 """ 209 helper function to calculate square of distance between 210 a event position and a text object. 211 """ 184 212 (x,y) = textobj._get_xy_display() 185 213 return (x-event.x)**2+(y-event.y)**2 186 214 187 215 def delete_note(self): 216 """ 217 Remove selected note. 218 """ 188 219 #print "You selected 'OK'" 189 220 self._remove_seltext() … … 192 223 @asaplog_post_dec 193 224 def _remove_seltext(self): 225 """helper function to remove the selected note""" 194 226 if len(self.seltext) < 3: 195 227 raise ValueError, "Don't under stand selected text obj." … … 202 234 203 235 textobj = self.seltext['textobj'] 204 msg = " A note deleted from figure: str ='"+textobj.get_text()+"'"205 msg += " at"+str(textobj.get_position())\206 +" in "+self.seltext['anchor']+"-coordinate"207 msg += "\n Total number of notes are "+str(self.numnote)236 msg = "Deleted note: '"+textobj.get_text()+"'" 237 msg += "@"+str(textobj.get_position())\ 238 +" ("+self.seltext['anchor']+"-coord)" 239 msg += "\ntotal number of notes are "+str(self.numnote) 208 240 asaplog.push( msg ) 209 241 210 242 self.seltext = {} 211 243 244 @asaplog_post_dec 212 245 def cancel_delete(self): 213 #print "You selected 'CANCEL'" 246 """ 247 Cancel deleting the selected note. 248 Fired when 'Cancel' button selected on confirmation dialog 249 """ 250 asaplog.push( "Cancel deleting: '"+self.seltext['textobj'].get_text()+"'" ) 214 251 self.seltext = {} 215 252 … … 224 261 225 262 class NotationWindowTkAgg(NotationWindowCommon): 263 """ 264 Backend based 265 """ 226 266 def __init__(self,master=None): 227 267 self.parent = master._tkcanvas 228 268 NotationWindowCommon.__init__(self,master=master) 229 self.textwin=self._create_textwindow(master=None) 230 self.menu=self._create_modmenu(master=self.parent) 231 269 self.anchval = None 270 self.textwin = self._create_textwindow(master=None) 271 self.menu = self._create_modmenu(master=self.parent) 272 273 ### Notation window widget 232 274 def _create_textwindow(self,master=None): 233 275 twin = Tk.Toplevel(padx=3,pady=3) … … 255 297 radio = Tk.LabelFrame(master=parent,text="anchor", 256 298 labelanchor="nw",padx=5,pady=3) 257 self.anc val = Tk.IntVar(master=radio,value=0)299 self.anchval = Tk.IntVar(master=radio,value=0) 258 300 self.rFig = self._NewRadioButton(radio,"figure",state=Tk.NORMAL, 259 variable=self.anc val,value=0,301 variable=self.anchval,value=0, 260 302 side=Tk.LEFT) 261 303 self.rAxis = self._NewRadioButton(radio,"panel",state=Tk.DISABLED, 262 variable=self.anc val,value=1,304 variable=self.anchval,value=1, 263 305 side=Tk.LEFT) 264 306 self.rData = self._NewRadioButton(radio,"data",state=Tk.DISABLED, 265 variable=self.anc val,value=2,307 variable=self.anchval,value=2, 266 308 side=Tk.LEFT) 267 309 # set initial selection "figure" 268 self.anc val.set(0)310 self.anchval.set(0) 269 311 return radio 270 312 … … 297 339 self.rFig.select() 298 340 341 def _get_anchval(self): 342 return self.anchval.get() 343 299 344 def _get_note(self): 300 345 return self.textbox.get("1.0",Tk.END) … … 323 368 324 369 def _cancel_text(self): 325 self. _finish_textwindow()370 self.close_textwindow() 326 371 327 372 def _print_text(self): 328 373 self.print_text() 329 self. _finish_textwindow()374 self.close_textwindow() 330 375 331 376 def load_textwindow(self,event): 377 """ 378 Load text window at a event position to add a note on a plot. 379 Parameter: 380 event: an even object to specify the position to load 381 text window. 382 """ 383 self.close_modmenu() 332 384 if event.canvas._tkcanvas != self.parent: 333 385 raise RuntimeError, "Got invalid event!" … … 336 388 is_ax = (event.inaxes != None) 337 389 (xpix, ypix) = self._disppix2screen(event.x, event.y) 338 offset =5390 offset = 5 339 391 self.show_textwindow(xpix+offset,ypix+offset,enableaxes=is_ax) 340 392 341 393 def show_textwindow(self,xpix,ypix,basetext=None,enableaxes=False): 394 """ 395 Load text window at a position of screen to add a note on a plot. 396 Parameters: 397 xpix, ypix: a pixel position from Upper-left corner 398 of the screen. 399 basetext: None (default) or any string. 400 A string to be printed on text box when loaded. 401 enable axes: False (default) or True. 402 If True, 'panel' & 'data' radio button is enabled. 403 """ 342 404 if not self.textwin: return 343 405 self._reset_radio() … … 352 414 self.textwin.geometry("%sx%s+%s+%s"%(w,h,xpix,ypix)) 353 415 354 def _finish_textwindow(self): 416 def close_textwindow(self): 417 """Close text window.""" 418 self.seltext = {} 355 419 self._reset_radio() 356 420 self._clear_textbox() 357 421 self.textwin.withdraw() 358 422 423 424 ### Modify/Delete menu widget 359 425 def _create_modmenu(self,master=None): 360 426 if master: 361 self.parent =master427 self.parent = master 362 428 if not self.parent: 363 429 return False 364 430 menu = Tk.Menu(master=self.parent,tearoff=False) 365 menu.add_command(label="Modify",command=self. modify)366 menu.add_command(label="Delete",command=self. delnote_dialog)431 menu.add_command(label="Modify",command=self._modify_note) 432 menu.add_command(label="Delete",command=self._delnote_dialog) 367 433 return menu 368 434 369 def show_modmenu(self,event): 370 self.seltext = self._selected_text(event) 435 def load_modmenu(self,event): 436 """ 437 Load cascade menu at a event position to modify or delete 438 selected text. 439 Parameter: 440 event: an even object to specify the position to load 441 text window. 442 """ 443 self.close_textwindow() 444 self.seltext = self._get_selected_text(event) 371 445 if len(self.seltext) == 3: 372 tkcanvas =event.canvas._tkcanvas446 tkcanvas = event.canvas._tkcanvas 373 447 xpos = tkcanvas.winfo_rootx() + int(event.x) 374 448 ypos = tkcanvas.winfo_rooty() \ … … 376 450 self.menu.post(xpos,ypos) 377 451 378 def modify(self): 452 def close_modmenu(self): 453 """Close cascade menu.""" 454 self.seltext = {} 455 self.menu.unpost() 456 457 ### load text window for modification 458 def _modify_note(self): 459 """helper function to load text window to modify selected note""" 379 460 #print "Modify selected!!" 380 461 textobj = self.seltext['textobj'] … … 393 474 self._set_note(textobj.get_text()) 394 475 395 def delnote_dialog(self): 476 ### close all widgets 477 def close_widgets(self): 478 """Close note window and menu""" 479 self.close_textwindow() 480 self.close_modmenu() 481 482 ### dialog to confirm deleting note 483 def _delnote_dialog(self): 396 484 remind = "Delete text?\n '"+self.seltext['textobj'].get_text()+"'" 397 485 answer = tkMessageBox.askokcancel(parent=self.parent,title="Delete?", … … 403 491 self.cancel_delete() 404 492 405 493 ### helper functions 406 494 def _disppix2screen(self,xpixd,ypixd): 407 495 # calculate a pixel position form Upper-left of the SCREEN
Note:
See TracChangeset
for help on using the changeset viewer.