1 | |
---|
2 | import numpy |
---|
3 | import string |
---|
4 | import wx |
---|
5 | import wx.aui |
---|
6 | |
---|
7 | import basepage |
---|
8 | |
---|
9 | _BOX_WIDTH = 80 |
---|
10 | |
---|
11 | |
---|
12 | class StateIterator(object): |
---|
13 | """ |
---|
14 | Contains all saved state of a given page. |
---|
15 | Provide position of the current state of a page, the first save state |
---|
16 | and the last state for a given page. |
---|
17 | Allow easy undo or redo for a given page |
---|
18 | """ |
---|
19 | def __init__(self): |
---|
20 | """ |
---|
21 | """ |
---|
22 | self._current=0 |
---|
23 | |
---|
24 | def __iter__(self): |
---|
25 | """ |
---|
26 | """ |
---|
27 | return self |
---|
28 | |
---|
29 | def first(self): |
---|
30 | """ |
---|
31 | """ |
---|
32 | self._current =0 |
---|
33 | return self._current |
---|
34 | |
---|
35 | def next(self, max ): |
---|
36 | """ |
---|
37 | """ |
---|
38 | if self._current < max: |
---|
39 | self._current += 1 |
---|
40 | return self._current |
---|
41 | |
---|
42 | def previous(self): |
---|
43 | """ |
---|
44 | """ |
---|
45 | if self._current > 0: |
---|
46 | self._current = self._current -1 |
---|
47 | return self._current |
---|
48 | |
---|
49 | def currentPosition(self): |
---|
50 | """ |
---|
51 | """ |
---|
52 | return self._current |
---|
53 | |
---|
54 | def setPosition(self, value): |
---|
55 | """ |
---|
56 | """ |
---|
57 | if value >=0: |
---|
58 | self._current = int(value) |
---|
59 | |
---|
60 | |
---|
61 | class ListOfState(list): |
---|
62 | """ |
---|
63 | """ |
---|
64 | def __init__(self, *args, **kw): |
---|
65 | list.__init__(self, *args, **kw) |
---|
66 | self.iterator = StateIterator() |
---|
67 | |
---|
68 | def appendItem(self, x): |
---|
69 | """ |
---|
70 | """ |
---|
71 | self.append(x) |
---|
72 | self.iterator.setPosition(value= len(self)-1) |
---|
73 | |
---|
74 | def removeItem(self, x): |
---|
75 | """ |
---|
76 | """ |
---|
77 | self.iterator.previous() |
---|
78 | self.remove(x) |
---|
79 | |
---|
80 | def getPreviousItem(self): |
---|
81 | """ |
---|
82 | """ |
---|
83 | position = self.iterator.previous() |
---|
84 | |
---|
85 | if position < 0: |
---|
86 | return None |
---|
87 | else: |
---|
88 | return self[position] |
---|
89 | |
---|
90 | def getNextItem(self): |
---|
91 | """ |
---|
92 | """ |
---|
93 | position = self.iterator.next(max= len(self)-1) |
---|
94 | if position >= len(self): |
---|
95 | return None |
---|
96 | else: |
---|
97 | return self[position] |
---|
98 | |
---|
99 | def getCurrentItem(self): |
---|
100 | """ |
---|
101 | """ |
---|
102 | postion = self.iterator.currentPosition() |
---|
103 | if postion >= 0 and position < len(self): |
---|
104 | return self[postion] |
---|
105 | else: |
---|
106 | return None |
---|
107 | |
---|
108 | def getCurrentPosition(self): |
---|
109 | """ |
---|
110 | """ |
---|
111 | return self.iterator.currentPosition() |
---|
112 | |
---|
113 | |
---|
114 | class PageInfo(object): |
---|
115 | """ |
---|
116 | this class contains the minimum numbers of data members |
---|
117 | a fitpage or model page need to be initialized. |
---|
118 | """ |
---|
119 | data = None |
---|
120 | model = None |
---|
121 | manager = None |
---|
122 | event_owner= None |
---|
123 | model_list_box = None |
---|
124 | name = None |
---|
125 | ## Internal name for the AUI manager |
---|
126 | window_name = "Page" |
---|
127 | ## Title to appear on top of the window |
---|
128 | window_caption = "Page" |
---|
129 | #type of page can be real data , theory 1D or therory2D |
---|
130 | type = "Data" |
---|
131 | def __init__(self, model=None, data=None, manager=None, |
---|
132 | event_owner=None, model_list_box=None, name=None): |
---|
133 | """ |
---|
134 | Initialize data members |
---|
135 | """ |
---|
136 | self.data = data |
---|
137 | self.model= model |
---|
138 | self.manager= manager |
---|
139 | self.event_owner= event_owner |
---|
140 | self.model_list_box = model_list_box |
---|
141 | self.name=None |
---|
142 | self.window_name = "Page" |
---|
143 | self.window_caption = "Page" |
---|
144 | self.type = "Data" |
---|
145 | |
---|
146 | class FitPanel(wx.aui.AuiNotebook): |
---|
147 | |
---|
148 | """ |
---|
149 | FitPanel class contains fields allowing to fit models and data |
---|
150 | |
---|
151 | :note: For Fit to be performed the user should check at least one parameter |
---|
152 | on fit Panel window. |
---|
153 | |
---|
154 | """ |
---|
155 | ## Internal name for the AUI manager |
---|
156 | window_name = "Fit panel" |
---|
157 | ## Title to appear on top of the window |
---|
158 | window_caption = "Fit Panel " |
---|
159 | CENTER_PANE = True |
---|
160 | |
---|
161 | def __init__(self, parent, *args, **kwargs): |
---|
162 | """ |
---|
163 | """ |
---|
164 | wx.aui.AuiNotebook.__init__(self, parent, -1, |
---|
165 | style= wx.aui.AUI_NB_WINDOWLIST_BUTTON| |
---|
166 | wx.aui.AUI_NB_DEFAULT_STYLE| |
---|
167 | wx.CLIP_CHILDREN) |
---|
168 | |
---|
169 | self.manager=None |
---|
170 | self.parent=parent |
---|
171 | self.event_owner=None |
---|
172 | |
---|
173 | pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE |
---|
174 | self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.on_close_page) |
---|
175 | |
---|
176 | #dictionary of miodel {model class name, model class} |
---|
177 | self.model_list_box = {} |
---|
178 | ## save the title of the last page tab added |
---|
179 | self.fit_page_name = {} |
---|
180 | ## list of existing fit page |
---|
181 | self.opened_pages = {} |
---|
182 | #page of simultaneous fit |
---|
183 | self.sim_page = None |
---|
184 | ## get the state of a page |
---|
185 | self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate) |
---|
186 | self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo) |
---|
187 | self.Bind(basepage.EVT_NEXT_STATE, self._onRedo) |
---|
188 | |
---|
189 | #add default page |
---|
190 | from hint_fitpage import HintFitPage |
---|
191 | self.hint_page = HintFitPage(self) |
---|
192 | self.AddPage(page=self.hint_page, caption="Hint") |
---|
193 | #Add the first fit page |
---|
194 | self.add_empty_page() |
---|
195 | |
---|
196 | # increment number for model name |
---|
197 | self.count=0 |
---|
198 | #updating the panel |
---|
199 | self.Update() |
---|
200 | self.Center() |
---|
201 | |
---|
202 | def set_state(self, state): |
---|
203 | """ |
---|
204 | Restore state of the panel |
---|
205 | """ |
---|
206 | page_is_opened = False |
---|
207 | if state is not None: |
---|
208 | page_info = self.get_page_info(data=state.data) |
---|
209 | for name, panel in self.opened_pages.values(): |
---|
210 | #Don't return any panel is the exact same page is created |
---|
211 | if name == page_info.window_name: |
---|
212 | # the page is still opened |
---|
213 | panel.reset_page(state=state) |
---|
214 | page_is_opened = True |
---|
215 | if not page_is_opened: |
---|
216 | panel = self.add_fit_page(data=state.data) |
---|
217 | # add data associated to the page created |
---|
218 | if panel is not None: |
---|
219 | self.manager.store_page(page=panel, data=state.data) |
---|
220 | panel.reset_page(state=state) |
---|
221 | |
---|
222 | def on_close_page(self, event): |
---|
223 | """ |
---|
224 | close page and remove all references to the closed page |
---|
225 | """ |
---|
226 | nbr_page = self.GetPageCount() |
---|
227 | if nbr_page == 1: |
---|
228 | event.Veto() |
---|
229 | return |
---|
230 | selected_page = self.GetPage(self.GetSelection()) |
---|
231 | self._close_helper(selected_page=selected_page) |
---|
232 | |
---|
233 | def close_page_with_data(self, deleted_data): |
---|
234 | """ |
---|
235 | close a fit page when its data is completely remove from the graph |
---|
236 | """ |
---|
237 | if deleted_data is None: |
---|
238 | return |
---|
239 | for index in range(self.GetPageCount()): |
---|
240 | selected_page = self.GetPage(index) |
---|
241 | if hasattr(selected_page,"get_data"): |
---|
242 | data = selected_page.get_data() |
---|
243 | if data is None: |
---|
244 | #the fitpanel exists and only the initial fit page is open |
---|
245 | #with no selected data |
---|
246 | return |
---|
247 | if data.name == deleted_data.name: |
---|
248 | self._close_helper(selected_page) |
---|
249 | self.DeletePage(index) |
---|
250 | break |
---|
251 | |
---|
252 | def set_manager(self, manager): |
---|
253 | """ |
---|
254 | set panel manager |
---|
255 | |
---|
256 | :param manager: instance of plugin fitting |
---|
257 | |
---|
258 | """ |
---|
259 | self.manager = manager |
---|
260 | |
---|
261 | |
---|
262 | def set_owner(self,owner): |
---|
263 | """ |
---|
264 | set and owner for fitpanel |
---|
265 | |
---|
266 | :param owner: the class responsible of plotting |
---|
267 | |
---|
268 | """ |
---|
269 | self.event_owner = owner |
---|
270 | |
---|
271 | def set_model_list(self, dict): |
---|
272 | """ |
---|
273 | copy a dictionary of model into its own dictionary |
---|
274 | |
---|
275 | :param dict: dictionnary made of model name as key and model class |
---|
276 | as value |
---|
277 | """ |
---|
278 | self.model_list_box = dict |
---|
279 | |
---|
280 | def get_current_page(self): |
---|
281 | """ |
---|
282 | :return: the current page selected |
---|
283 | |
---|
284 | """ |
---|
285 | return self.GetPage(self.GetSelection() ) |
---|
286 | |
---|
287 | def add_sim_page(self): |
---|
288 | """ |
---|
289 | Add the simultaneous fit page |
---|
290 | """ |
---|
291 | from simfitpage import SimultaneousFitPage |
---|
292 | page_finder= self.manager.get_page_finder() |
---|
293 | self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1) |
---|
294 | |
---|
295 | self.AddPage(self.sim_page,caption="Simultaneous Fit",select=True) |
---|
296 | self.sim_page.set_manager(self.manager) |
---|
297 | return self.sim_page |
---|
298 | |
---|
299 | def get_page_info(self, data=None): |
---|
300 | """ |
---|
301 | fill information required to add a page in the fit panel |
---|
302 | """ |
---|
303 | name = "Fit Page" |
---|
304 | type = 'empty' |
---|
305 | if data is not None: |
---|
306 | if data.is_data: |
---|
307 | name = data.name |
---|
308 | type = 'Data' |
---|
309 | else: |
---|
310 | if data.__class__.__name__ == "Data2D": |
---|
311 | name = 'Model 2D Fit' |
---|
312 | type = 'Theory2D' |
---|
313 | else: |
---|
314 | name = 'Model 1D Fit' |
---|
315 | type = 'Theory1D' |
---|
316 | page_info = PageInfo(data=data, name=name) |
---|
317 | page_info.event_owner = self.event_owner |
---|
318 | page_info.manager = self.manager |
---|
319 | page_info.window_name = name |
---|
320 | page_info.window_caption = name |
---|
321 | page_info.type = type |
---|
322 | return page_info |
---|
323 | |
---|
324 | def add_empty_page(self): |
---|
325 | """ |
---|
326 | add an empty page |
---|
327 | """ |
---|
328 | page_info = self.get_page_info() |
---|
329 | from fitpage import FitPage |
---|
330 | panel = FitPage(parent=self, page_info=page_info) |
---|
331 | self.AddPage(page=panel, caption=page_info.window_name, select=True) |
---|
332 | self.opened_pages[page_info.type] = [page_info.window_name, panel] |
---|
333 | return panel |
---|
334 | |
---|
335 | def add_page(self, page_info): |
---|
336 | """ |
---|
337 | add a new page |
---|
338 | """ |
---|
339 | from fitpage import FitPage |
---|
340 | panel = FitPage(parent=self, page_info=page_info) |
---|
341 | self.AddPage(page=panel, caption=page_info.window_name, select=True) |
---|
342 | index = self.GetPageIndex(panel) |
---|
343 | self.change_page_content(data=page_info.data, index=index) |
---|
344 | return panel |
---|
345 | |
---|
346 | def change_page_content(self, data, index): |
---|
347 | """ |
---|
348 | replace the contains of an existing page |
---|
349 | """ |
---|
350 | page_info = self.get_page_info(data=data) |
---|
351 | self.SetPageText(index, page_info.window_name) |
---|
352 | panel = self.GetPage(index) |
---|
353 | panel.set_data(data) |
---|
354 | if panel.model_list_box is None or len(panel.model_list_box) == 0: |
---|
355 | page_info.model_list_box = self.model_list_box.get_list() |
---|
356 | panel.populate_box(dict=page_info.model_list_box) |
---|
357 | panel.initialize_combox() |
---|
358 | panel.set_page_info(page_info=page_info) |
---|
359 | self.opened_pages[page_info.type] = [page_info.window_name, panel] |
---|
360 | return panel |
---|
361 | |
---|
362 | def replace_page(self, index, page_info, type): |
---|
363 | """ |
---|
364 | replace an existing page |
---|
365 | """ |
---|
366 | self.DeletePage(index) |
---|
367 | del self.opened_pages[type] |
---|
368 | return self.add_page(page_info=page_info) |
---|
369 | |
---|
370 | def add_fit_page(self, data, reset=False): |
---|
371 | """ |
---|
372 | Add a fitting page on the notebook contained by fitpanel |
---|
373 | |
---|
374 | :param data: data to fit |
---|
375 | |
---|
376 | :return panel : page just added for further used. is used by fitting module |
---|
377 | |
---|
378 | """ |
---|
379 | if data is None: |
---|
380 | return None |
---|
381 | page_info = self.get_page_info(data=data) |
---|
382 | type = page_info.type |
---|
383 | npages = len(self.opened_pages.keys()) |
---|
384 | #check if only and empty page is opened |
---|
385 | if len(self.opened_pages.keys()) > 0: |
---|
386 | first_page_type = self.opened_pages.keys()[0] |
---|
387 | if npages == 1 and first_page_type in ['empty']: |
---|
388 | #replace the first empty page |
---|
389 | name, panel = self.opened_pages[first_page_type] |
---|
390 | index = self.GetPageIndex(panel) |
---|
391 | panel = self.change_page_content(data=data, index=index) |
---|
392 | del self.opened_pages[first_page_type] |
---|
393 | return panel |
---|
394 | if type in self.opened_pages.keys(): |
---|
395 | #this type of page is already created but it is a theory |
---|
396 | # meaning the same page is just to fit different data |
---|
397 | if not type.lower() in ['data']: |
---|
398 | #delete the previous theory page and add a new one |
---|
399 | name, panel = self.opened_pages[type] |
---|
400 | #self.manager.reset_plot_panel(panel.get_data()) |
---|
401 | #delete the existing page and replace it |
---|
402 | index = self.GetPageIndex(panel) |
---|
403 | panel = self.replace_page(index=index, page_info=page_info, type=type) |
---|
404 | return panel |
---|
405 | else: |
---|
406 | for name, panel in self.opened_pages.values(): |
---|
407 | #Don't return any panel is the exact same page is created |
---|
408 | if name == page_info.window_name: |
---|
409 | return None |
---|
410 | else: |
---|
411 | panel = self.add_page(page_info=page_info) |
---|
412 | return panel |
---|
413 | else: |
---|
414 | #a new type of page is created |
---|
415 | panel = self.add_page(page_info=page_info) |
---|
416 | return panel |
---|
417 | |
---|
418 | def _onGetstate(self, event): |
---|
419 | """ |
---|
420 | copy the state of a page |
---|
421 | """ |
---|
422 | page= event.page |
---|
423 | if page.window_name in self.fit_page_name: |
---|
424 | self.fit_page_name[page.window_name].appendItem(page.createMemento()) |
---|
425 | |
---|
426 | def _onUndo(self, event ): |
---|
427 | """ |
---|
428 | return the previous state of a given page is available |
---|
429 | """ |
---|
430 | page = event.page |
---|
431 | if page.window_name in self.fit_page_name: |
---|
432 | if self.fit_page_name[page.window_name].getCurrentPosition()==0: |
---|
433 | state = None |
---|
434 | else: |
---|
435 | state = self.fit_page_name[page.window_name].getPreviousItem() |
---|
436 | page._redo.Enable(True) |
---|
437 | page.reset_page(state) |
---|
438 | |
---|
439 | def _onRedo(self, event): |
---|
440 | """ |
---|
441 | return the next state available |
---|
442 | """ |
---|
443 | page = event.page |
---|
444 | if page.window_name in self.fit_page_name: |
---|
445 | length= len(self.fit_page_name[page.window_name]) |
---|
446 | if self.fit_page_name[page.window_name].getCurrentPosition()== length -1: |
---|
447 | state = None |
---|
448 | page._redo.Enable(False) |
---|
449 | page._redo.Enable(True) |
---|
450 | else: |
---|
451 | state = self.fit_page_name[page.window_name].getNextItem() |
---|
452 | page.reset_page(state) |
---|
453 | |
---|
454 | def _close_helper(self, selected_page): |
---|
455 | """ |
---|
456 | Delete the given page from the notebook |
---|
457 | """ |
---|
458 | #remove hint page |
---|
459 | if selected_page == self.hint_page: |
---|
460 | return |
---|
461 | ## removing sim_page |
---|
462 | if selected_page == self.sim_page : |
---|
463 | self.manager.sim_page=None |
---|
464 | return |
---|
465 | |
---|
466 | ## closing other pages |
---|
467 | state = selected_page.createMemento() |
---|
468 | page_name = selected_page.window_name |
---|
469 | page_finder = self.manager.get_page_finder() |
---|
470 | fitproblem = None |
---|
471 | ## removing fit page |
---|
472 | data = selected_page.get_data() |
---|
473 | #Don' t remove plot for 2D |
---|
474 | flag = True |
---|
475 | if data.__class__.__name__ == 'Data2D': |
---|
476 | flag = False |
---|
477 | if selected_page in page_finder: |
---|
478 | #Delete the name of the page into the list of open page |
---|
479 | for type, list in self.opened_pages.iteritems(): |
---|
480 | #Don't return any panel is the exact same page is created |
---|
481 | name = str(list[0]) |
---|
482 | if flag and selected_page.window_name == name: |
---|
483 | if type.lower() in ['theory1d', 'theory2d']: |
---|
484 | self.manager.remove_plot(selected_page, theory=True) |
---|
485 | else: |
---|
486 | self.manager.remove_plot(selected_page, theory=False) |
---|
487 | break |
---|
488 | del page_finder[selected_page] |
---|
489 | ##remove the check box link to the model name of this page (selected_page) |
---|
490 | try: |
---|
491 | self.sim_page.draw_page() |
---|
492 | except: |
---|
493 | ## that page is already deleted no need to remove check box on |
---|
494 | ##non existing page |
---|
495 | pass |
---|
496 | |
---|
497 | #Delete the name of the page into the list of open page |
---|
498 | for type, list in self.opened_pages.iteritems(): |
---|
499 | #Don't return any panel is the exact same page is created |
---|
500 | name = str(list[0]) |
---|
501 | if selected_page.window_name == name: |
---|
502 | del self.opened_pages[type] |
---|
503 | break |
---|
504 | |
---|
505 | |
---|