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
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 ={}, *args, **kwargs):
47        ScrolledPanel.__init__(self, parent,style= wx.FULL_REPAINT_ON_RESIZE )
48        PanelBase.__init__(self, parent)
49        """
50        Simultaneous page display
51        """
52        self.SetupScrolling()
53        ##Font size
54        self.SetWindowVariant(variant = FONT_VARIANT)
55        self.uid = wx.NewId()
56        self.parent = parent
57        ## store page_finder
58        self.page_finder = page_finder
59        ## list contaning info to set constraint
60        ## look like self.constraint_dict[page_id]= page
61        self.constraint_dict={}
62        ## item list 
63        # self.constraints_list=[combobox1, combobox2,=,textcrtl, button ]
64        self.constraints_list=[]
65        ## list of current model
66        self.model_list=[]
67        ## selected mdoel to fit
68        self.model_toFit=[]
69        ## number of constraint
70        self.nb_constraint= 0
71        self.model_cbox_left = None
72        self.model_cbox_right = None
73        self.uid = wx.NewId()
74        ## draw page
75        self.define_page_structure()
76        self.draw_page()
77        self.set_layout()
78        self._set_save_flag(False)
79       
80    def define_page_structure(self):
81        """
82        Create empty sizer for a panel
83        """
84        self.vbox  = wx.BoxSizer(wx.VERTICAL)
85        self.sizer1 = wx.BoxSizer(wx.VERTICAL)
86        self.sizer2 = wx.BoxSizer(wx.VERTICAL)
87        self.sizer3 = wx.BoxSizer(wx.VERTICAL)
88
89        self.sizer1.SetMinSize((PANEL_WID,-1))
90        self.sizer2.SetMinSize((PANEL_WID,-1))
91        self.sizer3.SetMinSize((PANEL_WID,-1))
92        self.vbox.Add(self.sizer1)
93        self.vbox.Add(self.sizer2)
94        self.vbox.Add(self.sizer3)
95       
96    def set_scroll(self):
97        """
98        """
99        #self.SetScrollbars(20,20,25,65)
100        self.Layout() 
101         
102    def set_layout(self):
103        """
104        layout
105        """
106        self.vbox.Layout()
107        self.vbox.Fit(self) 
108        self.SetSizer(self.vbox)
109        self.set_scroll()
110        self.Centre()
111       
112    def onRemove(self, event):
113        """
114        Remove constraint fields
115        """
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:
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]
126                sizer.Clear(True)
127                self.sizer_constraints.Remove(sizer)
128                #self.SetScrollbars(20,20,25,65)
129                self.constraints_list.remove(item)
130                self.nb_constraint -= 1
131                self.sizer2.Layout()
132                self.Layout()
133                break
134        self._onAdd_constraint(None)   
135             
136    def onFit(self, event):
137        """
138        signal for fitting
139       
140        """
141        ## making sure all parameters content a constraint
142        ## validity of the constraint expression is own by fit engine
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"))
150        if self.show_constraint.GetValue():
151            if not self._set_constraint():
152                return
153        ## model was actually selected from this page to be fit
154        if len(self.model_toFit) >= 1 :
155            self.manager._reset_schedule_problem(value=0)
156            for item in self.model_list:
157                if item[0].GetValue():
158                    self.manager.schedule_for_fit(value=1, uid=item[2]) 
159            try:
160                if not self.manager.onFit(uid=self.uid):
161                    return
162            except:
163                msg= "Select at least one parameter to fit in the FitPages."
164                wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
165        else:
166            msg= "Select at least one model check box to fit "
167            wx.PostEvent(self.parent.parent, StatusEvent(status=msg))
168           
169    def set_manager(self, manager):
170        """
171        set panel manager
172       
173        :param manager: instance of plugin fitting
174       
175        """
176        self.manager = manager
177       
178    def check_all_model_name(self,event):
179        """
180        check all models names
181        """
182        self.model_toFit = [] 
183        if self.cb1.GetValue()== True:
184            for item in self.model_list:
185                if item[0].IsEnabled():
186                    item[0].SetValue(True)
187                    self.model_toFit.append(item)
188               
189            ## constraint info
190            self._store_model()
191            ## display constraint fields
192            if self.show_constraint.GetValue() and\
193                             len(self.constraints_list)==0:
194                self._show_all_constraint() 
195                self._show_constraint()
196        else:
197            for item in self.model_list:
198                item[0].SetValue(False) 
199               
200            self.model_toFit=[]
201            ##constraint info
202            self._hide_constraint()
203           
204        self._update_easy_setup_cb()
205        self.Refresh()
206       
207    def check_model_name(self,event):
208        """
209        Save information related to checkbox and their states
210        """
211        self.model_toFit=[]
212        cbox = event.GetEventObject()
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)
220       
221        ## display constraint fields
222        if len(self.model_toFit)>=1:
223            self._store_model()
224            if self.show_constraint.GetValue() and\
225                             len(self.constraints_list)==0:
226                self._show_all_constraint() 
227                self._show_constraint()
228
229        elif len(self.model_toFit)< 1:
230            ##constraint info
231            self._hide_constraint() 
232                       
233        self._update_easy_setup_cb()
234        ## set the value of the main check button         
235        if len(self.model_list)==len(self.model_toFit):
236            self.cb1.SetValue(True)
237            self.Layout()
238            return
239        else:
240            self.cb1.SetValue(False)
241            self.Layout()
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:
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
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)
265                if str(model.name) not in self.model_cbox.GetItems():
266                    self.model_cbox.Append(str(model.name), model)
267            self.model_cbox_left.SetSelection(0)
268            self.sizer2.Layout()
269            self.sizer3.Layout()
270       
271    def draw_page(self):     
272        """
273        Draw a sizer containing couples of data and model
274        """ 
275        self.model_list=[]
276        self.model_toFit=[]
277        self.constraints_list=[]
278        self.constraint_dict={}
279        self.nb_constraint= 0
280        self.model_cbox_left = None
281        self.model_cbox_right = None
282       
283        if len(self.model_list)>0:
284            for item in self.model_list:
285                item[0].SetValue(False) 
286                self.manager.schedule_for_fit(value=0, uid=item[2])
287               
288        self.sizer1.Clear(True)   
289        box_description= wx.StaticBox(self, -1,"Fit Combinations")
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:
295            msg = " No fit combinations are found! \n\n"
296            msg += " Please load data and set up "
297            msg += "at least two fit panels first..."
298            sizer_title.Add(wx.StaticText(self, -1, msg))
299        else:
300            ## store model 
301            self._store_model()
302       
303            self.cb1 = wx.CheckBox(self, -1,'Select all')
304            self.cb1.SetValue(False)
305            wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.check_all_model_name)
306           
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
313            self._fill_sizer_model_list(sizer_couples)
314            ## draw the sizer containing constraint info
315            self._fill_sizer_constraint()
316            ## draw fit button
317            self._fill_sizer_fit()
318        #--------------------------------------------------------
319        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=5) 
320        boxsizer1.Add(sizer_couples, 1, flag= wx.TOP|wx.BOTTOM,border=5)
321       
322        self.sizer1.Add(boxsizer1, 1, wx.EXPAND | wx.ALL, 10)
323        self.sizer1.Layout()
324        #self.SetScrollbars(20,20,25,65)
325        self.AdjustScrollbars()
326        self.Layout()
327       
328    def _store_model(self):
329        """
330         Store selected model
331        """
332        if len(self.model_toFit) < 1:
333            return
334        for item in self.model_toFit:
335            model = item[3]
336            page_id= item[2]
337            self.constraint_dict[page_id] = model
338                   
339    def _display_constraint(self, event):
340        """
341        Show fields to add constraint
342        """
343        if len(self.model_toFit)< 1:
344            msg= "Select at least 1 model to add constraint "
345            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
346            ## hide button
347            self._hide_constraint()
348            return
349        if self.show_constraint.GetValue():
350            self._show_all_constraint() 
351            self._show_constraint()
352            self.Layout()
353            return
354        else:
355           self._hide_constraint()
356           self.Layout()
357           return 
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()
409        #self.SetScrollbars(20,20,25,65)
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
463                if num_cbox == (len(param_list) + 1):
464                    break
465                self._show_constraint()
466       
467        self.sizer_constraints.Layout()
468        self.sizer2.Layout()
469        self.SetScrollbars(20,20,25,65)
470        self.Layout()
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 ))     
480    def _show_constraint(self):
481        """
482        Show constraint fields
483        """
484        self.btAdd.Show(True)
485        if len(self.constraints_list)!= 0:
486            nb_fit_param = 0
487            for id, model in self.constraint_dict.iteritems():
488                nb_fit_param += len(self.page_finder[id].get_param2fit())
489            ##Don't add anymore
490            if len(self.constraints_list) == nb_fit_param:
491                msg= "Cannot add another constraint .Maximum of number "
492                msg += "Parameters name reached %s"%str(nb_fit_param)
493                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
494                self.sizer_constraints.Layout()
495                self.sizer2.Layout()
496                #self.SetScrollbars(20,20,25,65)
497                return
498        if len(self.model_toFit) < 1 :
499            msg= "Select at least 1 model to add constraint "
500            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
501            self.sizer_constraints.Layout()
502            self.sizer2.Layout()
503            #self.SetScrollbars(20,20,25,65)
504            return
505           
506        sizer_constraint =  wx.BoxSizer(wx.HORIZONTAL)
507        model_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY)
508        model_cbox.Clear()
509        param_cbox = wx.ComboBox(self, -1,style=wx.CB_READONLY, size=(100,-1),)
510        param_cbox.Hide()
511       
512        #This is for GetCLientData() _on_select_param: Was None return on MAC.
513        self.param_cbox = param_cbox
514       
515        wx.EVT_COMBOBOX(param_cbox,-1, self._on_select_param)
516        self.ctl2 = wx.TextCtrl(self, -1)
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()
525        for id, model in self.constraint_dict.iteritems():
526            ## check if all parameters have been selected for constraint
527            ## then do not allow add constraint on parameters
528            model_cbox.Append( str(model.name), model)
529           
530        #This is for GetCLientData() passing to self._on_select_param: Was None return on MAC.
531        self.model_cbox = model_cbox
532           
533        wx.EVT_COMBOBOX(model_cbox,-1, self._on_select_model)
534        sizer_constraint.Add((5,-1))
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)
538        sizer_constraint.Add(self.ctl2, flag= wx.RIGHT|wx.EXPAND,border=10)
539        sizer_constraint.Add(self.btRemove, flag= wx.RIGHT|wx.EXPAND,border=10)
540     
541        self.sizer_constraints.Insert(before=self.nb_constraint,
542                        item=sizer_constraint, flag= wx.TOP|wx.BOTTOM|wx.EXPAND,
543                        border=5)
544        ##[combobox1, combobox2,=,textcrtl, remove button ]
545        self.constraints_list.append([model_cbox, param_cbox, egal_txt, 
546                                    self.ctl2,self.btRemove,sizer_constraint])
547   
548        self.nb_constraint += 1
549        self.sizer_constraints.Layout()
550        self.sizer2.Layout()
551        #self.SetScrollbars(20,20,25,65)
552       
553    def _hide_constraint(self): 
554        """
555        hide buttons related constraint
556        """ 
557        for id in  self.page_finder.iterkeys():
558            self.page_finder[id].clear_model_param()
559               
560        self.nb_constraint = 0     
561        self.constraint_dict = {}
562        if hasattr(self,"btAdd"):
563            self.btAdd.Hide()
564        self._store_model()
565        if self.model_cbox_left != None:
566            try:
567                self.model_cbox_left.Clear()
568            except:
569                pass
570            self.model_cbox_left = None
571        if self.model_cbox_right != None:
572            try:
573                self.model_cbox_right.Clear()
574            except:
575                pass
576            self.model_cbox_right = None
577        self.constraints_list = []   
578        self.sizer_all_constraints.Clear(True) 
579        self.sizer_all_constraints.Layout()         
580        self.sizer_constraints.Clear(True) 
581        self.sizer_constraints.Layout()   
582        self.sizer2.Layout()
583        #self.SetScrollbars(20,20,25,65)
584        #self.AdjustScrollbars()   
585           
586    def _on_select_model(self, event):
587        """
588        fill combox box with list of parameters
589        """
590        param_list = []
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)
594        for id, dic_model in self.constraint_dict.iteritems():
595            if model == dic_model:
596                param_list = self.page_finder[id].get_param2fit()
597                #break
598        #param_list= get_fittableParam(model)
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:
606            param_cbox.Append( str(param), model)
607
608        param_cbox.Show(True)
609        self.btRemove.Show(True)
610        self.btAdd.Show(True)
611        self.sizer2.Layout()
612        #self.SetScrollbars(20,20,25,65)
613       
614    def _on_select_param(self, event):
615        """
616        Store the appropriate constraint in the page_finder
617        """
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)
621        param = event.GetString()
622     
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)
631        #self.sizer2.Layout()
632        #self.SetScrollbars(20,20,25,65)
633       
634    def _onAdd_constraint(self, event): 
635        """
636        Add another line for constraint
637        """
638        if not self.show_constraint.GetValue():
639            msg= " Select Yes to add Constraint "
640            wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
641            return 
642        ## check that a constraint is added
643        # before allow to add another constraint
644        for item in self.constraints_list:
645            model_cbox = item[0]
646            if model_cbox.GetString(0)=="":
647                msg= " Select a model Name! "
648                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
649                return 
650            param_cbox = item[1]
651            if param_cbox.GetString(0)=="":
652                msg= " Select a parameter Name! "
653                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
654                return 
655            ctl2 = item[3]
656            if ctl2.GetValue().lstrip().rstrip()=="":
657                model= param_cbox.GetClientData(param_cbox.GetCurrentSelection())
658                msg= " Enter a constraint for %s.%s! "%(model.name, 
659                                                        param_cbox.GetString(0))           
660                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
661                return 
662        ## some model or parameters can be constrained
663        self._show_constraint()
664       
665    def _fill_sizer_fit(self):
666        """
667        Draw fit button
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         
674        self.btFit = wx.Button(self,wx.NewId(),'Fit', size=wx.DefaultSize)
675        self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id= self.btFit.GetId())
676        self.btFit.SetToolTipString("Perform fit.")
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."
680        #text+= "automatically for more than 2 combinations checked"
681        text_hint = wx.StaticText(self,-1,text)
682       
683        sizer_button.Add(text_hint,  wx.RIGHT|wx.EXPAND, 10)
684        sizer_button.Add(self.btFit, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10)
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()
689        #self.SetScrollbars(20,20,25,65)
690       
691    def _fill_sizer_constraint(self):
692        """
693        Fill sizer containing constraint info
694        """
695        msg = "Select at least 2 model to add constraint "
696        wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
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)
702        self.sizer_all_constraints = wx.BoxSizer(wx.HORIZONTAL)
703        self.sizer_constraints = wx.BoxSizer(wx.VERTICAL)
704        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
705       
706        self.hide_constraint = wx.RadioButton(self, -1, 'No', (10, 10), 
707                                              style=wx.RB_GROUP)
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()    )
713        self.hide_constraint.SetValue(True)
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) )
723       
724        self.btAdd =wx.Button(self,wx.NewId(),'Add')
725        self.btAdd.Bind(wx.EVT_BUTTON, self._onAdd_constraint, 
726                        id=self.btAdd.GetId())
727        self.btAdd.SetToolTipString("Add another constraint?")
728        self.btAdd.Hide()
729     
730        text_hint = wx.StaticText(self, -1, 
731                                  "Example: [M0][paramter] = M1.parameter") 
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       
735        boxsizer1.Add(sizer_title, flag= wx.TOP|wx.BOTTOM,border=10)
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)
741       
742        self.sizer2.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
743        self.sizer2.Layout()
744        #self.SetScrollbars(20,20,25,65)
745   
746    def _set_constraint(self):
747        """
748        get values from the constrainst textcrtl ,parses them into model name
749        parameter name and parameters values.
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
753        """
754        for item in self.constraints_list:
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)
763            constraint = item[3].GetValue().lstrip().rstrip()
764            if param.lstrip().rstrip()=="":
765                param= None
766                msg= " Constraint will be ignored!. missing parameters"
767                msg+= " in combobox to set constraint! "
768                wx.PostEvent(self.parent.parent, StatusEvent(status= msg ))
769            for id, value in self.constraint_dict.iteritems():
770                if model == value:
771                    if constraint == "":
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 ))
776                        constraint = None
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                       
793                    for fid in self.page_finder[id].iterkeys():
794                        self.page_finder[id].set_model_param(param,
795                                                        constraint, fid=fid)
796                    break
797        return True
798   
799    def _fill_sizer_model_list(self,sizer):
800        """
801        Receive a dictionary containing information to display model name
802       
803        :param page_finder: the dictionary containing models information
804       
805        """
806        ix = 0
807        iy = 0
808        list=[]
809        sizer.Clear(True)
810       
811        new_name = wx.StaticText(self, -1, '  Model Title ',
812                                 style=wx.ALIGN_CENTER)
813        new_name.SetBackgroundColour('orange')
814        new_name.SetForegroundColour(wx.WHITE)
815        sizer.Add(new_name,(iy, ix),(1,1),
816                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
817        ix += 2 
818        model_type = wx.StaticText(self, -1, '  Model ')
819        model_type.SetBackgroundColour('grey')
820        model_type.SetForegroundColour(wx.WHITE)
821        sizer.Add(model_type,(iy, ix),(1,1),
822                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
823        ix += 1 
824        data_used = wx.StaticText(self, -1, '  Data ')
825        data_used.SetBackgroundColour('grey')
826        data_used.SetForegroundColour(wx.WHITE)
827        sizer.Add(data_used,(iy, ix),(1,1),
828                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
829        ix += 1 
830        tab_used = wx.StaticText(self, -1, '  FitPage ')
831        tab_used.SetBackgroundColour('grey')
832        tab_used.SetForegroundColour(wx.WHITE)
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():
836            if id not in self.parent.opened_pages:
837                continue
838            try:
839                ix = 0
840                for fitproblem in value.get_fit_problem():
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)
876               
877                    self.model_list.append([cb,value,id,model])   
878            except:
879                raise
880                #pass
881        iy += 1
882        sizer.Add((20, 20), (iy, ix), (1, 1), 
883                  wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
884        sizer.Layout()   
885   
886
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:
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()
895       
Note: See TracBrowser for help on using the repository browser.