1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" A generic base-class for items in a tree data structure.
An example::
root = TreeItem(data='Root')
fruit = TreeItem(data='Fruit')
fruit.append(TreeItem(data='Apple', allows_children=False))
fruit.append(TreeItem(data='Orange', allows_children=False))
fruit.append(TreeItem(data='Pear', allows_children=False))
root.append(fruit)
veg = TreeItem(data='Veg')
veg.append(TreeItem(data='Carrot', allows_children=False))
veg.append(TreeItem(data='Cauliflower', allows_children=False))
veg.append(TreeItem(data='Sprout', allows_children=False))
root.append(veg)
"""
from traits.api import Any, Bool, HasTraits, Instance, List, Property
class TreeItem(HasTraits):
""" A generic base-class for items in a tree data structure. """
# 'TreeItem' interface -------------------------------------------------
# Does this item allow children?
allows_children = Bool(True)
# The item's children.
children = List(Instance("TreeItem"))
# Arbitrary data associated with the item.
data = Any()
# Does the item have any children?
has_children = Property(Bool)
# The item's parent.
parent = Instance("TreeItem")
# ------------------------------------------------------------------------
# 'object' interface.
# ------------------------------------------------------------------------
def __str__(self):
""" Returns the informal string representation of the object. """
if self.data is None:
s = ""
else:
s = str(self.data)
return s
# ------------------------------------------------------------------------
# 'TreeItem' interface.
# ------------------------------------------------------------------------
# Properties -----------------------------------------------------------
# has_children
def _get_has_children(self):
""" True iff the item has children. """
return len(self.children) != 0
# Methods -------------------------------------------------------------#
def append(self, child):
""" Appends a child to this item.
This removes the child from its current parent (if it has one).
"""
return self.insert(len(self.children), child)
def insert(self, index, child):
""" Inserts a child into this item at the specified index.
This removes the child from its current parent (if it has one).
"""
if child.parent is not None:
child.parent.remove(child)
child.parent = self
self.children.insert(index, child)
return child
def remove(self, child):
""" Removes a child from this item. """
child.parent = None
self.children.remove(child)
return child
def insert_before(self, before, child):
""" Inserts a child into this item before the specified item.
This removes the child from its current parent (if it has one).
"""
index = self.children.index(before)
self.insert(index, child)
return (index, child)
def insert_after(self, after, child):
""" Inserts a child into this item after the specified item.
This removes the child from its current parent (if it has one).
"""
index = self.children.index(after)
self.insert(index + 1, child)
return (index, child)
|