source: sasview/fittingview/src/sans/perspectives/fitting/simfitpage.py @ dcbd084f

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 dcbd084f was 81a7b6c, checked in by Jae Cho <jhjcho@…>, 13 years ago

simpage update on delete data

  • Property mode set to 100644
File size: 36.5 KB
RevLine 
[d89f09b]1
[2140e68]2import sys,re,string, wx 
3import wx.lib.newevent 
[4ce74917]4from sans.guiframe.events import StatusEvent
[4bd492f]5from sans.guiframe.panel_base import PanelBase
6from wx.lib.scrolledpanel import ScrolledPanel
[1976004]7from sans.guiframe.events import PanelOnFocusEvent
[c99a6c5]8#Control panel width
[6e9976b]9if sys.platform.count("darwin")==0:
[c99a6c5]10    PANEL_WID = 420
[f1aa385]11    FONT_VARIANT = 0
[c99a6c5]12else:
13    PANEL_WID = 490
[f1aa385]14    FONT_VARIANT = 1
[8bd4dc0]15           
16def get_fittableParam( model):
[2140e68]17    """
[5062bbf]18    return list of fittable parameters name of a model
19   
20    :param model: the model used
21   
[2140e68]22    """
[8bd4dc0]23    fittable_param=[]
24    for item in model.getParamList():
25        if not item  in model.getDispParamList():
[fb59ed9]26            if not item in model.non_fittable:
27                fittable_param.append(item)
[8bd4dc0]28           
29    for item in model.fixed:
30        fittable_param.append(item)
[2140e68]31       
[8bd4dc0]32    return fittable_param
[2140e68]33
[4bd492f]34class SimultaneousFitPage(ScrolledPanel, PanelBase):
[d89f09b]35    """
[5062bbf]36    Simultaneous fitting panel
37    All that needs to be defined are the
38    two data members window_name and window_caption
[d89f09b]39    """
[925a30e]40    ## Internal name for the AUI manager
[d89f09b]41    window_name = "simultaneous Fit page"
42    ## Title to appear on top of the window
43    window_caption = "Simultaneous Fit Page"
44   
45   
[2140e68]46    def __init__(self, parent,page_finder ={}, *args, **kwargs):
[4bd492f]47        ScrolledPanel.__init__(self, parent,style= wx.FULL_REPAINT_ON_RESIZE )
48        PanelBase.__init__(self, parent)
[d89f09b]49        """
[5062bbf]50        Simultaneous page display
[d89f09b]51        """
[00daba9]52        self.SetupScrolling()
[f1aa385]53        ##Font size
54        self.SetWindowVariant(variant = FONT_VARIANT)
[922497f]55        self.uid = wx.NewId()
[d89f09b]56        self.parent = parent
[2140e68]57        ## store page_finder
[b28717b]58        self.page_finder = page_finder
[2140e68]59        ## list contaning info to set constraint
[6bbeacd4]60        ## look like self.constraint_dict[page_id]= page
[2140e68]61        self.constraint_dict={}
[1b14795]62        ## item list 
63        # self.constraints_list=[combobox1, combobox2,=,textcrtl, button ]
[2140e68]64        self.constraints_list=[]
65        ## list of current model
66        self.model_list=[]
67        ## selected mdoel to fit
68        self.model_toFit=[]
[b28717b]69        ## number of constraint
70        self.nb_constraint= 0
[53fc5ad9]71        self.model_cbox_left = None
72        self.model_cbox_right = None
[dc613d6]73        self.uid = wx.NewId()
[b28717b]74        ## draw page
[2140e68]75        self.define_page_structure()
[b28717b]76        self.draw_page()
[2140e68]77        self.set_layout()
[87e1d1a]78        self._set_save_flag(False)
[2140e68]79       
80    def define_page_structure(self):
81        """
[5062bbf]82        Create empty sizer for a panel
[2140e68]83        """
[d89f09b]84        self.vbox  = wx.BoxSizer(wx.VERTICAL)
[2140e68]85        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
86        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
[8bd4dc0]87        self.sizer3 = wx.BoxSizer(wx.VERTICAL)
[c99a6c5]88
89        self.sizer1.SetMinSize((PANEL_WID,-1))
90        self.sizer2.SetMinSize((PANEL_WID,-1))
91        self.sizer3.SetMinSize((PANEL_WID,-1))
[d89f09b]92        self.vbox.Add(self.sizer1)
93        self.vbox.Add(self.sizer2)
[8bd4dc0]94        self.vbox.Add(self.sizer3)
[51d47b5]95       
[2140e68]96    def set_scroll(self):
[5062bbf]97        """
98        """
[e1a97f8]99        #self.SetScrollbars(20,20,25,65)
[2140e68]100        self.Layout() 
101         
102    def set_layout(self):
103        """
[5062bbf]104        layout
[2140e68]105        """
[d89f09b]106        self.vbox.Layout()
107        self.vbox.Fit(self) 
108        self.SetSizer(self.vbox)
[2140e68]109        self.set_scroll()
[d89f09b]110        self.Centre()
111       
[8bd4dc0]112    def onRemove(self, event):
113        """
[5062bbf]114        Remove constraint fields
[8bd4dc0]115        """
[a911b48]116        if len(self.constraints_list)==1:
117            self.hide_constraint.SetValue(True)
118            self._hide_constraint()
119            return 
120        if len(self.constraints_list)==0:
[8bd4dc0]121            return 
122        for item in self.constraints_list:
123            length= len(item)
124            if event.GetId()==item[length-2].GetId():
125                sizer= item[length-1]
[bb7d8a4]126                sizer.Clear(True)
[8bd4dc0]127                self.sizer_constraints.Remove(sizer)
[1b14795]128                #self.SetScrollbars(20,20,25,65)
[8bd4dc0]129                self.constraints_list.remove(item)
130                self.nb_constraint -= 1
[1b14795]131                self.sizer2.Layout()
132                self.Layout()
[8bd4dc0]133                break
[1b14795]134        self._onAdd_constraint(None)   
135             
[922497f]136    def onFit(self, event):
[5062bbf]137        """
138        signal for fitting
139       
140        """
[2140e68]141        ## making sure all parameters content a constraint
142        ## validity of the constraint expression is own by fit engine
[1153d0e]143        if self.parent._manager._fit_engine != "park" and\
144                    self._manager.sim_page.uid == self.uid: 
145            msg = "The FitEnging will be set to 'Park' fit engine\n"
146            msg += " for the simultaneous fit..."
147            #wx.MessageBox(msg, 'Info')
148            wx.PostEvent(self._manager.parent, StatusEvent(status=\
149                            "Fitting: %s"%msg, info="info"))
[ac11e40]150        if self.show_constraint.GetValue():
[00daba9]151            if not self._set_constraint():
152                return
[2140e68]153        ## model was actually selected from this page to be fit
[3215d32]154        if len(self.model_toFit) >= 1 :
[66ff250]155            self.manager._reset_schedule_problem(value=0)
[6f023e8]156            for item in self.model_list:
157                if item[0].GetValue():
[c647377]158                    self.manager.schedule_for_fit(value=1, uid=item[2]) 
[1b14795]159            try:
[31469d50]160                if not self.manager.onFit(uid=self.uid):
161                    return
[1b14795]162            except:
163                msg= "Select at least one parameter to fit in the FitPages."
164                wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
[1b07935d]165        else:
[31469d50]166            msg= "Select at least one model check box to fit "
[1b14795]167            wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
[f343069]168           
[d89f09b]169    def set_manager(self, manager):
170        """
[5062bbf]171        set panel manager
172       
173        :param manager: instance of plugin fitting
174       
[d89f09b]175        """
176        self.manager = manager
[b28717b]177       
[2140e68]178    def check_all_model_name(self,event):
[d89f09b]179        """
[5062bbf]180        check all models names
[d89f09b]181        """
[6bbeacd4]182        self.model_toFit = [] 
183        if self.cb1.GetValue()== True:
[d89f09b]184            for item in self.model_list:
[6c08ba5]185                if item[0].IsEnabled():
186                    item[0].SetValue(True)
187                    self.model_toFit.append(item)
[2140e68]188               
189            ## constraint info
190            self._store_model()
191            ## display constraint fields
[53fc5ad9]192            if self.show_constraint.GetValue() and\
193                             len(self.constraints_list)==0:
[77a43fb]194                self._show_all_constraint() 
[2140e68]195                self._show_constraint()
[d89f09b]196        else:
197            for item in self.model_list:
198                item[0].SetValue(False) 
[948add7]199               
[d89f09b]200            self.model_toFit=[]
[2140e68]201            ##constraint info
202            self._hide_constraint()
[53fc5ad9]203           
204        self._update_easy_setup_cb()
[df566e8]205        self.Refresh()
[925a30e]206       
[2140e68]207    def check_model_name(self,event):
[d89f09b]208        """
[5062bbf]209        Save information related to checkbox and their states
[d89f09b]210        """
211        self.model_toFit=[]
[53fc5ad9]212        cbox = event.GetEventObject()
[d89f09b]213        for item in self.model_list:
214            if item[0].GetValue()==True:
215                self.model_toFit.append(item)
216            else:
217                if item in self.model_toFit:
218                    self.model_toFit.remove(item)
219                    self.cb1.SetValue(False)
[b28717b]220       
[2140e68]221        ## display constraint fields
[53fc5ad9]222        if len(self.model_toFit)>=1:
[2140e68]223            self._store_model()
[1b14795]224            if self.show_constraint.GetValue() and\
225                             len(self.constraints_list)==0:
[77a43fb]226                self._show_all_constraint() 
[2140e68]227                self._show_constraint()
[53fc5ad9]228
[284f6fe]229        elif len(self.model_toFit)< 1:
[b28717b]230            ##constraint info
[53fc5ad9]231            self._hide_constraint() 
232                       
233        self._update_easy_setup_cb()
[2140e68]234        ## set the value of the main check button         
[d89f09b]235        if len(self.model_list)==len(self.model_toFit):
236            self.cb1.SetValue(True)
[df566e8]237            self.Layout()
[b28717b]238            return
[d89f09b]239        else:
240            self.cb1.SetValue(False)
[df566e8]241            self.Layout()
[53fc5ad9]242           
243    def _update_easy_setup_cb(self):   
244        """
245        Update easy setup combobox on selecting a model
246        """
247        if self.model_cbox_left != None and self.model_cbox_right != None:
[9e9be13]248            try:
249                # when there is something
250                self.model_cbox_left.Clear()
251                self.model_cbox_right.Clear()
252                self.model_cbox.Clear()
253            except:
254                # when there is nothing
255                pass
[53fc5ad9]256            #for id, model in self.constraint_dict.iteritems():
257            for item in self.model_toFit:
258                model = item[3]
259                ## check if all parameters have been selected for constraint
260                ## then do not allow add constraint on parameters
261                if str(model.name) not in self.model_cbox_left.GetItems():
262                    self.model_cbox_left.Append(str(model.name), model)
263                if str(model.name) not in self.model_cbox_right.GetItems():
264                    self.model_cbox_right.Append(str(model.name), model)
[9e9be13]265                if str(model.name) not in self.model_cbox.GetItems():
266                    self.model_cbox.Append(str(model.name), model)
[53fc5ad9]267            self.model_cbox_left.SetSelection(0)
268            self.sizer2.Layout()
269            self.sizer3.Layout()
[948add7]270       
[b28717b]271    def draw_page(self):     
[2140e68]272        """
[5062bbf]273        Draw a sizer containing couples of data and model
[b28717b]274        """ 
[2140e68]275        self.model_list=[]
276        self.model_toFit=[]
[8bd4dc0]277        self.constraints_list=[]
[1d2782d]278        self.constraint_dict={}
279        self.nb_constraint= 0
[81a7b6c]280        self.model_cbox_left = None
281        self.model_cbox_right = None
[1d2782d]282       
[2140e68]283        if len(self.model_list)>0:
284            for item in self.model_list:
285                item[0].SetValue(False) 
[c647377]286                self.manager.schedule_for_fit(value=0, uid=item[2])
[2140e68]287               
[5062bbf]288        self.sizer1.Clear(True)   
[8bd4dc0]289        box_description= wx.StaticBox(self, -1,"Fit Combinations")
[2140e68]290        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
291        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
292        sizer_couples = wx.GridBagSizer(5,5)
293        #------------------------------------------------------
294        if len(self.page_finder)==0:
[2296316]295            msg = " No fit combinations are found! \n\n"
[1b14795]296            msg += " Please load data and set up "
297            msg += "at least two fit panels first..."
[2296316]298            sizer_title.Add(wx.StaticText(self, -1, msg))
[2140e68]299        else:
300            ## store model 
301            self._store_model()
302       
[8bd4dc0]303            self.cb1 = wx.CheckBox(self, -1,'Select all')
[2140e68]304            self.cb1.SetValue(False)
305            wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.check_all_model_name)
306           
[8bd4dc0]307            sizer_title.Add((10,10),0,
308                wx.TOP|wx.BOTTOM|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
309            sizer_title.Add(self.cb1,0,
310                wx.TOP|wx.BOTTOM|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE,border=5)
311           
312            ## draw list of model and data name
[2140e68]313            self._fill_sizer_model_list(sizer_couples)
314            ## draw the sizer containing constraint info
315            self._fill_sizer_constraint()
[8bd4dc0]316            ## draw fit button
317            self._fill_sizer_fit()
[2140e68]318        #--------------------------------------------------------
[8bd4dc0]319        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=5) 
[0a9871c]320        boxsizer1.Add(sizer_couples, 1, flag= wx.TOP|wx.BOTTOM,border=5)
[2140e68]321       
[0a9871c]322        self.sizer1.Add(boxsizer1, 1, wx.EXPAND | wx.ALL, 10)
[2140e68]323        self.sizer1.Layout()
[e1a97f8]324        #self.SetScrollbars(20,20,25,65)
[2140e68]325        self.AdjustScrollbars()
[0a9871c]326        self.Layout()
[2140e68]327       
328    def _store_model(self):
[d89f09b]329        """
[5062bbf]330         Store selected model
[d89f09b]331        """
[284f6fe]332        if len(self.model_toFit) < 1:
[2140e68]333            return
[1d2782d]334        for item in self.model_toFit:
335            model = item[3]
[6bbeacd4]336            page_id= item[2]
337            self.constraint_dict[page_id] = model
[2140e68]338                   
339    def _display_constraint(self, event):
340        """
[5062bbf]341        Show fields to add constraint
[2140e68]342        """
[284f6fe]343        if len(self.model_toFit)< 1:
[1aa6145]344            msg= "Select at least 1 model to add constraint "
[1b14795]345            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[2140e68]346            ## hide button
347            self._hide_constraint()
348            return
349        if self.show_constraint.GetValue():
[1b14795]350            self._show_all_constraint() 
[2140e68]351            self._show_constraint()
[e1a97f8]352            self.Layout()
[2140e68]353            return
354        else:
355           self._hide_constraint()
[e1a97f8]356           self.Layout()
[2140e68]357           return 
[1b14795]358       
359    def _show_all_constraint(self):
360        """
361        Show constraint fields
362        """
363        #for id, model in self.constraint_dict.iteritems():   
364        box_description= wx.StaticBox(self, -1,"Easy Setup ")
365        boxsizer = wx.StaticBoxSizer(box_description, wx.HORIZONTAL)     
366        sizer_constraint =  wx.BoxSizer(wx.HORIZONTAL|wx.LEFT|wx.RIGHT|wx.EXPAND)
367        self.model_cbox_left = wx.ComboBox(self, -1, style=wx.CB_READONLY)
368        self.model_cbox_left.Clear()
369        self.model_cbox_right = wx.ComboBox(self, -1, style=wx.CB_READONLY)
370        self.model_cbox_right.Clear()
371        wx.EVT_COMBOBOX(self.model_cbox_left, -1, self._on_select_modelcb)
372        wx.EVT_COMBOBOX(self.model_cbox_right, -1, self._on_select_modelcb)
373        egal_txt= wx.StaticText(self,-1," = ")
374        self.set_button =wx.Button(self, wx.NewId(),'Set All')
375        self.set_button.Bind(wx.EVT_BUTTON, self._on_set_all_equal,
376                                       id=self.set_button.GetId())
377        set_tip = "Add constraints for all the adjustable parameters "
378        set_tip += "(checked in FitPages) if exist."
379        self.set_button.SetToolTipString(set_tip)
380        self.set_button.Disable()
381       
382        for id, model in self.constraint_dict.iteritems():
383            ## check if all parameters have been selected for constraint
384            ## then do not allow add constraint on parameters
385            self.model_cbox_left.Append( str(model.name), model)
386        self.model_cbox_left.Select(0)
387        for id, model in self.constraint_dict.iteritems():
388            ## check if all parameters have been selected for constraint
389            ## then do not allow add constraint on parameters
390            self.model_cbox_right.Append( str(model.name), model)
391        boxsizer.Add(self.model_cbox_left, 
392                             flag= wx.RIGHT|wx.EXPAND, border=10)
393        boxsizer.Add(wx.StaticText(self, -1,".parameters"),
394                             flag= wx.RIGHT|wx.EXPAND, border=5)
395        boxsizer.Add(egal_txt, flag= wx.RIGHT|wx.EXPAND, border=5)
396        boxsizer.Add(self.model_cbox_right, 
397                             flag= wx.RIGHT|wx.EXPAND, border=10)
398        boxsizer.Add(wx.StaticText(self, -1,".parameters"),
399                             flag= wx.RIGHT|wx.EXPAND,border=5)
400        boxsizer.Add((20,-1))
401        boxsizer.Add(self.set_button, flag= wx.RIGHT|wx.EXPAND, border=5)
402        sizer_constraint.Add(boxsizer, flag= wx.RIGHT|wx.EXPAND, border=5)
403        self.sizer_all_constraints.Insert(before=0,
404                             item=sizer_constraint, 
405                             flag= wx.TOP|wx.BOTTOM|wx.EXPAND, border=5)
406
407        self.sizer_all_constraints.Layout()
408        self.sizer2.Layout()
[e1a97f8]409        #self.SetScrollbars(20,20,25,65)
[1b14795]410   
411    def _on_select_modelcb(self, event):
412        """
413        On select model left or right combobox
414        """
415        event.Skip()
416        flag = True
417        if self.model_cbox_left.GetValue().strip() == '':
418            flag = False
419        if self.model_cbox_right.GetValue().strip() == '':
420            flag = False
421        if self.model_cbox_left.GetValue() ==\
422                self.model_cbox_right.GetValue():
423            flag = False
424        self.set_button.Enable(flag)
425       
426    def _on_set_all_equal(self, event):
427        """
428        On set button
429        """
430        event.Skip()
431        length = len(self.constraints_list)
432        if length < 1:
433            return 
434        param_list = []
435        param_listB = []
436        selection = self.model_cbox_left.GetCurrentSelection()
437        model_left = self.model_cbox_left.GetValue()
438        model = self.model_cbox_left.GetClientData(selection)
439        selectionB = self.model_cbox_right.GetCurrentSelection()
440        model_right = self.model_cbox_right.GetValue()
441        modelB = self.model_cbox_right.GetClientData(selectionB)
442        for id, dic_model in self.constraint_dict.iteritems():
443            if model == dic_model:
444                param_list = self.page_finder[id].get_param2fit()
445            if modelB == dic_model:
446                param_listB = self.page_finder[id].get_param2fit()
447            if len(param_list) > 0 and len(param_listB) > 0:
448                break
449        #param_list= get_fittableParam(model)
450        #param_listB= get_fittableParam(modelB)
451        num_cbox = 0
452        has_param = False
453        for param in param_list:
454            num_cbox += 1
455            if param in param_listB:
456                self.model_cbox.SetStringSelection(model_left)
457                self._on_select_model(None)
458                self.param_cbox.Clear()
459                self.param_cbox.Append( str(param), model)
460                self.param_cbox.SetStringSelection( str(param))
461                self.ctl2.SetValue(str(model_right + "." + str(param)))
462                has_param = True
[00daba9]463                if num_cbox == (len(param_list) + 1):
[1b14795]464                    break
465                self._show_constraint()
466       
467        self.sizer_constraints.Layout()
468        self.sizer2.Layout()
[e1a97f8]469        self.SetScrollbars(20,20,25,65)
470        self.Layout()
[1b14795]471        if not has_param:
472            msg= " There is no adjustable parameter (checked to fit)"
473            msg += " either one of the models."
474            wx.PostEvent(self.parent.parent, StatusEvent(info="warning", 
475                                                         status= msg ))
476        else:
477            msg= " The constraints are added."
478            wx.PostEvent(self.parent.parent, StatusEvent(info="info", 
479                                                         status= msg ))     
[2140e68]480    def _show_constraint(self):
481        """
[5062bbf]482        Show constraint fields
[2140e68]483        """
[77e23a2]484        self.btAdd.Show(True)
[2140e68]485        if len(self.constraints_list)!= 0:
486            nb_fit_param = 0
[1b14795]487            for id, model in self.constraint_dict.iteritems():
[00daba9]488                nb_fit_param += len(self.page_finder[id].get_param2fit())
[2140e68]489            ##Don't add anymore
490            if len(self.constraints_list) == nb_fit_param:
[ac11e40]491                msg= "Cannot add another constraint .Maximum of number "
492                msg += "Parameters name reached %s"%str(nb_fit_param)
[1b14795]493                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[b28717b]494                self.sizer_constraints.Layout()
[ac11e40]495                self.sizer2.Layout()
[00daba9]496                #self.SetScrollbars(20,20,25,65)
[ac11e40]497                return
[284f6fe]498        if len(self.model_toFit) < 1 :
499            msg= "Select at least 1 model to add constraint "
[1b14795]500            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[b28717b]501            self.sizer_constraints.Layout()
[2140e68]502            self.sizer2.Layout()
[00daba9]503            #self.SetScrollbars(20,20,25,65)
[2140e68]504            return
505           
506        sizer_constraint =  wx.BoxSizer(wx.HORIZONTAL)
[8bd4dc0]507        model_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY)
[2140e68]508        model_cbox.Clear()
[1b14795]509        param_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY, size=(100,-1),)
[b28717b]510        param_cbox.Hide()
[8dfe0fd]511       
512        #This is for GetCLientData() _on_select_param: Was None return on MAC.
513        self.param_cbox = param_cbox
514       
[2140e68]515        wx.EVT_COMBOBOX(param_cbox,-1, self._on_select_param)
[1b14795]516        self.ctl2 = wx.TextCtrl(self, -1)
[77a43fb]517        egal_txt= wx.StaticText(self,-1, " = ")
518        self.btRemove = wx.Button(self,wx.NewId(),'Remove')
519        self.btRemove.Bind(wx.EVT_BUTTON, self.onRemove, 
520                                    id=self.btRemove.GetId())
521        self.btRemove.SetToolTipString("Remove constraint.")
522        self.btRemove.Hide()
523        if hasattr(self,"btAdd"):
524            self.btAdd.Hide()
[1b14795]525        for id, model in self.constraint_dict.iteritems():
[2140e68]526            ## check if all parameters have been selected for constraint
527            ## then do not allow add constraint on parameters
[ac11e40]528            model_cbox.Append( str(model.name), model)
[2140e68]529           
[8dfe0fd]530        #This is for GetCLientData() passing to self._on_select_param: Was None return on MAC.
531        self.model_cbox = model_cbox
[2140e68]532           
533        wx.EVT_COMBOBOX(model_cbox,-1, self._on_select_model)
[1b14795]534        sizer_constraint.Add((5,-1))
[b28717b]535        sizer_constraint.Add(model_cbox, flag= wx.RIGHT|wx.EXPAND,border=10)
536        sizer_constraint.Add(param_cbox, flag= wx.RIGHT|wx.EXPAND,border=5)
537        sizer_constraint.Add(egal_txt, flag= wx.RIGHT|wx.EXPAND,border=5)
[1b14795]538        sizer_constraint.Add(self.ctl2, flag= wx.RIGHT|wx.EXPAND,border=10)
[77a43fb]539        sizer_constraint.Add(self.btRemove, flag= wx.RIGHT|wx.EXPAND,border=10)
[b28717b]540     
541        self.sizer_constraints.Insert(before=self.nb_constraint,
[77a43fb]542                        item=sizer_constraint, flag= wx.TOP|wx.BOTTOM|wx.EXPAND,
543                        border=5)
[8bd4dc0]544        ##[combobox1, combobox2,=,textcrtl, remove button ]
[77a43fb]545        self.constraints_list.append([model_cbox, param_cbox, egal_txt, 
546                                    self.ctl2,self.btRemove,sizer_constraint])
[5062bbf]547   
[b28717b]548        self.nb_constraint += 1
549        self.sizer_constraints.Layout()
[2140e68]550        self.sizer2.Layout()
[00daba9]551        #self.SetScrollbars(20,20,25,65)
[2140e68]552       
553    def _hide_constraint(self): 
554        """
[5062bbf]555        hide buttons related constraint
[ac11e40]556        """ 
[6bbeacd4]557        for id in  self.page_finder.iterkeys():
558            self.page_finder[id].clear_model_param()
[b28717b]559               
[1b14795]560        self.nb_constraint = 0     
561        self.constraint_dict = {}
[69bee6d]562        if hasattr(self,"btAdd"):
563            self.btAdd.Hide()
[ac11e40]564        self._store_model()
[53fc5ad9]565        if self.model_cbox_left != None:
[81a7b6c]566            try:
567                self.model_cbox_left.Clear()
568            except:
569                pass
[53fc5ad9]570            self.model_cbox_left = None
571        if self.model_cbox_right != None:
[81a7b6c]572            try:
573                self.model_cbox_right.Clear()
574            except:
575                pass
[53fc5ad9]576            self.model_cbox_right = None
[1b14795]577        self.constraints_list = []   
578        self.sizer_all_constraints.Clear(True) 
579        self.sizer_all_constraints.Layout()         
[b28717b]580        self.sizer_constraints.Clear(True) 
581        self.sizer_constraints.Layout()   
[2140e68]582        self.sizer2.Layout()
[e1a97f8]583        #self.SetScrollbars(20,20,25,65)
584        #self.AdjustScrollbars()   
[5062bbf]585           
[2140e68]586    def _on_select_model(self, event):
587        """
[5062bbf]588        fill combox box with list of parameters
[2140e68]589        """
[53fc5ad9]590        param_list = []
[8dfe0fd]591        ##This way PC/MAC both work, instead of using event.GetClientData().
592        n = self.model_cbox.GetCurrentSelection()
593        model = self.model_cbox.GetClientData(n)
[1b14795]594        for id, dic_model in self.constraint_dict.iteritems():
595            if model == dic_model:
596                param_list = self.page_finder[id].get_param2fit()
[53fc5ad9]597                #break
[1b14795]598        #param_list= get_fittableParam(model)
[2140e68]599        length = len(self.constraints_list)
600        if length < 1:
601            return 
602        param_cbox = self.constraints_list[length-1][1]
603        param_cbox.Clear()
604        ## insert only fittable paramaters
605        for param in param_list:
[ac11e40]606            param_cbox.Append( str(param), model)
[53fc5ad9]607
[2140e68]608        param_cbox.Show(True)
[77a43fb]609        self.btRemove.Show(True)
610        self.btAdd.Show(True)
[2140e68]611        self.sizer2.Layout()
[e1a97f8]612        #self.SetScrollbars(20,20,25,65)
[2140e68]613       
614    def _on_select_param(self, event):
615        """
[5062bbf]616        Store the appropriate constraint in the page_finder
[2140e68]617        """
[8dfe0fd]618        ##This way PC/MAC both work, instead of using event.GetClientData().
619        n = self.param_cbox.GetCurrentSelection()
620        model = self.param_cbox.GetClientData(n)
[2140e68]621        param = event.GetString()
[ac11e40]622     
[2140e68]623        length = len(self.constraints_list)
624        if length < 1:
625            return 
626        egal_txt = self.constraints_list[length-1][2]
627        egal_txt.Show(True)       
628       
629        ctl2 = self.constraints_list[length-1][3]
630        ctl2.Show(True)
[6e9976b]631        #self.sizer2.Layout()
[e1a97f8]632        #self.SetScrollbars(20,20,25,65)
[2140e68]633       
634    def _onAdd_constraint(self, event): 
635        """
[5062bbf]636        Add another line for constraint
[2140e68]637        """
[8bd4dc0]638        if not self.show_constraint.GetValue():
639            msg= " Select Yes to add Constraint "
[1b14795]640            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[8bd4dc0]641            return 
[1b14795]642        ## check that a constraint is added
643        # before allow to add another constraint
[2140e68]644        for item in self.constraints_list:
645            model_cbox = item[0]
646            if model_cbox.GetString(0)=="":
647                msg= " Select a model Name! "
[1b14795]648                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[2140e68]649                return 
650            param_cbox = item[1]
651            if param_cbox.GetString(0)=="":
652                msg= " Select a parameter Name! "
[1b14795]653                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[2140e68]654                return 
655            ctl2 = item[3]
656            if ctl2.GetValue().lstrip().rstrip()=="":
[b28717b]657                model= param_cbox.GetClientData(param_cbox.GetCurrentSelection())
[1b14795]658                msg= " Enter a constraint for %s.%s! "%(model.name, 
659                                                        param_cbox.GetString(0))           
660                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[b28717b]661                return 
[2140e68]662        ## some model or parameters can be constrained
663        self._show_constraint()
664       
[8bd4dc0]665    def _fill_sizer_fit(self):
666        """
[5062bbf]667        Draw fit button
[8bd4dc0]668        """
669        self.sizer3.Clear(True)
670        box_description= wx.StaticBox(self, -1,"Fit ")
671        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
672        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
673         
[df566e8]674        self.btFit = wx.Button(self,wx.NewId(),'Fit', size=wx.DefaultSize)
[8bd4dc0]675        self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id= self.btFit.GetId())
676        self.btFit.SetToolTipString("Perform fit.")
[1aa6145]677        text  = "     Note: Park fitting engine will be used automatically. \n"
678        text += "     This page requires at least one FitPage with a data \n"
679        text += "       and a model set for fitting."
[284f6fe]680        #text+= "automatically for more than 2 combinations checked"
[8bd4dc0]681        text_hint = wx.StaticText(self,-1,text)
682       
683        sizer_button.Add(text_hint,  wx.RIGHT|wx.EXPAND, 10)
[df566e8]684        sizer_button.Add(self.btFit, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10)
[8bd4dc0]685       
686        boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM,border=10)
687        self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
688        self.sizer3.Layout()
[e1a97f8]689        #self.SetScrollbars(20,20,25,65)
[2140e68]690       
691    def _fill_sizer_constraint(self):
692        """
[5062bbf]693        Fill sizer containing constraint info
[2140e68]694        """
[5062bbf]695        msg = "Select at least 2 model to add constraint "
[1b14795]696        wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[2140e68]697       
698        self.sizer2.Clear(True)
699        box_description= wx.StaticBox(self, -1,"Fit Constraints")
700        boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL)
701        sizer_title = wx.BoxSizer(wx.HORIZONTAL)
[1b14795]702        self.sizer_all_constraints = wx.BoxSizer(wx.HORIZONTAL)
[b28717b]703        self.sizer_constraints = wx.BoxSizer(wx.VERTICAL)
[2140e68]704        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
705       
[1b14795]706        self.hide_constraint = wx.RadioButton(self, -1, 'No', (10, 10), 
707                                              style=wx.RB_GROUP)
[2140e68]708        self.show_constraint = wx.RadioButton(self, -1, 'Yes', (10, 30))
709        self.Bind( wx.EVT_RADIOBUTTON, self._display_constraint,
710                    id= self.hide_constraint.GetId() )
711        self.Bind(  wx.EVT_RADIOBUTTON, self._display_constraint,
712                         id= self.show_constraint.GetId()    )
[17c5868]713        self.hide_constraint.SetValue(True)
[2140e68]714        sizer_title.Add( wx.StaticText(self,-1," Model") )
715        sizer_title.Add(( 10,10) )
716        sizer_title.Add( wx.StaticText(self,-1," Parameter") )
717        sizer_title.Add(( 10,10) )
718        sizer_title.Add( wx.StaticText(self,-1," Add Constraint?") )
719        sizer_title.Add(( 10,10) )
720        sizer_title.Add( self.show_constraint )
721        sizer_title.Add( self.hide_constraint )
722        sizer_title.Add(( 10,10) )
[1b14795]723       
[2140e68]724        self.btAdd =wx.Button(self,wx.NewId(),'Add')
[1b14795]725        self.btAdd.Bind(wx.EVT_BUTTON, self._onAdd_constraint, 
726                        id=self.btAdd.GetId())
[2140e68]727        self.btAdd.SetToolTipString("Add another constraint?")
[77e23a2]728        self.btAdd.Hide()
[8bd4dc0]729     
[1b14795]730        text_hint = wx.StaticText(self, -1, 
731                                  "Example: [M0][paramter] = M1.parameter") 
[2140e68]732        sizer_button.Add(text_hint, 0 , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
733        sizer_button.Add(self.btAdd, 0, wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
734       
[8bd4dc0]735        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=10)
[1b14795]736        boxsizer1.Add(self.sizer_all_constraints, flag= wx.TOP|wx.BOTTOM, 
737                      border=10)
738        boxsizer1.Add(self.sizer_constraints, flag= wx.TOP|wx.BOTTOM, 
739                      border=10)
740        boxsizer1.Add(sizer_button, flag= wx.TOP|wx.BOTTOM, border=10)
[2140e68]741       
742        self.sizer2.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
743        self.sizer2.Layout()
[e1a97f8]744        #self.SetScrollbars(20,20,25,65)
[5062bbf]745   
[2140e68]746    def _set_constraint(self):
[d89f09b]747        """
[5062bbf]748        get values from the constrainst textcrtl ,parses them into model name
749        parameter name and parameters values.
[1b14795]750        store them in a list self.params .when when params is not empty
751        set_model uses it to reset the appropriate model
752        and its appropriates parameters
[d89f09b]753        """
[2140e68]754        for item in self.constraints_list:
[fdb1f375]755            select0 = item[0].GetSelection()
756            if select0 == wx.NOT_FOUND:
757                continue
758            model = item[0].GetClientData(select0)
759            select1 = item[1].GetSelection()
760            if select1 == wx.NOT_FOUND:
761                continue
762            param = item[1].GetString(select1)
[2140e68]763            constraint = item[3].GetValue().lstrip().rstrip()
[bb7d8a4]764            if param.lstrip().rstrip()=="":
765                param= None
[1b14795]766                msg= " Constraint will be ignored!. missing parameters"
767                msg+= " in combobox to set constraint! "
768                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
[6bbeacd4]769            for id, value in self.constraint_dict.iteritems():
[1f57dfd]770                if model == value:
771                    if constraint == "":
[1b14795]772                        msg= " Constraint will be ignored!. missing value"
773                        msg+= " in textcrtl to set constraint! "
774                        wx.PostEvent(self.parent.parent, 
775                                     StatusEvent(status= msg ))
[1f57dfd]776                        constraint = None
[00daba9]777                    if str(param) in self.page_finder[id].get_param2fit():
778                        msg = " Checking constraint for parameter: %s ", param
779                        wx.PostEvent(self.parent.parent, 
780                                    StatusEvent(info="info", status= msg ))
781                    else:
782                        model_name = item[0].GetLabel()
783                        fitpage = self.page_finder[id].get_fit_tab_caption()
784                        msg = "All constrainted parameters must be set "
785                        msg += " adjustable: '%s.%s' "% (model_name, param)
786                        msg += "is NOT checked in '%s'. "% fitpage
787                        msg += " Please check it to fit or"
788                        msg += " remove the line of the constraint."
789                        wx.PostEvent(self.parent.parent, 
790                                StatusEvent(info="error", status= msg ))
791                        return False
792                       
[c647377]793                    for fid in self.page_finder[id].iterkeys():
794                        self.page_finder[id].set_model_param(param,
795                                                        constraint, fid=fid)
[1f57dfd]796                    break
[00daba9]797        return True
[5062bbf]798   
[2140e68]799    def _fill_sizer_model_list(self,sizer):
[d89f09b]800        """
[5062bbf]801        Receive a dictionary containing information to display model name
802       
803        :param page_finder: the dictionary containing models information
804       
[d89f09b]805        """
[2140e68]806        ix = 0
807        iy = 0
808        list=[]
809        sizer.Clear(True)
810       
[dafc36f]811        new_name = wx.StaticText(self, -1, '  Model Title ',
[1b14795]812                                 style=wx.ALIGN_CENTER)
[2140e68]813        new_name.SetBackgroundColour('orange')
[dafc36f]814        new_name.SetForegroundColour(wx.WHITE)
[2140e68]815        sizer.Add(new_name,(iy, ix),(1,1),
816                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
[5062bbf]817        ix += 2 
[dafc36f]818        model_type = wx.StaticText(self, -1, '  Model ')
[2140e68]819        model_type.SetBackgroundColour('grey')
[dafc36f]820        model_type.SetForegroundColour(wx.WHITE)
[2140e68]821        sizer.Add(model_type,(iy, ix),(1,1),
822                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
[5062bbf]823        ix += 1 
[dafc36f]824        data_used = wx.StaticText(self, -1, '  Data ')
[2140e68]825        data_used.SetBackgroundColour('grey')
[dafc36f]826        data_used.SetForegroundColour(wx.WHITE)
[2140e68]827        sizer.Add(data_used,(iy, ix),(1,1),
828                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
[6bbeacd4]829        ix += 1 
[dafc36f]830        tab_used = wx.StaticText(self, -1, '  FitPage ')
[6bbeacd4]831        tab_used.SetBackgroundColour('grey')
[dafc36f]832        tab_used.SetForegroundColour(wx.WHITE)
[6bbeacd4]833        sizer.Add(tab_used,(iy, ix),(1,1),
834                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
835        for id, value in self.page_finder.iteritems():
[53fc5ad9]836            if id not in self.parent.opened_pages:
837                continue
[2140e68]838            try:
839                ix = 0
[c647377]840                for fitproblem in value.get_fit_problem():
[dafc36f]841                    if  self.parent.get_page_by_id(id).batch_on:
842                        continue
843                    data = fitproblem.get_fit_data()
844                    if not data.is_data:
845                        continue
846                    model = fitproblem.get_model()
847                    if model == None:
848                        continue
849                    iy += 1 
850                    name = '_'
851                    if model is not None:
852                        name = str(model.name)
853                    cb = wx.CheckBox(self, -1, name)
854                    cb.SetValue(False)
855                    cb.Enable(model is not None and data.is_data)
856                    sizer.Add(cb, (iy, ix), (1, 1), 
857                               wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
858                    wx.EVT_CHECKBOX(self, cb.GetId(), self.check_model_name)
859                    ix += 2 
860                    type = model.__class__.__name__
861                    model_type = wx.StaticText(self, -1, str(type))
862                    sizer.Add(model_type, (iy, ix), (1, 1), 
863                              wx.EXPAND|wx.ADJUST_MINSIZE, 0)
864                    name = '-'
865                    if data is not None and data.is_data:
866                        name = str(data.name)
867                    data_used = wx.StaticText(self, -1, name)
868                    ix += 1 
869                    sizer.Add(data_used, (iy, ix), (1, 1), 
870                              wx.EXPAND|wx.ADJUST_MINSIZE, 0)
871                    ix += 1 
872                    caption = value.get_fit_tab_caption()
873                    tab_caption_used= wx.StaticText(self, -1, str(caption))
874                    sizer.Add(tab_caption_used, (iy, ix), (1, 1), 
875                              wx.EXPAND|wx.ADJUST_MINSIZE, 0)
[2140e68]876               
[dafc36f]877                    self.model_list.append([cb,value,id,model])   
[2140e68]878            except:
[6bbeacd4]879                raise
880                #pass
[5062bbf]881        iy += 1
[c647377]882        sizer.Add((20, 20), (iy, ix), (1, 1), 
883                  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
[2140e68]884        sizer.Layout()   
[0a9871c]885   
[c647377]886
[4133316]887    def on_set_focus(self, event=None):
888        """
889        The  derivative class is on focus if implemented
890        """
891        if self.parent is not None:
[1976004]892            if self.parent.parent is not None:
893                wx.PostEvent(self.parent.parent, PanelOnFocusEvent(panel=self))
894            self.page_finder = self.parent._manager.get_page_finder()
[4133316]895       
Note: See TracBrowser for help on using the repository browser.