Source code for validators
'''
Validators
by Patrick Ruoff in 2012
'''
from transfer import CTransfer
import wx
[docs]class CValidator(wx.PyValidator):
""" Generic validator, mediates between dialog_data and the associated control """
dialog_data = None;
def __init__(self, dialog_data = None):
super(CValidator, self).__init__();
self.dialog_data = dialog_data;
[docs] def SetData(self, dialog_data):
self.dialog_data = dialog_data;
# PyValidator functions
[docs] def Clone(self):
return CValidator(self.dialog_data);
[docs] def Validate(self, win):
return True
[docs] def TransferToWindow(self):
return True
[docs] def TransferFromWindow(self):
return True
# utility functions
[docs] def GetCtrlValueAs(self, t):
""" Tries to interpret the control's value as type *t* """
ctrl = self.GetWindow();
if issubclass(type(ctrl), wx.TextCtrl) or \
issubclass(type(ctrl), wx.Slider) or \
issubclass(type(ctrl), wx.SpinCtrl) or \
issubclass(type(ctrl), wx.CheckBox):
return t(ctrl.GetValue());
elif issubclass(type(ctrl), wx.ControlWithItems):
return t(ctrl.GetSelection());
elif issubclass(type(ctrl), wx.RadioBox):
return t(ctrl.GetSelection());
[docs] def SetCtrlValue(self, value):
""" Tries to set the control's value to *value* """
ctrl = self.GetWindow();
if issubclass(type(ctrl), wx.Slider) or \
issubclass(type(ctrl), wx.SpinCtrl):
ctrl.SetValue(int(value));
elif issubclass(type(ctrl), wx.TextCtrl):
ctrl.SetValue(str(value));
elif issubclass(type(ctrl), wx.ControlWithItems):
ctrl.SetSelection(int(value));
elif issubclass(type(ctrl), wx.CheckBox):
ctrl.SetValue(bool(value));
[docs] def ShowError(self, error_string):
""" Indicate an error, try to present *error_string* to the user """
ctrl = self.GetWindow();
ctrl.SetToolTipString(error_string);
if issubclass(type(ctrl), wx.TextCtrl) or \
issubclass(type(ctrl), wx.SpinCtrl):
ctrl.SetBackgroundColour("pink");
ctrl.SetFocus();
ctrl.Refresh();
[docs] def ResetError(self):
""" Reset error status """
ctrl = self.GetWindow();
ctrl.SetToolTipString("");
if issubclass(type(ctrl), wx.TextCtrl) or \
issubclass(type(ctrl), wx.SpinCtrl):
ctrl.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW));
ctrl.Refresh();
[docs]class CSingleItemValidator(CValidator):
""" Validator that mediates between the control and a single data item by using transfer functions """
data_item_key = None;
transfer = None;
def __init__(self, data_item_key, transfer = None, dialog_data = None):
""" Use the transfer function *transfer* to mediate between the data item at *data_item_key*
and the control's value """
super(CSingleItemValidator, self).__init__(dialog_data);
self.data_item_key = data_item_key;
if transfer == None:
self.transfer = CTransfer();
else:
self.transfer = transfer;
[docs] def SetTransfer(self, transfer):
self.transfer = transfer;
[docs] def Clone(self):
return CSingleItemValidator(self.data_item_key, self.transfer, self.dialog_data);
[docs] def TransferToWindow(self):
try:
self.SetCtrlValue(self.transfer.ToWindow(self.dialog_data, self.data_item_key));
except:
return False;
return True;
[docs] def TransferFromWindow(self):
data_type = type(self.dialog_data[self.data_item_key]);
try:
cval = self.GetCtrlValueAs(data_type);
except:
self.ShowError("Cannot parse entered value as "+str(data_type));
return False;
ret = self.transfer.FromWindow(self.dialog_data, self.data_item_key ,cval);
if ret:
self.ResetError();
else:
self.ShowError("Entered value did not respect the constraints")
return ret;