Debian Codegraph @

traitsui/examples/demo/Standard_Editors/TableEditor_demo.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 """
12 **WARNING**
13
14 This demo might not work as expected and some documented features might be
15 missing.
16
17 -------------------------------------------------------------------------------
18
19 Implementation of a TableEditor demo plugin for Traits UI demo program
20
21 This demo shows the full behavior of a straightforward TableEditor. Only
22 one style of TableEditor is implemented, so that is the one shown.
23
24 Please refer to the `TableEditor API docs`_ for further information.
25
26 .. _TableEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.table_editor.html#traitsui.editors.table_editor.TableEditor
27 """
28 # Issue related to the demo warning: enthought/traitsui#948
29
30
31 from traits.api import HasTraits, HasStrictTraits, Str, Int, Regex, List
32
33 from traitsui.api import (
34 View,
35 Group,
36 Item,
37 TableEditor,
38 ObjectColumn,
39 ExpressionColumn,
40 EvalTableFilter,
41 )
42 from traitsui.table_filter import (
43 EvalFilterTemplate,
44 MenuFilterTemplate,
45 RuleFilterTemplate,
46 )
47
48
49 # A helper class for the 'Department' class below:
50 class Employee(HasTraits):
51 first_name = Str()
52 last_name = Str()
53 age = Int()
54 phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d')
55
56 traits_view = View(
57 'first_name',
58 'last_name',
59 'age',
60 'phone',
61 title='Create new employee',
62 width=0.18,
63 buttons=['OK', 'Cancel'],
64 )
65
66
67 # For readability, the TableEditor of the demo is defined here, rather than in
68 # the View:
69 table_editor = TableEditor(
70 columns=[
71 ObjectColumn(name='first_name', width=0.20),
72 ObjectColumn(name='last_name', width=0.20),
73 ExpressionColumn(
74 label='Full Name',
75 width=0.30,
76 expression="'%s %s' % (object.first_name, " "object.last_name )",
77 ),
78 ObjectColumn(name='age', width=0.10, horizontal_alignment='center'),
79 ObjectColumn(name='phone', width=0.20),
80 ],
81 deletable=True,
82 sort_model=True,
83 auto_size=False,
84 orientation='vertical',
85 edit_view=View(
86 Group('first_name', 'last_name', 'age', 'phone', show_border=True),
87 resizable=True,
88 ),
89 filters=[EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate],
90 search=EvalTableFilter(),
91 show_toolbar=True,
92 row_factory=Employee,
93 )
94
95
96 # The class to be edited with the TableEditor:
97 class Department(HasStrictTraits):
98
99 employees = List(Employee)
100
101 traits_view = View(
102 Group(
103 Item('employees', show_label=False, editor=table_editor),
104 show_border=True,
105 ),
106 title='Department Personnel',
107 width=0.4,
108 height=0.4,
109 resizable=True,
110 buttons=['OK'],
111 kind='live',
112 )
113
114
115 # Create some employees:
116 employees = [
117 Employee(first_name='Jason', last_name='Smith', age=32, phone='555-1111'),
118 Employee(first_name='Mike', last_name='Tollan', age=34, phone='555-2222'),
119 Employee(
120 first_name='Dave', last_name='Richards', age=42, phone='555-3333'
121 ),
122 Employee(first_name='Lyn', last_name='Spitz', age=40, phone='555-4444'),
123 Employee(first_name='Greg', last_name='Andrews', age=45, phone='555-5555'),
124 ]
125
126 # Create the demo:
127 demo = Department(employees=employees)
128
129 # Run the demo (if invoked from the command line):
130 if __name__ == '__main__':
131 demo.configure_traits()