Debian Codegraph @

docs/source/traitsui_user_manual/examples/tree_editor.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 # tree_editor.py -- Example of a tree editor
12
13 from traits.api import HasTraits, Instance, List, Str, Regex
14 from traitsui.api import (
15 Action,
16 Group,
17 Handler,
18 HGroup,
19 Item,
20 Menu,
21 Separator,
22 TreeEditor,
23 TreeNode,
24 View,
25 VSplit,
26 )
27 from traitsui.editors.tree_editor import (
28 NewAction,
29 CopyAction,
30 CutAction,
31 PasteAction,
32 DeleteAction,
33 RenameAction,
34 )
35
36
37 class Employee(HasTraits):
38 name = Str('<unknown>')
39 title = Str()
40 phone = Regex(regex=r'\d\d\d-\d\d\d\d')
41
42 def default_title(self):
43 self.title = 'Senior Engineer'
44
45
46 class Department(HasTraits):
47 name = Str('<unknown>')
48 employees = List(Employee)
49
50
51 class Company(HasTraits):
52 name = Str('<unknown>')
53 departments = List(Department)
54 employees = List(Employee)
55
56
57 class Owner(HasTraits):
58 name = Str('<unknown>')
59 company = Instance(Company)
60
61
62 jason = Employee(name='Jason', title='Engineer', phone='536-1057')
63 mike = Employee(name='Mike', title='Sr. Marketing Analyst', phone='536-1057')
64 dave = Employee(name='Dave', title='Sr. Engineer', phone='536-1057')
65 susan = Employee(name='Susan', title='Engineer', phone='536-1057')
66 betty = Employee(name='Betty', title='Marketing Analyst')
67
68 owner = Owner(
69 name='wile',
70 company=Company(
71 name='Acme Labs, Inc.',
72 departments=[
73 Department(name='Marketing', employees=[mike, betty]),
74 Department(name='Engineering', employees=[dave, susan, jason]),
75 ],
76 employees=[dave, susan, mike, betty, jason],
77 ),
78 )
79
80 # View for objects that aren't edited
81 no_view = View()
82
83 # Actions used by tree editor context menu
84 def_title_action = Action(name='Default title', action='object.default')
85
86 dept_action = Action(
87 name='Department',
88 action='handler.employee_department(editor,object)',
89 )
90
91 # View used by tree editor
92 employee_view = View(
93 VSplit(
94 HGroup('3', 'name'),
95 HGroup('9', 'title'),
96 HGroup('phone'),
97 id='vsplit',
98 ),
99 id='traits.doc.example.treeeditor',
100 dock='vertical',
101 )
102
103
104 class TreeHandler(Handler):
105 def employee_department(self, editor, object):
106 dept = editor.get_parent(object)
107 print(f'{object.name} works in the {dept.name} department.')
108
109
110 tree_editor = TreeEditor(
111 nodes=[
112 TreeNode(
113 node_for=[Company],
114 auto_open=True,
115 children='',
116 label='name',
117 view=View(Group('name', orientation='vertical', show_left=True)),
118 ),
119 TreeNode(
120 node_for=[Company],
121 auto_open=True,
122 children='departments',
123 label='=Departments',
124 view=no_view,
125 add=[Department],
126 ),
127 TreeNode(
128 node_for=[Company],
129 auto_open=True,
130 children='employees',
131 label='=Employees',
132 view=no_view,
133 add=[Employee],
134 ),
135 TreeNode(
136 node_for=[Department],
137 auto_open=True,
138 children='employees',
139 label='name',
140 menu=Menu(
141 NewAction,
142 Separator(),
143 DeleteAction,
144 Separator(),
145 RenameAction,
146 Separator(),
147 CopyAction,
148 CutAction,
149 PasteAction,
150 ),
151 view=View(Group('name', orientation='vertical', show_left=True)),
152 add=[Employee],
153 ),
154 TreeNode(
155 node_for=[Employee],
156 auto_open=True,
157 label='name',
158 menu=Menu(
159 NewAction,
160 Separator(),
161 def_title_action,
162 dept_action,
163 Separator(),
164 CopyAction,
165 CutAction,
166 PasteAction,
167 Separator(),
168 DeleteAction,
169 Separator(),
170 RenameAction,
171 ),
172 view=employee_view,
173 ),
174 ],
175 )
176
177 # The main view
178 view = View(
179 Group(
180 Item(name='company', id='company', editor=tree_editor, resizable=True),
181 orientation='vertical',
182 show_labels=True,
183 show_left=True,
184 ),
185 title='Company Structure',
186 id='traitsui.tests.tree_editor_test',
187 dock='horizontal',
188 drop_class=HasTraits,
189 handler=TreeHandler(),
190 buttons=['Undo', 'OK', 'Cancel'],
191 resizable=True,
192 width=0.3,
193 height=0.3,
194 )
195
196 if __name__ == '__main__':
197 owner.configure_traits(view=view)