This commit is contained in:
yumoqing 2021-01-05 10:20:04 +08:00
parent 870d17f6b3
commit e8674f61e2
2 changed files with 55 additions and 16 deletions

View File

@ -14,6 +14,7 @@ from appPublic.Singleton import SingletonDecorator, GlobalEnv
from appPublic.datamapping import keyMapping from appPublic.datamapping import keyMapping
from appPublic.registerfunction import RegisterFunction from appPublic.registerfunction import RegisterFunction
from kivy.logger import Logger
from kivy.config import Config from kivy.config import Config
from kivy.metrics import sp,dp,mm from kivy.metrics import sp,dp,mm
from kivy.core.window import WindowBase, Window from kivy.core.window import WindowBase, Window
@ -153,11 +154,11 @@ class Blocks(EventDispatcher):
fname = 'action%d' % self.action_id fname = 'action%d' % self.action_id
l = { l = {
} }
body="""def %s(widget,obj=None, *args): body="""def %s(widget,obj=None, *args, **kw):
jsonstr='''%s''' jsonstr='''%s'''
desc = json.loads(jsonstr) desc = json.loads(jsonstr)
blocks = Blocks() blocks = Blocks()
blocks.uniaction(widget, desc,*args) blocks.uniaction(widget, desc,*args, **kw)
""" % (fname, json.dumps(desc)) """ % (fname, json.dumps(desc))
exec(body,globals(),l) exec(body,globals(),l)
f = l.get(fname,None) f = l.get(fname,None)
@ -283,7 +284,7 @@ class Blocks(EventDispatcher):
# print('w_build(),desc=',desc) # print('w_build(),desc=',desc)
widgetClass = desc.get('widgettype',None) widgetClass = desc.get('widgettype',None)
if not widgetClass: if not widgetClass:
print("w_build(), desc invalid", desc) Logger.info("Block: w_build(), desc invalid", desc)
raise Exception(desc) raise Exception(desc)
widgetClass = desc['widgettype'] widgetClass = desc['widgettype']
@ -360,16 +361,21 @@ class Blocks(EventDispatcher):
wid = desc.get('wid','self') wid = desc.get('wid','self')
w = Blocks.getWidgetById(desc.get('wid','self'),from_widget=widget) w = Blocks.getWidgetById(desc.get('wid','self'),from_widget=widget)
if not w: if not w:
print(desc.get('wid','self'),'not found via Blocks.getWidgetById()') Logger.info('Block: %s %s',desc.get('wid','self'),
'not found via Blocks.getWidgetById()')
return return
event = desc.get('event') event = desc.get('event')
if event is None: if event is None:
Logger.info('Block: binds desc miss event, desc=%s',str(desc))
return return
f = self.buildAction(widget,desc) f = self.buildAction(widget,desc)
if f is None:
Logger.Info('Block: get a null function,%s',str(desc))
return
w.bind(**{event:f}) w.bind(**{event:f})
# print('w=',w,event,desc)
def uniaction(self,widget,desc, *args): def uniaction(self,widget,desc, *args):
Logger.info('Block: uniaction() called, desc=%s', str(desc))
acttype = desc.get('actiontype') acttype = desc.get('actiontype')
if acttype=='blocks': if acttype=='blocks':
return self.blocksAction(widget,desc, *args) return self.blocksAction(widget,desc, *args)
@ -385,6 +391,9 @@ class Blocks(EventDispatcher):
def blocksAction(self,widget,desc, *args): def blocksAction(self,widget,desc, *args):
target = Blocks.getWidgetById(desc.get('target','self'),widget) target = Blocks.getWidgetById(desc.get('target','self'),widget)
if target is None:
Logger.Info('Block: blocksAction():desc(%s) target not found',
str(desc))
add_mode = desc.get('mode','replace') add_mode = desc.get('mode','replace')
opts = desc.get('options').copy() opts = desc.get('options').copy()
d = self.getActionData(widget,desc) d = self.getActionData(widget,desc)
@ -397,6 +406,8 @@ class Blocks(EventDispatcher):
target.add_widget(w) target.add_widget(w)
def doerr(o,e): def doerr(o,e):
Logger.info('Block: blocksAction(): desc=%s widgetBuild error'
,str(desc))
raise e raise e
b = Blocks() b = Blocks()
@ -405,8 +416,10 @@ class Blocks(EventDispatcher):
b.widgetBuild(opts) b.widgetBuild(opts)
def urlwidgetAction(self,widget,desc, *args): def urlwidgetAction(self,widget,desc, *args):
print('urlwidgetAction():args=',args)
target = Blocks.getWidgetById(desc.get('target','self'),widget) target = Blocks.getWidgetById(desc.get('target','self'),widget)
if target is None:
Logger.Info('Block: urlwidgetAction():desc(%s) target not found',
str(desc))
add_mode = desc.get('mode','replace') add_mode = desc.get('mode','replace')
opts = desc.get('options').copy() opts = desc.get('options').copy()
p = opts.get('params',{}).copy() p = opts.get('params',{}).copy()
@ -427,6 +440,8 @@ class Blocks(EventDispatcher):
target.add_widget(w) target.add_widget(w)
def doerr(o,e): def doerr(o,e):
Logger.info('Block: urlwidgetAction(): desc=%s widgetBuild error'
,str(desc))
raise e raise e
b = Blocks() b = Blocks()
@ -439,45 +454,66 @@ class Blocks(EventDispatcher):
if desc.get('datawidget',False): if desc.get('datawidget',False):
dwidget = Blocks.getWidgetById(desc.get('datawidget','self'), dwidget = Blocks.getWidgetById(desc.get('datawidget','self'),
from_widget=widget) from_widget=widget)
if dwidget and hasattr(dwidget,'getValue'): if dwidget is None:
Logger.info('Block: desc(%s) datawidget not defined',
str(desc))
if hasattr(dwidget,'getValue'):
data = dwidget.getValue() data = dwidget.getValue()
if desc.get('keymapping'): if desc.get('keymapping'):
data = keyMapping(data, desc.get('keymapping')) data = keyMapping(data, desc.get('keymapping'))
else:
Logger.info('Block: desc(%s) datawidget has not getValue',
str(desc))
return data return data
def registedfunctionAction(self, widget, desc, *args): def registedfunctionAction(self, widget, desc, *args):
target = Blocks.getWidgetById(desc.get('target','self'), target = Blocks.getWidgetById(desc.get('target','self'),
from_widget=widget) from_widget=widget)
if target is None:
Logger.Info('Block: registedfunctionAction():desc(%s) target not found',
str(desc))
rf = RegisterFunction() rf = RegisterFunction()
name = desc.get('rfname') name = desc.get('rfname')
func = rf.get(name) func = rf.get(name)
if func is None: if func is None:
print('rfname(%s) not found' % name) Logger.Info('Block: desc=%s rfname(%s) not found',
str(desc), name)
raise Exception('rfname(%s) not found' % name) raise Exception('rfname(%s) not found' % name)
params = desc.get('params',{}).copy() params = desc.get('params',{}).copy()
d = self.getActionData(widget,desc) d = self.getActionData(widget,desc)
params.update(d) params.update(d)
# print('registedfunctionAction(),params=',params)
func(target, *args, **params) func(target, *args, **params)
def scriptAction(self, widget, desc, *args): def scriptAction(self, widget, desc, *args):
script = desc.get('script') script = desc.get('script')
if not script: if not script:
Logger.Info('Block: scriptAction():desc(%s) target not found',
str(desc))
return return
target = Blocks.getWidgetById(desc.get('target','self'), target = Blocks.getWidgetById(desc.get('target','self'),
from_widget=widget) from_widget=widget)
if target is None:
Logger.Info('Block: scriptAction():desc(%s) target not found',
str(desc))
d = self.getActionData(widget,desc) d = self.getActionData(widget,desc)
ns = { ns = {
"self":target, "self":target,
"args":args "args":args
} }
ns.update(d) ns.update(d)
try:
self.eval(script, ns) self.eval(script, ns)
except Exception as e:
print_exc()
print(e)
def methodAction(self, widget, desc, *args): def methodAction(self, widget, desc, *args):
method = desc.get('method') method = desc.get('method')
target = Blocks.getWidgetById(desc.get('target','self'),widget) target = Blocks.getWidgetById(desc.get('target','self'),widget)
if target is None:
Logger.Info('Block: methodAction():desc(%s) target not found',
str(desc))
if hasattr(target,method): if hasattr(target,method):
f = getattr(target, method) f = getattr(target, method)
kwargs = desc.get('options',{}).copy() kwargs = desc.get('options',{}).copy()
@ -504,7 +540,8 @@ class Blocks(EventDispatcher):
def doit(desc): def doit(desc):
if not isinstance(desc,dict): if not isinstance(desc,dict):
print('desc must be a dict object',desc,type(desc)) Logger.info('Block: desc must be a dict object',
desc,type(desc))
raise Exception('desc must be a dict') raise Exception('desc must be a dict')
desc = self.valueExpr(desc) desc = self.valueExpr(desc)

View File

@ -6,11 +6,13 @@ from kivy.uix.label import Label
from kivy.app import App from kivy.app import App
from .utils import CSize from .utils import CSize
from .bgcolorbehavior import BGColorBehavior from .bgcolorbehavior import BGColorBehavior
from .ready import WidgetReady
class PageContainer(BGColorBehavior, FloatLayout): class PageContainer(WidgetReady, BGColorBehavior, FloatLayout):
def __init__(self,**kw): def __init__(self,**kw):
FloatLayout.__init__(self, **kw) FloatLayout.__init__(self, **kw)
BGColorBehavior.__init__(self) BGColorBehavior.__init__(self)
WidgetReady.__init__(self)
self.show_back = True self.show_back = True
self.pageWidgets = [] self.pageWidgets = []
self.backButton = Button(text='<',size_hint=(None,None), self.backButton = Button(text='<',size_hint=(None,None),