From b21aef7f6d01850117ce3933457152fe6734b0b5 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sat, 4 Dec 2021 23:31:35 +0800 Subject: [PATCH] bugfix --- kivyblocks/clickable.py | 76 +++++++++++++++++++------------------ kivyblocks/multi_select.py | 34 ++++++++++------- kivyblocks/typewriterbox.py | 50 ++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 50 deletions(-) create mode 100644 kivyblocks/typewriterbox.py diff --git a/kivyblocks/clickable.py b/kivyblocks/clickable.py index bfd606f..bdb1b4a 100644 --- a/kivyblocks/clickable.py +++ b/kivyblocks/clickable.py @@ -4,6 +4,7 @@ from kivy.uix.behaviors import TouchRippleButtonBehavior from kivy.graphics import Color, Rectangle from kivy.uix.boxlayout import BoxLayout from kivy.factory import Factory +from kivy.uix.image import AsyncImage from kivy.properties import ObjectProperty, StringProperty, BooleanProperty from kivyblocks.ready import WidgetReady @@ -24,6 +25,7 @@ class ClickableBox(TouchRippleButtonBehavior, Box): radius=radius, **kw) self.border_width = border_width + self.bind(minimum_height=self.setter('height')) def on_press(self,o=None): pass @@ -33,6 +35,9 @@ class ClickableText(ClickableBox): def __init__(self, **kw): super(ClickableText, self).__init__(**kw) 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) def on_text(self, o, txt): @@ -63,46 +68,43 @@ class ToggleText(ClickableText): self.clscss = self.off_css 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( -class PressableBox(TouchRippleButtonBehavior, Box): - normal_css = StringProperty("default") - actived_css = StringProperty("default") - box_actived = BooleanProperty(False) - 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 on_source(self, o, source): + if self.img_w: + self.img_w.source = source + return + self.img_w = AsyncImage(source=self.source) - def active(self, flag): - self.box_actived = flag +class ToggleImage(ClickableImage): + on_source = StringProperty(None) + select_state = BooleanProperty(False) + def __init__(self, **kw): + super(ToggleImage, self).__init__(**kw) - def on_box_actived(self, o, v): - if self.box_actived: - self.csscls = self.actived_css + def on_press(self, o): + self.select_state = if self.select_state ? False, True + + def on_select_state(self, o, f): + if self.img_w: + if f: + self.img_w.source = self.on_source + else: + self.img_w.source = self.source + return + if f: + self.img_w = AsyncImage(source=self.on_source) else: - self.csscls = self.normal_css + self.img_w = AsyncImage(source=self.source) - def on_press(self,o=None): - self.box_actived = True - - - def setValue(self,d): - self.user_data = d - - def getValue(self): - return self.user_data +class Select(VBox): + """ + """ diff --git a/kivyblocks/multi_select.py b/kivyblocks/multi_select.py index f194fee..3b85410 100644 --- a/kivyblocks/multi_select.py +++ b/kivyblocks/multi_select.py @@ -1,29 +1,32 @@ from kivy.properties import BooleanProperty, StringProperty,\ ListProperty, DictProperty from kivyblocks.baseWidgets import VBox, PressableText +from kivyblocks.typewriterbox import TypeWriterBox -class MultiSelect(VBox): +class MultiSelect(TypeWriterBox): items = ListProperty([]) item_cls = DictProperty({}) item_selected_cls = DictProperty({}) - all_button_position = StringProperty('begin') + all_button_position = StringProperty(None) default_selected=StringProperty([]) def __init__(self, **kw): super(MultiSelectBotton, self).__init__(**kw) self.button_dic = {} + self.value_b = {} self.button_state = {} def on_items(self, o, items): self.clear_widgets() - dic = { - 'text':'all', - 'value':None, - } - if all_button_position == 'begin': - items.insert(dic,0) - else: - items.append(dic) + if all_button_position is not None: + dic = { + 'text':'all', + 'value':'_all_', + } + if all_button_position == 'begin': + items.insert(dic,0) + else: + items.append(dic) for item in items: dic = item.copy() if item['name'] in self.default_selected: @@ -35,6 +38,7 @@ class MultiSelect(VBox): b = PressableText(dic) 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): @@ -44,7 +48,7 @@ class MultiSelect(VBox): else: o.clscss = item_selected_cls 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': for b, state in self.button_state.copy().items(): if b == o: @@ -59,6 +63,10 @@ class MultiSelect(VBox): if b in selected_buttons ] return r - def setValue(self, v): - pass + def setValue(self, value): + for v in value: + b = self.value_b[v] + self.button_state[b] = 'selected' + b.clscss = item_selected_cls + diff --git a/kivyblocks/typewriterbox.py b/kivyblocks/typewriterbox.py new file mode 100644 index 0000000..95fe1ee --- /dev/null +++ b/kivyblocks/typewriterbox.py @@ -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)