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