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