Debian Codegraph @

traitsui/examples/demo/Standard_Editors/TreeEditor_demo.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 """
12 Tree editor for hierarchal data
13
14 Demonstrates using the TreeEditor to display a hierarchically organized data
15 structure.
16
17 In this case, the tree has the following hierarchy:
18
19 - Partner
20 - Company
21 - Department
22 - Employee
23
24 The TreeEditor generates a hierarchical tree control, consisting of nodes. It
25 is useful for cases where objects contain lists of other objects.
26
27 The tree control is displayed in one pane of the editor, and a user interface
28 for the selected object is displayed in the other pane. The layout orientation
29 of the tree and the object editor is determined by the *orientation* parameter
30 of TreeEditor(), which can be 'horizontal' or 'vertical'.
31
32 You must specify the types of nodes that can appear in the tree using the
33 *nodes* parameter, which must be a list of instances of TreeNode (or of
34 subclasses of TreeNode).
35
36 You must specify the classes whose instances the node type applies to. Use the
37 **node_for** attribute of TreeNode to specify a list of classes; often, this
38 list contains only one class. You can have more than one node type that applies
39 to a particular class; in this case, each object of that class is represented
40 by multiple nodes, one for each applicable node type.
41
42 See the Traits User Manual for more details.
43
44 Please refer to the `TreeEditor API docs`_ for further information.
45
46 .. _TreeEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.tree_editor.html#traitsui.editors.tree_editor.TreeEditor
47 """
48
49 from traits.api import HasTraits, Str, Regex, List, Instance
50
51 from traitsui.api import Item, View, TreeEditor, TreeNode
52
53
54 class Employee(HasTraits):
55 """Defines a company employee."""
56
57 name = Str('<unknown>')
58 title = Str()
59 phone = Regex(regex=r'\d\d\d-\d\d\d\d')
60
61 def default_title(self):
62 self.title = 'Senior Engineer'
63
64
65 class Department(HasTraits):
66 """Defines a department with employees."""
67
68 name = Str('<unknown>')
69 employees = List(Employee)
70
71
72 class Company(HasTraits):
73 """Defines a company with departments and employees."""
74
75 name = Str('<unknown>')
76 departments = List(Department)
77 employees = List(Employee)
78
79
80 # Create an empty view for objects that have no data to display:
81 no_view = View()
82
83 # Define the TreeEditor used to display the hierarchy:
84 tree_editor = TreeEditor(
85 nodes=[
86 # The first node specified is the top level one
87 TreeNode(
88 node_for=[Company],
89 auto_open=True,
90 # child nodes are
91 children='',
92 label='name', # label with Company name
93 view=View(['name']),
94 ),
95 TreeNode(
96 node_for=[Company],
97 auto_open=True,
98 children='departments',
99 label='=Departments', # constant label
100 view=no_view,
101 add=[Department],
102 ),
103 TreeNode(
104 node_for=[Company],
105 auto_open=True,
106 children='employees',
107 label='=Employees', # constant label
108 view=no_view,
109 add=[Employee],
110 ),
111 TreeNode(
112 node_for=[Department],
113 auto_open=True,
114 children='employees',
115 label='name', # label with Department name
116 view=View(['name']),
117 add=[Employee],
118 ),
119 TreeNode(
120 node_for=[Employee],
121 auto_open=True,
122 label='name', # label with Employee name
123 view=View(['name', 'title', 'phone']),
124 ),
125 ]
126 )
127
128
129 class Partner(HasTraits):
130 """Defines a business partner."""
131
132 name = Str('<unknown>')
133 company = Instance(Company)
134
135 traits_view = View(
136 Item(name='company', editor=tree_editor, show_label=False),
137 title='Company Structure',
138 buttons=['OK'],
139 resizable=True,
140 style='custom',
141 width=0.3,
142 height=500,
143 )
144
145
146 # Create an example data structure:
147 jason = Employee(name='Jason', title='Senior Engineer', phone='536-1057')
148 mike = Employee(name='Mike', title='Senior Engineer', phone='536-1057')
149 dave = Employee(name='Dave', title='Senior Developer', phone='536-1057')
150 martin = Employee(name='Martin', title='Senior Engineer', phone='536-1057')
151 duncan = Employee(name='Duncan', title='Consultant', phone='526-1057')
152
153 # Create the demo:
154 demo = Partner(
155 name='Enthought, Inc.',
156 company=Company(
157 name='Enthought',
158 employees=[dave, martin, duncan, jason, mike],
159 departments=[
160 Department(name='Business', employees=[jason, mike]),
161 Department(name='Scientific', employees=[dave, martin, duncan]),
162 ],
163 ),
164 )
165
166 # Run the demo (if invoked from the command line):
167 if __name__ == '__main__':
168 demo.configure_traits()