1 | |
---|
2 | import numpy |
---|
3 | import string |
---|
4 | import wx |
---|
5 | import sys |
---|
6 | #from wx.lib.flatnotebook import FlatNotebook as nb |
---|
7 | from wx.aui import AuiNotebook as nb |
---|
8 | |
---|
9 | from sans.guiframe.panel_base import PanelBase |
---|
10 | from sans.guiframe.events import PanelOnFocusEvent |
---|
11 | from sans.guiframe.events import StatusEvent |
---|
12 | |
---|
13 | import basepage |
---|
14 | import models |
---|
15 | _BOX_WIDTH = 80 |
---|
16 | |
---|
17 | |
---|
18 | class FitPanel(nb, PanelBase): |
---|
19 | |
---|
20 | """ |
---|
21 | FitPanel class contains fields allowing to fit models and data |
---|
22 | |
---|
23 | :note: For Fit to be performed the user should check at least one parameter |
---|
24 | on fit Panel window. |
---|
25 | |
---|
26 | """ |
---|
27 | ## Internal name for the AUI manager |
---|
28 | window_name = "Fit panel" |
---|
29 | ## Title to appear on top of the window |
---|
30 | window_caption = "Fit Panel " |
---|
31 | CENTER_PANE = True |
---|
32 | |
---|
33 | def __init__(self, parent, manager=None, *args, **kwargs): |
---|
34 | """ |
---|
35 | """ |
---|
36 | nb.__init__(self, parent, -1, |
---|
37 | style= wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
38 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
39 | wx.CLIP_CHILDREN) |
---|
40 | PanelBase.__init__(self, parent) |
---|
41 | #self.SetWindowStyleFlag(style=nb.FNB_FANCY_TABS) |
---|
42 | self._manager = manager |
---|
43 | self.parent = parent |
---|
44 | self.event_owner = None |
---|
45 | #dictionary of miodel {model class name, model class} |
---|
46 | self.menu_mng = models.ModelManager() |
---|
47 | self.model_list_box = self.menu_mng.get_model_list() |
---|
48 | #pageClosedEvent = nb.EVT_FLATNOTEBOOK_PAGE_CLOSING |
---|
49 | self.pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE |
---|
50 | |
---|
51 | self.Bind(self.pageClosedEvent, self.on_close_page) |
---|
52 | ## save the title of the last page tab added |
---|
53 | self.fit_page_name = {} |
---|
54 | ## list of existing fit page |
---|
55 | self.opened_pages = {} |
---|
56 | #page of simultaneous fit |
---|
57 | self.sim_page = None |
---|
58 | self.fit_engine_type = "scipy" |
---|
59 | ## get the state of a page |
---|
60 | self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate) |
---|
61 | self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo) |
---|
62 | self.Bind(basepage.EVT_NEXT_STATE, self._onRedo) |
---|
63 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.on_page_changing) |
---|
64 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSED, self.on_closed) |
---|
65 | |
---|
66 | def on_closed(self, event): |
---|
67 | """ |
---|
68 | """ |
---|
69 | if self.GetPageCount() == 0: |
---|
70 | self.add_empty_page() |
---|
71 | self.enable_close_button() |
---|
72 | |
---|
73 | def save_project(self, doc=None): |
---|
74 | """ |
---|
75 | return an xml node containing state of the panel |
---|
76 | that guiframe can write to file |
---|
77 | """ |
---|
78 | for uid, page in self.opened_pages.iteritems(): |
---|
79 | data = page.get_data() |
---|
80 | # state must be cloned |
---|
81 | state = page.get_state().clone() |
---|
82 | if data is not None: |
---|
83 | new_doc = self._manager.state_reader.write_toXML(data, state) |
---|
84 | if doc != None and hasattr(doc, "firstChild"): |
---|
85 | child = new_doc.firstChild.firstChild |
---|
86 | doc.firstChild.appendChild(child) |
---|
87 | else: |
---|
88 | doc = new_doc |
---|
89 | return doc |
---|
90 | |
---|
91 | def _on_engine_change(self, name='scipy'): |
---|
92 | """ |
---|
93 | """ |
---|
94 | for panel in self.opened_pages.values(): |
---|
95 | self.set_engine_helper(panel=panel, name=name) |
---|
96 | |
---|
97 | def set_engine_helper(self, panel, name='scipy'): |
---|
98 | """ |
---|
99 | """ |
---|
100 | self.fit_engine_type = name |
---|
101 | if panel != self.sim_page: |
---|
102 | panel._on_engine_change(name=self.fit_engine_type) |
---|
103 | |
---|
104 | def update_model_list(self): |
---|
105 | """ |
---|
106 | """ |
---|
107 | temp = self.menu_mng.update() |
---|
108 | if len(temp): |
---|
109 | self.model_list_box = temp |
---|
110 | return temp |
---|
111 | |
---|
112 | |
---|
113 | def get_page_by_id(self, uid): |
---|
114 | """ |
---|
115 | """ |
---|
116 | if uid not in self.opened_pages: |
---|
117 | msg = "Fitpanel cannot find ID: %s in self.opened_pages" % str(uid) |
---|
118 | raise ValueError, msg |
---|
119 | else: |
---|
120 | return self.opened_pages[uid] |
---|
121 | |
---|
122 | def on_page_changing(self, event): |
---|
123 | """ |
---|
124 | calls the function when the current event handler has exited. avoiding |
---|
125 | to call panel on focus on a panel that is currently deleted |
---|
126 | """ |
---|
127 | wx.CallAfter(self.helper_on_page_change) |
---|
128 | |
---|
129 | |
---|
130 | def helper_on_page_change(self): |
---|
131 | """ |
---|
132 | """ |
---|
133 | pos = self.GetSelection() |
---|
134 | if pos != -1: |
---|
135 | selected_page = self.GetPage(pos) |
---|
136 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page)) |
---|
137 | self.enable_close_button() |
---|
138 | |
---|
139 | def on_set_focus(self, event): |
---|
140 | """ |
---|
141 | """ |
---|
142 | pos = self.GetSelection() |
---|
143 | if pos != -1: |
---|
144 | selected_page = self.GetPage(pos) |
---|
145 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page)) |
---|
146 | |
---|
147 | def get_data(self): |
---|
148 | """ |
---|
149 | get the data in the current page |
---|
150 | """ |
---|
151 | pos = self.GetSelection() |
---|
152 | if pos != -1: |
---|
153 | selected_page = self.GetPage(pos) |
---|
154 | return selected_page.get_data() |
---|
155 | |
---|
156 | def set_model_state(state): |
---|
157 | """ |
---|
158 | receive a state to reset the model in the current page |
---|
159 | """ |
---|
160 | pos = self.GetSelection() |
---|
161 | if pos != -1: |
---|
162 | selected_page = self.GetPage(pos) |
---|
163 | selected_page.set_model_state(state) |
---|
164 | |
---|
165 | def get_state(self): |
---|
166 | """ |
---|
167 | return the state of the current selected page |
---|
168 | """ |
---|
169 | pos = self.GetSelection() |
---|
170 | if pos != -1: |
---|
171 | selected_page = self.GetPage(pos) |
---|
172 | return selected_page.get_state() |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | def close_all(self): |
---|
177 | """ |
---|
178 | remove all pages, used when a svs file is opened |
---|
179 | """ |
---|
180 | |
---|
181 | #get number of pages |
---|
182 | nop = self.GetPageCount() |
---|
183 | #use while-loop, for-loop will not do the job well. |
---|
184 | while (nop>0): |
---|
185 | #delete the first page until no page exists |
---|
186 | page = self.GetPage(0) |
---|
187 | if self._manager.parent.panel_on_focus == page: |
---|
188 | self._manager.parent.panel_on_focus = None |
---|
189 | self._close_helper(selected_page=page) |
---|
190 | self.DeletePage(0) |
---|
191 | nop = nop - 1 |
---|
192 | |
---|
193 | ## save the title of the last page tab added |
---|
194 | self.fit_page_name = {} |
---|
195 | ## list of existing fit page |
---|
196 | self.opened_pages = {} |
---|
197 | |
---|
198 | def set_state(self, state): |
---|
199 | """ |
---|
200 | Restore state of the panel |
---|
201 | """ |
---|
202 | page_is_opened = False |
---|
203 | if state is not None: |
---|
204 | for uid, panel in self.opened_pages.iteritems(): |
---|
205 | #Don't return any panel is the exact same page is created |
---|
206 | if uid == panel.uid and panel.data == state.data: |
---|
207 | # the page is still opened |
---|
208 | panel.reset_page(state=state) |
---|
209 | panel.save_current_state() |
---|
210 | page_is_opened = True |
---|
211 | if not page_is_opened: |
---|
212 | panel = self._manager.add_fit_page(data=state.data) |
---|
213 | # add data associated to the page created |
---|
214 | if panel is not None: |
---|
215 | self._manager.store_data(uid=panel.uid, |
---|
216 | data=state.data, |
---|
217 | caption=panel.window_caption) |
---|
218 | panel.reset_page(state=state) |
---|
219 | panel.save_current_state() |
---|
220 | |
---|
221 | def clear_panel(self): |
---|
222 | """ |
---|
223 | Clear and close all panels, used by guimanager |
---|
224 | """ |
---|
225 | |
---|
226 | #close all panels only when svs file opened |
---|
227 | self.close_all() |
---|
228 | self._manager.mypanels = [] |
---|
229 | |
---|
230 | |
---|
231 | def on_close_page(self, event=None): |
---|
232 | """ |
---|
233 | close page and remove all references to the closed page |
---|
234 | """ |
---|
235 | nbr_page = self.GetPageCount() |
---|
236 | selected_page = self.GetPage(self.GetSelection()) |
---|
237 | if nbr_page == 1: |
---|
238 | if selected_page.get_data() == None: |
---|
239 | if event is not None: |
---|
240 | event.Veto() |
---|
241 | return |
---|
242 | self._close_helper(selected_page=selected_page) |
---|
243 | |
---|
244 | def close_page_with_data(self, deleted_data): |
---|
245 | """ |
---|
246 | close a fit page when its data is completely remove from the graph |
---|
247 | """ |
---|
248 | if deleted_data is None: |
---|
249 | return |
---|
250 | for index in range(self.GetPageCount()): |
---|
251 | selected_page = self.GetPage(index) |
---|
252 | if hasattr(selected_page,"get_data"): |
---|
253 | data = selected_page.get_data() |
---|
254 | |
---|
255 | if data is None: |
---|
256 | #the fitpanel exists and only the initial fit page is open |
---|
257 | #with no selected data |
---|
258 | return |
---|
259 | if data.id == deleted_data.id: |
---|
260 | self._close_helper(selected_page) |
---|
261 | self.DeletePage(index) |
---|
262 | break |
---|
263 | |
---|
264 | def set_manager(self, manager): |
---|
265 | """ |
---|
266 | set panel manager |
---|
267 | |
---|
268 | :param manager: instance of plugin fitting |
---|
269 | |
---|
270 | """ |
---|
271 | self._manager = manager |
---|
272 | for pos in range(self.GetPageCount()): |
---|
273 | page = self.GetPage(pos) |
---|
274 | if page is not None: |
---|
275 | page.set_manager(self._manager) |
---|
276 | |
---|
277 | def set_model_list(self, dict): |
---|
278 | """ |
---|
279 | copy a dictionary of model into its own dictionary |
---|
280 | |
---|
281 | :param dict: dictionnary made of model name as key and model class |
---|
282 | as value |
---|
283 | """ |
---|
284 | self.model_list_box = dict |
---|
285 | |
---|
286 | def get_current_page(self): |
---|
287 | """ |
---|
288 | :return: the current page selected |
---|
289 | |
---|
290 | """ |
---|
291 | return self.GetPage(self.GetSelection()) |
---|
292 | |
---|
293 | def add_sim_page(self): |
---|
294 | """ |
---|
295 | Add the simultaneous fit page |
---|
296 | """ |
---|
297 | from simfitpage import SimultaneousFitPage |
---|
298 | page_finder= self._manager.get_page_finder() |
---|
299 | self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1) |
---|
300 | self.sim_page.uid = wx.NewId() |
---|
301 | self.AddPage(self.sim_page,"Simultaneous Fit", True) |
---|
302 | self.sim_page.set_manager(self._manager) |
---|
303 | self.enable_close_button() |
---|
304 | return self.sim_page |
---|
305 | |
---|
306 | |
---|
307 | def add_empty_page(self): |
---|
308 | """ |
---|
309 | add an empty page |
---|
310 | """ |
---|
311 | from fitpage import FitPage |
---|
312 | panel = FitPage(parent=self) |
---|
313 | panel.uid = wx.NewId() |
---|
314 | panel.populate_box(dict=self.model_list_box) |
---|
315 | panel.set_manager(self._manager) |
---|
316 | caption = str(panel.window_name) + " " + str(self._manager.index_model) |
---|
317 | self.AddPage(panel, caption, select=True) |
---|
318 | self.opened_pages[panel.uid] = panel |
---|
319 | self.set_engine_helper(panel=panel) |
---|
320 | self.enable_close_button() |
---|
321 | return panel |
---|
322 | |
---|
323 | def enable_close_button(self): |
---|
324 | """ |
---|
325 | display the close button on tab for more than 1 tabs else remove the |
---|
326 | close button |
---|
327 | """ |
---|
328 | if self.GetPageCount() <= 1: |
---|
329 | style = self.GetWindowStyleFlag() |
---|
330 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB: |
---|
331 | style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
332 | self.SetWindowStyle(style) |
---|
333 | else: |
---|
334 | style = self.GetWindowStyleFlag() |
---|
335 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB: |
---|
336 | style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
337 | self.SetWindowStyle(style) |
---|
338 | |
---|
339 | def delete_data(self, data): |
---|
340 | """ |
---|
341 | Delete the given data |
---|
342 | """ |
---|
343 | if data.__class__.__name__ != "list": |
---|
344 | raise ValueError, "Fitpanel delete_data expect list of id" |
---|
345 | else: |
---|
346 | n = self.GetPageCount() |
---|
347 | for page in self.opened_pages.values(): |
---|
348 | pos = self.GetPageIndex(page) |
---|
349 | temp_data = page.get_data() |
---|
350 | #stop the fitting before deleting the page |
---|
351 | page.is_fitting() |
---|
352 | if temp_data is not None and temp_data.id in data: |
---|
353 | self.SetSelection(pos) |
---|
354 | self.on_close_page(event=None) |
---|
355 | temp = self.GetSelection() |
---|
356 | self.DeletePage(temp) |
---|
357 | if self.GetPageCount()== 0: |
---|
358 | self._manager.on_add_new_page(event=None) |
---|
359 | |
---|
360 | def set_data(self, data): |
---|
361 | """ |
---|
362 | Add a fitting page on the notebook contained by fitpanel |
---|
363 | |
---|
364 | :param data: data to fit |
---|
365 | |
---|
366 | :return panel : page just added for further used. is used by fitting module |
---|
367 | |
---|
368 | """ |
---|
369 | if data is None: |
---|
370 | return None |
---|
371 | for page in self.opened_pages.values(): |
---|
372 | #check if the selected data existing in the fitpanel |
---|
373 | pos = self.GetPageIndex(page) |
---|
374 | if page.get_data() is None: |
---|
375 | enable2D = page.get_view_mode() |
---|
376 | if (data.__class__.__name__ == "Data2D" and enable2D)\ |
---|
377 | or (data.__class__.__name__ == "Data1D" and not enable2D): |
---|
378 | page.set_data(data) |
---|
379 | self.SetPageText(pos, str(data.name)) |
---|
380 | self.SetSelection(pos) |
---|
381 | return page |
---|
382 | |
---|
383 | elif page.get_data().id == data.id: |
---|
384 | msg = "Data already existing in the fitting panel" |
---|
385 | wx.PostEvent(self._manager.parent, |
---|
386 | StatusEvent(status=msg, info='warning')) |
---|
387 | self.SetSelection(pos) |
---|
388 | return page |
---|
389 | |
---|
390 | page = self.add_empty_page() |
---|
391 | pos = self.GetPageIndex(page) |
---|
392 | page.set_data(data) |
---|
393 | self.SetPageText(pos, str(data.name)) |
---|
394 | self.opened_pages[page.uid] = page |
---|
395 | |
---|
396 | return page |
---|
397 | |
---|
398 | def _onGetstate(self, event): |
---|
399 | """ |
---|
400 | copy the state of a page |
---|
401 | """ |
---|
402 | page = event.page |
---|
403 | if page.uid in self.fit_page_name: |
---|
404 | self.fit_page_name[page.uid].appendItem(page.createMemento()) |
---|
405 | |
---|
406 | def _onUndo(self, event): |
---|
407 | """ |
---|
408 | return the previous state of a given page is available |
---|
409 | """ |
---|
410 | page = event.page |
---|
411 | if page.uid in self.fit_page_name: |
---|
412 | if self.fit_page_name[page.uid].getCurrentPosition()==0: |
---|
413 | state = None |
---|
414 | else: |
---|
415 | state = self.fit_page_name[page.uid].getPreviousItem() |
---|
416 | page._redo.Enable(True) |
---|
417 | page.reset_page(state) |
---|
418 | |
---|
419 | def _onRedo(self, event): |
---|
420 | """ |
---|
421 | return the next state available |
---|
422 | """ |
---|
423 | page = event.page |
---|
424 | if page.uid in self.fit_page_name: |
---|
425 | length= len(self.fit_page_name[page.uid]) |
---|
426 | if self.fit_page_name[page.uid].getCurrentPosition()== length -1: |
---|
427 | state = None |
---|
428 | page._redo.Enable(False) |
---|
429 | page._redo.Enable(True) |
---|
430 | else: |
---|
431 | state =self.fit_page_name[page.uid].getNextItem() |
---|
432 | page.reset_page(state) |
---|
433 | |
---|
434 | def _close_helper(self, selected_page): |
---|
435 | """ |
---|
436 | Delete the given page from the notebook |
---|
437 | """ |
---|
438 | #remove hint page |
---|
439 | #if selected_page == self.hint_page: |
---|
440 | # return |
---|
441 | ## removing sim_page |
---|
442 | if selected_page == self.sim_page : |
---|
443 | self._manager.sim_page=None |
---|
444 | return |
---|
445 | |
---|
446 | ## closing other pages |
---|
447 | state = selected_page.createMemento() |
---|
448 | page_name = selected_page.window_name |
---|
449 | page_finder = self._manager.get_page_finder() |
---|
450 | fitproblem = None |
---|
451 | ## removing fit page |
---|
452 | data = selected_page.get_data() |
---|
453 | #Don' t remove plot for 2D |
---|
454 | flag = True |
---|
455 | if data.__class__.__name__ == 'Data2D': |
---|
456 | flag = False |
---|
457 | if selected_page in page_finder: |
---|
458 | #Delete the name of the page into the list of open page |
---|
459 | for uid, list in self.opened_pages.iteritems(): |
---|
460 | #Don't return any panel is the exact same page is created |
---|
461 | |
---|
462 | if flag and selected_page.uid == uid: |
---|
463 | self._manager.remove_plot(uid, theory=False) |
---|
464 | break |
---|
465 | del page_finder[selected_page] |
---|
466 | ##remove the check box link to the model name of this page (selected_page) |
---|
467 | try: |
---|
468 | self.sim_page.draw_page() |
---|
469 | except: |
---|
470 | ## that page is already deleted no need to remove check box on |
---|
471 | ##non existing page |
---|
472 | pass |
---|
473 | |
---|
474 | #Delete the name of the page into the list of open page |
---|
475 | for uid, list in self.opened_pages.iteritems(): |
---|
476 | #Don't return any panel is the exact same page is created |
---|
477 | if selected_page.uid == uid: |
---|
478 | del self.opened_pages[selected_page.uid] |
---|
479 | break |
---|
480 | |
---|
481 | |
---|