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