I have modified the Adapted_tree_editor_demo.py example code to display some simple model objects using the adaptor approach. However, right-click menu options are all disabled. How can I enable them? Implementing can_delete_me()
and confirm_delete()
does not seem to help.
from os \
import getcwd
from traits.api \
import HasTraits, Property, Directory, Instance, Str, List, adapts, property_depends_on
from traitsui.api \
import View, ModelView, VGroup, Item, TreeEditor, ITreeNode, ITreeNodeAdapter
from pkg_resources import resource_filename
traits_icons_path = resource_filename("traitsui", "wx/images")
# Model classes to adapt
class Person(HasTraits):
name = Str('Bob')
class Department(HasTraits):
name = Str('Sales')
people = List(Person)
# FileAdapter Class
class PersonAdapter ( ITreeNodeAdapter ):
adapts( Person, ITreeNode )
def allows_children ( self ):
""" Returns whether this object can have children.
"""
return False
def has_children ( self ):
""" Returns whether the object has children.
"""
return False
def get_children ( self ):
""" Gets the object's children.
"""
return []
def get_label ( self ):
""" Gets the label to display for a specified object.
"""
return self.adaptee.name
def get_tooltip ( self ):
""" Gets the tooltip to display for a specified object.
"""
if self.adaptee.name == 'Bob':
return 'This is Bob, he is a man.'
elif self.adaptee.name == 'Alice':
return 'This is Alice, she is a woman.'
return 'An employee of a department.'
def get_icon_path(self):
""" Return the path of the icons
"""
return traits_icons_path
def get_icon ( self, is_expanded ):
""" Returns the icon for a specified object.
"""
if self.adaptee.name == 'Bob':
return 'file'
elif self.adaptee.name == 'Alice':
return 'object'
else:
return 'item'
def can_auto_close ( self ):
""" Returns whether the object's children should be automatically
closed.
"""
return True
def can_delete_me(self):
return True
def confirm_delete(self):
return True
class DepartmentAdapter ( ITreeNodeAdapter ):
adapts( Department, ITreeNode )
def allows_children ( self ):
""" Returns whether this object can have children.
"""
return True
def has_children ( self ):
""" Returns whether the object has children.
"""
return len(self.adaptee.people) > 0
def get_children ( self ):
""" Gets the object's children.
"""
return self.adaptee.people
def get_label ( self ):
""" Gets the label to display for a specified object.
"""
return self.adaptee.name
def get_tooltip ( self ):
""" Gets the tooltip to display for a specified object.
"""
return 'The {} department'.format(self.adaptee.name)
def get_icon ( self, is_expanded ):
""" Returns the icon for a specified object.
"""
if is_expanded:
return '<open>'
return '<close>'
def can_auto_close ( self ):
""" Returns whether the object's children should be automatically
closed.
"""
return True
# Create main view
class DepartmentView(ModelView):
model = Instance(Department)
# The traits view to display:
view = View(
VGroup(
Item( 'object.model.name', style='readonly'),
Item( 'model',
editor = TreeEditor( editable = False, auto_open = 1 )
),
show_labels = False
),
width = 0.33,
height = 0.50,
resizable = True
)
# Create model object graph
bob = Person(name='Bob')
alice = Person(name='Alice')
dept = Department(name='Sales', people=[bob, alice])
view = DepartmentView(model=dept)
# Run the demo (if invoked form the command line):
if __name__ == '__main__':
view.configure_traits()