From 0e748ceff85ce3f8dd1c90ec3daadda3a430f559 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 25 Dec 2020 15:39:28 +0800 Subject: [PATCH] bugfix --- kivyblocks/baseWidget.py | 127 ++++++++++++++++++++++++++++++-- kivyblocks/bgcolorbehavior.py | 73 ++++++------------ kivyblocks/blocks.py | 39 ++++++---- kivyblocks/color_definitions.py | 3 +- kivyblocks/pagescontainer.py | 6 +- kivyblocks/register.py | 10 +++ kivyblocks/toggleitems.py | 9 ++- 7 files changed, 190 insertions(+), 77 deletions(-) diff --git a/kivyblocks/baseWidget.py b/kivyblocks/baseWidget.py index 779515b..2cc0fb0 100755 --- a/kivyblocks/baseWidget.py +++ b/kivyblocks/baseWidget.py @@ -71,6 +71,7 @@ from .tab import TabsPanel from .qrdata import QRCodeWidget from .threadcall import HttpClient from .i18n import I18n +from .utils import CSize if platform == 'android': from .widgetExt.phonebutton import PhoneButton @@ -82,17 +83,75 @@ class WrapText(Label): self.bind(width=lambda *x: self.setter('text_size')(self, (self.width, None)), texture_size=lambda *x: self.setter('height')(self, self.texture_size[1])) +class Box(BGColorBehavior, BoxLayout): + def __init__(self,color_level=-1,radius=[],**kw): + BoxLayout.__init__(self, **kw) + BGColorBehavior.__init__(self, color_level=color_level, + radius=radius) + + def add_widget(self, w, *args, **kw): + super().add_widget(w, *args, **kw) + self.setWidgetTextColor(w) + + def setWidgetTextColor(self,w): + if isinstance(w,Box): + return + + if isinstance(w,Text): + if not w.bgcolor: + w.color = self.fgcolor + return + for c in w.children: + self.setWidgetTextColor(c) + + def selected(self): + super().selected() + if self.fgcolor == self.selected_fgcolor: + return + for c in self.children: + self.setWidgetTextColor(c) + + def unselected(self): + super().unselected() + if self.fgcolor == self.normal_fgcolor: + return + for c in self.children: + self.setWidgetTextColor(c) + +class HBox(Box): + def __init__(self,**kw): + kw.update({'orientation':'horizontal'}) + Box.__init__(self, **kw) + + +class VBox(Box): + def __init__(self,color_level=-1,radius=[],**kw): + kw.update({'orientation':'vertical'}) + Box.__init__(self, **kw) + class Text(Label): lang=StringProperty('') otext = StringProperty('') - def __init__(self,i18n=False, textype='text', wrap=False, **kw): + def __init__(self,i18n=False, texttype='text', wrap=False, + fgcolor=None, **kw): + + fontsize={'font_size':CSize(1)} + offset={ + 'text':0, + 'title1':CSize(0.6), + 'title2':CSize(0.5), + 'title3':CSize(0.4), + 'title4':CSize(0.3), + 'title5':CSize(0.2), + 'title6':CSize(0.1), + } + fontsize = {'font_size': CSize(1) + offset.get(texttype,0)} self._i18n = i18n + self.bgcolor = fgcolor kwargs = kw.copy() config = getConfig() self.wrap = wrap - if config.texttypes: - attrs = config.texttypes.get('texttype',{}) - kwargs.update(attrs) + kwargs.update(fontsize) super().__init__(**kwargs) if self._i18n: self.i18n = I18n() @@ -101,7 +160,9 @@ class Text(Label): if self.wrap: font_size = self.font_size self.text_size = self.width, None - + if self.bgcolor: + self.color = self.bgcolor + def on_size(self,o,size): # super().on_size(o,size) if self.wrap: @@ -120,6 +181,62 @@ class Text(Label): def on_lang(self,o,lang): self.text = self.i18n(self.otext) +class Title1(Text): + def __init__(self, **kw): + kw.update({'texttype':'title1','bold':True}) + Text.__init__(self, **kw) + +class Title2(Text): + def __init__(self, **kw): + kw.update({'texttype':'title2','bold':True}) + Text.__init__(self, **kw) + +class Title3(Text): + def __init__(self, **kw): + kw.update({'texttype':'title3','bold':True}) + Text.__init__(self, **kw) + +class Title4(Text): + def __init__(self, **kw): + kw.update({'texttype':'title4','bold':True}) + Text.__init__(self, **kw) + +class Title5(Text): + def __init__(self, **kw): + kw.update({'texttype':'title5','bold':True}) + Text.__init__(self, **kw) + +class Title6(Text): + def __init__(self, **kw): + kw.update({'texttype':'title6','bold':True}) + Text.__init__(self, **kw) + +class Modal(BGColorBehavior, ModalView): + def __init__(self, auto_open=False, color_level=-1, radius=[], **kw): + kw.update({'auto_dismiss':False}) + ModalView.__init__(self, **kw) + BGColorBehavior.__init__(self, color_level=color_level, + radius=radius) + self.auto_open = auto_open + if self.auto_open: + self.open() + +class TimedModal(Modal): + def __init__(self, show_time=5, **kw): + self.show_time = show_time + self.time_task = None + Modal.__init__(self, **kw) + + def open(self, *args, **kw): + self.time_task = Clock.schedule_once(self.dismiss, self.show_time) + super().open(*args, **kw) + + def dismiss(self, *args, **kw): + if self.time_task: + self.time_task.cancel() + self.time_task = None + super().dismiss(*args, **kw) + class PressableImage(ButtonBehavior,AsyncImage): def on_press(self): pass diff --git a/kivyblocks/bgcolorbehavior.py b/kivyblocks/bgcolorbehavior.py index 1765408..d50988b 100644 --- a/kivyblocks/bgcolorbehavior.py +++ b/kivyblocks/bgcolorbehavior.py @@ -6,47 +6,24 @@ from .color_definitions import getColors _logcnt = 0 class BGColorBehavior(object): def __init__(self, color_level=-1,radius=[],**kwargs): + if color_level==-1: + color_level = 0 self.color_level = color_level self.radius = radius self.bgcolor = [] self.fgcolor = [] - self.useOwnColor = False - if color_level != -1: - fg,bg= getColors(color_level) - self.fgcolor = fg - self.bgcolor = bg - self.normal_bgcolor = bg - self.normal_fgcolor = fg - fg,bg= getColors(color_level,selected=True) - self.selected_bgcolor = bg - self.selected_fgcolor = fg - self.useOwnColor = True - self.on_bgcolor() + fg,bg= getColors(color_level) + self.fgcolor = fg + self.bgcolor = bg + self.normal_bgcolor = bg + self.normal_fgcolor = fg + fg,bg= getColors(color_level,selected=True) + self.selected_bgcolor = bg + self.selected_fgcolor = fg + self.on_bgcolor() self.bind(size=self.onSize_bgcolor_behavior, - pos=self.onSize_bgcolor_behavior) - - def useAcestorColor(self,selected=False): - p = self.parent - cnt = 0 - while p: - if isinstance(p,BGColorBehavior) and p.useOwnColor: - if selected: - self.bgcolor = p.selected_bgcolor - self.color = self.fgcolor = p.selected_fgcolor - else: - self.bgcolor = p.normal_bgcolor - self.color = self.fgcolor = p.normal_fgcolor - self.on_bgcolor() - return - p = p.parent - cnt += 1 - if cnt > 100: - break - fg,bg= getColors(0,selected=selected) - self.bgcolor = bg - self.color = self.fgcolor = fg - self.on_bgcolor() + pos=self.onSize_bgcolor_behavior) def onSize_bgcolor_behavior(self,o,v=None): if not hasattr(self,'rect'): @@ -72,21 +49,15 @@ class BGColorBehavior(object): print('on_bgcolor():self.canvas is None') def selected(self): - if self.useOwnColor: - if self.bgcolor == self.selected_bgcolor: - return - self.bgcolor = self.selected_bgcolor - self.color = self.fgcolor = self.selected_fgcolor - self.on_bgcolor() - else: - self.useAcestorColor(selected=True) + if self.bgcolor == self.selected_bgcolor: + return + self.bgcolor = self.selected_bgcolor + self.color = self.fgcolor = self.selected_fgcolor + self.on_bgcolor() def unselected(self): - if self.useOwnColor: - if self.bgcolor == self.normal_bgcolor: - return - self.bgcolor = self.normal_bgcolor - self.color = self.fgcolor = self.normal_fgcolor - self.on_bgcolor() - else: - self.useAcestorColor(selected=False) + if self.bgcolor == self.normal_bgcolor: + return + self.bgcolor = self.normal_bgcolor + self.color = self.fgcolor = self.normal_fgcolor + self.on_bgcolor() diff --git a/kivyblocks/blocks.py b/kivyblocks/blocks.py index ab87a03..59a02ac 100755 --- a/kivyblocks/blocks.py +++ b/kivyblocks/blocks.py @@ -361,6 +361,7 @@ class Blocks(EventDispatcher): w = Blocks.getWidgetById(desc.get('wid','self'),from_widget=widget) if not w: print(desc.get('wid','self'),'not found via Blocks.getWidgetById()') + return event = desc.get('event') if event is None: return @@ -532,22 +533,34 @@ class Blocks(EventDispatcher): @classmethod def getWidgetById(self,id,from_widget=None): + def find_widget_by_id(id, from_widget=None): + if id=='self': + return from_widget + if hasattr(from_widget,'widget_id'): + if from_widget.widget_id == id: + return from_widget + if hasattr(from_widget, id): + w = getattr(from_widget,id) + if isinstance(w,Widget): + return w + for c in from_widget.children: + ret = find_widget_by_id(id,from_widget=c) + if ret: + return ret + return None + ids = id.split('.') app = App.get_running_app() - if id in ['root','/self']: - print('return app.root',app.root) - return app.root - if id=='self': - return from_widget + if id.startswith('/self') or id.startswith('root'): + from_widget = app.root + ids[0] = 'self' if from_widget is None: from_widget = app.root - if hasattr(from_widget,'widget_id'): - if from_widget.widget_id == id: - return from_widget - for c in from_widget.children: - ret = Blocks.getWidgetById(id,from_widget=c) - if ret: - return ret - return None + for id in ids: + w = find_widget_by_id(id,from_widget=from_widget) + if w is None: + return None + from_widget = w + return w def on_built(self,v=None): return diff --git a/kivyblocks/color_definitions.py b/kivyblocks/color_definitions.py index 99a7aab..31e000e 100644 --- a/kivyblocks/color_definitions.py +++ b/kivyblocks/color_definitions.py @@ -359,8 +359,7 @@ level_bg_colors = [ ] text_colors = { - 'normal':['eeeeee','111111'], - 'highlight':['ffffff','000000'] + 'normal':['ffff99','8b210c'], } def getConfigStyle(): diff --git a/kivyblocks/pagescontainer.py b/kivyblocks/pagescontainer.py index d2dbce5..93dfbab 100644 --- a/kivyblocks/pagescontainer.py +++ b/kivyblocks/pagescontainer.py @@ -5,10 +5,12 @@ from kivy.uix.button import Button from kivy.uix.label import Label from kivy.app import App from .utils import CSize +from .bgcolorbehavior import BGColorBehavior -class PageContainer(FloatLayout): +class PageContainer(BGColorBehavior, FloatLayout): def __init__(self,**kw): - super().__init__(**kw) + FloatLayout.__init__(self, **kw) + BGColorBehavior.__init__(self) self.show_back = True self.pageWidgets = [] self.backButton = Button(text='<',size_hint=(None,None), diff --git a/kivyblocks/register.py b/kivyblocks/register.py index fe5adb6..d6411dd 100644 --- a/kivyblocks/register.py +++ b/kivyblocks/register.py @@ -54,6 +54,16 @@ r('HostImage',HostImage) r('APlayer',APlayer) r('WrapText',WrapText) r('PressableBox',PressableBox) +r('Title1',Title1) +r('Title2',Title2) +r('Title3',Title3) +r('Title4',Title4) +r('Title5',Title5) +r('Title6',Title6) +r('Modal',Modal) +r('TimedModal',TimedModal) +r('HBox',HBox) +r('VBox',VBox) r('ToggleItems',ToggleItems) if platform == 'android': r('PhoneButton',PhoneButton) diff --git a/kivyblocks/toggleitems.py b/kivyblocks/toggleitems.py index d1daec6..e21e193 100644 --- a/kivyblocks/toggleitems.py +++ b/kivyblocks/toggleitems.py @@ -7,21 +7,22 @@ from kivy.factory import Factory from kivyblocks.ready import WidgetReady from kivyblocks.bgcolorbehavior import BGColorBehavior from kivyblocks.utils import CSize +from kivyblocks.baseWidget import Box -class PressableBox(BGColorBehavior, TouchRippleButtonBehavior, BoxLayout): +class PressableBox(TouchRippleButtonBehavior, Box): def __init__(self,border_width=1, color_level=-1, user_data=None, radius=[], **kw): - BoxLayout.__init__(self, padding=[border_width, + Box.__init__(self, padding=[border_width, border_width, border_width, border_width], + color_level=color_level, + radius=radius, **kw) TouchRippleButtonBehavior.__init__(self) - BGColorBehavior.__init__(self,color_level=color_level, - radius=radius) self.border_width = border_width self.user_data = user_data self.unselected()