Changeset 1177


Ignore:
Timestamp:
Dec 19, 2013 1:34:54 PM (8 years ago)
Author:
vondreele
Message:

try to add more control over number format from ValidatedTxtCtrl?
not entirely successful so far

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIgrid.py

    r1172 r1177  
    175175      type of int, float, str or unicode; the TextCrtl will be initialized
    176176      from this value.
     177     
     178    :param list [nDig,nPlc]: number of digits & places after decimal to use
     179      for display of float (default=[10,2])
    177180
    178181    :param bool notBlank: if True (default) blank values are invalid
     
    223226
    224227    '''
    225     def __init__(self,parent,loc,key,notBlank=True,min=None,max=None,
     228    def __init__(self,parent,loc,key,nDig=[10,2],notBlank=True,min=None,max=None,
    226229                 OKcontrol=None,OnLeave=None,typeHint=None,
    227230                 CIFinput=False, **kw):
     
    229232        self.result = loc
    230233        self.key = key
     234        self.nDig = nDig
    231235        self.OKcontrol=OKcontrol
    232236        self.OnLeave = OnLeave
     
    314318                else:
    315319                    self.invalid = True
    316             wx.TextCtrl.SetValue(self,str(G2py3.FormatValue(val)))
     320            wx.TextCtrl.SetValue(self,str(G2py3.FormatValue(val,self.nDig)))
    317321        else:
    318322            wx.TextCtrl.SetValue(self,str(val))
  • trunk/GSASIIpwdGUI.py

    r1175 r1177  
    13171317   
    13181318    parms = []
    1319     parms.append(['Scale','Histogram scale factor: '])
    1320     parms.append(['Gonio. radius','Goniometer radius (mm): '])
     1319    parms.append(['Scale','Histogram scale factor: ',[10,4]])
     1320    parms.append(['Gonio. radius','Goniometer radius (mm): ',[10,3]])
    13211321    if data['Type'] == 'Debye-Scherrer':
    1322         parms += [['DisplaceX',u'Sample X displ. perp. to beam (\xb5m): '],
    1323             ['DisplaceY',u'Sample Y displ. || to beam (\xb5m): '],
    1324             ['Absorption',u'Sample absorption (\xb5\xb7r): '],]
     1322        parms += [['DisplaceX',u'Sample X displ. perp. to beam (\xb5m): ',[10,3]],
     1323            ['DisplaceY',u'Sample Y displ. || to beam (\xb5m): ',[10,3]],
     1324            ['Absorption',u'Sample absorption (\xb5\xb7r): ',[10,4]],]
    13251325    elif data['Type'] == 'Bragg-Brentano':
    1326         parms += [['Shift',u'Sample displacement(\xb5m): '],
    1327             ['Transparency',u'Sample transparency(1/\xb5eff, cm): '],
    1328             ['SurfRoughA','Surface roughness A: '],
    1329             ['SurfRoughB','Surface roughness B: '],]
    1330     parms.append(['Omega','Goniometer omega:',])
    1331     parms.append(['Chi','Goniometer chi:',])
    1332     parms.append(['Phi','Goniometer phi:',])
    1333     parms.append(['Temperature','Sample temperature (K): ',])
    1334     parms.append(['Pressure','Sample pressure (MPa): ',])
     1326        parms += [['Shift',u'Sample displacement(\xb5m): ',[10,4]],
     1327            ['Transparency',u'Sample transparency(1/\xb5eff, cm): ',[10,3]],
     1328            ['SurfRoughA','Surface roughness A: ',[10,4]],
     1329            ['SurfRoughB','Surface roughness B: ',[10,4]]]
     1330    parms.append(['Omega','Goniometer omega:',[10,3]])
     1331    parms.append(['Chi','Goniometer chi:',[10,3]])
     1332    parms.append(['Phi','Goniometer phi:',[10,3]])
     1333    parms.append(['Temperature','Sample temperature (K): ',[10,3]])
     1334    parms.append(['Pressure','Sample pressure (MPa): ',[10,3]])
    13351335               
    13361336    mainSizer = wx.BoxSizer(wx.VERTICAL)
     
    13631363
    13641364    parmSizer = wx.FlexGridSizer(10,2,5,0)
    1365     for key,lbl in parms:
     1365    for key,lbl,nDig in parms:
    13661366        if 'list' in str(type(data[key])):
    13671367            parmRef = G2gd.G2CheckBox(G2frame.dataDisplay,' '+lbl,data[key],1)
    13681368            parmSizer.Add(parmRef,0,wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
    1369             parmVal = G2gd.ValidatedTxtCtrl(G2frame.dataDisplay,data[key],0,typeHint=float)
     1369            parmVal = G2gd.ValidatedTxtCtrl(G2frame.dataDisplay,data[key],0,nDig=nDig,typeHint=float)
    13701370        else:
    13711371            parmSizer.Add(wx.StaticText(G2frame.dataDisplay,label=' '+lbl),
  • trunk/GSASIIpy3.py

    r1077 r1177  
    3939    return val
    4040
    41 def FormatValue(val,maxdigits=10):
    42     '''Format a float to fit in ``maxdigits`` spaces, showing as much
    43     precision as possible, more or less.
     41def FormatValue(val,maxdigits=[10,2]):
     42    '''Format a float to fit in ``maxdigits[0]`` spaces with maxdigits[1] after decimal.
    4443
    4544    :param float val: number to be formatted.
    4645
    47     :param int maxdigits: the number of digits to be used for display of the
    48       number (defaults to 10).
     46    :param list maxdigits: the number of digits & places after decimal to be used for display of the
     47      number (defaults to [10,2]).
    4948
    5049    :returns: a string with <= maxdigits characters (I hope). 
     
    5251    # does the standard str() conversion fit?
    5352    string = str(val)
    54     if len(string) <= maxdigits: return string.strip()
     53    if len(string) <= maxdigits[0]: return string.strip()
    5554    # negative numbers, leave room for a sign
    56     if val < 0: maxdigits -= 1
    57     decimals = maxdigits - 2
     55    if val < 0: maxdigits[0] -= 1
     56    decimals = maxdigits[0] - maxdigits[1]
    5857    if abs(val) < 1e-99 or abs(val) > 1e99:
    59         decimals = maxdigits - 6
    60         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}" # create format string
     58        decimals = min(maxdigits[0]-6,maxdigits[1])
     59        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}" # create format string
    6160    elif abs(val) < 1e-9 or abs(val) > 1e9:
    62         decimals = maxdigits - 5
    63         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     61        decimals = min(maxdigits[0]-5,maxdigits[1])
     62        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
    6463    elif abs(val) < 10**(4-decimals): # make sure at least 4 decimals show
    65         decimals = maxdigits - 5
    66         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     64        decimals = min(maxdigits[0]-5,maxdigits[1])
     65        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
    6766    elif abs(val) >= 10**decimals: # deal with large numbers in smaller spaces
    68         decimals = maxdigits - 5
    69         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     67        decimals = min(maxdigits[0]-5,maxdigits[1])
     68        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
    7069    elif abs(val) < 1: # use f format for small numbers
    71         decimals = maxdigits - 2
    72         fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
     70        decimals = min(maxdigits[0]-3,maxdigits[1])
     71        fmt = "{" + (":{:d}.{:d}f".format(maxdigits[0],decimals))+"}"
    7372    else: # in range where g formatting should do what I want
    74         decimals = maxdigits - 1
    75         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     73        decimals = maxdigits[0] - 1
     74        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
     75    print fmt,val
    7676    return fmt.format(val).strip()
Note: See TracChangeset for help on using the changeset viewer.