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="Loaded Data") |
---|
193 | # increment number for model name |
---|
194 | self.count = 0 |
---|
195 | #updating the panel |
---|
196 | self.Update() |
---|
197 | self.Center() |
---|
198 | |
---|
199 | def set_data(self, list=[], state=None): |
---|
200 | """ |
---|
201 | Receive a list of data from data manager |
---|
202 | """ |
---|
203 | if len(list) ==0: |
---|
204 | return |
---|
205 | self.hint_page.set_data(list=list) |
---|
206 | |
---|
207 | def set_state(self, state): |
---|
208 | """ |
---|
209 | Restore state of the panel |
---|
210 | """ |
---|
211 | page_is_opened = False |
---|
212 | if state is not None: |
---|
213 | page_info = self.get_page_info(data=state.data) |
---|
214 | for name, panel, type in self.opened_pages.values(): |
---|
215 | #Don't return any panel is the exact same page is created |
---|
216 | if name == page_info.window_name: |
---|
217 | # the page is still opened |
---|
218 | panel.set_manager(self.manager) |
---|
219 | panel.set_owner(self.event_owner) |
---|
220 | if panel.model_list_box is None or len(panel.model_list_box) == 0: |
---|
221 | page_info.model_list_box = self.model_list_box.get_list() |
---|
222 | panel.populate_box(dict=page_info.model_list_box) |
---|
223 | panel.initialize_combox() |
---|
224 | panel.set_page_info(page_info=page_info) |
---|
225 | self.opened_pages[page_info.window_name] = [page_info.window_name, panel, page_info.type] |
---|
226 | if panel is not None: |
---|
227 | self.manager.store_page(page=panel, data=state.data) |
---|
228 | panel.reset_page(state=state) |
---|
229 | page_is_opened = True |
---|
230 | |
---|
231 | if not page_is_opened: |
---|
232 | panel = self.add_fit_page(data=state.data) |
---|
233 | # add data associated to the page created |
---|
234 | if panel is not None: |
---|
235 | self.manager.store_page(page=panel, data=state.data) |
---|
236 | panel.reset_page(state=state) |
---|
237 | |
---|
238 | def on_close_page(self, event): |
---|
239 | """ |
---|
240 | close page and remove all references to the closed page |
---|
241 | """ |
---|
242 | nbr_page = self.GetPageCount() |
---|
243 | pos = self.GetSelection() |
---|
244 | if nbr_page == 1 or self.GetPage(pos)== self.hint_page: |
---|
245 | event.Veto() |
---|
246 | return |
---|
247 | selected_page = self.GetPage(self.GetSelection()) |
---|
248 | self._close_helper(selected_page=selected_page) |
---|
249 | |
---|
250 | def close_page_with_data(self, deleted_data): |
---|
251 | """ |
---|
252 | close a fit page when its data is completely remove from the graph |
---|
253 | """ |
---|
254 | if deleted_data is None: |
---|
255 | return |
---|
256 | for index in range(self.GetPageCount()): |
---|
257 | selected_page = self.GetPage(index) |
---|
258 | if hasattr(selected_page,"get_data"): |
---|
259 | data = selected_page.get_data() |
---|
260 | if data is None: |
---|
261 | #the fitpanel exists and only the initial fit page is open |
---|
262 | #with no selected data |
---|
263 | return |
---|
264 | if data.name == deleted_data.name: |
---|
265 | self._close_helper(selected_page) |
---|
266 | self.DeletePage(index) |
---|
267 | break |
---|
268 | |
---|
269 | def set_manager(self, manager): |
---|
270 | """ |
---|
271 | set panel manager |
---|
272 | |
---|
273 | :param manager: instance of plugin fitting |
---|
274 | |
---|
275 | """ |
---|
276 | self.manager = manager |
---|
277 | |
---|
278 | |
---|
279 | def set_owner(self,owner): |
---|
280 | """ |
---|
281 | set and owner for fitpanel |
---|
282 | |
---|
283 | :param owner: the class responsible of plotting |
---|
284 | |
---|
285 | """ |
---|
286 | self.event_owner = owner |
---|
287 | |
---|
288 | def set_model_list(self, dict): |
---|
289 | """ |
---|
290 | copy a dictionary of model into its own dictionary |
---|
291 | |
---|
292 | :param dict: dictionnary made of model name as key and model class |
---|
293 | as value |
---|
294 | """ |
---|
295 | self.model_list_box = dict |
---|
296 | |
---|
297 | def get_current_page(self): |
---|
298 | """ |
---|
299 | :return: the current page selected |
---|
300 | |
---|
301 | """ |
---|
302 | return self.GetPage(self.GetSelection() ) |
---|
303 | |
---|
304 | def add_sim_page(self): |
---|
305 | """ |
---|
306 | Add the simultaneous fit page |
---|
307 | """ |
---|
308 | from simfitpage import SimultaneousFitPage |
---|
309 | page_finder= self.manager.get_page_finder() |
---|
310 | self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1) |
---|
311 | |
---|
312 | self.AddPage(self.sim_page,caption="Simultaneous Fit",select=True) |
---|
313 | self.sim_page.set_manager(self.manager) |
---|
314 | return self.sim_page |
---|
315 | |
---|
316 | def get_page_info(self, data=None): |
---|
317 | """ |
---|
318 | fill information required to add a page in the fit panel |
---|
319 | """ |
---|
320 | name = "Fit Page" |
---|
321 | type = 'empty' |
---|
322 | if data is not None and hasattr(data, "is_data"): |
---|
323 | if data.is_data: |
---|
324 | name = data.name |
---|
325 | type = 'Data' |
---|
326 | else: |
---|
327 | if data.__class__.__name__ == "Data2D": |
---|
328 | name = 'Model 2D Fit' |
---|
329 | type = 'Theory2D' |
---|
330 | else: |
---|
331 | name = 'Model 1D Fit' |
---|
332 | type = 'Theory1D' |
---|
333 | page_info = PageInfo(data=data, name=name) |
---|
334 | page_info.event_owner = self.event_owner |
---|
335 | page_info.manager = self.manager |
---|
336 | page_info.window_name = name |
---|
337 | page_info.window_caption = name |
---|
338 | page_info.type = type |
---|
339 | return page_info |
---|
340 | |
---|
341 | def add_empty_page(self): |
---|
342 | """ |
---|
343 | add an empty page |
---|
344 | """ |
---|
345 | page_info = self.get_page_info() |
---|
346 | from fitpage import FitPage |
---|
347 | panel = FitPage(parent=self, page_info=page_info) |
---|
348 | self.AddPage(page=panel, caption=page_info.window_name, select=True) |
---|
349 | self.opened_pages[page_info.window_name] = [page_info.window_name, panel, page_info.type] |
---|
350 | return panel |
---|
351 | |
---|
352 | def add_page(self, page_info): |
---|
353 | """ |
---|
354 | add a new page |
---|
355 | """ |
---|
356 | from fitpage import FitPage |
---|
357 | panel = FitPage(parent=self, page_info=page_info) |
---|
358 | self.AddPage(page=panel, caption=page_info.window_name, select=True) |
---|
359 | index = self.GetPageIndex(panel) |
---|
360 | self.change_page_content(data=page_info.data, index=index) |
---|
361 | return panel |
---|
362 | |
---|
363 | def change_page_content(self, data, index): |
---|
364 | """ |
---|
365 | replace the contains of an existing page |
---|
366 | """ |
---|
367 | page_info = self.get_page_info(data=data) |
---|
368 | self.SetPageText(index, page_info.window_name) |
---|
369 | panel = self.GetPage(index) |
---|
370 | panel.set_data(data) |
---|
371 | if panel.model_list_box is None or len(panel.model_list_box) == 0: |
---|
372 | page_info.model_list_box = self.model_list_box.get_list() |
---|
373 | panel.populate_box(dict=page_info.model_list_box) |
---|
374 | panel.initialize_combox() |
---|
375 | panel.set_page_info(page_info=page_info) |
---|
376 | self.opened_pages[page_info.window_name] = [page_info.window_name, panel, page_info.type] |
---|
377 | return panel |
---|
378 | |
---|
379 | def replace_page(self, index, page_info, type): |
---|
380 | """ |
---|
381 | replace an existing page |
---|
382 | """ |
---|
383 | self.DeletePage(index) |
---|
384 | del self.opened_pages[page_info.window_name] |
---|
385 | return self.add_page(page_info=page_info) |
---|
386 | |
---|
387 | def add_fit_page(self, data, reset=False): |
---|
388 | """ |
---|
389 | Add a fitting page on the notebook contained by fitpanel |
---|
390 | |
---|
391 | :param data: data to fit |
---|
392 | |
---|
393 | :return panel : page just added for further used. is used by fitting module |
---|
394 | |
---|
395 | """ |
---|
396 | if data is None: |
---|
397 | return None |
---|
398 | page_info = self.get_page_info(data=data) |
---|
399 | type = page_info.type |
---|
400 | npages = len(self.opened_pages.keys()) |
---|
401 | if npages == 0: |
---|
402 | # create new type of page |
---|
403 | panel = self.add_page(page_info=page_info) |
---|
404 | return panel |
---|
405 | else: |
---|
406 | if page_info.window_name not in self.opened_pages.keys() and \ |
---|
407 | page_info.type.lower() in ['data']: |
---|
408 | #create new data type page |
---|
409 | panel = self.add_page(page_info=page_info) |
---|
410 | return panel |
---|
411 | elif page_info.type.lower() not in ['data']: |
---|
412 | #create new theory page |
---|
413 | for open_tab_caption, value in self.opened_pages.iteritems(): |
---|
414 | open_tab = value[1] |
---|
415 | open_type = value[2] |
---|
416 | if page_info.type == open_type: |
---|
417 | #replace theory fit tab |
---|
418 | existing_value = self.opened_pages[open_tab_caption] |
---|
419 | panel = existing_value[1] |
---|
420 | #delete the existing page and replace it |
---|
421 | index = self.GetPageIndex(panel) |
---|
422 | panel = self.replace_page(index=index, page_info=page_info, |
---|
423 | type=type) |
---|
424 | return panel |
---|
425 | panel = self.add_page(page_info=page_info) |
---|
426 | return panel |
---|
427 | else: |
---|
428 | _, panel, _ = self.opened_pages[page_info.window_name] |
---|
429 | index = self.GetPageIndex(panel) |
---|
430 | self.SetSelection(index) |
---|
431 | #page already existing |
---|
432 | return None |
---|
433 | |
---|
434 | def _onGetstate(self, event): |
---|
435 | """ |
---|
436 | copy the state of a page |
---|
437 | """ |
---|
438 | page= event.page |
---|
439 | if page.window_name in self.fit_page_name: |
---|
440 | self.fit_page_name[page.window_name].appendItem(page.createMemento()) |
---|
441 | |
---|
442 | def _onUndo(self, event ): |
---|
443 | """ |
---|
444 | return the previous state of a given page is available |
---|
445 | """ |
---|
446 | page = event.page |
---|
447 | if page.window_name in self.fit_page_name: |
---|
448 | if self.fit_page_name[page.window_name].getCurrentPosition()==0: |
---|
449 | state = None |
---|
450 | else: |
---|
451 | state = self.fit_page_name[page.window_name].getPreviousItem() |
---|
452 | page._redo.Enable(True) |
---|
453 | page.reset_page(state) |
---|
454 | |
---|
455 | def _onRedo(self, event): |
---|
456 | """ |
---|
457 | return the next state available |
---|
458 | """ |
---|
459 | page = event.page |
---|
460 | if page.window_name in self.fit_page_name: |
---|
461 | length= len(self.fit_page_name[page.window_name]) |
---|
462 | if self.fit_page_name[page.window_name].getCurrentPosition()== length -1: |
---|
463 | state = None |
---|
464 | page._redo.Enable(False) |
---|
465 | page._redo.Enable(True) |
---|
466 | else: |
---|
467 | state = self.fit_page_name[page.window_name].getNextItem() |
---|
468 | page.reset_page(state) |
---|
469 | |
---|
470 | def _close_helper(self, selected_page): |
---|
471 | """ |
---|
472 | Delete the given page from the notebook |
---|
473 | """ |
---|
474 | #remove hint page |
---|
475 | if selected_page == self.hint_page: |
---|
476 | return |
---|
477 | ## removing sim_page |
---|
478 | if selected_page == self.sim_page : |
---|
479 | self.manager.sim_page=None |
---|
480 | return |
---|
481 | |
---|
482 | ## closing other pages |
---|
483 | state = selected_page.createMemento() |
---|
484 | page_name = selected_page.window_name |
---|
485 | page_finder = self.manager.get_page_finder() |
---|
486 | fitproblem = None |
---|
487 | ## removing fit page |
---|
488 | data = selected_page.get_data() |
---|
489 | #Don' t remove plot for 2D |
---|
490 | flag = True |
---|
491 | if data.__class__.__name__ == 'Data2D': |
---|
492 | flag = False |
---|
493 | if selected_page in page_finder: |
---|
494 | #Delete the name of the page into the list of open page |
---|
495 | for type, list in self.opened_pages.iteritems(): |
---|
496 | #Don't return any panel is the exact same page is created |
---|
497 | name = str(list[0]) |
---|
498 | if flag and selected_page.window_name == name: |
---|
499 | if type.lower() in ['theory1d', 'theory2d']: |
---|
500 | self.manager.remove_plot(selected_page, theory=True) |
---|
501 | else: |
---|
502 | self.manager.remove_plot(selected_page, theory=False) |
---|
503 | break |
---|
504 | del page_finder[selected_page] |
---|
505 | ##remove the check box link to the model name of this page (selected_page) |
---|
506 | try: |
---|
507 | self.sim_page.draw_page() |
---|
508 | except: |
---|
509 | ## that page is already deleted no need to remove check box on |
---|
510 | ##non existing page |
---|
511 | pass |
---|
512 | |
---|
513 | #Delete the name of the page into the list of open page |
---|
514 | for type, list in self.opened_pages.iteritems(): |
---|
515 | #Don't return any panel is the exact same page is created |
---|
516 | name = str(list[0]) |
---|
517 | if selected_page.window_name == name: |
---|
518 | del self.opened_pages[type] |
---|
519 | break |
---|
520 | |
---|
521 | |
---|