This commit is contained in:
yumoqing 2021-03-22 22:54:22 +08:00
parent eedef483aa
commit 094f81195a
4 changed files with 82 additions and 49 deletions

View File

@ -82,14 +82,68 @@ def getAuthHeader():
print('serverinfo authCode not found') print('serverinfo authCode not found')
return {} return {}
kivyblocks_css_keys = [
"height_nm",
"width_nm",
"height_cm",
"width_cm",
"bgcolor",
"fgcolor",
"radius",
"spacing",
"padding",
"border"
]
class BlocksApp(App): class BlocksApp(App):
def load_csses(self):
config = getConfig()
if not config.css:
return
if config.css.css_filename:
with codecs.open(config.css.css_filename, 'r', 'utf-8') as f:
d = json.load(f)
self.buildCsses(d)
if config.css.css_url:
hc = HttpClient()
d = hc.get(self.realurl(config.css.css_url))
self.buildCsses(d)
def buildCsses(self, dic):
for k,v in dic.items():
if isinstance(v,dict):
self.csses[k] = \
{x:y for x,y in v.items() if x in kivyblocks_css_keys}
def get_csses(self):
return self.csses
def get_css(self, name):
return self.csses.get(name,self.csses.get('default',{}))
def build(self): def build(self):
config = getConfig() config = getConfig()
self.csses = {
"default":{
"bgcolor":[0.35,0.35,0.35,1],
"fgcolor":[0.85,0.85,0.85,1]
},
"input":{
"bgcolor":[0.35,0.35,0.35,1],
"fgcolor":[0.85,0.85,0.85,1]
},
"input_focus":{
"bgcolor":[1,0.35,0.35,1],
"fgcolor":[1,0.85,0.85,1]
}
}
self.public_headers = {} self.public_headers = {}
Window.bind(on_request_close=self.on_close) Window.bind(on_request_close=self.on_close)
Window.bind(size=self.device_info) Window.bind(size=self.device_info)
self.workers = Workers(maxworkers=config.maxworkers or 80) self.workers = Workers(maxworkers=config.maxworkers or 80)
self.workers.start() self.workers.start()
self.load_csses()
self.running = True self.running = True
if config.root: if config.root:
blocks = Blocks() blocks = Blocks()

View File

@ -24,6 +24,7 @@ from .paging import Paging, RelatedLoader
from .ready import WidgetReady from .ready import WidgetReady
from .toolbar import Toolbar from .toolbar import Toolbar
from .bgcolorbehavior import BGColorBehavior from .bgcolorbehavior import BGColorBehavior
from .widget_css import WidgetCSS
def field_widget(desc, rec): def field_widget(desc, rec):
viewer = desc.get('viewer') viewer = desc.get('viewer')
@ -67,6 +68,7 @@ class BLabel(ButtonBehavior, Text):
def __init__(self, **kw): def __init__(self, **kw):
ButtonBehavior.__init__(self) ButtonBehavior.__init__(self)
Text.__init__(self,**kw) Text.__init__(self,**kw)
self.csscls = 'dummy'
class Cell(BoxLayout): class Cell(BoxLayout):
def __init__(self,row,desc): def __init__(self,row,desc):
@ -129,6 +131,7 @@ class Row(GridLayout):
self.linewidth = 1 self.linewidth = 1
self.rowdesc = rowdesc self.rowdesc = rowdesc
super().__init__(cols=len(self.rowdesc),spacing=self.linewidth) super().__init__(cols=len(self.rowdesc),spacing=self.linewidth)
self.spacing = 2
# Clock.schedule_once(self.init,0) # Clock.schedule_once(self.init,0)
self.init(0) self.init(0)
@ -156,12 +159,10 @@ class Row(GridLayout):
self.part.datagrid.select_row = self 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(ScrollWidget, WidgetReady, WidgetCSS):
def __init__(self,part,color_level=-1,**kw): def __init__(self,part,color_level=-1,**kw):
self.part = part self.part = part
ScrollWidget.__init__(self,**kw) super(Header, self).__init__(**kw)
WidgetReady.__init__(self)
BGColorBehavior.__init__(self,color_level=color_level)
self.init(1) self.init(1)
self.bind(on_scroll_stop=self.part.datagrid.on_scrollstop) self.bind(on_scroll_stop=self.part.datagrid.on_scrollstop)
if self.part.freeze_flag: if self.part.freeze_flag:
@ -176,10 +177,10 @@ class Header(WidgetReady, BGColorBehavior, ScrollWidget):
self.add_widget(self.header) self.add_widget(self.header)
self.height = self.header.height self.height = self.header.height
class Body(ScrollWidget): class Body(ScrollWidget, WidgetReady, WidgetCSS):
def __init__(self,part,**kw): def __init__(self,part,**kw):
self.part = part self.part = part
ScrollWidget.__init__(self,**kw) super(Body, self).__init__(**kw)
self.idRow = {} self.idRow = {}
self.bind(on_scroll_stop=self.part.datagrid.on_scrollstop) self.bind(on_scroll_stop=self.part.datagrid.on_scrollstop)
if self.part.freeze_flag: if self.part.freeze_flag:

View File

