TreeNode instance view

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

TreeNode instance view

Eraldo Pomponi
Hi All, 

Is there a straight way to get the view of each node in a TreeEditor class for a 
specific class directly from each instance? 

Something like: 
#######################################################

from traits.api import HasTraits, Str, List, Instance
from traitsui.api import View, Item, TreeEditor, TreeNode

class Foo(HasTraits):
    
    def default_trait_view(self):
        self.add_trait('n1',10)
        self.add_trait('n2',20)
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        return View(*items)

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

no_view = View()

tr = TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             ),
                    TreeNode(
                             node_for=[ Foo ],
                             auto_open=True,
                             children='',
                             label='=Foo class',
                             view='object.trait_view',
                             )
                    ],
                    editable=False
               )

a = Foo()
b = Foo()

class TreeTest(HasTraits):
    """ Defines a business partner."""

    lfoo = Instance(ListFoo)

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


lf = ListFoo(name = 'lf', foos = [a,b])

tt = TreeTest(lfoo = lf)

tt.configure_traits()

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


Sorry but text formatting is a bit messy after copy and past. 
This example doesn't raise any error but still not work. 
Any advises? 


Cheers,
Eraldo 


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

Re: TreeNode instance view

Brennan Williams
I create a MyTreeNode class which inherits from TreeNode
I then have a get_view method in that class which returns the view definition (if it hasn't been created yet then I call that object's create_view method)

something along the lines of...

class MyTreeNode(TreeNode):

    def get_view(self,object):

    if isinstance(object,Thing):
      if self.add:
        childobject=str(self.add[0])

        if childobject.find("Foo")>=0:
          if object.foolist_view:
                return object.foolist_view
          else:
                object.foolist_view=object.create_foolist_view()
                return object.foolist_view
        else:
          if object.thing_view:
                return object.thing_view
          else:
                object.thing_view=object.create_thing_view()
                return object.thing_view
    elif isinstance(object,AnotherThing):
        if object.anotherthing_view:
                return object.anotherthing_view
        else:
                object.anotherthing_view=object.create_anotherthing_view()
                return object.ranotherthing_view

etc etc

Hope that helps.

Brennan

On 13/06/2012 1:33 p.m., Eraldo Pomponi wrote:
Hi All, 

Is there a straight way to get the view of each node in a TreeEditor class for a 
specific class directly from each instance? 

Something like: 
#######################################################

from traits.api import HasTraits, Str, List, Instance
from traitsui.api import View, Item, TreeEditor, TreeNode

class Foo(HasTraits):
    
    def default_trait_view(self):
        self.add_trait('n1',10)
        self.add_trait('n2',20)
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        return View(*items)

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

no_view = View()

tr = TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             ),
                    TreeNode(
                             node_for=[ Foo ],
                             auto_open=True,
                             children='',
                             label='=Foo class',
                             view='object.trait_view',
                             )
                    ],
                    editable=False
               )

a = Foo()
b = Foo()

class TreeTest(HasTraits):
    """ Defines a business partner."""

    lfoo = Instance(ListFoo)

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


lf = ListFoo(name = 'lf', foos = [a,b])

tt = TreeTest(lfoo = lf)

tt.configure_traits()

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


Sorry but text formatting is a bit messy after copy and past. 
This example doesn't raise any error but still not work. 
Any advises? 


Cheers,
Eraldo 



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


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

Re: TreeNode instance view

Eraldo Pomponi
Dear Brennan, 



On Wed, Jun 13, 2012 at 3:31 PM, Brennan Williams <[hidden email]> wrote:
I create a MyTreeNode class which inherits from TreeNode
I then have a get_view method in that class which returns the view definition (if it hasn't been created yet then I call that object's create_view method)


I tried to follow your suggestion (and others ideas) but without success. The strange thing is that it works fine but the selected item View is never displayed. 

Any thought? 

Cheers,
Eraldo 


########## Updated Code ##################################

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


class Foo(HasTraits):
    
    view = View()

    def __init__(self):
        super(HasTraits, self).__init__()
        self.create_trait_view()

    def create_trait_view(self):
        self.add_trait('n1', Int(10))
        self.add_trait('n2', Int(20))
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        self.view = View(*items)


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


