Browse Source

Added Sorting Children

Gyedo Jeon 18 năm trước cách đây
mục cha
commit
2ba6205d7e
2 tập tin đã thay đổi với 30 bổ sung1 xóa
  1. 10 0
      direct/src/tkwidgets/SceneGraphExplorer.py
  2. 20 1
      direct/src/tkwidgets/Tree.py

+ 10 - 0
direct/src/tkwidgets/SceneGraphExplorer.py

@@ -91,6 +91,11 @@ class SceneGraphExplorer(Pmw.MegaWidget, DirectObject):
         # Check keywords and initialise options based on input values.
         self.initialiseoptions(SceneGraphExplorer)
 
+    # [gjeon] to set fSortChildren of tree node
+    def setFSortChildren(self, fSortChildren):
+        self._node.setFSortChildren(fSortChildren)
+        self._node.update()
+
     def update(self, fUseCachedChildren = 1):
         """ Refresh scene graph explorer """
         self._node.update(fUseCachedChildren)
@@ -122,6 +127,11 @@ class SceneGraphExplorer(Pmw.MegaWidget, DirectObject):
         self.ignore('DIRECT_activeParent')
         self.ignore('SGE_Update Explorer')
     
+    def sortChildren(self, searchKey):
+        sceneGraphItem = self._node.find(searchKey)
+        if sceneGraphItem:
+            sceneGraphItem.updateSelf(fSortChildren = 1)
+    
     def updateSelection(self, searchKey):
         # [gjeon] update SGE selection with directSession
         sceneGraphItem = self._node.find(searchKey)

+ 20 - 1
direct/src/tkwidgets/Tree.py

@@ -51,6 +51,11 @@ class TreeNode:
         self.menuVar = IntVar()
         self.menuVar.set(0)
         self._popupMenu = None
+        self.fSortChildren = 0 # [gjeon] flag for sorting children or not
+
+    # [gjeon] to set fSortChildren
+    def setFSortChildren(self, fSortChildren):
+        self.fSortChildren = fSortChildren
 
     def destroy(self):
         if self._popupMenu:
@@ -140,7 +145,7 @@ class TreeNode:
         self.item.MenuCommand(command)
         if self.parent and (command != 'Update Explorer'):
             # Update parent to try to keep explorer up to date
-            self.parent.update()
+            self.parent.update(self.fSortChilren)
 
     def expand(self, event=None):
         if not self.item.IsExpandable():
@@ -218,12 +223,26 @@ class TreeNode:
             # IsExpandable() was mistaken; that's allowed
             return y+17
         self.kidKeys = []
+        
+        # [gjeon] to sort children
+        if self.fSortChildren:
+            def compareText(x, y):
+                textX = x.GetText()
+                textY = y.GetText()
+                if (textX > textY):
+                    return 1
+                elif (textX == textY):
+                    return 0
+                else: # textX < textY
+                    return -1
+            sublist.sort(compareText)
         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)
+            child.setFSortChildren(self.fSortChildren)
             self.children[key] = child
             self.kidKeys.append(key)
         # Remove unused children