| 1 |
# (C) Copyright 2005-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 |
# --(Extended Property depends_on References)---------------------------------- |
| 12 |
""" |
| 13 |
Extended Property *depends_on* References |
| 14 |
========================================= |
| 15 |
|
| 16 |
In Traits 3.0, the **Property** *depends_on* metadata has been extended to |
| 17 |
take advantage of the new extended trait name support offered by the |
| 18 |
*on_trait_change* method. |
| 19 |
|
| 20 |
Previously, the *depends_on* metadata for a *Property* was restricted to |
| 21 |
referencing traits defined either on the same object as the **Property**, or |
| 22 |
on an object immediately reachable from the object. For example:: |
| 23 |
|
| 24 |
class Wheel(Part): |
| 25 |
|
| 26 |
axel = Instance(Axel) |
| 27 |
position = Property(depends_on = 'axel.position') |
| 28 |
|
| 29 |
... |
| 30 |
|
| 31 |
Starting with Traits 3.0, the *depends_on* metadata may now include any |
| 32 |
extended trait reference that is allowed by the enhanced *on_trait_change* |
| 33 |
method. So, for example it is now legal to write things like:: |
| 34 |
|
| 35 |
class Wheel(Part): |
| 36 |
|
| 37 |
axel = Instance(Axel) |
| 38 |
position = Property(depends_on = 'axel.chassis.position') |
| 39 |
|
| 40 |
or:: |
| 41 |
|
| 42 |
class Child(Person): |
| 43 |
|
| 44 |
mother = Instance(Person) |
| 45 |
father = Instance(Person) |
| 46 |
mood = Property(depends_on = ['mother.+mood_affecting', |
| 47 |
'father.+mood_affecting']) |
| 48 |
|
| 49 |
In particular, in the last example we are declaring that the **Child** class's |
| 50 |
*mood* property depends upon the values of any of either its mother or |
| 51 |
father object's traits that have *mood_affecting* metadata defined. |
| 52 |
|
| 53 |
Thus, a **Child** object's *mood* property will fire a trait change |
| 54 |
notification whenever any of the its mother's or father's mood affecting |
| 55 |
traits change. |
| 56 |
|
| 57 |
Refer also to the code tabs for this lesson for a complete example using a |
| 58 |
**Property** definition using *depends_on* metadata containing an extended |
| 59 |
trait reference. In particular, take a look at the **LeagueModelView Class** |
| 60 |
tab's *total_hits* trait definition. |
| 61 |
""" |
| 62 |
|
| 63 |
# FIXME redo example without traitsui |
| 64 |
|
| 65 |
from traits.api import ( |
| 66 |
Button, cached_property, HasTraits, Instance, Int, List, Property, Str) |
| 67 |
|
| 68 |
from traitsui.api import ( |
| 69 |
HGroup, Item, ModelView, ObjectColumn, TableEditor, VGroup, View) |
| 70 |
|
| 71 |
|
| 72 |
# --[Player Class]------------------------------------------------------------- |
| 73 |
# Define a baseball player: |
| 74 |
class Player(HasTraits): |
| 75 |
|
| 76 |
# The name of the player: |
| 77 |
name = Str("<new player>") |
| 78 |
|
| 79 |
# The number of hits the player made this season: |
| 80 |
hits = Int |
| 81 |
|
| 82 |
|
| 83 |
# --[Team Class]--------------------------------------------------------------- |
| 84 |
# Define a baseball team: |
| 85 |
class Team(HasTraits): |
| 86 |
|
| 87 |
# The name of the team: |
| 88 |
name = Str("<new team>") |
| 89 |
|
| 90 |
# The players on the team: |
| 91 |
players = List(Player) |
| 92 |
|
| 93 |
# The number of players on the team: |
| 94 |
num_players = Property(depends_on="players") |
| 95 |
|
| 96 |
def _get_num_players(self): |
| 97 |
""" Implementation of the 'num_players' property. |
| 98 |
""" |
| 99 |
return len(self.players) |
| 100 |
|
| 101 |
|
| 102 |
# --[League Class]------------------------------------------------------------- |
| 103 |
# Define a baseball league model: |
| 104 |
class League(HasTraits): |
| 105 |
|
| 106 |
# The name of the league: |
| 107 |
name = Str("<new league>") |
| 108 |
|
| 109 |
# The teams in the league: |
| 110 |
teams = List(Team) |
| 111 |
|
| 112 |
|
| 113 |
# --[LeagueModelView Class]---------------------------------------------------- |
| 114 |
# Define a ModelView for a League model: |
| 115 |
class LeagueModelView(ModelView): |
| 116 |
|
| 117 |
# The currently selected team: |
| 118 |
team = Instance(Team) |
| 119 |
|
| 120 |
# The currently selected player: |
| 121 |
player = Instance(Player) |
| 122 |
|
| 123 |
# Button to add a hit to the current player: |
| 124 |
got_hit = Button("Got a Hit") |
| 125 |
|
| 126 |
# The total number of hits (note the 'depends_on' extended trait |
| 127 |
# reference): |
| 128 |
total_hits = Property(depends_on="model.teams.players.hits") |
| 129 |
|
| 130 |
@cached_property |
| 131 |
def _get_total_hits(self): |
| 132 |
""" Returns the total number of hits across all teams and players. |
| 133 |
""" |
| 134 |
return sum( |
| 135 |
sum(p.hits for p in t.players) |
| 136 |
for t in self.model.teams |
| 137 |
) |
| 138 |
|
| 139 |
view = View( |
| 140 |
VGroup( |
| 141 |
HGroup( |
| 142 |
Item("total_hits", style="readonly"), |
| 143 |
label="League Statistics", |
| 144 |
show_border=True, |
| 145 |
), |
| 146 |
VGroup( |
| 147 |
Item( |
| 148 |
"model.teams", |
| 149 |
show_label=False, |
| 150 |
editor=TableEditor( |
| 151 |
columns=[ |
| 152 |
ObjectColumn(name="name", width=0.70), |
| 153 |
ObjectColumn( |
| 154 |
name="num_players", |
| 155 |
label="# Players", |
| 156 |
editable=False, |
| 157 |
width=0.29, |
| 158 |
), |
| 159 |
], |
| 160 |
selected="object.team", |
| 161 |
auto_add=True, |
| 162 |
row_factory=Team, |
| 163 |
configurable=False, |
| 164 |
sortable=False, |
| 165 |
), |
| 166 |
), |
| 167 |
label="League Teams", |
| 168 |
show_border=True, |
| 169 |
), |
| 170 |
VGroup( |
| 171 |
Item( |
| 172 |
"object.team.players", |
| 173 |
show_label=False, |
| 174 |
editor=TableEditor( |
| 175 |
columns=[ |
| 176 |
ObjectColumn(name="name", width=0.70), |
| 177 |
ObjectColumn( |
| 178 |
name="hits", editable=False, width=0.29 |
| 179 |
), |
| 180 |
], |
| 181 |
selected="object.player", |
| 182 |
auto_add=True, |
| 183 |
row_factory=Player, |
| 184 |
configurable=False, |
| 185 |
sortable=False, |
| 186 |
), |
| 187 |
), |
| 188 |
"_", |
| 189 |
HGroup( |
| 190 |
Item( |
| 191 |
"got_hit", |
| 192 |
show_label=False, |
| 193 |
enabled_when="player is not None", |
| 194 |
) |
| 195 |
), |
| 196 |
label="Team Players", |
| 197 |
show_labels=False, |
| 198 |
show_border=True, |
| 199 |
), |
| 200 |
), |
| 201 |
resizable=True, |
| 202 |
) |
| 203 |
|
| 204 |
def _model_changed(self, model): |
| 205 |
""" Handles the 'league' model being initialized. |
| 206 |
""" |
| 207 |
if len(model.teams) > 0: |
| 208 |
self.team = model.teams[0] |
| 209 |
|
| 210 |
def _got_hit_changed(self): |
| 211 |
""" Handles the currently selected player making a hit. |
| 212 |
""" |
| 213 |
self.player.hits += 1 |
| 214 |
|
| 215 |
def _team_changed(self, team): |
| 216 |
""" Handles a new team being selected. |
| 217 |
""" |
| 218 |
if len(team.players) > 0: |
| 219 |
self.player = team.players[0] |
| 220 |
else: |
| 221 |
self.player = None |
| 222 |
|
| 223 |
|
| 224 |
# Function to add two numbers (used with 'reduce'): |
| 225 |
add = lambda a, b: a + b |
| 226 |
|
| 227 |
# --[Example*]----------------------------------------------------------------- |
| 228 |
|
| 229 |
# Define some sample teams and players: |
| 230 |
blue_birds = Team( |
| 231 |
name="Blue Birds", |
| 232 |
players=[ |
| 233 |
Player(name="Mike Scott", hits=25), |
| 234 |
Player(name="Willy Shofield", hits=37), |
| 235 |
Player(name="Tony Barucci", hits=19), |
| 236 |
], |
| 237 |
) |
| 238 |
|
| 239 |
chicken_hawks = Team( |
| 240 |
name="Chicken Hawks", |
| 241 |
players=[ |
| 242 |
Player(name="Jimmy Domore", hits=34), |
| 243 |
Player(name="Bill Janks", hits=16), |
| 244 |
Player(name="Tim Saunders", hits=27), |
| 245 |
], |
| 246 |
) |
| 247 |
|
| 248 |
eagles = Team( |
| 249 |
name="Eagles", |
| 250 |
players=[ |
| 251 |
Player(name="Joe Peppers", hits=33), |
| 252 |
Player(name="Sam Alone", hits=12), |
| 253 |
Player(name="Roger Clemson", hits=23), |
| 254 |
], |
| 255 |
) |
| 256 |
|
| 257 |
# Create a league and its corresponding model view: |
| 258 |
demo = LeagueModelView( |
| 259 |
League( |
| 260 |
name="National Baseball Conference", |
| 261 |
teams=[blue_birds, chicken_hawks, eagles], |
| 262 |
) |
| 263 |
) |