[e2f1023] | 1 | |
---|
| 2 | import numpy |
---|
| 3 | import string |
---|
[d89f09b] | 4 | import wx |
---|
[26bf293] | 5 | import wx.aui |
---|
[330573d] | 6 | |
---|
[cfc0913] | 7 | import basepage |
---|
[00c3aac] | 8 | |
---|
[330573d] | 9 | _BOX_WIDTH = 80 |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | class StateIterator(object): |
---|
[1b69256] | 13 | """ |
---|
| 14 | Contains all saved state of a given page. |
---|
| 15 | Provide position of the current state of a page, the first save state |
---|
| 16 | and the last state for a given page. |
---|
[330573d] | 17 | Allow easy undo or redo for a given page |
---|
[1b69256] | 18 | """ |
---|
[330573d] | 19 | def __init__(self): |
---|
| 20 | self._current=0 |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | def __iter__(self): |
---|
| 24 | return self |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | def first(self): |
---|
| 28 | self._current =0 |
---|
| 29 | return self._current |
---|
| 30 | |
---|
| 31 | def next(self, max ): |
---|
| 32 | if self._current < max: |
---|
| 33 | self._current += 1 |
---|
| 34 | return self._current |
---|
| 35 | |
---|
| 36 | def previous(self): |
---|
| 37 | if self._current > 0: |
---|
| 38 | self._current = self._current -1 |
---|
| 39 | return self._current |
---|
| 40 | |
---|
| 41 | def currentPosition(self): |
---|
| 42 | return self._current |
---|
| 43 | |
---|
| 44 | def setPosition(self, value): |
---|
| 45 | if value >=0: |
---|
| 46 | self._current = int(value) |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | class ListOfState(list): |
---|
| 50 | def __init__(self, *args, **kw): |
---|
| 51 | list.__init__(self, *args, **kw) |
---|
| 52 | self.iterator = StateIterator() |
---|
| 53 | |
---|
| 54 | def appendItem(self, x): |
---|
| 55 | self.append(x) |
---|
| 56 | self.iterator.setPosition(value= len(self)-1) |
---|
| 57 | |
---|
| 58 | def removeItem(self, x): |
---|
| 59 | self.iterator.previous() |
---|
| 60 | self.remove(x) |
---|
| 61 | |
---|
| 62 | def getPreviousItem(self): |
---|
| 63 | position = self.iterator.previous() |
---|
| 64 | |
---|
| 65 | if position < 0: |
---|
| 66 | return None |
---|
| 67 | else: |
---|
| 68 | return self[position] |
---|
| 69 | |
---|
| 70 | def getNextItem(self): |
---|
[fe496eeb] | 71 | position = self.iterator.next(max= len(self)-1) |
---|
[330573d] | 72 | if position >= len(self): |
---|
| 73 | return None |
---|
| 74 | else: |
---|
| 75 | return self[position] |
---|
| 76 | |
---|
| 77 | def getCurrentItem(self): |
---|
| 78 | postion = self.iterator.currentPosition() |
---|
| 79 | if postion >= 0 and position < len(self): |
---|
| 80 | return self[postion] |
---|
| 81 | else: |
---|
| 82 | return None |
---|
| 83 | |
---|
| 84 | def getCurrentPosition(self): |
---|
| 85 | return self.iterator.currentPosition() |
---|
[2f189dc] | 86 | |
---|
[330573d] | 87 | |
---|
[cfc0913] | 88 | class PageInfo(object): |
---|
| 89 | """ |
---|
| 90 | this class contains the minimum numbers of data members |
---|
| 91 | a fitpage or model page need to be initialized. |
---|
| 92 | """ |
---|
| 93 | data = None |
---|
[ffa69b6] | 94 | model = None |
---|
| 95 | manager = None |
---|
[cfc0913] | 96 | event_owner= None |
---|
| 97 | model_list_box = None |
---|
[ffa69b6] | 98 | name = None |
---|
[330573d] | 99 | ## Internal name for the AUI manager |
---|
[cfc0913] | 100 | window_name = "Page" |
---|
| 101 | ## Title to appear on top of the window |
---|
| 102 | window_caption = "Page" |
---|
[2f189dc] | 103 | #type of page can be real data , theory 1D or therory2D |
---|
| 104 | type = "Data" |
---|
| 105 | def __init__(self, model=None, data=None, manager=None, |
---|
| 106 | event_owner=None, model_list_box=None, name=None): |
---|
[cfc0913] | 107 | """ |
---|
| 108 | Initialize data members |
---|
| 109 | """ |
---|
| 110 | self.data = data |
---|
| 111 | self.model= model |
---|
| 112 | self.manager= manager |
---|
| 113 | self.event_owner= event_owner |
---|
| 114 | self.model_list_box = model_list_box |
---|
| 115 | self.name=None |
---|
| 116 | self.window_name = "Page" |
---|
| 117 | self.window_caption = "Page" |
---|
[2f189dc] | 118 | self.type = "Data" |
---|
| 119 | |
---|
[90d04eb] | 120 | class FitPanel(wx.aui.AuiNotebook): |
---|
[925a30e] | 121 | |
---|
[d89f09b] | 122 | """ |
---|
| 123 | FitPanel class contains fields allowing to fit models and data |
---|
| 124 | @note: For Fit to be performed the user should check at least one parameter |
---|
| 125 | on fit Panel window. |
---|
| 126 | |
---|
| 127 | """ |
---|
| 128 | ## Internal name for the AUI manager |
---|
| 129 | window_name = "Fit panel" |
---|
| 130 | ## Title to appear on top of the window |
---|
| 131 | window_caption = "Fit Panel " |
---|
[2139c3f] | 132 | CENTER_PANE = True |
---|
[7437880] | 133 | |
---|
[d89f09b] | 134 | def __init__(self, parent, *args, **kwargs): |
---|
[90d04eb] | 135 | wx.aui.AuiNotebook.__init__(self, parent, -1, |
---|
[3244cbe1] | 136 | style= wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
| 137 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
| 138 | wx.CLIP_CHILDREN) |
---|
[848a2ef] | 139 | |
---|
[d89f09b] | 140 | self.manager=None |
---|
| 141 | self.parent=parent |
---|
| 142 | self.event_owner=None |
---|
[26bf293] | 143 | |
---|
[3244cbe1] | 144 | pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE |
---|
[2f189dc] | 145 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.on_close_page) |
---|
[51d47b5] | 146 | |
---|
[d89f09b] | 147 | #dictionary of miodel {model class name, model class} |
---|
[2f189dc] | 148 | self.model_list_box = {} |
---|
[cfc0913] | 149 | ## save the title of the last page tab added |
---|
[2f189dc] | 150 | self.fit_page_name = {} |
---|
[cfc0913] | 151 | ## list of existing fit page |
---|
[2f189dc] | 152 | self.opened_pages = {} |
---|
| 153 | #page of simultaneous fit |
---|
[ffa69b6] | 154 | self.sim_page = None |
---|
[cfc0913] | 155 | ## get the state of a page |
---|
| 156 | self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate) |
---|
[330573d] | 157 | self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo) |
---|
[fe496eeb] | 158 | self.Bind(basepage.EVT_NEXT_STATE, self._onRedo) |
---|
[9237df4] | 159 | |
---|
| 160 | #add default page |
---|
| 161 | from hint_fitpage import HintFitPage |
---|
[2377cd4] | 162 | self.hint_page = HintFitPage(self) |
---|
| 163 | self.AddPage(page=self.hint_page, caption="Hint") |
---|
[ffa69b6] | 164 | #Add the first fit page |
---|
[2f189dc] | 165 | self.add_empty_page() |
---|
[ffa69b6] | 166 | |
---|
[6bcdad1] | 167 | # increment number for model name |
---|
| 168 | self.count=0 |
---|
[f39511b] | 169 | #updating the panel |
---|
[26bf293] | 170 | self.Update() |
---|
[d89f09b] | 171 | self.Center() |
---|
[2f189dc] | 172 | |
---|
| 173 | def on_close_page(self, event): |
---|
[51d47b5] | 174 | """ |
---|
[925a30e] | 175 | close page and remove all references to the closed page |
---|
[51d47b5] | 176 | """ |
---|
[d361b462] | 177 | nbr_page = self.GetPageCount() |
---|
| 178 | if nbr_page == 1: |
---|
| 179 | event.Veto() |
---|
| 180 | return |
---|
[51d47b5] | 181 | selected_page = self.GetPage(self.GetSelection()) |
---|
[2f189dc] | 182 | self._close_helper(selected_page=selected_page) |
---|
[9853ad0] | 183 | |
---|
[2f189dc] | 184 | def close_page_with_data(self, deleted_data): |
---|
| 185 | """ |
---|
| 186 | close a fit page when its data is completely remove from the graph |
---|
| 187 | """ |
---|
| 188 | for index in range(self.GetPageCount()): |
---|
| 189 | selected_page = self.GetPage(index) |
---|
| 190 | if hasattr(selected_page,"get_data"): |
---|
| 191 | data = selected_page.get_data() |
---|
| 192 | if data.name == deleted_data.name: |
---|
| 193 | self._close_helper(selected_page) |
---|
| 194 | self.DeletePage(index) |
---|
| 195 | break |
---|
[d89f09b] | 196 | |
---|
| 197 | def set_manager(self, manager): |
---|
| 198 | """ |
---|
| 199 | set panel manager |
---|
| 200 | @param manager: instance of plugin fitting |
---|
| 201 | """ |
---|
| 202 | self.manager = manager |
---|
[925a30e] | 203 | |
---|
[d89f09b] | 204 | |
---|
| 205 | def set_owner(self,owner): |
---|
| 206 | """ |
---|
| 207 | set and owner for fitpanel |
---|
| 208 | @param owner: the class responsible of plotting |
---|
| 209 | """ |
---|
[c77d859] | 210 | self.event_owner = owner |
---|
| 211 | |
---|
[ffa69b6] | 212 | def set_model_list(self, dict): |
---|
[c77d859] | 213 | """ |
---|
| 214 | copy a dictionary of model into its own dictionary |
---|
| 215 | @param dict: dictionnary made of model name as key and model class |
---|
| 216 | as value |
---|
| 217 | """ |
---|
| 218 | self.model_list_box = dict |
---|
[51d47b5] | 219 | |
---|
[c77d859] | 220 | |
---|
| 221 | def get_current_page(self): |
---|
| 222 | """ |
---|
| 223 | @return the current page selected |
---|
| 224 | """ |
---|
| 225 | return self.GetPage(self.GetSelection() ) |
---|
| 226 | |
---|
[51d47b5] | 227 | def add_sim_page(self): |
---|
[925a30e] | 228 | """ |
---|
| 229 | Add the simultaneous fit page |
---|
| 230 | """ |
---|
[51d47b5] | 231 | from simfitpage import SimultaneousFitPage |
---|
[b28717b] | 232 | page_finder= self.manager.get_page_finder() |
---|
| 233 | self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1) |
---|
| 234 | |
---|
[51d47b5] | 235 | self.AddPage(self.sim_page,caption="Simultaneous Fit",select=True) |
---|
| 236 | self.sim_page.set_manager(self.manager) |
---|
| 237 | return self.sim_page |
---|
[d89f09b] | 238 | |
---|
[2f189dc] | 239 | def get_page_info(self, data=None): |
---|
| 240 | """ |
---|
| 241 | fill information required to add a page in the fit panel |
---|
| 242 | """ |
---|
| 243 | name = "Fit Page" |
---|
| 244 | type = 'empty' |
---|
[ffa69b6] | 245 | if data is not None: |
---|
| 246 | if data.is_data: |
---|
| 247 | name = data.name |
---|
[2f189dc] | 248 | type = 'Data' |
---|
[882a912] | 249 | else: |
---|
[2f189dc] | 250 | if data.__class__.__name__ == "Data2D": |
---|
[ffa69b6] | 251 | name = 'Model 2D Fit' |
---|
[2f189dc] | 252 | type = 'Theory2D' |
---|
[ffa69b6] | 253 | else: |
---|
| 254 | name = 'Model 1D Fit' |
---|
[2f189dc] | 255 | type = 'Theory1D' |
---|
| 256 | page_info = PageInfo(data=data, name=name) |
---|
| 257 | page_info.event_owner = self.event_owner |
---|
| 258 | page_info.manager = self.manager |
---|
| 259 | page_info.window_name = name |
---|
| 260 | page_info.window_caption = name |
---|
| 261 | page_info.type = type |
---|
| 262 | return page_info |
---|
[9853ad0] | 263 | |
---|
[2f189dc] | 264 | def add_empty_page(self): |
---|
[9853ad0] | 265 | """ |
---|
[2f189dc] | 266 | add an empty page |
---|
[9853ad0] | 267 | """ |
---|
[2f189dc] | 268 | page_info = self.get_page_info() |
---|
| 269 | from fitpage import FitPage |
---|
| 270 | panel = FitPage(parent=self, page_info=page_info) |
---|
| 271 | self.AddPage(page=panel, caption=page_info.window_name, select=True) |
---|
| 272 | self.opened_pages[page_info.type] = [page_info.window_name, panel] |
---|
| 273 | return panel |
---|
| 274 | |
---|
| 275 | def add_page(self, page_info): |
---|
[848a2ef] | 276 | """ |
---|
[2f189dc] | 277 | add a new page |
---|
[848a2ef] | 278 | """ |
---|
[2f189dc] | 279 | from fitpage import FitPage |
---|
| 280 | panel = FitPage(parent=self, page_info=page_info) |
---|
| 281 | self.AddPage(page=panel, caption=page_info.window_name, select=True) |
---|
| 282 | index = self.GetPageIndex(panel) |
---|
| 283 | self.change_page_content(data=page_info.data, index=index) |
---|
| 284 | return panel |
---|
[848a2ef] | 285 | |
---|
[2f189dc] | 286 | def change_page_content(self, data, index): |
---|
| 287 | """ |
---|
| 288 | replace the contains of an existing page |
---|
| 289 | """ |
---|
| 290 | page_info = self.get_page_info(data=data) |
---|
| 291 | self.SetPageText(index, page_info.window_name) |
---|
| 292 | panel = self.GetPage(index) |
---|
| 293 | panel.set_data(data) |
---|
| 294 | if panel.model_list_box is None or len(panel.model_list_box) == 0: |
---|
| 295 | page_info.model_list_box = self.model_list_box.get_list() |
---|
| 296 | panel.populate_box(dict=page_info.model_list_box) |
---|
| 297 | panel.initialize_combox() |
---|
| 298 | panel.set_page_info(page_info=page_info) |
---|
| 299 | self.opened_pages[page_info.type] = [page_info.window_name, panel] |
---|
| 300 | return panel |
---|
| 301 | |
---|
| 302 | def replace_page(self, index, page_info, type): |
---|
| 303 | """ |
---|
| 304 | replace an existing page |
---|
| 305 | """ |
---|
| 306 | self.DeletePage(index) |
---|
| 307 | del self.opened_pages[type] |
---|
| 308 | return self.add_page(page_info=page_info) |
---|
[848a2ef] | 309 | |
---|
[2f189dc] | 310 | def add_fit_page(self, data, reset=False): |
---|
| 311 | """ |
---|
| 312 | Add a fitting page on the notebook contained by fitpanel |
---|
| 313 | @param data: data to fit |
---|
| 314 | @return panel : page just added for further used. is used by fitting module |
---|
| 315 | """ |
---|
| 316 | if data is None: |
---|
| 317 | return None |
---|
| 318 | page_info = self.get_page_info(data=data) |
---|
| 319 | type = page_info.type |
---|
| 320 | npages = len(self.opened_pages.keys()) |
---|
| 321 | #check if only and empty page is opened |
---|
| 322 | if len(self.opened_pages.keys()) > 0: |
---|
| 323 | first_page_type = self.opened_pages.keys()[0] |
---|
| 324 | if npages == 1 and first_page_type in ['empty']: |
---|
| 325 | #replace the first empty page |
---|
| 326 | name, panel = self.opened_pages[first_page_type] |
---|
| 327 | index = self.GetPageIndex(panel) |
---|
| 328 | panel = self.change_page_content(data=data, index=index) |
---|
| 329 | del self.opened_pages[first_page_type] |
---|
| 330 | return panel |
---|
| 331 | if type in self.opened_pages.keys(): |
---|
| 332 | #this type of page is already created but it is a theory |
---|
| 333 | # meaning the same page is just to fit different data |
---|
| 334 | if not type.lower() in ['data']: |
---|
| 335 | #delete the previous theory page and add a new one |
---|
| 336 | name, panel = self.opened_pages[type] |
---|
[286bb6c] | 337 | #self.manager.reset_plot_panel(panel.get_data()) |
---|
[2f189dc] | 338 | #delete the existing page and replace it |
---|
| 339 | index = self.GetPageIndex(panel) |
---|
| 340 | panel = self.replace_page(index=index, page_info=page_info, type=type) |
---|
| 341 | return panel |
---|
| 342 | else: |
---|
| 343 | for name, panel in self.opened_pages.values(): |
---|
| 344 | #Don't return any panel is the exact same page is created |
---|
| 345 | if name == page_info.window_name: |
---|
| 346 | return None |
---|
| 347 | else: |
---|
| 348 | panel = self.add_page(page_info=page_info) |
---|
| 349 | return panel |
---|
| 350 | else: |
---|
| 351 | #a new type of page is created |
---|
| 352 | panel = self.add_page(page_info=page_info) |
---|
| 353 | return panel |
---|
| 354 | |
---|
[cfc0913] | 355 | def _onGetstate(self, event): |
---|
| 356 | """ |
---|
| 357 | copy the state of a page |
---|
| 358 | """ |
---|
| 359 | page= event.page |
---|
[b787e68c] | 360 | if page.window_name in self.fit_page_name: |
---|
[330573d] | 361 | self.fit_page_name[page.window_name].appendItem(page.createMemento()) |
---|
[cfc0913] | 362 | |
---|
[330573d] | 363 | def _onUndo(self, event ): |
---|
| 364 | """ |
---|
| 365 | return the previous state of a given page is available |
---|
| 366 | """ |
---|
| 367 | page = event.page |
---|
| 368 | if page.window_name in self.fit_page_name: |
---|
| 369 | if self.fit_page_name[page.window_name].getCurrentPosition()==0: |
---|
| 370 | state = None |
---|
| 371 | else: |
---|
| 372 | state = self.fit_page_name[page.window_name].getPreviousItem() |
---|
[fe496eeb] | 373 | page._redo.Enable(True) |
---|
[330573d] | 374 | page.reset_page(state) |
---|
[fe496eeb] | 375 | |
---|
[2f189dc] | 376 | def _onRedo(self, event): |
---|
[fe496eeb] | 377 | """ |
---|
| 378 | return the next state available |
---|
| 379 | """ |
---|
| 380 | page = event.page |
---|
| 381 | if page.window_name in self.fit_page_name: |
---|
| 382 | length= len(self.fit_page_name[page.window_name]) |
---|
| 383 | if self.fit_page_name[page.window_name].getCurrentPosition()== length -1: |
---|
| 384 | state = None |
---|
| 385 | page._redo.Enable(False) |
---|
[3b9e023] | 386 | page._redo.Enable(True) |
---|
[fe496eeb] | 387 | else: |
---|
| 388 | state = self.fit_page_name[page.window_name].getNextItem() |
---|
| 389 | page.reset_page(state) |
---|
[2f189dc] | 390 | |
---|
| 391 | def _close_helper(self, selected_page): |
---|
[3f1af74] | 392 | """ |
---|
[2f189dc] | 393 | Delete the given page from the notebook |
---|
[3f1af74] | 394 | """ |
---|
[2f189dc] | 395 | #remove hint page |
---|
| 396 | if selected_page == self.hint_page: |
---|
| 397 | return |
---|
| 398 | ## removing sim_page |
---|
| 399 | if selected_page == self.sim_page : |
---|
| 400 | self.manager.sim_page=None |
---|
| 401 | return |
---|
[b787e68c] | 402 | |
---|
[2f189dc] | 403 | ## closing other pages |
---|
| 404 | state = selected_page.createMemento() |
---|
| 405 | page_name = selected_page.window_name |
---|
| 406 | page_finder = self.manager.get_page_finder() |
---|
| 407 | fitproblem = None |
---|
| 408 | ## removing fit page |
---|
[c6036f5] | 409 | data = selected_page.get_data() |
---|
| 410 | #Don' t remove plot for 2D |
---|
| 411 | flag = True |
---|
| 412 | if data.__class__.__name__ == 'Data2D': |
---|
| 413 | flag = False |
---|
[2f189dc] | 414 | if selected_page in page_finder: |
---|
| 415 | #Delete the name of the page into the list of open page |
---|
| 416 | for type, list in self.opened_pages.iteritems(): |
---|
| 417 | #Don't return any panel is the exact same page is created |
---|
| 418 | name = str(list[0]) |
---|
[c6036f5] | 419 | if flag and selected_page.window_name == name: |
---|
[2f189dc] | 420 | if type.lower() in ['theory1d', 'theory2d']: |
---|
| 421 | self.manager.remove_plot(selected_page, theory=True) |
---|
| 422 | else: |
---|
| 423 | self.manager.remove_plot(selected_page, theory=False) |
---|
| 424 | break |
---|
| 425 | del page_finder[selected_page] |
---|
| 426 | ##remove the check box link to the model name of this page (selected_page) |
---|
| 427 | try: |
---|
| 428 | self.sim_page.draw_page() |
---|
| 429 | except: |
---|
| 430 | ## that page is already deleted no need to remove check box on |
---|
| 431 | ##non existing page |
---|
| 432 | pass |
---|
| 433 | |
---|
| 434 | #Delete the name of the page into the list of open page |
---|
| 435 | for type, list in self.opened_pages.iteritems(): |
---|
| 436 | #Don't return any panel is the exact same page is created |
---|
| 437 | name = str(list[0]) |
---|
| 438 | if selected_page.window_name == name: |
---|
| 439 | del self.opened_pages[type] |
---|
| 440 | break |
---|
[0aeabc6] | 441 | |
---|
[b787e68c] | 442 | |
---|