From b70e2def122e7235b81c5cb5672e8789adbd13b3 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 27 Jan 2022 11:08:13 +0800 Subject: [PATCH] bugfix --- kivyblocks/uitype/__init__.py | 10 +++++ kivyblocks/uitype/amount.py | 84 +++++++++++++++++++++++++++++++++++ kivyblocks/uitype/checkbox.py | 0 kivyblocks/uitype/code.py | 28 ++++++++++++ kivyblocks/uitype/date.py | 15 +++++++ kivyblocks/uitype/factory.py | 67 ++++++++++++++++++++++++++++ kivyblocks/uitype/float.py | 51 +++++++++++++++++++++ kivyblocks/uitype/int.py | 48 ++++++++++++++++++++ kivyblocks/uitype/password.py | 31 +++++++++++++ kivyblocks/uitype/text.py | 31 +++++++++++++ kivyblocks/uitype/time.py | 0 11 files changed, 365 insertions(+) create mode 100644 kivyblocks/uitype/__init__.py create mode 100644 kivyblocks/uitype/amount.py create mode 100644 kivyblocks/uitype/checkbox.py create mode 100644 kivyblocks/uitype/code.py create mode 100644 kivyblocks/uitype/date.py create mode 100644 kivyblocks/uitype/factory.py create mode 100644 kivyblocks/uitype/float.py create mode 100644 kivyblocks/uitype/int.py create mode 100644 kivyblocks/uitype/password.py create mode 100644 kivyblocks/uitype/text.py create mode 100644 kivyblocks/uitype/time.py diff --git a/kivyblocks/uitype/__init__.py b/kivyblocks/uitype/__init__.py new file mode 100644 index 0000000..98a8dd9 --- /dev/null +++ b/kivyblocks/uitype/__init__.py @@ -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 * + diff --git a/kivyblocks/uitype/amount.py b/kivyblocks/uitype/amount.py new file mode 100644 index 0000000..f0173bc --- /dev/null +++ b/kivyblocks/uitype/amount.py @@ -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) + diff --git a/kivyblocks/uitype/checkbox.py b/kivyblocks/uitype/checkbox.py new file mode 100644 index 0000000..e69de29 diff --git a/kivyblocks/uitype/code.py b/kivyblocks/uitype/code.py new file mode 100644 index 0000000..ba9d20d --- /dev/null +++ b/kivyblocks/uitype/code.py @@ -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) + diff --git a/kivyblocks/uitype/date.py b/kivyblocks/uitype/date.py new file mode 100644 index 0000000..0ce5715 --- /dev/null +++ b/kivyblocks/uitype/date.py @@ -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) + diff --git a/kivyblocks/uitype/factory.py b/kivyblocks/uitype/factory.py new file mode 100644 index 0000000..7146bd0 --- /dev/null +++ b/kivyblocks/uitype/factory.py @@ -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) + diff --git a/kivyblocks/uitype/float.py b/kivyblocks/uitype/float.py new file mode 100644 index 0000000..121a6b1 --- /dev/null +++ b/kivyblocks/uitype/float.py @@ -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) + diff --git a/kivyblocks/uitype/int.py b/kivyblocks/uitype/int.py new file mode 100644 index 0000000..1bd9a99 --- /dev/null +++ b/kivyblocks/uitype/int.py @@ -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) + diff --git a/kivyblocks/uitype/password.py b/kivyblocks/uitype/password.py new file mode 100644 index 0000000..a204cf4 --- /dev/null +++ b/kivyblocks/uitype/password.py @@ -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) + diff --git a/kivyblocks/uitype/text.py b/kivyblocks/uitype/text.py new file mode 100644 index 0000000..7ab8358 --- /dev/null +++ b/kivyblocks/uitype/text.py @@ -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) + diff --git a/kivyblocks/uitype/time.py b/kivyblocks/uitype/time.py new file mode 100644 index 0000000..e69de29