| 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 |
This shows the three different types of editor that can be applied to a list |
| 20 |
of objects: |
| 21 |
|
| 22 |
- Table |
| 23 |
- List |
| 24 |
- Dockable notebook (a list variant) |
| 25 |
|
| 26 |
Each editor style is editing the exact same list of objects. Note that any |
| 27 |
changes made in one editor are automatically reflected in the others. |
| 28 |
""" |
| 29 |
# Issue related to the demo warning: enthought/traitsui#948 |
| 30 |
|
| 31 |
from traits.api import HasStrictTraits, Str, Int, Regex, List, Instance |
| 32 |
|
| 33 |
from traitsui.api import ( |
| 34 |
Item, |
| 35 |
ListEditor, |
| 36 |
ObjectColumn, |
| 37 |
RuleTableFilter, |
| 38 |
Tabbed, |
| 39 |
TableEditor, |
| 40 |
View, |
| 41 |
) |
| 42 |
|
| 43 |
from traitsui.table_filter import ( |
| 44 |
RuleFilterTemplate, |
| 45 |
MenuFilterTemplate, |
| 46 |
EvalFilterTemplate, |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
# 'Person' class: |
| 51 |
class Person(HasStrictTraits): |
| 52 |
|
| 53 |
# Trait definitions: |
| 54 |
name = Str() |
| 55 |
age = Int() |
| 56 |
phone = Regex(value='000-0000', regex=r'\d\d\d[-]\d\d\d\d') |
| 57 |
|
| 58 |
# Traits view definition: |
| 59 |
traits_view = View( |
| 60 |
'name', 'age', 'phone', width=0.18, buttons=['OK', 'Cancel'] |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
# Sample data: |
| 65 |
people = [ |
| 66 |
Person(name='Dave', age=39, phone='555-1212'), |
| 67 |
Person(name='Mike', age=28, phone='555-3526'), |
| 68 |
Person(name='Joe', age=34, phone='555-6943'), |
| 69 |
Person(name='Tom', age=22, phone='555-7586'), |
| 70 |
Person(name='Dick', age=63, phone='555-3895'), |
| 71 |
Person(name='Harry', age=46, phone='555-3285'), |
| 72 |
Person(name='Sally', age=43, phone='555-8797'), |
| 73 |
Person(name='Fields', age=31, phone='555-3547'), |
| 74 |
] |
| 75 |
|
| 76 |
# Table editor definition: |
| 77 |
filters = [EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate] |
| 78 |
|
| 79 |
table_editor = TableEditor( |
| 80 |
columns=[ |
| 81 |
ObjectColumn(name='name', width=0.4), |
| 82 |
ObjectColumn(name='age', width=0.2), |
| 83 |
ObjectColumn(name='phone', width=0.4), |
| 84 |
], |
| 85 |
editable=True, |
| 86 |
deletable=True, |
| 87 |
sortable=True, |
| 88 |
sort_model=True, |
| 89 |
auto_size=False, |
| 90 |
filters=filters, |
| 91 |
search=RuleTableFilter(), |
| 92 |
row_factory=Person, |
| 93 |
show_toolbar=True, |
| 94 |
) |
| 95 |
|
| 96 |
|
| 97 |
# 'ListTraitTest' class: |
| 98 |
class ListTraitTest(HasStrictTraits): |
| 99 |
|
| 100 |
# Trait definitions: |
| 101 |
people = List(Instance(Person, ())) |
| 102 |
|
| 103 |
# Traits view definitions: |
| 104 |
traits_view = View( |
| 105 |
Tabbed( |
| 106 |
Item('people', label='Table', id='table', editor=table_editor), |
| 107 |
Item( |
| 108 |
'people', |
| 109 |
label='List', |
| 110 |
id='list', |
| 111 |
style='custom', |
| 112 |
editor=ListEditor(style='custom', rows=5), |
| 113 |
), |
| 114 |
Item( |
| 115 |
'people', |
| 116 |
label='Notebook', |
| 117 |
id='notebook', |
| 118 |
style='custom', |
| 119 |
editor=ListEditor( |
| 120 |
use_notebook=True, |
| 121 |
deletable=True, |
| 122 |
export='DockShellWindow', |
| 123 |
page_name='.name', |
| 124 |
), |
| 125 |
), |
| 126 |
id='splitter', |
| 127 |
show_labels=False, |
| 128 |
), |
| 129 |
id='traitsui.demo.Traits UI Demo.Advanced.List_editors_demo', |
| 130 |
dock='horizontal', |
| 131 |
width=600, |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
# Create the demo: |
| 136 |
demo = ListTraitTest(people=people) |
| 137 |
|
| 138 |
# Run the demo (if invoked from the command line): |
| 139 |
if __name__ == '__main__': |
| 140 |
demo.configure_traits() |