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.bgcolorbehavior import BGColorBehavior
from kivyblocks.utils import CSize
class PressableBox(TouchRippleButtonBehavior, BoxLayout):
class PressableBox(BGColorBehavior, TouchRippleButtonBehavior, BoxLayout):
def __init__(self,border_width=1,
bgcolor=[0,1,0,1],
selected_color=[1,0,0,1],
color_level=-1,
radius=[],
**kw):
BoxLayout.__init__(self, padding=[border_width,
border_width,
@ -18,18 +19,12 @@ class PressableBox(TouchRippleButtonBehavior, BoxLayout):
border_width],
**kw)
TouchRippleButtonBehavior.__init__(self)
self.b_bgcolor = bgcolor
BGColorBehavior.__init__(self,color_level=color_level,
radius=radius)
self.border_width = border_width
self.b_selected_color = selected_color
self.user_data = None
self.unselected()
def on_size(self,o,s):
if self._selected:
self.selected()
else:
self.unselected()
def on_press(self,o=None):
self.selected()
@ -39,41 +34,43 @@ class PressableBox(TouchRippleButtonBehavior, BoxLayout):
def get_data(self):
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):
def __init__(self,
color_level=1,
radius=[],
orientation='horizontal',
unit_size=3,
items_desc=[],
border_width=1,
**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,
color_level=color_level,
radius=radius)
self.item_widgets = []
kw = {
"border_width":border_width,
"color_level":color_level,
"radius":radius
}
kw.update(kw1)
for desc in items_desc:
c = PressableBox(border_width=border_width,
bgcolor=self.normal_bgcolor,
selected_color=self.selected_bgcolor
)
c = PressableBox(**kw)
d = desc.get('data')
if d:
c.set_data(d)
@ -93,15 +90,15 @@ class ToggleItems(BGColorBehavior, BoxLayout):
self.item_widgets[0].selected()
else:
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')
pass
def select_item(self,o=None):
[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):
for i in self.item_widgets: