Skip to content

Commit

Permalink
wxGUI/mapwin: add map overlays 'at' parameter arg validation (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmszi committed Feb 17, 2021
1 parent 5c86956 commit 186b98a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 22 deletions.
31 changes: 24 additions & 7 deletions gui/wxpython/gui_core/forms.py
Expand Up @@ -101,7 +101,10 @@
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, \
Expand Down Expand Up @@ -1236,9 +1239,16 @@ def __init__(self, parent, giface, task, id=wx.ID_ANY,
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)
Expand Down Expand Up @@ -1313,9 +1323,16 @@ def __init__(self, parent, giface, task, id=wx.ID_ANY,
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:
Expand Down
79 changes: 64 additions & 15 deletions gui/wxpython/gui_core/widgets.py
Expand Up @@ -22,6 +22,7 @@
- widgets::GenericValidator
- widgets::GenericMultiValidator
- widgets::LayersListValidator
- widgets::PlacementValidator
- widgets::GListCtrl
- widgets::SearchModuleWidget
- widgets::ManageSettingsWidget
Expand All @@ -44,6 +45,8 @@
@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
Expand Down Expand Up @@ -576,14 +579,13 @@ def __init__(self):

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:
Expand All @@ -599,7 +601,6 @@ def _notvalid(self):
textCtrl = self.GetWindow()

textCtrl.SetBackgroundColour("grey")
textCtrl.SetFocus()
textCtrl.Refresh()

def _valid(self):
Expand All @@ -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(',')
Expand Down Expand Up @@ -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()
Expand All @@ -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')
Expand Down Expand Up @@ -1025,6 +1022,58 @@ def Validate(self, win, validate_all=False):
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
Expand Down

0 comments on commit 186b98a

Please sign in to comment.