Debian Codegraph @

integrationtests/ui/instance_editor_test6.py

raw
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', buttons=['OK', 'Cancel'])
34
35
36 # -------------------------------------------------------------------------
37 # Sample data:
38 # -------------------------------------------------------------------------
39
40 people = [
41 Person(name='Dave', age=39, phone='555-1212'),
42 Person(name='Mike', age=28, phone='555-3526'),
43 Person(name='Joe', age=34, phone='555-6943'),
44 Person(name='Tom', age=22, phone='555-7586'),
45 Person(name='Dick', age=63, phone='555-3895'),
46 Person(name='Harry', age=46, phone='555-3285'),
47 Person(name='Sally', age=43, phone='555-8797'),
48 Person(name='Fields', age=31, phone='555-3547'),
49 ]
50
51 # -------------------------------------------------------------------------
52 # 'Team' class:
53 # -------------------------------------------------------------------------
54
55
56 class Team(HasStrictTraits):
57
58 # -------------------------------------------------------------------------
59 # Trait definitions:
60 # -------------------------------------------------------------------------
61
62 name = Str()
63 captain = Instance(Person)
64 roster = List(Person)
65
66 # -------------------------------------------------------------------------
67 # Traits view definitions:
68 # -------------------------------------------------------------------------
69
70 traits_view = View(
71 [
72 'name',
73 '_',
74 Item(
75 'captain',
76 editor=InstanceEditor(
77 name='roster',
78 label='Edit...',
79 values=[
80 InstanceFactoryChoice(
81 klass=Person, name='Non player', view='edit_view'
82 )
83 ],
84 ),
85 ),
86 ],
87 buttons=['OK', 'Cancel'],
88 )
89
90
91 # -------------------------------------------------------------------------
92 # Run the test:
93 # -------------------------------------------------------------------------
94
95 if __name__ == '__main__':
96 Team(name='Vultures', captain=people[0], roster=people).configure_traits()