This commit is contained in:
yumoqing 2021-12-30 10:34:27 +08:00
parent 0d4b540cac
commit 0de5b5b0ac
2 changed files with 27 additions and 5 deletions

View File

@ -3,7 +3,7 @@ import traceback
from kivy.clock import Clock from kivy.clock import Clock
from kivy.factory import Factory from kivy.factory import Factory
from kivy.properties import ListProperty, BooleanProperty, \ from kivy.properties import ListProperty, BooleanProperty, \
StringProperty, DictProperty StringProperty, DictProperty, NumericProperty
from kivy.uix.treeview import TreeView, TreeViewNode, TreeViewLabel from kivy.uix.treeview import TreeView, TreeViewNode, TreeViewLabel
from kivy.uix.boxlayout import BoxLayout from kivy.uix.boxlayout import BoxLayout
@ -11,14 +11,17 @@ from .threadcall import HttpClient
from .scrollpanel import ScrollPanel from .scrollpanel import ScrollPanel
from .clickable import SingleCheckBox from .clickable import SingleCheckBox
from .baseWidget import Text from .baseWidget import Text
from .utils import CSize
class TreeViewComplexNode(BoxLayout, TreeViewLabel): class TreeViewComplexNode(BoxLayout, TreeViewLabel):
otext = StringProperty(None) otext = StringProperty(None)
checkbox = BooleanProperty(False) checkbox = BooleanProperty(False)
icon = StringProperty(None) icon = StringProperty(None)
data = DictProperty(None)
def __init__(self, **kw): def __init__(self, **kw):
super(TreeViewComplexNode, self).__init__(**kw) super(TreeViewComplexNode, self).__init__(**kw)
self.orientation = 'horizontal' self.orientation = 'horizontal'
self.size_hint_x = None
if self.checkbox: if self.checkbox:
cb = SingleCheckBox(size_hint=(None,None)) cb = SingleCheckBox(size_hint=(None,None))
cb.bind(on_press=self.set_checked) cb.bind(on_press=self.set_checked)
@ -28,10 +31,13 @@ class TreeViewComplexNode(BoxLayout, TreeViewLabel):
self.add_widget(img) self.add_widget(img)
txt = Text(otext=self.otext, i18n=True) txt = Text(otext=self.otext, i18n=True)
txt.texture_update() txt.texture_update()
txt.size_hint = (None, None)
txt.size = txt.texture_size txt.size = txt.texture_size
self.add_widget(txt) self.add_widget(txt)
def set_checked(self, o): def set_checked(self, o):
if not self.data:
return
if o.state(): if o.state():
self.data['checked'] = True self.data['checked'] = True
else: else:
@ -47,13 +53,21 @@ class Hirarchy(ScrollPanel):
checkbox = BooleanProperty(False) checkbox = BooleanProperty(False)
icon = StringProperty(None) icon = StringProperty(None)
def __init__(self, **kw): def __init__(self, **kw):
self.register_event_type('on_selected')
self.tree = TreeView(hide_root=True) self.tree = TreeView(hide_root=True)
self.tree.size_hint_y = None self.tree.size_hint = (None, None)
self.tree.bind(on_node_expand=self.check_load_subnodes) self.tree.bind(on_node_expand=self.check_load_subnodes)
self.tree.bind(selected_node=self.node_selected)
super(Hirarchy, self).__init__(inner=self.tree, **kw) super(Hirarchy, self).__init__(inner=self.tree, **kw)
if self.url: if self.url:
self.data = self.get_remote_data() self.data = self.get_remote_data()
def on_selected(self, node):
print('selected node=', node)
def node_selected(self, o, v):
self.dispatch('on_selected', o.selected_node)
def check_load_subnodes(self, treeview, node): def check_load_subnodes(self, treeview, node):
if node.is_loaded: if node.is_loaded:
return return
@ -68,19 +82,26 @@ class Hirarchy(ScrollPanel):
icon=self.icon icon=self.icon
) )
n.data = data n.data = data
if self.checkbox: n.width = self.tree.indent_start + \
cb = SingleCheckBox() self.tree.indent_level * n.level \
n.add_widget(cb, index=len(n.children)) + sum([i.width for i in n.children])
if node: if node:
self.tree.add_node(n, node) self.tree.add_node(n, node)
else: else:
self.tree.add_node(n) self.tree.add_node(n)
children = data.get('children')
if not children:
return
for c in children:
self.create_new_node(c, n)
n.is_loaded = True
def get_remote_data(self, node=None): def get_remote_data(self, node=None):
hc = HttpClient() hc = HttpClient()
params = self.params.copy() if self.params else {} params = self.params.copy() if self.params else {}
if node: if node:
params['id'] = node.data[self.idField] params['id'] = node.data[self.idField]
print('params=', params)
d = hc(self.url, method=self.method, params=params) d = hc(self.url, method=self.method, params=params)
if isinstance(d, list): if isinstance(d, list):
return d return d

View File

@ -99,6 +99,7 @@ class HttpClient(Http_Client):
stream=False, stream=False,
callback=None, callback=None,
errback=None): errback=None):
method = method.upper()
def cb(t,resp): def cb(t,resp):
return resp return resp