2020-10-31 11:13:59 +08:00
|
|
|
from functools import partial
|
2021-03-27 14:04:10 +08:00
|
|
|
from kivy.logger import Logger
|
2020-11-18 10:29:57 +08:00
|
|
|
from kivy.uix.behaviors import TouchRippleButtonBehavior
|
2020-10-31 11:13:59 +08:00
|
|
|
from kivy.graphics import Color, Rectangle
|
|
|
|
from kivy.uix.boxlayout import BoxLayout
|
|
|
|
from kivy.factory import Factory
|
2021-03-27 14:04:10 +08:00
|
|
|
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
|
2020-10-31 11:13:59 +08:00
|
|
|
|
|
|
|
from kivyblocks.ready import WidgetReady
|
2020-11-01 21:26:26 +08:00
|
|
|
from kivyblocks.bgcolorbehavior import BGColorBehavior
|
2020-12-04 23:12:28 +08:00
|
|
|
from kivyblocks.utils import CSize
|
2020-12-25 15:39:28 +08:00
|
|
|
from kivyblocks.baseWidget import Box
|
2021-03-27 14:04:10 +08:00
|
|
|
from kivyblocks.widget_css import WidgetCSS
|
2020-10-31 11:13:59 +08:00
|
|
|
|
2020-12-25 15:39:28 +08:00
|
|
|
class PressableBox(TouchRippleButtonBehavior, Box):
|
2021-03-27 14:04:10 +08:00
|
|
|
normal_css = StringProperty("default")
|
|
|
|
actived_css = StringProperty("default")
|
|
|
|
box_actived = BooleanProperty(False)
|
2021-05-07 22:17:56 +08:00
|
|
|
def __init__(self,
|
|
|
|
border_width=1,
|
2020-12-19 16:58:09 +08:00
|
|
|
user_data=None,
|
2020-12-04 23:12:28 +08:00
|
|
|
radius=[],
|
2020-10-31 11:13:59 +08:00
|
|
|
**kw):
|
2021-03-27 14:04:10 +08:00
|
|
|
super(PressableBox, self).__init__(
|
|
|
|
padding=[border_width,
|
2020-10-31 11:13:59 +08:00
|
|
|
border_width,
|
|
|
|
border_width,
|
|
|
|
border_width],
|
2020-12-25 15:39:28 +08:00
|
|
|
radius=radius,
|
2020-10-31 11:13:59 +08:00
|
|
|
**kw)
|
|
|
|
self.border_width = border_width
|
2020-12-19 16:58:09 +08:00
|
|
|
self.user_data = user_data
|
2021-03-27 14:04:10 +08:00
|
|
|
self.actived = False
|
|
|
|
self.csscls = self.normal_css
|
|
|
|
|
2021-05-18 12:42:43 +08:00
|
|
|
def active(self, flag):
|
|
|
|
self.box_actived = flag
|
|
|
|
|
2021-03-27 14:04:10 +08:00
|
|
|
def on_box_actived(self, o, v):
|
|
|
|
if self.box_actived:
|
|
|
|
self.csscls = self.actived_css
|
|
|
|
else:
|
|
|
|
self.csscls = self.normal_css
|
2020-10-31 11:13:59 +08:00
|
|
|
|
|
|
|
def on_press(self,o=None):
|
2021-03-27 14:04:10 +08:00
|
|
|
self.box_actived = True
|
|
|
|
|
2020-10-31 11:13:59 +08:00
|
|
|
|
2020-12-10 23:47:30 +08:00
|
|
|
def setValue(self,d):
|
2020-10-31 11:13:59 +08:00
|
|
|
self.user_data = d
|
|
|
|
|
2020-12-10 23:47:30 +08:00
|
|
|
def getValue(self):
|
2020-10-31 11:13:59 +08:00
|
|
|
return self.user_data
|
|
|
|
|
2021-01-16 10:12:16 +08:00
|
|
|
"""
|
|
|
|
ToggleItems format:
|
|
|
|
{
|
|
|
|
radius:
|
|
|
|
unit_size:
|
|
|
|
items_desc:
|
|
|
|
border_width:
|
|
|
|
orientation:
|
|
|
|
}
|
|
|
|
"""
|
2021-03-27 14:04:10 +08:00
|
|
|
class ToggleItems(BoxLayout, WidgetCSS):
|
2020-10-31 11:13:59 +08:00
|
|
|
def __init__(self,
|
2020-12-02 12:38:53 +08:00
|
|
|
radius=[],
|
2020-12-04 23:12:28 +08:00
|
|
|
orientation='horizontal',
|
|
|
|
unit_size=3,
|
2020-10-31 11:13:59 +08:00
|
|
|
items_desc=[],
|
|
|
|
border_width=1,
|
2021-03-27 14:04:10 +08:00
|
|
|
normal_css="default",
|
|
|
|
actived_css="default",
|
2020-10-31 11:13:59 +08:00
|
|
|
**kw):
|
2020-12-04 23:12:28 +08:00
|
|
|
self.unit_size_pix = CSize(unit_size)
|
|
|
|
kw1 = {
|
|
|
|
"orientation":orientation
|
|
|
|
}
|
|
|
|
kw1.update(kw)
|
|
|
|
if orientation=='horizontal':
|
|
|
|
kw1['size_hint_y'] = None
|
|
|
|
kw1['height'] = self.unit_size_pix
|
|
|
|
kw1['size_hint_min_x'] = self.unit_size_pix
|
|
|
|
else:
|
|
|
|
kw1['size_hint_x'] = None
|
|
|
|
kw1['width'] = self.unit_size_pix
|
|
|
|
kw1['size_hint_min_y'] = self.unit_size_pix
|
|
|
|
|
|
|
|
BoxLayout.__init__(self, **kw1)
|
2020-10-31 11:13:59 +08:00
|
|
|
self.item_widgets = []
|
2020-12-04 23:12:28 +08:00
|
|
|
kw = {
|
|
|
|
"border_width":border_width,
|
2021-03-27 14:04:10 +08:00
|
|
|
"normal_css":normal_css,
|
|
|
|
"actived_css":actived_css,
|
2020-12-04 23:12:28 +08:00
|
|
|
"radius":radius
|
|
|
|
}
|
|
|
|
kw.update(kw1)
|
|
|
|
|
2020-10-31 11:13:59 +08:00
|
|
|
for desc in items_desc:
|
2020-12-04 23:12:28 +08:00
|
|
|
c = PressableBox(**kw)
|
2020-10-31 11:13:59 +08:00
|
|
|
self.item_widgets.append(c)
|
|
|
|
self.add_widget(c)
|
|
|
|
c.bind(on_press=self.select_item)
|
2021-01-23 06:13:42 +08:00
|
|
|
if desc.get('data'):
|
|
|
|
c.setValue(desc['data'])
|
|
|
|
|
2020-10-31 11:13:59 +08:00
|
|
|
b = Factory.Blocks()
|
|
|
|
def cb(c,o,w):
|
|
|
|
c.add_widget(w)
|
|
|
|
def eb(desc,o,e):
|
|
|
|
print(desc,'error',e)
|
|
|
|
b.bind(on_built=partial(cb,c))
|
|
|
|
b.bind(on_failed=partial(eb,desc))
|
2020-11-19 12:22:16 +08:00
|
|
|
b.widgetBuild(desc)
|
2020-10-31 11:13:59 +08:00
|
|
|
|
2020-11-01 21:26:26 +08:00
|
|
|
if len(self.item_widgets) > 0:
|
2021-03-27 14:04:10 +08:00
|
|
|
self.item_widgets[0].actived = True
|
2020-11-01 21:26:26 +08:00
|
|
|
else:
|
|
|
|
print('items_desc=',items_desc,'error')
|
2020-12-04 23:12:28 +08:00
|
|
|
self.register_event_type('on_press')
|
2020-10-31 11:13:59 +08:00
|
|
|
|
2020-12-04 23:12:28 +08:00
|
|
|
def on_press(self,v=None):
|
2021-01-23 06:13:42 +08:00
|
|
|
print('Toggle on_press fired')
|
2020-11-01 21:26:26 +08:00
|
|
|
pass
|
|
|
|
|
2020-10-31 11:13:59 +08:00
|
|
|
def select_item(self,o=None):
|
2021-03-27 14:04:10 +08:00
|
|
|
for i in self.item_widgets:
|
|
|
|
if i!=o:
|
|
|
|
i.box_actived = False
|
|
|
|
|
2020-12-10 23:47:30 +08:00
|
|
|
self.dispatch('on_press',o.getValue())
|
2020-10-31 11:13:59 +08:00
|
|
|
|
|
|
|
def getValue(self):
|
|
|
|
for i in self.item_widgets:
|
2021-03-27 14:04:10 +08:00
|
|
|
if i.actived:
|
2020-12-10 23:47:30 +08:00
|
|
|
return i.getValue()
|
2020-10-31 11:13:59 +08:00
|
|
|
return None
|
|
|
|
|
2020-11-01 21:26:26 +08:00
|
|
|
def setValue(self,v):
|
|
|
|
for i in self.item_widgets:
|
2020-12-10 23:47:30 +08:00
|
|
|
if i.getValue() == v:
|
2021-03-27 14:04:10 +08:00
|
|
|
i.actived = True
|
2021-01-16 10:12:16 +08:00
|
|
|
self.select_item(i)
|
2021-03-27 14:04:10 +08:00
|
|
|
else:
|
|
|
|
i.actived = False
|
2020-11-01 21:26:26 +08:00
|
|
|
|