This commit is contained in:
yumoqing 2021-01-21 12:30:56 +08:00
parent 4c75d9f6a6
commit 47a1647f9a
6 changed files with 109 additions and 65 deletions

View File

@ -322,20 +322,6 @@ class Blocks(EventDispatcher):
def build_rest(self, widget,desc,t=None): def build_rest(self, widget,desc,t=None):
self.subwidget_total = len(desc.get('subwidgets',[])) self.subwidget_total = len(desc.get('subwidgets',[]))
self.subwidgets = [ None for i in range(self.subwidget_total)] 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 pos = 0
for pos,sw in enumerate(desc.get('subwidgets',[])): for pos,sw in enumerate(desc.get('subwidgets',[])):
params={ params={
@ -343,14 +329,11 @@ class Blocks(EventDispatcher):
'widget':widget, 'widget':widget,
'pos':pos 'pos':pos
} }
f = partial(doit,params)
b = Blocks() b = Blocks()
b.bind(on_built=f)
b.bind(on_failed=doerr)
kw = sw.copy() 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',[]): for b in desc.get('binds',[]):
kw = b.copy() kw = b.copy()
self.buildBind(widget,b) self.buildBind(widget,b)
@ -371,6 +354,7 @@ class Blocks(EventDispatcher):
Logger.info('Block: get a null function,%s',str(desc)) Logger.info('Block: get a null function,%s',str(desc))
return return
w.bind(**{event:f}) w.bind(**{event:f})
# Logger.info('Block: %s bind built', str(desc))
def uniaction(self,widget,desc, *args): def uniaction(self,widget,desc, *args):
Logger.info('Block: uniaction() called, desc=%s', str(desc)) Logger.info('Block: uniaction() called, desc=%s', str(desc))

View File