class FooObjectAdapter(ITreeNodeAdapter):
    adapts(Foo, ITreeNode)

    def allows_children(self):
        return False

    def has_children(self):
        return False

    def get_view(self,object):
        return object.create_trait_view()
    
    def get_label(self):

        label = 'Foo'
        return label 
        label = xgetattr( object, label, '' )

        if self.formatter is None:
            return label

        return self.formatter( object, label )


class MyTreeNode(TreeNode):
    
    def get_view(self, object):
        view = object.create_trait_view()
        return view


no_view = View()


# Get a tree editor
def _tree_editor(selected=''):
    """Return a TreeEditor specifically for HDF5 file trees."""
    return TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             )
                    ],
                    editable=False,
                    selected=selected
               )


# Get a tree editor
def _tree_editor_brennan(selected=''):
    """Return a TreeEditor specifically for HDF5 file trees."""
    return TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             ),
                    MyTreeNode(
                             node_for=[ Foo ],
                             auto_open=True,
                             children='',
                             label='=Foo class',
                             view=View(Item('n1')),
                             )
                    ],
                    editable=False,
                    selected=selected
               )


a = Foo()
b = Foo()

class TreeTest(HasTraits):
    """ Defines a business partner."""

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

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


lf = ListFoo(name = 'lf', foos = [a,b])


tt = TreeTest(name = 'Instance Tree', lfoo = lf)


tt.configure_traits()



 
something along the lines of...

class MyTreeNode(TreeNode):

    def get_view(self,object):

    if isinstance(object,Thing):
      if self.add:
        childobject=str(self.add[0])

        if childobject.find("Foo")>=0:
          if object.foolist_view:
                return object.foolist_view
          else:
                object.foolist_view=object.create_foolist_view()
                return object.foolist_view
        else:
          if object.thing_view:
                return object.thing_view
          else:
                object.thing_view=object.create_thing_view()
                return object.thing_view
    elif isinstance(object,AnotherThing):
        if object.anotherthing_view:
                return object.anotherthing_view
        else:
                object.anotherthing_view=object.create_anotherthing_view()
                return object.ranotherthing_view

etc etc

Hope that helps.

Brennan


On 13/06/2012 1:33 p.m., Eraldo Pomponi wrote:
Hi All, 

Is there a straight way to get the view of each node in a TreeEditor class for a 
specific class directly from each instance? 

Something like: 
#######################################################

from traits.api import HasTraits, Str, List, Instance
from traitsui.api import View, Item, TreeEditor, TreeNode

class Foo(HasTraits):
    
    def default_trait_view(self):
        self.add_trait('n1',10)
        self.add_trait('n2',20)
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        return View(*items)

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

no_view = View()

tr = TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             ),
                    TreeNode(
                             node_for=[ Foo ],
                             auto_open=True,
                             children='',
                             label='=Foo class',
                             view='object.trait_view',
                             )
                    ],
                    editable=False
               )

a = Foo()
b = Foo()

class TreeTest(HasTraits):
    """ Defines a business partner."""

    lfoo = Instance(ListFoo)

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


lf = ListFoo(name = 'lf', foos = [a,b])

tt = TreeTest(lfoo = lf)

tt.configure_traits()

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


Sorry but text formatting is a bit messy after copy and past. 
This example doesn't raise any error but still not work. 
Any advises? 


Cheers,
Eraldo 



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


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



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

Re: TreeNode instance view

Brennan Williams
On 13/06/2012 3:30 p.m., Eraldo Pomponi wrote:
Dear Brennan, 



On Wed, Jun 13, 2012 at 3:31 PM, Brennan Williams <[hidden email]> wrote:
I create a MyTreeNode class which inherits from TreeNode
I then have a get_view method in that class which returns the view definition (if it hasn't been created yet then I call that object's create_view method)


I tried to follow your suggestion (and others ideas) but without success. The strange thing is that it works fine but the selected item View is never displayed. 

Any thought? 

