From 25f8729c3df0dcf97e8e0f4bba88eccdbf1f7c67 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Mon, 26 Feb 2007 23:35:07 +0000 Subject: [PATCH] Updated to TreeMixin 0.7 git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@44574 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/demo/TreeMixin.py | 71 +++++++++++++++++------------ wxPython/wx/lib/mixins/treemixin.py | 10 ++-- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/wxPython/demo/TreeMixin.py b/wxPython/demo/TreeMixin.py index 8cb12ab189..eef25f0f58 100644 --- a/wxPython/demo/TreeMixin.py +++ b/wxPython/demo/TreeMixin.py @@ -3,6 +3,10 @@ import wx.lib.mixins.treemixin as treemixin class TreeModel(object): + ''' TreeModel holds the domain objects that are shown in the different + tree controls. Each domain object is simply a two-tuple consisting of + a label and a list of child tuples, i.e. (label, [list of child tuples]). + ''' def __init__(self, *args, **kwargs): self.items = [] self.itemCounter = 0 @@ -62,12 +66,17 @@ class DemoTreeMixin(treemixin.VirtualTree, treemixin.DragAndDrop, return self.model.GetChildrenCount(indices) def OnGetItemFont(self, indices): + # Show how to change the item font. Here we use a small font for + # items that have children and the default font otherwise. if self.model.GetChildrenCount(indices) > 0: return wx.SMALL_FONT else: return super(DemoTreeMixin, self).OnGetItemFont(indices) def OnGetItemTextColour(self, indices): + # Show how to change the item text colour. In this case second level + # items are coloured red and third level items are blue. All other + # items have the default text colour. if len(indices) % 2 == 0: return wx.RED elif len(indices) % 3 == 0: @@ -76,13 +85,16 @@ class DemoTreeMixin(treemixin.VirtualTree, treemixin.DragAndDrop, return super(DemoTreeMixin, self).OnGetItemTextColour(indices) def OnGetItemBackgroundColour(self, indices): - if indices[-1] == len(indices): + # Show how to change the item background colour. In this case the + # background colour of each third item is green. + if indices[-1] == 2: return wx.GREEN else: return super(DemoTreeMixin, self).OnGetItemBackgroundColour(indices) def OnGetItemImage(self, indices, which): + # Return the right icon depending on whether the item has children. if which in [wx.TreeItemIcon_Normal, wx.TreeItemIcon_Selected]: if self.model.GetChildrenCount(indices): return 0 @@ -117,10 +129,13 @@ class VirtualTreeListCtrl(DemoTreeMixin, wx.gizmos.TreeListCtrl): (16, 16))) def OnGetItemText(self, indices, column=0): + # Return a different label depending on column. return '%s, column %d'%\ (super(VirtualTreeListCtrl, self).OnGetItemText(indices), column) def OnGetItemImage(self, indices, which, column=0): + # Also change the image of the other columns when the item has + # children. if column == 0: return super(VirtualTreeListCtrl, self).OnGetItemImage(indices, which) @@ -174,19 +189,26 @@ class TreeNotebook(wx.Notebook): tree = class_(self, treemodel=treemodel, log=log) self.trees.append(tree) self.AddPage(tree, title) - self.RefreshItems() + self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged) + + def OnPageChanged(self, event): + oldTree = self.GetPage(event.OldSelection) + newTree = self.GetPage(event.Selection) + newTree.SetExpansionState(oldTree.GetExpansionState()) + newTree.RefreshItems() + event.Skip() + + def GetIndicesOfSelectedItems(self): + tree = self.trees[self.GetSelection()] + if tree.GetSelections(): + return [tree.ItemIndices(item) for item in tree.GetSelections()] + else: + return [()] def RefreshItems(self): - for tree in self.trees: - tree.RefreshItems() - tree.UnselectAll() - - def GetSelectedItemIndices(self): tree = self.trees[self.GetSelection()] - if tree.GetSelection(): - return tree.ItemIndices(tree.GetSelection()) - else: - return [] + tree.RefreshItems() + tree.UnselectAll() class TestPanel(wx.Panel): @@ -196,7 +218,6 @@ class TestPanel(wx.Panel): self.treemodel = TreeModel() self.CreateControls() self.LayoutControls() - self.BindEvents() def CreateControls(self): self.notebook = TreeNotebook(self, treemodel=self.treemodel, @@ -204,6 +225,7 @@ class TestPanel(wx.Panel): self.label = wx.StaticText(self, label='Number of children: ') self.childrenCountCtrl = wx.SpinCtrl(self, value='0', max=10000) self.button = wx.Button(self, label='Update children') + self.button.Bind(wx.EVT_BUTTON, self.OnEnter) def LayoutControls(self): hSizer = wx.BoxSizer(wx.HORIZONTAL) @@ -216,26 +238,15 @@ class TestPanel(wx.Panel): sizer.Add(hSizer, 0, wx.EXPAND) self.SetSizer(sizer) - def BindEvents(self): - self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged) - self.button.Bind(wx.EVT_BUTTON, self.OnEnter) - - def OnPageChanged(self, event): - if self.IsBeingDeleted(): - return - oldTree = self.notebook.GetPage(event.OldSelection) - newTree = self.notebook.GetPage(event.Selection) - newTree.SetExpansionState(oldTree.GetExpansionState()) - event.Skip() - def OnEnter(self, event): - indices = self.notebook.GetSelectedItemIndices() - text = self.treemodel.GetText(indices) - oldChildrenCount = self.treemodel.GetChildrenCount(indices) + indicesList = self.notebook.GetIndicesOfSelectedItems() newChildrenCount = self.childrenCountCtrl.GetValue() - self.log.write('%s %s now has %d children (was %d)'%(text, indices, - newChildrenCount, oldChildrenCount)) - self.treemodel.SetChildrenCount(indices, newChildrenCount) + for indices in indicesList: + text = self.treemodel.GetText(indices) + oldChildrenCount = self.treemodel.GetChildrenCount(indices) + self.log.write('%s %s now has %d children (was %d)'%(text, indices, + newChildrenCount, oldChildrenCount)) + self.treemodel.SetChildrenCount(indices, newChildrenCount) self.notebook.RefreshItems() diff --git a/wxPython/wx/lib/mixins/treemixin.py b/wxPython/wx/lib/mixins/treemixin.py index cce1714bd4..3092b41c20 100644 --- a/wxPython/wx/lib/mixins/treemixin.py +++ b/wxPython/wx/lib/mixins/treemixin.py @@ -25,8 +25,8 @@ The VirtualTree and DragAndDrop mixins force the wx.TR_HIDE_ROOT style. Author: Frank Niessink License: wxWidgets license -Version: 0.6 -Date: 17 February 2007 +Version: 0.7 +Date: 22 February 2007 ExpansionState is based on code and ideas from Karsten Hilbert. Andrea Gavana provided help with the CustomTreeCtrl integration. @@ -143,7 +143,7 @@ class TreeAPIHarmonizer(object): return children def ItemIndices(self, item): - ''' Construct an index typle for item. The root item is () the + ''' Construct an index typle for item. The root item is (), the first child of the root item is (0,), the second child of the root item is (1,), the first child of the first child of the root item is (0, 0), the second child of the first child of the @@ -409,8 +409,8 @@ class DragAndDrop(TreeAPIHarmonizer): def OnBeginDrag(self, event): # We allow only one item to be dragged at a time, to keep it simple - self._dragItem = self.GetSelections()[0] - if self._dragItem: + self._dragItem = event.GetItem() + if self._dragItem and self._dragItem != self.GetRootItem(): self.StartDragging() event.Allow() else: