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

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 f5348253 was 4bee68d, checked in by Jae Cho <jhjcho@…>, 14 years ago

partly fixed saving opening project/analysis file

  • Property mode set to 100644
File size: 15.1 KB
RevLine 
[e2f1023]1
2import numpy
3import string 
[d89f09b]4import wx
[1b1bbf9]5#from wx.lib.flatnotebook import FlatNotebook as nb
6from wx.aui import AuiNotebook as nb
[a93f525]7
[340c2b3]8from sans.guiframe.panel_base import PanelBase
[3cd5806]9from sans.guiframe.events import PanelOnFocusEvent
[6bbeacd4]10from sans.guiframe.events import StatusEvent
[66ff250]11
[cfc0913]12import basepage
[6bbeacd4]13import models
[330573d]14_BOX_WIDTH = 80
15
16
[1b1bbf9]17class FitPanel(nb, PanelBase):   
[925a30e]18
[d89f09b]19    """
[5062bbf]20    FitPanel class contains fields allowing to fit  models and  data
21   
22    :note: For Fit to be performed the user should check at least one parameter
[d89f09b]23        on fit Panel window.
24       
25    """
26    ## Internal name for the AUI manager
27    window_name = "Fit panel"
28    ## Title to appear on top of the window
29    window_caption = "Fit Panel "
[2139c3f]30    CENTER_PANE = True
[7437880]31   
[6bbeacd4]32    def __init__(self, parent, manager=None, *args, **kwargs):
[5062bbf]33        """
34        """
[1b1bbf9]35        nb.__init__(self, parent, -1,
[3244cbe1]36                    style= wx.aui.AUI_NB_WINDOWLIST_BUTTON|
37                    wx.aui.AUI_NB_DEFAULT_STYLE|
38                    wx.CLIP_CHILDREN)
[3cd5806]39        PanelBase.__init__(self, parent)
[1b1bbf9]40        #self.SetWindowStyleFlag(style=nb.FNB_FANCY_TABS)
[6bbeacd4]41        self._manager = manager
[340c2b3]42        self.parent = parent
43        self.event_owner = None
[d89f09b]44        #dictionary of miodel {model class name, model class}
[6bbeacd4]45        self.menu_mng = models.ModelManager()
[b2d9826]46        self.model_list_box = self.menu_mng.get_model_list()
[1b1bbf9]47        #pageClosedEvent = nb.EVT_FLATNOTEBOOK_PAGE_CLOSING
48        pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE
49       
50        self.Bind(pageClosedEvent, self.on_close_page)
[6bbeacd4]51         ## save the title of the last page tab added
[2f189dc]52        self.fit_page_name = {}
[cfc0913]53        ## list of existing fit page
[2f189dc]54        self.opened_pages = {}
55        #page of simultaneous fit
[ffa69b6]56        self.sim_page = None
[66ff250]57        self.fit_engine_type = "scipy"
[cfc0913]58        ## get the state of a page
59        self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate)
[330573d]60        self.Bind(basepage.EVT_PREVIOUS_STATE, self._onUndo)
[fe496eeb]61        self.Bind(basepage.EVT_NEXT_STATE, self._onRedo)
[90a7bbd]62        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.on_page_changing)
[1b1bbf9]63
[9b18735]64        #add default pages
65        self.add_default_pages()
[2296316]66
[4e78f56]67    def save_project(self, doc=None):
68        """
69        return an xml node containing state of the panel
70         that guiframe can write to file
71        """
72        for uid, page in self.opened_pages.iteritems():
73            data = page.get_data()
74            # state must be cloned
75            state = page.get_state().clone()
76            if data is not None:
77                new_doc = self._manager.state_reader.write_toXML(data, state)
78                if doc != None and hasattr(doc, "firstChild"):
79                    child = new_doc.firstChild.firstChild
80                    doc.firstChild.appendChild(child) 
81                else:
82                    doc = new_doc
83        return doc   
[6bbeacd4]84   
[66ff250]85    def _on_engine_change(self, name='scipy'):
86        """
87        """
88        for panel in self.opened_pages.values():
89            self.set_engine_helper(panel=panel, name=name)
90           
91    def set_engine_helper(self, panel, name='scipy'):
92        """
93        """
94        self.fit_engine_type = name
95        if panel != self.sim_page:
96            panel._on_engine_change(name=self.fit_engine_type)
97               
[b2d9826]98    def update_model_list(self):
99        """
100        """
[9466f2d6]101        temp = self.menu_mng.update()
102        if len(temp):
103            self.model_list_box = temp
104        return temp
[b2d9826]105       
106       
[66ff250]107    def get_page_by_id(self, uid): 
[6bbeacd4]108        """
109        """
[66ff250]110        if uid not in self.opened_pages:
111            msg = "Fitpanel cannot find ID: %s in self.opened_pages" % str(uid)
[6bbeacd4]112            raise ValueError, msg
113        else:
[66ff250]114            return self.opened_pages[uid]
[31b0c47]115       
[90a7bbd]116    def on_page_changing(self, event):
[6bbeacd4]117        """
118        """
[90a7bbd]119        pos = self.GetSelection()
120        if pos != -1:
121            selected_page = self.GetPage(pos)
122            wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page))
[6bbeacd4]123           
[3cd5806]124    def on_set_focus(self, event):
125        """
126        """
127        pos = self.GetSelection()
[90a7bbd]128        if pos != -1:
129            selected_page = self.GetPage(pos)
130            wx.PostEvent(self.parent, PanelOnFocusEvent(panel=selected_page))
[3cd5806]131       
[c8deee5]132    def get_data(self):
133        """
134        get the data in the current page
135        """
136        pos = self.GetSelection()
[90a7bbd]137        if pos != -1:
138            selected_page = self.GetPage(pos)
139            return selected_page.get_data()
[c8deee5]140   
[e88ebfd]141    def set_model_state(state):
142        """
143        receive a state to reset the model in the current page
144        """
145        pos = self.GetSelection()
146        if pos != -1:
147            selected_page = self.GetPage(pos)
148            selected_page.set_model_state(state)
149           
[c8deee5]150    def get_state(self):
151        """
152         return the state of the current selected page
153        """
154        pos = self.GetSelection()
[90a7bbd]155        if pos != -1:
156            selected_page = self.GetPage(pos)
157            return selected_page.get_state()
[c8deee5]158   
[9b18735]159    def add_default_pages(self):
160        """
161        Add default pages such as a hint page and an empty fit page
162        """
[2296316]163        pass
[9b18735]164        #add default page
[2296316]165        #from hint_fitpage import HintFitPage
166        #self.hint_page = HintFitPage(self)
167        #self.AddPage(self.hint_page,"Hint")
168        #self.hint_page.set_manager(self._manager)
[6bbeacd4]169 
[ac2dc0e]170    def close_all(self):
171        """
[8897d66]172        remove all pages, used when a svs file is opened
173        """
[90a7bbd]174       
[8897d66]175        #get number of pages
176        nop = self.GetPageCount()
177        #use while-loop, for-loop will not do the job well.
178        while (nop>0):
179            #delete the first page until no page exists
180            page = self.GetPage(0)
[90a7bbd]181            if self._manager.parent.panel_on_focus == page:
182                self._manager.parent.panel_on_focus = None
[8897d66]183            self._close_helper(selected_page=page)
184            self.DeletePage(0)
185            nop = nop - 1
[90a7bbd]186           
[ac2dc0e]187        ## save the title of the last page tab added
188        self.fit_page_name = {}
189        ## list of existing fit page
190        self.opened_pages = {} 
191         
[31b0c47]192    def set_state(self, state):
193        """
[5062bbf]194        Restore state of the panel
[31b0c47]195        """
196        page_is_opened = False
197        if state is not None:
[66ff250]198            for uid, panel in self.opened_pages.iteritems():
[31b0c47]199                #Don't return any panel is the exact same page is created
[4bee68d]200                if uid == panel.uid and panel.data == state.data:
[31b0c47]201                    # the page is still opened
202                    panel.reset_page(state=state)
[645f9b6]203                    panel.save_current_state() 
[31b0c47]204                    page_is_opened = True
205            if not page_is_opened:
[9f391af]206                panel = self._manager.add_fit_page(data=state.data)
[31b0c47]207                # add data associated to the page created
208                if panel is not None: 
[4bee68d]209                    self._manager.store_data(uid=panel.uid, 
210                                             data=state.data,
211                                             caption=panel.window_caption)
[31b0c47]212                    panel.reset_page(state=state)
[645f9b6]213                    panel.save_current_state()
[31b0c47]214                   
[90a7bbd]215    def clear_panel(self):
[9b18735]216        """
217        Clear and close all panels, used by guimanager
218        """
[90a7bbd]219       
220        #close all panels only when svs file opened
221        self.close_all()
222        self._manager.mypanels = []
223       
[9b18735]224                       
[ac2dc0e]225    def on_close_page(self, event=None):
[51d47b5]226        """
[5062bbf]227        close page and remove all references to the closed page
[51d47b5]228        """
[d361b462]229        nbr_page = self.GetPageCount()
[a93f525]230        if nbr_page == 1:
[ac2dc0e]231           
[d361b462]232            event.Veto()
233            return 
[51d47b5]234        selected_page = self.GetPage(self.GetSelection())
[2f189dc]235        self._close_helper(selected_page=selected_page)
[9853ad0]236       
[2f189dc]237    def close_page_with_data(self, deleted_data):
238        """
[5062bbf]239        close a fit page when its data is completely remove from the graph
[2f189dc]240        """
[784e2fa]241        if deleted_data is None:
242            return
[2f189dc]243        for index in range(self.GetPageCount()):
244            selected_page = self.GetPage(index) 
245            if hasattr(selected_page,"get_data"):
246                data = selected_page.get_data()
[ac2dc0e]247               
[784e2fa]248                if data is None:
249                    #the fitpanel exists and only the initial fit page is open
250                    #with no selected data
251                    return
[2f189dc]252                if data.name == deleted_data.name:
253                    self._close_helper(selected_page)
254                    self.DeletePage(index)
255                    break
[d89f09b]256       
257    def set_manager(self, manager):
258        """
[5062bbf]259        set panel manager
260       
261        :param manager: instance of plugin fitting
262       
[d89f09b]263        """
[c8deee5]264        self._manager = manager
[3cd5806]265        for pos in range(self.GetPageCount()):
266            page = self.GetPage(pos)
267            if page is not None:
268                page.set_manager(self._manager)
[a93f525]269
[ffa69b6]270    def set_model_list(self, dict):
[c77d859]271         """
[5062bbf]272         copy a dictionary of model into its own dictionary
273         
274         :param dict: dictionnary made of model name as key and model class
[c77d859]275             as value
276         """
277         self.model_list_box = dict
[51d47b5]278       
[c77d859]279    def get_current_page(self):
280        """
[5062bbf]281        :return: the current page selected
282       
[c77d859]283        """
[6bbeacd4]284        return self.GetPage(self.GetSelection())
[c77d859]285   
[51d47b5]286    def add_sim_page(self):
[925a30e]287        """
[5062bbf]288        Add the simultaneous fit page
[925a30e]289        """
[51d47b5]290        from simfitpage import SimultaneousFitPage
[c8deee5]291        page_finder= self._manager.get_page_finder()
[b28717b]292        self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1)
[66ff250]293        self.sim_page.uid = wx.NewId()
[6bbeacd4]294        self.AddPage(self.sim_page,"Simultaneous Fit", True)
[c8deee5]295        self.sim_page.set_manager(self._manager)
[51d47b5]296        return self.sim_page
[d89f09b]297       
[6bbeacd4]298 
[2f189dc]299    def add_empty_page(self):
[9853ad0]300        """
[5062bbf]301        add an empty page
[9853ad0]302        """
[2f189dc]303        from fitpage import FitPage
[6bbeacd4]304        panel = FitPage(parent=self)
[66ff250]305        panel.uid = wx.NewId()
[6bbeacd4]306        panel.populate_box(dict=self.model_list_box)
[c8deee5]307        panel.set_manager(self._manager)
[ae4ade7]308        caption = str(panel.window_name) + " " + str(self._manager.index_model)
309        self.AddPage(panel, caption, select=True)
[66ff250]310        self.opened_pages[panel.uid] = panel
311        self.set_engine_helper(panel=panel)
[2f189dc]312        return panel
313   
[8ee56a9]314    def delete_data(self, data):
315        """
316        Delete the given data
317        """
318        if data is None:
319            return None
[6bbeacd4]320    def set_data(self, data):
[2f189dc]321        """
[5062bbf]322        Add a fitting page on the notebook contained by fitpanel
323       
324        :param data: data to fit
325       
326        :return panel : page just added for further used. is used by fitting module
327       
[2f189dc]328        """
329        if data is None:
330            return None
[6bbeacd4]331        for page in self.opened_pages.values():
332            #check if the selected data existing in the fitpanel
333            pos = self.GetPageIndex(page)
334            if page.get_data() is None:
[a5701e6]335                enable2D = page.get_view_mode()
336                if (data.__class__.__name__ == "Data2D" and enable2D)\
337                or (data.__class__.__name__ == "Data1D" and not enable2D):
338                    page.set_data(data)
339                    self.SetPageText(pos, str(data.name))
340                    self.SetSelection(pos)
341                    return page
[ae4ade7]342               
[6bbeacd4]343            elif page.get_data().id == data.id:
344                msg = "Data already existing in the fitting panel"
345                wx.PostEvent(self._manager.parent, 
346                             StatusEvent(status=msg, info='warning')) 
347                self.SetSelection(pos)
348                return page
[9b18735]349       
[6bbeacd4]350        page = self.add_empty_page()
[9f391af]351        pos = self.GetPageIndex(page)
[6bbeacd4]352        page.set_data(data)
353        self.SetPageText(pos, str(data.name))
[66ff250]354        self.opened_pages[page.uid] = page
[ae4ade7]355       
[6bbeacd4]356        return page
357       
358    def _onGetstate(self, event):
[cfc0913]359        """
[5062bbf]360        copy the state of a page
[cfc0913]361        """
[6bbeacd4]362        page = event.page
[66ff250]363        if page.uid in self.fit_page_name:
364           self.fit_page_name[page.uid].appendItem(page.createMemento()) 
[cfc0913]365           
[4e78f56]366    def _onUndo(self, event):
[330573d]367        """
[5062bbf]368        return the previous state of a given page is available
[330573d]369        """
370        page = event.page
[66ff250]371        if page.uid in self.fit_page_name:
372            if self.fit_page_name[page.uid].getCurrentPosition()==0:
[330573d]373                state = None
374            else:
[66ff250]375                state = self.fit_page_name[page.uid].getPreviousItem()
[fe496eeb]376                page._redo.Enable(True)
[330573d]377            page.reset_page(state)
[fe496eeb]378       
[2f189dc]379    def _onRedo(self, event): 
[fe496eeb]380        """
[5062bbf]381        return the next state available
[fe496eeb]382        """       
383        page = event.page
[66ff250]384        if page.uid in self.fit_page_name:
385            length= len(self.fit_page_name[page.uid])
386            if self.fit_page_name[page.uid].getCurrentPosition()== length -1:
[fe496eeb]387                state = None
388                page._redo.Enable(False)
[3b9e023]389                page._redo.Enable(True)
[fe496eeb]390            else:
[66ff250]391                state =self.fit_page_name[page.uid].getNextItem()
[fe496eeb]392            page.reset_page(state) 
[2f189dc]393                 
394    def _close_helper(self, selected_page):
[3f1af74]395        """
[5062bbf]396        Delete the given page from the notebook
[3f1af74]397        """
[2f189dc]398        #remove hint page
[1b1bbf9]399        #if selected_page == self.hint_page:
400        #    return
[2f189dc]401        ## removing sim_page
402        if selected_page == self.sim_page :
[c8deee5]403            self._manager.sim_page=None 
[2f189dc]404            return
[b787e68c]405       
[2f189dc]406        ## closing other pages
407        state = selected_page.createMemento()
408        page_name = selected_page.window_name
[c8deee5]409        page_finder = self._manager.get_page_finder() 
[2f189dc]410        fitproblem = None
411        ## removing fit page
[c6036f5]412        data = selected_page.get_data()
413        #Don' t remove plot for 2D
414        flag = True
415        if data.__class__.__name__ == 'Data2D':
416            flag = False
[2f189dc]417        if selected_page in page_finder:
418            #Delete the name of the page into the list of open page
[66ff250]419            for uid, list in self.opened_pages.iteritems():
[2f189dc]420                #Don't return any panel is the exact same page is created
[6bbeacd4]421               
[66ff250]422                if flag and selected_page.uid == uid:
423                    self._manager.remove_plot(uid, theory=False)
[2f189dc]424                    break 
425            del page_finder[selected_page]
426        ##remove the check box link to the model name of this page (selected_page)
427        try:
428            self.sim_page.draw_page()
429        except:
430            ## that page is already deleted no need to remove check box on
431            ##non existing page
432            pass
433               
434        #Delete the name of the page into the list of open page
[66ff250]435        for uid, list in self.opened_pages.iteritems():
[2f189dc]436            #Don't return any panel is the exact same page is created
[6bbeacd4]437           
[66ff250]438            if selected_page.uid == uid:
439                del self.opened_pages[selected_page.uid]
[2f189dc]440                break 
[0aeabc6]441     
[b787e68c]442 
Note: See TracBrowser for help on using the repository browser.