kivyblocks/kivyblocks/toolbar.py

241 lines
5.7 KiB
Python
Raw Normal View History

2020-04-02 13:18:10 +08:00
from kivy.logger import Logger
2019-12-19 11:13:47 +08:00
from kivy.graphics import Color, Rectangle
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import AsyncImage
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.clock import Clock
2021-01-26 13:59:30 +08:00
from kivy.factory import Factory
2019-12-19 11:13:47 +08:00
from appPublic.dictObject import DictObject
2021-02-18 14:39:03 +08:00
from appPublic.registerfunction import RegisterFunction
2019-12-19 11:13:47 +08:00
from .widgetExt.scrollwidget import ScrollWidget
from .utils import *
from .ready import WidgetReady
2020-04-02 13:18:10 +08:00
from .color_definitions import getColors
from .bgcolorbehavior import BGColorBehavior
2020-12-10 23:47:30 +08:00
from .baseWidget import Text
2021-01-16 10:12:16 +08:00
from .toggleitems import PressableBox, ToggleItems
2019-12-19 11:13:47 +08:00
"""
toolbar options
{
2021-01-16 10:12:16 +08:00
color_level:
radius:
"mode":"icon", "icontext","text"
2019-12-19 11:13:47 +08:00
img_size=1.5,
2021-01-16 10:12:16 +08:00
text_size=0.5,
2019-12-19 11:13:47 +08:00
tools:[
{
"name":"myid",
"img_src":"gggggg",
"label":"gggggg"
},
...
]
}
"""
2021-01-16 10:12:16 +08:00
class Toolbar(BoxLayout):
def __init__(self, color_level=-1,
radius=[],
img_size=1.5,
text_size=0.5,
tools=[], **opts):
self.color_level = color_level
self.radius = radius
self.img_size = img_size
self.text_size = text_size
self.tools = tools
2019-12-19 11:13:47 +08:00
self.tool_widgets={}
2021-01-16 10:12:16 +08:00
BoxLayout.__init__(self, **opts)
self.register_event_type('on_press')
2019-12-19 11:13:47 +08:00
self.size_hint = (1,None)
first = True
2021-01-16 10:12:16 +08:00
subs_desc = []
for opt in self.tools:
subwidgets = []
img_src = opt.get('img_src',None)
if img_src:
subwidgets.append({
"widgettype":"AsyncImage",
"options":{
"size_hint_y":None,
"height":CSize(self.img_size),
"source":img_src
}
})
text = opt.get('label', None)
if text:
subwidgets.append({
"widgettype":"Text",
"options":{
"size_hint_y":None,
"i18n":True,
"height":CSize(self.text_size),
"font_size":CSize(self.text_size),
"text":text
}
})
desc = {
"widgettype":"VBox",
"options":{
},
"subwidgets":subwidgets,
"data":opt.get('name')
}
subs_desc.append(desc)
self.toggle_items = ToggleItems(
color_level=self.color_level,
radius=self.radius,
unit_size=self.img_size + self.text_size,
items_desc=subs_desc)
for ti in self.toggle_items.children:
ti.widget_id = ti.user_data
self.height = CSize(self.img_size + self.text_size) + 10
self.size_hint_y = None
self.toggle_items.bind(on_press=self.tool_press)
self.add_widget(self.toggle_items)
def on_press(self, o):
print('on_press(),', o)
2019-12-19 11:13:47 +08:00
def tool_press(self,o,v=None):
2021-01-16 10:12:16 +08:00
self.dispatch('on_press',self.toggle_items.getValue())
2019-12-19 11:13:47 +08:00
"""
Toolpage options
{
2021-01-26 13:59:30 +08:00
img_size:1.5,
text_size:0.7,
2019-12-19 11:13:47 +08:00
tool_at:"left","right","top","bottom",
2021-01-26 13:59:30 +08:00
color_level:0,
show_name:
2019-12-19 11:13:47 +08:00
tools:[
{
"name":"myid",
"img_src":"gggggg",
"text":"gggggg"
2021-01-26 13:59:30 +08:00
"flush":true
2019-12-19 11:13:47 +08:00
"url":"ggggggggg"
},
...
]
"""
2020-04-02 13:18:10 +08:00
class ToolPage(BGColorBehavior, BoxLayout):
2021-01-26 13:59:30 +08:00
def __init__(self,color_level=-1,radius=[],
show_name=None, tool_at='top', **opts):
2019-12-19 11:13:47 +08:00
self.opts = DictObject(**opts)
2021-01-16 10:12:16 +08:00
if tool_at in [ 'top','bottom']:
2019-12-19 11:13:47 +08:00
orient = 'vertical'
else:
orient = 'horizontal'
2021-01-26 13:59:30 +08:00
names = [i.name for i in self.opts.tools]
if not show_name or \
not show_name not in names:
show_name = self.opts.tools[0].name
self.content_widgets = {}
self.show_name = show_name
2020-12-02 12:38:53 +08:00
self.color_level=self.opts.color_level or 0
2021-01-16 10:12:16 +08:00
self.sub_radius = self.opts.radius
self.tool_at = tool_at
2020-04-02 13:18:10 +08:00
BoxLayout.__init__(self,orientation=orient)
2020-12-02 12:38:53 +08:00
BGColorBehavior.__init__(self,
color_level=color_level,
2021-01-16 10:12:16 +08:00
radius=[])
2019-12-19 11:13:47 +08:00
self.content = None
self.toolbar = None
self.init()
2021-01-26 13:59:30 +08:00
Clock.schedule_once(self.show_page, 0.5)
2019-12-19 11:13:47 +08:00
2021-01-26 13:59:30 +08:00
def show_page(self, *args):
toggle_items = self.toolbar.toggle_items
for c in toggle_items.children:
cvalue = c.getValue()
if cvalue == self.show_name:
c.dispatch('on_press')
2019-12-19 11:13:47 +08:00
def on_size(self,obj,size):
if self.content is None:
return
2021-01-16 10:12:16 +08:00
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
else:
self.toolbar.height = self.height
self.content.height = self.height
self.content.width = self.width - self.toolbar.width
2019-12-19 11:13:47 +08:00
def init(self):
self.initFlag = True
self.mywidgets = {}
self.content = BoxLayout()
2020-11-19 12:22:16 +08:00
self.content.widget_id = 'content'
2021-01-16 10:12:16 +08:00
opts = self.opts.copy()
if self.tool_at in ['top','bottom']:
opts['size_hint_x'] = 1
opts['size_hint_y'] = None
opts['height'] = CSize(self.opts.img_size + \
self.opts.text_size) + 10
else:
opts['size_hint_y'] = 1
opts['size_hint_x'] = None
opts['width'] = CSize(self.opts.img_size + \
self.opts.text_size) + 10
self.toolbar = Toolbar(color_level=self.color_level,
radius=self.sub_radius,
**opts)
if self.tool_at in ['top','left']:
2019-12-19 11:13:47 +08:00
self.add_widget(self.toolbar)
self.add_widget(self.content)
else:
self.add_widget(self.content)
self.add_widget(self.toolbar)
2021-01-26 13:59:30 +08:00
toggle_items = self.toolbar.toggle_items
for t in toggle_items.children:
t.bind(on_press=self.on_press_handle)
def get_tool_by_name(self,name):
for t in self.opts.tools:
if t.name == name:
return t
return None
def build_widget(self, url):
desc = {
"widgettype":"urlwidget",
"options":{
"url":url
}
}
b = Factory.Blocks()
return b.widgetBuild(desc)
def on_press_handle(self, o):
name = o.getValue()
t = self.get_tool_by_name(name)
w = self.content_widgets.get(name)
if w is None or t.fresh:
2021-02-18 14:39:03 +08:00
if t.url:
w = self.build_widget(t.url)
self.content_widgets[name] = w
return
if t.rfname:
rf = RegisterFunction()
f = rf.get(t.rfname)
if f:
return f()
return
2021-01-26 13:59:30 +08:00
if w:
2021-02-18 14:39:03 +08:00
print('toolbar.py: Use old widget')
2021-01-26 13:59:30 +08:00
self.content.clear_widgets()
self.content.add_widget(w)
2019-12-19 11:13:47 +08:00