| 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 |
from traits.api import HasStrictTraits, Instance, Int, List, Regex, Str |
| 12 |
from traitsui.api import InstanceEditor, InstanceFactoryChoice, Item, View |
| 13 |
|
| 14 |
# ------------------------------------------------------------------------- |
| 15 |
# 'Person' class: |
| 16 |
# ------------------------------------------------------------------------- |
| 17 |
|
| 18 |
|
| 19 |
class Person(HasStrictTraits): |
| 20 |
|
| 21 |
# ------------------------------------------------------------------------- |
| 22 |
# Trait definitions: |
| 23 |
# ------------------------------------------------------------------------- |
| 24 |
|
| 25 |
name = Str() |
| 26 |
age = Int() |
| 27 |
phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') |
| 28 |
|
| 29 |
# ------------------------------------------------------------------------- |
| 30 |
# Traits view definition: |
| 31 |
# ------------------------------------------------------------------------- |
| 32 |
|
| 33 |
traits_view = View('name~', 'age~', 'phone~') |
| 34 |
edit_view = View('name', 'age', 'phone') |
| 35 |
|
| 36 |
|
| 37 |
# ------------------------------------------------------------------------- |
| 38 |
# Sample data: |
| 39 |
# ------------------------------------------------------------------------- |
| 40 |
|
| 41 |
people = [ |
| 42 |
Person(name='Dave', age=39, phone='555-1212'), |
| 43 |
Person(name='Mike', age=28, phone='555-3526'), |
| 44 |
Person(name='Joe', age=34, phone='555-6943'), |
| 45 |
Person(name='Tom', age=22, phone='555-7586'), |
| 46 |
Person(name='Dick', age=63, phone='555-3895'), |
| 47 |
Person(name='Harry', age=46, phone='555-3285'), |
| 48 |
Person(name='Sally', age=43, phone='555-8797'), |
| 49 |
Person(name='Fields', age=31, phone='555-3547'), |
| 50 |
] |
| 51 |
|
| 52 |
# ------------------------------------------------------------------------- |
| 53 |
# 'Team' class: |
| 54 |
# ------------------------------------------------------------------------- |
| 55 |
|
| 56 |
|
| 57 |
class Team(HasStrictTraits): |
| 58 |
|
| 59 |
# ------------------------------------------------------------------------- |
| 60 |
# Trait definitions: |
| 61 |
# ------------------------------------------------------------------------- |
| 62 |
|
| 63 |
name = Str() |
| 64 |
captain = Instance(Person) |
| 65 |
roster = List(Person) |
| 66 |
|
| 67 |
# ------------------------------------------------------------------------- |
| 68 |
# Traits view definitions: |
| 69 |
# ------------------------------------------------------------------------- |
| 70 |
|
| 71 |
traits_view = View( |
| 72 |
[ |
| 73 |
[ |
| 74 |
'name', |
| 75 |
'_', |
| 76 |
Item( |
| 77 |
'captain@', |
| 78 |
editor=InstanceEditor( |
| 79 |
name='roster', |
| 80 |
editable=False, |
| 81 |
values=[ |
| 82 |
InstanceFactoryChoice( |
| 83 |
klass=Person, |
| 84 |
name='Non player', |
| 85 |
view='edit_view', |
| 86 |
) |
| 87 |
], |
| 88 |
), |
| 89 |
), |
| 90 |
'_', |
| 91 |
], |
| 92 |
['captain@', '|<>'], |
| 93 |
] |
| 94 |
) |
| 95 |
|
| 96 |
|
| 97 |
# ------------------------------------------------------------------------- |
| 98 |
# Run the test: |
| 99 |
# ------------------------------------------------------------------------- |
| 100 |
|
| 101 |
if __name__ == '__main__': |
| 102 |
t = Team(name='Vultures', captain=people[0], roster=people) |
| 103 |
t.configure_traits() |