| 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 |
Auto-update from a list to a TabularEditor |
| 20 |
|
| 21 |
Demonstrates using a TabularEditor with the 'auto_update' feature enabled, |
| 22 |
which allows the tabular editor to automatically update itself when the content |
| 23 |
of any object in the list associated with the editor is modified. |
| 24 |
|
| 25 |
To interact with the demo: |
| 26 |
|
| 27 |
- Select an employee from the list. |
| 28 |
- Adjust their salary increase. |
| 29 |
- Click the **Give raise** button. |
| 30 |
- Observe that the table automatically updates to reflect the employees new |
| 31 |
salary. |
| 32 |
|
| 33 |
In order for auto-update to work correctly, the editor trait should be a list |
| 34 |
of objects derived from HasTraits. Also, performance can be affected when very |
| 35 |
long lists are used, since enabling this feature adds and removed Traits |
| 36 |
listeners to each item in the list. |
| 37 |
|
| 38 |
""" |
| 39 |
# Issues related to the demo warning: |
| 40 |
# enthought/traitsui#960 |
| 41 |
|
| 42 |
from traits.api import HasTraits, Str, Float, List, Instance, Button |
| 43 |
from traitsui.api import ( |
| 44 |
View, |
| 45 |
HGroup, |
| 46 |
Item, |
| 47 |
TabularAdapter, |
| 48 |
TabularEditor, |
| 49 |
spring, |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
# -- EmployeeAdapter Class ------------------------------------------------ |
| 54 |
class EmployeeAdapter(TabularAdapter): |
| 55 |
|
| 56 |
columns = [('Name', 'name'), ('Salary', 'salary')] |
| 57 |
|
| 58 |
def get_default_value(self, object, trait): |
| 59 |
return Employee(name="John", salary=30000) |
| 60 |
|
| 61 |
|
| 62 |
# -- Employee Class ------------------------------------------------------- |
| 63 |
class Employee(HasTraits): |
| 64 |
|
| 65 |
name = Str() |
| 66 |
salary = Float() |
| 67 |
|
| 68 |
|
| 69 |
# -- Company Class -------------------------------------------------------- |
| 70 |
class Company(HasTraits): |
| 71 |
|
| 72 |
employees = List(Employee) |
| 73 |
employee = Instance(Employee) |
| 74 |
increase = Float() |
| 75 |
give_raise = Button('Give raise') |
| 76 |
|
| 77 |
traits_view = View( |
| 78 |
Item( |
| 79 |
'employees', |
| 80 |
show_label=False, |
| 81 |
editor=TabularEditor( |
| 82 |
adapter=EmployeeAdapter(), |
| 83 |
selected='employee', |
| 84 |
auto_resize=True, |
| 85 |
auto_update=True, |
| 86 |
), |
| 87 |
), |
| 88 |
HGroup( |
| 89 |
spring, |
| 90 |
Item('increase'), |
| 91 |
Item( |
| 92 |
'give_raise', |
| 93 |
show_label=False, |
| 94 |
enabled_when='employee is not None', |
| 95 |
), |
| 96 |
), |
| 97 |
title='Auto Update Tabular Editor demo', |
| 98 |
height=0.25, |
| 99 |
width=0.30, |
| 100 |
resizable=True, |
| 101 |
) |
| 102 |
|
| 103 |
def _give_raise_changed(self): |
| 104 |
self.employee.salary += self.increase |
| 105 |
self.employee = None |
| 106 |
|
| 107 |
|
| 108 |
# -- Set up the demo ------------------------------------------------------ |
| 109 |
demo = Company( |
| 110 |
increase=1000, |
| 111 |
employees=[ |
| 112 |
Employee(name='Fred', salary=45000), |
| 113 |
Employee(name='Sally', salary=52000), |
| 114 |
Employee(name='Jim', salary=39000), |
| 115 |
Employee(name='Helen', salary=41000), |
| 116 |
Employee(name='George', salary=49000), |
| 117 |
Employee(name='Betty', salary=46000), |
| 118 |
], |
| 119 |
) |
| 120 |
|
| 121 |
# Run the demo (if invoked from the command line): |
| 122 |
if __name__ == '__main__': |
| 123 |
demo.configure_traits() |