From 7e6bbc743459ecbe0ff9f7ece6f3211b351f7cd1 Mon Sep 17 00:00:00 2001 From: Gyedo Jeon Date: Wed, 25 Jul 2007 19:40:18 +0000 Subject: [PATCH] Added Expand All/Collapse All --- direct/src/tkwidgets/SceneGraphExplorer.py | 1 + direct/src/tkwidgets/Tree.py | 60 ++++++++++++++++------ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/direct/src/tkwidgets/SceneGraphExplorer.py b/direct/src/tkwidgets/SceneGraphExplorer.py index 2068792b5c..3059ad0c81 100644 --- a/direct/src/tkwidgets/SceneGraphExplorer.py +++ b/direct/src/tkwidgets/SceneGraphExplorer.py @@ -12,6 +12,7 @@ import Pmw DEFAULT_MENU_ITEMS = [ 'Update Explorer', 'Expand All', + 'Collapse All', 'Separator', 'Select', 'Deselect', 'Separator', diff --git a/direct/src/tkwidgets/Tree.py b/direct/src/tkwidgets/Tree.py index 9516df09f5..e9a00cd145 100644 --- a/direct/src/tkwidgets/Tree.py +++ b/direct/src/tkwidgets/Tree.py @@ -150,13 +150,9 @@ class TreeNode: command = self.menuList[self.menuVar.get()] if (command == 'Expand All'): - self.update(fExpandMode = 1) - ## elif (command == 'Collapse All'): - ## self.update(fExpandMode = 2) - ## elif (command == 'Expand'): - ## self.expand() - ## elif (command == 'Collapse'): - ## self.collapse() + self.updateAll(1) + elif (command == 'Collapse All'): + self.updateAll(0) else: self.item.MenuCommand(command) if self.parent and (command != 'Update Explorer'): @@ -213,6 +209,45 @@ class TreeNode: else: return self + # [gjeon] function to expand or collapse all the tree nodes + def updateAll(self, fMode, depth = 0, fUseCachedChildren = 1): + depth = depth + 1 + if not self.item.IsExpandable(): + return + if fMode: + self.state = 'expanded' + else: + if depth > 1: + self.state = 'collapsed' + + sublist = self.item._GetSubList() + if not sublist: + return + self.kidKeys = [] + for item in sublist: + key = item.GetKey() + if fUseCachedChildren and self.children.has_key(key): + child = self.children[key] + else: + child = TreeNode(self.canvas, self, item, self.menuList) + + self.children[key] = child + self.kidKeys.append(key) + + # Remove unused children + for key in self.children.keys(): + if key not in self.kidKeys: + del(self.children[key]) + + for key in self.kidKeys: + child = self.children[key] + child.updateAll(fMode, depth=depth) + + # [gjeon] to update the tree one time only + if depth == 1: + self.update() + self.view() + def update(self, fUseCachedChildren = 1, fExpandMode = 0): if self.parent: self.parent.update(fUseCachedChildren, fExpandMode = fExpandMode) @@ -221,22 +256,17 @@ class TreeNode: self.canvas['cursor'] = "watch" self.canvas.update() self.canvas.delete(ALL) # XXX could be more subtle - self.draw(7, 2, fUseCachedChildren, fExpandMode = fExpandMode) + self.draw(7, 2, fUseCachedChildren) x0, y0, x1, y1 = self.canvas.bbox(ALL) self.canvas.configure(scrollregion=(0, 0, x1, y1)) self.canvas['cursor'] = oldcursor - def draw(self, x, y, fUseCachedChildren = 1, fExpandMode = 0): + def draw(self, x, y, fUseCachedChildren = 1): # XXX This hard-codes too many geometry constants! self.x, self.y = x, y self.drawicon() self.drawtext() - if fExpandMode == 1: # [gjeon] expand all - self.state = 'expanded' - elif fExpandMode == 2: # [gjeon] collapse all - self.state = 'collapsed' - if self.state != 'expanded': return y+17 # draw children @@ -289,7 +319,7 @@ class TreeNode: child = self.children[key] cylast = cy self.canvas.create_line(x+9, cy+7, cx, cy+7, fill="gray50") - cy = child.draw(cx, cy, fUseCachedChildren, fExpandMode = fExpandMode) + cy = child.draw(cx, cy, fUseCachedChildren) if child.item.IsExpandable(): if child.state == 'expanded': iconname = "minusnode"