fix from Evaldas
[docutils.git] / sandbox / dugui / validator.py
blobd7c5fc55153fbc196c545db02b5ab28fadd875fe
1 # -*- coding: UTF-8 -*-
4 # validator.py
6 # Copyright (C) 2003-2006 Facundo Batista <facundo@taniquetil.com.ar>
7 # Copyright (C) 2003-2006 Mariano Draghi <chaghi@sion.com>
9 # This file is placed under the Python 2.3 license, see
10 # http://www.python.org/2.3/license.html
14 class ValidationSupervisor(object):
15 def __init__(self, statusBar, name):
16 self._statusBar = statusBar
17 self._externals = []
18 self._actions = []
19 self._editors = {}
20 self._editMessages = {}
21 self._name = name
23 def registerAction(self, action):
24 self._actions.append(action)
25 if self._allOK():
26 action.Enable(True)
27 else:
28 action.Enable(False)
29 return
31 def registerEditor(self, editor, message, initialState=None):
32 # as it's a dictionary, creating and setting is the same...
33 self._editMessages[editor] = message
34 self.setEditorState(editor, initialState)
35 return
37 def setEditorState(self, editor, newState):
38 self._editors[editor] = newState
39 self.checkValidations()
40 return
42 def checkValidations(self):
43 # print "check validations in vs", self._name
44 # check what to do
45 if self._allOK():
46 state = True
47 else:
48 state = False
49 # print "result:", state
51 # just do it in all the registered actions
52 for action in self._actions:
53 f = getattr(action, "Enable")
54 f(state)
55 return
57 def registerExternalValidator(self, validator, message):
58 f = getattr(validator, "validate")
59 self._externals.append((f, message))
60 return
62 def _allOK(self):
63 # check editors
64 for (editor, state) in self._editors.items():
65 # print "checking editor", editor, state
66 if state is None:
67 return False
68 if state == False:
69 self._statusBar(self._editMessages[editor])
70 return False
72 # check external validators
73 for (validator, message) in self._externals:
74 # print "checking external", editor, state
75 result = validator()
76 # print "it gave:", result
77 if result is None:
78 return False
79 if result == False:
80 self._statusBar(message)
81 return False
83 # everything is fine
84 self._statusBar("")
85 return True
88 # All external validators must have a "validate" method.
90 # If that method returns ... the supervisor will ...:
91 # True: keep looking in the next validators, if all returns True, clears the statusBar and enables the controllers
92 # False: shows the configured error message in the statusBar and disables the controllers
93 # None: does not touch the statusBar and disables the controllers
95 class ExistAccountValidator(object):
96 def __init__(self, entriesReport):
97 self.entriesReport = entriesReport
99 def validate(self):
100 if self.entriesReport.cuenta is None:
101 return None
102 return True
104 class DateFromToValidator(object):
105 def __init__(self, dateSelectorFrom, dateSelectorTo):
106 self.dsFrom = dateSelectorFrom
107 self.dsTo = dateSelectorTo
109 def validate(self):
110 dateFrom = self.dsFrom.GetDate()
111 dateTo = self.dsTo.GetDate()
112 if not self.dsFrom.IsEnabled() and not self.dsTo.IsEnabled():
113 return True
114 if dateFrom is None or dateTo is None:
115 return None
117 return dateFrom <= dateTo