Ignore:
Timestamp:
Apr 16, 2018 5:41:34 PM (5 years ago)
Author:
toby
Message:

rework ValidatedTextCtrl? so that typeHint overrides the initial variable type; fix display of invalid numbers; use nDig to drive typeHint to float; postpone import of pyspg & polymask when not needed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIctrlGUI.py

    r3344 r3345  
    428428       is either 'f' (default) or 'g'. Alternately, None can be specified which
    429429       causes numbers to be displayed with approximately 5 significant figures
     430       for floats. If this is specified, then :obj:`typeHint`=float becomes the
     431       default.
    430432       (Default=None).
    431433
     
    468470      be called.
    469471
    470     :param type typeHint: the value of typeHint is overrides the initial value
    471       for the dict/list element ``loc[key]``, if set to
    472       int or float, which specifies the type for input to the TextCtrl.
    473       Defaults as None, which is ignored.
     472    :param type typeHint: the value of typeHint should be int, float or str (or None).
     473      The value for this will override the initial type taken from value
     474      for the dict/list element ``loc[key]`` if not None and thus specifies the
     475      type for input to the TextCtrl.
     476      Defaults as None, which is ignored, unless  :obj:`nDig` is specified in which
     477      case the default is float.
    474478
    475479    :param bool CIFinput: for str input, indicates that only printable
     
    509513        else:
    510514            kw['style'] = wx.TE_PROCESS_ENTER
    511         if 'int' in str(type(val)) or typeHint is int:
     515        if typeHint is not None:
     516            self.type = typeHint
     517        elif nDig is not None:
     518            self.type = float
     519        elif 'int' in str(type(val)):
    512520            self.type = int
     521        elif 'float' in str(type(val)):
     522            self.type = float
     523        elif isinstance(val,str) or isinstance(val,unicode):
     524            self.type = str
     525        elif val is None:
     526            raise Exception("ValidatedTxtCtrl error: value of "+str(key)+
     527                             " element is None and typeHint not defined as int or float")
     528        else:
     529            raise Exception("ValidatedTxtCtrl error: Unknown element ("+str(key)+
     530                             ") type: "+str(type(val)))
     531        if self.type is int:       
    513532            wx.TextCtrl.__init__(self,parent,wx.ID_ANY,
    514533                validator=NumberValidator(int,result=loc,key=key,min=min,max=max,
     
    519538                self.invalid = True
    520539                self._IndicateValidity()
    521 
    522         elif 'float' in str(type(val)) or typeHint is float:
    523             self.type = float
     540        elif self.type is float:
    524541            wx.TextCtrl.__init__(self,parent,wx.ID_ANY,
    525542                validator=NumberValidator(float,result=loc,key=key,min=min,max=max,
     
    530547                self.invalid = True
    531548                self._IndicateValidity()
    532 
    533         elif isinstance(val,str) or isinstance(val,unicode) or typeHint is str:
     549        else:
    534550            if self.CIFinput:
    535551                wx.TextCtrl.__init__(
     
    547563                self.invalid = False
    548564                self.Bind(wx.EVT_CHAR,self._GetStringValue)
    549         elif val is None:
    550             raise Exception("ValidatedTxtCtrl error: value of "+str(key)+
    551                              " element is None and typeHint not defined as int or float")
    552         else:
    553             raise Exception("ValidatedTxtCtrl error: Unknown element ("+str(key)+
    554                              ") type: "+str(type(val)))
     565       
    555566        # When the mouse is moved away or the widget loses focus,
    556567        # display the last saved value, if an expression
     
    586597                else:
    587598                    self.invalid = True
    588             if show: wx.TextCtrl.SetValue(self,str(val))
     599            if show and not self.invalid: wx.TextCtrl.SetValue(self,str(val))
    589600        elif self.type is float:
    590601            try:
     
    595606                else:
    596607                    self.invalid = True
    597             if self.nDig and show:
     608            if self.nDig and show and not self.invalid:
    598609                wx.TextCtrl.SetValue(self,str(G2py3.FormatValue(val,self.nDig)))
    599             elif show:
     610            elif show and not self.invalid:
    600611                wx.TextCtrl.SetValue(self,str(G2py3.FormatSigFigs(val)).rstrip('0'))
    601612        else:
     
    50455056    # test Tutorial access
    50465057    #======================================================================
    5047     dlg = OpenTutorial(frm)
    5048     if dlg.ShowModal() == wx.ID_OK:
    5049         print("OK")
    5050     else:
    5051         print("Cancel")
    5052     dlg.Destroy()
    5053     sys.exit()
     5058    # dlg = OpenTutorial(frm)
     5059    # if dlg.ShowModal() == wx.ID_OK:
     5060    #     print("OK")
     5061    # else:
     5062    #     print("Cancel")
     5063    # dlg.Destroy()
     5064    # sys.exit()
    50545065    #======================================================================
    50555066    # test ScrolledMultiEditor
     
    50795090    # print 'after',Data1,'\n',Data2
    50805091    # dlg.Destroy()
    5081     Data3 = {
    5082          'Order':1.0,
    5083          'omega':1.1,
    5084          'chi':2.0,
    5085          'phi':2.3,
    5086          'Order1':1.0,
    5087          'omega1':1.1,
    5088          'chi1':2.0,
    5089          'phi1':2.3,
    5090          'Order2':1.0,
    5091          'omega2':1.1,
    5092          'chi2':2.0,
    5093          'phi2':2.3,
    5094          }
    5095     elemlst = sorted(Data3.keys())
    5096     dictlst = len(elemlst)*[Data3,]
    5097     prelbl = elemlst[:]
    5098     prelbl[0]="this is a much longer label to stretch things out"
    5099     Data2 = len(elemlst)*[False,]
    5100     Data2[1] = Data2[3] = True
    5101     Checkdictlst = len(elemlst)*[Data2,]
    5102     Checkelemlst = range(len(Checkdictlst))
     5092    # Data3 = {
     5093    #      'Order':1.0,
     5094    #      'omega':1.1,
     5095    #      'chi':2.0,
     5096    #      'phi':2.3,
     5097    #      'Order1':1.0,
     5098    #      'omega1':1.1,
     5099    #      'chi1':2.0,
     5100    #      'phi1':2.3,
     5101    #      'Order2':1.0,
     5102    #      'omega2':1.1,
     5103    #      'chi2':2.0,
     5104    #      'phi2':2.3,
     5105    #      }
     5106    # elemlst = sorted(Data3.keys())
     5107    # dictlst = len(elemlst)*[Data3,]
     5108    # prelbl = elemlst[:]
     5109    # prelbl[0]="this is a much longer label to stretch things out"
     5110    # Data2 = len(elemlst)*[False,]
     5111    # Data2[1] = Data2[3] = True
     5112    # Checkdictlst = len(elemlst)*[Data2,]
     5113    # Checkelemlst = range(len(Checkdictlst))
    51035114    #print 'before',Data3,'\n',Data2
    51045115    #print dictlst,"\n",elemlst
     
    51385149    # test G2MultiChoiceDialog
    51395150    #======================================================================
    5140     choices = []
    5141     for i in range(21):
    5142         choices.append("option_"+str(i))
    5143     od = {
    5144         'label_1':'This is a bool','value_1':True,
    5145         'label_2':'This is a int','value_2':-1,
    5146         'label_3':'This is a float','value_3':1.0,
    5147         'label_4':'This is a string','value_4':'test',}
    5148     dlg = G2MultiChoiceDialog(frm, 'Sequential refinement',
    5149                               'Select dataset to include',
    5150                               choices,extraOpts=od)
    5151     sel = range(2,11,2)
    5152     dlg.SetSelections(sel)
    5153     dlg.SetSelections((1,5))
    5154     if dlg.ShowModal() == wx.ID_OK:
    5155         for sel in dlg.GetSelections():
    5156             print (sel,choices[sel])
    5157     print (od)
    5158     od = {}
    5159     dlg = G2MultiChoiceDialog(frm, 'Sequential refinement',
    5160                               'Select dataset to include',
    5161                               choices,extraOpts=od)
    5162     sel = range(2,11,2)
    5163     dlg.SetSelections(sel)
    5164     dlg.SetSelections((1,5))
    5165     if dlg.ShowModal() == wx.ID_OK: pass
     5151    # choices = []
     5152    # for i in range(21):
     5153    #     choices.append("option_"+str(i))
     5154    # od = {
     5155    #     'label_1':'This is a bool','value_1':True,
     5156    #     'label_2':'This is a int','value_2':-1,
     5157    #     'label_3':'This is a float','value_3':1.0,
     5158    #     'label_4':'This is a string','value_4':'test',}
     5159    # dlg = G2MultiChoiceDialog(frm, 'Sequential refinement',
     5160    #                           'Select dataset to include',
     5161    #                           choices,extraOpts=od)
     5162    # sel = range(2,11,2)
     5163    # dlg.SetSelections(sel)
     5164    # dlg.SetSelections((1,5))
     5165    # if dlg.ShowModal() == wx.ID_OK:
     5166    #     for sel in dlg.GetSelections():
     5167    #         print (sel,choices[sel])
     5168    # print (od)
     5169    # od = {}
     5170    # dlg = G2MultiChoiceDialog(frm, 'Sequential refinement',
     5171    #                           'Select dataset to include',
     5172    #                           choices,extraOpts=od)
     5173    # sel = range(2,11,2)
     5174    # dlg.SetSelections(sel)
     5175    # dlg.SetSelections((1,5))
     5176    # if dlg.ShowModal() == wx.ID_OK: pass
    51665177    #======================================================================
    51675178    # test wx.MultiChoiceDialog
     
    51775188    #         print sel,choices[sel]
    51785189
    5179     # pnl = wx.Panel(frm)
    5180     # siz = wx.BoxSizer(wx.VERTICAL)
    5181 
    5182     # td = {'Goni':200.,'a':1.,'calc':1./3.,'string':'s'}
    5183     # for key in sorted(td):
    5184     #     txt = ValidatedTxtCtrl(pnl,td,key)
    5185     #     siz.Add(txt)
    5186     # pnl.SetSizer(siz)
    5187     # siz.Fit(frm)
    5188     # app.MainLoop()
    5189     # print td
     5190    pnl = wx.Panel(frm)
     5191    siz = wx.BoxSizer(wx.VERTICAL)
     5192    td = {'Goni':200.,'a':1.,'int':1,'calc':1./3.,'string':'s'}
     5193    for key in sorted(td):
     5194        txt = ValidatedTxtCtrl(pnl,td,key,typeHint=float)
     5195        siz.Add(txt)
     5196    pnl.SetSizer(siz)
     5197    siz.Fit(frm)
     5198    app.MainLoop()
     5199    print(td)
Note: See TracChangeset for help on using the changeset viewer.