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 | index = self.GetPageCount() |
---|
317 | #caption = str(panel.window_name) + " " + str(self._manager.index_model) |
---|
318 | caption = str(panel.window_name) + " " + str(index) |
---|
319 | self.AddPage(panel, caption, select=True) |
---|
320 | self.opened_pages[panel.uid] = panel |
---|
321 | self.set_engine_helper(panel=panel) |
---|
322 | self.enable_close_button() |
---|
323 | return panel |
---|
324 | |
---|
325 | def enable_close_button(self): |
---|
326 | """ |
---|
327 | display the close button on tab for more than 1 tabs else remove the |
---|
328 | close button |
---|
329 | """ |
---|
330 | if self.GetPageCount() <= 1: |
---|
331 | style = self.GetWindowStyleFlag() |
---|
332 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB: |
---|
333 | style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
334 | self.SetWindowStyle(style) |
---|
335 | else: |
---|
336 | style = self.GetWindowStyleFlag() |
---|
337 | if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB: |
---|
338 | style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB |
---|
339 | self.SetWindowStyle(style) |
---|
340 | |
---|
341 | def delete_data(self, data): |
---|
342 | """ |
---|
343 | Delete the given data |
---|
344 | """ |
---|
345 | if data.__class__.__name__ != "list": |
---|
346 | raise ValueError, "Fitpanel delete_data expect list of id" |
---|
347 | else: |
---|
348 | n = self.GetPageCount() |
---|
349 | for page in self.opened_pages.values(): |
---|
350 | pos = self.GetPageIndex(page) |
---|
351 | temp_data = page.get_data() |
---|
352 | #stop the fitting before deleting the page |
---|
353 | page.is_fitting() |
---|
354 | if temp_data is not None and temp_data.id in data: |
---|
355 | self.SetSelection(pos) |
---|
356 | self.on_close_page(event=None) |
---|
357 | temp = self.GetSelection() |
---|
358 | self.DeletePage(temp) |
---|
359 | if self.GetPageCount()== 0: |
---|
360 | self._manager.on_add_new_page(event=None) |
---|
361 | |
---|
362 | def set_data(self, data): |
---|
363 | """ |
---|
364 | Add a fitting page on the notebook contained by fitpanel |
---|
365 | |
---|
366 | :param data: data to fit |
---|
367 | |
---|
368 | :return panel : page just added for further used. is used by fitting module |
---|
369 | |
---|
370 | """ |
---|
371 | if data is None: |
---|
372 | return None |
---|
373 | for page in self.opened_pages.values(): |
---|
374 | #check if the selected data existing in the fitpanel |
---|
375 | pos = self.GetPageIndex(page) |
---|
376 | if page.get_data() is None: |
---|
377 | enable2D = page.get_view_mode() |
---|
378 | if (data.__class__.__name__ == "Data2D" and enable2D)\ |
---|
379 | or (data.__class__.__name__ == "Data1D" and not enable2D): |
---|
380 | page.set_data(data) |
---|
381 | self.SetPageText(pos, str(data.name)) |
---|
382 | self.SetSelection(pos) |
---|
383 | return page |
---|
384 | # Todo: Need to set different window name when has same data |
---|
385 | # Todo: to catch page with same data even if it is not on the top. |
---|
386 | """ |
---|
387 | elif page.get_data().id == data.id: |
---|
388 | msg = "Data already existing in the fitting panel" |
---|
389 | wx.PostEvent(self._manager.parent, |
---|
390 | StatusEvent(status=msg, info='warning')) |
---|
391 | self.SetSelection(pos) |
---|
392 | return page |
---|
393 | """ |
---|
394 | page = self.add_empty_page() |
---|
395 | pos = self.GetPageIndex(page) |
---|
396 | page.set_data(data) |
---|
397 | self.SetPageText(pos, str(data.name)) |
---|
398 | self.opened_pages[page.uid] = page |
---|
399 | |
---|
400 | return page |
---|
401 | |
---|
402 | def _onGetstate(self, event): |
---|
403 | """ |
---|
404 | copy the state of a page |
---|
405 | """ |
---|
406 | page = event.page |
---|
407 | if page.uid in self.fit_page_name: |
---|
408 | self.fit_page_name[page.uid].appendItem(page.createMemento()) |
---|
409 | |
---|
410 | def _onUndo(self, event): |
---|
411 | """ |
---|
412 | return the previous state of a given page is available |
---|
413 | """ |
---|
414 | page = event.page |
---|
415 | if page.uid in self.fit_page_name: |
---|
416 | if self.fit_page_name[page.uid].getCurrentPosition()==0: |
---|
417 | state = None |
---|
418 | else: |
---|
419 | state = self.fit_page_name[page.uid].getPreviousItem() |
---|
420 | page._redo.Enable(True) |
---|
421 | page.reset_page(state) |
---|
422 | |
---|
423 | def _onRedo(self, event): |
---|
424 | """ |
---|
425 | return the next state available |
---|
426 | """ |
---|
427 | page = event.page |
---|
428 | if page.uid in self.fit_page_name: |
---|
429 | length= len(self.fit_page_name[page.uid]) |
---|
430 | if self.fit_page_name[page.uid].getCurrentPosition()== length -1: |
---|
431 | state = None |
---|
432 | page._redo.Enable(False) |
---|
433 | page._redo.Enable(True) |
---|
434 | else: |
---|
435 | state =self.fit_page_name[page.uid].getNextItem() |
---|
436 | page.reset_page(state) |
---|
437 | |
---|
438 | def _close_helper(self, selected_page): |
---|
439 | """ |
---|
440 | Delete the given page from the notebook |
---|
441 | """ |
---|
442 | #remove hint page |
---|
443 | #if selected_page == self.hint_page: |
---|
444 | # return |
---|
445 | ## removing sim_page |
---|
446 | if selected_page == self.sim_page : |
---|
447 | self._manager.sim_page=None |
---|
448 | return |
---|
449 | |
---|
450 | ## closing other pages |
---|
451 | state = selected_page.createMemento() |
---|
452 | page_name = selected_page.window_name |
---|
453 | page_finder = self._manager.get_page_finder() |
---|
454 | fitproblem = None |
---|
455 | ## removing fit page |
---|
456 | data = selected_page.get_data() |
---|
457 | #Don' t remove plot for 2D |
---|
458 | flag = True |
---|
459 | if data.__class__.__name__ == 'Data2D': |
---|
460 | flag = False |
---|
461 | if selected_page in page_finder: |
---|
462 | #Delete the name of the page into the list of open page |
---|
463 | for uid, list in self.opened_pages.iteritems(): |
---|
464 | #Don't return any panel is the exact same page is created |
---|
465 | |
---|
466 | if flag and selected_page.uid == uid: |
---|
467 | self._manager.remove_plot(uid, theory=False) |
---|
468 | break |
---|
469 | del page_finder[selected_page] |
---|
470 | ##remove the check box link to the model name of this page (selected_page) |
---|
471 | try: |
---|
472 | self.sim_page.draw_page() |
---|
473 | except: |
---|
474 | ## that page is already deleted no need to remove check box on |
---|
475 | ##non existing page |
---|
476 | pass |
---|
477 | |
---|
478 | #Delete the name of the page into the list of open page |
---|
479 | for uid, list in self.opened_pages.iteritems(): |
---|
480 | #Don't return any panel is the exact same page is created |
---|
481 | if selected_page.uid == uid: |
---|
482 | del self.opened_pages[selected_page.uid] |
---|
483 | break |
---|
484 | |
---|
485 | |
---|