[2155] | 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 PyQt4 as qt |
---|
| 10 | |
---|
[2168] | 11 | ###################################### |
---|
| 12 | ## Add CASA custom toolbar ## |
---|
| 13 | ###################################### |
---|
[2155] | 14 | class CustomToolbarQT4Agg(CustomToolbarCommon, qt.QtGui.QToolBar): |
---|
| 15 | def __init__(self,parent): |
---|
| 16 | from asap.asapplotter import asapplotter |
---|
| 17 | if not isinstance(parent,asapplotter): |
---|
| 18 | return False |
---|
| 19 | if not parent._plotter: |
---|
| 20 | return False |
---|
| 21 | self._p = parent._plotter |
---|
| 22 | self.figmgr = self._p.figmgr |
---|
| 23 | self.canvas = self.figmgr.canvas |
---|
| 24 | self.mode = '' |
---|
| 25 | self.button = True |
---|
| 26 | self.pagecount = None |
---|
| 27 | CustomToolbarCommon.__init__(self,parent) |
---|
[2168] | 28 | self.notewin = NotationWindowQT4Agg(master=self.canvas) |
---|
[2155] | 29 | self._add_custom_toolbar() |
---|
| 30 | |
---|
| 31 | def _add_custom_toolbar(self): |
---|
| 32 | qt.QtGui.QToolBar.__init__(self,parent=self.figmgr.window) |
---|
| 33 | self.figmgr.window.addToolBar(qt.QtCore.Qt.BottomToolBarArea,self) |
---|
| 34 | self.bNote = self._NewButton(master=self, |
---|
| 35 | text='notation', |
---|
[2168] | 36 | command=self.modify_note, |
---|
[2172] | 37 | balloon="add note on plot") |
---|
[2155] | 38 | self.bNote.setCheckable(True) |
---|
| 39 | |
---|
| 40 | self.bStat = self._NewButton(master=self, |
---|
| 41 | text='statistics', |
---|
[2168] | 42 | command=self.stat_cal, |
---|
[2172] | 43 | balloon="calculate statistics") |
---|
[2155] | 44 | self.bStat.setCheckable(True) |
---|
| 45 | |
---|
| 46 | # page change oparations |
---|
| 47 | frPage = qt.QtGui.QWidget(parent=self,flags=qt.QtCore.Qt.Tool) |
---|
| 48 | loPage = qt.QtGui.QHBoxLayout(self) |
---|
| 49 | loPage.addStretch(1) |
---|
| 50 | self.lPagetitle = qt.QtGui.QLabel('Page:',parent=frPage) |
---|
| 51 | self.lPagetitle.setMargin(5) |
---|
| 52 | loPage.addWidget(self.lPagetitle) |
---|
| 53 | self.pagecount = qt.QtGui.QLabel(parent=frPage) |
---|
| 54 | self.pagecount.setStyleSheet("background-color: white") |
---|
| 55 | self.pagecount.setMargin(3) |
---|
| 56 | self.pagecount.setText(' 1') |
---|
| 57 | loPage.addWidget(self.pagecount) |
---|
| 58 | |
---|
| 59 | self.bNext = self._NewButton(master=frPage, |
---|
| 60 | text=' + ', |
---|
[2168] | 61 | command=self.next_page, |
---|
[2172] | 62 | addit=False, |
---|
| 63 | balloon="plot next page") |
---|
[2155] | 64 | loPage.addWidget(self.bNext) |
---|
| 65 | self.bPrev = self._NewButton(master=frPage, |
---|
| 66 | text=' - ', |
---|
[2172] | 67 | command=self.prev_page,addit=False, |
---|
| 68 | balloon="plot previous page") |
---|
[2155] | 69 | loPage.addWidget(self.bPrev) |
---|
| 70 | frPage.setLayout(loPage) |
---|
| 71 | self.addWidget(frPage) |
---|
| 72 | |
---|
| 73 | self.bQuit = self._NewButton(master=self, |
---|
| 74 | text='Quit', |
---|
[2168] | 75 | command=self.quit, |
---|
| 76 | balloon="Close window") |
---|
[2155] | 77 | |
---|
| 78 | self.pagecount.setText(' '*4) |
---|
| 79 | |
---|
| 80 | self.disable_button() |
---|
| 81 | return |
---|
| 82 | |
---|
[2168] | 83 | def _NewButton(self, master, text, command, balloon=None,addit=True): |
---|
[2155] | 84 | b = qt.QtGui.QPushButton(text,parent=master) |
---|
[2168] | 85 | if balloon: b.setToolTip(balloon) |
---|
| 86 | if addit: master.addWidget(b) |
---|
[2155] | 87 | master.connect(b,qt.QtCore.SIGNAL('clicked()'),command) |
---|
| 88 | return b |
---|
| 89 | |
---|
| 90 | def show_pagenum(self,pagenum,formatstr): |
---|
| 91 | self.pagecount.setText(formatstr % (pagenum)) |
---|
| 92 | |
---|
| 93 | def spec_show(self): |
---|
| 94 | if not self.figmgr.toolbar.mode == '' or not self.button: return |
---|
[2173] | 95 | self.figmgr.toolbar.set_message("spec value: drag on a spec") |
---|
[2155] | 96 | if self.mode == 'spec': return |
---|
| 97 | self.mode = 'spec' |
---|
[2168] | 98 | self.notewin.close_widgets() |
---|
[2155] | 99 | self.__disconnect_event() |
---|
| 100 | self._p.register('button_press',self._select_spectrum) |
---|
| 101 | |
---|
| 102 | def stat_cal(self): |
---|
| 103 | if not self.figmgr.toolbar.mode == '' or not self.button: |
---|
| 104 | # Get back button status BEFORE clicked |
---|
| 105 | self.bStat.setChecked(not self.bStat.isChecked()) |
---|
| 106 | return |
---|
| 107 | if self.mode == 'stat': |
---|
| 108 | # go back to spec mode |
---|
| 109 | self.bStat.setChecked(False) |
---|
[2172] | 110 | self.bStat.setToolTip("calculate statistics") |
---|
[2155] | 111 | self.spec_show() |
---|
| 112 | return |
---|
[2173] | 113 | self.figmgr.toolbar.set_message("statistics: click at start and end channels") |
---|
[2155] | 114 | self.bStat.setChecked(True) |
---|
[2168] | 115 | self.bStat.setToolTip("Back to spec value mode") |
---|
[2155] | 116 | self.bNote.setChecked(False) |
---|
[2172] | 117 | self.bNote.setToolTip("add note on plot") |
---|
[2155] | 118 | self.mode = 'stat' |
---|
[2168] | 119 | self.notewin.close_widgets() |
---|
[2155] | 120 | self.__disconnect_event() |
---|
| 121 | self._p.register('button_press',self._single_mask) |
---|
| 122 | |
---|
| 123 | def modify_note(self): |
---|
| 124 | if not self.figmgr.toolbar.mode == '': |
---|
| 125 | # Get back button status BEFORE clicked |
---|
| 126 | self.bNote.setChecked(not self.bNote.isChecked()) |
---|
| 127 | return |
---|
| 128 | if self.mode == 'note': |
---|
| 129 | self.bNote.setChecked(False) |
---|
[2172] | 130 | self.bNote.setToolTip("add note on plot") |
---|
[2155] | 131 | self.mode = 'none' |
---|
| 132 | self.spec_show() |
---|
| 133 | return |
---|
[2173] | 134 | self.figmgr.toolbar.set_message("text: select a position/text") |
---|
[2155] | 135 | self.bStat.setChecked(False) |
---|
[2172] | 136 | self.bStat.setToolTip("calculate statistics") |
---|
[2155] | 137 | self.bNote.setChecked(True) |
---|
[2168] | 138 | self.bNote.setToolTip("Back to spec value mode") |
---|
[2155] | 139 | self.mode = 'note' |
---|
| 140 | self.__disconnect_event() |
---|
| 141 | self._p.register('button_press',self._mod_note) |
---|
| 142 | |
---|
| 143 | def quit(self): |
---|
| 144 | self.__disconnect_event() |
---|
| 145 | self.disable_button() |
---|
| 146 | self._p.quit() |
---|
| 147 | |
---|
| 148 | def enable_button(self): |
---|
| 149 | if self.button: return |
---|
| 150 | self.bStat.setEnabled(True) |
---|
| 151 | self.button = True |
---|
| 152 | self.spec_show() |
---|
| 153 | |
---|
| 154 | def disable_button(self): |
---|
| 155 | if not self.button: return |
---|
| 156 | self.bStat.setChecked(False) |
---|
| 157 | self.bStat.setDisabled(True) |
---|
| 158 | self.button = False |
---|
| 159 | self.mode = '' |
---|
| 160 | self.__disconnect_event() |
---|
| 161 | |
---|
| 162 | def enable_next(self): |
---|
| 163 | self.bNext.setEnabled(True) |
---|
| 164 | |
---|
| 165 | def disable_next(self): |
---|
| 166 | self.bNext.setDisabled(True) |
---|
| 167 | |
---|
| 168 | def enable_prev(self): |
---|
| 169 | self.bPrev.setEnabled(True) |
---|
| 170 | |
---|
| 171 | def disable_prev(self): |
---|
| 172 | self.bPrev.setDisabled(True) |
---|
| 173 | |
---|
| 174 | # pause buttons for slow operations |
---|
| 175 | def _pause_buttons(self,operation="end",msg=""): |
---|
| 176 | buttons = ["bStat","bNote","bQuit"] |
---|
| 177 | if operation == "start": |
---|
| 178 | enable = False |
---|
| 179 | else: |
---|
| 180 | enable = True |
---|
| 181 | for btn in buttons: |
---|
| 182 | getattr(self,btn).setEnabled(enable) |
---|
| 183 | self.figmgr.toolbar.set_message(msg) |
---|
| 184 | |
---|
| 185 | def delete_bar(self): |
---|
| 186 | self.__disconnect_event() |
---|
| 187 | self.destroy() |
---|
| 188 | |
---|
| 189 | def __disconnect_event(self): |
---|
| 190 | self._p.register('button_press',None) |
---|
| 191 | self._p.register('button_release',None) |
---|
| 192 | |
---|
[2172] | 193 | def _draw_span(self,axes,x0,x1,**kwargs): |
---|
| 194 | height = self._p.figure.bbox.height |
---|
| 195 | y1 = height - axes.bbox.y1 |
---|
| 196 | h = axes.bbox.height |
---|
| 197 | w = abs(x1 - x0) |
---|
| 198 | rect = [ int(val) for val in min(x0,x1), y1, w, h ] |
---|
| 199 | self._p.canvas.drawRectangle( rect ) |
---|
| 200 | # nothing is returned by drawRectangle |
---|
| 201 | return None |
---|
[2155] | 202 | |
---|
[2172] | 203 | def _remove_span(self,span): |
---|
[2173] | 204 | # Nothing to do with remove. just refresh (call only once) |
---|
[2172] | 205 | self.canvas.draw() |
---|
[2168] | 206 | |
---|
| 207 | |
---|
[2172] | 208 | |
---|
[2168] | 209 | ###################################### |
---|
| 210 | ## Notation box window ## |
---|
| 211 | ###################################### |
---|
| 212 | class NotationWindowQT4Agg(NotationWindowCommon): |
---|
| 213 | """ |
---|
| 214 | Backend based class to create widgets to add, modify, or delete |
---|
| 215 | note on the plot. |
---|
| 216 | |
---|
| 217 | Note: |
---|
| 218 | Press LEFT-mouse button on the plot to ADD a note on the canvas. |
---|
| 219 | A notation window will be loaded for specifying note string and |
---|
| 220 | anchor. The note will be anchored on a position in whether figure- |
---|
| 221 | (0-1 relative in a figure), panel- (0-1 relative in a plot axes), |
---|
| 222 | or data-coordinate (data value in a plot axes). |
---|
| 223 | Press RIGHT-mouse button on a note to MODIFY/DELETE it. A cascade |
---|
| 224 | menu will be displayed and you can select an operation. |
---|
| 225 | """ |
---|
| 226 | def __init__(self,master=None): |
---|
| 227 | self.parent = master |
---|
| 228 | NotationWindowCommon.__init__(self,master=master) |
---|
| 229 | self.anchval = None |
---|
| 230 | self.textwin = self._create_textwindow(master=None) |
---|
| 231 | self.menu = self._create_modmenu(master=self.parent) |
---|
| 232 | |
---|
| 233 | ### Notation window widget |
---|
| 234 | def _create_textwindow(self,master=None): |
---|
| 235 | """Create notation window widget and iconfy it""" |
---|
| 236 | #twin = qt.QtGui.QWidget(parent=master, flags=qt.QtCore.Qt.Popup) |
---|
| 237 | twin = qt.QtGui.QWidget(parent=master, flags=qt.QtCore.Qt.Dialog) |
---|
| 238 | twin.setWindowTitle("Notation") |
---|
| 239 | self.textbox = self._NotationBox(parent=twin) |
---|
| 240 | radiobox = self._AnchorRadio(parent=twin) |
---|
| 241 | self.actionbs = self._ActionButtons(parent=twin) |
---|
| 242 | vbox = qt.QtGui.QVBoxLayout(twin) |
---|
| 243 | vbox.addWidget(self.textbox) |
---|
| 244 | vbox.addWidget(radiobox) |
---|
| 245 | vbox.addLayout(self.actionbs) |
---|
| 246 | twin.setLayout(vbox) |
---|
| 247 | #twin.setCentralWidget(self.textbox) |
---|
| 248 | twin.hide() |
---|
| 249 | return twin |
---|
| 250 | |
---|
| 251 | def _NotationBox(self,parent=None): |
---|
| 252 | textbox = qt.QtGui.QPlainTextEdit(parent=parent) |
---|
| 253 | textbox.setStyleSheet("background-color: white") |
---|
| 254 | fmetric = qt.QtGui.QFontMetrics(textbox.currentCharFormat().font()) |
---|
| 255 | textbox.resize(fmetric.width("A")*20+fmetric.leading()*2, |
---|
| 256 | fmetric.height()*2+fmetric.ascent()+fmetric.descent()) |
---|
| 257 | del fmetric |
---|
| 258 | textbox.setMinimumSize(textbox.size()) |
---|
| 259 | textbox.setUndoRedoEnabled(True) |
---|
| 260 | textbox.setMidLineWidth(3) |
---|
| 261 | textbox.setFrameShadow(qt.QtGui.QFrame.Sunken) |
---|
| 262 | textbox.setCursor(qt.QtCore.Qt.IBeamCursor) |
---|
| 263 | textbox.setFocus() |
---|
| 264 | return textbox |
---|
| 265 | |
---|
| 266 | def _AnchorRadio(self,parent=None): |
---|
| 267 | # Returns a QGoupBox object which includes radio butons to |
---|
| 268 | # select an anchor |
---|
| 269 | anchbox = qt.QtGui.QGroupBox("anchor",parent=parent) |
---|
| 270 | self.radio = qt.QtGui.QButtonGroup(parent=anchbox) |
---|
| 271 | self.rFig = self._NewRadioButton(anchbox,"figure",\ |
---|
| 272 | bgr=self.radio,value=0,\ |
---|
| 273 | balloon="a fixed position in figure") |
---|
| 274 | self.rAxis = self._NewRadioButton(anchbox,"panel",\ |
---|
| 275 | bgr=self.radio,value=1,\ |
---|
| 276 | balloon="a fixed realtive position in subplot") |
---|
| 277 | self.rData = self._NewRadioButton(anchbox,"data",\ |
---|
| 278 | bgr=self.radio,value=2,\ |
---|
| 279 | balloon="a fixed data position in subplot") |
---|
| 280 | hbox = qt.QtGui.QHBoxLayout(anchbox) |
---|
| 281 | hbox.addWidget(self.rFig) |
---|
| 282 | hbox.addWidget(self.rAxis) |
---|
| 283 | hbox.addWidget(self.rData) |
---|
| 284 | anchbox.setLayout(hbox) |
---|
| 285 | # set initial selection "figure" |
---|
| 286 | self.rFig.setChecked(True) |
---|
| 287 | self.radio.setExclusive(True) |
---|
| 288 | self.anchval = self.radio.checkedId() |
---|
| 289 | return anchbox |
---|
| 290 | |
---|
| 291 | def _NewRadioButton(self,parent,text,balloon=None,bgr=None,value=None): |
---|
| 292 | rb= qt.QtGui.QRadioButton(text,parent=parent) |
---|
| 293 | if bgr: |
---|
| 294 | if value is not None: |
---|
| 295 | bgr.addButton(rb,value) |
---|
| 296 | else: |
---|
| 297 | bgr.addButton(rb) |
---|
| 298 | if balloon: rb.setToolTip(balloon) |
---|
| 299 | return rb |
---|
| 300 | |
---|
| 301 | def _enable_radio(self): |
---|
| 302 | """Enable 'panel' and 'data' radio button""" |
---|
| 303 | self.rAxis.setEnabled(True) |
---|
| 304 | self.rData.setEnabled(True) |
---|
| 305 | # select Figure as the default value |
---|
| 306 | self.rFig.setChecked(True) |
---|
| 307 | self.anchval = self.radio.checkedId() |
---|
| 308 | |
---|
| 309 | def _reset_radio(self): |
---|
| 310 | """Disable 'panel' and 'data' radio button""" |
---|
| 311 | self.rAxis.setDisabled(True) |
---|
| 312 | self.rData.setDisabled(True) |
---|
| 313 | self.rFig.setEnabled(True) |
---|
| 314 | # select Figure as the default value |
---|
| 315 | self.rFig.setChecked(True) |
---|
| 316 | self.anchval = self.radio.checkedId() |
---|
| 317 | |
---|
| 318 | def _select_radio(self,selection): |
---|
| 319 | """Select a specified radio button""" |
---|
| 320 | if not selection in self.anchors: |
---|
| 321 | return |
---|
| 322 | if selection == "data": |
---|
| 323 | self.rData.setChecked(True) |
---|
| 324 | elif selection == "axes": |
---|
| 325 | self.rAxis.setChecked(True) |
---|
| 326 | else: |
---|
| 327 | self.rFig.setChecked(True) |
---|
| 328 | self.anchval = self.radio.checkedId() |
---|
| 329 | |
---|
| 330 | def _get_anchval(self): |
---|
| 331 | """Returns a integer of a selected radio button""" |
---|
| 332 | self.anchval = self.radio.checkedId() |
---|
| 333 | return self.anchval |
---|
| 334 | |
---|
| 335 | def _get_note(self): |
---|
| 336 | """Returns a note string specified in the text box""" |
---|
| 337 | return str(self.textbox.toPlainText()) |
---|
| 338 | |
---|
| 339 | def _clear_textbox(self): |
---|
| 340 | """Clear the text box""" |
---|
| 341 | self.textbox.clear() |
---|
| 342 | |
---|
| 343 | def _set_note(self,note=None): |
---|
| 344 | """Set a note string to the text box""" |
---|
| 345 | self._clear_textbox() |
---|
| 346 | if len(note) >0: |
---|
| 347 | self.textbox.setPlainText(note) |
---|
| 348 | |
---|
| 349 | def _ActionButtons(self,parent=None): |
---|
| 350 | # Returns a layout object which includes "cancel" and "print" buttons |
---|
| 351 | actbuts = qt.QtGui.QHBoxLayout() |
---|
| 352 | bCancel = self._NewButton(parent,"cancel",self._cancel_text,\ |
---|
| 353 | addit=False,\ |
---|
| 354 | balloon="cancel printing/modifying") |
---|
| 355 | bPrint = self._NewButton(parent,"print", self._print_text,\ |
---|
| 356 | addit=False,\ |
---|
| 357 | balloon="print text on plot") |
---|
| 358 | actbuts.addWidget(bCancel) |
---|
| 359 | actbuts.addWidget(bPrint) |
---|
| 360 | return actbuts |
---|
| 361 | |
---|
| 362 | def _NewButton(self, parent, text, command, balloon=None, addit=True): |
---|
| 363 | b = qt.QtGui.QPushButton(text,parent=parent) |
---|
| 364 | if balloon: b.setToolTip(balloon) |
---|
| 365 | if addit: parent.addWidget(b) |
---|
| 366 | parent.connect(b,qt.QtCore.SIGNAL('clicked()'),command) |
---|
| 367 | return b |
---|
| 368 | |
---|
| 369 | def _cancel_text(self): |
---|
| 370 | """ |
---|
| 371 | Cancel adding/modifying a note and close notaion window. |
---|
| 372 | called when 'cancel' is selected. |
---|
| 373 | """ |
---|
| 374 | self.close_textwindow() |
---|
| 375 | |
---|
| 376 | def _print_text(self): |
---|
| 377 | """ |
---|
| 378 | Add/Modify a note. Called when 'print' is selected on the |
---|
| 379 | notation window. |
---|
| 380 | """ |
---|
| 381 | self.print_text() |
---|
| 382 | self.close_textwindow() |
---|
| 383 | |
---|
| 384 | def load_textwindow(self,event): |
---|
| 385 | """ |
---|
| 386 | Load text window at a event position to add a note on a plot. |
---|
| 387 | Parameter: |
---|
| 388 | event: an even object to specify the position to load |
---|
| 389 | text window. |
---|
| 390 | """ |
---|
| 391 | self.close_modmenu() |
---|
| 392 | if event.canvas != self.parent: |
---|
| 393 | raise RuntimeError, "Got invalid event!" |
---|
| 394 | |
---|
| 395 | self.event = event |
---|
| 396 | is_ax = (event.inaxes != None) |
---|
| 397 | (xpix, ypix) = self._disppix2screen(event.x, event.y) |
---|
| 398 | offset = 5 |
---|
| 399 | self.show_textwindow(xpix+offset,ypix+offset,enableaxes=is_ax) |
---|
| 400 | |
---|
| 401 | def show_textwindow(self,xpix,ypix,basetext=None,enableaxes=False): |
---|
| 402 | """ |
---|
| 403 | Load text window at a position of screen to add a note on a plot. |
---|
| 404 | Parameters: |
---|
| 405 | xpix, ypix: a pixel position from Upper-left corner |
---|
| 406 | of the screen. |
---|
| 407 | basetext: None (default) or any string. |
---|
| 408 | A string to be printed on text box when loaded. |
---|
| 409 | enableaxes: False (default) or True. |
---|
| 410 | If True, 'panel' & 'data' radio button is enabled. |
---|
| 411 | """ |
---|
| 412 | if not self.textwin: return |
---|
| 413 | self._reset_radio() |
---|
| 414 | if enableaxes: |
---|
| 415 | self._enable_radio() |
---|
| 416 | self.textwin.activateWindow() |
---|
| 417 | h = self.textwin.minimumHeight() |
---|
| 418 | w = self.textwin.minimumWidth() |
---|
| 419 | self.textwin.resize(w,h) |
---|
| 420 | self.textwin.move(xpix,ypix) |
---|
| 421 | self.textbox.setFocus() |
---|
| 422 | self.textwin.raise_() |
---|
| 423 | self.textwin.show() |
---|
| 424 | if w*h <= 1: # Initial load |
---|
| 425 | self.textwin.setMinimumSize(self.textwin.size()) |
---|
| 426 | |
---|
| 427 | def close_textwindow(self): |
---|
| 428 | """Close text window.""" |
---|
| 429 | self.seltext = {} |
---|
| 430 | self._reset_radio() |
---|
| 431 | self._clear_textbox() |
---|
| 432 | self.textwin.hide() |
---|
| 433 | |
---|
| 434 | |
---|
| 435 | ### Modify/Delete menu widget |
---|
| 436 | def _create_modmenu(self,master=None): |
---|
| 437 | """Create modify/delete menu widget""" |
---|
| 438 | if master: |
---|
| 439 | self.parent = master |
---|
| 440 | if not self.parent: |
---|
| 441 | return False |
---|
| 442 | menu = qt.QtGui.QMenu(parent=self.parent) |
---|
| 443 | menu.setTearOffEnabled(False) |
---|
| 444 | menu.addAction("Modify",self._modify_note) |
---|
| 445 | menu.addAction("Delete",self._delnote_dialog) |
---|
| 446 | return menu |
---|
| 447 | |
---|
| 448 | def load_modmenu(self,event): |
---|
| 449 | """ |
---|
| 450 | Load cascade menu at a event position to modify or delete |
---|
| 451 | selected text. |
---|
| 452 | Parameter: |
---|
| 453 | event: an even object to specify the position to load |
---|
| 454 | text window. |
---|
| 455 | """ |
---|
| 456 | self.close_textwindow() |
---|
| 457 | self.seltext = self._get_selected_text(event) |
---|
| 458 | if len(self.seltext) == 3: |
---|
| 459 | canvas = event.canvas |
---|
| 460 | corig = canvas.mapToGlobal(qt.QtCore.QPoint(0,0)) |
---|
| 461 | xpixs = corig.x() + int(event.x) |
---|
| 462 | ypixs = corig.y() + canvas.height() - int(event.y) |
---|
| 463 | self.menu.activateWindow() |
---|
| 464 | self.menu.move(xpixs,ypixs) |
---|
| 465 | self.menu.show() |
---|
| 466 | |
---|
| 467 | def close_modmenu(self): |
---|
| 468 | """Close cascade menu.""" |
---|
| 469 | self.seltext = {} |
---|
| 470 | self.menu.hide() |
---|
| 471 | |
---|
| 472 | ### load text window for modification |
---|
| 473 | def _modify_note(self): |
---|
| 474 | """helper function to load text window to modify selected note""" |
---|
| 475 | textobj = self.seltext['textobj'] |
---|
| 476 | (xtx, ytx) = textobj._get_xy_display() |
---|
| 477 | is_ax = (self.seltext['anchor'] != 'figure') |
---|
| 478 | if not is_ax: |
---|
| 479 | # previous anchor is figure |
---|
| 480 | pos = textobj.get_position() |
---|
| 481 | is_ax = (self._get_axes_from_pos(pos,self.canvas) != None) |
---|
| 482 | |
---|
| 483 | (xpix, ypix) = self._disppix2screen(xtx,ytx) |
---|
| 484 | offset = int(textobj.get_size())*2 |
---|
| 485 | self.show_textwindow(xpix,ypix+offset,basetext=textobj.get_text(),\ |
---|
| 486 | enableaxes=is_ax) |
---|
| 487 | self._select_radio(self.seltext['anchor']) |
---|
| 488 | self._set_note(textobj.get_text()) |
---|
| 489 | |
---|
| 490 | ### close all widgets |
---|
| 491 | def close_widgets(self): |
---|
| 492 | """Close note window and menu""" |
---|
| 493 | self.close_textwindow() |
---|
| 494 | self.close_modmenu() |
---|
| 495 | |
---|
| 496 | ### dialog to confirm deleting note |
---|
| 497 | def _delnote_dialog(self): |
---|
| 498 | """Load dialog to confirm deletion of the text""" |
---|
| 499 | remind = "Delete text?\n '"+self.seltext['textobj'].get_text()+"'" |
---|
| 500 | from PyQt4.QtGui import QMessageBox as mbox |
---|
| 501 | answer = mbox.question(self.parent,"Delete?",remind, |
---|
| 502 | buttons = mbox.Ok | mbox.Cancel, |
---|
| 503 | defaultButton=mbox.Cancel) |
---|
| 504 | if answer == mbox.Ok: |
---|
| 505 | self.delete_note() |
---|
| 506 | else: |
---|
| 507 | self.cancel_delete() |
---|
| 508 | |
---|
| 509 | ### helper functions |
---|
| 510 | def _disppix2screen(self,xpixd,ypixd): |
---|
| 511 | """ |
---|
| 512 | helper function to calculate a pixel position form Upper-left |
---|
| 513 | corner of the SCREEN from a pixel position (xpixd, ypixd) |
---|
| 514 | from Lower-left of the CANVAS (which, e.g., event.x/y returns) |
---|
| 515 | |
---|
| 516 | Returns: |
---|
| 517 | (x, y): pixel position from Upper-left corner of the SCREEN. |
---|
| 518 | """ |
---|
| 519 | corig = self.parent.mapToGlobal(qt.QtCore.QPoint(0,0)) |
---|
| 520 | xpixs = corig.x() + xpixd |
---|
| 521 | ypixs = corig.y() + self.parent.height() - ypixd |
---|
| 522 | return (int(xpixs), int(ypixs)) |
---|
| 523 | |
---|
| 524 | |
---|
| 525 | |
---|
| 526 | |
---|
| 527 | |
---|
| 528 | |
---|
| 529 | ########################################### |
---|
| 530 | ## Add CASA custom Flag toolbar ## |
---|
| 531 | ########################################### |
---|
[2173] | 532 | class CustomFlagToolbarQT4Agg(CustomFlagToolbarCommon, qt.QtGui.QToolBar): |
---|
| 533 | def __init__(self,parent): |
---|
| 534 | from asap.asapplotter import asapplotter |
---|
| 535 | if not isinstance(parent,asapplotter): |
---|
| 536 | return False |
---|
| 537 | if not parent._plotter: |
---|
| 538 | return False |
---|
| 539 | self._p = parent._plotter |
---|
| 540 | self.figmgr = self._p.figmgr |
---|
| 541 | self.canvas = self.figmgr.canvas |
---|
| 542 | self.mode = '' |
---|
| 543 | self.button = True |
---|
| 544 | self.pagecount = None |
---|
| 545 | CustomFlagToolbarCommon.__init__(self,parent) |
---|
| 546 | self.notewin=NotationWindowQT4Agg(master=self.canvas) |
---|
| 547 | self._add_custom_toolbar() |
---|
[2172] | 548 | |
---|
[2173] | 549 | def _add_custom_toolbar(self): |
---|
| 550 | qt.QtGui.QToolBar.__init__(self,parent=self.figmgr.window) |
---|
| 551 | self.figmgr.window.addToolBar(qt.QtCore.Qt.BottomToolBarArea,self) |
---|
| 552 | self.bRegion = self._NewButton(master=self, |
---|
| 553 | text='region', |
---|
| 554 | command=self.select_region, |
---|
| 555 | balloon="select channel regions") |
---|
| 556 | self.bRegion.setCheckable(True) |
---|
[2172] | 557 | |
---|
[2173] | 558 | self.bPanel = self._NewButton(master=self, |
---|
| 559 | text='panel', |
---|
| 560 | command=self.select_panel, |
---|
| 561 | balloon="select a whole spectrum") |
---|
| 562 | self.bPanel.setCheckable(True) |
---|
[2172] | 563 | |
---|
[2173] | 564 | self.bClear = self._NewButton(master=self, |
---|
| 565 | text='clear', |
---|
| 566 | command=self.cancel_select, |
---|
| 567 | balloon="clear selections") |
---|
[2172] | 568 | |
---|
[2173] | 569 | self.bFlag = self._NewButton(master=self, |
---|
| 570 | text='flag', |
---|
| 571 | command=self.flag, |
---|
| 572 | balloon="flag selections") |
---|
[2172] | 573 | |
---|
[2173] | 574 | self.bUnflag = self._NewButton(master=self, |
---|
| 575 | text='unflag', |
---|
| 576 | command=self.unflag, |
---|
| 577 | balloon="unflag selections") |
---|
[2172] | 578 | |
---|
[2173] | 579 | self.bStat = self._NewButton(master=self, |
---|
| 580 | text='statistics', |
---|
| 581 | command=self.stat_cal, |
---|
| 582 | balloon="print statistics of selections") |
---|
[2172] | 583 | |
---|
[2173] | 584 | self.bNote = self._NewButton(master=self, |
---|
| 585 | text='notation', |
---|
| 586 | command=self.modify_note, |
---|
| 587 | balloon="add note on plot") |
---|
| 588 | self.bNote.setCheckable(True) |
---|
[2172] | 589 | |
---|
[2173] | 590 | # page change oparations |
---|
| 591 | frPage = qt.QtGui.QWidget(parent=self,flags=qt.QtCore.Qt.Tool) |
---|
| 592 | loPage = qt.QtGui.QHBoxLayout(self) |
---|
| 593 | loPage.addStretch(1) |
---|
| 594 | self.lPagetitle = qt.QtGui.QLabel('Page:',parent=frPage) |
---|
| 595 | self.lPagetitle.setMargin(5) |
---|
| 596 | loPage.addWidget(self.lPagetitle) |
---|
| 597 | self.pagecount = qt.QtGui.QLabel(parent=frPage) |
---|
| 598 | self.pagecount.setStyleSheet("background-color: white") |
---|
| 599 | self.pagecount.setMargin(3) |
---|
| 600 | self.pagecount.setText(' 1') |
---|
| 601 | loPage.addWidget(self.pagecount) |
---|
[2172] | 602 | |
---|
[2173] | 603 | self.bNext = self._NewButton(master=frPage, |
---|
| 604 | text='+', |
---|
| 605 | #imagename="hand.ppm", |
---|
| 606 | command=self.next_page, |
---|
| 607 | addit=False, |
---|
| 608 | balloon="plot next page") |
---|
| 609 | loPage.addWidget(self.bNext) |
---|
| 610 | self.bPrev = self._NewButton(master=frPage, |
---|
| 611 | text='-', |
---|
| 612 | command=self.prev_page, |
---|
| 613 | addit=False, |
---|
| 614 | balloon="plot previous page") |
---|
| 615 | loPage.addWidget(self.bPrev) |
---|
| 616 | frPage.setLayout(loPage) |
---|
| 617 | self.addWidget(frPage) |
---|
[2172] | 618 | |
---|
[2173] | 619 | self.bQuit = self._NewButton(master=self, |
---|
| 620 | text='Quit', |
---|
| 621 | #imagename="stock_close.ppm", |
---|
| 622 | command=self.quit, |
---|
| 623 | balloon="Close window") |
---|
[2172] | 624 | |
---|
[2173] | 625 | self.pagecount.setText(' '*4) |
---|
| 626 | self.disable_button() |
---|
| 627 | return |
---|
[2172] | 628 | |
---|
[2173] | 629 | def _NewButton(self, master, text, command, balloon=None,addit=True,imagename=None): |
---|
| 630 | img = None |
---|
| 631 | if imagename: |
---|
| 632 | imagename = os.path.join(matplotlib.rcParams['datapath'], 'images', imagename) |
---|
| 633 | # img = Tk.PhotoImage(master=master, file=imagename) |
---|
[2172] | 634 | |
---|
[2173] | 635 | b = qt.QtGui.QPushButton(text,parent=master) |
---|
| 636 | if balloon: b.setToolTip(balloon) |
---|
| 637 | if addit: master.addWidget(b) |
---|
| 638 | master.connect(b,qt.QtCore.SIGNAL('clicked()'),command) |
---|
| 639 | # if img: b.image = img |
---|
| 640 | return b |
---|
[2172] | 641 | |
---|
[2173] | 642 | def show_pagenum(self,pagenum,formatstr): |
---|
| 643 | self.pagecount.setText(formatstr % (pagenum)) |
---|
[2172] | 644 | |
---|
[2173] | 645 | def spec_show(self): |
---|
| 646 | if not self.figmgr.toolbar.mode == '' or not self.button: return |
---|
| 647 | self.figmgr.toolbar.set_message('spec value: drag on a spec') |
---|
| 648 | if self.mode == 'spec': return |
---|
| 649 | self.mode = 'spec' |
---|
| 650 | self.notewin.close_widgets() |
---|
| 651 | self.__disconnect_event() |
---|
| 652 | self._p.register('button_press',self._select_spectrum) |
---|
[2172] | 653 | |
---|
[2173] | 654 | def modify_note(self): |
---|
| 655 | if not self.figmgr.toolbar.mode == '': |
---|
| 656 | # Get back button status BEFORE clicked |
---|
| 657 | self.bNote.setChecked(not self.bNote.isChecked()) |
---|
| 658 | return |
---|
| 659 | if self.mode == 'note': |
---|
| 660 | self.bNote.setChecked(False) |
---|
| 661 | self.bNote.setToolTip("add note on plot") |
---|
| 662 | self.mode = 'none' |
---|
| 663 | self.spec_show() |
---|
| 664 | return |
---|
| 665 | self.figmgr.toolbar.set_message('text: select a position/text') |
---|
| 666 | self.bNote.setChecked(True) |
---|
| 667 | self.bNote.setToolTip("Back to spec value mode") |
---|
| 668 | self.bRegion.setChecked(False) |
---|
| 669 | self.bRegion.setToolTip("select channel regions") |
---|
| 670 | self.bPanel.setChecked(False) |
---|
| 671 | self.bPanel.setToolTip("select a whole spectrum") |
---|
| 672 | self.mode = 'note' |
---|
| 673 | self.__disconnect_event() |
---|
| 674 | self._p.register('button_press',self._mod_note) |
---|
[2172] | 675 | |
---|
[2173] | 676 | def select_region(self): |
---|
| 677 | if not self.figmgr.toolbar.mode == '' or not self.button: |
---|
| 678 | # Get back button status BEFORE clicked |
---|
| 679 | self.bRegion.setChecked(not self.bRegion.isChecked()) |
---|
| 680 | if self.mode == 'region': |
---|
| 681 | self.bRegion.setChecked(False) |
---|
| 682 | self.bRegion.setToolTip("select channel regions") |
---|
| 683 | self.mode = 'none' |
---|
| 684 | self.spec_show() |
---|
| 685 | return |
---|
| 686 | self.figmgr.toolbar.set_message('select regions: click at start and end channels') |
---|
| 687 | self.bNote.setChecked(False) |
---|
| 688 | self.bNote.setToolTip("add note on plot") |
---|
| 689 | self.bRegion.setChecked(True) |
---|
| 690 | self.bRegion.setToolTip("Back to spec value mode") |
---|
| 691 | self.bPanel.setChecked(False) |
---|
| 692 | self.bPanel.setToolTip("select a whole spectrum") |
---|
| 693 | self.mode = 'region' |
---|
| 694 | self.notewin.close_widgets() |
---|
| 695 | self.__disconnect_event() |
---|
| 696 | self._p.register('button_press',self._add_region) |
---|
[2172] | 697 | |
---|
[2173] | 698 | def select_panel(self): |
---|
| 699 | if not self.figmgr.toolbar.mode == '' or not self.button: |
---|
| 700 | # Get back button status BEFORE clicked |
---|
| 701 | self.bPanel.setChecked(not self.bPanel.isChecked()) |
---|
| 702 | return |
---|
| 703 | if self.mode == 'panel': |
---|
| 704 | self.bPanel.setChecked(False) |
---|
| 705 | self.bPanel.setToolTip("select subplots") |
---|
| 706 | self.mode = 'none' |
---|
| 707 | self.spec_show() |
---|
| 708 | return |
---|
| 709 | self.figmgr.toolbar.set_message('select spectra: click on subplots') |
---|
| 710 | self.bNote.setChecked(False) |
---|
| 711 | self.bNote.setToolTip("add note on plot") |
---|
| 712 | self.bRegion.setChecked(False) |
---|
| 713 | self.bRegion.setToolTip("select channel regions") |
---|
| 714 | self.bPanel.setChecked(True) |
---|
| 715 | self.bPanel.setToolTip("Back to spec value mode") |
---|
| 716 | self.mode = 'panel' |
---|
| 717 | self.notewin.close_widgets() |
---|
| 718 | self.__disconnect_event() |
---|
| 719 | self._p.register('button_press',self._add_panel) |
---|
[2172] | 720 | |
---|
[2173] | 721 | def quit(self): |
---|
| 722 | self.__disconnect_event() |
---|
| 723 | self.disable_button() |
---|
| 724 | self._p.unmap() |
---|
[2172] | 725 | |
---|
[2173] | 726 | def enable_button(self): |
---|
| 727 | if self.button: return |
---|
| 728 | self.bRegion.setEnabled(True) |
---|
| 729 | self.bPanel.setEnabled(True) |
---|
| 730 | self.bClear.setEnabled(True) |
---|
| 731 | self.bFlag.setEnabled(True) |
---|
| 732 | self.bUnflag.setEnabled(True) |
---|
| 733 | self.bStat.setEnabled(True) |
---|
| 734 | self.button = True |
---|
| 735 | self.spec_show() |
---|
[2172] | 736 | |
---|
[2173] | 737 | def disable_button(self): |
---|
| 738 | ## disable buttons which don't work for plottp |
---|
| 739 | if not self.button: return |
---|
| 740 | self.bRegion.setChecked(False) |
---|
| 741 | self.bRegion.setToolTip("select channel regions") |
---|
| 742 | self.bPanel.setChecked(False) |
---|
| 743 | self.bPanel.setToolTip("select subplots") |
---|
[2172] | 744 | |
---|
[2173] | 745 | self.bRegion.setDisabled(True) |
---|
| 746 | self.bPanel.setDisabled(True) |
---|
| 747 | self.bClear.setDisabled(True) |
---|
| 748 | self.bFlag.setDisabled(True) |
---|
| 749 | self.bUnflag.setDisabled(True) |
---|
| 750 | self.bStat.setDisabled(True) |
---|
| 751 | self.bNext.setDisabled(True) |
---|
| 752 | self.bPrev.setDisabled(True) |
---|
| 753 | self.button = False |
---|
| 754 | self.mode = '' |
---|
| 755 | self.notewin.close_widgets() |
---|
| 756 | self.__disconnect_event() |
---|
[2172] | 757 | |
---|
[2173] | 758 | def enable_next(self): |
---|
| 759 | self.bNext.setEnabled(True) |
---|
[2172] | 760 | |
---|
[2173] | 761 | def disable_next(self): |
---|
| 762 | self.bNext.setDisabled(True) |
---|
[2172] | 763 | |
---|
[2173] | 764 | def enable_prev(self): |
---|
| 765 | self.bPrev.setEnabled(True) |
---|
[2172] | 766 | |
---|
[2173] | 767 | def disable_prev(self): |
---|
| 768 | self.bPrev.setDisabled(True) |
---|
[2172] | 769 | |
---|
[2173] | 770 | # pause buttons for slow operations |
---|
| 771 | def _pause_buttons(self,operation="end",msg=""): |
---|
| 772 | buttons = ["bRegion","bPanel","bClear","bFlag","bUnflag","bStat", |
---|
| 773 | "bNote","bQuit"] |
---|
| 774 | if operation == "start": |
---|
| 775 | enable = False |
---|
| 776 | else: |
---|
| 777 | enable = True |
---|
| 778 | for btn in buttons: |
---|
| 779 | getattr(self,btn).setEnabled(enable) |
---|
| 780 | self.figmgr.toolbar.set_message(msg) |
---|
[2172] | 781 | |
---|
[2173] | 782 | def delete_bar(self): |
---|
| 783 | self.__disconnect_event() |
---|
| 784 | self.destroy() |
---|
[2172] | 785 | |
---|
[2173] | 786 | def __disconnect_event(self): |
---|
| 787 | self._p.register('button_press',None) |
---|
| 788 | self._p.register('button_release',None) |
---|
[2172] | 789 | |
---|
[2173] | 790 | def _draw_span(self,axes,x0,x1,**kwargs): |
---|
| 791 | height = self._p.figure.bbox.height |
---|
| 792 | y1 = height - axes.bbox.y1 |
---|
| 793 | h = axes.bbox.height |
---|
| 794 | w = abs(x1 - x0) |
---|
| 795 | rect = [ int(val) for val in min(x0,x1), y1, w, h ] |
---|
| 796 | self._p.canvas.drawRectangle( rect ) |
---|
| 797 | # nothing is returned by drawRectangle |
---|
| 798 | return None |
---|
[2172] | 799 | |
---|
[2173] | 800 | def _remove_span(self,span): |
---|
| 801 | # Nothing to do with remove. |
---|
| 802 | pass |
---|