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
This commit is contained in:
@@ -3,6 +3,10 @@ import wx.lib.mixins.treemixin as treemixin
|
|||||||
|
|
||||||
|
|
||||||
class TreeModel(object):
|
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):
|
def __init__(self, *args, **kwargs):
|
||||||
self.items = []
|
self.items = []
|
||||||
self.itemCounter = 0
|
self.itemCounter = 0
|
||||||
@@ -62,12 +66,17 @@ class DemoTreeMixin(treemixin.VirtualTree, treemixin.DragAndDrop,
|
|||||||
return self.model.GetChildrenCount(indices)
|
return self.model.GetChildrenCount(indices)
|
||||||
|
|
||||||
def OnGetItemFont(self, 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:
|
if self.model.GetChildrenCount(indices) > 0:
|
||||||
return wx.SMALL_FONT
|
return wx.SMALL_FONT
|
||||||
else:
|
else:
|
||||||
return super(DemoTreeMixin, self).OnGetItemFont(indices)
|
return super(DemoTreeMixin, self).OnGetItemFont(indices)
|
||||||
|
|
||||||
def OnGetItemTextColour(self, 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:
|
if len(indices) % 2 == 0:
|
||||||
return wx.RED
|
return wx.RED
|
||||||
elif len(indices) % 3 == 0:
|
elif len(indices) % 3 == 0:
|
||||||
@@ -76,13 +85,16 @@ class DemoTreeMixin(treemixin.VirtualTree, treemixin.DragAndDrop,
|
|||||||
return super(DemoTreeMixin, self).OnGetItemTextColour(indices)
|
return super(DemoTreeMixin, self).OnGetItemTextColour(indices)
|
||||||
|
|
||||||
def OnGetItemBackgroundColour(self, 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
|
return wx.GREEN
|
||||||
else:
|
else:
|
||||||
return super(DemoTreeMixin,
|
return super(DemoTreeMixin,
|
||||||
self).OnGetItemBackgroundColour(indices)
|
self).OnGetItemBackgroundColour(indices)
|
||||||
|
|
||||||
def OnGetItemImage(self, indices, which):
|
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 which in [wx.TreeItemIcon_Normal, wx.TreeItemIcon_Selected]:
|
||||||
if self.model.GetChildrenCount(indices):
|
if self.model.GetChildrenCount(indices):
|
||||||
return 0
|
return 0
|
||||||
@@ -117,10 +129,13 @@ class VirtualTreeListCtrl(DemoTreeMixin, wx.gizmos.TreeListCtrl):
|
|||||||
(16, 16)))
|
(16, 16)))
|
||||||
|
|
||||||
def OnGetItemText(self, indices, column=0):
|
def OnGetItemText(self, indices, column=0):
|
||||||
|
# Return a different label depending on column.
|
||||||
return '%s, column %d'%\
|
return '%s, column %d'%\
|
||||||
(super(VirtualTreeListCtrl, self).OnGetItemText(indices), column)
|
(super(VirtualTreeListCtrl, self).OnGetItemText(indices), column)
|
||||||
|
|
||||||
def OnGetItemImage(self, indices, which, column=0):
|
def OnGetItemImage(self, indices, which, column=0):
|
||||||
|
# Also change the image of the other columns when the item has
|
||||||
|
# children.
|
||||||
if column == 0:
|
if column == 0:
|
||||||
return super(VirtualTreeListCtrl, self).OnGetItemImage(indices,
|
return super(VirtualTreeListCtrl, self).OnGetItemImage(indices,
|
||||||
which)
|
which)
|
||||||
@@ -174,20 +189,27 @@ class TreeNotebook(wx.Notebook):
|
|||||||
tree = class_(self, treemodel=treemodel, log=log)
|
tree = class_(self, treemodel=treemodel, log=log)
|
||||||
self.trees.append(tree)
|
self.trees.append(tree)
|
||||||
self.AddPage(tree, title)
|
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):
|
def RefreshItems(self):
|
||||||
for tree in self.trees:
|
tree = self.trees[self.GetSelection()]
|
||||||
tree.RefreshItems()
|
tree.RefreshItems()
|
||||||
tree.UnselectAll()
|
tree.UnselectAll()
|
||||||
|
|
||||||
def GetSelectedItemIndices(self):
|
|
||||||
tree = self.trees[self.GetSelection()]
|
|
||||||
if tree.GetSelection():
|
|
||||||
return tree.ItemIndices(tree.GetSelection())
|
|
||||||
else:
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
class TestPanel(wx.Panel):
|
class TestPanel(wx.Panel):
|
||||||
def __init__(self, parent, log):
|
def __init__(self, parent, log):
|
||||||
@@ -196,7 +218,6 @@ class TestPanel(wx.Panel):
|
|||||||
self.treemodel = TreeModel()
|
self.treemodel = TreeModel()
|
||||||
self.CreateControls()
|
self.CreateControls()
|
||||||
self.LayoutControls()
|
self.LayoutControls()
|
||||||
self.BindEvents()
|
|
||||||
|
|
||||||
def CreateControls(self):
|
def CreateControls(self):
|
||||||
self.notebook = TreeNotebook(self, treemodel=self.treemodel,
|
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.label = wx.StaticText(self, label='Number of children: ')
|
||||||
self.childrenCountCtrl = wx.SpinCtrl(self, value='0', max=10000)
|
self.childrenCountCtrl = wx.SpinCtrl(self, value='0', max=10000)
|
||||||
self.button = wx.Button(self, label='Update children')
|
self.button = wx.Button(self, label='Update children')
|
||||||
|
self.button.Bind(wx.EVT_BUTTON, self.OnEnter)
|
||||||
|
|
||||||
def LayoutControls(self):
|
def LayoutControls(self):
|
||||||
hSizer = wx.BoxSizer(wx.HORIZONTAL)
|
hSizer = wx.BoxSizer(wx.HORIZONTAL)
|
||||||
@@ -216,23 +238,12 @@ class TestPanel(wx.Panel):
|
|||||||
sizer.Add(hSizer, 0, wx.EXPAND)
|
sizer.Add(hSizer, 0, wx.EXPAND)
|
||||||
self.SetSizer(sizer)
|
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):
|
def OnEnter(self, event):
|
||||||
indices = self.notebook.GetSelectedItemIndices()
|
indicesList = self.notebook.GetIndicesOfSelectedItems()
|
||||||
|
newChildrenCount = self.childrenCountCtrl.GetValue()
|
||||||
|
for indices in indicesList:
|
||||||
text = self.treemodel.GetText(indices)
|
text = self.treemodel.GetText(indices)
|
||||||
oldChildrenCount = self.treemodel.GetChildrenCount(indices)
|
oldChildrenCount = self.treemodel.GetChildrenCount(indices)
|
||||||
newChildrenCount = self.childrenCountCtrl.GetValue()
|
|
||||||
self.log.write('%s %s now has %d children (was %d)'%(text, indices,
|
self.log.write('%s %s now has %d children (was %d)'%(text, indices,
|
||||||
newChildrenCount, oldChildrenCount))
|
newChildrenCount, oldChildrenCount))
|
||||||
self.treemodel.SetChildrenCount(indices, newChildrenCount)
|
self.treemodel.SetChildrenCount(indices, newChildrenCount)
|
||||||
|
@@ -25,8 +25,8 @@ The VirtualTree and DragAndDrop mixins force the wx.TR_HIDE_ROOT style.
|
|||||||
|
|
||||||
Author: Frank Niessink <frank@niessink.com>
|
Author: Frank Niessink <frank@niessink.com>
|
||||||
License: wxWidgets license
|
License: wxWidgets license
|
||||||
Version: 0.6
|
Version: 0.7
|
||||||
Date: 17 February 2007
|
Date: 22 February 2007
|
||||||
|
|
||||||
ExpansionState is based on code and ideas from Karsten Hilbert.
|
ExpansionState is based on code and ideas from Karsten Hilbert.
|
||||||
Andrea Gavana provided help with the CustomTreeCtrl integration.
|
Andrea Gavana provided help with the CustomTreeCtrl integration.
|
||||||
@@ -143,7 +143,7 @@ class TreeAPIHarmonizer(object):
|
|||||||
return children
|
return children
|
||||||
|
|
||||||
def ItemIndices(self, item):
|
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
|
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 (1,), the first child of the first child of the
|
||||||
root item is (0, 0), the second 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):
|
def OnBeginDrag(self, event):
|
||||||
# We allow only one item to be dragged at a time, to keep it simple
|
# We allow only one item to be dragged at a time, to keep it simple
|
||||||
self._dragItem = self.GetSelections()[0]
|
self._dragItem = event.GetItem()
|
||||||
if self._dragItem:
|
if self._dragItem and self._dragItem != self.GetRootItem():
|
||||||
self.StartDragging()
|
self.StartDragging()
|
||||||
event.Allow()
|
event.Allow()
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user