| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" Demonstrates how to use the Dynamic Views facility. |
| 12 |
""" |
| 13 |
|
| 14 |
|
| 15 |
from traits.api import Bool, HasTraits, Str, Instance, Button |
| 16 |
|
| 17 |
from traitsui.api import View, HGroup, Group, Item, Handler, Label, spring |
| 18 |
|
| 19 |
from traitsui.extras.has_dynamic_views import DynamicView, HasDynamicViews |
| 20 |
|
| 21 |
|
| 22 |
class HasFooView(HasDynamicViews): |
| 23 |
"""A base class declaring the existence of the 'foo' dynamic view.""" |
| 24 |
|
| 25 |
def __init__(self, *args, **traits): |
| 26 |
"""Constructor. |
| 27 |
|
| 28 |
Extended to declare our dynamic foo view. |
| 29 |
""" |
| 30 |
super().__init__(*args, **traits) |
| 31 |
|
| 32 |
# Declare and add our dynamic view: |
| 33 |
declaration = DynamicView( |
| 34 |
name='foo', |
| 35 |
id='traitsui.demos.dynamic_views', |
| 36 |
keywords={ |
| 37 |
'buttons': ['OK'], |
| 38 |
'dock': 'tab', |
| 39 |
'height': 0.4, |
| 40 |
'width': 0.4, |
| 41 |
'resizable': True, |
| 42 |
'scrollable': True, |
| 43 |
}, |
| 44 |
use_as_default=True, |
| 45 |
) |
| 46 |
self.declare_dynamic_view(declaration) |
| 47 |
|
| 48 |
|
| 49 |
class MyInfoHandler(Handler): |
| 50 |
def object_first_changed(self, info): |
| 51 |
info.object.derived = info.object.first |
| 52 |
|
| 53 |
|
| 54 |
class BaseFoo(HasFooView): |
| 55 |
"""A base class that puts some content in the 'foo' dynamic view.""" |
| 56 |
|
| 57 |
first = Str('My first name') |
| 58 |
last = Str('My last name') |
| 59 |
|
| 60 |
# A derived trait set by the handler associated with out dynamic view |
| 61 |
# contribution: |
| 62 |
derived = Str() |
| 63 |
|
| 64 |
ui_person = Group( |
| 65 |
Item( |
| 66 |
label='On this tab, notice how the sub-handler keeps\n' |
| 67 |
'the derived value equal to the first name.\n\n' |
| 68 |
'On the next tab, change the selection in order to\n' |
| 69 |
'control which tabs are visible when the ui is \n' |
| 70 |
'displayed for the 2nd time.' |
| 71 |
), |
| 72 |
spring, |
| 73 |
'first', |
| 74 |
'last', |
| 75 |
spring, |
| 76 |
'derived', |
| 77 |
label='My Info', |
| 78 |
_foo_order=5, |
| 79 |
_foo_priority=1, |
| 80 |
_foo_handler=MyInfoHandler(), |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
class FatherInfoHandler(Handler): |
| 85 |
def object_father_first_name_changed(self, info): |
| 86 |
info.object.father_derived = info.object.father_first_name |
| 87 |
|
| 88 |
|
| 89 |
class DerivedFoo(BaseFoo): |
| 90 |
"""A derived class that puts additional content in the 'foo' dynamic view. |
| 91 |
Note that the additional content could also have been added via a traits |
| 92 |
category contribution, or even dynamic manipulation of metadata on a UI |
| 93 |
subelement. The key is what the metadata represents when the view is |
| 94 |
*created* |
| 95 |
""" |
| 96 |
|
| 97 |
knows_mother = Bool(False) |
| 98 |
mother_first_name = Str("My mother's first name") |
| 99 |
mother_last_name = Str("My mother's last name") |
| 100 |
|
| 101 |
knows_father = Bool(True) |
| 102 |
father_first_name = Str("My father's first name") |
| 103 |
father_last_name = Str("My father's last name") |
| 104 |
father_derived = Str() |
| 105 |
|
| 106 |
ui_parents = Group( |
| 107 |
'knows_mother', |
| 108 |
'knows_father', |
| 109 |
label='Parents?', |
| 110 |
_foo_order=7, |
| 111 |
_foo_priority=1, |
| 112 |
) |
| 113 |
|
| 114 |
ui_mother = Group( |
| 115 |
'mother_first_name', |
| 116 |
'mother_last_name', |
| 117 |
label="Mother's Info", |
| 118 |
_foo_priority=1, |
| 119 |
) |
| 120 |
|
| 121 |
ui_father = Group( |
| 122 |
'father_first_name', |
| 123 |
'father_last_name', |
| 124 |
spring, |
| 125 |
'father_derived', |
| 126 |
label="Father's Info", |
| 127 |
_foo_order=15, |
| 128 |
_foo_priority=1, |
| 129 |
_foo_handler=FatherInfoHandler(), |
| 130 |
) |
| 131 |
|
| 132 |
def _knows_mother_changed(self, old, new): |
| 133 |
ui_mother = self.trait_view('ui_mother') |
| 134 |
if new: |
| 135 |
ui_mother._foo_order = 10 |
| 136 |
elif hasattr(ui_mother, '_foo_order'): |
| 137 |
del ui_mother._foo_order |
| 138 |
|
| 139 |
def _knows_father_changed(self, old, new): |
| 140 |
ui_father = self.trait_view('ui_father') |
| 141 |
if new: |
| 142 |
ui_father._foo_order = 15 |
| 143 |
elif hasattr(ui_father, '_foo_order'): |
| 144 |
del ui_father._foo_order |
| 145 |
|
| 146 |
|
| 147 |
class FooDemo(HasTraits): |
| 148 |
"""Defines a class to run the demo.""" |
| 149 |
|
| 150 |
foo = Instance(DerivedFoo, ()) |
| 151 |
configure = Button('Configure') |
| 152 |
|
| 153 |
view = View( |
| 154 |
Label( |
| 155 |
"Try configuring several times, each time changing the items " |
| 156 |
"on the 'Parents?' tab." |
| 157 |
), |
| 158 |
'_', |
| 159 |
HGroup(spring, Item('configure', show_label=False)), |
| 160 |
) |
| 161 |
|
| 162 |
def _configure_changed(self): |
| 163 |
self.foo.configure_traits() |
| 164 |
|
| 165 |
|
| 166 |
# Create the demo: |
| 167 |
popup = FooDemo() |
| 168 |
|
| 169 |
# Run the demo (if invoked from the command line): |
| 170 |
if __name__ == '__main__': |
| 171 |
popup.configure_traits() |