TreeEditor and Instance variables example [was:TreeNode instance view]

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

TreeEditor and Instance variables example [was:TreeNode instance view]

Eraldo Pomponi
Hi all,

I'm posting a small example about the problem that I had to show instance variables 
with TreeEditor. Thanks to Brennan for the help.


Cheers,
Eraldo 

#####################################################################
from traits.api import HasTraits, Str, List, Instance, adapts, Int
from traitsui.api import View, Item, TreeEditor, TreeNode, ITreeNodeAdapter, ITreeNode


class Foo(HasTraits):
    """ Example class with instance variables."""
    name = Str('<nn>')

    def __init__(self):
        super(HasTraits, self).__init__()
        self.name = 'Pippo'
        self.add_trait('n1', Int(10))
        self.add_trait('n2', Int(20))

    def _trait_view_default(self):
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        return View(*items)


class ListFoo(HasTraits):
    """Container class."""
    name = Str('List of Foo')
    foos = List(Foo)


class FooObjectAdapter(ITreeNodeAdapter):
    """ITreeNodeAdapter for class Foo.""" 
    adapts(Foo, ITreeNode)

    def allows_children(self):
        return False

    def has_children(self):
        return False

    def get_view(self):
        return self.adaptee.trait_view()
    
    def get_label(self):
        return self.adaptee.name
 

no_view = View()


# Get a tree editor
def _tree_editor(selected=''):
    """Return a TreeEditor specific for ListFoo that has no adapter."""
    return TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='name',
                             view=no_view,
                             )
                    ],
                    #editable=False,
                    selected=selected
               )


class TreeTest(HasTraits):
    """ Example class. Tree editor and instance variables."""

    name    = Str('<unknown>')
    lfoo = Instance(ListFoo)
    cfoo = Instance(Foo)

    view = View(
        Item(name       = 'lfoo',
             editor     = _tree_editor(selected='cfoo'),
             show_label = False
        ),
        title     = 'Tree Test',
        buttons   = [ 'OK' ],
        resizable = True,
        style     = 'custom',
        width     = .3,
        height    = .5
    )

    def _cfoo_changed(self):
        print self.cfoo.n1


if __name__ == '__main__':
    a = Foo()
    b = Foo()
    
    lf = ListFoo(name = 'lf', foos = [a,b])
    tt = TreeTest(name = 'Instance Tree', lfoo = lf)
    tt.configure_traits()


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: TreeEditor and Instance variables example [was:TreeNode instance view]

Eraldo Pomponi
Hi All, 

there was an error in the example I posted. This is the corrected version: 

#############################################################

from traits.api import HasTraits, Str, List, Instance, adapts, Int, Any
from traitsui.api import View, Item, TreeEditor, TreeNode, ITreeNodeAdapter, \
    ITreeNode


class Foo(HasTraits):
    """ Example class with instance variables."""
    name = Str('<nn>')
    ist_view = Any

    def __init__(self):
        super(HasTraits, self).__init__()
        self.name = 'Pippo'
        self.add_trait('n1', Int(10))
        self.add_trait('n2', Int(20))

    def _ist_view_default(self):
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        return View(*items)


class ListFoo(HasTraits):
    """Container class."""
    name = Str('List of Foo')
    foos = List(Foo)


class FooObjectAdapter(ITreeNodeAdapter):
    """ITreeNodeAdapter for class Foo."""
    adapts(Foo, ITreeNode)

    def allows_children(self):
        return False

    def has_children(self):
        return False

    def get_view(self):
        return self.adaptee.ist_view

    def get_label(self):
        return self.adaptee.name


no_view = View()


# Get a tree editor
def _tree_editor(selected=''):
    """Return a TreeEditor specific for ListFoo that has no adapter."""
    return TreeEditor(
            nodes=[
                    TreeNode(
                             node_for=[ListFoo],
                             auto_open=True,
                             children='foos',
                             label='name',
                             view=no_view,
                             )
                    ],
                    editable=True,
                    selected=selected
               )


class TreeTest(HasTraits):
    """ Example class. Tree editor and instance variables."""

    name = Str('<unknown>')
    lfoo = Instance(ListFoo)
    cfoo = Instance(Foo)

    view = View(
        Item(name='lfoo',
             editor=_tree_editor(selected='cfoo'),
             show_label=False
        ),
        title='Tree Test',
        buttons=['OK'],
        resizable=True,
        style='custom',
        width=.3,
        height=.5
    )

    def _cfoo_changed(self):
        print self.cfoo.n1, self.cfoo.n2


if __name__ == '__main__':
    a = Foo()
    b = Foo()

    lf = ListFoo(name='lf', foos=[a, b])
    tt = TreeTest(name='Instance Tree', lfoo=lf)
    tt.configure_traits()
#######################################################

Cheers,
Eraldo 


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev