source: sasview/sansview/perspectives/fitting/fitpanel.py @ 848a2ef

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 848a2ef was 848a2ef, checked in by Gervaise Alina <gervyh@…>, 15 years ago

automatic clear of slicer when panel closed

  • Property mode set to 100644
File size: 13.0 KB
Line 
1import wx
2import wx.aui
3import wx.lib
4import numpy
5import string ,re
6#import models
7_BOX_WIDTH = 80
8import basepage
9
10class PageInfo(object):
11    """
12        this class contains the minimum numbers of data members
13        a fitpage or model page need to be initialized.
14    """
15    data = None
16    model= None
17    manager= None
18    event_owner= None
19    model_list_box = None
20    name=None
21     ## Internal name for the AUI manager
22    window_name = "Page"
23    ## Title to appear on top of the window
24    window_caption = "Page"
25   
26    def __init__(self, model=None,data=None, manager=None,
27                  event_owner=None,model_list_box=None , name=None):
28        """
29            Initialize data members
30        """
31        self.data = data
32        self.model= model
33        self.manager= manager
34        self.event_owner= event_owner
35        self.model_list_box = model_list_box
36        self.name=None
37        self.window_name = "Page"
38        self.window_caption = "Page"
39   
40class FitPanel(wx.aui.AuiNotebook):   
41
42    """
43        FitPanel class contains fields allowing to fit  models and  data
44        @note: For Fit to be performed the user should check at least one parameter
45        on fit Panel window.
46       
47    """
48    ## Internal name for the AUI manager
49    window_name = "Fit panel"
50    ## Title to appear on top of the window
51    window_caption = "Fit Panel "
52    CENTER_PANE = True
53    def __init__(self, parent, *args, **kwargs):
54        wx.aui.AuiNotebook.__init__(self,parent,-1,
55                    style= wx.aui.AUI_NB_WINDOWLIST_BUTTON|wx.aui.AUI_NB_DEFAULT_STYLE|wx.CLIP_CHILDREN  )
56   
57        self.manager=None
58        self.parent=parent
59        self.event_owner=None
60       
61        pageClosedEvent = wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE
62        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.onClosePage)
63        ##Creating the default page --welcomed page
64        self.about_page=None
65        from welcome_panel import PanelAbout
66        self.about_page = PanelAbout(self, -1)
67        self.AddPage(self.about_page,"Welcome!")
68     
69       
70        #dictionary of miodel {model class name, model class}
71        self.model_list_box={}
72        ##dictionary of page info
73        self.page_info_dict={}
74        ## save the title of the last page tab added
75        self.fit_page_name={}
76        ## list of existing fit page
77        self.list_fitpage_name=[]
78   
79        #model page info
80        self.model_page_number=None
81        ## fit page number for model plot
82        self.fit_page1D_number=None
83        self.fit_page2D_number=None
84        self.model_page=None
85        self.sim_page=None
86        ## get the state of a page
87        self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate)
88        # increment number for model name
89        self.count=0
90        #updating the panel
91        self.Update()
92        self.Center()
93       
94       
95    def onClosePage(self, event):
96        """
97             close page and remove all references to the closed page
98        """
99        selected_page = self.GetPage(self.GetSelection())
100        ## removing about page
101        if selected_page==self.about_page:
102            self.about_page=None
103            return 
104        ## removing sim_page
105        if selected_page == self.sim_page:
106            self.manager.sim_page=None 
107            return
108       
109        ## closing other pages
110        state = selected_page.createMemento()
111        page_name = selected_page.window_name
112        page_finder = self.manager.get_page_finder() 
113        fitproblem = None
114        ## removing model page
115        if selected_page == self.model_page:
116            fitproblem = selected_page.model.clone()
117            self.model_page = None
118            self.count =0
119            ## page on menu
120            self.manager._add_page_onmenu(page_name, fitproblem)
121        else:
122            if selected_page in page_finder:
123       
124                fitproblem= page_finder[selected_page].clone()
125                if self.GetPageIndex(selected_page)==self.fit_page1D_number:
126                    self.fit_page1D_number=None
127                if self.GetPageIndex(selected_page)==self.fit_page2D_number:
128                    self.fit_page2D_number=None
129                ## page on menu
130                self.manager._add_page_onmenu(page_name, fitproblem)
131                del page_finder[selected_page]
132            ##remove the check box link to the model name of this page (selected_page)
133            try:
134                self.sim_page.draw_page()
135            except:
136                ## that page is already deleted no need to remove check box on
137                ##non existing page
138                pass
139               
140        #Delete the name of the page into the list of open page
141        if selected_page.window_name in self.list_fitpage_name:
142            self.list_fitpage_name.remove(selected_page.window_name)
143           
144       
145    def set_manager(self, manager):
146        """
147             set panel manager
148             @param manager: instance of plugin fitting
149        """
150        self.manager = manager
151
152       
153    def set_owner(self,owner):
154        """
155            set and owner for fitpanel
156            @param owner: the class responsible of plotting
157        """
158        self.event_owner = owner
159   
160    def set_model_list(self,dict):
161         """
162             copy a dictionary of model into its own dictionary
163             @param dict: dictionnary made of model name as key and model class
164             as value
165         """
166         self.model_list_box = dict
167       
168 
169    def get_current_page(self):
170        """
171            @return the current page selected
172        """
173        return self.GetPage(self.GetSelection() )
174   
175    def add_sim_page(self):
176        """
177            Add the simultaneous fit page
178        """
179        from simfitpage import SimultaneousFitPage
180        page_finder= self.manager.get_page_finder()
181        self.sim_page = SimultaneousFitPage(self,page_finder=page_finder, id=-1)
182       
183        self.AddPage(self.sim_page,caption="Simultaneous Fit",select=True)
184        self.sim_page.set_manager(self.manager)
185        return self.sim_page
186       
187    def add_fit_page( self,data, reset=False ):
188        """
189            Add a fitting page on the notebook contained by fitpanel
190            @param data: data to fit
191            @return panel : page just added for further used. is used by fitting module
192        """     
193        if data.is_data:
194            name = data.name
195        else:
196            if data.__class__.__name__=="Data2D":
197                name = 'Model 2D Fit'
198            else:
199                name = 'Model 1D Fit'
200        if not name in self.list_fitpage_name:
201            myinfo = PageInfo( data=data, name=name )
202            myinfo.model_list_box = self.model_list_box.get_list()
203            myinfo.event_owner = self.event_owner
204            myinfo.manager = self.manager
205            myinfo.window_name = name
206            myinfo.window_caption = name
207       
208            #if not name in self.fit_page_name :
209            from fitpage import FitPage
210            panel = FitPage(parent= self, page_info=myinfo)
211           
212            self.AddPage(page=panel, caption=name, select=True)
213            if name == 'Model 1D Fit':
214                self.fit_page1D_number= self.GetPageIndex(panel)
215            if name =='Model 2D Fit':
216                self.fit_page2D_number= self.GetPageIndex(panel)
217               
218            self.list_fitpage_name.append(name)
219            if reset:
220                if name in self.fit_page_name.keys():
221                    memento= self.fit_page_name[name][0]
222                    panel.reset_page(memento)
223            else:
224                self.fit_page_name[name]=[]
225                self.fit_page_name[name].insert(0,panel.createMemento())
226            #GetPage(self, page_idx)
227            return panel
228        elif name =='Model 1D Fit':
229            if self.fit_page1D_number!=None:
230                panel =self.GetPage(self.fit_page1D_number) 
231                self.fit_page_name[name]=[]
232                self.fit_page_name[name].insert(0,panel.createMemento())
233                return panel
234            return None
235        elif name =='Model 2D Fit':
236            if self.fit_page2D_number!=None:
237                panel =self.GetPage(self.fit_page2D_number) 
238                self.fit_page_name[name]=[]
239                self.fit_page_name[name].insert(0,panel.createMemento())
240                return panel
241            return None
242        return None
243       
244   
245    def add_model_page(self,model,page_title="Model", qmin=0.0001, qmax=0.13,
246                        npts=50, topmenu=False, reset=False):
247        """
248            Add a model page only one  to display any model selected from the menu or the page combo box.
249            when this page is closed than the user will be able to open a new one
250           
251            @param model: the model for which paramters will be changed
252            @param page_title: the name of the page
253            @param page_info: contains info about the state of the page
254            @param qmin: mimimum Q
255            @param qmax: maximum Q
256            @param npts: number of Q points
257        """
258        if topmenu==True:
259            ##first time to open model page
260            if self.count==0 :
261                #if not page_title in self.list_fitpage_name :
262                self._help_add_model_page(model=model, page_title=page_title,
263                                qmin=qmin, qmax=qmax, npts=npts, reset=reset)
264                self.count +=1
265            else:
266                self.model_page.select_model(model)
267                self.fit_page_name[page_title].insert(0,self.model_page.createMemento())
268     
269     
270     
271    def _close_fitpage(self,data):
272        """
273            close a fit page when its data is completely remove from the graph
274        """
275        name = data.name
276        for index in range(self.GetPageCount()):
277            if self.GetPageText(index)== name:
278                selected_page = self.GetPage(index) 
279   
280                if index ==self.fit_page1D_number:
281                    self.fit_page1D_number=None
282                if index ==self.fit_page2D_number:
283                    self.fit_page2D_number=None
284                if selected_page in self.manager.page_finder:
285                    del self.manager.page_finder[selected_page]
286                ##remove the check box link to the model name of this page (selected_page)
287                try:
288                    self.sim_page.draw_page()
289                except:
290                    ## that page is already deleted no need to remove check box on
291                    ##non existing page
292                    pass
293               
294                #Delete the name of the page into the list of open page
295                if selected_page.window_name in self.list_fitpage_name:
296                    self.list_fitpage_name.remove(selected_page.window_name)
297                self.DeletePage(index)
298                break
299       
300       
301    def  _onGetstate(self, event):
302        """
303            copy the state of a page
304        """
305        page= event.page
306        if page.window_name in self.fit_page_name:
307            self.fit_page_name[page.window_name].insert(0,page.createMemento()) 
308           
309               
310    def _help_add_model_page(self,model,page_title="Model", qmin=0.0001, 
311                             qmax=0.13, npts=50,reset= False):
312        """
313            #TODO: fill in description
314           
315            @param qmin: mimimum Q
316            @param qmax: maximum Q
317            @param npts: number of Q points
318        """
319        ## creating object that contaning info about model
320        myinfo = PageInfo(model= model ,name= page_title)
321        myinfo.model_list_box = self.model_list_box.get_list()
322        myinfo.event_owner = self.event_owner
323        myinfo.manager = self.manager
324        myinfo.window_name = page_title
325        myinfo.window_caption = page_title
326     
327        from modelpage import ModelPage
328        panel = ModelPage(self,myinfo)
329       
330        self.AddPage(page=panel, caption=page_title, select=True)
331
332        self.model_page_number=self.GetSelection()
333        self.model_page=self.GetPage(self.GetSelection())
334        # Set the range used to plot models
335        self.model_page.set_range(qmin, qmax, npts)
336        ##  resetting page
337        if reset:
338            if page_title in self.fit_page_name.keys():
339
340                memento= self.fit_page_name[page_title][0]
341                print ""
342                panel.reset_page(memento)
343        else:
344            self.fit_page_name[page_title]=[]
345            self.fit_page_name[page_title].insert(0,panel.createMemento())
346         
347        # We just created a model page, we are ready to plot the model
348        #self.manager.draw_model(model, model.name)
349        #FOR PLUGIN  for some reason model.name is = BASEcomponent
350        self.manager.draw_model(model, qmin=qmin, qmax=qmax)
351 
352 
Note: See TracBrowser for help on using the repository browser.