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 get_state(self): |
---|
104 | """ |
---|
105 | return the state of the current selected page |
---|
106 | """ |
---|
107 | pos = self.GetSelection() |
---|
108 | if pos != -1: |
---|
109 | selected_page = self.GetPage(pos) |
---|
110 | return selected_page.get_state() |
---|
111 | |
---|
112 | def add_default_pages(self): |
---|
113 | """ |
---|
114 | Add default pages such as a hint page and an empty fit page |
---|
115 | """ |
---|
116 | #add default page |
---|
117 | from hint_fitpage import HintFitPage |
---|
118 | self.hint_page = HintFitPage(self) |
---|
119 | self.AddPage(self.hint_page,"Hint") |
---|
120 | self.hint_page.set_manager(self._manager) |
---|
121 | |
---|
122 | def close_all(self): |
---|
123 | """ |
---|
124 | remove all pages, used when a svs file is opened |
---|
125 | """ |
---|
126 | |
---|
127 | #get number of pages |
---|
128 | nop = self.GetPageCount() |
---|
129 | #use while-loop, for-loop will not do the job well. |
---|
130 | while (nop>0): |
---|
131 | #delete the first page until no page exists |
---|
132 | page = self.GetPage(0) |
---|
133 | if self._manager.parent.panel_on_focus == page: |
---|
134 | self._manager.parent.panel_on_focus = None |
---|
135 | self._close_helper(selected_page=page) |
---|
136 | self.DeletePage(0) |
---|
137 | nop = nop - 1 |
---|
138 | |
---|
139 | ## save the title of the last page tab added |
---|
140 | self.fit_page_name = {} |
---|
141 | ## list of existing fit page |
---|
142 | self.opened_pages = {} |
---|
143 | |
---|
144 | def set_state(self, state): |
---|
145 | """ |
---|
146 | Restore state of the panel |
---|
147 | """ |
---|
148 | page_is_opened = False |
---|
149 | if state is not None: |
---|
150 | for id, panel in self.opened_pages.values(): |
---|
151 | #Don't return any panel is the exact same page is created |
---|
152 | if id == panel.id: |
---|
153 | # the page is still opened |
---|
154 | panel.reset_page(state=state) |
---|
155 | panel.save_current_state() |
---|
156 | page_is_opened = True |
---|
157 | if not page_is_opened: |
---|
158 | panel = self._manager.add_fit_page(data=state.data) |
---|
159 | # add data associated to the page created |
---|
160 | if panel is not None: |
---|
161 | self._manager.store_page(page=panel.id, data=state.data) |
---|
162 | panel.reset_page(state=state) |
---|
163 | panel.save_current_state() |
---|
164 | |
---|
165 | def clear_panel(self): |
---|
166 | """ |
---|
167 | Clear and close all panels, used by guimanager |
---|
168 | """ |
---|
169 | |
---|
170 | #close all panels only when svs file opened |
---|
171 | self.close_all() |
---|
172 | self._manager.mypanels = [] |
---|
173 | |
---|
174 | |
---|
175 | def on_close_page(self, event=None): |
---|
176 | """ |
---|
177 | close page and remove all references to the closed page |
---|
178 | """ |
---|
179 | nbr_page = self.GetPageCount() |
---|
180 | if nbr_page == 1: |
---|
181 | |
---|
182 | event.Veto() |
---|
183 | return |
---|
184 | selected_page = self.GetPage(self.GetSelection()) |
---|
185 | self._close_helper(selected_page=selected_page) |
---|
186 | |
---|
187 | def close_page_with_data(self, deleted_data): |
---|
188 | """ |
---|
189 | close a fit page when its data is completely remove from the graph |
---|
190 | """ |
---|
191 | if deleted_data is None: |
---|
192 | return |
---|
193 | for index in range(self.GetPageCount()): |
---|
194 | selected_page = self.GetPage(index) |
---|
195 | if hasattr(selected_page,"get_data"): |
---|
196 | data = selected_page.get_data() |
---|
197 | |
---|
198 | if data is None: |
---|
199 | #the fitpanel exists and only the initial fit page is open |
---|
200 | #with no selected data |
---|
201 | return |
---|
202 | if data.name == deleted_data.name: |
---|
203 | self._close_helper(selected_page) |
---|
204 | self.DeletePage(index) |
---|
205 | break |
---|
206 | |
---|
207 | def set_manager(self, manager): |
---|
208 | """ |
---|
209 | set panel manager |
---|
210 | |
---|
211 | :param manager: instance of plugin fitting |
---|
212 | |
---|
213 | """ |
---|
214 | self._manager = manager |
---|
215 | for pos in range(self.GetPageCount()): |
---|
216 | page = self.GetPage(pos) |
---|
217 | if page is not None: |
---|
218 | page.set_manager(self._manager) |
---|
219 | |
---|
220 | def set_model_list(self, dict): |
---|
221 | """ |
---|
222 | copy a dictionary of model into its own dictionary |
---|
223 | |
---|
224 | :param dict: dictionnary made of model name as key and model class |
---|
225 | as value |
---|
226 | """ |
---|
227 | self.model_list_box = dict |
---|
228 | |
---|
229 | def get_current_page(self): |
---|
230 | """ |
---|
231 | :return: the current page selected |
---|
232 | |
---|
233 | """ |
---|
234 | return self.GetPage(self.GetSelection()) |
---|
235 | |
---|
236 | def add_sim_page(self): |
---|
237 | """ |
---|
238 | Add the simultaneous fit page |
---|
239 | """ |
---|
240 | from simfitpage import SimultaneousFitPage |
---|
241 | page_finder= self._manager.get_page_finder() |
---|
242 | self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1) |
---|
243 | self.sim_page.id = wx.NewId() |
---|
244 | self.AddPage(self.sim_page,"Simultaneous Fit", True) |
---|
245 | self.sim_page.set_manager(self._manager) |
---|
246 | return self.sim_page |
---|
247 | |
---|
248 | |
---|
249 | def add_empty_page(self): |
---|
250 | """ |
---|
251 | add an empty page |
---|
252 | """ |
---|
253 | from fitpage import FitPage |
---|
254 | panel = FitPage(parent=self) |
---|
255 | panel.id = wx.NewId() |
---|
256 | panel.populate_box(dict=self.model_list_box) |
---|
257 | panel.set_manager(self._manager) |
---|
258 | caption = str(panel.window_name) + " " + str(self._manager.index_model) |
---|
259 | self.AddPage(panel, caption, select=True) |
---|
260 | self.opened_pages[panel.id] = panel |
---|
261 | return panel |
---|
262 | |
---|
263 | |
---|
264 | def set_data(self, data): |
---|
265 | """ |
---|
266 | Add a fitting page on the notebook contained by fitpanel |
---|
267 | |
---|
268 | :param data: data to fit |
---|
269 | |
---|
270 | :return panel : page just added for further used. is used by fitting module |
---|
271 | |
---|
272 | """ |
---|
273 | if data is None: |
---|
274 | return None |
---|
275 | for page in self.opened_pages.values(): |
---|
276 | #check if the selected data existing in the fitpanel |
---|
277 | pos = self.GetPageIndex(page) |
---|
278 | if page.get_data() is None: |
---|
279 | enable2D = page.get_view_mode() |
---|
280 | if (data.__class__.__name__ == "Data2D" and enable2D)\ |
---|
281 | or (data.__class__.__name__ == "Data1D" and not enable2D): |
---|
282 | page.set_data(data) |
---|
283 | self.SetPageText(pos, str(data.name)) |
---|
284 | self.SetSelection(pos) |
---|
285 | return page |
---|
286 | |
---|
287 | elif page.get_data().id == data.id: |
---|
288 | msg = "Data already existing in the fitting panel" |
---|
289 | wx.PostEvent(self._manager.parent, |
---|
290 | StatusEvent(status=msg, info='warning')) |
---|
291 | self.SetSelection(pos) |
---|
292 | return page |
---|
293 | |
---|
294 | page = self.add_empty_page() |
---|
295 | pos = self.GetPageIndex(page) |
---|
296 | page.id = wx.NewId() |
---|
297 | page.set_data(data) |
---|
298 | self.SetPageText(pos, str(data.name)) |
---|
299 | self.opened_pages[page.id] = page |
---|
300 | |
---|
301 | return page |
---|
302 | |
---|
303 | def _onGetstate(self, event): |
---|
304 | """ |
---|
305 | copy the state of a page |
---|
306 | """ |
---|
307 | page = event.page |
---|
308 | if page.id in self.fit_page_name: |
---|
309 | self.fit_page_name[page.id].appendItem(page.createMemento()) |
---|
310 | |
---|
311 | def _onUndo(self, event ): |
---|
312 | """ |
---|
313 | return the previous state of a given page is available |
---|
314 | """ |
---|
315 | page = event.page |
---|
316 | if page.id in self.fit_page_name: |
---|
317 | if self.fit_page_name[page.id].getCurrentPosition()==0: |
---|
318 | state = None |
---|
319 | else: |
---|
320 | state = self.fit_page_name[page.id].getPreviousItem() |
---|
321 | page._redo.Enable(True) |
---|
322 | page.reset_page(state) |
---|
323 | |
---|
324 | def _onRedo(self, event): |
---|
325 | """ |
---|
326 | return the next state available |
---|
327 | """ |
---|
328 | page = event.page |
---|
329 | if page.id in self.fit_page_name: |
---|
330 | length= len(self.fit_page_name[page.id]) |
---|
331 | if self.fit_page_name[page.id].getCurrentPosition()== length -1: |
---|
332 | state = None |
---|
333 | page._redo.Enable(False) |
---|
334 | page._redo.Enable(True) |
---|
335 | else: |
---|
336 | state =self.fit_page_name[page.id].getNextItem() |
---|
337 | page.reset_page(state) |
---|
338 | |
---|
339 | def _close_helper(self, selected_page): |
---|
340 | """ |
---|
341 | Delete the given page from the notebook |
---|
342 | """ |
---|
343 | #remove hint page |
---|
344 | if selected_page == self.hint_page: |
---|
345 | return |
---|
346 | ## removing sim_page |
---|
347 | if selected_page == self.sim_page : |
---|
348 | self._manager.sim_page=None |
---|
349 | return |
---|
350 | |
---|
351 | ## closing other pages |
---|
352 | state = selected_page.createMemento() |
---|
353 | page_name = selected_page.window_name |
---|
354 | page_finder = self._manager.get_page_finder() |
---|
355 | fitproblem = None |
---|
356 | ## removing fit page |
---|
357 | data = selected_page.get_data() |
---|
358 | #Don' t remove plot for 2D |
---|
359 | flag = True |
---|
360 | if data.__class__.__name__ == 'Data2D': |
---|
361 | flag = False |
---|
362 | if selected_page in page_finder: |
---|
363 | #Delete the name of the page into the list of open page |
---|
364 | for id, list in self.opened_pages.iteritems(): |
---|
365 | #Don't return any panel is the exact same page is created |
---|
366 | |
---|
367 | if flag and selected_page.id == id: |
---|
368 | self._manager.remove_plot(id, theory=False) |
---|
369 | break |
---|
370 | del page_finder[selected_page] |
---|
371 | ##remove the check box link to the model name of this page (selected_page) |
---|
372 | try: |
---|
373 | self.sim_page.draw_page() |
---|
374 | except: |
---|
375 | ## that page is already deleted no need to remove check box on |
---|
376 | ##non existing page |
---|
377 | pass |
---|
378 | |
---|
379 | #Delete the name of the page into the list of open page |
---|
380 | for id, list in self.opened_pages.iteritems(): |
---|
381 | #Don't return any panel is the exact same page is created |
---|
382 | |
---|
383 | if selected_page.id == id: |
---|
384 | del self.opened_pages[selected_page.id] |
---|
385 | break |
---|
386 | |
---|
387 | |
---|