From 4c75d9f6a63d8fa97751c7390057baea7377acd0 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 20 Jan 2021 14:10:00 +0800 Subject: [PATCH] bugfix --- kivyblocks/blocks.py | 5 +- kivyblocks/blocksapp.py | 75 ++++++++++++----------------- kivyblocks/chart2d.py | 102 ++++++++++++++++++++++++++++++++++++++++ kivyblocks/register.py | 2 + 4 files changed, 136 insertions(+), 48 deletions(-) create mode 100644 kivyblocks/chart2d.py diff --git a/kivyblocks/blocks.py b/kivyblocks/blocks.py index 00ad750..6535cb4 100755 --- a/kivyblocks/blocks.py +++ b/kivyblocks/blocks.py @@ -545,13 +545,14 @@ class Blocks(EventDispatcher): if not isinstance(desc,dict): Logger.info('Block: desc must be a dict object', desc,type(desc)) - raise Exception('desc must be a dict') + return None desc = self.valueExpr(desc) widget = self.w_build(desc) self.dispatch('on_built',widget) if hasattr(widget,'ready'): widget.ready() + return widget if not (isinstance(desc, DictObject) or isinstance(desc, dict)): print('Block: desc must be a dict object', @@ -582,7 +583,7 @@ class Blocks(EventDispatcher): if addon: desc = dictExtend(desc,addon) - doit(desc) + return doit(desc) @classmethod def getWidgetById(self,id,from_widget=None): diff --git a/kivyblocks/blocksapp.py b/kivyblocks/blocksapp.py index 19ad9eb..74486e4 100644 --- a/kivyblocks/blocksapp.py +++ b/kivyblocks/blocksapp.py @@ -21,6 +21,7 @@ Config.set('kivy', 'default_font', [ 'DroidSansFallback.ttf']) from kivy.app import App +from kivy.utils import platform from .threadcall import HttpClient,Workers from .utils import * from .pagescontainer import PageContainer @@ -61,20 +62,6 @@ def signal_handler(signal, frame): signal.signal(signal.SIGINT, signal_handler) -def on_close(*args,**kwargs): - """ - catch the "x" button's event of window - """ - Logger.info('kivyblocks: on_close(), args=%s, kwargs=%s',str(args),str(kwargs)) - app = App.get_running_app() - if len(app.root.pageWidgets) <= 1: - app.workers.running = False - Logger.info('kivyblocks: on_close(), return False') - return False - app.root.previous() - Logger.info('kivyblocks: on_close(), return True') - return True - def getAuthHeader(): app = App.get_running_app() if not hasattr(app,'serverinfo'): @@ -88,39 +75,35 @@ def getAuthHeader(): print('serverinfo authCode not found') return {} -def closeWorkers(): - app = App.get_running_app() - app.workers.running = False - -def appBlocksHack(app): - config = getConfig() - # app.on_close = on_close - app.getAuthHeader = getAuthHeader - app.__del__ = closeWorkers - Window.bind(on_request_close=app.on_close) - app.serverinfo = ServerInfo() - app.title = 'Test Title' - app.blocks = Blocks() - app.workers = Workers(maxworkers=config.maxworkers or 80) - app.workers.start() - app.hc = HttpClient() - WindowBase.softinput_mode='below_target' - class BlocksApp(App): def build(self): - appBlocksHack(self) - root = PageContainer() - x = None config = getConfig() - blocks = Factory.Blocks() - blocks.bind(on_built=self.addTopWidget) - blocks.bind(on_error=self.showError) - blocks.widgetBuild(config.root) - return root - - def addTopWidget(self,o,w): - self.root.add_widget(w) - - def showError(self,o,e): - alert(str(self.config.root)+': cannt build widget') + self.public_headers = {} + Window.bind(on_request_close=self.on_close) + Window.bind(size=self.device_info) + self.workers = Workers(maxworkers=config.maxworkers or 80) + self.workers.start() + self.running = True + blocks = Blocks() + print(config.root) + x = blocks.widgetBuild(config.root) + if x is None: + alert('buildError,Exit', title='Error') + self.on_close() + return x + + def device_info(self, *args): + d = { + "platform":platform, + "width":Window.width, + "height":Window.height + } + device = { + "device_info":";".join([f'{k}={v}' for k,v in d.items()]) + } + self.public_headers.update(device) + + def on_close(self, *args): + self.workers.running = False + return False diff --git a/kivyblocks/chart2d.py b/kivyblocks/chart2d.py new file mode 100644 index 0000000..95f2275 --- /dev/null +++ b/kivyblocks/chart2d.py @@ -0,0 +1,102 @@ +from kivyblocks.graph import * +from kivy.utils import get_color_from_hex as rgb +from .threadcall import HttpClient + +class Chart2d(Graph): + """ + json format: + { + "widgettype":"Chart2d", + "options":{ + "xlabel":"Xlabel", + "ylable":"Ylabel", + "x_ticks_minor":1, + "x_ticks_major":5, + "y_ticks_minor":1, + "y_ticks_major":5, + "x_grid_label":true, + "y_grid_label":true, + "padding":5, + "xlog":false, + "ylog":false, + "x_grid":true, + "y_grid":true, + "xmin":0, + "xmax":100, + "ymax":100, + "ymin":1, + "x_field":"xxx", + "dataurl":"xxx", + "charts":[ + { + "y_field":"yy", + "color":"efefef", + "charttype":"LinePlot" + } + ] + + } + } + """ + plotmappings={ + "line":LinePlot, + "hbar":BarPlot + } + graph_theme = { + 'label_options': { + 'color': rgb('444444'), # color of tick labels and titles + 'bold': True}, + 'background_color': rgb('f8f8f2'), # canvas background color + 'tick_color': rgb('808080'), # ticks and grid + 'border_color': rgb('808080') # border drawn around each graph + } + + def __init__(self, dataurl='', + data=None, + x_field='', + params={}, + headers={}, + charts=[], + **kw): + self._dataurl = dataurl + self._params = params + self._headers = {} + self.x_field = x_field + self._data = data + if not self._data: + self._data = self.get_data() + xmax = len(data) + ymax = 0 + xvalues = [ self._data[i][self.x_field] for i in range(xmax) ] + self._charts = charts + plots = [] + for c in charts: + plotKlass = self.plotmappings.get('charttype') + if not plot: + continue + yvalue = [self._data[i][c['y_field']] for i in range(xmax)] + color = rgb(c.get('color','d8d8d8')) + plot = plotKlass(color=color) + plot.points = [(xvalue[i],yvalue[i]) for i in range(xmax)] + plots.append(plot) + maxv = max(yvalue) + if ymax < maxv: + ymax = maxv + + gkw = kw.copy() + gkw['ymax'] = ymax + gkw['xmax'] = xmax + Graph.__init__(self, **kw) + self.ymax = ymax + + for p in plots: + self.add_plot(p) + p.bind_to_graph(self) + + def get_data(self): + hc = HttpClient() + d = hc.get(self._dataurl, + params=self._params, + headers=self._headers) + return d + diff --git a/kivyblocks/register.py b/kivyblocks/register.py index d04f1af..b07e724 100644 --- a/kivyblocks/register.py +++ b/kivyblocks/register.py @@ -20,8 +20,10 @@ from .graph import Graph, MeshLinePlot, MeshStemPlot, LinePlot, \ SmoothLinePlot, ContourPlot, BarPlot, HBar, VBar, ScatterPlot, \ PointPlot from .mapview import MapView +from .chart2d import Chart2d r = Factory.register +r('Chart2d', Chart2d) r('Popup', Popup) r('Graph', Graph) r('MeshLinePlot', MeshLinePlot)