This commit is contained in:
yumoqing 2021-12-07 09:04:54 +08:00
parent 6621d9583f
commit db03b70792
3 changed files with 55 additions and 27 deletions

View File

@ -59,6 +59,18 @@ class ToggleText(ClickableText):
def __init__(self, **kw):
super(ToggleText, self).__init__(**kw)
def toggle(self):
self.select_state = False if self.select_state else True
def select(self, flag):
if flag:
self.select_state = True
else:
self.select_state = False
def state(self):
return self.select_state
def on_press(self, o=None):
self.select_state = False if self.select_state else True
@ -90,18 +102,35 @@ class ToggleImage(ClickableImage):
def __init__(self, **kw):
super(ToggleImage, self).__init__(**kw)
def toggle(self):
self.select_state = False if self.select_state else True
def select(self, flag):
if flag:
self.select_state = True
else:
self.select_state = False
def state(self):
return self.select_state
def on_press(self, o):
self.select_state = False if self.select_state else True
def on_select_state(self, o, f):
if self.img_w:
if f:
if self.select_state and self.on_source is not None:
self.img_w.source = self.on_source
else:
self.img_w.source = self.source
return
if f:
if self.select_state and self.on_source is not None:
self.img_w = AsyncImage(source=self.on_source)
else:
self.img_w = AsyncImage(source=self.source)
r = Factory.register
r('ClickableBox', ClickableBox)
r('ClickableText',ClickableText)
r('ToggleText',ToggleText)
r('ClickableImage',ClickableImage)

View File

@ -2,6 +2,7 @@ from kivy.properties import BooleanProperty, StringProperty,\
ListProperty, DictProperty
from kivyblocks.baseWidgets import VBox, PressableText
from kivyblocks.typewriterbox import TypeWriterBox
from kivy.factory import Factory
class MultiSelect(TypeWriterBox):
items = ListProperty([])
@ -14,7 +15,18 @@ class MultiSelect(TypeWriterBox):
super(MultiSelectBotton, self).__init__(**kw)
self.button_dic = {}
self.value_b = {}
self.button_state = {}
def buildItem(self, dic):
keys = dic.keys()
ToggleText = Factory.ToggleText
ToggleImage = Factory.ToggleImage
if 'text' in keys and 'value' in keys:
desc = dic.copy()
desc['off_css'] = self.item_css
desc['on_css'] = self.item_selected_css
return ToggleText(**desc)
if 'source' in keys and 'on_source' in keys:
return ToggleImage(**dic)
def on_items(self, o, items):
self.clear_widgets()
@ -28,37 +40,22 @@ class MultiSelect(TypeWriterBox):
else:
items.append(dic)
for item in items:
dic = item.copy()
if item['name'] in self.default_selected:
dic['clscss'] = self.item_selected_cls
state = 'selected'
else:
dic['clscss'] = self.item_cls
state = 'unselected'
b = PressableText(dic)
b = self.buildItem(item)
b.bind(on_press=self.button_pressed)
self.button_dic.update({b:dic})
self.value_b.update({dic['value']:b})
self.button_state.update({b:state})
def button_pressed(self, o):
if self.button_state[o] == 'selected':
o.clscss = item_cls
self.button_state.update({o:'unselected'})
else:
o.clscss = item_selected_cls
self.button_state.update({o:'selected'})
if self.button_dic[o]['value'] == '_all_' \
and self.button_state[o] == 'selected':
for b, state in self.button_state.copy().items():
o.toggle()
if self.button_dic[o]['value'] == '_all_' and o.state():
for b, dic in self.button_dic.copy().items():
if b == o:
continue
b.clscss = item_cls
self.button_state.update({b:'unselected'})
b.select(False)
def getValue(self):
selected_buttons = [ b for b,state in self.button_state.item() \
if state == 'selected' ]
selected_buttons = [ b for b in self.button_dic.item() \
if b.state() ]
r = [ dic['value'] for b, dic in self.button_dic.items() \
if b in selected_buttons ]
return r
@ -66,7 +63,6 @@ class MultiSelect(TypeWriterBox):
def setValue(self, value):
for v in value:
b = self.value_b[v]
self.button_state[b] = 'selected'
b.clscss = item_selected_cls
b.select(True)

View File

@ -4,6 +4,9 @@ from kivy.uix.textinput import TextInput
from appPublic.registerfunction import RegisterFunction
import kivyblocks.clickable
import kivyblocks.multi-select
from .baseWidget import *
from .tree import Tree, TextTree, MenuTree, PopupMenu
from .toolbar import ToolPage, Toolbar