| 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 |
Defining column-specific context menu in a Tabular Editor. Shows how the |
| 20 |
example for the Table Editor (`Table_Editor_with_context_menu_demo.py`) can be |
| 21 |
adapted to work with a Tabular Editor. |
| 22 |
|
| 23 |
The demo is a simple baseball scoring system, which lists each player and |
| 24 |
their current batting statistics. After a given player has an at bat, you |
| 25 |
right-click on the table cell corresponding to the player and the result of |
| 26 |
the at-bat (e.g. 'S' = single) and select the 'Add' menu option to register |
| 27 |
that the player hit a single and update the player's overall statistics. |
| 28 |
|
| 29 |
This demo also illustrates the use of Property traits, and how using 'event' |
| 30 |
meta-data can simplify event handling by collapsing an event that can |
| 31 |
occur on a number of traits into a category of event, which can be handled by |
| 32 |
a single event handler defined for the category (in this case, the category |
| 33 |
is 'affects_average'). |
| 34 |
""" |
| 35 |
# Issue related to the demo warning: enthought/traitsui#960 |
| 36 |
|
| 37 |
|
| 38 |
from random import randint |
| 39 |
from traits.api import HasStrictTraits, Str, Int, Float, List, Property |
| 40 |
from traitsui.api import ( |
| 41 |
Action, |
| 42 |
Item, |
| 43 |
Menu, |
| 44 |
TabularAdapter, |
| 45 |
TabularEditor, |
| 46 |
View, |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
# Define a custom tabular adapter for handling items which affect the player's |
| 51 |
# batting average: |
| 52 |
class PlayerAdapter(TabularAdapter): |
| 53 |
|
| 54 |
# Overwrite default values |
| 55 |
alignment = 'center' |
| 56 |
width = 0.09 |
| 57 |
|
| 58 |
def get_menu(self, object, trait, row, column): |
| 59 |
column_name = self.column_map[column] |
| 60 |
if column_name not in ['name', 'average']: |
| 61 |
menu = Menu( |
| 62 |
Action(name='Add', action='editor.adapter.add(item, column)'), |
| 63 |
Action(name='Sub', action='editor.adapter.sub(item, column)'), |
| 64 |
) |
| 65 |
return menu |
| 66 |
else: |
| 67 |
return super().get_menu(object, trait, row, column) |
| 68 |
|
| 69 |
def get_format(self, object, trait, row, column): |
| 70 |
column_name = self.column_map[column] |
| 71 |
if column_name == 'average': |
| 72 |
return '%0.3f' |
| 73 |
else: |
| 74 |
return super().get_format(object, trait, row, column) |
| 75 |
|
| 76 |
def add(self, object, column): |
| 77 |
"""Increment the affected player statistic.""" |
| 78 |
column_name = self.column_map[column] |
| 79 |
setattr(object, column_name, getattr(object, column_name) + 1) |
| 80 |
|
| 81 |
def sub(self, object, column): |
| 82 |
"""Decrement the affected player statistic.""" |
| 83 |
column_name = self.column_map[column] |
| 84 |
setattr(object, column_name, getattr(object, column_name) - 1) |
| 85 |
|
| 86 |
|
| 87 |
# The 'players' trait table editor: |
| 88 |
columns = [ |
| 89 |
('Player Name', 'name'), |
| 90 |
('AB', 'at_bats'), |
| 91 |
('SO', 'strike_outs'), |
| 92 |
('S', 'singles'), |
| 93 |
('D', 'doubles'), |
| 94 |
('T', 'triples'), |
| 95 |
('HR', 'home_runs'), |
| 96 |
('W', 'walks'), |
| 97 |
('Ave', 'average'), |
| 98 |
] |
| 99 |
|
| 100 |
player_editor = TabularEditor( |
| 101 |
editable=True, |
| 102 |
auto_resize=True, |
| 103 |
auto_resize_rows=True, |
| 104 |
stretch_last_section=False, |
| 105 |
auto_update=True, |
| 106 |
adapter=PlayerAdapter(columns=columns), |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
# 'Player' class: |
| 111 |
class Player(HasStrictTraits): |
| 112 |
|
| 113 |
# Trait definitions: |
| 114 |
name = Str() |
| 115 |
at_bats = Int() |
| 116 |
strike_outs = Int(event='affects_average') |
| 117 |
singles = Int(event='affects_average') |
| 118 |
doubles = Int(event='affects_average') |
| 119 |
triples = Int(event='affects_average') |
| 120 |
home_runs = Int(event='affects_average') |
| 121 |
walks = Int() |
| 122 |
average = Property(Float) |
| 123 |
|
| 124 |
def _get_average(self): |
| 125 |
"""Computes the player's batting average from the current statistics.""" |
| 126 |
if self.at_bats == 0: |
| 127 |
return 0.0 |
| 128 |
|
| 129 |
return ( |
| 130 |
float(self.singles + self.doubles + self.triples + self.home_runs) |
| 131 |
/ self.at_bats |
| 132 |
) |
| 133 |
|
| 134 |
def _affects_average_changed(self): |
| 135 |
"""Handles an event that affects the player's batting average.""" |
| 136 |
self.at_bats += 1 |
| 137 |
|
| 138 |
|
| 139 |
class Team(HasStrictTraits): |
| 140 |
|
| 141 |
# Trait definitions: |
| 142 |
players = List(Player) |
| 143 |
|
| 144 |
# Trait view definitions: |
| 145 |
traits_view = View( |
| 146 |
Item('players', show_label=False, editor=player_editor), |
| 147 |
title='Baseball Scoring Demo', |
| 148 |
width=0.5, |
| 149 |
height=0.5, |
| 150 |
resizable=True, |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
def random_player(name): |
| 155 |
"""Generates and returns a random player.""" |
| 156 |
p = Player( |
| 157 |
name=name, |
| 158 |
strike_outs=randint(0, 50), |
| 159 |
singles=randint(0, 50), |
| 160 |
doubles=randint(0, 20), |
| 161 |
triples=randint(0, 5), |
| 162 |
home_runs=randint(0, 30), |
| 163 |
walks=randint(0, 50), |
| 164 |
) |
| 165 |
return p.trait_set( |
| 166 |
at_bats=( |
| 167 |
p.strike_outs |
| 168 |
+ p.singles |
| 169 |
+ p.doubles |
| 170 |
+ p.triples |
| 171 |
+ p.home_runs |
| 172 |
+ randint(100, 200) |
| 173 |
) |
| 174 |
) |
| 175 |
|
| 176 |
|
| 177 |
# Create the demo: |
| 178 |
demo = Team( |
| 179 |
players=[ |
| 180 |
random_player(name) |
| 181 |
for name in [ |
| 182 |
'Dave', |
| 183 |
'Mike', |
| 184 |
'Joe', |
| 185 |
'Tom', |
| 186 |
'Dick', |
| 187 |
'Harry', |
| 188 |
'Dirk', |
| 189 |
'Fields', |
| 190 |
'Stretch', |
| 191 |
] |
| 192 |
] |
| 193 |
) |
| 194 |
|
| 195 |
# Run the demo (if invoked from the command line): |
| 196 |
if __name__ == '__main__': |
| 197 |
demo.configure_traits() |