Package Gnumed :: Package wxpython :: Module gmCharacterValidator
[frames] | no frames]

Source Code for Module Gnumed.wxpython.gmCharacterValidator

  1   
  2  try: 
  3          import wxversion 
  4          import wx 
  5  except ImportError: 
  6          from wxPython import wx 
  7   
  8  import string 
  9   
 10                   
11 -class CharValidator:
12 - def __init__(self):
13 self.enabled = 1 14 #self.special_upper = range(192,224) 15 #self.special_lower = range( 224, 256) 16 #self.lowercase = range(97, 123) 17 #self.uppercase = range(65, 91) 18 19 #self.all_lower = self.lowercase + self.special_lower 20 #self.all_upper = self.uppercase + self.special_upper 21 22 23 self.digits = range(48, 58) 24 self.print_other = range(18, 48) + range( 58 , 65) + range(91, 97) + range(123,126) 25 self.print_other.remove(32) 26 self.print_other.remove(39) 27 self.print_other.remove(45) 28 self.name_punctuation = [39, 45]
29 30 31
32 - def setSingleSpace(self, control):
33 wx.EVT_CHAR( control, self.allow_single_spaces)
34
35 - def setUpperAlpha(self, control):
36 wx.EVT_CHAR( control, self.allow_upper_only_exclusive)
37
38 - def setCapitalize(self, control):
39 wx.EVT_CHAR( control, self.capitalize_exclusive)
40
41 - def setDigits(self, control):
42 wx.EVT_CHAR( control, self.allow_digits_only)
43
44 - def allow_single_spaces(self, keyEvent):
45 """wrapper for handling single space input""" 46 if self._allow_single_spaces(keyEvent) == 0: 47 keyEvent.Skip()
48
49 - def _allow_single_spaces(self, keyEvent):
50 """intercepts space input and vetos extra spaces. 51 returns 1 if intercepted and Skip() was called or 0 if no event was handled. 52 """ 53 textCtrl = keyEvent.GetEventObject() 54 #allow tab to be processed , for navigation 55 if keyEvent.GetKeyCode() == WXK_TAB: 56 keyEvent.Skip(1) 57 return 1 58 59 if keyEvent.GetKeyCode() == WXK_SPACE and ( 60 textCtrl.GetInsertionPoint() == 0 61 or textCtrl.GetValue()[textCtrl.GetInsertionPoint()-1] == ' ' 62 ): 63 keyEvent.Skip(0) 64 return 1 65 66 return 0
67
68 - def allow_upper_only_exclusive(self, keyEvent):
69 """wrapper for converting to upper case and single spaces 70 converts intercepts lowercase input and puts in uppercase. 71 """ 72 #print "keyCode=", keyEvent.GetKeyCode() 73 if not self._allow_case_only(keyEvent, string.lowercase, string.uppercase, exclusiveof = self.print_other + self.digits): 74 keyEvent.Skip()
75
76 - def allow_lower_only(self, keyEvent):
77 """ wrapper to convert uppercase to lowercase. 78 """ 79 if not self._allow_case_only( self, keyEvent, string.uppercase, string.lowercase): 80 keyEvent.Skip()
81
82 - def allow_digits_only( self, keyEvent):
83 84 if self._allow_single_spaces(keyEvent): 85 return 86 87 k = keyEvent.GetKeyCode() 88 89 if chr(k) in string.letters or k in self.print_other or k in self.name_punctuation: 90 keyEvent.Skip(0) 91 return 92 93 keyEvent.Skip(1)
94 95 96
97 - def _allow_case_only( self, keyEvent, case, toCase, exclusiveof = []):
98 if self._allow_single_spaces(keyEvent): 99 return 1 100 101 #print "not processed by single space" 102 103 c = keyEvent.GetEventObject() 104 105 if chr(keyEvent.GetKeyCode()) in case: 106 #print "in range" 107 keyEvent.Skip(0) 108 109 p = c.GetInsertionPoint() 110 111 #print "insertion point =", p, " length of text=", len(c.GetValue()) 112 t = c.GetValue() 113 u = toCase[case.index(chr(keyEvent.GetKeyCode()))] 114 #print len(t),len(u) 115 #wxwidget's initial value bug. 116 #t = self._remove_init_whitespace_bug(t) 117 c.SetValue(t[:p]+u+t[p:]) 118 #print len(t),len(u) 119 #print p 120 c.SetInsertionPoint(p+1) 121 return 1 122 123 if exclusiveof <>[] and keyEvent.GetKeyCode() in exclusiveof: 124 keyEvent.Skip(0) 125 return 1 126 return 0
127
128 - def capitalize_exclusive(self, keyEvent):
129 if not self._capitalize(keyEvent): 130 if keyEvent.GetKeyCode() in self.print_other + self.digits : 131 keyEvent.Skip(0) 132 return 133 134 keyEvent.Skip(1)
135
136 - def _remove_init_whitespace_bug(self,t):
137 if (t.isspace() and len(t) == 1): 138 t = '' 139 return t
140
141 - def _capitalize(self, keyEvent):
142 if self._allow_single_spaces(keyEvent): 143 return 1 144 k = keyEvent.GetKeyCode() 145 if chr(k) in string.lowercase: 146 keyEvent.Skip(0) 147 c = keyEvent.GetEventObject() 148 p = c.GetInsertionPoint() 149 t = c.GetValue() 150 #wxwidget's initial value bug. 151 t = self._remove_init_whitespace_bug(t) 152 t = t[:p] + chr(k) +t[p:] 153 l = t.split(' ') 154 l = self.capitalize_list(l) 155 156 t = ' '.join(l) 157 158 c.SetValue(t) 159 c.SetInsertionPoint(p+1) 160 return 1 161 162 return 0
163
164 - def capitalize_list(self, l):
165 for i in xrange(len(l)): 166 w = l[i].capitalize() 167 if '-' in w: 168 l[i] = '-'.join(self.capitalize_list(w.split('-'))) 169 170 elif len(w) > 3 and w[:3] == 'Mac': 171 l[i] = w[:3] + w[3].upper() + w[4:] 172 elif len(w) > 2 and w[:2] == 'Mc': 173 l[i] = w[:2] + w[2].upper() + w[3:] 174 elif len(w) > 2 and w[1] == "'": 175 l[i] = w[:2] + w[2].upper() + w[3:] 176 else: 177 l[i] =w 178 return l
179 180 181 if __name__ == "__main__": 182 183 184 try: 185 import wxversion 186 import wx 187 except ImportError: 188 from wxPython import wx 189 190
191 - class REWResizer:
192 - def __init__(self, txtCtrl):
193 self.c = txtCtrl 194 wx.EVT_TEXT( txtCtrl, txtCtrl.GetId(), self.checkSize)
195
196 - def checkSize(self, event):
197 #print self.c, self.c.GetNumberOfLines(), self.c.GetTextExtent('A') 198 yc = self.c.GetTextExtent('X')[1] 199 y = self.c.GetNumberOfLines() * yc 200 x0, y0 = self.c.GetClientSizeTuple() 201 print y, y0 202 if y > y0 : 203 self.c.SetClientSize( (x0, y0 + yc )) 204 print "window", self.c.GetId(), "size changed" 205 self.adjustParentsChildren(yc) 206 207 c = self.c 208 while c <> None: 209 if c.GetParent() is None: 210 (w,h) = c.GetSizeTuple() 211 c.SetSize( (w, h + yc)) 212 break 213 c = c.GetParent()
214
215 - def adjustParentsChildren(self, yc):
216 c = self.c.GetParent() 217 l = c.GetChildren() 218 for w in l: 219 (x,y) = w.GetPositionTuple() 220 if y > self.c.GetPositionTuple()[1]: 221 w.SetPosition( ( x, y + yc) )
222 223
224 - class REPanel(wx.Panel):
225 - def __init__(self, parent, id):
226 wx.Panel.__init__(self, parent, id) 227 szr1 = wx.BoxSizer(wx.VERTICAL) 228 self.wmap = {} 229 self.smap = {} 230 #for i in range(0,4): 231 # szr1.AddGrowableRow(i) 232 233 labels = [ 'S', 'O', 'A', 'P' ] 234 for i in range(0,4): 235 self.getLine(labels[i],szr1) 236 szr1.Add(1,1) 237 szr1.Add(1,1) 238 self.SetSizer(szr1) 239 szr1.Fit(self)
240 241
242 - def getLine( self, text, szr):
243 szr2 = wx.BoxSizer(wx.HORIZONTAL) 244 szr2.Add(wx.StaticText(self, -1, text) ) 245 #ctrl = wxTextCtrl(self, -1, "") 246 ctrl = wx.TextCtrl(self, -1, "\n", wx.DefaultPosition, 247 wx.DefaultSize, wx.TE_MULTILINE ) 248 self.wmap[text] = ctrl 249 szr2.Add(ctrl, 1, wx.GROW ) 250 self.smap[text] = REWResizer(ctrl) # hold on so not garbage collected 251 szr.Add(szr2, 1, wx.GROW) 252 return szr
253
254 - class REApp(wx.App):
255
256 - def OnInit(self):
257 f = REFrame(None, -1, "Richards XPEditor") 258 f.Show(true) 259 f.p.SetSizer(None) 260 self.SetTopWindow(f) 261 self.f = f 262 return true
263
264 - def getSOAPMap(self):
265 return self.f.p.wmap
266 267 268 269 app = REApp() 270 v = CharValidator() 271 272 m = app.getSOAPMap() 273 v.setSingleSpace(m['S']) 274 v.setUpperAlpha(m['O']) 275 v.setCapitalize(m['A']) 276 v.setDigits(m['P']) 277 278 279 app.MainLoop() 280