1
# Copyright (c) 2007, Enthought, Inc.
4
""" Demonstrates how to use the Dynamic Views facility.
8
import Bool, HasTraits, Str, Instance, Button
11
import View, HGroup, Group, Item, Handler, Label, spring
13
from traits.has_dynamic_views \
14
import DynamicView, HasDynamicViews
16
class HasFooView ( HasDynamicViews ):
17
""" A base class declaring the existence of the 'foo' dynamic view.
20
def __init__ ( self, *args, **traits ):
23
Extended to declare our dynamic foo view.
25
super( HasFooView, self ).__init__( *args, **traits )
27
# Declare and add our dynamic view:
28
declaration = DynamicView(
30
id = 'traitsui.demos.dynamic_views',
39
use_as_default = True,
41
self.declare_dynamic_view( declaration )
43
class MyInfoHandler ( Handler ):
45
def object_first_changed ( self, info ):
46
info.object.derived = info.object.first
48
class BaseFoo ( HasFooView ):
49
""" A base class that puts some content in the 'foo' dynamic view.
52
first = Str( 'My first name' )
53
last = Str( 'My last name' )
55
# A derived trait set by the handler associated with out dynamic view
60
Item(label='On this tab, notice how the sub-handler keeps\n'
61
'the derived value equal to the first name.\n\n'
62
'On the next tab, change the selection in order to\n'
63
'control which tabs are visible when the ui is \n'
64
'displayed for the 2nd time.'
73
_foo_handler = MyInfoHandler(),
76
class FatherInfoHandler ( Handler ):
78
def object_father_first_name_changed ( self, info ):
79
info.object.father_derived = info.object.father_first_name
81
class DerivedFoo ( BaseFoo ):
82
""" A derived class that puts additional content in the 'foo' dynamic view.
83
Note that the additional content could also have been added via a traits
84
category contribution, or even dynamic manipulation of metadata on a UI
85
subelement. The key is what the metadata represents when the view is
89
knows_mother = Bool( False )
90
mother_first_name = Str( "My mother's first name" )
91
mother_last_name = Str( "My mother's last name" )
93
knows_father = Bool( True )
94
father_first_name = Str( "My father's first name" )
95
father_last_name = Str( "My father's last name" )
109
label = "Mother's Info",
118
label = "Father's Info",
121
_foo_handler = FatherInfoHandler(),
124
def _knows_mother_changed ( self, old, new ):
125
ui_mother = self.trait_view( 'ui_mother' )
127
ui_mother._foo_order = 10
128
elif hasattr( ui_mother, '_foo_order' ):
129
del ui_mother._foo_order
131
def _knows_father_changed ( self, old, new ):
132
ui_father = self.trait_view( 'ui_father' )
134
ui_father._foo_order = 15
135
elif hasattr( ui_father, '_foo_order' ):
136
del ui_father._foo_order
139
class FooDemo ( HasTraits ):
140
""" Defines a class to run the demo.
143
foo = Instance( DerivedFoo, () )
144
configure = Button( 'Configure' )
147
Label( "Try configuring several times, each time changing the items "
148
"on the 'Parents?' tab." ),
150
HGroup( spring, Item( 'configure', show_label = False ) )
153
def _configure_changed ( self ):
154
self.foo.configure_traits()
159
# Run the demo (if invoked from the command line):
160
if __name__ == '__main__':
161
popup.configure_traits()