@ -13,6 +13,7 @@ from kivy.properties import BooleanProperty, ListProperty
from ..threadcall import HttpClient from ..threadcall import HttpClient
from ..utils import CSize from ..utils import CSize
from ..widget_css import WidgetCSS
class BoolInput(Switch): class BoolInput(Switch):
def __init__(self,**kw): def __init__(self,**kw):
@ -40,16 +41,7 @@ class BoolInput(Switch):
def setValue(self,v): def setValue(self,v):
self.active = v self.active = v
class StrInput(TextInput): class StrInput(WidgetCSS, TextInput):
bgcolor = ListProperty(None)
fgcolor = ListProperty(None)
def on_bgcolor(self, o, bgcolor):
self.background_color = bgcolor
def on_fgcolor(self, o, fgcolor):
self.foreground_color = fgcolor
def __init__(self,**kv): def __init__(self,**kv):
if kv is None: if kv is None:
kv = {} kv = {}
@ -78,7 +70,7 @@ class StrInput(TextInput):
a['font_size'] = CSize(kv.get('font_size',0.9)) a['font_size'] = CSize(kv.get('font_size',0.9))
Logger.info('TextInput:a=%s,kv=%s',a,kv) Logger.info('TextInput:a=%s,kv=%s',a,kv)
super(StrInput,self).__init__(**a) super(StrInput, self).__init__(**a)
self.old_value = None self.old_value = None
self.register_event_type('on_changed') self.register_event_type('on_changed')
self.bind(focus=self.on_focus) self.bind(focus=self.on_focus)
@ -95,8 +87,10 @@ class StrInput(TextInput):
def on_focus(self,t,v): def on_focus(self,t,v):
if v: if v:
self.old_value = self.getValue() self.old_value = self.getValue()
self.csscls = 'input_focus'
else: else:
self.checkChange(None) self.checkChange(None)
self.csscls = 'input'
def getValue(self): def getValue(self):
return self.text return self.text

View File

@ -4,24 +4,7 @@ from kivy.uix.textinput import TextInput
from kivy.uix.button import Button from kivy.uix.button import Button
from kivy.uix.label import Label from kivy.uix.label import Label
from kivyblocks.utils import CSize from kivyblocks.utils import CSize
from kivy.app import App
kivyblocks_css_keys = [
"height_nm",
"width_nm",
"height_cm",
"width_cm",
"bgcolor",
"fgcolor",
"radius",
"spacing",
"padding",
"border"
]
kivyblocks_csses = {
"default":{
},
}
class WidgetCSS(object): class WidgetCSS(object):
height_nm = NumericProperty(None) height_nm = NumericProperty(None)
@ -32,6 +15,8 @@ class WidgetCSS(object):
fgcolor = ListProperty(None) fgcolor = ListProperty(None)
csscls = StringProperty(None) csscls = StringProperty(None)
radius = ListProperty(None) radius = ListProperty(None)
background_rec = None
bg_func = Rectangle
def on_height_nm(self, o, v): def on_height_nm(self, o, v):
if not self.height_nm: if not self.height_nm:
@ -53,16 +38,13 @@ class WidgetCSS(object):
def on_csscls(self, o, csscls): def on_csscls(self, o, csscls):
if isinstance(self.csscls, str): if isinstance(self.csscls, str):
self.set_css(sef.csscls) self.set_css(self.csscls)
def _get_css_dict_by_name(self, css_name):
dic = kivyblocks_csses.get(css_name,{})
return dic
def set_css(self, css_str): def set_css(self, css_str):
css = {} css = {}
app = App.get_running_app()
for css_name in css_str.split(' '): for css_name in css_str.split(' '):
css.update(self._get_css_dict_by_name(css_name)) css.update(app.get_css(css_name))
for k,v in css.items(): for k,v in css.items():
setattr(self,k,v) setattr(self,k,v)
@ -82,9 +64,9 @@ class WidgetCSS(object):
if not self.fgcolor: if not self.fgcolor:
return return
if isinstance(self, TextInput): if isinstance(self, TextInput):
self.foreground_color = fgcolor self.foreground_color = self.fgcolor
if isinstance(self, Label): if isinstance(self, Label):
self.color = fgcolor self.color = self.fgcolor
return return
return return
@ -92,17 +74,19 @@ class WidgetCSS(object):
if not self.bgcolor: if not self.bgcolor:
return return
if isinstance(self, TextInput): if isinstance(self, TextInput):
self.background_color = bgcolor self.background_color = self.bgcolor
return return
if isinstance(self, Button): if isinstance(self, Button):
self.background_color = bgcolor self.background_color = self.bgcolor
return return
self.set_background_color() self.set_background_color()
def on_radius(self, o, r): def on_radius(self, o, r):
if not radius: if not radius:
self.bg_func = Rectangle
return return
self.set_background_color() self.bg_func = RoundedRectangle
# self.set_background_color()
def set_background_color(self, *args): def set_background_color(self, *args):
if not self.bgcolor: if not self.bgcolor:
@ -112,11 +96,11 @@ class WidgetCSS(object):
with self.canvas.before: with self.canvas.before:
Color(*self.bgcolor) Color(*self.bgcolor)
if self.radius and len(self.radius) == 4: if self.radius:
self.background_rect = RoundedRectangle(pos=self.pos, self.background_rect = self.bg_func(pos=self.pos,
size=self.size, size=self.size,
radius=self.radius) radius=self.radius)
else: else:
self.background_rect = Rectangle(pos=self.pos, self.background_rect = self.bg_func(pos=self.pos,
size=self.size) size=self.size)