This commit is contained in:
yumoqing 2022-01-27 11:08:13 +08:00
parent 4f81e6e8a7
commit b70e2def12
11 changed files with 365 additions and 0 deletions

View File

@ -0,0 +1,10 @@
from .factory import *
from .date import *
from .time import *
from .int import *
from .float import *
from .amount import *
from .password import *
from .text import *
from .code import *

View File

@ -0,0 +1,84 @@
from ..baseWidget import FloatInput
from ..utils import *
from .float import FloatView
from .factory import UiFactory, get_value
def amountstr(amount,dec):
f = '%%.0%df' % dec
s = f % amount
parts = s.split('.')
p_int = parts[0]
subs = []
if p_int[0] == '-':
subs.append('-')
p_int = p_int[1:]
l = [i for i in p_int]
l.reverse()
k = []
for i,c in enumerate(l):
if i and (i) % 3 == 0:
k.append(',')
k.append(c)
k.reverse()
subs.append(''.join(k))
if len(parts) > 1:
subs.append(f'.{parts[1]}')
return ''.join(subs)
class AmountView(FloatView):
def on_value(self, *args):
if self.value is None:
self.text = self.nullstr
else:
self.text = self.amount_str()
def amount_str(self):
return amountstr(self.value, self.desc)
def builder(self, desc, rec=None):
v = get_value(desc, rec=rec)
defaultvalue = desc.get('nullstr', '')
return AmountView(value=v,
dec=desc.get('dec', 2),
nullstr=defaultvale,
font_size=CSize(1),
halign='right',
valign='middle'
)
class AmountInput(FloatInput):
def input_filter(self,substring, from_undo=False):
s = super(AmountInput,self).filter(substring)
a = s.split('.')
b = a[0]
if len(b)>3:
k = []
while len(b)>3:
x = b[-3:]
k.insert(0,x)
b = b[:-3]
a[0] = ','.join(k)
s = '.'.join(a)
return '.'.join(a)
def insert_text(self, substring, from_undo=False):
x = StrInput.insert_text(self,s, from_undo=from_undo)
y = amountstr(x, self.dec)
return y
def getValue(self):
txt = ''.join(self.text.split(','))
return float(txt)
@classmethod
def builder(self, desc, rec=None):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
w = AmountInput(**kw)
if v is not None:
w.setValue(v)
return w
UiFactory.register('amount', AmountInput.builder, AmountView.builder)

View File

28
kivyblocks/uitype/code.py Normal file
View File

@ -0,0 +1,28 @@
from ..baseWidget import Text, SelectInput
from ..utils import *
from .factory import UiFactory, get_value
def build_input_code_widget(desc, rec=None):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
w = SelectInput(**kw)
if v:
w.setValue(v)
return w
def build_view_code_widget(desc, rec=None):
tf = desc.get('textField','text')
vf = desc.get('valueField','value')
v = rec.get(tf,rec.get(vf, ' '))
return Text(text=v,
i18n=True,
font_size = CSize(1),
wrap=True,
halign='center',
valign='middle'
)
UiFactory.register('code', build_input_code_widget, build_view_code_widget)

15
kivyblocks/uitype/date.py Normal file
View File

@ -0,0 +1,15 @@
from ..utils import *
from ..dateinput import DateInput
from .factory import UiFactory, StringView, get_value
def build_input_date_widget(desc, rec=None):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
w = DateInput(**kw)
if v:
w.setValue(v)
return w
UiFactory.register('date', build_input_date_widget, StringView.builder)

View File

@ -0,0 +1,67 @@
from kivy.uix.label import Label
from kivyblocks.widgetExt.inputext import StrInput
from ..utils import *
view_widget_builders = {
}
input_widget_builders = {
}
def get_value(desc, rec=None):
if rec:
return rec.get(desc['name'])
return desc.get('value', desc.get('defaultvalue', ''))
class UiFactory:
@classmethod
def register(self, ut, inpf, viewf):
view_widget_builders.update({ut:viewf})
input_widget_builders.update({ut:inpf})
@classmethod
def build_view_widget(self, desc, rec=None):
ut = desc.get('uitype', 'str')
f = view_widget_builders.get(ut)
return f(desc, rec=rec)
@classmethod
def build_input_widget(self, desc, rec=None):
ut = desc.get('uitype', 'str')
f = input_widget_builders.get(ut)
return f(desc, rec=rec)
class StringView(Label):
@classmethod
def builder(self, desc, rec):
txt = get_value(desc, rec=rec)
return StringView(text=str(txt) if txt else '',
font_size=CSize(1),
halign='left',
valign='middle')
def __init__(self, **kw):
super(StringView, self).__init__(**kw)
self.wrap = True
self.halign = 'left'
self.valign = 'middle'
def getValue(self):
return self.text
def setValue(self, v):
self.text = str(v)
class StringInput(StrInput):
@classmethod
def builder(self, desc, rec):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
kw['multiline'] = False
w = StringInput(**kw)
if v:
w.setValue(v)
return w
UiFactory.register('str', StringInput.builder, StringView.builder)

