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

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 33477fd was 7fd62c3, checked in by Jae Cho <jhjcho@…>, 13 years ago

OOps, removed print statement

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