Changeset 6f16e25 in sasview for src/sas/guiframe/utils.py
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.