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