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