This commit is contained in:
yumoqing 2021-12-17 22:28:15 +08:00
parent 979ac006b2
commit c0c749f66c
5 changed files with 119 additions and 92 deletions

View File

@ -74,7 +74,7 @@ from .threadcall import HttpClient
from .i18n import I18n
from .widget_css import WidgetCSS
from .ready import WidgetReady
from .utils import CSize
from .utils import CSize, kwarg_pop
from .swipebehavior import SwipeBehavior
if platform == 'android':
@ -90,6 +90,7 @@ class WrapText(Label):
class Box(WidgetCSS, WidgetReady, BoxLayout):
def __init__(self, **kw):
try:
kwarg_pop(self, kw)
super(Box, self).__init__(**kw)
except Exception as e:
print('Box(',kw,') Error')
@ -108,6 +109,7 @@ class VBox(Box):
class SwipeBox(SwipeBehavior, Box):
def __init__(self, **kw):
kwarg_pop(self, kw)
super(SwipeBox, self).__init__(**kw)
@ -138,6 +140,7 @@ class Text(Label):
pass
else:
kwargs.update(fontsize)
kwarg_pop(self, kwargs)
super().__init__(**kwargs)
if self._i18n:
self.i18n = I18n()

View File

@ -10,7 +10,7 @@ from kivy.properties import NumericProperty, DictProperty, \
from kivyblocks.ready import WidgetReady
from kivyblocks.bgcolorbehavior import BGColorBehavior
from kivyblocks.utils import CSize
from kivyblocks.utils import CSize, kwarg_pop
from kivyblocks.baseWidget import Box, Text
from kivyblocks.widget_css import WidgetCSS
@ -23,6 +23,7 @@ class ClickableBox(TouchRippleButtonBehavior, Box):
def __init__(self,
border_width=1,
**kw):
kwarg_pop(self, kw)
super(ClickableBox, self).__init__(
padding=[border_width,
border_width,
@ -38,11 +39,15 @@ class ClickableBox(TouchRippleButtonBehavior, Box):
class ClickableText(ClickableBox):
text = StringProperty(' ')
fontsize = NumericProperty(1)
def __init__(self, **kw):
print('ClickableText begin inited')
self.txt_w = None
kwarg_pop(self, kw)
super(ClickableText, self).__init__(**kw)
self.txt_w = TinyText(text=self.text, i18n=True)
self.txt_w = TinyText(otext=self.text,
i18n=True,
font_size=CSize(self.fontsize))
self.txt_w.bind(texture_size=self.reset_size)
self.add_widget(self.txt_w)
self.txt_w.size_hint = (None, None)
@ -67,6 +72,7 @@ class ClickableIconText(ClickableText):
img_kw = DictProperty({})
def __init__(self, **kw):
self.img_w = None
kwarg_pop(self, kw)
super(ClickableIconText, self).__init__(**kw)
print('ClickableIconText inited')
@ -101,6 +107,7 @@ class ToggleText(ClickableText):
css_on = StringProperty('default')
css_off = StringProperty('default')
def __init__(self, **kw):
kwarg_pop(self, kw)
super(ToggleText, self).__init__(**kw)
def toggle(self):
@ -136,6 +143,7 @@ class ToggleIconText(ToggleText):
img_kw = DictProperty({})
def __init__(self, **kw):
self.img_w = None
kwarg_pop(self, kw)
super(ToggleIconText, self).__init__(**kw)
self.source = self.source_off
self.img_w = AsyncImage(source=self.source, **self.img_kw)
@ -164,6 +172,7 @@ class ClickableImage(ClickableBox):
img_kw = DictProperty(None)
def __init__(self, **kw):
self.img_w = None
kwarg_pop(self, kw)
super(ClickableImage, self).__init__(**kw)
self.img_w = AsyncImage(source=self.source, **self.img_kw)
self.add_widget(self.img_w)
@ -180,6 +189,7 @@ class ToggleImage(ClickableImage):
source_off = StringProperty(None)
select_state = BooleanProperty(False)
def __init__(self, **kw):
kwarg_pop(self, kw)
super(ToggleImage, self).__init__(**kw)
self.source = self.source_on

View File

@ -12,34 +12,35 @@ class ScrollPanel(ScrollView):
y_padding_c = NumericProperty(0)
bgcolor = ListProperty([0.2, 0.2, 0.2, 1])
orient = StringProperty('V')
def __init__(self,inner=None, **kw):
print('ScrollPanel:kw=', kw)
super(ScrollPanel,self).__init__(**kw)
self.effect_cls = ScrollEffect
if not inner:
kw = {
'size_hint':(None,None),
'bgcolor':self.bgcolor,
'orientation':'vertical' if self.orient=='V' else 'horizontal'
}
desc = {
"widgettype":"Box",
"options":kw
}
self._inner = Factory.Blocks().widgetBuild(desc)
if not self._inner:
print('desc=', desc)
raise Exception('widget build failed')
if isinstance(self._inner, BoxLayout):
if self.orient.upper() == 'H':
self._inner.orientation = 'horizontal'
else:
self._inner.orientation = 'vertical'
self._inner.padding = self._inner.spacing = \
[CSize(self.x_padding_c), CSize(self.y_padding_c)]
elif isinstance(inner, Widget):
self._inner = inner
else:
self._inner = Factory.Blocks().widgetBuild(inner)
if isinstance(self._inner, BoxLayout):
if self.orient.upper() == 'H':
self._inner.orientation = 'horizontal'
else:
self._inner.orientation = 'vertical'
self._inner.padding = self._inner.spacing = \
[CSize(self.x_padding_c), CSize(self.y_padding_c)]
self._inner.bind(
minimum_height=self._inner.setter('height'))
self._inner.bind(

View File

@ -9,7 +9,8 @@ from kivy.uix.widget import Widget
from kivy.app import App
from kivy.clock import Clock
from kivy.factory import Factory
from kivy.properties import StringProperty, ListProperty, NumericProperty
from kivy.properties import StringProperty, DictProperty, \
ListProperty, NumericProperty
from appPublic.dictObject import DictObject
from appPublic.registerfunction import RegisterFunction
@ -77,8 +78,8 @@ class Toolbar(Box):
"size_hint_y":None,
"i18n":True,
"height":CSize(self.text_size),
"font_size":CSize(self.text_size),
"text":text
"fontsize":CSize(self.text_size),
"otext":text
}
})
desc = {
@ -142,29 +143,53 @@ class ScrollToolbar(ScrollPanel):
}
toolbar has follow attributes
{
"img_height_c":image height in charecter size
"img_width_c":image width in charecter size
"padding_c":x spacing
"img_size_c":image height in charecter size
"text_size_c":
"orient":"H" or "V"
"tool_orient":"horizontal" or "vertical"
"css_on",
"css_off",
"tools":
"tool_orient"
}
css_on='default',
css_off='default',
tools=[],
tool_orient = 'horizontal',
orient='H',
padding_c=1,
img_size_c=2,
text_size_c=1,
self.css_on = css_on
self.css_off = css_off
self.tools=tools
self.tool_orient = tool_orient
self.orient = orient
self.padding_c = padding_c
self.img_size_c = img_size_c
self.text_size_c = text_size_c
"""
css_on = StringProperty('default')
css_off = StringProperty('default')
tools = ListProperty([])
tool_orient = StringProperty('horizontal')
img_height_c = NumericProperty(2)
img_width_c = NumericProperty(2)
orient = StringProperty('H')
padding_c = NumericProperty(1)
img_size_c = NumericProperty(2)
text_size_c = NumericProperty(1)
def __init__(self, **kw):
print('#############init begin##########')
kwarg_pop(self, kw)
super(ScrollToolbar, self).__init__(**kw)
print('#############super init end##########')
self.register_event_type('on_press')
if self.orient == 'H':
self._inner.orientation = 'horizontal'
else:
self._inner.orientation = 'vertical'
self.clear_widgets()
self.w_dic = {}
for t in self.tools:
label = t.get('label')
label = t.get('label', t.get('name', None))
source_on = t.get('source_on', t.get('img_src',None))
source_off = t.get('source_off', t.get('img_src', None))
ToggleIconText = Factory.ToggleIconText
@ -174,19 +199,21 @@ class ScrollToolbar(ScrollPanel):
w = ToggleIconText(css_on=self.css_on,
css_off=self.css_off,
text=label,
fontsize=CSize(self.text_size_c),
source_on=source_on,
source_off=source_off,
orientation=self.tool_orient,
img_kw={
"size_hint":(None, None),
"height":CSize(self.img_height_c),
"width":CSize(self.img_width_c)
"height":CSize(self.img_size_c),
"width":CSize(self.img_size_c)
}
)
elif label:
w = ToggleText(css_on=self.css_on,
css_off=self.css_off,
orientation=self.tool_orient,
fontsize=CSize(self.text_size_c),
text=label)
elif source_on:
w = ToggleImage( source_on=source_on,
@ -194,8 +221,8 @@ class ScrollToolbar(ScrollPanel):
orientation=self.tool_orient,
img_kw={
"size_hint":(None, None),
"height":CSize(self.img_height_c),
"width":CSize(self.img_width_c)
"height":CSize(self.img_size_c),
"width":CSize(self.img_size_c)
}
)
@ -203,6 +230,19 @@ class ScrollToolbar(ScrollPanel):
self.add_widget(w)
self.w_dic[w] = t
w.bind(on_press=self.tool_press)
if self.orient == 'horizontal':
self.do_scroll_y = False
self.size_hint_y = None
if len(self.w_dic.keys()) > 0:
self.height = max([w.height for w in self.w_dic.keys()])
self._inner.spacing = CSize(self.padding_c,0)
else:
self.do_scroll_x = False
self.size_hint_x = None
if len(self.w_dic.keys()) > 0:
self.width = max([w.width for w in self.w_dic.keys() ])
self._inner.spacing = CSize(0, self.padding_c)
def on_press(self, o):
pass
@ -224,83 +264,49 @@ class ScrollToolbar(ScrollPanel):
self.dispatch('on_press', ret_v)
class ToolPage(BoxLayout):
normal_css=StringProperty(None)
actived_css=StringProperty(None)
toolbar_size = NumericProperty(2)
img_size = NumericProperty(1.5)
text_size = NumericProperty(0.7)
tool_at = StringProperty('top')
tools = ListProperty([])
toolbar = DictProperty({})
def __init__(self, **kw):
if self.tool_at in [ 'top','bottom']:
orient = 'vertical'
else:
orient = 'horizontal'
kw['orientation'] = orient
print('ToolPage:kw=',kw)
super(ToolPage, self).__init__(**kw)
if self.tool_at in [ 'top','bottom']:
self.orientation = 'vertical'
else:
self.orientation = 'horizontal'
if not self.toolbar_size:
self.toolbar_size = self.img_size + self.text_size + 0.3
self.content_widgets = {}
self.content = None
self.toolbar = None
self.content_w = None
self.toolbar_w = None
self.init()
name = self.tools[0]['name']
self.toolbar.select(name)
name = self.toolbar['tools'][0]['name']
self.toolbar_w.select(name)
def show_page(self, *args):
toggle_items = self.toolbar.toggle_items
for c in toggle_items.item_widgets:
cvalue = c.getValue()
if cvalue == self.show_name:
c.dispatch('on_press')
def on_size(self,obj,size):
if self.content is None:
if self.content_w is None:
return
if self.tool_at in ['top','bottom']:
self.toolbar.width = self.width
self.content.width = self.width
self.content.height = self.height - self.toolbar.height
self.toolbar_w.width = self.width
self.content_w.width = self.width
self.content_w.height = self.height - self.toolbar_w.height
else:
self.toolbar.height = self.height
self.content.height = self.height
self.content.width = self.width - self.toolbar.width
print(f'tb-width={self.toolbar.width}')
print(f'tb-height={self.toolbar.height}')
print(f'c-height={self.content.height}')
print(f'c-width={self.content.width}')
print(f'height={self.height}')
print(f'width={self.width}')
self.toolbar_w.height = self.height
self.content_w.height = self.height
self.content_w.width = self.width - self.toolbar_w.width
def init(self):
self.initFlag = True
self.mywidgets = {}
self.content = BoxLayout()
self.content.widget_id = 'content'
opts = {}
opts['img_height_c'] = self.img_size
opts['img_width_c'] = self.img_size
opts['css_on'] = self.actived_css
opts['css_off'] = self.normal_css
opts['orient'] = 'H'
if self.tool_at in ['top','bottom']:
opts['size_hint_x'] = 1
opts['size_hint_y'] = None
opts['height'] = CSize(self.toolbar_size)
else:
opts['size_hint_y'] = 1
opts['size_hint_x'] = None
opts['width'] = CSize(self.toolbar_size)
opts['tools'] = self.tools
self.toolbar = ScrollToolbar(**opts)
self.content_w = BoxLayout()
self.content_w.widget_id = 'content'
self.toolbar_w = ScrollToolbar(**self.toolbar)
if self.tool_at in ['top','left']:
self.add_widget(self.toolbar)
self.add_widget(self.content)
self.add_widget(self.toolbar_w)
self.add_widget(self.content_w)
else:
self.add_widget(self.content)
self.add_widget(self.toolbar)
self.add_widget(self.content_w)
self.add_widget(self.toolbar_w)
self.toolbar.bind(on_press=self.on_press_handle)
self.toolbar_w.bind(on_press=self.on_press_handle)
def build_widget(self, url):
desc = {
@ -316,14 +322,14 @@ class ToolPage(BoxLayout):
name = v.get('name')
fress = v.get('fress')
w = self.content_widgets.get(name)
self.content.clear_widgets()
self.content_w.clear_widgets()
if w is None or fresh:
url = v.get('url')
if url:
w = self.build_widget(url)
if w:
self.content_widgets[name] = w
self.content.add_widget(w)
self.content_w.add_widget(w)
return
rfname = v.get('rfname')
if rfname:
@ -332,8 +338,9 @@ class ToolPage(BoxLayout):
if f:
r = f()
if isinstance(r,Widget):
self.content.add_widget(r)
self.content_w.add_widget(r)
return
if w:
self.content.add_widget(w)
self.content_w.add_widget(w)
Factory.register('ScrollToolbar', ScrollToolbar)

View File

@ -31,6 +31,12 @@ class HTTPError(Exception):
alert_widget= None
def kwarg_pop(obj, kw):
keys = [k for k in kw.keys()]
for k in keys:
if hasattr(obj, k):
setattr(obj, k, kw.pop(k))
def blockImage(name):
p = os.path.dirname(os.path.abspath(__file__))
return os.path.join(p,'imgs',name)