Changeset 3828

Show
Ignore:
Timestamp:
12/21/07 05:56:07 (1 year ago)
Author:
ed
Message:

Added the StrictNumericEntry? property to dTextBoxMixin. It defaults to True, which will result in the exact same type-coercion behavior as before. When set to False, it will evaluate the string and convert the Value to the indicated type, depending on whether it contains a decimal point, or a trailing "L", or a single "e".

Updated dSpinnerX to use the new StrictNumericEntry? property to allow it to behave as designed.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/ui/uiwx/dSpinnerX.py

    r3738 r3828  
    4040        self._baseClass = dSpinnerX 
    4141        # Create the child controls 
    42         self._proxy_textbox = dabo.ui.dTextBox(self, Value=val, Width=32) 
     42        self._proxy_textbox = dabo.ui.dTextBox(self, Value=val, Width=32,  
     43                StrictNumericEntry=False) 
    4344        self._proxy_spinner = dSpinButton(parent=self) 
    4445        self.__constructed = True 
     
    286287        self.Max = 10 
    287288        self.Min = 0 
    288         self.Value = 0.0 
    289         self.Increment = 1.0 
     289        self.Value = 0 
     290        self.Increment = 1 
    290291        self.SpinnerWrap = True 
    291292        self.FontSize = 10 
     
    323324            gsz = dabo.ui.dGridSizer(MaxCols=2, HGap=4, VGap=6) 
    324325            lbl = dabo.ui.dLabel(pnl, Caption="Min") 
    325             txt = dabo.ui.dTextBox(pnl, DataSource=spn, DataField="Min"
     326            txt = dabo.ui.dTextBox(pnl, DataSource=spn, DataField="Min", StrictNumericEntry=False
    326327            gsz.append(lbl, halign="right") 
    327328            gsz.append(txt) 
    328329            lbl = dabo.ui.dLabel(pnl, Caption="Max") 
    329             txt = dabo.ui.dTextBox(pnl, DataSource=spn, DataField="Max"
     330            txt = dabo.ui.dTextBox(pnl, DataSource=spn, DataField="Max", StrictNumericEntry=False
    330331            gsz.append(lbl, halign="right") 
    331332            gsz.append(txt) 
    332333            lbl = dabo.ui.dLabel(pnl, Caption="Increment") 
    333             txt = dabo.ui.dTextBox(pnl, DataSource=spn, DataField="Increment"
     334            txt = dabo.ui.dTextBox(pnl, DataSource=spn, DataField="Increment", StrictNumericEntry=False
    334335            gsz.append(lbl, halign="right") 
    335336            gsz.append(txt) 
  • trunk/dabo/ui/uiwx/dTextBoxMixin.py

    r3824 r3828  
    22import re 
    33import datetime 
     4import locale 
    45import wx 
    56import wx.lib.masked as masked 
     
    89try: 
    910    import decimal 
     11    numericTypes = (int, long, decimal.Decimal, float) 
     12    valueErrors = (ValueError, decimal.InvalidOperation) 
    1013except ImportError: 
    1114    # decimal is only in Python 2.4 or greater 
    1215    decimal = None 
    13  
    14 import dabo, dabo.ui 
     16    numericTypes = (int, long, float) 
     17    valueErrors = (ValueError, ) 
     18# Make this locale-independent 
     19decimalPoint = locale.localeconv()["decimal_point"] 
     20 
     21import dabo 
    1522if __name__ == "__main__": 
    1623    dabo.ui.loadUI("wx") 
     
    177184                self._addWindowStyleFlag(wx.TE_RIGHT) 
    178185            else: 
    179                 raise ValueError, "The only possible values are 'Left', 'Center', and 'Right'" 
     186                raise ValueError, _("The only possible values are 'Left', 'Center', and 'Right'") 
    180187            self.SetEditable(rw) 
    181188        else: 
     
    409416    def convertStringValueToDataType(self, strVal, dataType): 
    410417        """Given a string value and a type, return an appropriate value of that type. 
    411          
    412418        If the value can't be converted, a ValueError will be raised. 
    413         """         
     419        """ 
    414420        if dataType == bool: 
    415421            # Bools can't convert from string representations, because a zero- 
     
    429435                 
    430436            if retVal is None: 
    431                 raise ValueError, "String not in ISO 8601 format." 
     437                raise ValueError, _("String not in ISO 8601 format.") 
    432438        elif str(dataType) == "<type 'DateTime'>": 
    433439            # mx DateTime type. MySQLdb will use this if mx is installed. 
     
    436442                retVal = mx.DateTime.DateTimeFrom(str(strVal)) 
    437443            except ImportError: 
    438                 raise ValueError, "Can't import mx.DateTime" 
     444                raise ValueError, _("Can't import mx.DateTime") 
    439445        elif str(dataType) == "<type 'DateTimeDelta'>": 
    440446            # mx TimeDelta type. MySQLdb will use this for Time columns if mx is installed. 
     
    443449                retVal = mx.DateTime.TimeFrom(str(strVal)) 
    444450            except ImportError: 
    445                 raise ValueError, "Can't import mx.DateTime" 
    446         elif (decimal is not None and dataType == decimal.Decimal)
     451                raise ValueError, _("Can't import mx.DateTime") 
     452        elif decimal is not None and (dataType == decimal.Decimal) and self.StrictNumericEntry
    447453            try: 
    448454                _oldVal = self._oldVal 
     
    457463                    retVal = decimal.Decimal(strVal) 
    458464            except: 
    459                 raise ValueError, "Can't convert to decimal." 
     465                raise ValueError, _("Can't convert to decimal.") 
    460466        elif dataType in (tuple, list): 
    461467            retVal = eval(strVal) 
     468        elif not self.StrictNumericEntry and (dataType in numericTypes): 
     469            isint = (strVal.count(decimalPoint) == 0) and (strVal.lower().count("e") == 0) 
     470            try: 
     471                if isint: 
     472                    if strVal.strip().endswith("L"): 
     473                        retVal = long(strVal) 
     474                    else: 
     475                        retVal = int(strVal) 
     476                else: 
     477                    if decimal.Decimal in numericTypes: 
     478                        retVal = decimal.Decimal(strVal) 
     479                    else: 
     480                        retVal = float(strVal) 
     481            except valueErrors: 
     482                raise ValueError, _("Invalid Numeric Value: %s") % strVal 
    462483        else: 
    463484            # Other types can convert directly. 
     
    470491                # implemented, won't let the user get this far. Just keep the  
    471492                # old value. 
    472                 raise ValueError, "Can't convert." 
     493                raise ValueError, _("Can't convert.") 
    473494        return retVal 
    474495     
     
    497518        elif value is None: 
    498519            strVal = self.Application.NoneDisplay 
     520        elif isinstance(value, long): 
     521            strVal = value.__repr__() 
    499522        else: 
    500523            # convert all other data types to string: 
     
    555578    def _getStrictDateEntry(self): 
    556579        try: 
    557             v = self._strictDateEntry 
     580            ret = self._strictDateEntry 
    558581        except AttributeError: 
    559             v = self._strictDateEntry = False 
    560         return v 
     582            ret = self._strictDateEntry = False 
     583        return ret 
    561584     
    562585    def _setStrictDateEntry(self, val): 
     
    564587     
    565588     
     589    def _getStrictNumericEntry(self): 
     590        try: 
     591            ret = self._strictNumericEntry 
     592        except AttributeError: 
     593            ret = self._strictNumericEntry = True 
     594        return ret 
     595 
     596    def _setStrictNumericEntry(self, val): 
     597        if self._constructed(): 
     598            self._strictNumericEntry = val 
     599        else: 
     600            self._properties["StrictNumericEntry"] = val 
     601 
     602 
    566603    #Overrides the dTextBoxMixinBase getter and setters because of the data conversion 
    567604    #introduced in this class 
     
    672709            which will be coerced into sensible date values automatically.""")) 
    673710     
     711    StrictNumericEntry = property(_getStrictNumericEntry, _setStrictNumericEntry, None, 
     712            _("""When True, the DataType will be preserved across numeric types. When False, the  
     713            DataType will respond to user input to convert to the 'obvious' numeric type.   
     714            Default=True. (bool)""")) 
     715     
    674716    Value = property(_getValue, _setValue, None, 
    675717            _("Specifies the current state of the control (the value of the field). (varies)"))