traits: InstanceEditor name updates

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

traits: InstanceEditor name updates

Cherrett, Adam

Hi everyone,

 

I’m trying to write a GUI that uses traits, in which I can define a list of objects of a certain type, and then select individual instances to be used in other definitions. A cartoon version of the code that illustrates my problem is quoted below.

 

This nearly works, in that the InstanceEditor allows me to select one of the available objects in the list (bar.foos). When I add or delete objects, the available options are updated, as I expect. However, it does not update when the names of the Foo objects are changed. Is there any way to enable this ?

 

Many thanks,

 

Adam

 

 

#!/usr/bin/env python

 

from traits.api import HasTraits, Str, Instance, List

from traitsui.api import InstanceEditor, ModelView, View, Item, TableEditor, ObjectColumn

 

class Foo(HasTraits):

    name = Str

   

    def __init__(self, name='Foo'):

        self.name = name

 

class Bar(HasTraits):

    foos = List(Foo)

    selected = Instance(Foo)

   

    def __init__(self):

        self.foos = [ Foo('bing'), Foo('bang'), Foo('bong') ]

        self.selected = self.foos[0]

class BarModelView(ModelView):

     

    view = View(Item('model.foos',

                     editor=TableEditor(columns = [ ObjectColumn(name='name', label='Name') ],

                                        deletable = True,

                                        sort_model = False,

                                        auto_size = False,

                                        configurable = False,

                                        reorderable = False,

                                        show_toolbar = True,

                                        sortable = False,

                                        row_factory = Foo )),

                Item('model.selected', editor=InstanceEditor(name='model.foos', editable=False, selectable=True)),

                resizable = True)

 

bar = Bar()

mv = BarModelView(bar)

 

mv.configure_traits()

   



This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to which they are addressed. If you have received this e-mail in error please notify the system manager at [hidden email].

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

Re: traits: InstanceEditor name updates

Pietro Berkes
Hi Adam,

I recommend using an EnumEditor for this task. You can then give a mapping between the objects that can be selected and the label that appear on the drop-down menu. In this way, you can listen explicitly for changes in the traits you care about (foo.name), and update the mapping accordingly. The mapping is an object required solely for the UI, so I would put it in BarModelView:

class BarModelView(ModelView):

    foo_menu_values = Property(depends_on='model:foos:name,model:foos[]')
    @cached_property
    def _get_foo_menu_values(self):
        values = {foo: foo.name for foo in self.model.foos}
        return values

    view = View(Item('model.foos',
                     editor=TableEditor(
                         columns=[ObjectColumn(name='name', label='Name')],
                         deletable = True,
                         sort_model = False,
                         auto_size = False,
                         configurable = False,
                         reorderable = False,
                         show_toolbar = True,
                         sortable = False,
                         row_factory = Foo),
                     ),
                Item('model.selected',
                     editor=EnumEditor(name='object.foo_menu_values')),
                resizable = True)

Best,
P.



On Fri, Jul 26, 2013 at 10:15 AM, Cherrett, Adam <[hidden email]> wrote:

Hi everyone,

 

I’m trying to write a GUI that uses traits, in which I can define a list of objects of a certain type, and then select individual instances to be used in other definitions. A cartoon version of the code that illustrates my problem is quoted below.

 

This nearly works, in that the InstanceEditor allows me to select one of the available objects in the list (bar.foos). When I add or delete objects, the available options are updated, as I expect. However, it does not update when the names of the Foo objects are changed. Is there any way to enable this ?

 

Many thanks,

 

Adam

 

 

#!/usr/bin/env python

 

from traits.api import HasTraits, Str, Instance, List

from traitsui.api import InstanceEditor, ModelView, View, Item, TableEditor, ObjectColumn

 

class Foo(HasTraits):

    name = Str

   

    def __init__(self, name='Foo'):

        self.name = name

 

class Bar(HasTraits):

    foos = List(Foo)

    selected = Instance(Foo)

   

    def __init__(self):

        self.foos = [ Foo('bing'), Foo('bang'), Foo('bong') ]

        self.selected = self.foos[0]

class BarModelView(ModelView):

     

    view = View(Item('model.foos',

                     editor=TableEditor(columns = [ ObjectColumn(name='name', label='Name') ],

                                        deletable = True,

                                        sort_model = False,

                                        auto_size = False,

                                        configurable = False,

                                        reorderable = False,

                                        show_toolbar = True,

                                        sortable = False,

                                        row_factory = Foo )),

                Item('model.selected', editor=InstanceEditor(name='model.foos', editable=False, selectable=True)),

                resizable = True)

 

bar = Bar()

mv = BarModelView(bar)

 

mv.configure_traits()

   



This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to which they are addressed. If you have received this e-mail in error please notify the system manager at [hidden email].

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




--
Pietro Berkes
Scientific software developer
Enthought UK


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