~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to examples/demo/Advanced/Dynamic_views_demo.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#  Copyright (c) 2007, Enthought, Inc.
 
2
#  License: BSD Style.
 
3
 
 
4
""" Demonstrates how to use the Dynamic Views facility.
 
5
"""
 
6
 
 
7
from traits.api \
 
8
    import Bool, HasTraits, Str, Instance, Button
 
9
 
 
10
from traitsui.api \
 
11
    import View, HGroup, Group, Item, Handler, Label, spring
 
12
 
 
13
from traits.has_dynamic_views \
 
14
    import DynamicView, HasDynamicViews
 
15
 
 
16
class HasFooView ( HasDynamicViews ):
 
17
    """ A base class declaring the existence of the 'foo' dynamic view.
 
18
    """
 
19
 
 
20
    def __init__ ( self, *args, **traits ):
 
21
        """ Constructor.
 
22
 
 
23
            Extended to declare our dynamic foo view.
 
24
        """
 
25
        super( HasFooView, self ).__init__( *args, **traits )
 
26
 
 
27
        # Declare and add our dynamic view:
 
28
        declaration = DynamicView(
 
29
            name     = 'foo',
 
30
            id       = 'traitsui.demos.dynamic_views',
 
31
            keywords = {
 
32
                'buttons':    [ 'OK' ],
 
33
                'dock':       'tab',
 
34
                'height':     0.4,
 
35
                'width':      0.4,
 
36
                'resizable':  True,
 
37
                'scrollable': True,
 
38
            },
 
39
            use_as_default = True,
 
40
        )
 
41
        self.declare_dynamic_view( declaration )
 
42
 
 
43
class MyInfoHandler ( Handler ):
 
44
 
 
45
    def object_first_changed ( self, info ):
 
46
        info.object.derived = info.object.first
 
47
 
 
48
class BaseFoo ( HasFooView ):
 
49
    """ A base class that puts some content in the 'foo' dynamic view.
 
50
    """
 
51
 
 
52
    first = Str( 'My first name' )
 
53
    last  = Str( 'My last name' )
 
54
 
 
55
    # A derived trait set by the handler associated with out dynamic view
 
56
    # contribution:
 
57
    derived = Str
 
58
 
 
59
    ui_person = Group(
 
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.'
 
65
            ),
 
66
        spring,
 
67
        'first', 'last',
 
68
        spring,
 
69
        'derived',
 
70
        label = 'My Info',
 
71
        _foo_order    = 5,
 
72
        _foo_priority = 1,
 
73
        _foo_handler  = MyInfoHandler(),
 
74
    )
 
75
 
 
76
class FatherInfoHandler ( Handler ):
 
77
 
 
78
    def object_father_first_name_changed ( self, info ):
 
79
        info.object.father_derived = info.object.father_first_name
 
80
 
 
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
 
86
        *created*
 
87
    """
 
88
 
 
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" )
 
92
 
 
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" )
 
96
    father_derived    = Str
 
97
 
 
98
    ui_parents = Group(
 
99
        'knows_mother',
 
100
        'knows_father',
 
101
        label         = 'Parents?',
 
102
        _foo_order    = 7,
 
103
        _foo_priority = 1,
 
104
    )
 
105
 
 
106
    ui_mother = Group(
 
107
        'mother_first_name',
 
108
        'mother_last_name',
 
109
        label         = "Mother's Info",
 
110
        _foo_priority = 1,
 
111
    )
 
112
 
 
113
    ui_father = Group(
 
114
        'father_first_name',
 
115
        'father_last_name',
 
116
        spring,
 
117
        'father_derived',
 
118
        label         = "Father's Info",
 
119
        _foo_order    = 15,
 
120
        _foo_priority = 1,
 
121
        _foo_handler  = FatherInfoHandler(),
 
122
    )
 
123
 
 
124
    def _knows_mother_changed ( self, old, new ):
 
125
        ui_mother = self.trait_view( 'ui_mother' )
 
126
        if new:
 
127
            ui_mother._foo_order = 10
 
128
        elif hasattr( ui_mother, '_foo_order' ):
 
129
            del ui_mother._foo_order
 
130
 
 
131
    def _knows_father_changed ( self, old, new ):
 
132
        ui_father = self.trait_view( 'ui_father' )
 
133
        if new:
 
134
            ui_father._foo_order = 15
 
135
        elif hasattr( ui_father, '_foo_order' ):
 
136
            del ui_father._foo_order
 
137
 
 
138
 
 
139
class FooDemo ( HasTraits ):
 
140
    """ Defines a class to run the demo.
 
141
    """
 
142
 
 
143
    foo       = Instance( DerivedFoo, () )
 
144
    configure = Button( 'Configure' )
 
145
 
 
146
    view = View(
 
147
        Label( "Try configuring several times, each time changing the items "
 
148
               "on the 'Parents?' tab." ),
 
149
        '_',
 
150
        HGroup( spring, Item( 'configure', show_label = False ) )
 
151
    )
 
152
 
 
153
    def _configure_changed ( self ):
 
154
        self.foo.configure_traits()
 
155
 
 
156
# Create the demo:
 
157
popup = FooDemo()
 
158
 
 
159
# Run the demo (if invoked from the command line):
 
160
if __name__ == '__main__':
 
161
    popup.configure_traits()
 
162