no need to import customtreectrl here,

switch to """ instead of '''


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_8_BRANCH@45907 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2007-05-09 00:37:30 +00:00
parent a329847b07
commit 98c16c304e

View File

@@ -1,4 +1,4 @@
''' """
treemixin.py treemixin.py
This module provides three mixin classes that can be used with tree This module provides three mixin classes that can be used with tree
@@ -18,7 +18,7 @@ controls:
all items in the tree to restore it later. all items in the tree to restore it later.
All mixin classes work with wx.TreeCtrl, wx.gizmos.TreeListCtrl, All mixin classes work with wx.TreeCtrl, wx.gizmos.TreeListCtrl,
and wx.lib.customtree.CustomTreeCtrl. They can be used together or and wx.lib.customtreectrl.CustomTreeCtrl. They can be used together or
separately. separately.
The VirtualTree and DragAndDrop mixins force the wx.TR_HIDE_ROOT style. The VirtualTree and DragAndDrop mixins force the wx.TR_HIDE_ROOT style.
@@ -30,15 +30,15 @@ Date: 15 April 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.
''' """
import wx, wx.lib.customtreectrl import wx
class TreeAPIHarmonizer(object): class TreeAPIHarmonizer(object):
''' This class attempts to hide the differences in API between the """ This class attempts to hide the differences in API between the
different tree controls that are part of wxPython. ''' different tree controls that are part of wxPython. """
def __callSuper(self, methodName, default, *args, **kwargs): def __callSuper(self, methodName, default, *args, **kwargs):
# If our super class has a method called methodName, call it, # If our super class has a method called methodName, call it,
@@ -165,12 +165,12 @@ class TreeAPIHarmonizer(object):
**kwargs) **kwargs)
def HitTest(self, *args, **kwargs): def HitTest(self, *args, **kwargs):
''' HitTest returns a two-tuple (item, flags) for tree controls """ HitTest returns a two-tuple (item, flags) for tree controls
without columns and a three-tuple (item, flags, column) for tree without columns and a three-tuple (item, flags, column) for tree
controls with columns. Our caller can indicate this method to controls with columns. Our caller can indicate this method to
always return a three-tuple no matter what tree control we're mixed always return a three-tuple no matter what tree control we're mixed
in with by specifying the optional argument 'alwaysReturnColumn' in with by specifying the optional argument 'alwaysReturnColumn'
to be True. ''' to be True. """
alwaysReturnColumn = kwargs.pop('alwaysReturnColumn', False) alwaysReturnColumn = kwargs.pop('alwaysReturnColumn', False)
hitTestResult = super(TreeAPIHarmonizer, self).HitTest(*args, **kwargs) hitTestResult = super(TreeAPIHarmonizer, self).HitTest(*args, **kwargs)
if len(hitTestResult) == 2 and alwaysReturnColumn: if len(hitTestResult) == 2 and alwaysReturnColumn:
@@ -209,11 +209,11 @@ class TreeAPIHarmonizer(object):
class TreeHelper(object): class TreeHelper(object):
''' This class provides methods that are not part of the API of any """ This class provides methods that are not part of the API of any
tree control, but are convenient to have available. ''' tree control, but are convenient to have available. """
def GetItemChildren(self, item=None, recursively=False): def GetItemChildren(self, item=None, recursively=False):
''' Return the children of item as a list. ''' """ Return the children of item as a list. """
if not item: if not item:
item = self.GetRootItem() item = self.GetRootItem()
if not item: if not item:
@@ -228,7 +228,7 @@ class TreeHelper(object):
return children return children
def GetIndexOfItem(self, item): def GetIndexOfItem(self, item):
''' Return the index of item. ''' """ Return the index of item. """
parent = self.GetItemParent(item) parent = self.GetItemParent(item)
if parent: if parent:
parentIndices = self.GetIndexOfItem(parent) parentIndices = self.GetIndexOfItem(parent)
@@ -238,7 +238,7 @@ class TreeHelper(object):
return () return ()
def GetItemByIndex(self, index): def GetItemByIndex(self, index):
''' Return the item specified by index. ''' """ Return the item specified by index. """
item = self.GetRootItem() item = self.GetRootItem()
for i in index: for i in index:
children = self.GetItemChildren(item) children = self.GetItemChildren(item)
@@ -247,7 +247,7 @@ class TreeHelper(object):
class VirtualTree(TreeAPIHarmonizer, TreeHelper): class VirtualTree(TreeAPIHarmonizer, TreeHelper):
''' This is a mixin class that can be used to allow for virtual tree """ This is a mixin class that can be used to allow for virtual tree
controls. It can be mixed in with wx.TreeCtrl, wx.gizmos.TreeListCtrl, controls. It can be mixed in with wx.TreeCtrl, wx.gizmos.TreeListCtrl,
wx.lib.customtree.CustomTreeCtrl. wx.lib.customtree.CustomTreeCtrl.
@@ -279,7 +279,7 @@ class VirtualTree(TreeAPIHarmonizer, TreeHelper):
the fourth one. A tuple with two integers, e.g. (3,0), represents a the fourth one. A tuple with two integers, e.g. (3,0), represents a
child of a visible root item, in this case the first child of the child of a visible root item, in this case the first child of the
fourth root item. fourth root item.
''' """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs['style'] = kwargs.get('style', wx.TR_DEFAULT_STYLE) | \ kwargs['style'] = kwargs.get('style', wx.TR_DEFAULT_STYLE) | \
@@ -289,69 +289,69 @@ class VirtualTree(TreeAPIHarmonizer, TreeHelper):
self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed) self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed)
def OnGetChildrenCount(self, index): def OnGetChildrenCount(self, index):
''' This function *must* be overloaded in the derived class. """ This function *must* be overloaded in the derived class.
It should return the number of child items of the item with the It should return the number of child items of the item with the
provided index. If index == () it should return the number of provided index. If index == () it should return the number of
root items. ''' root items. """
raise NotImplementedError raise NotImplementedError
def OnGetItemText(self, index, column=0): def OnGetItemText(self, index, column=0):
''' This function *must* be overloaded in the derived class. It """ This function *must* be overloaded in the derived class. It
should return the string containing the text of the specified should return the string containing the text of the specified
item. ''' item. """
raise NotImplementedError raise NotImplementedError
def OnGetItemFont(self, index): def OnGetItemFont(self, index):
''' This function may be overloaded in the derived class. It """ This function may be overloaded in the derived class. It
should return the wx.Font to be used for the specified item. ''' should return the wx.Font to be used for the specified item. """
return wx.NullFont return wx.NullFont
def OnGetItemTextColour(self, index): def OnGetItemTextColour(self, index):
''' This function may be overloaded in the derived class. It """ This function may be overloaded in the derived class. It
should return the wx.Colour to be used as text colour for the should return the wx.Colour to be used as text colour for the
specified item. ''' specified item. """
return wx.NullColour return wx.NullColour
def OnGetItemBackgroundColour(self, index): def OnGetItemBackgroundColour(self, index):
''' This function may be overloaded in the derived class. It """ This function may be overloaded in the derived class. It
should return the wx.Colour to be used as background colour for should return the wx.Colour to be used as background colour for
the specified item. ''' the specified item. """
return wx.NullColour return wx.NullColour
def OnGetItemImage(self, index, which=wx.TreeItemIcon_Normal, column=0): def OnGetItemImage(self, index, which=wx.TreeItemIcon_Normal, column=0):
''' This function may be overloaded in the derived class. It """ This function may be overloaded in the derived class. It
should return the index of the image to be used. Don't forget should return the index of the image to be used. Don't forget
to associate an ImageList with the tree control. ''' to associate an ImageList with the tree control. """
return -1 return -1
def OnGetItemType(self, index): def OnGetItemType(self, index):
''' This function may be overloaded in the derived class, but """ This function may be overloaded in the derived class, but
that only makes sense when this class is mixed in with a tree that only makes sense when this class is mixed in with a tree
control that supports checkable items, i.e. CustomTreeCtrl. control that supports checkable items, i.e. CustomTreeCtrl.
This method should return whether the item is to be normal (0, This method should return whether the item is to be normal (0,
the default), a checkbox (1) or a radiobutton (2). the default), a checkbox (1) or a radiobutton (2).
Note that OnGetItemChecked needs to be implemented as well; it Note that OnGetItemChecked needs to be implemented as well; it
should return whether the item is actually checked. ''' should return whether the item is actually checked. """
return 0 return 0
def OnGetItemChecked(self, index): def OnGetItemChecked(self, index):
''' This function may be overloaded in the derived class, but """ This function may be overloaded in the derived class, but
that only makes sense when this class is mixed in with a tree that only makes sense when this class is mixed in with a tree
control that supports checkable items, i.e. CustomTreeCtrl. control that supports checkable items, i.e. CustomTreeCtrl.
This method should return whether the item is to be checked. This method should return whether the item is to be checked.
Note that OnGetItemType should return 1 (checkbox) or 2 Note that OnGetItemType should return 1 (checkbox) or 2
(radiobutton) for this item. ''' (radiobutton) for this item. """
return False return False
def RefreshItems(self): def RefreshItems(self):
''' Redraws all visible items. ''' """ Redraws all visible items. """
rootItem = self.GetRootItem() rootItem = self.GetRootItem()
if not rootItem: if not rootItem:
rootItem = self.AddRoot('Hidden root') rootItem = self.AddRoot('Hidden root')
self.RefreshChildrenRecursively(rootItem) self.RefreshChildrenRecursively(rootItem)
def RefreshItem(self, index): def RefreshItem(self, index):
''' Redraws the item with the specified index. ''' """ Redraws the item with the specified index. """
try: try:
item = self.GetItemByIndex(index) item = self.GetItemByIndex(index)
except IndexError: except IndexError:
@@ -362,8 +362,8 @@ class VirtualTree(TreeAPIHarmonizer, TreeHelper):
self.DoRefreshItem(item, index, hasChildren) self.DoRefreshItem(item, index, hasChildren)
def RefreshChildrenRecursively(self, item, itemIndex=None): def RefreshChildrenRecursively(self, item, itemIndex=None):
''' Refresh the children of item, reusing as much of the """ Refresh the children of item, reusing as much of the
existing items in the tree as possible. ''' existing items in the tree as possible. """
if itemIndex is None: if itemIndex is None:
itemIndex = self.GetIndexOfItem(item) itemIndex = self.GetIndexOfItem(item)
reusableChildren = self.GetItemChildren(item) reusableChildren = self.GetItemChildren(item)
@@ -377,7 +377,7 @@ class VirtualTree(TreeAPIHarmonizer, TreeHelper):
self.Delete(child) self.Delete(child)
def RefreshItemRecursively(self, item, itemIndex): def RefreshItemRecursively(self, item, itemIndex):
''' Refresh the item and its children recursively. ''' """ Refresh the item and its children recursively. """
hasChildren = bool(self.OnGetChildrenCount(itemIndex)) hasChildren = bool(self.OnGetChildrenCount(itemIndex))
item = self.DoRefreshItem(item, itemIndex, hasChildren) item = self.DoRefreshItem(item, itemIndex, hasChildren)
# We need to refresh the children when the item is expanded and # We need to refresh the children when the item is expanded and
@@ -388,7 +388,7 @@ class VirtualTree(TreeAPIHarmonizer, TreeHelper):
self.SetItemHasChildren(item, hasChildren) self.SetItemHasChildren(item, hasChildren)
def DoRefreshItem(self, item, index, hasChildren): def DoRefreshItem(self, item, index, hasChildren):
''' Refresh one item. ''' """ Refresh one item. """
item = self.RefreshItemType(item, index) item = self.RefreshItemType(item, index)
self.RefreshItemText(item, index) self.RefreshItemText(item, index)
self.RefreshColumns(item, index) self.RefreshColumns(item, index)
@@ -458,7 +458,7 @@ class VirtualTree(TreeAPIHarmonizer, TreeHelper):
event.Skip() event.Skip()
def __refreshAttribute(self, item, index, attribute, *args): def __refreshAttribute(self, item, index, attribute, *args):
''' Refresh the specified attribute if necessary. ''' """ Refresh the specified attribute if necessary. """
value = getattr(self, 'OnGet%s'%attribute)(index, *args) value = getattr(self, 'OnGet%s'%attribute)(index, *args)
if getattr(self, 'Get%s'%attribute)(item, *args) != value: if getattr(self, 'Get%s'%attribute)(item, *args) != value:
return getattr(self, 'Set%s'%attribute)(item, value, *args) return getattr(self, 'Set%s'%attribute)(item, value, *args)
@@ -467,7 +467,7 @@ class VirtualTree(TreeAPIHarmonizer, TreeHelper):
class DragAndDrop(TreeAPIHarmonizer, TreeHelper): class DragAndDrop(TreeAPIHarmonizer, TreeHelper):
''' This is a mixin class that can be used to easily implement """ This is a mixin class that can be used to easily implement
dragging and dropping of tree items. It can be mixed in with dragging and dropping of tree items. It can be mixed in with
wx.TreeCtrl, wx.gizmos.TreeListCtrl, or wx.lib.customtree.CustomTreeCtrl. wx.TreeCtrl, wx.gizmos.TreeListCtrl, or wx.lib.customtree.CustomTreeCtrl.
@@ -480,7 +480,7 @@ class DragAndDrop(TreeAPIHarmonizer, TreeHelper):
dropped an item on top of another item. It's up to you to decide how dropped an item on top of another item. It's up to you to decide how
to handle the drop. If you are using this mixin together with the to handle the drop. If you are using this mixin together with the
VirtualTree mixin, it makes sense to rearrange your underlying data VirtualTree mixin, it makes sense to rearrange your underlying data
and then call RefreshItems to let the virtual tree refresh itself. ''' and then call RefreshItems to let the virtual tree refresh itself. """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs['style'] = kwargs.get('style', wx.TR_DEFAULT_STYLE) | \ kwargs['style'] = kwargs.get('style', wx.TR_DEFAULT_STYLE) | \
@@ -489,11 +489,11 @@ class DragAndDrop(TreeAPIHarmonizer, TreeHelper):
self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnBeginDrag) self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnBeginDrag)
def OnDrop(self, dropItem, dragItem): def OnDrop(self, dropItem, dragItem):
''' This function must be overloaded in the derived class. """ This function must be overloaded in the derived class.
dragItem is the item being dragged by the user. dropItem is the dragItem is the item being dragged by the user. dropItem is the
item dragItem is dropped upon. If the user doesn't drop dragItem item dragItem is dropped upon. If the user doesn't drop dragItem
on another item, dropItem equals the (hidden) root item of the on another item, dropItem equals the (hidden) root item of the
tree control. ''' tree control. """
raise NotImplementedError raise NotImplementedError
def OnBeginDrag(self, event): def OnBeginDrag(self, event):
@@ -567,7 +567,7 @@ class DragAndDrop(TreeAPIHarmonizer, TreeHelper):
class ExpansionState(TreeAPIHarmonizer, TreeHelper): class ExpansionState(TreeAPIHarmonizer, TreeHelper):
''' This is a mixin class that can be used to save and restore """ This is a mixin class that can be used to save and restore
the expansion state (i.e. which items are expanded and which items the expansion state (i.e. which items are expanded and which items
are collapsed) of a tree. It can be mixed in with wx.TreeCtrl, are collapsed) of a tree. It can be mixed in with wx.TreeCtrl,
wx.gizmos.TreeListCtrl, or wx.lib.customtree.CustomTreeCtrl. wx.gizmos.TreeListCtrl, or wx.lib.customtree.CustomTreeCtrl.
@@ -590,20 +590,20 @@ class ExpansionState(TreeAPIHarmonizer, TreeHelper):
expansion doesn't depend on the position of items in the tree, but expansion doesn't depend on the position of items in the tree, but
rather on some more stable characteristic of the underlying domain rather on some more stable characteristic of the underlying domain
object, e.g. a social security number in case of persons or an isbn object, e.g. a social security number in case of persons or an isbn
number in case of books. ''' number in case of books. """
def GetItemIdentity(self, item): def GetItemIdentity(self, item):
''' Return a hashable object that represents the identity of the """ Return a hashable object that represents the identity of the
item. By default this returns the position of the item in the item. By default this returns the position of the item in the
tree. You may want to override this to return the item label tree. You may want to override this to return the item label
(if you know that labels are unique and don't change), or return (if you know that labels are unique and don't change), or return
something that represents the underlying domain object, e.g. something that represents the underlying domain object, e.g.
a database key. ''' a database key. """
return self.GetIndexOfItem(item) return self.GetIndexOfItem(item)
def GetExpansionState(self): def GetExpansionState(self):
''' GetExpansionState() -> list of expanded items. Expanded items """ GetExpansionState() -> list of expanded items. Expanded items
are coded as determined by the result of GetItemIdentity(item). ''' are coded as determined by the result of GetItemIdentity(item). """
root = self.GetRootItem() root = self.GetRootItem()
if not root: if not root:
return [] return []
@@ -613,9 +613,9 @@ class ExpansionState(TreeAPIHarmonizer, TreeHelper):
return self.GetExpansionStateOfItem(root) return self.GetExpansionStateOfItem(root)
def SetExpansionState(self, listOfExpandedItems): def SetExpansionState(self, listOfExpandedItems):
''' SetExpansionState(listOfExpandedItems). Expands all tree items """ SetExpansionState(listOfExpandedItems). Expands all tree items
whose identity, as determined by GetItemIdentity(item), is present whose identity, as determined by GetItemIdentity(item), is present
in the list and collapses all other tree items. ''' in the list and collapses all other tree items. """
root = self.GetRootItem() root = self.GetRootItem()
if not root: if not root:
return return