| 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 |
# Imports: |
| 13 |
# ------------------------------------------------------------------------- |
| 14 |
|
| 15 |
from traits.api import HasTraits, Str, Regex, List, Instance |
| 16 |
|
| 17 |
from traitsui.api import ( |
| 18 |
TreeEditor, |
| 19 |
TreeNode, |
| 20 |
View, |
| 21 |
Group, |
| 22 |
Item, |
| 23 |
Handler, |
| 24 |
InstanceEditor, |
| 25 |
) |
| 26 |
|
| 27 |
from traitsui.instance_choice import InstanceDropChoice |
| 28 |
|
| 29 |
from traitsui.menu import Menu, Action, Separator |
| 30 |
|
| 31 |
from traitsui.editors.tree_editor import ( |
| 32 |
NewAction, |
| 33 |
CopyAction, |
| 34 |
CutAction, |
| 35 |
PasteAction, |
| 36 |
DeleteAction, |
| 37 |
RenameAction, |
| 38 |
) |
| 39 |
|
| 40 |
# ------------------------------------------------------------------------- |
| 41 |
# 'Employee' class: |
| 42 |
# ------------------------------------------------------------------------- |
| 43 |
|
| 44 |
|
| 45 |
class Employee(HasTraits): |
| 46 |
name = Str('<unknown>') |
| 47 |
title = Str() |
| 48 |
phone = Regex(regex=r'\d\d\d-\d\d\d\d') |
| 49 |
|
| 50 |
view = View('title', 'phone') |
| 51 |
|
| 52 |
def default_title(self): |
| 53 |
self.title = 'Senior Engineer' |
| 54 |
|
| 55 |
|
| 56 |
# ------------------------------------------------------------------------- |
| 57 |
# 'Department' class: |
| 58 |
# ------------------------------------------------------------------------- |
| 59 |
|
| 60 |
|
| 61 |
class Department(HasTraits): |
| 62 |
name = Str('<unknown>') |
| 63 |
employees = List(Employee) |
| 64 |
|
| 65 |
view = View(['employees', '|<>']) |
| 66 |
|
| 67 |
|
| 68 |
# ------------------------------------------------------------------------- |
| 69 |
# 'Company' class: |
| 70 |
# ------------------------------------------------------------------------- |
| 71 |
|
| 72 |
|
| 73 |
class Company(HasTraits): |
| 74 |
name = Str('<unknown>') |
| 75 |
departments = List(Department) |
| 76 |
employees = List(Employee) |
| 77 |
|
| 78 |
|
| 79 |
# ------------------------------------------------------------------------- |
| 80 |
# 'Partner' class: |
| 81 |
# ------------------------------------------------------------------------- |
| 82 |
|
| 83 |
|
| 84 |
class Partner(HasTraits): |
| 85 |
name = Str('<unknown>') |
| 86 |
company = Instance(Company) |
| 87 |
eom = Instance(Employee) |
| 88 |
dom = Instance(Department) |
| 89 |
|
| 90 |
|
| 91 |
# ------------------------------------------------------------------------- |
| 92 |
# Create a hierarchy: |
| 93 |
# ------------------------------------------------------------------------- |
| 94 |
|
| 95 |
jason = Employee(name='Jason', title='Sr. Engineer', phone='536-1057') |
| 96 |
|
| 97 |
mike = Employee(name='Mike', title='Sr. Engineer', phone='536-1057') |
| 98 |
|
| 99 |
dave = Employee(name='Dave', title='Sr. Engineer', phone='536-1057') |
| 100 |
|
| 101 |
martin = Employee(name='Martin', title='Sr. Engineer', phone='536-1057') |
| 102 |
|
| 103 |
duncan = Employee(name='Duncan', title='Sr. Engineer') |
| 104 |
|
| 105 |
partner = Partner( |
| 106 |
name='eric', |
| 107 |
company=Company( |
| 108 |
name='Enthought, Inc.', |
| 109 |
departments=[ |
| 110 |
Department(name='Business', employees=[jason, mike]), |
| 111 |
Department(name='Scientific', employees=[dave, martin, duncan]), |
| 112 |
], |
| 113 |
employees=[dave, martin, mike, duncan, jason], |
| 114 |
), |
| 115 |
) |
| 116 |
|
| 117 |
# ------------------------------------------------------------------------- |
| 118 |
# Define the tree trait editor: |
| 119 |
# ------------------------------------------------------------------------- |
| 120 |
|
| 121 |
no_view = View() |
| 122 |
|
| 123 |
tree_editor = TreeEditor( |
| 124 |
editable=False, |
| 125 |
nodes=[ |
| 126 |
TreeNode( |
| 127 |
node_for=[Company], |
| 128 |
auto_open=True, |
| 129 |
children='', |
| 130 |
label='name', |
| 131 |
view=View(['name', '|<']), |
| 132 |
), |
| 133 |
TreeNode( |
| 134 |
node_for=[Company], |
| 135 |
auto_open=True, |
| 136 |
children='departments', |
| 137 |
label='=Departments', |
| 138 |
view=no_view, |
| 139 |
add=[Department], |
| 140 |
), |
| 141 |
TreeNode( |
| 142 |
node_for=[Company], |
| 143 |
auto_open=True, |
| 144 |
children='employees', |
| 145 |
label='=Employees', |
| 146 |
view=no_view, |
| 147 |
add=[Employee], |
| 148 |
), |
| 149 |
TreeNode( |
| 150 |
node_for=[Department], |
| 151 |
auto_open=True, |
| 152 |
children='employees', |
| 153 |
label='name', |
| 154 |
menu=Menu( |
| 155 |
NewAction, |
| 156 |
Separator(), |
| 157 |
DeleteAction, |
| 158 |
Separator(), |
| 159 |
RenameAction, |
| 160 |
Separator(), |
| 161 |
CopyAction, |
| 162 |
CutAction, |
| 163 |
PasteAction, |
| 164 |
), |
| 165 |
view=View(['name', '|<']), |
| 166 |
add=[Employee], |
| 167 |
), |
| 168 |
TreeNode( |
| 169 |
node_for=[Employee], |
| 170 |
auto_open=True, |
| 171 |
label='name', |
| 172 |
menu=Menu( |
| 173 |
NewAction, |
| 174 |
Separator(), |
| 175 |
Action(name='Default title', action='object.default_title'), |
| 176 |
Action( |
| 177 |
name='Department', |
| 178 |
action='handler.employee_department(editor,object)', |
| 179 |
), |
| 180 |
Separator(), |
| 181 |
CopyAction, |
| 182 |
CutAction, |
| 183 |
PasteAction, |
| 184 |
Separator(), |
| 185 |
DeleteAction, |
| 186 |
Separator(), |
| 187 |
RenameAction, |
| 188 |
), |
| 189 |
view=View(['name', 'title', 'phone', '|<']), |
| 190 |
), |
| 191 |
], |
| 192 |
) |
| 193 |
|
| 194 |
# ------------------------------------------------------------------------- |
| 195 |
# 'TreeHandler' class: |
| 196 |
# ------------------------------------------------------------------------- |
| 197 |
|
| 198 |
|
| 199 |
class TreeHandler(Handler): |
| 200 |
def employee_department(self, editor, object): |
| 201 |
dept = editor.get_parent(object) |
| 202 |
print('%s works in the %s department.' % (object.name, dept.name)) |
| 203 |
|
| 204 |
|
| 205 |
# ------------------------------------------------------------------------- |
| 206 |
# Define the View to use: |
| 207 |
# ------------------------------------------------------------------------- |
| 208 |
|
| 209 |
view = View( |
| 210 |
Group( |
| 211 |
[Item('company', editor=tree_editor, resizable=True), '|<>'], |
| 212 |
Group( |
| 213 |
[ |
| 214 |
'{Employee of the Month}@', |
| 215 |
Item( |
| 216 |
'eom@', |
| 217 |
editor=InstanceEditor( |
| 218 |
values=[ |
| 219 |
InstanceDropChoice(klass=Employee, selectable=True) |
| 220 |
] |
| 221 |
), |
| 222 |
resizable=True, |
| 223 |
), |
| 224 |
'|<>', |
| 225 |
], |
| 226 |
[ |
| 227 |
'{Department of the Month}@', |
| 228 |
Item( |
| 229 |
'dom@', |
| 230 |
editor=InstanceEditor( |
| 231 |
values=[InstanceDropChoice(klass=Department)] |
| 232 |
), |
| 233 |
resizable=True, |
| 234 |
), |
| 235 |
'|<>', |
| 236 |
], |
| 237 |
show_labels=False, |
| 238 |
layout='split', |
| 239 |
), |
| 240 |
orientation='horizontal', |
| 241 |
show_labels=False, |
| 242 |
layout='split', |
| 243 |
), |
| 244 |
title='Company Structure', |
| 245 |
handler=TreeHandler(), |
| 246 |
buttons=['OK', 'Cancel'], |
| 247 |
resizable=True, |
| 248 |
width=0.5, |
| 249 |
height=0.5, |
| 250 |
) |
| 251 |
|
| 252 |
# ------------------------------------------------------------------------- |
| 253 |
# Edit it: |
| 254 |
# ------------------------------------------------------------------------- |
| 255 |
|
| 256 |
if __name__ == '__main__': |
| 257 |
partner.configure_traits(view=view) |