This commit is contained in:
yumoqing 2020-12-04 23:12:28 +08:00
parent a36ada0574
commit 09d6372b15

View File

@ -6,11 +6,12 @@ from kivy.factory import Factory
from kivyblocks.ready import WidgetReady from kivyblocks.ready import WidgetReady
from kivyblocks.bgcolorbehavior import BGColorBehavior from kivyblocks.bgcolorbehavior import BGColorBehavior
from kivyblocks.utils import CSize
class PressableBox(TouchRippleButtonBehavior, BoxLayout): class PressableBox(BGColorBehavior, TouchRippleButtonBehavior, BoxLayout):
def __init__(self,border_width=1, def __init__(self,border_width=1,
bgcolor=[0,1,0,1], color_level=-1,
selected_color=[1,0,0,1], radius=[],
**kw): **kw):
BoxLayout.__init__(self, padding=[border_width, BoxLayout.__init__(self, padding=[border_width,
border_width, border_width,
@ -18,18 +19,12 @@ class PressableBox(TouchRippleButtonBehavior, BoxLayout):
border_width], border_width],
**kw) **kw)
TouchRippleButtonBehavior.__init__(self) TouchRippleButtonBehavior.__init__(self)
self.b_bgcolor = bgcolor BGColorBehavior.__init__(self,color_level=color_level,
radius=radius)
self.border_width = border_width self.border_width = border_width
self.b_selected_color = selected_color
self.user_data = None self.user_data = None
self.unselected() self.unselected()
def on_size(self,o,s):
if self._selected:
self.selected()
else:
self.unselected()
def on_press(self,o=None): def on_press(self,o=None):
self.selected() self.selected()
@ -39,41 +34,43 @@ class PressableBox(TouchRippleButtonBehavior, BoxLayout):
def get_data(self): def get_data(self):
return self.user_data return self.user_data
def is_selected(self):
return self._selected
def selected(self):
self._selected = True
with self.canvas.before:
Color(*self.b_selected_color)
Rectangle(pos=self.pos,size=self.size)
Color(*self.b_bgcolor)
Rectangle(size=(self.width - 2*self.border_width, self.height - 2*self.border_width),
pos=(int(self.center_x - (self.width - 2*self.border_width)/2.0), int(self.center_y - (self.height - 2*self.border_width)/2.0)))
def unselected(self):
self._selected = False
with self.canvas.before:
Color(*self.b_bgcolor)
Rectangle(pos=self.pos,size=self.size)
class ToggleItems(BGColorBehavior, BoxLayout): class ToggleItems(BGColorBehavior, BoxLayout):
def __init__(self, def __init__(self,
color_level=1, color_level=1,
radius=[], radius=[],
orientation='horizontal',
unit_size=3,
items_desc=[], items_desc=[],
border_width=1, border_width=1,
**kw): **kw):
BoxLayout.__init__(self, **kw) 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)
BGColorBehavior.__init__(self, BGColorBehavior.__init__(self,
color_level=color_level, color_level=color_level,
radius=radius) radius=radius)
self.item_widgets = [] self.item_widgets = []
kw = {
"border_width":border_width,
"color_level":color_level,
"radius":radius
}
kw.update(kw1)
for desc in items_desc: for desc in items_desc:
c = PressableBox(border_width=border_width, c = PressableBox(**kw)
bgcolor=self.normal_bgcolor,
selected_color=self.selected_bgcolor
)
d = desc.get('data') d = desc.get('data')
if d: if d:
c.set_data(d) c.set_data(d)
@ -93,15 +90,15 @@ class ToggleItems(BGColorBehavior, BoxLayout):
self.item_widgets[0].selected() self.item_widgets[0].selected()
else: else:
print('items_desc=',items_desc,'error') print('items_desc=',items_desc,'error')
self.register_event_type('on_changed') self.register_event_type('on_press')
def on_changed(self,v=None): def on_press(self,v=None):
print('Toggle on_changed fired') print('Toggle on_changed fired')
pass pass
def select_item(self,o=None): def select_item(self,o=None):
[i.unselected() for i in self.item_widgets if i != o] [i.unselected() for i in self.item_widgets if i != o]
self.dispatch('on_changed',o.get_data()) self.dispatch('on_press',o.get_data())
def getValue(self): def getValue(self):
for i in self.item_widgets: for i in self.item_widgets: