如何在使用适配器时启用TraitsUI TreeView中的右键菜单?

3
我已经修改了Adapted_tree_editor_demo.py示例代码,使用适配器方法显示一些简单的模型对象。然而,右键菜单选项都是禁用的。如何启用它们?实现can_delete_me()confirm_delete()似乎没有帮助。

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()
1个回答

1
我使用了一些canXXX方法以及在Department菜单条目中添加Paste条目的方式,最终成功使所有Person菜单条目显示出来。我还添加了一些打印语句以查看这些方法被调用的时间和位置。
不幸的是,尽管我付出了努力(例如,你不会认为重命名很难实现),但部门菜单的所有其他条目都没有成功。虽然存在剪切和粘贴,但它们无法操作,而Department中的其他方法(如append_child)可能需要实现。
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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接