Debian Codegraph @

integrationtests/ui/table_editor_test.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 # Imports:
13 # -------------------------------------------------------------------------
14
15 from traits.api import HasStrictTraits, Str, Int, Regex, List, Instance
16
17 from traitsui.api import View, Group, Item, TableEditor, EnumEditor
18
19 from traitsui.table_column import ObjectColumn
20
21 from traitsui.table_filter import (
22 TableFilter,
23 RuleTableFilter,
24 RuleFilterTemplate,
25 MenuFilterTemplate,
26 EvalFilterTemplate,
27 )
28
29 # -------------------------------------------------------------------------
30 # 'Person' class:
31 # -------------------------------------------------------------------------
32
33
34 class Person(HasStrictTraits):
35
36 # -------------------------------------------------------------------------
37 # Trait definitions:
38 # -------------------------------------------------------------------------
39
40 name = Str()
41 age = Int()
42 phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d')
43 state = Str()
44
45 # -------------------------------------------------------------------------
46 # Traits view definition:
47 # -------------------------------------------------------------------------
48
49 traits_view = View(
50 'name',
51 'age',
52 'phone',
53 'state',
54 title='Create new person',
55 width=0.18,
56 buttons=['OK', 'Cancel'],
57 )
58
59
60 # -------------------------------------------------------------------------
61 # Sample data:
62 # -------------------------------------------------------------------------
63
64 people = [
65 Person(name='Dave', age=39, phone='555-1212'),
66 Person(name='Mike', age=28, phone='555-3526'),
67 Person(name='Joe', age=34, phone='555-6943'),
68 Person(name='Tom', age=22, phone='555-7586'),
69 Person(name='Dick', age=63, phone='555-3895'),
70 Person(name='Harry', age=46, phone='555-3285'),
71 Person(name='Sally', age=43, phone='555-8797'),
72 Person(name='Fields', age=31, phone='555-3547'),
73 ]
74
75 # -------------------------------------------------------------------------
76 # 'AgeFilter' class:
77 # -------------------------------------------------------------------------
78
79
80 class AgeFilter(TableFilter):
81
82 # -------------------------------------------------------------------------
83 # Trait definitions:
84 # -------------------------------------------------------------------------
85
86 name = "Age filter"
87 _name = "Age filter"
88
89 age = Int(0)
90
91 # -------------------------------------------------------------------------
92 # Traits view definitions:
93 # -------------------------------------------------------------------------
94
95 # filter_view = Group( 'age{Age >=}' )
96
97 # -------------------------------------------------------------------------
98 # Returns whether an object passes the filter or not:
99 # -------------------------------------------------------------------------
100
101 def filter(self, person):
102 """Returns whether an object passes the filter or not."""
103 return person.age >= self.age
104
105 # -------------------------------------------------------------------------
106 # Returns a user readable description of what the filter does:
107 # -------------------------------------------------------------------------
108
109 def description(self):
110 """Returns a user readable description of what the filter does."""
111 return 'Age >= %d' % self.age
112
113 def _age_changed(self, old, new):
114 self.name = self.description()
115 print('AgeFilter _age_changed', self.name)
116
117
118 # -------------------------------------------------------------------------
119 # 'NameFilter' class:
120 # -------------------------------------------------------------------------
121
122
123 class NameFilter(TableFilter):
124
125 # -------------------------------------------------------------------------
126 # Trait definitions:
127 # -------------------------------------------------------------------------
128
129 mname = Str()
130
131 # -------------------------------------------------------------------------
132 # Traits view definitions:
133 # -------------------------------------------------------------------------
134
135 filter_view = Group('mname{Name contains}')
136
137 # -------------------------------------------------------------------------
138 # Returns whether an object passes the filter or not:
139 # -------------------------------------------------------------------------
140
141 def filter(self, person):
142 """Returns whether an object passes the filter or not."""
143 return person.name.lower().find(self.mname.lower()) >= 0
144
145 # -------------------------------------------------------------------------
146 # Returns a user readable description of what the filter does:
147 # -------------------------------------------------------------------------
148
149 def description(self):
150 """Returns a user readable description of what the filter does."""
151 return "Name contains '%s'" % self.mname
152
153
154 # -------------------------------------------------------------------------
155 # Table editor definition:
156 # -------------------------------------------------------------------------
157
158 filters = [
159 AgeFilter(age=30),
160 NameFilter(mname='d'),
161 EvalFilterTemplate,
162 MenuFilterTemplate,
163 RuleFilterTemplate,
164 ]
165
166
167 def evaluate_value(v):
168 print('evaluate_value', v)
169 return str(v)
170
171
172 # -------------------------------------------------------------------------
173 # 'TableTest' class:
174 # -------------------------------------------------------------------------
175
176
177 class TableTest(HasStrictTraits):
178
179 # -------------------------------------------------------------------------
180 # Trait definitions:
181 # -------------------------------------------------------------------------
182
183 # people = Instance( Person )
184 people = List(Person)
185
186 # -------------------------------------------------------------------------
187 # Traits view definitions:
188 # -------------------------------------------------------------------------
189
190 _valid_states = List(["AL", "AR", "AZ", "AK"])
191
192 _state_editor = EnumEditor(
193 name="_valid_states",
194 evaluate=evaluate_value,
195 object='table_editor_object',
196 )
197
198 table_editor = TableEditor(
199 columns=[
200 ObjectColumn(name='name'),
201 ObjectColumn(name='age'),
202 ObjectColumn(name='phone'),
203 ObjectColumn(name='state', editor=_state_editor),
204 ],
205 editable=True,
206 deletable=True,
207 sortable=True,
208 sort_model=True,
209 show_lines=True,
210 orientation='vertical',
211 show_column_labels=True,
212 edit_view=View(
213 ['name', 'age', 'phone', 'state', '|[]'], resizable=True
214 ),
215 filter=None,
216 filters=filters,
217 row_factory=Person,
218 )
219
220 traits_view = View(
221 [Item('people', id='people', editor=table_editor), '|[]<>'],
222 title='Table Editor Test',
223 id='traitsui.tests.table_editor_test',
224 dock='horizontal',
225 width=0.4,
226 height=0.3,
227 resizable=True,
228 kind='live',
229 )
230
231
232 # -------------------------------------------------------------------------
233 # Run the tests:
234 # -------------------------------------------------------------------------
235
236 if __name__ == '__main__':
237 tt = TableTest(people=people)
238 tt.configure_traits()
239 for p in tt.people:
240 p.print_traits()
241 print('--------------')