3

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.

Disabled menu items.

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()
user
  • 5,370
  • 8
  • 47
  • 75
Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62

1 Answers1

1

I managed to make all the Person menu entries to appear by adding some canXXX methods, as well as the Paste entry of the Department menu entry. I put some print statements to see which and when the methods are called.

Unfortunately, all the other entries of the Department menu resisted despite all my efforts (e.g. 'Rename', that you would not expect to be very complicated to obtain). Also, cut and paste exist but are not operative, other methods like append_child in Department should probably be implemented.

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, TreeNode

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(self):
        print('can_delete from PersonAdapter')
        return True

    def can_delete_me(self):
        print('can_delete_me from PersonAdapter')
        return True

    def confirm_delete(self):
        return True

    def can_copy ( self ):
        """ Returns whether the object's children can be copied.
        """
        print('can_copy from PersonAdapter')
        return True

    def can_rename_me ( self ):
        """ Returns whether the object can be renamed.
        """
        print('can_rename_me from PersonAdapter')
        return True

    def can_rename ( self ):
        """ Returns whether the object can be renamed.
        """
        print('can_rename_me from PersonAdapter')
        return True

    def can_add ( self, add_object ):
        """ Returns whether a given object is droppable on the node.
        """
        print('can_add from PersonAdapter')
        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

    def can_copy ( self ):
        """ Returns whether the object's children can be copied.
        """
        print('can_copy from DepartmentAdapter')
        return True

    def can_add ( self, add_object ):
        """ Returns whether a given object is droppable on the node.
        """
        print('can_add from DepartmentAdapter')
        return True

    def can_delete ( self ):
        """ Returns whether a given object is droppable on the node.
        """
        print('can_delete from DepartmentAdapter')
        return True

    def can_delete_me ( self ):
        """ Returns whether a given object is droppable on the node.
        """
        print('can_delete_me from DepartmentAdapter')
        return True

    def can_rename ( self ):
        """ Returns whether the object can be renamed.
        """
        print('can_rename from DepartmentAdapter')
        return True

    def can_rename_me ( self ):
        """ Returns whether the object can be renamed.
        """
        print('can_rename_me from DepartmentAdapter')
        return True

    def can_insert ( self ):
        """ Returns whether the object's children can be inserted (vs.
            appended).
        """
        print('can_insert from DepartmentAdapter')
        return True

    def get_add ( self ):
        """ Returns the list of classes that can be added to the object.
        """
        print('get_add from DepartmentAdapter')
        return [ Person ]

# 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()
Yves Surrel
  • 193
  • 1
  • 10