View File

@ -0,0 +1,51 @@
from kivy.uix.label import Label
from kivy.properties import NumericProperty, StringProperty
from ..baseWidget import FloatInput
from ..utils import *
from .factory import UiFactory, get_value
class FloatView(Label):
value = NumericProperty(None)
nullstr = StringProperty('')
dec = NumericProperty(2)
def __init__(self, **kw):
super(FloatView, self).__init__(text=self.nullstr, **kw)
self.wrap = True
self.halign = 'right'
self.valign = 'middle'
def on_value(self, *args):
if self.value is None:
self.text = self.bullstr
f = '%%.0%df' % self.dec
self.text = f % self.value
def getValue(self):
return self.value
def setValue(self, v):
self.value = v
@classmethod
def builder(self, desc, rec=None):
v = get_value(desc, rec=rec)
return FloatView(value=v,
dec=desc.get('dec', 2),
nullstr=desc.get('nullstr',''),
font_size=CSize(1),
halign='right',
valign='middle'
)
def build_input_float_widget(desc, rec=None):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
w = FloatInput(**kw)
if v is not None:
w.setValue(v)
return w
UiFactory.register('float', build_input_float_widget, FloatView.builder)

48
kivyblocks/uitype/int.py Normal file
View File

@ -0,0 +1,48 @@
from kivy.uix.label import Label
from kivy.properties import NumericProperty, StringProperty
from ..baseWidget import IntegerInput
from ..utils import *
from .factory import UiFactory, StringView
class IntView(Label):
value = NumericProperty(None)
nullstr = StringProperty('')
def __init__(self, **kw):
super(IntView, self).__init__(text='', **kw)
self.wrap = True
self.halign = 'right'
self.valign = 'middle'
def on_value(self, *args):
if self.value is not None:
self.text = str(v)
else:
self.text = self.nullstr
def setValue(self, v):
self.value = v
def getValue(self):
return self.value
@classmethod
def builder(self,desc, rec=None):
v = get_value(desc, rec=rec)
return IntValue(value=v,
nullstr=desc.get('nullstr',''),
font_size=CSize(1),
halign='right',
valign='middle'
)
def build_input_int_widget(desc, rec=None):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
w = IntegerInput(**kw)
if v is not None:
w.setValue(v)
return w
UiFactory.register('int', build_input_int_widget, IntView.builder)

View File

@ -0,0 +1,31 @@
from ..baseWidget import Password
from ..utils import *
from .factory import UiFactory, get_value, StringView
class PasswordView(StringView):
def __init__(self, text='*', **kw):
super(PasswordView, self).__init__(text=text, **kw)
def getValue(self):
return None
def setValue(self, v):
pass
def builder(self, desc, rec=None):
return PasswordView(text='*',
font_size=CSize(1),
halign='left',
valign='middle')
def build_input_password_widget(desc, rec=None):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
w = Password(**kw)
if v:
w.setValue(v)
return w
UiFactory.register('password', build_input_password_widget, PasswordView.builder)

31
kivyblocks/uitype/text.py Normal file
View File

@ -0,0 +1,31 @@
from ..baseWidget import WrapText, StrInput
from ..utils import *
from .factory import UiFactory, get_value
class TextView(WrapText):
def getValue(self):
return self.text
def setValue(self, v):
self.text = v
@classmethod
def builder(self, desc, rec=None):
txt = get_value(desc, rec=rec)
return TextView(text=str(txt) if txt else '',
font_size=CSize(1),
halign='left',
valign='middle')
def build_input_text_widget(desc, rec=None):
v = get_value(desc, rec=rec)
kw = desc.get('uiparams', {})
w = StrInput(**kw)
if v:
self.multiline = True
w.setValue(v)
return w
UiFactory.register('text', build_input_text_widget, TextView.builder)

View File