source: sasview/sansview/perspectives/fitting/fitpanel.py @ cf9b6950

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since cf9b6950 was 90a7bbd, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working save state

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