[f32d144] | 1 | """ |
---|
| 2 | FitPanel class contains fields allowing to fit models and data |
---|
[e2f1023] | 3 | |
---|
[f32d144] | 4 | :note: For Fit to be performed the user should check at least one parameter |
---|
| 5 | on fit Panel window. |
---|
| 6 | |
---|
| 7 | """ |
---|
[d89f09b] | 8 | import wx |
---|
[1b1bbf9] | 9 | from wx.aui import AuiNotebook as nb |
---|
[a93f525] | 10 | |
---|
[340c2b3] | 11 | from sans.guiframe.panel_base import PanelBase |
---|
[3cd5806] | 12 | from sans.guiframe.events import PanelOnFocusEvent |
---|
[6bbeacd4] | 13 | from sans.guiframe.events import StatusEvent |
---|
[aefc09f] | 14 | from sans.guiframe.dataFitting import check_data_validity |
---|
[66ff250] | 15 | |
---|
[cfc0913] | 16 | import basepage |
---|
[6bbeacd4] | 17 | import models |
---|
[330573d] | 18 | _BOX_WIDTH = 80 |
---|
| 19 | |
---|
| 20 | |
---|
[f32d144] | 21 | class FitPanel(nb, PanelBase): |
---|
[d89f09b] | 22 | """ |
---|
[5062bbf] | 23 | FitPanel class contains fields allowing to fit models and data |
---|
| 24 | |
---|
| 25 | :note: For Fit to be performed the user should check at least one parameter |
---|
[d89f09b] | 26 | on fit Panel window. |
---|
| 27 | |
---|
| 28 | """ |
---|
| 29 | ## Internal name for the AUI manager |
---|
| 30 | window_name = "Fit panel" |
---|
| 31 | ## Title to appear on top of the window |
---|
| 32 | window_caption = "Fit Panel " |
---|
[2139c3f] | 33 | CENTER_PANE = True |
---|
[7437880] | 34 | |
---|
[6bbeacd4] | 35 | def __init__(self, parent, manager=None, *args, **kwargs): |
---|
[5062bbf] | 36 | """ |
---|
| 37 | """ |
---|
[1b1bbf9] | 38 | nb.__init__(self, parent, -1, |
---|
[f32d144] | 39 | style=wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
[3244cbe1] | 40 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
| 41 | wx.CLIP_CHILDREN) |
---|
[3cd5806] | 42 | PanelBase.__init__(self, parent) |
---|
[1b1bbf9] | 43 | #self.SetWindowStyleFlag(style=nb.FNB_FANCY_TABS) |
---|
[6bbeacd4] | 44 | self._manager = manager |
---|
[340c2b3] | 45 | self.parent = parent |
---|
| 46 | self.event_owner = None |
---|
[d89f09b] | 47 | #dictionary of miodel {model class name, model class} |
---|
[6bbeacd4] | 48 | self.menu_mng = models.ModelManager() |
---|
[b2d9826] | 49 | self.model_list_box = self.menu_mng.get_model_list() |
---|
[f32d144] | 50 | #pageClosedEvent = nb.EVT_FLATNOTEBOOK_PAGE_CLOSING |
---|
[ea5fa58] | 51 | self.model_dictionary = self.menu_mng.get_model_dictionary() |
---|
[f22e626] | 52 | self.pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE |
---|
[1b1bbf9] | 53 | |
---|
[f22e626] | 54 | self.Bind(self.pageClosedEvent, self.on_close_page) |
---|
[f32d144] | 55 | ## save the title of the last page tab added |
---|
[2f189dc] | 56 | self.fit_page_name = {} |
---|
[cfc0913] | 57 | ## list of existing fit page |
---|
[2f189dc] | 58 | self.opened_pages = {} |
---|
[f32d144] | 59 | #index of fit page |
---|
[cc31608] | 60 | self.fit_page_index = 0 |
---|
| 61 | #index of batch page |
---|
| 62 | self.batch_page_index = 0 |
---|
[f32d144] | 63 | #page of simultaneous fit |
---|
[ffa69b6] | 64 | self.sim_page = None |
---|
[fa02d95] | 65 | self.batch_page = None |
---|
[66ff250] | 66 | self.fit_engine_type = "scipy" |
---|
[cfc0913] | 67 | ## get the state of a page |
---|
| 68 | self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate) |
---|
[330573d] | 69 | self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo) |
---|
[fe496eeb] | 70 | self.Bind(basepage.EVT_NEXT_STATE, self._onRedo) |
---|
[90a7bbd] | 71 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.on_page_changing) |
---|
[f22e626] | 72 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSED, self.on_closed) |
---|
[1b1bbf9] | 73 | |
---|
[f22e626] | 74 | def on_closed(self, event): |
---|
| 75 | """ |
---|
| 76 | """ |
---|
[394ffa7] | 77 | if self.GetPageCount() == 0: |
---|
| 78 | self.add_empty_page() |
---|
| 79 | self.enable_close_button() |
---|
[f22e626] | 80 | |
---|
[4e78f56] | 81 | def save_project(self, doc=None): |
---|
| 82 | """ |
---|
| 83 | return an xml node containing state of the panel |
---|
| 84 | that guiframe can write to file |
---|
| 85 | """ |
---|
[72bd8e7] | 86 | msg = "" |
---|
[4e78f56] | 87 | for uid, page in self.opened_pages.iteritems(): |
---|
[72bd8e7] | 88 | if page.batch_on: |
---|
| 89 | pos = self.GetPageIndex(page) |
---|
[fa02d95] | 90 | if pos != -1 and page not in [self.sim_page, self.batch_page]: |
---|
[72bd8e7] | 91 | msg += "%s .\n" % str(self.GetPageText(pos)) |
---|
| 92 | else: |
---|
| 93 | data = page.get_data() |
---|
| 94 | # state must be cloned |
---|
| 95 | state = page.get_state().clone() |
---|
| 96 | if data is not None: |
---|
[f32d144] | 97 | new_doc = self._manager.state_reader.write_toXML(data, |
---|
| 98 | state) |
---|
[72bd8e7] | 99 | if doc != None and hasattr(doc, "firstChild"): |
---|
| 100 | child = new_doc.firstChild.firstChild |
---|
[f32d144] | 101 | doc.firstChild.appendChild(child) |
---|
[72bd8e7] | 102 | else: |
---|
[f32d144] | 103 | doc = new_doc |
---|
[72bd8e7] | 104 | if msg.strip() != "": |
---|
| 105 | temp = "Save Project is not supported for Batch page.\n" |
---|
| 106 | temp += "The following pages will not be save:\n" |
---|
| 107 | message = temp + msg |
---|
| 108 | wx.PostEvent(self._manager.parent, StatusEvent(status=message, |
---|
| 109 | info="warning")) |
---|
[f32d144] | 110 | return doc |
---|
[6bbeacd4] | 111 | |
---|
[66ff250] | 112 | def _on_engine_change(self, name='scipy'): |
---|
| 113 | """ |
---|
| 114 | """ |
---|
| 115 | for panel in self.opened_pages.values(): |
---|
| 116 | self.set_engine_helper(panel=panel, name=name) |
---|
| 117 | |
---|
| 118 | def set_engine_helper(self, panel, name='scipy'): |
---|
| 119 | """ |
---|
| 120 | """ |
---|
| 121 | self.fit_engine_type = name |
---|
[fa02d95] | 122 | if panel not in[self.batch_page, self.sim_page]: |
---|
[66ff250] | 123 | panel._on_engine_change(name=self.fit_engine_type) |
---|
| 124 | |
---|
[b2d9826] | 125 | def update_model_list(self): |
---|
| 126 | """ |
---|
| 127 | """ |
---|
[9466f2d6] | 128 | temp = self.menu_mng.update() |
---|
| 129 | if len(temp): |
---|
| 130 | self.model_list_box = temp |
---|
| 131 | return temp |
---|
[916f5c0] | 132 | |
---|
| 133 | def reset_pmodel_list(self): |
---|
| 134 | """ |
---|
| 135 | """ |
---|
| 136 | temp = self.menu_mng.pulgins_reset() |
---|
| 137 | if len(temp): |
---|
| 138 | self.model_list_box = temp |
---|
| 139 | return temp |
---|
| 140 | |
---|
[f32d144] | 141 | def get_page_by_id(self, uid): |
---|
[6bbeacd4] | 142 | """ |
---|
| 143 | """ |
---|
[66ff250] | 144 | if uid not in self.opened_pages: |
---|
| 145 | msg = "Fitpanel cannot find ID: %s in self.opened_pages" % str(uid) |
---|
[6bbeacd4] | 146 | raise ValueError, msg |
---|
| 147 | else: |
---|
[66ff250] | 148 | return self.opened_pages[uid] |
---|
[31b0c47] | 149 | |
---|
[90a7bbd] | 150 | def on_page_changing(self, event): |
---|
[6bbeacd4] | 151 | """ |
---|
[f22e626] | 152 | calls the function when the current event handler has exited. avoiding |
---|
| 153 | to call panel on focus on a panel that is currently deleted |
---|
| 154 | """ |
---|
| 155 | wx.CallAfter(self.helper_on_page_change) |
---|
| 156 | |
---|
| 157 | def helper_on_page_change(self): |
---|
| 158 | """ |
---|
[6bbeacd4] | 159 | """ |
---|
[90a7bbd] | 160 | pos = self.GetSelection() |
---|
| 161 | if pos != -1: |
---|
| 162 | selected_page = self.GetPage(pos) |
---|
| 163 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page)) |
---|
[e4c9030] | 164 | self.enable_close_button() |
---|
[f22e626] | 165 | |
---|
[3cd5806] | 166 | def on_set_focus(self, event): |
---|
| 167 | """ |
---|
| 168 | """ |
---|
| 169 | pos = self.GetSelection() |
---|
[90a7bbd] | 170 | if pos != -1: |
---|
| 171 | selected_page = self.GetPage(pos) |
---|
| 172 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page)) |
---|
[3cd5806] | 173 | |
---|
[c8deee5] | 174 | def get_data(self): |
---|
| 175 | """ |
---|
| 176 | get the data in the current page |
---|
| 177 | """ |
---|
| 178 | pos = self.GetSelection() |
---|
[90a7bbd] | 179 | if pos != -1: |
---|
| 180 | selected_page = self.GetPage(pos) |
---|
| 181 | return selected_page.get_data() |
---|
[c8deee5] | 182 | |
---|
[6d8bad4] | 183 | def set_model_state(self, state): |
---|
[e88ebfd] | 184 | """ |
---|
| 185 | receive a state to reset the model in the current page |
---|
| 186 | """ |
---|
| 187 | pos = self.GetSelection() |
---|
| 188 | if pos != -1: |
---|
| 189 | selected_page = self.GetPage(pos) |
---|
| 190 | selected_page.set_model_state(state) |
---|
| 191 | |
---|
[c8deee5] | 192 | def get_state(self): |
---|
| 193 | """ |
---|
| 194 | return the state of the current selected page |
---|
| 195 | """ |
---|
| 196 | pos = self.GetSelection() |
---|
[90a7bbd] | 197 | if pos != -1: |
---|
| 198 | selected_page = self.GetPage(pos) |
---|
| 199 | return selected_page.get_state() |
---|
[c8deee5] | 200 | |
---|
[ac2dc0e] | 201 | def close_all(self): |
---|
| 202 | """ |
---|
[8897d66] | 203 | remove all pages, used when a svs file is opened |
---|
| 204 | """ |
---|
[90a7bbd] | 205 | |
---|
[8897d66] | 206 | #get number of pages |
---|
| 207 | nop = self.GetPageCount() |
---|
| 208 | #use while-loop, for-loop will not do the job well. |
---|
[f32d144] | 209 | while (nop > 0): |
---|
[8897d66] | 210 | #delete the first page until no page exists |
---|
| 211 | page = self.GetPage(0) |
---|
[90a7bbd] | 212 | if self._manager.parent.panel_on_focus == page: |
---|
| 213 | self._manager.parent.panel_on_focus = None |
---|
[8897d66] | 214 | self._close_helper(selected_page=page) |
---|
| 215 | self.DeletePage(0) |
---|
| 216 | nop = nop - 1 |
---|
[90a7bbd] | 217 | |
---|
[ac2dc0e] | 218 | ## save the title of the last page tab added |
---|
| 219 | self.fit_page_name = {} |
---|
| 220 | ## list of existing fit page |
---|
[f32d144] | 221 | self.opened_pages = {} |
---|
[ac2dc0e] | 222 | |
---|
[31b0c47] | 223 | def set_state(self, state): |
---|
| 224 | """ |
---|
[5062bbf] | 225 | Restore state of the panel |
---|
[31b0c47] | 226 | """ |
---|
| 227 | page_is_opened = False |
---|
| 228 | if state is not None: |
---|
[66ff250] | 229 | for uid, panel in self.opened_pages.iteritems(): |
---|
[31b0c47] | 230 | #Don't return any panel is the exact same page is created |
---|
[4bee68d] | 231 | if uid == panel.uid and panel.data == state.data: |
---|
[31b0c47] | 232 | # the page is still opened |
---|
| 233 | panel.reset_page(state=state) |
---|
[f32d144] | 234 | panel.save_current_state() |
---|
[31b0c47] | 235 | page_is_opened = True |
---|
| 236 | if not page_is_opened: |
---|
[ea8283d] | 237 | if state.data.__class__.__name__ != 'list': |
---|
| 238 | #To support older state file format |
---|
| 239 | list_data = [state.data] |
---|
| 240 | else: |
---|
| 241 | #Todo: need new file format for the list |
---|
| 242 | list_data = state.data |
---|
| 243 | panel = self._manager.add_fit_page(data=list_data) |
---|
[31b0c47] | 244 | # add data associated to the page created |
---|
[f32d144] | 245 | if panel is not None: |
---|
| 246 | self._manager.store_data(uid=panel.uid, |
---|
[ea8283d] | 247 | data_list=list_data, |
---|
[4bee68d] | 248 | caption=panel.window_caption) |
---|
[31b0c47] | 249 | panel.reset_page(state=state) |
---|
[645f9b6] | 250 | panel.save_current_state() |
---|
[31b0c47] | 251 | |
---|
[90a7bbd] | 252 | def clear_panel(self): |
---|
[9b18735] | 253 | """ |
---|
| 254 | Clear and close all panels, used by guimanager |
---|
| 255 | """ |
---|
[90a7bbd] | 256 | |
---|
| 257 | #close all panels only when svs file opened |
---|
| 258 | self.close_all() |
---|
| 259 | self._manager.mypanels = [] |
---|
| 260 | |
---|
[ac2dc0e] | 261 | def on_close_page(self, event=None): |
---|
[51d47b5] | 262 | """ |
---|
[5062bbf] | 263 | close page and remove all references to the closed page |
---|
[51d47b5] | 264 | """ |
---|
[d361b462] | 265 | nbr_page = self.GetPageCount() |
---|
[51d47b5] | 266 | selected_page = self.GetPage(self.GetSelection()) |
---|
[f22e626] | 267 | if nbr_page == 1: |
---|
| 268 | if selected_page.get_data() == None: |
---|
| 269 | if event is not None: |
---|
| 270 | event.Veto() |
---|
[f32d144] | 271 | return |
---|
[2f189dc] | 272 | self._close_helper(selected_page=selected_page) |
---|
[9853ad0] | 273 | |
---|
[2f189dc] | 274 | def close_page_with_data(self, deleted_data): |
---|
| 275 | """ |
---|
[5062bbf] | 276 | close a fit page when its data is completely remove from the graph |
---|
[2f189dc] | 277 | """ |
---|
[784e2fa] | 278 | if deleted_data is None: |
---|
| 279 | return |
---|
[2f189dc] | 280 | for index in range(self.GetPageCount()): |
---|
[f32d144] | 281 | selected_page = self.GetPage(index) |
---|
| 282 | if hasattr(selected_page, "get_data"): |
---|
[2f189dc] | 283 | data = selected_page.get_data() |
---|
[ac2dc0e] | 284 | |
---|
[784e2fa] | 285 | if data is None: |
---|
[f32d144] | 286 | #the fitpanel exists and only the initial fit page is open |
---|
[784e2fa] | 287 | #with no selected data |
---|
| 288 | return |
---|
[f22e626] | 289 | if data.id == deleted_data.id: |
---|
[2f189dc] | 290 | self._close_helper(selected_page) |
---|
| 291 | self.DeletePage(index) |
---|
| 292 | break |
---|
[d89f09b] | 293 | |
---|
| 294 | def set_manager(self, manager): |
---|
| 295 | """ |
---|
[5062bbf] | 296 | set panel manager |
---|
| 297 | |
---|
| 298 | :param manager: instance of plugin fitting |
---|
| 299 | |
---|
[d89f09b] | 300 | """ |
---|
[c8deee5] | 301 | self._manager = manager |
---|
[3cd5806] | 302 | for pos in range(self.GetPageCount()): |
---|
| 303 | page = self.GetPage(pos) |
---|
| 304 | if page is not None: |
---|
| 305 | page.set_manager(self._manager) |
---|
[a93f525] | 306 | |
---|
[ea5fa58] | 307 | def set_model_list(self, dict): |
---|
[f32d144] | 308 | """ |
---|
| 309 | copy a dictionary of model into its own dictionary |
---|
| 310 | |
---|
[df7a7e3] | 311 | :param m_dict: dictionnary made of model name as key and model class |
---|
[f32d144] | 312 | as value |
---|
| 313 | """ |
---|
[ea5fa58] | 314 | self.model_list_box = dict |
---|
[51d47b5] | 315 | |
---|
[df7a7e3] | 316 | def set_model_dict(self, m_dict): |
---|
| 317 | """ |
---|
| 318 | copy a dictionary of model name -> model object |
---|
| 319 | |
---|
| 320 | :param m_dict: dictionary linking model name -> model object |
---|
| 321 | """ |
---|
| 322 | |
---|
[c77d859] | 323 | def get_current_page(self): |
---|
| 324 | """ |
---|
[5062bbf] | 325 | :return: the current page selected |
---|
| 326 | |
---|
[c77d859] | 327 | """ |
---|
[6bbeacd4] | 328 | return self.GetPage(self.GetSelection()) |
---|
[c77d859] | 329 | |
---|
[fa02d95] | 330 | def add_sim_page(self, caption="Simultaneous Fit"): |
---|
[925a30e] | 331 | """ |
---|
[5062bbf] | 332 | Add the simultaneous fit page |
---|
[925a30e] | 333 | """ |
---|
[51d47b5] | 334 | from simfitpage import SimultaneousFitPage |
---|
[fa02d95] | 335 | page_finder = self._manager.get_page_finder() |
---|
[09b3505] | 336 | if caption == "Simultaneous Fit": |
---|
[f32d144] | 337 | self.sim_page = SimultaneousFitPage(self, page_finder=page_finder, |
---|
[fa02d95] | 338 | id=-1, batch_on=False) |
---|
| 339 | self.sim_page.window_caption = caption |
---|
| 340 | self.sim_page.window_name = caption |
---|
| 341 | self.sim_page.uid = wx.NewId() |
---|
| 342 | self.AddPage(self.sim_page, caption, True) |
---|
| 343 | self.sim_page.set_manager(self._manager) |
---|
| 344 | self.enable_close_button() |
---|
| 345 | return self.sim_page |
---|
| 346 | else: |
---|
| 347 | self.batch_page = SimultaneousFitPage(self, batch_on=True, |
---|
| 348 | page_finder=page_finder) |
---|
| 349 | self.batch_page.window_caption = caption |
---|
| 350 | self.batch_page.window_name = caption |
---|
| 351 | self.batch_page.uid = wx.NewId() |
---|
| 352 | self.AddPage(self.batch_page, caption, True) |
---|
| 353 | self.batch_page.set_manager(self._manager) |
---|
| 354 | self.enable_close_button() |
---|
| 355 | return self.batch_page |
---|
[6bbeacd4] | 356 | |
---|
[2f189dc] | 357 | def add_empty_page(self): |
---|
[9853ad0] | 358 | """ |
---|
[5062bbf] | 359 | add an empty page |
---|
[9853ad0] | 360 | """ |
---|
[3f36675] | 361 | """ |
---|
[cc31608] | 362 | if self.batch_on: |
---|
| 363 | from batchfitpage import BatchFitPage |
---|
| 364 | panel = BatchFitPage(parent=self) |
---|
| 365 | #Increment index of batch page |
---|
| 366 | self.batch_page_index += 1 |
---|
| 367 | index = self.batch_page_index |
---|
| 368 | else: |
---|
[3f36675] | 369 | """ |
---|
| 370 | from fitpage import FitPage |
---|
[ea5fa58] | 371 | panel = FitPage(parent=self) |
---|
[aefc09f] | 372 | if self.batch_on: |
---|
| 373 | self.batch_page_index += 1 |
---|
[55bb249c] | 374 | caption = "BatchPage" + str(self.batch_page_index) |
---|
[aefc09f] | 375 | panel.set_index_model(self.batch_page_index) |
---|
| 376 | else: |
---|
| 377 | #Increment index of fit page |
---|
| 378 | self.fit_page_index += 1 |
---|
[55bb249c] | 379 | caption = "FitPage" + str(self.fit_page_index) |
---|
[aefc09f] | 380 | panel.set_index_model(self.fit_page_index) |
---|
[22ce7e2] | 381 | panel.batch_on = self.batch_on |
---|
[61523e2] | 382 | panel._set_save_flag(not panel.batch_on) |
---|
[df7a7e3] | 383 | panel.set_model_dictionary(self.model_dictionary) |
---|
[ea5fa58] | 384 | panel.populate_box(dict=self.model_list_box) |
---|
| 385 | panel.formfactor_combo_init() |
---|
[c8deee5] | 386 | panel.set_manager(self._manager) |
---|
[aefc09f] | 387 | panel.window_caption = caption |
---|
| 388 | panel.window_name = caption |
---|
[ae4ade7] | 389 | self.AddPage(panel, caption, select=True) |
---|
[66ff250] | 390 | self.opened_pages[panel.uid] = panel |
---|
| 391 | self.set_engine_helper(panel=panel) |
---|
[aefc09f] | 392 | self._manager.create_fit_problem(panel.uid) |
---|
| 393 | self._manager.page_finder[panel.uid].add_data(panel.get_data()) |
---|
[f22e626] | 394 | self.enable_close_button() |
---|
[dafc36f] | 395 | panel.on_set_focus(None) |
---|
[f32d144] | 396 | return panel |
---|
[2f189dc] | 397 | |
---|
[f22e626] | 398 | def enable_close_button(self): |
---|
| 399 | """ |
---|
[f32d144] | 400 | display the close button on tab for more than 1 tabs else remove the |
---|
[f22e626] | 401 | close button |
---|
| 402 | """ |
---|
| 403 | if self.GetPageCount() <= 1: |
---|
[f32d144] | 404 | style = self.GetWindowStyleFlag() |
---|
[3f36675] | 405 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 406 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == flag: |
---|
[f22e626] | 407 | style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 408 | self.SetWindowStyle(style) |
---|
| 409 | else: |
---|
| 410 | style = self.GetWindowStyleFlag() |
---|
[3f36675] | 411 | flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 412 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != flag: |
---|
[f22e626] | 413 | style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
| 414 | self.SetWindowStyle(style) |
---|
| 415 | |
---|
[8ee56a9] | 416 | def delete_data(self, data): |
---|
| 417 | """ |
---|
| 418 | Delete the given data |
---|
| 419 | """ |
---|
[f22e626] | 420 | if data.__class__.__name__ != "list": |
---|
| 421 | raise ValueError, "Fitpanel delete_data expect list of id" |
---|
| 422 | else: |
---|
| 423 | n = self.GetPageCount() |
---|
| 424 | for page in self.opened_pages.values(): |
---|
| 425 | pos = self.GetPageIndex(page) |
---|
| 426 | temp_data = page.get_data() |
---|
| 427 | if temp_data is not None and temp_data.id in data: |
---|
| 428 | self.SetSelection(pos) |
---|
| 429 | self.on_close_page(event=None) |
---|
| 430 | temp = self.GetSelection() |
---|
| 431 | self.DeletePage(temp) |
---|
[f32d144] | 432 | if self.GetPageCount() == 0: |
---|
[e4c9030] | 433 | self._manager.on_add_new_page(event=None) |
---|
[f22e626] | 434 | |
---|
[3f36675] | 435 | def set_data_on_batch_mode(self, data_list): |
---|
| 436 | """ |
---|
| 437 | Add all data to a single tab when the application is on Batch mode. |
---|
| 438 | However all data in the set of data must be either 1D or 2D type. |
---|
| 439 | This method presents option to select the data type before creating a |
---|
[f32d144] | 440 | tab. |
---|
[3f36675] | 441 | """ |
---|
| 442 | data_1d_list = [] |
---|
| 443 | data_2d_list = [] |
---|
[2e643a8] | 444 | group_id_1d = wx.NewId() |
---|
[4225aed] | 445 | # separate data into data1d and data2d list |
---|
[3f36675] | 446 | for data in data_list: |
---|
| 447 | if data.__class__.__name__ == "Data1D": |
---|
[2e643a8] | 448 | data.group_id = group_id_1d |
---|
[3f36675] | 449 | data_1d_list.append(data) |
---|
| 450 | if data.__class__.__name__ == "Data2D": |
---|
[017e0390] | 451 | data.group_id = wx.NewId() |
---|
[3f36675] | 452 | data_2d_list.append(data) |
---|
| 453 | page = None |
---|
| 454 | for p in self.opened_pages.values(): |
---|
[f32d144] | 455 | #check if there is an empty page to fill up |
---|
[dafc36f] | 456 | if not check_data_validity(p.get_data()) and p.batch_on: |
---|
[f3f6746] | 457 | |
---|
| 458 | #make sure data get placed in 1D empty tab if data is 1D |
---|
| 459 | #else data get place on 2D tab empty tab |
---|
| 460 | enable2D = p.get_view_mode() |
---|
| 461 | if (data.__class__.__name__ == "Data2D" and enable2D)\ |
---|
| 462 | or (data.__class__.__name__ == "Data1D" and not enable2D): |
---|
| 463 | page = p |
---|
| 464 | break |
---|
[3f36675] | 465 | if data_1d_list and data_2d_list: |
---|
| 466 | # need to warning the user that this batch is a special case |
---|
| 467 | from .fitting_widgets import BatchDataDialog |
---|
| 468 | dlg = BatchDataDialog(self) |
---|
| 469 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 470 | data_type = dlg.get_data() |
---|
| 471 | dlg.Destroy() |
---|
| 472 | if page is None: |
---|
| 473 | page = self.add_empty_page() |
---|
| 474 | if data_type == 1: |
---|
| 475 | #user has selected only data1D |
---|
| 476 | page.fill_data_combobox(data_1d_list) |
---|
| 477 | elif data_type == 2: |
---|
| 478 | page.fill_data_combobox(data_2d_list) |
---|
| 479 | else: |
---|
| 480 | #the batch analysis is canceled |
---|
[f32d144] | 481 | dlg.Destroy() |
---|
| 482 | return None |
---|
[3f36675] | 483 | else: |
---|
| 484 | if page is None: |
---|
| 485 | page = self.add_empty_page() |
---|
| 486 | if data_1d_list and not data_2d_list: |
---|
[f32d144] | 487 | #only on type of data |
---|
[3f36675] | 488 | page.fill_data_combobox(data_1d_list) |
---|
| 489 | elif not data_1d_list and data_2d_list: |
---|
| 490 | page.fill_data_combobox(data_2d_list) |
---|
[72bd8e7] | 491 | |
---|
[aefc09f] | 492 | pos = self.GetPageIndex(page) |
---|
[05efe88] | 493 | page.batch_on = self.batch_on |
---|
[61523e2] | 494 | page._set_save_flag(not page.batch_on) |
---|
[72bd8e7] | 495 | self.SetSelection(pos) |
---|
[3f36675] | 496 | self.opened_pages[page.uid] = page |
---|
| 497 | return page |
---|
| 498 | |
---|
[cc31608] | 499 | def set_data(self, data_list): |
---|
[f32d144] | 500 | """ |
---|
[5062bbf] | 501 | Add a fitting page on the notebook contained by fitpanel |
---|
| 502 | |
---|
| 503 | :param data: data to fit |
---|
| 504 | |
---|
[f32d144] | 505 | :return panel : page just added for further used. |
---|
[3f36675] | 506 | is used by fitting module |
---|
[5062bbf] | 507 | |
---|
[2f189dc] | 508 | """ |
---|
[cc31608] | 509 | if not data_list: |
---|
[2f189dc] | 510 | return None |
---|
[cc31608] | 511 | if self.batch_on: |
---|
[f32d144] | 512 | return self.set_data_on_batch_mode(data_list) |
---|
[cc31608] | 513 | else: |
---|
| 514 | data = None |
---|
[7f76f89] | 515 | try: |
---|
| 516 | data = data_list[0] |
---|
| 517 | except: |
---|
| 518 | # for 'fitv' files |
---|
| 519 | data_list = [data] |
---|
| 520 | data = data_list[0] |
---|
| 521 | |
---|
[cc31608] | 522 | if data is None: |
---|
| 523 | return None |
---|
[6bbeacd4] | 524 | for page in self.opened_pages.values(): |
---|
| 525 | #check if the selected data existing in the fitpanel |
---|
| 526 | pos = self.GetPageIndex(page) |
---|
[dafc36f] | 527 | if not check_data_validity(page.get_data()) and not page.batch_on: |
---|
[cc31608] | 528 | #make sure data get placed in 1D empty tab if data is 1D |
---|
| 529 | #else data get place on 2D tab empty tab |
---|
[a5701e6] | 530 | enable2D = page.get_view_mode() |
---|
| 531 | if (data.__class__.__name__ == "Data2D" and enable2D)\ |
---|
| 532 | or (data.__class__.__name__ == "Data1D" and not enable2D): |
---|
[05efe88] | 533 | page.batch_on = self.batch_on |
---|
[61523e2] | 534 | page._set_save_flag(not page.batch_on) |
---|
[cc31608] | 535 | page.fill_data_combobox(data_list) |
---|
[dafc36f] | 536 | #caption = "FitPage" + str(self.fit_page_index) |
---|
| 537 | self.SetPageText(pos, page.window_caption) |
---|
[cc31608] | 538 | self.SetSelection(pos) |
---|
| 539 | return page |
---|
[aefc09f] | 540 | #create new page and add data |
---|
[6bbeacd4] | 541 | page = self.add_empty_page() |
---|
[9f391af] | 542 | pos = self.GetPageIndex(page) |
---|
[cc31608] | 543 | page.fill_data_combobox(data_list) |
---|
[66ff250] | 544 | self.opened_pages[page.uid] = page |
---|
[09ef5a60] | 545 | self.SetSelection(pos) |
---|
[6bbeacd4] | 546 | return page |
---|
| 547 | |
---|
| 548 | def _onGetstate(self, event): |
---|
[cfc0913] | 549 | """ |
---|
[5062bbf] | 550 | copy the state of a page |
---|
[cfc0913] | 551 | """ |
---|
[6bbeacd4] | 552 | page = event.page |
---|
[66ff250] | 553 | if page.uid in self.fit_page_name: |
---|
[f32d144] | 554 | self.fit_page_name[page.uid].appendItem(page.createMemento()) |
---|
[cfc0913] | 555 | |
---|
[4e78f56] | 556 | def _onUndo(self, event): |
---|
[330573d] | 557 | """ |
---|
[5062bbf] | 558 | return the previous state of a given page is available |
---|
[330573d] | 559 | """ |
---|
[f32d144] | 560 | page = event.page |
---|
[66ff250] | 561 | if page.uid in self.fit_page_name: |
---|
[f32d144] | 562 | if self.fit_page_name[page.uid].getCurrentPosition() == 0: |
---|
[330573d] | 563 | state = None |
---|
| 564 | else: |
---|
[66ff250] | 565 | state = self.fit_page_name[page.uid].getPreviousItem() |
---|
[fe496eeb] | 566 | page._redo.Enable(True) |
---|
[330573d] | 567 | page.reset_page(state) |
---|
[fe496eeb] | 568 | |
---|
[f32d144] | 569 | def _onRedo(self, event): |
---|
[fe496eeb] | 570 | """ |
---|
[5062bbf] | 571 | return the next state available |
---|
[f32d144] | 572 | """ |
---|
| 573 | page = event.page |
---|
[66ff250] | 574 | if page.uid in self.fit_page_name: |
---|
[f32d144] | 575 | length = len(self.fit_page_name[page.uid]) |
---|
| 576 | if self.fit_page_name[page.uid].getCurrentPosition() == length - 1: |
---|
[fe496eeb] | 577 | state = None |
---|
| 578 | page._redo.Enable(False) |
---|
[3b9e023] | 579 | page._redo.Enable(True) |
---|
[fe496eeb] | 580 | else: |
---|
[f32d144] | 581 | state = self.fit_page_name[page.uid].getNextItem() |
---|
| 582 | page.reset_page(state) |
---|
[2f189dc] | 583 | |
---|
| 584 | def _close_helper(self, selected_page): |
---|
[3f1af74] | 585 | """ |
---|
[5062bbf] | 586 | Delete the given page from the notebook |
---|
[3f1af74] | 587 | """ |
---|
[2f189dc] | 588 | #remove hint page |
---|
[1b1bbf9] | 589 | #if selected_page == self.hint_page: |
---|
| 590 | # return |
---|
[2f189dc] | 591 | ## removing sim_page |
---|
[f32d144] | 592 | if selected_page == self.sim_page: |
---|
| 593 | self._manager.sim_page = None |
---|
[2f189dc] | 594 | return |
---|
[f32d144] | 595 | if selected_page == self.batch_page: |
---|
| 596 | self._manager.batch_page = None |
---|
[fa02d95] | 597 | return |
---|
[5e48acb] | 598 | """ |
---|
| 599 | # The below is not working when delete #5 and still have #6. |
---|
[cc31608] | 600 | if selected_page.__class__.__name__ == "FitPage": |
---|
| 601 | self.fit_page_index -= 1 |
---|
| 602 | else: |
---|
| 603 | self.batch_page_index -= 1 |
---|
[5e48acb] | 604 | """ |
---|
[2f189dc] | 605 | ## closing other pages |
---|
| 606 | state = selected_page.createMemento() |
---|
[f32d144] | 607 | page_finder = self._manager.get_page_finder() |
---|
[2f189dc] | 608 | ## removing fit page |
---|
[c6036f5] | 609 | data = selected_page.get_data() |
---|
| 610 | #Don' t remove plot for 2D |
---|
| 611 | flag = True |
---|
| 612 | if data.__class__.__name__ == 'Data2D': |
---|
| 613 | flag = False |
---|
[2f189dc] | 614 | if selected_page in page_finder: |
---|
| 615 | #Delete the name of the page into the list of open page |
---|
[66ff250] | 616 | for uid, list in self.opened_pages.iteritems(): |
---|
[2f189dc] | 617 | #Don't return any panel is the exact same page is created |
---|
[6bbeacd4] | 618 | |
---|
[66ff250] | 619 | if flag and selected_page.uid == uid: |
---|
| 620 | self._manager.remove_plot(uid, theory=False) |
---|
[f32d144] | 621 | break |
---|
[2f189dc] | 622 | del page_finder[selected_page] |
---|
[f32d144] | 623 | |
---|
[2f189dc] | 624 | #Delete the name of the page into the list of open page |
---|
[66ff250] | 625 | for uid, list in self.opened_pages.iteritems(): |
---|
[2f189dc] | 626 | #Don't return any panel is the exact same page is created |
---|
[66ff250] | 627 | if selected_page.uid == uid: |
---|
| 628 | del self.opened_pages[selected_page.uid] |
---|
[f32d144] | 629 | break |
---|
[81a7b6c] | 630 | ##remove the check box link to the model name of this page (selected_page) |
---|
| 631 | try: |
---|
| 632 | self.sim_page.draw_page() |
---|
| 633 | except: |
---|
| 634 | ## that page is already deleted no need to remove check box on |
---|
| 635 | ##non existing page |
---|
[f32d144] | 636 | pass |
---|
[fa02d95] | 637 | try: |
---|
| 638 | self.batch_page.draw_page() |
---|
| 639 | except: |
---|
| 640 | ## that page is already deleted no need to remove check box on |
---|
| 641 | ##non existing page |
---|
[f32d144] | 642 | pass |
---|