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@…>, 8 years ago

clean up wx id handling in fitting perspective

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