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