Browse Source

wxGUI: new validators (EmailValidator, TimeISOValidator) by krejcmat (GSoC 2014)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61510 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 10 years ago
parent
commit
8a44ca3a27
1 changed files with 50 additions and 0 deletions
  1. 50 0
      gui/wxpython/gui_core/widgets.py

+ 50 - 0
gui/wxpython/gui_core/widgets.py

@@ -14,6 +14,8 @@ Classes:
  - widgets::CoordinatesValidator
  - widgets::CoordinatesValidator
  - widgets::IntegerValidator
  - widgets::IntegerValidator
  - widgets::FloatValidator
  - widgets::FloatValidator
+ - widgets::EmailValidator
+ - widgets::TimeISOValidator
  - widgets::GListCtrl
  - widgets::GListCtrl
  - widgets::SearchModuleWidget
  - widgets::SearchModuleWidget
  - widgets::ManageSettingsWidget
  - widgets::ManageSettingsWidget
@@ -22,6 +24,9 @@ Classes:
  - widgets::BarscalesComboBox
  - widgets::BarscalesComboBox
  - widgets::NArrowsComboBox
  - widgets::NArrowsComboBox
 
 
+@todo:
+ - move validators to a separate file gui_core/validators.py
+
 (C) 2008-2014 by the GRASS Development Team
 (C) 2008-2014 by the GRASS Development Team
 
 
 This program is free software under the GNU General Public License
 This program is free software under the GNU General Public License
@@ -31,6 +36,7 @@ This program is free software under the GNU General Public License
 @author Enhancements by Michael Barton <michael.barton asu.edu>
 @author Enhancements by Michael Barton <michael.barton asu.edu>
 @author Anna Kratochvilova <kratochanna gmail.com> (Google SoC 2011)
 @author Anna Kratochvilova <kratochanna gmail.com> (Google SoC 2011)
 @author Stepan Turek <stepan.turek seznam.cz> (ManageSettingsWidget - created from GdalSelect)
 @author Stepan Turek <stepan.turek seznam.cz> (ManageSettingsWidget - created from GdalSelect)
+@author Matej Krejci <matejkrejci gmail.com> (Google GSoC 2014; EmailValidator, TimeISOValidator)
 """
 """
 
 
 import os
 import os
@@ -586,6 +592,50 @@ class FloatValidator(BaseValidator):
         """Clone validator"""
         """Clone validator"""
         return FloatValidator()
         return FloatValidator()
 
 
+class EmailValidator(BaseValidator):
+    """Validator for email input"""
+    def __init__(self):
+        BaseValidator.__init__(self)
+ 
+    def Validate(self):
+        """Validate input"""
+        textCtrl = self.GetWindow()
+        text = textCtrl.GetValue()
+        if text:
+            if re.match(r'\b[\w.-]+@[\w.-]+.\w{2,4}\b', text) is None:
+                self._notvalid()
+                return False
+        
+        self._valid()
+        return True
+ 
+    def Clone(self):
+        """Clone validator"""
+        return EmailValidator()
+ 
+class TimeISOValidator(BaseValidator):
+    """Validator for time ISO format (YYYY-MM-DD) input"""
+    def __init__(self):
+        BaseValidator.__init__(self)
+    
+    def Validate(self):
+        """Validate input"""
+        textCtrl = self.GetWindow()
+        text = textCtrl.GetValue()
+        if text:
+            try:
+                datetime.strptime(text, '%Y-%m-%d')
+            except:
+                self._notvalid()
+                return False
+        
+        self._valid()
+        return True
+        
+    def Clone(self):
+        """Clone validator"""
+        return TimeISOValidator()
+
 class NTCValidator(wx.PyValidator):
 class NTCValidator(wx.PyValidator):
     """validates input in textctrls, taken from wxpython demo"""
     """validates input in textctrls, taken from wxpython demo"""
     def __init__(self, flag = None):
     def __init__(self, flag = None):