forked from ryandoherty/RaceCapture_App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaluefield.py
76 lines (59 loc) · 2.66 KB
/
valuefield.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import re
from kivy.uix.textinput import TextInput
from kivy.properties import NumericProperty
from kivy.core.window import Window
class ValueField(TextInput):
def __init__(self, *args, **kwargs):
self.next = kwargs.pop('next', None)
self.multiline = False
super(ValueField, self).__init__(*args, **kwargs)
def on_focus(self, instance, value):
if value:
Window.bind(on_keyboard=self._on_keyboard)
else:
Window.unbind(on_keyboard=self._on_keyboard)
self.dispatch('on_text_validate')
def set_next(self, next):
self.next = next
def _on_keyboard(self, keyboard, keycode, *args):
if keycode == 9: #tab
self.next.focus = True
self.dispatch('on_text_validate')
class TextValueField(ValueField):
max_len = NumericProperty(100)
def insert_text(self, substring, from_undo=False):
if len(self.text) < self.max_len:
super(TextValueField, self).insert_text(substring, from_undo=from_undo)
class NumericValueField(ValueField):
min_value = NumericProperty(None, allownone=True)
max_value = NumericProperty(None, allownone=True)
def __init__(self, *args, **kwargs):
super(NumericValueField, self).__init__(*args, **kwargs)
self.bind(on_text_validate=self.validate_minmax)
def validate_minmax(self, *args):
try:
value = int(self.text)
if self.min_value is not None and value < self.min_value:
self.text = str(self.min_value)
if self.max_value is not None and value > self.max_value:
self.text = str(self.max_value)
except:
pass
class IntegerValueField(NumericValueField):
pat = re.compile('[^0-9]')
def insert_text(self, substring, from_undo=False):
if '-' in substring and not '-' in self.text:
return super(IntegerValueField, self).insert_text(substring, from_undo=from_undo)
s = re.sub(self.pat, '', substring)
super(IntegerValueField, self).insert_text(s, from_undo=from_undo)
class FloatValueField(NumericValueField):
pat = re.compile('[^0-9]')
def insert_text(self, substring, from_undo=False):
if '-' in substring and not '-' in self.text:
return super(FloatValueField, self).insert_text(substring, from_undo=from_undo)
pat = self.pat
if '.' in self.text:
s = re.sub(pat, '', substring)
else:
s = '.'.join([re.sub(pat, '', s) for s in substring.split('.', 1)])
super(FloatValueField, self).insert_text(s, from_undo=from_undo)