From 47a1647f9a7a68febd4965199e2f277c2da64189 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Thu, 21 Jan 2021 12:30:56 +0800 Subject: [PATCH] bugfix --- kivyblocks/blocks.py | 28 ++++------------- kivyblocks/blocksapp.py | 45 ++++++++++++++++++++++++++++ kivyblocks/chart2d.py | 53 ++++++++++++++++++++++++++------- kivyblocks/dg.py | 5 ++-- kivyblocks/graph/__init__.py | 6 ++++ kivyblocks/orientationlayout.py | 37 +++++------------------ 6 files changed, 109 insertions(+), 65 deletions(-) diff --git a/kivyblocks/blocks.py b/kivyblocks/blocks.py index 6535cb4..564c504 100755 --- a/kivyblocks/blocks.py +++ b/kivyblocks/blocks.py @@ -322,20 +322,6 @@ class Blocks(EventDispatcher): def build_rest(self, widget,desc,t=None): self.subwidget_total = len(desc.get('subwidgets',[])) self.subwidgets = [ None for i in range(self.subwidget_total)] - def doit(params,o,w): - desc = params['desc'] - widget = params['widget'] - self.subwidgets[params['pos']] = w - if None not in self.subwidgets: - for w in self.subwidgets: - widget.add_widget(w) - for b in desc.get('binds',[]): - kw = b.copy() - self.buildBind(widget,kw) - - def doerr(o,e): - raise e - pos = 0 for pos,sw in enumerate(desc.get('subwidgets',[])): params={ @@ -343,17 +329,14 @@ class Blocks(EventDispatcher): 'widget':widget, 'pos':pos } - f = partial(doit,params) b = Blocks() - b.bind(on_built=f) - b.bind(on_failed=doerr) kw = sw.copy() - b.widgetBuild(kw) + w = b.widgetBuild(kw) + widget.add_widget(w) - if self.subwidget_total == 0: - for b in desc.get('binds',[]): - kw = b.copy() - self.buildBind(widget,b) + for b in desc.get('binds',[]): + kw = b.copy() + self.buildBind(widget,b) def buildBind(self,widget,desc): wid = desc.get('wid','self') @@ -371,6 +354,7 @@ class Blocks(EventDispatcher): Logger.info('Block: get a null function,%s',str(desc)) return w.bind(**{event:f}) + # Logger.info('Block: %s bind built', str(desc)) def uniaction(self,widget,desc, *args): Logger.info('Block: uniaction() called, desc=%s', str(desc)) diff --git a/kivyblocks/blocksapp.py b/kivyblocks/blocksapp.py index 74486e4..952045f 100644 --- a/kivyblocks/blocksapp.py +++ b/kivyblocks/blocksapp.py @@ -2,9 +2,12 @@ import os import sys import signal +import codecs +import json from appPublic.jsonConfig import getConfig from appPublic.folderUtils import ProgramPath +from appPublic.uniqueID import getID from kivy.factory import Factory from kivy.config import Config @@ -14,6 +17,8 @@ from kivy.clock import Clock from kivy.logger import Logger import kivy +import plyer + from kivy.resources import resource_add_path resource_add_path(os.path.join(os.path.dirname(__file__),'./ttf')) Config.set('kivy', 'default_font', [ @@ -28,6 +33,8 @@ from .pagescontainer import PageContainer from .blocks import Blocks from .theming import ThemeManager from appPublic.rsa import RSA +if platform == 'android': + from jnius import autoclass class ServerInfo: def __init__(self): @@ -92,6 +99,44 @@ class BlocksApp(App): self.on_close() return x + def get_user_data_path(self): + if platform == 'android': + Environment = autoclass('android.os.Environment') + sdpath = Environment.get_running_app().getExternalStorageDirectory() + return str(sdpath) + sdpath = App.get_running_app().user_data_dir + return str(sdpath) + + def get_profile_name(self): + fname = os.path.join(self.get_user_data_path(),'.profile.json') + print('profile_path=', fname) + return fname + + def write_profile(self, dic): + fname = self.get_profile_name() + with codecs.open(fname,'w','utf-8') as f: + json.dump(dic,f) + + def write_default_profile(self): + device_id = getID() + try: + device_id = plyer.uniqueid.id + except: + pass + + d = { + 'device_id': device_id + } + self.write_profile(d) + + def read_profile(self): + fname = self.get_profile_name() + if not os.path.isfile(fname): + self.write_default_profile() + with codecs.open(fname, 'r', 'utf-8') as f: + d = json.load(f) + return d + def device_info(self, *args): d = { "platform":platform, diff --git a/kivyblocks/chart2d.py b/kivyblocks/chart2d.py index 95f2275..9e2a8b2 100644 --- a/kivyblocks/chart2d.py +++ b/kivyblocks/chart2d.py @@ -1,3 +1,4 @@ +import math from kivyblocks.graph import * from kivy.utils import get_color_from_hex as rgb from .threadcall import HttpClient @@ -42,7 +43,24 @@ class Chart2d(Graph): "line":LinePlot, "hbar":BarPlot } - graph_theme = { + default_options = { + "x_grid_label":True, + "y_grid_label":True, + "xlabel":"Xlabel", + "ylabel":"Ylabel", + "xmin":0, + "xmax":100, + "ymax":100, + "ymin":1, + "x_grid":True, + "y_grid":True, + "padding":5, + "xlog":False, + "ylog":False, + 'x_ticks_minor':1, + 'x_ticks_major':5, + "y_ticks_minor":1, + "y_ticks_major":5, 'label_options': { 'color': rgb('444444'), # color of tick labels and titles 'bold': True}, @@ -65,33 +83,46 @@ class Chart2d(Graph): self._data = data if not self._data: self._data = self.get_data() - xmax = len(data) + print('_data=',self._data, 'url=', self._dataurl) + xmax = len(self._data) ymax = 0 - xvalues = [ self._data[i][self.x_field] for i in range(xmax) ] + xvalue = [ self._data[i][self.x_field] for i in range(xmax) ] self._charts = charts + print('charts=', charts) plots = [] for c in charts: - plotKlass = self.plotmappings.get('charttype') - if not plot: + plotKlass = self.plotmappings.get(c['charttype']) + if not plotKlass: + print('charttype not defined', c) continue yvalue = [self._data[i][c['y_field']] for i in range(xmax)] + print('yvalue=', yvalue) color = rgb(c.get('color','d8d8d8')) plot = plotKlass(color=color) - plot.points = [(xvalue[i],yvalue[i]) for i in range(xmax)] + plot.points = [(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 = self.default_options.copy() + gkw.update(kw) + gkw['ymax'] = math.ceil(ymax) + gkw['y_ticks_minor'] = gkw['ymax'] / 10 + gkw['y_ticks_major'] = gkw['ymax'] / 2 + gkw['x_ticks_minor'] = 1 + if gkw['x_ticks_major'] > xmax: + gkw['x_ticks_major'] = xmax gkw['xmax'] = xmax - Graph.__init__(self, **kw) - self.ymax = ymax + print('gkw=', gkw) + Graph.__init__(self, **gkw) + print('plots=', plots) for p in plots: + print('points=', p.points) self.add_plot(p) - p.bind_to_graph(self) + if hasattr(p,'bind_to_graph'): + p.bind_to_graph(self) def get_data(self): hc = HttpClient() diff --git a/kivyblocks/dg.py b/kivyblocks/dg.py index 28993f7..a41fe98 100644 --- a/kivyblocks/dg.py +++ b/kivyblocks/dg.py @@ -116,6 +116,7 @@ class Row(GridLayout): self.part.datagrid.row_selected = True self.part.datagrid.select_rowid = self.row_id + self.part.datagrid.select_row = self self.part.datagrid.dispatch('on_selected',self) class Header(WidgetReady, BGColorBehavior, ScrollWidget): @@ -342,7 +343,6 @@ class DataGrid(WidgetReady, BGColorBehavior, BoxLayout): self.normal_part.body.clearRows() def add_page(self,o,data): - print('dg.py:add_page() called',data) ids = [] recs = data['data'] page = data['page'] @@ -354,7 +354,6 @@ class DataGrid(WidgetReady, BGColorBehavior, BoxLayout): for r in recs: id = self.addRow(r,index=idx) ids.append(id) - print('rec added',r,id) self.dataloader.bufferObjects(page,ids) x = self.dataloader.getLocater() self.locater(x) @@ -383,7 +382,7 @@ class DataGrid(WidgetReady, BGColorBehavior, BoxLayout): self.toolbar = Toolbar(ancestor=self,**tb) def on_selected(self,row): - self.selected_row = row + print("DataGrid():on_selected fire", self.widget_id or 'not widget_id seted') def loadData(self,**kwargs): page = kwargs.get('page',1) diff --git a/kivyblocks/graph/__init__.py b/kivyblocks/graph/__init__.py index c6b2e99..1f26966 100644 --- a/kivyblocks/graph/__init__.py +++ b/kivyblocks/graph/__init__.py @@ -311,6 +311,12 @@ class Graph(Widget): k2 = 0 # position in points minor for m in range(0, n_ticks): if minor and m % minor: + if k2 >= len(points_minor): + continue + print('len(points_minor)=', len(points_minor), \ + 'len(points_major)=',len(points_major), \ + 'major=', major, 'minor=', minor or 1, \ + 'k2=',k2, 'n_ticks=', n_ticks) points_minor[k2] = m * tick_dist + s_min k2 += 1 else: diff --git a/kivyblocks/orientationlayout.py b/kivyblocks/orientationlayout.py index 164f49c..46232f3 100644 --- a/kivyblocks/orientationlayout.py +++ b/kivyblocks/orientationlayout.py @@ -14,31 +14,26 @@ class OrientationLayout(WidgetReady, SwipeBehavior, FloatLayout): def __init__(self, main_widget=None, second_widget=None, **kw): self.main_widget = main_widget self.second_widget = second_widget - self.widget_main = main_widget - self.widget_second = second_widget + self.widget_main = None + self.widget_second = None self.second_flg = False FloatLayout.__init__(self, **kw) SwipeBehavior.__init__(self) WidgetReady.__init__(self) - Clock.schedule_once(self.build_children,0) + self.build_children() self.bind(on_swipe_left=self.toggle_second) self.bind(on_swipe_right=self.toggle_second) self.bind(size=self.on_size_changed) self.bind(pos=self.on_size_changed) self.current_orient = None self.register_event_type('on_orientation_changed') + self.reready() def build_children(self, *args): - if isinstance(self.main_widget, dict): - blocks = Factory.Blocks() - blocks.bind(on_built=self.main_widget_built) - blocks.bind(on_failed=self.widget_build_failed) - blocks.widgetBuild(self.main_widget) - if isinstance(self.second_widget, dict): - blocks = Factory.Blocks() - blocks.bind(on_built=self.second_widget_built) - blocks.bind(on_failed=self.widget_build_failed) - blocks.widgetBuild(self.second_widget) + blocks = Factory.Blocks() + self.widget_main = blocks.widgetBuild(self.main_widget) + blocks = Factory.Blocks() + self.widget_second = blocks.widgetBuild(self.second_widget) def isLandscape(self): return self.width > self.height @@ -106,22 +101,6 @@ class OrientationLayout(WidgetReady, SwipeBehavior, FloatLayout): self.add_widget(self.widget_main) self.add_widget(self.widget_second) - def main_widget_built(self,o,w): - print('main_widget_built() called ...') - self.widget_main = w - self.add_widget(self.widget_main) - if isinstance(self.widget_main, Widget) and isinstance(self.widget_second, Widget): - print('ready() called ..') - self.reready() - - - def second_widget_built(self,o,w): - print('second_widget_built() called ...') - self.widget_second = w - if isinstance(self.widget_main, Widget) and isinstance(self.widget_second, Widget): - print('ready() called ..') - self.reready() - def widget_build_failed(self, o, e): pass