Try commenting out the view= lines in the TreeNode definitions. Having them in probably means that the get_view method is not overriding the view defined by view= and as these are null views initially that is probably the reason you aren't seeing anything.
Cheers,
Eraldo 


########## Updated Code ##################################

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


class Foo(HasTraits):
    
    view = View()

    def __init__(self):
        super(HasTraits, self).__init__()
        self.create_trait_view()

    def create_trait_view(self):
        self.add_trait('n1', Int(10))
        self.add_trait('n2', Int(20))
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        self.view = View(*items)


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


class FooObjectAdapter(ITreeNodeAdapter):
    adapts(Foo, ITreeNode)

    def allows_children(self):
        return False

    def has_children(self):
        return False

    def get_view(self,object):
        return object.create_trait_view()
    
    def get_label(self):

        label = 'Foo'
        return label 
        label = xgetattr( object, label, '' )

        if self.formatter is None:
            return label

        return self.formatter( object, label )


class MyTreeNode(TreeNode):
    
    def get_view(self, object):
        view = object.create_trait_view()
        return view


no_view = View()


# Get a tree editor
def _tree_editor(selected=''):
    """Return a TreeEditor specifically for HDF5 file trees."""
    return TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             )
                    ],
                    editable=False,
                    selected=selected
               )


# Get a tree editor
def _tree_editor_brennan(selected=''):
    """Return a TreeEditor specifically for HDF5 file trees."""
    return TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             ),
                    MyTreeNode(
                             node_for=[ Foo ],
                             auto_open=True,
                             children='',
                             label='=Foo class',
                             view=View(Item('n1')),
                             )
                    ],
                    editable=False,
                    selected=selected
               )


a = Foo()
b = Foo()

class TreeTest(HasTraits):
    """ Defines a business partner."""

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

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


lf = ListFoo(name = 'lf', foos = [a,b])


tt = TreeTest(name = 'Instance Tree', lfoo = lf)


tt.configure_traits()



 
something along the lines of...

class MyTreeNode(TreeNode):

    def get_view(self,object):

    if isinstance(object,Thing):
      if self.add:
        childobject=str(self.add[0])

        if childobject.find("Foo")>=0:
          if object.foolist_view:
                return object.foolist_view
          else:
                object.foolist_view=object.create_foolist_view()
                return object.foolist_view
        else:
          if object.thing_view:
                return object.thing_view
          else:
                object.thing_view=object.create_thing_view()
                return object.thing_view
    elif isinstance(object,AnotherThing):
        if object.anotherthing_view:
                return object.anotherthing_view
        else:
                object.anotherthing_view=object.create_anotherthing_view()
                return object.ranotherthing_view

etc etc

Hope that helps.

Brennan


On 13/06/2012 1:33 p.m., Eraldo Pomponi wrote:
Hi All, 

Is there a straight way to get the view of each node in a TreeEditor class for a 
specific class directly from each instance? 

Something like: 
#######################################################

from traits.api import HasTraits, Str, List, Instance
from traitsui.api import View, Item, TreeEditor, TreeNode

class Foo(HasTraits):
    
    def default_trait_view(self):
        self.add_trait('n1',10)
        self.add_trait('n2',20)
        trait_dict = self._instance_traits()
        items = [Item(name) for name in sorted(trait_dict)]
        return View(*items)

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

no_view = View()

tr = TreeEditor(
            nodes =[
                    TreeNode(
                             node_for=[ ListFoo ],
                             auto_open=True,
                             children='foos',
                             label='=Foo class',
                             view=no_view,
                             ),
                    TreeNode(
                             node_for=[ Foo ],
                             auto_open=True,
                             children='',
                             label='=Foo class',
                             view='object.trait_view',
                             )
                    ],
                    editable=False
               )

a = Foo()
b = Foo()

class TreeTest(HasTraits):
    """ Defines a business partner."""

    lfoo = Instance(ListFoo)

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


lf = ListFoo(name = 'lf', foos = [a,b])

tt = TreeTest(lfoo = lf)

tt.configure_traits()

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


Sorry but text formatting is a bit messy after copy and past. 
This example doesn't raise any error but still not work. 
Any advises? 


Cheers,
Eraldo 



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


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




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


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