| 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, Item, VSplit, TableEditor, ListEditor |
| 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 |
|
| 44 |
# ------------------------------------------------------------------------- |
| 45 |
# Traits view definition: |
| 46 |
# ------------------------------------------------------------------------- |
| 47 |
|
| 48 |
traits_view = View( |
| 49 |
'name', 'age', 'phone', width=0.18, buttons=['OK', 'Cancel'] |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
# ------------------------------------------------------------------------- |
| 54 |
# Sample data: |
| 55 |
# ------------------------------------------------------------------------- |
| 56 |
|
| 57 |
people = [ |
| 58 |
Person(name='Dave', age=39, phone='555-1212'), |
| 59 |
Person(name='Mike', age=28, phone='555-3526'), |
| 60 |
Person(name='Joe', age=34, phone='555-6943'), |
| 61 |
Person(name='Tom', age=22, phone='555-7586'), |
| 62 |
Person(name='Dick', age=63, phone='555-3895'), |
| 63 |
Person(name='Harry', age=46, phone='555-3285'), |
| 64 |
Person(name='Sally', age=43, phone='555-8797'), |
| 65 |
Person(name='Fields', age=31, phone='555-3547'), |
| 66 |
] |
| 67 |
|
| 68 |
# ------------------------------------------------------------------------- |
| 69 |
# Table editor definition: |
| 70 |
# ------------------------------------------------------------------------- |
| 71 |
|
| 72 |
filters = [EvalFilterTemplate, MenuFilterTemplate, RuleFilterTemplate] |
| 73 |
|
| 74 |
table_editor = TableEditor( |
| 75 |
columns=[ |
| 76 |
ObjectColumn(name='name'), |
| 77 |
ObjectColumn(name='age'), |
| 78 |
ObjectColumn(name='phone'), |
| 79 |
], |
| 80 |
editable=True, |
| 81 |
deletable=True, |
| 82 |
sortable=True, |
| 83 |
sort_model=True, |
| 84 |
filters=filters, |
| 85 |
search=RuleTableFilter(), |
| 86 |
row_factory=Person, |
| 87 |
) |
| 88 |
|
| 89 |
# ------------------------------------------------------------------------- |
| 90 |
# 'ListTraitTest' class: |
| 91 |
# ------------------------------------------------------------------------- |
| 92 |
|
| 93 |
|
| 94 |
class ListTraitTest(HasStrictTraits): |
| 95 |
|
| 96 |
# ------------------------------------------------------------------------- |
| 97 |
# Trait definitions: |
| 98 |
# ------------------------------------------------------------------------- |
| 99 |
|
| 100 |
people = List(Person) |
| 101 |
|
| 102 |
# ------------------------------------------------------------------------- |
| 103 |
# Traits view definitions: |
| 104 |
# ------------------------------------------------------------------------- |
| 105 |
|
| 106 |
traits_view = View( |
| 107 |
VSplit( |
| 108 |
Item('people', id='table', editor=table_editor), |
| 109 |
Item( |
| 110 |
'people@', id='list', editor=ListEditor(style='custom', rows=5) |
| 111 |
), |
| 112 |
Item( |
| 113 |
'people@', |
| 114 |
id='notebook', |
| 115 |
editor=ListEditor( |
| 116 |
use_notebook=True, |
| 117 |
deletable=True, |
| 118 |
export='DockShellWindow', |
| 119 |
dock_style='tab', |
| 120 |
page_name='.name', |
| 121 |
), |
| 122 |
), |
| 123 |
id='splitter', |
| 124 |
show_labels=False, |
| 125 |
), |
| 126 |
title='List Trait Editor Test', |
| 127 |
id='traitsui.tests.list_traits_ui_test', |
| 128 |
dock='horizontal', |
| 129 |
width=0.4, |
| 130 |
height=0.6, |
| 131 |
resizable=True, |
| 132 |
kind='live', |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
# ------------------------------------------------------------------------- |
| 137 |
# Run the tests: |
| 138 |
# ------------------------------------------------------------------------- |
| 139 |
|
| 140 |
if __name__ == '__main__': |
| 141 |
ListTraitTest(people=people).configure_traits() |