kivyblocks/kivyblocks/form.py

303 lines
7.2 KiB
Python
Raw Normal View History

2019-12-19 11:13:47 +08:00
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
2020-01-08 18:04:39 +08:00
from kivycalendar import DatePicker
from .responsivelayout import VResponsiveLayout
2019-12-19 11:13:47 +08:00
from .widgetExt.inputext import FloatInput,IntegerInput, \
StrInput,SelectInput, BoolInput, AmountInput, Password
from .baseWidget import *
from .utils import *
2020-01-08 18:04:39 +08:00
from .i18n import I18nText, I18n
from .toolbar import Toolbar
2019-12-19 11:13:47 +08:00
"""
form options
{
"toolbar":
"dataloader":{
"dataurl":
"params":
"method"
}
"cols":"1"
"labelwidth":
"textsize":
"inputheight":
"inline":True,False
"fields":[
{
"name":
"label":
"datatype",
"uitype",
"uiparams",
"default",
2019-12-21 18:28:00 +08:00
"readonly",
2019-12-19 11:13:47 +08:00
"required"
},
]
"binds":[
]
}
"""
uitypes = {
"string":{
"orientation":"horizontal",
"wclass":StrInput,
},
2020-01-08 18:04:39 +08:00
"password":{
"orientation":"horizontal",
"wclass":Password,
},
2019-12-19 11:13:47 +08:00
"number":{
"orientation":"horizontal",
"wclass":IntegerInput,
},
"float":{
"orientation":"horizontal",
"wclass":FloatInput,
},
"amount":{
"orientation":"horizontal",
"wclass":AmountInput,
},
"date":{
"orientation":"horizontal",
"wclass":DatePicker,
},
"time":{
"orientation":"horizontal",
"wclass":StrInput,
},
"bool":{
"orientation":"horizontal",
"wclass":BoolInput,
},
"code":{
"orientation":"horizontal",
"wclass":SelectInput,
},
"text":{
"orientation":"vertical",
"wclass":StrInput,
"options":{
"multiline":True,
"height":CSize(6),
}
},
"teleno":{
"orientation":"horizontal",
"wclass":StrInput,
},
"email":{
"orientation":"horizontal",
"wclass":StrInput,
},
}
class InputBox(BoxLayout):
def __init__(self, form, **options):
self.form = form
self.options = options
2020-01-08 18:04:39 +08:00
self.uitype = options.get('uitype','string')
2019-12-19 11:13:47 +08:00
self.uidef = uitypes[self.uitype]
orientation = self.uidef['orientation']
width = self.form.inputwidth
height = self.form.inputheight
self.labelwidth = self.form.options['labelwidth']
kwargs = {
"orientation":orientation,
"size_hint_y":None,
2020-01-08 18:04:39 +08:00
"height":height
2019-12-19 11:13:47 +08:00
}
if width <= 1:
kwargs['size_hint_x'] = width
else:
kwargs['size_hint_x'] = None
kwargs['width'] = CSize(width)
super().__init__(**kwargs)
self.initflag = False
self.bind(on_size=self.setSize,
pos=self.setSize)
self.register_event_type("on_datainput")
2020-01-08 18:04:39 +08:00
def on_datainput(self,o,v=None):
print('on_datainput fired ...',o,v)
2019-12-19 11:13:47 +08:00
def init(self):
2020-01-08 18:04:39 +08:00
i18n = I18n()
2019-12-19 11:13:47 +08:00
if self.initflag:
return
label = self.options.get('label',self.options.get('name'))
if self.options.get('required'):
label = label + '*'
kwargs = {
"otext":label,
2020-01-08 18:04:39 +08:00
"font_size":CSize(1),
"size_hint_y":None,
"height":CSize(2)
2019-12-19 11:13:47 +08:00
}
if self.labelwidth<=1:
kwargs['size_hint_x'] = self.labelwidth
else:
kwargs['size_hint_x'] = None
kwargs['width'] = self.labelwidth
self.labeltext = I18nText(**kwargs)
self.add_widget(self.labeltext)
options = self.uidef.get('options',{}).copy()
options.update(self.options.get('uiparams',{}))
options['allow_copy'] = True
if self.options.get('tip'):
options['hint_text'] = i18n(self.options.get('tip'))
2019-12-21 18:28:00 +08:00
2020-01-08 18:04:39 +08:00
print('uitype=',self.options['uitype'], self.uitype, 'uidef=',self.uidef)
2019-12-19 11:13:47 +08:00
self.input_widget = self.uidef['wclass'](**options)
2019-12-21 18:28:00 +08:00
if self.options.get('readonly'):
self.input_widget.disabled = True
self.form.widget_ids[self.options['name']] = self.input_widget
2019-12-19 11:13:47 +08:00
self.add_widget(self.input_widget)
self.initflag = True
self.input_widget.bind(on_focus=self.on_focus)
if self.options.get('default'):
self.input_widget.setValue(self.options.get('default'))
2019-12-21 18:28:00 +08:00
def clear(self):
self.input_widget.setValue('')
2019-12-19 11:13:47 +08:00
def on_focus(self,o,v):
if v:
self.old_value = o.text
else:
if self.old_value != o.text:
self.dispatch('on_datainput',o.text)
def setSize(self,o,v=None):
self.init()
def setValue(self,v):
self.input_widget.setValue(v)
def getValue(self):
2020-01-08 18:04:39 +08:00
return {self.options.get('name'):self.input_widget.getValue()}
2019-12-19 11:13:47 +08:00
def defaultToolbar():
return {
2020-01-08 18:04:39 +08:00
"img_size":1.5,
"text_size":0.7,
"tools":[
2019-12-19 11:13:47 +08:00
{
"name":"__submit",
2020-01-10 12:05:30 +08:00
"img_src":"/imgs/submit.png",
2019-12-19 11:13:47 +08:00
"label":"submit"
},
{
"name":"__clear",
2020-01-10 12:05:30 +08:00
"img_src":"/imgs/clear.png",
2019-12-19 11:13:47 +08:00
"label":"clear"
}
]
}
class Form(BoxLayout):
def __init__(self, **options):
self.options = options
BoxLayout.__init__(self, orientation='vertical')
self.widget_ids = {}
2020-01-09 17:52:02 +08:00
self.cols = self.options_cols = self.options.get('cols',1)
2019-12-19 11:13:47 +08:00
if isHandHold() and Window.width < Window.height:
self.cols = 1
self.inputwidth = Window.width / self.cols
self.inputheight = CSize(self.options.get('inputheight',3))
self.initflag = False
2020-02-26 17:24:58 +08:00
self.register_event_type('on_submit')
2019-12-19 11:13:47 +08:00
self.bind(size=self.on_size,
pos=self.on_size)
def init(self):
if self.initflag:
return
2020-01-08 18:04:39 +08:00
self.toolbar = Toolbar(ancestor=self,**self.options.get('toolbar',defaultToolbar()))
2020-01-09 17:52:02 +08:00
self.fsc = VResponsiveLayout(
self.inputwidth,
self.cols
)
print('box_width=%d,cols=%d' % (self.inputwidth, self.cols))
2020-01-08 18:04:39 +08:00
self.add_widget(self.toolbar)
self.add_widget(self.fsc)
2019-12-19 11:13:47 +08:00
self.fieldWidgets=[]
for f in self.options['fields']:
2020-01-08 18:04:39 +08:00
w = InputBox(self, **f)
# print('w size=',w.size)
2019-12-19 11:13:47 +08:00
self.fsc.add_widget(w)
self.fieldWidgets.append(w)
blocks = App.get_running_app().blocks
2020-02-26 17:24:58 +08:00
# wid = self.widget_ids['__submit']
wid = blocks.getWidgetByIdPath(self,'__submit')
2019-12-19 11:13:47 +08:00
wid.bind(on_press=self.on_submit_button)
2020-02-26 17:24:58 +08:00
wid = blocks.getWidgetByIdPath(self,'__clear')
# wid = self.widget_ids['__clear']
2019-12-19 11:13:47 +08:00
wid.bind(on_press=self.on_clear_button)
self.initflag = True
2020-01-08 18:04:39 +08:00
def getData(self):
d = {}
for f in self.fieldWidgets:
v = f.getValue()
d.update(v)
return d
2020-02-26 17:24:58 +08:00
def on_submit(self,v=None):
print('Form():on_submit fired ...',v)
return False
2020-01-08 18:04:39 +08:00
2019-12-19 11:13:47 +08:00
def on_submit_button(self,o,v=None):
2020-01-08 18:04:39 +08:00
d = self.getData()
2020-02-26 17:24:58 +08:00
self.dispatch('on_submit',d)
2019-12-19 11:13:47 +08:00
def on_clear_button(self,o,v=None):
for fw in self.fieldWidgets:
fw.clear()
def on_size(self,o, v=None):
self.init()
class StrSearchForm(BoxLayout):
def __init__(self,img_url=None,**options):
self.name = options.get('name','search_string')
BoxLayout.__init__(self,orientation='horizontal',size_hint_y=None,height=CSize(3))
self.inputwidget = TextInput(
2019-12-22 22:02:37 +08:00
text='',
2019-12-19 11:13:47 +08:00
multiline=False,
font_size=CSize(1),
size_hint_y=None,
height=CSize(3))
self.add_widget(self.inputwidget)
imgsrc = img_url if img_url else blockImage('search.png')
self.search = PressableImage(source=imgsrc,
size_hint=(None,None),
size=CSize(3,3)
)
self.add_widget(self.search)
self.register_event_type('on_submit')
self.search.bind(on_press=self.submit_input)
self.inputwidget.bind(on_text_validate=self.submit_input)
2020-03-19 18:00:35 +08:00
def getData(self):
d = {
self.name:self.inputwidget.text
}
return d
2019-12-19 11:13:47 +08:00
def submit_input(self,o,v=None):
text = self.inputwidget.text
if text != '':
d = {
self.name:text
}
self.dispatch('on_submit',d)
2020-02-26 17:24:58 +08:00
def on_submit(self,v=None):
2019-12-19 11:13:47 +08:00
print('StrSearchForm():on_submit fired ..........')