source: sasview/sansview/perspectives/fitting/fitpanel.py @ 60fff67

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 60fff67 was f22e626, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on remove data

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