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): def __init__(self, **kw):
super(ToggleText, self).__init__(**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): def on_press(self, o=None):
self.select_state = False if self.select_state else True self.select_state = False if self.select_state else True
@ -90,18 +102,35 @@ class ToggleImage(ClickableImage):
def __init__(self, **kw): def __init__(self, **kw):
super(ToggleImage, self).__init__(**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): def on_press(self, o):
self.select_state = False if self.select_state else True self.select_state = False if self.select_state else True
def on_select_state(self, o, f): def on_select_state(self, o, f):
if self.img_w: if self.img_w:
if f: if self.select_state and self.on_source is not None:
self.img_w.source = self.on_source self.img_w.source = self.on_source
else: else:
self.img_w.source = self.source self.img_w.source = self.source
return return
if f: if self.select_state and self.on_source is not None:
self.img_w = AsyncImage(source=self.on_source) self.img_w = AsyncImage(source=self.on_source)
else: else:
self.img_w = AsyncImage(source=self.source) 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 ListProperty, DictProperty
from kivyblocks.baseWidgets import VBox, PressableText from kivyblocks.baseWidgets import VBox, PressableText
from kivyblocks.typewriterbox import TypeWriterBox from kivyblocks.typewriterbox import TypeWriterBox
from kivy.factory import Factory
class MultiSelect(TypeWriterBox): class MultiSelect(TypeWriterBox):
items = ListProperty([]) items = ListProperty([])
@ -14,7 +15,18 @@ class MultiSelect(TypeWriterBox):
super(MultiSelectBotton, self).__init__(**kw) super(MultiSelectBotton, self).__init__(**kw)
self.button_dic = {} self.button_dic = {}
self.value_b = {} 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): def on_items(self, o, items):
self.clear_widgets() self.clear_widgets()
@ -28,37 +40,22 @@ class MultiSelect(TypeWriterBox):
else: else:
items.append(dic) items.append(dic)
for item in items: for item in items:
dic = item.copy() b = self.buildItem(item)
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.bind(on_press=self.button_pressed) b.bind(on_press=self.button_pressed)
self.button_dic.update({b:dic}) self.button_dic.update({b:dic})
self.value_b.update({dic['value']:b}) self.value_b.update({dic['value']:b})
self.button_state.update({b:state})
def button_pressed(self, o): def button_pressed(self, o):
if self.button_state[o] == 'selected': o.toggle()
o.clscss = item_cls if self.button_dic[o]['value'] == '_all_' and o.state():
self.button_state.update({o:'unselected'}) for b, dic in self.button_dic.copy().items():
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():
if b == o: if b == o:
continue continue
b.clscss = item_cls b.select(False)
self.button_state.update({b:'unselected'})
def getValue(self): def getValue(self):
selected_buttons = [ b for b,state in self.button_state.item() \ selected_buttons = [ b for b in self.button_dic.item() \
if state == 'selected' ] if b.state() ]
r = [ dic['value'] for b, dic in self.button_dic.items() \ r = [ dic['value'] for b, dic in self.button_dic.items() \
if b in selected_buttons ] if b in selected_buttons ]
return r return r
@ -66,7 +63,6 @@ class MultiSelect(TypeWriterBox):
def setValue(self, value): def setValue(self, value):
for v in value: for v in value:
b = self.value_b[v] b = self.value_b[v]
self.button_state[b] = 'selected' b.select(True)
b.clscss = item_selected_cls

View File

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