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