Changeset 6f16e25 in sasview for src/sas/guiframe
- Timestamp:
- Oct 21, 2015 8:35:00 AM (9 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 85130cb
- Parents:
- 2d88fc4
- Location:
- src/sas/guiframe
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/guiframe/gui_toolbar.py
r79492222 r6f16e25 36 36 Implement toolbar for guiframe 37 37 """ 38 ID_BOOKMARK = wx.NewId() 38 39 def __init__(self, parent, *args, **kwds): 39 40 Tbar.__init__(self, parent, *args, **kwds) … … 133 134 Add default items in bookmark menu 134 135 """ 135 id = wx.NewId() 136 self._bookmark_menu.Append(id, 'Bookmark This Page State') 136 self._bookmark_menu.Append(self.ID_BOOKMARK, 'Bookmark This Page State') 137 137 self._bookmark_menu.AppendSeparator() 138 wx.EVT_MENU(self, id, self.on_bookmark)138 wx.EVT_MENU(self, self.ID_BOOKMARK, self.on_bookmark) 139 139 140 140 def on_bind_button(self): -
src/sas/guiframe/local_perspectives/plotting/Plotter1D.py
r2d88fc4 r6f16e25 19 19 from sas.guiframe.events import StatusEvent 20 20 from sas.guiframe.events import PanelOnFocusEvent 21 from sas.guiframe.utils import PanelMenu 21 from sas.guiframe.utils import PanelMenu, IdList 22 22 from sas.guiframe.panel_base import PanelBase 23 23 from sas.guiframe.gui_style import GUIFRAME_ICON … … 35 35 """return the key of dictionary dic given the value""" 36 36 return [k for k, v in dic.iteritems() if v == val][0] 37 38 class IdCollection:39 def __init__(self):40 self._ids = []41 self._index = 042 def next(self):43 if len(self._ids) == self._index:44 self._ids.append(wx.NewId())45 self._index += 146 return self._ids[self._index-1]47 def reset(self):48 self._index = 049 50 37 51 38 class ModelPanel1D(PlotPanel, PanelBase): … … 63 50 ## Group ID 64 51 group_id = None 65 _menu_ids = Id Collection()52 _menu_ids = IdList() 66 53 67 54 def __init__(self, parent, id=-1, color=None, … … 80 67 81 68 self._available_data = [] 82 self._menu_add_ids = []83 69 self._symbol_labels = self.get_symbol_label() 84 70 self._color_labels = self.get_color_label() … … 568 554 self._slicerpop.set_plots(self.plots) 569 555 self._slicerpop.set_graph(self.graph) 570 self._menu_ids.reset()556 ids = iter(self._menu_ids) 571 557 if not self.graph.selected_plottable in self.plots: 572 558 # Various plot options 573 wx_id = self._menu_ids.next()559 wx_id = ids.next() 574 560 self._slicerpop.Append(wx_id, '&Save Image', 'Save image as PNG') 575 561 wx.EVT_MENU(self, wx_id, self.onSaveImage) 576 wx_id = self._menu_ids.next()562 wx_id = ids.next() 577 563 self._slicerpop.Append(wx_id, '&Print Image', 'Print image ') 578 564 wx.EVT_MENU(self, wx_id, self.onPrint) 579 565 580 wx_id = self._menu_ids.next()566 wx_id = ids.next() 581 567 self._slicerpop.Append(wx_id, '&Copy to Clipboard', 582 568 'Copy to the clipboard') … … 595 581 continue 596 582 597 wx_id = self._menu_ids.next()583 wx_id = ids.next() 598 584 plot_menu.Append(wx_id, "&DataInfo", name) 599 585 wx.EVT_MENU(self, wx_id, self. _onDataShow) 600 wx_id = self._menu_ids.next()586 wx_id = ids.next() 601 587 plot_menu.Append(wx_id, "&Save Points as a File", name) 602 588 wx.EVT_MENU(self, wx_id, self._onSave) … … 605 591 # add menu of other plugins 606 592 item_list = self.parent.get_current_context_menu(self) 607 608 593 if (not item_list == None) and (not len(item_list) == 0): 609 for item in item_list: 594 # Note: reusing menu ids in submenu. This code works because 595 # IdItems is set up as a lazy iterator returning each id in 596 # sequence, creating new ids as needed so it never runs out. 597 # zip() is set up to stop when any iterator is empty, so it 598 # only asks for the number of ids in item_list. 599 for item, wx_id in zip(item_list, self._menu_ids): 610 600 611 601 try: 612 wx_id = self._menu_ids.next()613 602 plot_menu.Append(wx_id, item[0], name) 614 603 wx.EVT_MENU(self, wx_id, item[2]) … … 620 609 621 610 if self.parent.ClassName.count('wxDialog') == 0: 622 wx_id = self._menu_ids.next()611 wx_id = ids.next() 623 612 plot_menu.Append(wx_id, '&Linear Fit', name) 624 613 wx.EVT_MENU(self, wx_id, self.onFitting) 625 614 plot_menu.AppendSeparator() 626 615 627 wx_id = self._menu_ids.next()616 wx_id = ids.next() 628 617 plot_menu.Append(wx_id, "Remove", name) 629 618 wx.EVT_MENU(self, wx_id, self._onRemove) 630 619 if not plot.is_data: 631 wx_id = self._menu_ids.next()620 wx_id = ids.next() 632 621 plot_menu.Append(wx_id, '&Freeze', name) 633 622 wx.EVT_MENU(self, wx_id, self.onFreeze) … … 635 624 636 625 if plot.is_data: 637 wx_id = self._menu_ids.next()626 wx_id = ids.next() 638 627 self.hide_menu = plot_menu.Append(wx_id, "Hide Error Bar", name) 639 628 … … 649 638 plot_menu.AppendSeparator() 650 639 651 wx_id = self._menu_ids.next()640 wx_id = ids.next() 652 641 plot_menu.Append(wx_id, '&Modify Plot Property', name) 653 642 wx.EVT_MENU(self, wx_id, self.createAppDialog) 654 wx_id = self._menu_ids.next()643 wx_id = ids.next() 655 644 # plot_menu.SetTitle(name) 656 645 self._slicerpop.AppendMenu(wx_id, '&%s' % name, plot_menu) … … 661 650 loc_menu = wx.Menu() 662 651 for label in self._loc_labels: 663 wx_id = self._menu_ids.next()652 wx_id = ids.next() 664 653 loc_menu.Append(wx_id, str(label), str(label)) 665 654 wx.EVT_MENU(self, wx_id, self.onChangeLegendLoc) 666 655 667 wx_id = self._menu_ids.next()656 wx_id = ids.next() 668 657 self._slicerpop.Append(wx_id, '&Modify Graph Appearance', 669 658 'Modify graph appearance') … … 673 662 674 663 if self.position != None: 675 wx_id = self._menu_ids.next()664 wx_id = ids.next() 676 665 self._slicerpop.Append(wx_id, '&Add Text') 677 666 wx.EVT_MENU(self, wx_id, self._on_addtext) 678 wx_id = self._menu_ids.next()667 wx_id = ids.next() 679 668 self._slicerpop.Append(wx_id, '&Remove Text') 680 669 wx.EVT_MENU(self, wx_id, self._on_removetext) 681 670 self._slicerpop.AppendSeparator() 682 wx_id = self._menu_ids.next()671 wx_id = ids.next() 683 672 self._slicerpop.Append(wx_id, '&Change Scale') 684 673 wx.EVT_MENU(self, wx_id, self._onProperties) 685 674 self._slicerpop.AppendSeparator() 686 wx_id = self._menu_ids.next()675 wx_id = ids.next() 687 676 self._slicerpop.Append(wx_id, '&Reset Graph Range') 688 677 wx.EVT_MENU(self, wx_id, self.onResetGraph) … … 690 679 if self.parent.ClassName.count('wxDialog') == 0: 691 680 self._slicerpop.AppendSeparator() 692 wx_id = self._menu_ids.next()681 wx_id = ids.next() 693 682 self._slicerpop.Append(wx_id, '&Window Title') 694 683 wx.EVT_MENU(self, wx_id, self.onChangeCaption) -
src/sas/guiframe/local_perspectives/plotting/Plotter2D.py
r2d88fc4 r6f16e25 287 287 288 288 """ 289 self._menu_ids.reset()289 ids = iter(self._menu_ids) 290 290 slicerpop = PanelMenu() 291 291 slicerpop.set_plots(self.plots) 292 292 slicerpop.set_graph(self.graph) 293 293 294 wx_id = self._menu_ids.next()294 wx_id = ids.next() 295 295 slicerpop.Append(wx_id, '&Save Image') 296 296 wx.EVT_MENU(self, wx_id, self.onSaveImage) 297 297 298 wx_id = self._menu_ids.next()298 wx_id = ids.next() 299 299 slicerpop.Append(wx_id, '&Print Image', 'Print image') 300 300 wx.EVT_MENU(self, wx_id, self.onPrint) 301 301 302 wx_id = self._menu_ids.next()302 wx_id = ids.next() 303 303 slicerpop.Append(wx_id, '&Copy to Clipboard', 'Copy to the clipboard') 304 304 wx.EVT_MENU(self, wx_id, self.OnCopyFigureMenu) … … 306 306 # saving data 307 307 plot = self.data2D 308 wx_id = self._menu_ids.next()308 wx_id = ids.next() 309 309 slicerpop.Append(wx_id, "&Data Info") 310 310 wx.EVT_MENU(self, wx_id, self._onDataShow) 311 311 312 wx_id = self._menu_ids.next()312 wx_id = ids.next() 313 313 slicerpop.Append(wx_id, "&Save as a File (DAT)") 314 314 self.action_ids[str(wx_id)] = plot … … 321 321 self.data2D.name.split(" ")[0] != 'Residuals': 322 322 # The line above; Not for trunk 323 for item in item_list: 323 # Note: reusing menu ids for the sub-menus. See Plotter1D. 324 for item, wx_id in zip(item_list, self._menu_ids): 324 325 try: 325 wx_id = self._menu_ids.next()326 326 slicerpop.Append(wx_id, item[0], item[1]) 327 327 wx.EVT_MENU(self, wx_id, item[2]) … … 332 332 slicerpop.AppendSeparator() 333 333 334 wx_id = self._menu_ids.next()334 wx_id = ids.next() 335 335 slicerpop.Append(wx_id, '&Perform Circular Average') 336 336 wx.EVT_MENU(self, wx_id, self.onCircular) \ 337 337 # For Masked Data 338 338 if not plot.mask.all(): 339 wx_id = self._menu_ids.next()339 wx_id = ids.next() 340 340 slicerpop.Append(wx_id, '&Masked Circular Average') 341 341 wx.EVT_MENU(self, wx_id, self.onMaskedCircular) 342 wx_id = self._menu_ids.next()342 wx_id = ids.next() 343 343 slicerpop.Append(wx_id, '&Sector [Q View]') 344 344 wx.EVT_MENU(self, wx_id, self.onSectorQ) 345 wx_id = self._menu_ids.next()345 wx_id = ids.next() 346 346 slicerpop.Append(wx_id, '&Annulus [Phi View ]') 347 347 wx.EVT_MENU(self, wx_id, self.onSectorPhi) 348 wx_id = self._menu_ids.next()348 wx_id = ids.next() 349 349 slicerpop.Append(wx_id, '&Box Sum') 350 350 wx.EVT_MENU(self, wx_id, self.onBoxSum) 351 wx_id = self._menu_ids.next()351 wx_id = ids.next() 352 352 slicerpop.Append(wx_id, '&Box Averaging in Qx') 353 353 wx.EVT_MENU(self, wx_id, self.onBoxavgX) 354 wx_id = self._menu_ids.next()354 wx_id = ids.next() 355 355 slicerpop.Append(wx_id, '&Box Averaging in Qy') 356 356 wx.EVT_MENU(self, wx_id, self.onBoxavgY) 357 357 if self.slicer != None: 358 wx_id = self._menu_ids.next()358 wx_id = ids.next() 359 359 slicerpop.Append(wx_id, '&Clear Slicer') 360 360 wx.EVT_MENU(self, wx_id, self.onClearSlicer) 361 361 if self.slicer.__class__.__name__ != "BoxSum": 362 wx_id = self._menu_ids.next()362 wx_id = ids.next() 363 363 slicerpop.Append(wx_id, '&Edit Slicer Parameters') 364 364 wx.EVT_MENU(self, wx_id, self._onEditSlicer) 365 365 slicerpop.AppendSeparator() 366 366 367 wx_id = self._menu_ids.next()367 wx_id = ids.next() 368 368 slicerpop.Append(wx_id, '&Edit Graph Label', 'Edit Graph Label') 369 369 wx.EVT_MENU(self, wx_id, self.onEditLabels) … … 372 372 # ILL mod here 373 373 374 wx_id = self._menu_ids.next()374 wx_id = ids.next() 375 375 slicerpop.Append(wx_id, '&Modify graph appearance', 'Modify graph appearance') 376 376 wx.EVT_MENU(self, wx_id, self.modifyGraphAppearance) 377 377 slicerpop.AppendSeparator() 378 378 379 wx_id = self._menu_ids.next()379 wx_id = ids.next() 380 380 slicerpop.Append(wx_id, '&2D Color Map') 381 381 wx.EVT_MENU(self, wx_id, self._onEditDetector) 382 382 slicerpop.AppendSeparator() 383 383 384 wx_id = self._menu_ids.next()384 wx_id = ids.next() 385 385 slicerpop.Append(wx_id, '&Toggle Linear/Log Scale') 386 386 wx.EVT_MENU(self, wx_id, self._onToggleScale) 387 387 388 388 slicerpop.AppendSeparator() 389 wx_id = self._menu_ids.next()389 wx_id = ids.next() 390 390 slicerpop.Append(wx_id, '&Window Title') 391 391 wx.EVT_MENU(self, wx_id, self.onChangeCaption) -
src/sas/guiframe/utils.py
r79492222 r6f16e25 123 123 return begin_flag, end_flag 124 124 125 class IdList: 126 """ 127 Create a list of wx ids that can be reused. 128 129 Ids for items need to be unique within their context. In a dynamic 130 application where the number of ids needed different each time the 131 form is created, depending for example, on the number of items that 132 need to be shown in the context menu, you cannot preallocate the 133 ids that you are going to use for the form. Instead, you can use 134 an IdList, which will reuse ids from context to context, adding new 135 ones if the new context requires more than a previous context. 136 137 IdList is set up as an iterator, which returns new ids forever 138 or until it runs out. This makes it pretty useful for defining 139 menus:: 140 141 class Form(wx.Dialog): 142 _form_id_pool = IdList() 143 def __init__(self): 144 ... 145 menu = wx.Menu() 146 for item, wx_id in zip(menu_items, self._form_id_pool): 147 name, description, callback = item 148 menu.Append(wx_id, name, description) 149 wx.EVT_MENU(self, wx_id, callback) 150 ... 151 152 It is a little unusual to use an iterator outside of a loop, but it is 153 supported. For example, when defining a form, your class definition 154 might look something like:: 155 156 class Form(wx.Dialog): 157 _form_id_pool = IdList() 158 def __init__(self, pairs, ...): 159 ids = iter(_form_id_pool) 160 ... 161 wx.StaticText(self, ids.next(), "Some key-value pairs") 162 for name, value in pairs: 163 label = wx.StaticText(self, ids.next(), name) 164 input = wx.TextCtrl(self, ids.next(), value=str(value)) 165 ... 166 ... 167 168 If the dialog is really dynamic, and not defined all in one place, then 169 save the id list iterator as *self._ids = iter(_form_id_pool)* in the 170 constructor. 171 172 The wx documentation is not clear on whether ids need to be unique. 173 Clearly different dialogs can use the same ids, as this is done for the 174 standard button ids such as wx.ID_HELP. Presumably each widget on the 175 form needs its own id, but whether these ids can match the ids of menu 176 items is not indicated, or whether different submenus need their own 177 ids. Using different id lists for menu items and widgets is safest, 178 but probably not necessary. And what about notebook tabs. Do the 179 ids need to be unique across all tabs? 180 """ 181 def __init__(self): 182 self._ids = [] 183 def __iter__(self): 184 return _IdListIterator(self) 185 def __getitem__(self, index): 186 while index >= len(self._ids): 187 self._ids.append(wx.NewId()) 188 return self._ids[index] 189 190 class _IdListIterator: 191 def __init__(self, id_list): 192 self.id_list = id_list 193 self.index = -1 194 def next(self): 195 self.index += 1 196 return self.id_list[self.index] 197
Note: See TracChangeset
for help on using the changeset viewer.