Explorar o código

wxGUI/mapwin: add map overlays 'at' parameter arg validation (#1069)

Tomas Zigo %!s(int64=4) %!d(string=hai) anos
pai
achega
186b98a7f6
Modificáronse 2 ficheiros con 88 adicións e 22 borrados
  1. 24 7
      gui/wxpython/gui_core/forms.py
  2. 64 15
      gui/wxpython/gui_core/widgets.py

+ 24 - 7
gui/wxpython/gui_core/forms.py

@@ -101,7 +101,10 @@ from gui_core import gselect
 from core import gcmd
 from core import utils
 from core.settings import UserSettings
-from gui_core.widgets import FloatValidator, GNotebook, FormNotebook, FormListbook
+from gui_core.widgets import (
+    FloatValidator, FormListbook, FormNotebook, GNotebook,
+    PlacementValidator,
+)
 from core.giface import Notification, StandaloneGrassInterface
 from gui_core.widgets import LayersList
 from gui_core.wrap import BitmapFromImage, Button, CloseButton, StaticText, \
@@ -1236,9 +1239,16 @@ class CmdPanel(wx.Panel):
                                 max=maxValue)
                             style = wx.BOTTOM | wx.LEFT
                         else:
-                            txt2 = TextCtrl(
-                                parent=which_panel, value=p.get(
-                                    'default', ''))
+                            if p['name'] in ('at'):
+                                txt2 = TextCtrl(
+                                    parent=which_panel, value=p.get(
+                                        'default', ''),
+                                    validator=PlacementValidator(
+                                        num_of_params=len(p['key_desc'])))
+                            else:
+                                txt2 = TextCtrl(
+                                    parent=which_panel, value=p.get(
+                                        'default', ''))
                             style = wx.EXPAND | wx.BOTTOM | wx.LEFT
 
                         value = self._getValue(p)
@@ -1313,9 +1323,16 @@ class CmdPanel(wx.Panel):
                 if p.get('multiple', False) or \
                         p.get('type', 'string') == 'string' or \
                         len(p.get('key_desc', [])) > 1:
-                    win = TextCtrl(
-                        parent=which_panel, value=p.get(
-                            'default', ''))
+                    if p['name'] in ('at'):
+                        win = TextCtrl(
+                            parent=which_panel, value=p.get(
+                                'default', ''),
+                            validator=PlacementValidator(
+                                num_of_params=len(p['key_desc'])))
+                    else:
+                        win = TextCtrl(
+                            parent=which_panel, value=p.get(
+                                'default', ''))
 
                     value = self._getValue(p)
                     if value:

+ 64 - 15
gui/wxpython/gui_core/widgets.py

@@ -22,6 +22,7 @@ Classes:
  - widgets::GenericValidator
  - widgets::GenericMultiValidator
  - widgets::LayersListValidator
+ - widgets::PlacementValidator
  - widgets::GListCtrl
  - widgets::SearchModuleWidget
  - widgets::ManageSettingsWidget
@@ -44,6 +45,8 @@ This program is free software under the GNU General Public License
 @author Anna Kratochvilova <kratochanna gmail.com> (Google SoC 2011)
 @author Stepan Turek <stepan.turek seznam.cz> (ManageSettingsWidget - created from GdalSelect)
 @author Matej Krejci <matejkrejci gmail.com> (Google GSoC 2014; EmailValidator, TimeISOValidator)
+@author Tomas Zigo <tomas.zigo slovanet.sk> (LayersListValidator,
+PlacementValidator)
 """
 
 import os
@@ -576,14 +579,13 @@ class BaseValidator(Validator):
 
     def OnText(self, event):
         """Do validation"""
-        self.Validate()
+        self.Validate(win=event.GetEventObject())
 
         event.Skip()
 
-    def Validate(self):
+    def Validate(self, win):
         """Validate input"""
-        textCtrl = self.GetWindow()
-        text = textCtrl.GetValue()
+        text = win.GetValue()
 
         if text:
             try:
@@ -599,7 +601,6 @@ class BaseValidator(Validator):
         textCtrl = self.GetWindow()
 
         textCtrl.SetBackgroundColour("grey")
-        textCtrl.SetFocus()
         textCtrl.Refresh()
 
     def _valid(self):
@@ -624,11 +625,9 @@ class CoordinatesValidator(BaseValidator):
     def __init__(self):
         BaseValidator.__init__(self)
 
-    def Validate(self):
+    def Validate(self, win):
         """Validate input"""
-
-        textCtrl = self.GetWindow()
-        text = textCtrl.GetValue()
+        text = win.GetValue()
         if text:
             try:
                 text = text.split(',')
@@ -681,10 +680,9 @@ class EmailValidator(BaseValidator):
     def __init__(self):
         BaseValidator.__init__(self)
 
-    def Validate(self):
+    def Validate(self, win):
         """Validate input"""
-        textCtrl = self.GetWindow()
-        text = textCtrl.GetValue()
+        text = win.GetValue()
         if text:
             if re.match(r'\b[\w.-]+@[\w.-]+.\w{2,4}\b', text) is None:
                 self._notvalid()
@@ -704,10 +702,9 @@ class TimeISOValidator(BaseValidator):
     def __init__(self):
         BaseValidator.__init__(self)
 
-    def Validate(self):
+    def Validate(self, win):
         """Validate input"""
-        textCtrl = self.GetWindow()
-        text = textCtrl.GetValue()
+        text = win.GetValue()
         if text:
             try:
                 datetime.strptime(text, '%Y-%m-%d')
@@ -1025,6 +1022,58 @@ class LayersListValidator(GenericValidator):
         return True
 
 
+class PlacementValidator(BaseValidator):
+    """Validator for placement input (list of floats separated by comma)"""
+
+    def __init__(self, num_of_params):
+        self._num_of_params = num_of_params
+        super().__init__()
+
+    def _enableDisableBtn(self, enable):
+        """Enable/Disable buttomn
+
+        :param bool enable: Enable/Disable btn
+        """
+        win = self.GetWindow().GetTopLevelParent()
+        for btn_id in (wx.ID_OK, wx.ID_APPLY):
+            btn = win.FindWindow(id=btn_id)
+            if btn:
+                btn.Enable(enable)
+
+    def _valid(self):
+        super()._valid()
+        self._enableDisableBtn(enable=True)
+
+    def _notvalid(self):
+        super()._notvalid()
+        self._enableDisableBtn(enable=False)
+
+    def Validate(self, win):
+        """Validate input"""
+        text = win.GetValue()
+        if text:
+            try:
+                text = text.split(',')
+
+                for t in text:
+                    float(t)
+
+                if len(text) % self._num_of_params != 0:
+                    self._notvalid()
+                    return False
+
+            except ValueError:
+                self._notvalid()
+                return False
+
+        self._valid()
+        return True
+
+    def Clone(self):
+        """Clone validator"""
+        return PlacementValidator(num_of_params=self._num_of_params)
+
+
 class GListCtrl(ListCtrl, listmix.ListCtrlAutoWidthMixin,
                 CheckListCtrlMixin):
     """Generic ListCtrl with popup menu to select/deselect all