mirror of https://github.com/kivy/kivy.git
switch checkbox to use ToggleButtonBehavior
This commit is contained in:
parent
28cf1efac0
commit
00cb0bbbe5
|
@ -32,10 +32,11 @@ __all__ = ('CheckBox', )
|
|||
|
||||
from weakref import ref
|
||||
from kivy.uix.widget import Widget
|
||||
from kivy.properties import BooleanProperty, ObjectProperty
|
||||
from kivy.properties import BooleanProperty
|
||||
from kivy.uix.behaviors import ToggleButtonBehavior
|
||||
|
||||
|
||||
class CheckBox(Widget):
|
||||
class CheckBox(ToggleButtonBehavior, Widget):
|
||||
'''CheckBox class, see module documentation for more information.
|
||||
'''
|
||||
|
||||
|
@ -46,71 +47,16 @@ class CheckBox(Widget):
|
|||
to False.
|
||||
'''
|
||||
|
||||
__groups = {}
|
||||
|
||||
group = ObjectProperty(None, allownone=True)
|
||||
'''Group of the checkbox. If None, no group will be used (the checkbox is
|
||||
independent). If specified, the :attr:`group` must be a hashable object
|
||||
such as a string. Only one checkbox in a group can be active.
|
||||
|
||||
:attr:`group` is an :class:`~kivy.properties.ObjectProperty` and
|
||||
defaults to None.
|
||||
'''
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self._previous_group = None
|
||||
super(CheckBox, self).__init__(**kwargs)
|
||||
|
||||
def on_group(self, *largs):
|
||||
groups = CheckBox.__groups
|
||||
if self._previous_group:
|
||||
group = groups[self._previous_group]
|
||||
for item in group[:]:
|
||||
if item() is self:
|
||||
group.remove(item)
|
||||
break
|
||||
group = self._previous_group = self.group
|
||||
if group not in groups:
|
||||
groups[group] = []
|
||||
r = ref(self, CheckBox._clear_groups)
|
||||
groups[group].append(r)
|
||||
|
||||
def _release_group(self, current):
|
||||
if self.group is None:
|
||||
return
|
||||
group = self.__groups[self.group]
|
||||
for item in group[:]:
|
||||
widget = item()
|
||||
if widget is None:
|
||||
group.remove(item)
|
||||
if widget is current:
|
||||
continue
|
||||
widget.active = False
|
||||
def on_state(self, instance, value):
|
||||
if value == 'down':
|
||||
self.active = True
|
||||
else:
|
||||
self.active = False
|
||||
|
||||
def _toggle_active(self):
|
||||
self._release_group(self)
|
||||
self.active = not self.active
|
||||
self._do_press()
|
||||
|
||||
def _release(self):
|
||||
self.active = not self.active
|
||||
|
||||
def on_touch_down(self, touch):
|
||||
if not self.collide_point(*touch.pos):
|
||||
return
|
||||
if self.disabled:
|
||||
return True
|
||||
if self.group is None or self.group == '':
|
||||
self._release()
|
||||
elif self.group:
|
||||
if not self.active:
|
||||
self._toggle_active()
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def _clear_groups(wk):
|
||||
# auto flush the element when the weak reference have been deleted
|
||||
groups = CheckBox.__groups
|
||||
for group in list(groups.values()):
|
||||
if wk in group:
|
||||
group.remove(wk)
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue