| 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 |
This is a modified version of the Standard_Editors/TreeEditor_demo.py in which |
| 13 |
the `Employee` class is now a subclass of HasRequiredTraits. Thus, to create a |
| 14 |
new `TreeNode` for `Employee`, to create the associated `Employee` object we |
| 15 |
can not simply do `Employee()`. Luckily the `TreeNode` API provides a means for |
| 16 |
specifying a callable that returns a created instance when we want to add a new |
| 17 |
node. To do so we can simply pass a tuple of the for (klass, prompt, factorry) |
| 18 |
as an item the `add` list trait for a TreeNode. klass is the class of object we |
| 19 |
want to be addable, prompt is a boolean indicating whether or not we want to |
| 20 |
prompt the user to specify traits after we instantiate the object before it is |
| 21 |
added to the tree, and factory is the previously described callable that must |
| 22 |
return an instance of klass. |
| 23 |
""" |
| 24 |
|
| 25 |
from traits.api import HasRequiredTraits, HasTraits, Str, Regex, List, Instance |
| 26 |
|
| 27 |
from traitsui.api import Item, View, TreeEditor, TreeNode |
| 28 |
|
| 29 |
|
| 30 |
class Employee(HasRequiredTraits): |
| 31 |
"""Defines a company employee.""" |
| 32 |
|
| 33 |
name = Str('<unknown>') |
| 34 |
title = Str() |
| 35 |
phone = Regex(regex=r'\d\d\d-\d\d\d\d') |
| 36 |
|
| 37 |
def default_title(self): |
| 38 |
self.title = 'Senior Engineer' |
| 39 |
|
| 40 |
|
| 41 |
def create_default_employee(): |
| 42 |
return Employee(name="Dilbert", title="Engineer", phone="999-8212") |
| 43 |
|
| 44 |
|
| 45 |
class Department(HasTraits): |
| 46 |
"""Defines a department with employees.""" |
| 47 |
|
| 48 |
name = Str('<unknown>') |
| 49 |
employees = List(Employee) |
| 50 |
|
| 51 |
|
| 52 |
class Company(HasTraits): |
| 53 |
"""Defines a company with departments and employees.""" |
| 54 |
|
| 55 |
name = Str('<unknown>') |
| 56 |
departments = List(Department) |
| 57 |
employees = List(Employee) |
| 58 |
|
| 59 |
|
| 60 |
# Create an empty view for objects that have no data to display: |
| 61 |
no_view = View() |
| 62 |
|
| 63 |
# Define the TreeEditor used to display the hierarchy: |
| 64 |
tree_editor = TreeEditor( |
| 65 |
nodes=[ |
| 66 |
# The first node specified is the top level one |
| 67 |
TreeNode( |
| 68 |
node_for=[Company], |
| 69 |
auto_open=True, |
| 70 |
# child nodes are |
| 71 |
children='', |
| 72 |
label='name', # label with Company name |
| 73 |
view=View(['name']), |
| 74 |
), |
| 75 |
TreeNode( |
| 76 |
node_for=[Company], |
| 77 |
auto_open=True, |
| 78 |
children='departments', |
| 79 |
label='=Departments', # constant label |
| 80 |
view=no_view, |
| 81 |
add=[Department], |
| 82 |
), |
| 83 |
TreeNode( |
| 84 |
node_for=[Company], |
| 85 |
auto_open=True, |
| 86 |
children='employees', |
| 87 |
label='=Employees', # constant label |
| 88 |
view=no_view, |
| 89 |
add=[(Employee, True, create_default_employee)], |
| 90 |
), |
| 91 |
TreeNode( |
| 92 |
node_for=[Department], |
| 93 |
auto_open=True, |
| 94 |
children='employees', |
| 95 |
label='name', # label with Department name |
| 96 |
view=View(['name']), |
| 97 |
add=[(Employee, True, create_default_employee)], |
| 98 |
), |
| 99 |
TreeNode( |
| 100 |
node_for=[Employee], |
| 101 |
auto_open=True, |
| 102 |
label='name', # label with Employee name |
| 103 |
view=View(['name', 'title', 'phone']), |
| 104 |
), |
| 105 |
] |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
class Partner(HasTraits): |
| 110 |
"""Defines a business partner.""" |
| 111 |
|
| 112 |
name = Str('<unknown>') |
| 113 |
company = Instance(Company) |
| 114 |
|
| 115 |
traits_view = View( |
| 116 |
Item(name='company', editor=tree_editor, show_label=False), |
| 117 |
title='Company Structure', |
| 118 |
buttons=['OK'], |
| 119 |
resizable=True, |
| 120 |
style='custom', |
| 121 |
width=0.3, |
| 122 |
height=500, |
| 123 |
) |
| 124 |
|
| 125 |
|
| 126 |
# Create an example data structure: |
| 127 |
jason = Employee(name='Jason', title='Senior Engineer', phone='536-1057') |
| 128 |
mike = Employee(name='Mike', title='Senior Engineer', phone='536-1057') |
| 129 |
dave = Employee(name='Dave', title='Senior Developer', phone='536-1057') |
| 130 |
martin = Employee(name='Martin', title='Senior Engineer', phone='536-1057') |
| 131 |
duncan = Employee(name='Duncan', title='Consultant', phone='526-1057') |
| 132 |
|
| 133 |
# Create the demo: |
| 134 |
demo = Partner( |
| 135 |
name='Enthought, Inc.', |
| 136 |
company=Company( |
| 137 |
name='Enthought', |
| 138 |
employees=[dave, martin, duncan, jason, mike], |
| 139 |
departments=[ |
| 140 |
Department(name='Business', employees=[jason, mike]), |
| 141 |
Department(name='Scientific', employees=[dave, martin, duncan]), |
| 142 |
], |
| 143 |
), |
| 144 |
) |
| 145 |
|
| 146 |
# Run the demo (if invoked from the command line): |
| 147 |
if __name__ == '__main__': |
| 148 |
demo.configure_traits() |