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 | |
---|
385 | elif page.get_data().id == data.id: |
---|
386 | msg = "Data already existing in the fitting panel" |
---|
387 | wx.PostEvent(self._manager.parent, |
---|
388 | StatusEvent(status=msg, info='warning')) |
---|
389 | self.SetSelection(pos) |
---|
390 | return page |
---|
391 | |
---|
392 | page = self.add_empty_page() |
---|
393 | pos = self.GetPageIndex(page) |
---|
394 | page.set_data(data) |
---|
395 | self.SetPageText(pos, str(data.name)) |
---|
396 | self.opened_pages[page.uid] = page |
---|
397 | |
---|
398 | return page |
---|
399 | |
---|
400 | def _onGetstate(self, event): |
---|
401 | """ |
---|
402 | copy the state of a page |
---|
403 | """ |
---|
404 | page = event.page |
---|
405 | if page.uid in self.fit_page_name: |
---|
406 | self.fit_page_name[page.uid].appendItem(page.createMemento()) |
---|
407 | |
---|
408 | def _onUndo(self, event): |
---|
409 | """ |
---|
410 | return the previous state of a given page is available |
---|
411 | """ |
---|
412 | page = event.page |
---|
413 | if page.uid in self.fit_page_name: |
---|
414 | if self.fit_page_name[page.uid].getCurrentPosition()==0: |
---|
415 | state = None |
---|
416 | else: |
---|
417 | state = self.fit_page_name[page.uid].getPreviousItem() |
---|
418 | page._redo.Enable(True) |
---|
419 | page.reset_page(state) |
---|
420 | |
---|
421 | def _onRedo(self, event): |
---|
422 | """ |
---|
423 | return the next state available |
---|
424 | """ |
---|
425 | page = event.page |
---|
426 | if page.uid in self.fit_page_name: |
---|
427 | length= len(self.fit_page_name[page.uid]) |
---|
428 | if self.fit_page_name[page.uid].getCurrentPosition()== length -1: |
---|
429 | state = None |
---|
430 | page._redo.Enable(False) |
---|
431 | page._redo.Enable(True) |
---|
432 | else: |
---|
433 | state =self.fit_page_name[page.uid].getNextItem() |
---|
434 | page.reset_page(state) |
---|
435 | |
---|
436 | def _close_helper(self, selected_page): |
---|
437 | """ |
---|
438 | Delete the given page from the notebook |
---|
439 | """ |
---|
440 | #remove hint page |
---|
441 | #if selected_page == self.hint_page: |
---|
442 | # return |
---|
443 | ## removing sim_page |
---|
444 | if selected_page == self.sim_page : |
---|
445 | self._manager.sim_page=None |
---|
446 | return |
---|
447 | |
---|
448 | ## closing other pages |
---|
449 | state = selected_page.createMemento() |
---|
450 | page_name = selected_page.window_name |
---|
451 | page_finder = self._manager.get_page_finder() |
---|
452 | fitproblem = None |
---|
453 | ## removing fit page |
---|
454 | data = selected_page.get_data() |
---|
455 | #Don' t remove plot for 2D |
---|
456 | flag = True |
---|
457 | if data.__class__.__name__ == 'Data2D': |
---|
458 | flag = False |
---|
459 | if selected_page in page_finder: |
---|
460 | #Delete the name of the page into the list of open page |
---|
461 | for uid, list in self.opened_pages.iteritems(): |
---|
462 | #Don't return any panel is the exact same page is created |
---|
463 | |
---|
464 | if flag and selected_page.uid == uid: |
---|
465 | self._manager.remove_plot(uid, theory=False) |
---|
466 | break |
---|
467 | del page_finder[selected_page] |
---|
468 | ##remove the check box link to the model name of this page (selected_page) |
---|
469 | try: |
---|
470 | self.sim_page.draw_page() |
---|
471 | except: |
---|
472 | ## that page is already deleted no need to remove check box on |
---|
473 | ##non existing page |
---|
474 | pass |
---|
475 | |
---|
476 | #Delete the name of the page into the list of open page |
---|
477 | for uid, list in self.opened_pages.iteritems(): |
---|
478 | #Don't return any panel is the exact same page is created |
---|
479 | if selected_page.uid == uid: |
---|
480 | del self.opened_pages[selected_page.uid] |
---|
481 | break |
---|
482 | |
---|
483 | |
---|