@ -2,9 +2,12 @@
import os import os
import sys import sys
import signal import signal
import codecs
import json
from appPublic.jsonConfig import getConfig from appPublic.jsonConfig import getConfig
from appPublic.folderUtils import ProgramPath from appPublic.folderUtils import ProgramPath
from appPublic.uniqueID import getID
from kivy.factory import Factory from kivy.factory import Factory
from kivy.config import Config from kivy.config import Config
@ -14,6 +17,8 @@ from kivy.clock import Clock
from kivy.logger import Logger from kivy.logger import Logger
import kivy import kivy
import plyer
from kivy.resources import resource_add_path from kivy.resources import resource_add_path
resource_add_path(os.path.join(os.path.dirname(__file__),'./ttf')) resource_add_path(os.path.join(os.path.dirname(__file__),'./ttf'))
Config.set('kivy', 'default_font', [ Config.set('kivy', 'default_font', [
@ -28,6 +33,8 @@ from .pagescontainer import PageContainer
from .blocks import Blocks from .blocks import Blocks
from .theming import ThemeManager from .theming import ThemeManager
from appPublic.rsa import RSA from appPublic.rsa import RSA
if platform == 'android':
from jnius import autoclass
class ServerInfo: class ServerInfo:
def __init__(self): def __init__(self):
@ -92,6 +99,44 @@ class BlocksApp(App):
self.on_close() self.on_close()
return x 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): def device_info(self, *args):
d = { d = {
"platform":platform, "platform":platform,

View File

@ -1,3 +1,4 @@
import math
from kivyblocks.graph import * from kivyblocks.graph import *
from kivy.utils import get_color_from_hex as rgb from kivy.utils import get_color_from_hex as rgb
from .threadcall import HttpClient from .threadcall import HttpClient
@ -42,7 +43,24 @@ class Chart2d(Graph):
"line":LinePlot, "line":LinePlot,
"hbar":BarPlot "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': { 'label_options': {
'color': rgb('444444'), # color of tick labels and titles 'color': rgb('444444'), # color of tick labels and titles
'bold': True}, 'bold': True},
@ -65,32 +83,45 @@ class Chart2d(Graph):
self._data = data self._data = data
if not self._data: if not self._data:
self._data = self.get_data() self._data = self.get_data()
xmax = len(data) print('_data=',self._data, 'url=', self._dataurl)
xmax = len(self._data)
ymax = 0 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 self._charts = charts
print('charts=', charts)
plots = [] plots = []
for c in charts: for c in charts:
plotKlass = self.plotmappings.get('charttype') plotKlass = self.plotmappings.get(c['charttype'])
if not plot: if not plotKlass:
print('charttype not defined', c)
continue continue
yvalue = [self._data[i][c['y_field']] for i in range(xmax)] yvalue = [self._data[i][c['y_field']] for i in range(xmax)]
print('yvalue=', yvalue)
color = rgb(c.get('color','d8d8d8')) color = rgb(c.get('color','d8d8d8'))
plot = plotKlass(color=color) 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) plots.append(plot)
maxv = max(yvalue) maxv = max(yvalue)
if ymax < maxv: if ymax < maxv:
ymax = maxv ymax = maxv
gkw = kw.copy() gkw = self.default_options.copy()
gkw['ymax'] = ymax 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 gkw['xmax'] = xmax
Graph.__init__(self, **kw) print('gkw=', gkw)
self.ymax = ymax Graph.__init__(self, **gkw)
print('plots=', plots)
for p in plots: for p in plots:
print('points=', p.points)
self.add_plot(p) self.add_plot(p)
if hasattr(p,'bind_to_graph'):
p.bind_to_graph(self) p.bind_to_graph(self)
def get_data(self): def get_data(self):

View File

@ -116,6 +116,7 @@ class Row(GridLayout):
self.part.datagrid.row_selected = True self.part.datagrid.row_selected = True
self.part.datagrid.select_rowid = self.row_id self.part.datagrid.select_rowid = self.row_id
self.part.datagrid.select_row = self
self.part.datagrid.dispatch('on_selected',self) self.part.datagrid.dispatch('on_selected',self)
class Header(WidgetReady, BGColorBehavior, ScrollWidget): class Header(WidgetReady, BGColorBehavior, ScrollWidget):
@ -342,7 +343,6 @@ class DataGrid(WidgetReady, BGColorBehavior, BoxLayout):
self.normal_part.body.clearRows() self.normal_part.body.clearRows()
def add_page(self,o,data): def add_page(self,o,data):
print('dg.py:add_page() called',data)
ids = [] ids = []
recs = data['data'] recs = data['data']
page = data['page'] page = data['page']
@ -354,7 +354,6 @@ class DataGrid(WidgetReady, BGColorBehavior, BoxLayout):
for r in recs: for r in recs:
id = self.addRow(r,index=idx) id = self.addRow(r,index=idx)
ids.append(id) ids.append(id)
print('rec added',r,id)
self.dataloader.bufferObjects(page,ids) self.dataloader.bufferObjects(page,ids)
x = self.dataloader.getLocater() x = self.dataloader.getLocater()
self.locater(x) self.locater(x)
@ -383,7 +382,7 @@ class DataGrid(WidgetReady, BGColorBehavior, BoxLayout):
self.toolbar = Toolbar(ancestor=self,**tb) self.toolbar = Toolbar(ancestor=self,**tb)
def on_selected(self,row): 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): def loadData(self,**kwargs):
page = kwargs.get('page',1) page = kwargs.get('page',1)

View File

@ -311,6 +311,12 @@ class Graph(Widget):
k2 = 0 # position in points minor k2 = 0 # position in points minor
for m in range(0, n_ticks): for m in range(0, n_ticks):
if minor and m % minor: 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 points_minor[k2] = m * tick_dist + s_min
k2 += 1 k2 += 1
else: else:

View File

@ -14,31 +14,26 @@ class OrientationLayout(WidgetReady, SwipeBehavior, FloatLayout):
def __init__(self, main_widget=None, second_widget=None, **kw): def __init__(self, main_widget=None, second_widget=None, **kw):
self.main_widget = main_widget self.main_widget = main_widget
self.second_widget = second_widget self.second_widget = second_widget
self.widget_main = main_widget self.widget_main = None
self.widget_second = second_widget self.widget_second = None
self.second_flg = False self.second_flg = False
FloatLayout.__init__(self, **kw) FloatLayout.__init__(self, **kw)
SwipeBehavior.__init__(self) SwipeBehavior.__init__(self)
WidgetReady.__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_left=self.toggle_second)
self.bind(on_swipe_right=self.toggle_second) self.bind(on_swipe_right=self.toggle_second)
self.bind(size=self.on_size_changed) self.bind(size=self.on_size_changed)
self.bind(pos=self.on_size_changed) self.bind(pos=self.on_size_changed)
self.current_orient = None self.current_orient = None
self.register_event_type('on_orientation_changed') self.register_event_type('on_orientation_changed')
self.reready()
def build_children(self, *args): def build_children(self, *args):
if isinstance(self.main_widget, dict):
blocks = Factory.Blocks() blocks = Factory.Blocks()
blocks.bind(on_built=self.main_widget_built) self.widget_main = blocks.widgetBuild(self.main_widget)
blocks.bind(on_failed=self.widget_build_failed)
blocks.widgetBuild(self.main_widget)
if isinstance(self.second_widget, dict):
blocks = Factory.Blocks() blocks = Factory.Blocks()
blocks.bind(on_built=self.second_widget_built) self.widget_second = blocks.widgetBuild(self.second_widget)
blocks.bind(on_failed=self.widget_build_failed)
blocks.widgetBuild(self.second_widget)
def isLandscape(self): def isLandscape(self):
return self.width > self.height 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_main)
self.add_widget(self.widget_second) 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): def widget_build_failed(self, o, e):
pass pass