Ignore:
Timestamp:
May 20, 2021 5:26:45 PM (2 years ago)
Author:
toby
Message:

allow , decimal in ValidatedTextCtrl?; Use ValidatedTextCtrl? in SingleFloatDialog?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIctrlGUI.py

    r4911 r4912  
    637637        elif self.type is float:
    638638            try:
     639                if type(val) is str: val = val.replace(',','.')
    639640                val = float(val) # convert strings, if needed
    640641            except:
     
    839840            # allow for above and sind, cosd, sqrt, tand, pi, and abbreviations
    840841            # also addition, subtraction, division, multiplication, exponentiation
    841             self.validchars = '0123456789.-+eE/cosindcqrtap()*'
     842            self.validchars = '0123456789.-+eE/cosindcqrtap()*,'
    842843        else:
    843844            self.validchars = None
     
    882883        except (ValueError, SyntaxError):
    883884            if self.typ is float: # for float values, see if an expression can be evaluated
    884                 val = G2py3.FormulaEval(tc.GetValue())
     885                val = G2py3.FormulaEval(tc.GetValue().replace(',','.'))
    885886                if val is None:
    886887                    tc.invalid = True
     
    24962497
    24972498    '''
    2498     # TODO: better to generalize this for int & float, use validated text control, OK as default.
    2499     # then make SingleFloatDialog & SingleIntDialog as wrappers. Would be good to remove the %-style
    2500     # format, too.
    2501     def __init__(self,parent,title,prompt,value,limits=[0.,1.],format='%.5g'):
     2499    def __init__(self,parent,title,prompt,value,limits=[0.,1.],fmt='%.5g'):
    25022500        wx.Dialog.__init__(self,parent,-1,title,
    25032501            pos=wx.DefaultPosition,style=wx.DEFAULT_DIALOG_STYLE)
    2504         self.panel = None
    2505         self.limits = limits
    2506         self.value = value
    2507         self.prompt = prompt
    2508         self.format = format
    2509         self.Draw()
    2510        
    2511     def Draw(self):
    2512        
    2513         def OnValItem(event):
    2514             if event: event.Skip()
    2515             try:
    2516                 val = float(valItem.GetValue())
    2517                 if self.limits[0] is not None and val < self.limits[0]:
    2518                     raise ValueError
    2519                 if self.limits[1] is not None and val > self.limits[1]:
    2520                     raise ValueError
    2521             except ValueError:
    2522                 val = self.value
    2523             self.value = val
    2524             valItem.SetValue(self.format%(self.value))
    2525            
    2526         if self.panel: self.panel.Destroy()
    2527         self.panel = wx.Panel(self)
     2502        self.CenterOnParent()
    25282503        mainSizer = wx.BoxSizer(wx.VERTICAL)
    2529         mainSizer.Add(wx.StaticText(self.panel,-1,self.prompt),0,wx.ALIGN_CENTER)
    2530         valItem = wx.TextCtrl(self.panel,-1,value=self.format%(self.value),style=wx.TE_PROCESS_ENTER)
     2504        mainSizer.Add(wx.StaticText(self,-1,prompt),0,wx.ALIGN_CENTER)
     2505        #valItem = wx.TextCtrl(self,-1,value=self.format%(self.value),style=wx.TE_PROCESS_ENTER)
     2506        self.buffer = {0:float(fmt%(value))}
     2507        a,b = fmt[1:].split('.')
     2508        f = b[-1]
     2509        try:
     2510            d = int(b[:-1])
     2511        except:
     2512            d = 5
     2513        try:
     2514            w = int(a)
     2515        except:
     2516            w = 3+d
     2517        self.OKbtn = wx.Button(self,wx.ID_OK)
     2518        CancelBtn = wx.Button(self,wx.ID_CANCEL)
     2519        valItem = ValidatedTxtCtrl(self,self.buffer,0,nDig=(w,d,f),
     2520                                       xmin=limits[0],xmax=limits[1],
     2521                                       OKcontrol=self.ControlOKButton)
    25312522        mainSizer.Add(valItem,0,wx.ALIGN_CENTER)
    2532         valItem.Bind(wx.EVT_TEXT_ENTER,OnValItem)
    2533         valItem.Bind(wx.EVT_KILL_FOCUS,OnValItem)
    2534         OkBtn = wx.Button(self.panel,-1,"Ok")
    2535         OkBtn.Bind(wx.EVT_BUTTON, self.OnOk)
    2536         CancelBtn = wx.Button(self.panel,-1,'Cancel')
     2523        self.OKbtn.Bind(wx.EVT_BUTTON, self.OnOk)
    25372524        CancelBtn.Bind(wx.EVT_BUTTON, self.OnCancel)
    25382525        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
    25392526        btnSizer.Add((20,20),1)
    2540         btnSizer.Add(OkBtn)
     2527        btnSizer.Add(self.OKbtn)
    25412528        btnSizer.Add(CancelBtn)
    25422529        btnSizer.Add((20,20),1)
    25432530        mainSizer.Add(btnSizer,0,wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
    2544         self.panel.SetSizer(mainSizer)
    2545         self.panel.Fit()
     2531        self.SetSizer(mainSizer)
    25462532        self.Fit()
    25472533
    25482534    def GetValue(self):
    2549         return self.value
     2535        return self.buffer[0]
    25502536       
    25512537    def OnOk(self,event):
     
    25582544        if parent is not None: parent.Raise()
    25592545        self.EndModal(wx.ID_CANCEL)
     2546
     2547    def ControlOKButton(self,setvalue):
     2548        '''Enable or Disable the OK button for the dialog. Note that this is
     2549        passed into the ValidatedTxtCtrl for use by validators.
     2550
     2551        :param bool setvalue: if True, all entries in the dialog are
     2552          checked for validity. if False then the OK button is disabled.
     2553
     2554        '''
     2555        if setvalue: # turn button on, do only if all controls show as valid
     2556            self.OKbtn.Enable()
     2557        else:
     2558            self.OKbtn.Disable()
    25602559
    25612560class SingleIntDialog(SingleFloatDialog):
Note: See TracChangeset for help on using the changeset viewer.