| 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 |
Tabular editor |
| 20 |
|
| 21 |
The TabularEditor can be used for many of the same purposes as the TableEditor, |
| 22 |
that is, for displaying a table of attributes of lists or arrays of objects. |
| 23 |
|
| 24 |
While similar in function, the tabular editor has advantages and disadvantages |
| 25 |
relative to the table editor. See the Traits UI User Manual for details. |
| 26 |
|
| 27 |
This example defines three classes: |
| 28 |
|
| 29 |
- *Person*: A single person. |
| 30 |
- *MarriedPerson*: A married person (subclass of Person). |
| 31 |
- *Report*: Defines a report based on a list of single and married people. |
| 32 |
|
| 33 |
It creates a tabular display of 10,000 single and married people showing the |
| 34 |
following information: |
| 35 |
|
| 36 |
- Name of the person. |
| 37 |
- Age of the person. |
| 38 |
- The person's address. |
| 39 |
- The name of the person's spouse (if any). |
| 40 |
|
| 41 |
In addition: |
| 42 |
|
| 43 |
- It uses a Courier 10 point font for each line in the table. |
| 44 |
- It displays age column right, instead of left, justified. |
| 45 |
- If the person is a minor (age < 18) and married, it displays a red flag |
| 46 |
image in the age column. |
| 47 |
- If the person is married, it makes the background color for that row a light |
| 48 |
blue. |
| 49 |
|
| 50 |
- If this demo is running under QT, it displays each person's surname |
| 51 |
in a row label. |
| 52 |
|
| 53 |
This example demonstrates: |
| 54 |
|
| 55 |
- How to set up a *TabularEditor*. |
| 56 |
- The display speed of the *TabularEditor*. |
| 57 |
- How to create a *TabularAdapter* that meets each of the specified display |
| 58 |
requirements. |
| 59 |
|
| 60 |
Additional notes: |
| 61 |
|
| 62 |
- You can change the current selection using the up and down arrow keys. |
| 63 |
- If the demo is running under WX, you can move a selected row up and down in |
| 64 |
the table using the left and right arrow keys. |
| 65 |
- If the demo is running under QT, you can move rows by clicking and dragging. |
| 66 |
|
| 67 |
Hopefully, this simple example conveys some of the power and flexibility that |
| 68 |
the `TabularAdapter` class provides you. But, just in case it |
| 69 |
doesn't, let's go over some of the more interesting details: |
| 70 |
|
| 71 |
- Note the values in the `~TabularAdapter.columns` trait. The first |
| 72 |
three values define *column ids* which map directly to traits defined on our |
| 73 |
data objects, while the last one defines an arbitrary string which we define |
| 74 |
so that we can reference it in the `MarriedPerson_spouse_text` and |
| 75 |
`Person_spouse_text` trait definitions. |
| 76 |
|
| 77 |
- Since the font we want to use applies to all table rows, we just specify a |
| 78 |
new default value for the existing `TabularAdapter.font` trait. |
| 79 |
|
| 80 |
- Since we only want to override the default left alignment for the age column, |
| 81 |
we simply define an `age_alignment` trait as a constant ``'right'`` |
| 82 |
value. We could have also used ``age_alignment = Str('right')``, but |
| 83 |
`Constant` never requires storage to be used in an object. |
| 84 |
|
| 85 |
- We define the `MarriedPerson_age_image` property to handle putting |
| 86 |
the ``'red flag'`` image in the age column. By including the class name of |
| 87 |
the items it applies to, we only need to check the `age` value in |
| 88 |
determining what value to return. |
| 89 |
|
| 90 |
- Similary, we use the `MarriedPerson_bg_color` trait to ensure that |
| 91 |
each `MarriedPerson` object has the correct background color in the |
| 92 |
table. |
| 93 |
|
| 94 |
- Finally, we use the `MarriedPerson_spouse_text` and |
| 95 |
`Person_spouse_text` traits, one a property and the other a simple |
| 96 |
constant value, to determine what text to display in the *Spouse* column for |
| 97 |
the different object types. Note that even though a |
| 98 |
`MarriedPerson` is both a `Person` and a |
| 99 |
`MarriedPerson`, it will correctly use the |
| 100 |
`MarriedPerson_spouse_text` trait since the search for a matching |
| 101 |
trait is always made in *mro* order. |
| 102 |
|
| 103 |
Although this is completely subjective, some of the things that the author |
| 104 |
feels stand out about this approach are: |
| 105 |
|
| 106 |
- The class definition is short and sweet. Less code is good. |
| 107 |
- The bulk of the code is declarative. Less room for logic errors. |
| 108 |
- There is only one bit of logic in the class (the ``if`` statement in the |
| 109 |
`MarriedPerson_age_image` property implementation). Again, less |
| 110 |
logic usually translates into more reliable code). |
| 111 |
- The defined traits and even the property implementation method names read |
| 112 |
very descriptively. `_get_MarriedPerson_age_image` pretty much says |
| 113 |
what you would write in a comment or doc string. The implementation almost is |
| 114 |
the documentation. |
| 115 |
|
| 116 |
""" |
| 117 |
# Issue related to the demo warning: enthought/traitsui#960 |
| 118 |
|
| 119 |
from functools import partial |
| 120 |
from random import randint, choice, shuffle |
| 121 |
|
| 122 |
from traits.api import HasTraits, Str, Int, List, Instance, Property, Constant |
| 123 |
from traits.etsconfig.api import ETSConfig |
| 124 |
from traitsui.api import ( |
| 125 |
View, |
| 126 |
Group, |
| 127 |
Item, |
| 128 |
TabularAdapter, |
| 129 |
TabularEditor, |
| 130 |
Color, |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
# -- Person Class Definition ---------------------------------------------- |
| 135 |
class Person(HasTraits): |
| 136 |
|
| 137 |
name = Str() |
| 138 |
address = Str() |
| 139 |
age = Int() |
| 140 |
|
| 141 |
# surname is displayed in qt-only row label: |
| 142 |
surname = Property(fget=lambda self: self.name.split()[-1], observe='name') |
| 143 |
|
| 144 |
|
| 145 |
# -- MarriedPerson Class Definition --------------------------------------- |
| 146 |
class MarriedPerson(Person): |
| 147 |
|
| 148 |
partner = Instance(Person) |
| 149 |
|
| 150 |
|
| 151 |
# -- Tabular Adapter Definition ------------------------------------------- |
| 152 |
class ReportAdapter(TabularAdapter): |
| 153 |
"""The tabular adapter interfaces between the tabular editor and the data |
| 154 |
being displayed. For more details, please refer to the traitsUI user guide. |
| 155 |
""" |
| 156 |
|
| 157 |
# List of (Column labels, Column ID). |
| 158 |
columns = [ |
| 159 |
('Name', 'name'), |
| 160 |
('Age', 'age'), |
| 161 |
('Address', 'address'), |
| 162 |
('Spouse', 'spouse'), |
| 163 |
] |
| 164 |
|
| 165 |
row_label_name = 'surname' |
| 166 |
|
| 167 |
# Interfacing between the model and the view: make some of the cell |
| 168 |
# attributes a property whose behavior is then controlled by the get |
| 169 |
# (and optionally set methods). The cell is identified by its column |
| 170 |
# ID (age, spouse). |
| 171 |
|
| 172 |
# Overwrite default value |
| 173 |
font = 'Courier 10' |
| 174 |
|
| 175 |
age_alignment = Constant('right') |
| 176 |
MarriedPerson_age_image = Property() |
| 177 |
MarriedPerson_bg_color = Color(0xE0E0FF) |
| 178 |
MarriedPerson_spouse_text = Property() |
| 179 |
Person_spouse_text = Constant('') |
| 180 |
|
| 181 |
def _get_MarriedPerson_age_image(self): |
| 182 |
if self.item.age < 18: |
| 183 |
return '@icons:red_ball' |
| 184 |
|
| 185 |
return None |
| 186 |
|
| 187 |
def _get_MarriedPerson_spouse_text(self): |
| 188 |
return self.item.partner.name |
| 189 |
|
| 190 |
|
| 191 |
# -- Tabular Editor Definition -------------------------------------------- |
| 192 |
# The tabular editor works in conjunction with an adapter class, derived from |
| 193 |
# TabularAdapter. |
| 194 |
tabular_editor = TabularEditor( |
| 195 |
adapter=ReportAdapter(), |
| 196 |
operations=['move', 'edit'], |
| 197 |
# Row titles are not supported in WX: |
| 198 |
show_row_titles=(ETSConfig.toolkit in {"qt", "qt4"}), |
| 199 |
) |
| 200 |
|
| 201 |
|
| 202 |
# -- Report Class Definition ---------------------------------------------- |
| 203 |
class Report(HasTraits): |
| 204 |
|
| 205 |
people = List(Person) |
| 206 |
|
| 207 |
traits_view = View( |
| 208 |
Group( |
| 209 |
Item('people', id='table', editor=tabular_editor), |
| 210 |
show_labels=False, |
| 211 |
), |
| 212 |
title='Tabular Editor Demo', |
| 213 |
id='traitsui.demo.Applications.tabular_editor_demo', |
| 214 |
width=0.60, |
| 215 |
height=0.75, |
| 216 |
resizable=True, |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
# -- Generate 10,000 random single and married people --------------------- |
| 221 |
male_names = [ |
| 222 |
'Michael', |
| 223 |
'Edward', |
| 224 |
'Timothy', |
| 225 |
'James', |
| 226 |
'George', |
| 227 |
'Ralph', |
| 228 |
'David', |
| 229 |
'Martin', |
| 230 |
'Bryce', |
| 231 |
'Richard', |
| 232 |
'Eric', |
| 233 |
'Travis', |
| 234 |
'Robert', |
| 235 |
'Bryan', |
| 236 |
'Alan', |
| 237 |
'Harold', |
| 238 |
'John', |
| 239 |
'Stephen', |
| 240 |
'Gael', |
| 241 |
'Frederic', |
| 242 |
'Eli', |
| 243 |
'Scott', |
| 244 |
'Samuel', |
| 245 |
'Alexander', |
| 246 |
'Tobias', |
| 247 |
'Sven', |
| 248 |
'Peter', |
| 249 |
'Albert', |
| 250 |
'Thomas', |
| 251 |
'Horatio', |
| 252 |
'Julius', |
| 253 |
'Henry', |
| 254 |
'Walter', |
| 255 |
'Woodrow', |
| 256 |
'Dylan', |
| 257 |
'Elmer', |
| 258 |
] |
| 259 |
|
| 260 |
female_names = [ |
| 261 |
'Leah', |
| 262 |
'Jaya', |
| 263 |
'Katrina', |
| 264 |
'Vibha', |
| 265 |
'Diane', |
| 266 |
'Lisa', |
| 267 |
'Jean', |
| 268 |
'Alice', |
| 269 |
'Rebecca', |
| 270 |
'Delia', |
| 271 |
'Christine', |
| 272 |
'Marie', |
| 273 |
'Dorothy', |
| 274 |
'Ellen', |
| 275 |
'Victoria', |
| 276 |
'Elizabeth', |
| 277 |
'Margaret', |
| 278 |
'Joyce', |
| 279 |
'Sally', |
| 280 |
'Ethel', |
| 281 |
'Esther', |
| 282 |
'Suzanne', |
| 283 |
'Monica', |
| 284 |
'Hortense', |
| 285 |
'Samantha', |
| 286 |
'Tabitha', |
| 287 |
'Judith', |
| 288 |
'Ariel', |
| 289 |
'Helen', |
| 290 |
'Mary', |
| 291 |
'Jane', |
| 292 |
'Janet', |
| 293 |
'Jennifer', |
| 294 |
'Rita', |
| 295 |
'Rena', |
| 296 |
'Rianna', |
| 297 |
] |
| 298 |
|
| 299 |
all_names = male_names + female_names |
| 300 |
|
| 301 |
male_name = partial(choice, male_names) |
| 302 |
female_name = partial(choice, female_names) |
| 303 |
any_name = partial(choice, all_names) |
| 304 |
age = partial(randint, 15, 72) |
| 305 |
|
| 306 |
|
| 307 |
def family_name(): |
| 308 |
return choice( |
| 309 |
[ |
| 310 |
'Jones', |
| 311 |
'Smith', |
| 312 |
'Thompson', |
| 313 |
'Hayes', |
| 314 |
'Thomas', |
| 315 |
'Boyle', |
| 316 |
"O'Reilly", |
| 317 |
'Lebowski', |
| 318 |
'Lennon', |
| 319 |
'Starr', |
| 320 |
'McCartney', |
| 321 |
'Harrison', |
| 322 |
'Harrelson', |
| 323 |
'Steinbeck', |
| 324 |
'Rand', |
| 325 |
'Hemingway', |
| 326 |
'Zhivago', |
| 327 |
'Clemens', |
| 328 |
'Heinlien', |
| 329 |
'Farmer', |
| 330 |
'Niven', |
| 331 |
'Van Vogt', |
| 332 |
'Sturbridge', |
| 333 |
'Washington', |
| 334 |
'Adams', |
| 335 |
'Bush', |
| 336 |
'Kennedy', |
| 337 |
'Ford', |
| 338 |
'Lincoln', |
| 339 |
'Jackson', |
| 340 |
'Johnson', |
| 341 |
'Eisenhower', |
| 342 |
'Truman', |
| 343 |
'Roosevelt', |
| 344 |
'Wilson', |
| 345 |
'Coolidge', |
| 346 |
'Mack', |
| 347 |
'Moon', |
| 348 |
'Monroe', |
| 349 |
'Springsteen', |
| 350 |
'Rigby', |
| 351 |
"O'Neil", |
| 352 |
'Philips', |
| 353 |
'Clinton', |
| 354 |
'Clapton', |
| 355 |
'Santana', |
| 356 |
'Midler', |
| 357 |
'Flack', |
| 358 |
'Conner', |
| 359 |
'Bond', |
| 360 |
'Seinfeld', |
| 361 |
'Costanza', |
| 362 |
'Kramer', |
| 363 |
'Falk', |
| 364 |
'Moore', |
| 365 |
'Cramdon', |
| 366 |
'Baird', |
| 367 |
'Baer', |
| 368 |
'Spears', |
| 369 |
'Simmons', |
| 370 |
'Roberts', |
| 371 |
'Michaels', |
| 372 |
'Stuart', |
| 373 |
'Montague', |
| 374 |
'Miller', |
| 375 |
] |
| 376 |
) |
| 377 |
|
| 378 |
|
| 379 |
def address(): |
| 380 |
number = randint(11, 999) |
| 381 |
text_1 = choice( |
| 382 |
[ |
| 383 |
'Spring', |
| 384 |
'Summer', |
| 385 |
'Moonlight', |
| 386 |
'Winding', |
| 387 |
'Windy', |
| 388 |
'Whispering', |
| 389 |
'Falling', |
| 390 |
'Roaring', |
| 391 |
'Hummingbird', |
| 392 |
'Mockingbird', |
| 393 |
'Bluebird', |
| 394 |
'Robin', |
| 395 |
'Babbling', |
| 396 |
'Cedar', |
| 397 |
'Pine', |
| 398 |
'Ash', |
| 399 |
'Maple', |
| 400 |
'Oak', |
| 401 |
'Birch', |
| 402 |
'Cherry', |
| 403 |
'Blossom', |
| 404 |
'Rosewood', |
| 405 |
'Apple', |
| 406 |
'Peach', |
| 407 |
'Blackberry', |
| 408 |
'Strawberry', |
| 409 |
'Starlight', |
| 410 |
'Wilderness', |
| 411 |
'Dappled', |
| 412 |
'Beaver', |
| 413 |
'Acorn', |
| 414 |
'Pecan', |
| 415 |
'Pheasant', |
| 416 |
'Owl', |
| 417 |
] |
| 418 |
) |
| 419 |
text_2 = choice( |
| 420 |
[ |
| 421 |
'Way', |
| 422 |
'Lane', |
| 423 |
'Boulevard', |
| 424 |
'Street', |
| 425 |
'Drive', |
| 426 |
'Circle', |
| 427 |
'Avenue', |
| 428 |
'Trail', |
| 429 |
] |
| 430 |
) |
| 431 |
return '%d %s %s' % (number, text_1, text_2) |
| 432 |
|
| 433 |
|
| 434 |
people = [ |
| 435 |
Person( |
| 436 |
name='%s %s' % (any_name(), family_name()), |
| 437 |
age=age(), |
| 438 |
address=address(), |
| 439 |
) |
| 440 |
for i in range(5000) |
| 441 |
] |
| 442 |
|
| 443 |
marrieds = [ |
| 444 |
( |
| 445 |
MarriedPerson( |
| 446 |
name='%s %s' % (female_name(), last_name), |
| 447 |
age=age(), |
| 448 |
address=address, |
| 449 |
), |
| 450 |
MarriedPerson( |
| 451 |
name='%s %s' % (male_name(), last_name), age=age(), address=address |
| 452 |
), |
| 453 |
) |
| 454 |
for last_name, address in [(family_name(), address()) for i in range(2500)] |
| 455 |
] |
| 456 |
|
| 457 |
for female, male in marrieds: |
| 458 |
female.partner = male |
| 459 |
male.partner = female |
| 460 |
people.extend([female, male]) |
| 461 |
|
| 462 |
shuffle(people) |
| 463 |
|
| 464 |
|
| 465 |
# Create the demo: |
| 466 |
demo = Report(people=people) |
| 467 |
|
| 468 |
# Run the demo (if invoked from the command line): |
| 469 |
if __name__ == '__main__': |
| 470 |
demo.configure_traits() |