Changeset 2898

Show
Ignore:
Timestamp:
03/09/07 16:47:01 (2 years ago)
Author:
nate
Message:

This is a result of a request made by John F for dTextBox objects to have the ability to limit the amount of characters that can be entered. I added a TextLength? property that will dynamically adjust the value in a way similar to the ForceCase? Property.

I created a full suite of unit tests for this property. The only thing the UT doesn't do is check for the dynamic updating when a user enters characters in a text box. I checked that by hand and it seems to work. Also, I checked that it was playing well with ForceCase? and everything seemed to be working well. However, I would encourage people to play with this and report any bugs like usual.

This only affects string textboxes. I figured it would be useful so you don't pass a bizobj a 10 char string when the db field is 2 characters long. Let me know what you think.

Files:

Legend:

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

    r2789 r2898  
    2929        self._forceCase = None 
    3030        self._inForceCase = False 
     31        self._textLength = None 
     32        self._inTextLength = False 
    3133 
    3234        preClass = wx.PreTextCtrl 
     
    214216                or keyChar in """,./<>?;':"[]\\{}|`~!@#$%%^&*()-_=+"""): 
    215217            dabo.ui.callAfter(self.__forceCase) 
    216          
     218            dabo.ui.callAfter(self.__textLength) 
     219     
     220    def __textLength(self): 
     221        """If the TextLength property is set, checks the current value of the control 
     222        and truncates it if too long""" 
     223        if not isinstance(self.Value, basestring): 
     224            #Don't bother if it isn't a string type 
     225            return 
     226        length = self.TextLength 
     227        if not length: 
     228            return 
     229         
     230        insPos = self.InsertionPosition 
     231         
     232        self._inTextLength = True 
     233        if len(self.Value) > length: 
     234            self.Value = self.Value[:length] 
     235            if insPos > length: 
     236                self.InsertionPosition = length 
     237            else: 
     238                self.InsertionPosition = insPos 
     239            self.refresh() 
     240        self._inTextLength = False 
    217241     
    218242    def __forceCase(self): 
     
    285309            self.__forceCase() 
    286310            self.unbindEvent(dEvents.KeyChar, self.__onKeyChar) 
    287             if self._forceCase
     311            if self._forceCase or self._textLength
    288312                self.bindEvent(dEvents.KeyChar, self.__onKeyChar) 
    289313        else: 
     
    369393    def _setStrictDateEntry(self, val): 
    370394        self._strictDateEntry = bool(val) 
     395 
     396 
     397    def _getTextLength(self): 
     398        return self._textLength 
     399 
     400    def _setTextLength(self, val): 
     401        if val == None: 
     402            self._textLength = None 
     403        else: 
     404            val = int(val) 
     405            if val < 1: 
     406                raise ValueError, 'TextLength must be a positve Integer' 
     407            self._textLength = val 
     408        self.__textLength() 
     409         
     410        self.unbindEvent(dEvents.KeyChar, self.__onKeyChar) 
     411        if self._forceCase or self._textLength: 
     412            self.bindEvent(dEvents.KeyChar, self.__onKeyChar) 
    371413 
    372414 
     
    421463            else: 
    422464                dabo.ui.callAfter(self.__forceCase) 
    423          
     465            
    424466            strVal = self.getStringValue(val) 
    425467            _oldVal = self._oldVal = self.Value 
     
    440482        else: 
    441483            self._properties["Value"] = val 
     484         
     485        self.__textLength() 
    442486 
    443487         
     
    488532            If not strict, dates can be accepted in YYYYMMDD, YYMMDD, and MMDD format, 
    489533            which will be coerced into sensible date values automatically.""")) 
     534     
     535    TextLength = property(_getTextLength, _setTextLength, None, 
     536            _("""The maximum length the entered text can be. (int)""")) 
    490537 
    491538    Value = property(_getValue, _setValue, None, 
  • trunk/tests/TestCaseTemplate.py

    r2857 r2898  
    1616    pass 
    1717 
    18 def suite(): 
    19     classList = [TestCaseSample] 
    20     return unittest.TestSuite(classList) 
    21  
    2218if __name__ == "__main__": 
    2319    unittest.main() 
  • trunk/tests/unitTests/masterTestSuite.py

    r2884 r2898  
    1717 
    1818import dabo 
     19dabo.ui.loadUI('wx') 
     20 
    1921import db 
    2022import biz 
     
    3537 
    3638coverage.stop() 
    37 coverage.report([dabo.dColors, dabo.dObject, dabo]) 
     39#You can uncomment this to get test coverage on a particular module, but if you want to 
     40#see the entire report for dabo, run "python CoverageReport.py".  I would pipe it to a file though 
     41#coverage.report([dabo.dColors, dabo.dObject, dabo]) 
  • trunk/tests/unitTests/ui/UIwx/__init__.py

    r2857 r2898  
    1212 
    1313#import TestCase suites and add to list here 
    14  
     14import Test_dTextBox 
     15suiteList.append(unittest.TestLoader().loadTestsFromModule(Test_dTextBox)) 
    1516 
    1617#setup a suite and return it