[e2f1023] | 1 | |
---|
| 2 | import numpy |
---|
| 3 | import string |
---|
[d89f09b] | 4 | import wx |
---|
[6bbeacd4] | 5 | import wx.lib.flatnotebook as fnb |
---|
[a93f525] | 6 | |
---|
[340c2b3] | 7 | from sans.guiframe.panel_base import PanelBase |
---|
[3cd5806] | 8 | from sans.guiframe.events import PanelOnFocusEvent |
---|
[6bbeacd4] | 9 | from sans.guiframe.events import StatusEvent |
---|
[cfc0913] | 10 | import basepage |
---|
[6bbeacd4] | 11 | import models |
---|
[330573d] | 12 | _BOX_WIDTH = 80 |
---|
| 13 | |
---|
| 14 | |
---|
[6bbeacd4] | 15 | class FitPanel(fnb.FlatNotebook, PanelBase): |
---|
[925a30e] | 16 | |
---|
[d89f09b] | 17 | """ |
---|
[5062bbf] | 18 | FitPanel class contains fields allowing to fit models and data |
---|
| 19 | |
---|
| 20 | :note: For Fit to be performed the user should check at least one parameter |
---|
[d89f09b] | 21 | on fit Panel window. |
---|
| 22 | |
---|
| 23 | """ |
---|
| 24 | ## Internal name for the AUI manager |
---|
| 25 | window_name = "Fit panel" |
---|
| 26 | ## Title to appear on top of the window |
---|
| 27 | window_caption = "Fit Panel " |
---|
[2139c3f] | 28 | CENTER_PANE = True |
---|
[7437880] | 29 | |
---|
[6bbeacd4] | 30 | def __init__(self, parent, manager=None, *args, **kwargs): |
---|
[5062bbf] | 31 | """ |
---|
| 32 | """ |
---|
[6bbeacd4] | 33 | fnb.FlatNotebook.__init__(self, parent, -1, |
---|
[3244cbe1] | 34 | style= wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
| 35 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
| 36 | wx.CLIP_CHILDREN) |
---|
[3cd5806] | 37 | PanelBase.__init__(self, parent) |
---|
[6bbeacd4] | 38 | self.SetWindowStyleFlag(style=fnb.FNB_FANCY_TABS) |
---|
| 39 | self._manager = manager |
---|
[340c2b3] | 40 | self.parent = parent |
---|
| 41 | self.event_owner = None |
---|
[d89f09b] | 42 | #dictionary of miodel {model class name, model class} |
---|
[6bbeacd4] | 43 | self.menu_mng = models.ModelManager() |
---|
| 44 | self.model_list_box = self.menu_mng.get_model_list().get_list() |
---|
| 45 | #pageClosedEvent = fnb.EVT_FLATNOTEBOOK_PAGE_CLOSING |
---|
| 46 | self.Bind(fnb.EVT_FLATNOTEBOOK_PAGE_CLOSING , self.on_close_page) |
---|
| 47 | ## save the title of the last page tab added |
---|
[2f189dc] | 48 | self.fit_page_name = {} |
---|
[cfc0913] | 49 | ## list of existing fit page |
---|
[2f189dc] | 50 | self.opened_pages = {} |
---|
| 51 | #page of simultaneous fit |
---|
[ffa69b6] | 52 | self.sim_page = None |
---|
[cfc0913] | 53 | ## get the state of a page |
---|
| 54 | self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate) |
---|
[330573d] | 55 | self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo) |
---|
[fe496eeb] | 56 | self.Bind(basepage.EVT_NEXT_STATE, self._onRedo) |
---|
[90a7bbd] | 57 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.on_page_changing) |
---|
[3cd5806] | 58 | |
---|
[9b18735] | 59 | #add default pages |
---|
| 60 | self.add_default_pages() |
---|
[6bbeacd4] | 61 | |
---|
| 62 | def get_page_by_id(self, id): |
---|
| 63 | """ |
---|
| 64 | """ |
---|
| 65 | if id not in self.opened_pages: |
---|
| 66 | msg = "Fitpanel cannot find ID: %s in self.opened_pages" % str(id) |
---|
| 67 | raise ValueError, msg |
---|
| 68 | else: |
---|
| 69 | return self.opened_pages[id] |
---|
[31b0c47] | 70 | |
---|
[90a7bbd] | 71 | def on_page_changing(self, event): |
---|
[6bbeacd4] | 72 | """ |
---|
| 73 | """ |
---|
[90a7bbd] | 74 | pos = self.GetSelection() |
---|
| 75 | if pos != -1: |
---|
| 76 | selected_page = self.GetPage(pos) |
---|
| 77 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page)) |
---|
[6bbeacd4] | 78 | |
---|
[3cd5806] | 79 | def on_set_focus(self, event): |
---|
| 80 | """ |
---|
| 81 | """ |
---|
| 82 | pos = self.GetSelection() |
---|
[90a7bbd] | 83 | if pos != -1: |
---|
| 84 | selected_page = self.GetPage(pos) |
---|
| 85 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page)) |
---|
[3cd5806] | 86 | |
---|
[c8deee5] | 87 | def get_data(self): |
---|
| 88 | """ |
---|
| 89 | get the data in the current page |
---|
| 90 | """ |
---|
| 91 | pos = self.GetSelection() |
---|
[90a7bbd] | 92 | if pos != -1: |
---|
| 93 | selected_page = self.GetPage(pos) |
---|
| 94 | return selected_page.get_data() |
---|
[c8deee5] | 95 | |
---|
| 96 | def get_state(self): |
---|
| 97 | """ |
---|
| 98 | return the state of the current selected page |
---|
| 99 | """ |
---|
| 100 | pos = self.GetSelection() |
---|
[90a7bbd] | 101 | if pos != -1: |
---|
| 102 | selected_page = self.GetPage(pos) |
---|
| 103 | return selected_page.get_state() |
---|
[c8deee5] | 104 | |
---|
[9b18735] | 105 | def add_default_pages(self): |
---|
| 106 | """ |
---|
| 107 | Add default pages such as a hint page and an empty fit page |
---|
| 108 | """ |
---|
| 109 | #add default page |
---|
| 110 | from hint_fitpage import HintFitPage |
---|
| 111 | self.hint_page = HintFitPage(self) |
---|
[6bbeacd4] | 112 | self.AddPage(self.hint_page,"Hint") |
---|
[3cd5806] | 113 | self.hint_page.set_manager(self._manager) |
---|
[6bbeacd4] | 114 | |
---|
[ac2dc0e] | 115 | def close_all(self): |
---|
| 116 | """ |
---|
[8897d66] | 117 | remove all pages, used when a svs file is opened |
---|
| 118 | """ |
---|
[90a7bbd] | 119 | |
---|
[8897d66] | 120 | #get number of pages |
---|
| 121 | nop = self.GetPageCount() |
---|
| 122 | #use while-loop, for-loop will not do the job well. |
---|
| 123 | while (nop>0): |
---|
| 124 | #delete the first page until no page exists |
---|
| 125 | page = self.GetPage(0) |
---|
[90a7bbd] | 126 | if self._manager.parent.panel_on_focus == page: |
---|
| 127 | self._manager.parent.panel_on_focus = None |
---|
[8897d66] | 128 | self._close_helper(selected_page=page) |
---|
| 129 | self.DeletePage(0) |
---|
| 130 | nop = nop - 1 |
---|
[90a7bbd] | 131 | |
---|
[ac2dc0e] | 132 | ## save the title of the last page tab added |
---|
| 133 | self.fit_page_name = {} |
---|
| 134 | ## list of existing fit page |
---|
| 135 | self.opened_pages = {} |
---|
| 136 | |
---|
[31b0c47] | 137 | def set_state(self, state): |
---|
| 138 | """ |
---|
[5062bbf] | 139 | Restore state of the panel |
---|
[31b0c47] | 140 | """ |
---|
| 141 | page_is_opened = False |
---|
| 142 | if state is not None: |
---|
[6bbeacd4] | 143 | for id, panel in self.opened_pages.values(): |
---|
[31b0c47] | 144 | #Don't return any panel is the exact same page is created |
---|
[6bbeacd4] | 145 | if id == panel.id: |
---|
[31b0c47] | 146 | # the page is still opened |
---|
| 147 | panel.reset_page(state=state) |
---|
[645f9b6] | 148 | panel.save_current_state() |
---|
[31b0c47] | 149 | page_is_opened = True |
---|
| 150 | if not page_is_opened: |
---|
| 151 | panel = self.add_fit_page(data=state.data) |
---|
| 152 | # add data associated to the page created |
---|
| 153 | if panel is not None: |
---|
[6bbeacd4] | 154 | self._manager.store_page(page=panel.id, data=state.data) |
---|
[31b0c47] | 155 | panel.reset_page(state=state) |
---|
[645f9b6] | 156 | panel.save_current_state() |
---|
[31b0c47] | 157 | |
---|
[90a7bbd] | 158 | def clear_panel(self): |
---|
[9b18735] | 159 | """ |
---|
| 160 | Clear and close all panels, used by guimanager |
---|
| 161 | """ |
---|
[90a7bbd] | 162 | |
---|
| 163 | #close all panels only when svs file opened |
---|
| 164 | self.close_all() |
---|
| 165 | self._manager.mypanels = [] |
---|
| 166 | |
---|
[9b18735] | 167 | |
---|
[ac2dc0e] | 168 | def on_close_page(self, event=None): |
---|
[51d47b5] | 169 | """ |
---|
[5062bbf] | 170 | close page and remove all references to the closed page |
---|
[51d47b5] | 171 | """ |
---|
[d361b462] | 172 | nbr_page = self.GetPageCount() |
---|
[a93f525] | 173 | if nbr_page == 1: |
---|
[ac2dc0e] | 174 | |
---|
[d361b462] | 175 | event.Veto() |
---|
| 176 | return |
---|
[51d47b5] | 177 | selected_page = self.GetPage(self.GetSelection()) |
---|
[2f189dc] | 178 | self._close_helper(selected_page=selected_page) |
---|
[9853ad0] | 179 | |
---|
[2f189dc] | 180 | def close_page_with_data(self, deleted_data): |
---|
| 181 | """ |
---|
[5062bbf] | 182 | close a fit page when its data is completely remove from the graph |
---|
[2f189dc] | 183 | """ |
---|
[784e2fa] | 184 | if deleted_data is None: |
---|
| 185 | return |
---|
[2f189dc] | 186 | for index in range(self.GetPageCount()): |
---|
| 187 | selected_page = self.GetPage(index) |
---|
| 188 | if hasattr(selected_page,"get_data"): |
---|
| 189 | data = selected_page.get_data() |
---|
[ac2dc0e] | 190 | |
---|
[784e2fa] | 191 | if data is None: |
---|
| 192 | #the fitpanel exists and only the initial fit page is open |
---|
| 193 | #with no selected data |
---|
| 194 | return |
---|
[2f189dc] | 195 | if data.name == deleted_data.name: |
---|
| 196 | self._close_helper(selected_page) |
---|
| 197 | self.DeletePage(index) |
---|
| 198 | break |
---|
[d89f09b] | 199 | |
---|
| 200 | def set_manager(self, manager): |
---|
| 201 | """ |
---|
[5062bbf] | 202 | set panel manager |
---|
| 203 | |
---|
| 204 | :param manager: instance of plugin fitting |
---|
| 205 | |
---|
[d89f09b] | 206 | """ |
---|
[c8deee5] | 207 | self._manager = manager |
---|
[3cd5806] | 208 | for pos in range(self.GetPageCount()): |
---|
| 209 | page = self.GetPage(pos) |
---|
| 210 | if page is not None: |
---|
| 211 | page.set_manager(self._manager) |
---|
[a93f525] | 212 | |
---|
[ffa69b6] | 213 | def set_model_list(self, dict): |
---|
[c77d859] | 214 | """ |
---|
[5062bbf] | 215 | copy a dictionary of model into its own dictionary |
---|
| 216 | |
---|
| 217 | :param dict: dictionnary made of model name as key and model class |
---|
[c77d859] | 218 | as value |
---|
| 219 | """ |
---|
| 220 | self.model_list_box = dict |
---|
[51d47b5] | 221 | |
---|
[c77d859] | 222 | def get_current_page(self): |
---|
| 223 | """ |
---|
[5062bbf] | 224 | :return: the current page selected |
---|
| 225 | |
---|
[c77d859] | 226 | """ |
---|
[6bbeacd4] | 227 | return self.GetPage(self.GetSelection()) |
---|
[c77d859] | 228 | |
---|
[51d47b5] | 229 | def add_sim_page(self): |
---|
[925a30e] | 230 | """ |
---|
[5062bbf] | 231 | Add the simultaneous fit page |
---|
[925a30e] | 232 | """ |
---|
[51d47b5] | 233 | from simfitpage import SimultaneousFitPage |
---|
[c8deee5] | 234 | page_finder= self._manager.get_page_finder() |
---|
[b28717b] | 235 | self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1) |
---|
[6bbeacd4] | 236 | self.sim_page.id = wx.NewId() |
---|
| 237 | self.AddPage(self.sim_page,"Simultaneous Fit", True) |
---|
[c8deee5] | 238 | self.sim_page.set_manager(self._manager) |
---|
[51d47b5] | 239 | return self.sim_page |
---|
[d89f09b] | 240 | |
---|
[6bbeacd4] | 241 | |
---|
[2f189dc] | 242 | def add_empty_page(self): |
---|
[9853ad0] | 243 | """ |
---|
[5062bbf] | 244 | add an empty page |
---|
[9853ad0] | 245 | """ |
---|
[2f189dc] | 246 | from fitpage import FitPage |
---|
[6bbeacd4] | 247 | panel = FitPage(parent=self) |
---|
| 248 | panel.id = wx.NewId() |
---|
| 249 | panel.populate_box(dict=self.model_list_box) |
---|
[c8deee5] | 250 | panel.set_manager(self._manager) |
---|
[6bbeacd4] | 251 | self.AddPage(panel, panel.window_name, select=True) |
---|
| 252 | self.opened_pages[panel.id] = panel |
---|
[2f189dc] | 253 | return panel |
---|
[6bbeacd4] | 254 | |
---|
[2f189dc] | 255 | |
---|
[6bbeacd4] | 256 | def set_data(self, data): |
---|
[2f189dc] | 257 | """ |
---|
[5062bbf] | 258 | Add a fitting page on the notebook contained by fitpanel |
---|
| 259 | |
---|
| 260 | :param data: data to fit |
---|
| 261 | |
---|
| 262 | :return panel : page just added for further used. is used by fitting module |
---|
| 263 | |
---|
[2f189dc] | 264 | """ |
---|
| 265 | if data is None: |
---|
| 266 | return None |
---|
[6bbeacd4] | 267 | for page in self.opened_pages.values(): |
---|
| 268 | #check if the selected data existing in the fitpanel |
---|
| 269 | pos = self.GetPageIndex(page) |
---|
| 270 | if page.get_data() is None: |
---|
| 271 | page.set_data(data) |
---|
| 272 | self.SetPageText(pos, str(data.name)) |
---|
| 273 | self.SetSelection(pos) |
---|
| 274 | return page |
---|
| 275 | elif page.get_data().id == data.id: |
---|
| 276 | msg = "Data already existing in the fitting panel" |
---|
| 277 | wx.PostEvent(self._manager.parent, |
---|
| 278 | StatusEvent(status=msg, info='warning')) |
---|
| 279 | self.SetSelection(pos) |
---|
| 280 | return page |
---|
[9b18735] | 281 | |
---|
[6bbeacd4] | 282 | page = self.add_empty_page() |
---|
| 283 | page.id = wx.NewId() |
---|
| 284 | page.set_data(data) |
---|
| 285 | self.SetPageText(pos, str(data.name)) |
---|
| 286 | self.opened_pages[page.id] = page |
---|
| 287 | return page |
---|
| 288 | |
---|
| 289 | def _onGetstate(self, event): |
---|
[cfc0913] | 290 | """ |
---|
[5062bbf] | 291 | copy the state of a page |
---|
[cfc0913] | 292 | """ |
---|
[6bbeacd4] | 293 | page = event.page |
---|
| 294 | if page.id in self.fit_page_name: |
---|
| 295 | self.fit_page_name[page.id].appendItem(page.createMemento()) |
---|
[cfc0913] | 296 | |
---|
[330573d] | 297 | def _onUndo(self, event ): |
---|
| 298 | """ |
---|
[5062bbf] | 299 | return the previous state of a given page is available |
---|
[330573d] | 300 | """ |
---|
| 301 | page = event.page |
---|
[6bbeacd4] | 302 | if page.id in self.fit_page_name: |
---|
| 303 | if self.fit_page_name[page.id].getCurrentPosition()==0: |
---|
[330573d] | 304 | state = None |
---|
| 305 | else: |
---|
[6bbeacd4] | 306 | state = self.fit_page_name[page.id].getPreviousItem() |
---|
[fe496eeb] | 307 | page._redo.Enable(True) |
---|
[330573d] | 308 | page.reset_page(state) |
---|
[fe496eeb] | 309 | |
---|
[2f189dc] | 310 | def _onRedo(self, event): |
---|
[fe496eeb] | 311 | """ |
---|
[5062bbf] | 312 | return the next state available |
---|
[fe496eeb] | 313 | """ |
---|
| 314 | page = event.page |
---|
[6bbeacd4] | 315 | if page.id in self.fit_page_name: |
---|
| 316 | length= len(self.fit_page_name[page.id]) |
---|
| 317 | if self.fit_page_name[page.id].getCurrentPosition()== length -1: |
---|
[fe496eeb] | 318 | state = None |
---|
| 319 | page._redo.Enable(False) |
---|
[3b9e023] | 320 | page._redo.Enable(True) |
---|
[fe496eeb] | 321 | else: |
---|
[6bbeacd4] | 322 | state =self.fit_page_name[page.id].getNextItem() |
---|
[fe496eeb] | 323 | page.reset_page(state) |
---|
[2f189dc] | 324 | |
---|
| 325 | def _close_helper(self, selected_page): |
---|
[3f1af74] | 326 | """ |
---|
[5062bbf] | 327 | Delete the given page from the notebook |
---|
[3f1af74] | 328 | """ |
---|
[2f189dc] | 329 | #remove hint page |
---|
[6bbeacd4] | 330 | if selected_page.id == self.hint_page.id: |
---|
[2f189dc] | 331 | return |
---|
| 332 | ## removing sim_page |
---|
| 333 | if selected_page == self.sim_page : |
---|
[c8deee5] | 334 | self._manager.sim_page=None |
---|
[2f189dc] | 335 | return |
---|
[b787e68c] | 336 | |
---|
[2f189dc] | 337 | ## closing other pages |
---|
| 338 | state = selected_page.createMemento() |
---|
| 339 | page_name = selected_page.window_name |
---|
[c8deee5] | 340 | page_finder = self._manager.get_page_finder() |
---|
[2f189dc] | 341 | fitproblem = None |
---|
| 342 | ## removing fit page |
---|
[c6036f5] | 343 | data = selected_page.get_data() |
---|
| 344 | #Don' t remove plot for 2D |
---|
| 345 | flag = True |
---|
| 346 | if data.__class__.__name__ == 'Data2D': |
---|
| 347 | flag = False |
---|
[2f189dc] | 348 | if selected_page in page_finder: |
---|
| 349 | #Delete the name of the page into the list of open page |
---|
[6bbeacd4] | 350 | for id, list in self.opened_pages.iteritems(): |
---|
[2f189dc] | 351 | #Don't return any panel is the exact same page is created |
---|
[6bbeacd4] | 352 | |
---|
| 353 | if flag and selected_page.id == id: |
---|
| 354 | self._manager.remove_plot(id, theory=False) |
---|
[2f189dc] | 355 | break |
---|
| 356 | del page_finder[selected_page] |
---|
| 357 | ##remove the check box link to the model name of this page (selected_page) |
---|
| 358 | try: |
---|
| 359 | self.sim_page.draw_page() |
---|
| 360 | except: |
---|
| 361 | ## that page is already deleted no need to remove check box on |
---|
| 362 | ##non existing page |
---|
| 363 | pass |
---|
| 364 | |
---|
| 365 | #Delete the name of the page into the list of open page |
---|
[6bbeacd4] | 366 | for id, list in self.opened_pages.iteritems(): |
---|
[2f189dc] | 367 | #Don't return any panel is the exact same page is created |
---|
[6bbeacd4] | 368 | |
---|
| 369 | if selected_page.id == id: |
---|
[2f189dc] | 370 | del self.opened_pages[type] |
---|
| 371 | break |
---|
[0aeabc6] | 372 | |
---|
[b787e68c] | 373 | |
---|