This commit is contained in:
yumoqing 2021-12-04 23:31:35 +08:00
parent 62984b8501
commit b21aef7f6d
3 changed files with 110 additions and 50 deletions

View File

@ -4,6 +4,7 @@ from kivy.uix.behaviors import TouchRippleButtonBehavior
from kivy.graphics import Color, Rectangle from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout from kivy.uix.boxlayout import BoxLayout
from kivy.factory import Factory from kivy.factory import Factory
from kivy.uix.image import AsyncImage
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
from kivyblocks.ready import WidgetReady from kivyblocks.ready import WidgetReady
@ -24,6 +25,7 @@ class ClickableBox(TouchRippleButtonBehavior, Box):
radius=radius, radius=radius,
**kw) **kw)
self.border_width = border_width self.border_width = border_width
self.bind(minimum_height=self.setter('height'))
def on_press(self,o=None): def on_press(self,o=None):
pass pass
@ -33,6 +35,9 @@ class ClickableText(ClickableBox):
def __init__(self, **kw): def __init__(self, **kw):
super(ClickableText, self).__init__(**kw) super(ClickableText, self).__init__(**kw)
self.txt_w = Text(text=self.text, i18n=True) self.txt_w = Text(text=self.text, i18n=True)
self.txt_w.size_hint = (None, None)
self.txt_w.bind(minimum_height=self.self.txt_w.setter('height'))
self.txt_w.bind(minimum_width=self.self.txt_w.setter('width'))
self.add_widget(self.txt_w) self.add_widget(self.txt_w)
def on_text(self, o, txt): def on_text(self, o, txt):
@ -63,46 +68,43 @@ class ToggleText(ClickableText):
self.clscss = self.off_css self.clscss = self.off_css
class ClickableImage(ClickableBox): class ClickableImage(ClickableBox):
source=StringProperty('none') source=StringProperty(None)
img_height = NumericProperty(None)
img_width = NumericProperty(None)
def __init__(self, **kw):
super(ClickableImage, self).__init__(**kw)
self.img_w = None
if source:
self.img_w = AsyncImage(source=self.source)
class ToggleImage( def on_source(self, o, source):
class PressableBox(TouchRippleButtonBehavior, Box): if self.img_w:
normal_css = StringProperty("default") self.img_w.source = source
actived_css = StringProperty("default") return
box_actived = BooleanProperty(False) self.img_w = AsyncImage(source=self.source)
def __init__(self,
border_width=1,
user_data=None,
radius=[],
**kw):
super(PressableBox, self).__init__(
padding=[border_width,
border_width,
border_width,
border_width],
radius=radius,
**kw)
self.border_width = border_width
self.user_data = user_data
self.actived = False
self.csscls = self.normal_css
def active(self, flag): class ToggleImage(ClickableImage):
self.box_actived = flag on_source = StringProperty(None)
select_state = BooleanProperty(False)
def __init__(self, **kw):
super(ToggleImage, self).__init__(**kw)
def on_box_actived(self, o, v): def on_press(self, o):
if self.box_actived: self.select_state = if self.select_state ? False, True
self.csscls = self.actived_css
def on_select_state(self, o, f):
if self.img_w:
if f:
self.img_w.source = self.on_source
else: else:
self.csscls = self.normal_css self.img_w.source = self.source
return
if f:
self.img_w = AsyncImage(source=self.on_source)
else:
self.img_w = AsyncImage(source=self.source)
def on_press(self,o=None): class Select(VBox):
self.box_actived = True """
def setValue(self,d):
self.user_data = d
def getValue(self):
return self.user_data
"""

View File

@ -1,24 +1,27 @@
from kivy.properties import BooleanProperty, StringProperty,\ 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
class MultiSelect(VBox): class MultiSelect(TypeWriterBox):
items = ListProperty([]) items = ListProperty([])
item_cls = DictProperty({}) item_cls = DictProperty({})
item_selected_cls = DictProperty({}) item_selected_cls = DictProperty({})
all_button_position = StringProperty('begin') all_button_position = StringProperty(None)
default_selected=StringProperty([]) default_selected=StringProperty([])
def __init__(self, **kw): def __init__(self, **kw):
super(MultiSelectBotton, self).__init__(**kw) super(MultiSelectBotton, self).__init__(**kw)
self.button_dic = {} self.button_dic = {}
self.value_b = {}
self.button_state = {} self.button_state = {}
def on_items(self, o, items): def on_items(self, o, items):
self.clear_widgets() self.clear_widgets()
if all_button_position is not None:
dic = { dic = {
'text':'all', 'text':'all',
'value':None, 'value':'_all_',
} }
if all_button_position == 'begin': if all_button_position == 'begin':
items.insert(dic,0) items.insert(dic,0)
@ -35,6 +38,7 @@ class MultiSelect(VBox):
b = PressableText(dic) 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.button_state.update({b:state}) self.button_state.update({b:state})
def button_pressed(self, o): def button_pressed(self, o):
@ -44,7 +48,7 @@ class MultiSelect(VBox):
else: else:
o.clscss = item_selected_cls o.clscss = item_selected_cls
self.button_state.update({o:'selected'}) self.button_state.update({o:'selected'})
if self.button_dic[o]['text'] == 'all' \ if self.button_dic[o]['value'] == '_all_' \
and self.button_state[o] == 'selected': and self.button_state[o] == 'selected':
for b, state in self.button_state.copy().items(): for b, state in self.button_state.copy().items():
if b == o: if b == o:
@ -59,6 +63,10 @@ class MultiSelect(VBox):
if b in selected_buttons ] if b in selected_buttons ]
return r return r
def setValue(self, v): def setValue(self, value):
pass for v in value:
b = self.value_b[v]
self.button_state[b] = 'selected'
b.clscss = item_selected_cls

View File

@ -0,0 +1,50 @@
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.factory import Factory
from kivyblocks.widget_css import WidgetCSS
from kivyblocks.ready import WidgetReady
class TypeWriterBox(WidgetCSS, WidgetReady, Widget):
def __init__(self, **kw):
super(TypeWriterBox, self).__init__(**kw)
self.size_hint_y = None
self.sub_widgets = []
self.curbox = self.add_vertical_box()
self.bind(minimum_height=self.setter('height'))
def on_width(self, o, size):
self.relocate()
def add_widget(self, w, *args):
width = 0
for c in self.curbox.children:
width += c.width
if width + c.width > self.width:
self.curbox = self.add_vertical_box()
self.curbox.add_widget(w, *args)
self.sub_widgets.append(w)
def del_widget(self, w):
self.sub_widgets = [i for i in self.sub_widgets.copy() if i !=w ]
self.relocate()
def add_veritcal_box(self):
box = BoxLayout(orientation='vertical', size_hint_y=None)
box.bind(minimum_height = box.setter('height')
super(TypeWriterBox, self).add_widget(box)
return box
def relocate(self):
with self.fboContext():
for b in self.children:
b.clear_widgets()
width = 0
self.clear_widgets()
self.curbox = add_vertical_box()
for w in self.sub_widgets:
if width + w.width > self.width:
self.curbox = self.add_vertical_box()
width = 0
self.curbox.add_widget(w)
Factory.register('TypeWriterBox', TypeWriterBox)