source: sasview/src/sas/perspectives/fitting/simfitpage.py @ 6f16e25

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 6f16e25 was 6f16e25, checked in by Paul Kienzle <pkienzle@…>, 9 years ago

clean up wx id handling in fitting perspective

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