| 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 |
Enum editor |
| 13 |
|
| 14 |
The Enum editor provides a simple way for the user to choose one item from |
| 15 |
a list of known values (normally strings or numbers). |
| 16 |
|
| 17 |
An Enum trait can take any value from a specified list of values. These values |
| 18 |
are typically strings, integers, or floats, but can also be None or hashable |
| 19 |
tuples. |
| 20 |
|
| 21 |
An Enum can be displayed / edited in one of five styles: |
| 22 |
|
| 23 |
* 'simple' displays a drop-down list of allowed values |
| 24 |
* 'custom' by default, displays one or more columns of radio buttons (only |
| 25 |
one of which is selected at a time). |
| 26 |
* 'custom' in 'list' mode (see source code below), displays a list of all the |
| 27 |
allowed values at once. |
| 28 |
* 'readonly' displays the current value as non-editable text. |
| 29 |
* 'text' displays the current value as text. You can also edit this text, |
| 30 |
but your text must be in the list of allowed values. |
| 31 |
|
| 32 |
Please refer to the `EnumEditor API docs`_ for further information. |
| 33 |
|
| 34 |
.. _EnumEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.enum_editor.html#traitsui.editors.enum_editor.EnumEditor |
| 35 |
""" |
| 36 |
|
| 37 |
from traits.api import HasTraits, Enum |
| 38 |
|
| 39 |
from traitsui.api import Item, Group, View, EnumEditor |
| 40 |
|
| 41 |
|
| 42 |
class EnumEditorDemo(HasTraits): |
| 43 |
"""Defines the main EnumEditor demo class.""" |
| 44 |
|
| 45 |
# Define an Enum trait to view. |
| 46 |
name_list = Enum( |
| 47 |
'A-495', |
| 48 |
'A-498', |
| 49 |
'R-1226', |
| 50 |
'TS-17', |
| 51 |
'TS-18', |
| 52 |
'Foo', |
| 53 |
12345, |
| 54 |
(11, 7), |
| 55 |
None, |
| 56 |
) |
| 57 |
|
| 58 |
# Items are used to define the display, one Item per editor style: |
| 59 |
enum_group = Group( |
| 60 |
Item('name_list', style='simple', label='Simple', id="simple"), |
| 61 |
Item('_'), |
| 62 |
# The simple style also supports text entry: |
| 63 |
Item( |
| 64 |
'name_list', |
| 65 |
style='simple', |
| 66 |
label='Simple (text entry)', |
| 67 |
editor=EnumEditor( |
| 68 |
values=name_list, completion_mode='popup', evaluate=True |
| 69 |
), |
| 70 |
id="simple_text", |
| 71 |
), |
| 72 |
Item('_'), |
| 73 |
# The custom style defaults to radio button mode: |
| 74 |
Item('name_list', style='custom', label='Custom radio', id="radio"), |
| 75 |
Item('_'), |
| 76 |
# The custom style can also display in list mode, with extra work: |
| 77 |
Item( |
| 78 |
'name_list', |
| 79 |
style='custom', |
| 80 |
label='Custom list', |
| 81 |
editor=EnumEditor(values=name_list, mode='list'), |
| 82 |
id="list", |
| 83 |
), |
| 84 |
Item('_'), |
| 85 |
Item('name_list', style='text', label='Text', id="text"), |
| 86 |
Item('_'), |
| 87 |
Item('name_list', style='readonly', label='ReadOnly', id="readonly"), |
| 88 |
) |
| 89 |
|
| 90 |
# Demo view: |
| 91 |
traits_view = View( |
| 92 |
enum_group, title='EnumEditor', buttons=['OK'], resizable=True |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
# Create the demo: |
| 97 |
demo = EnumEditorDemo() |
| 98 |
|
| 99 |
# Run the demo (if invoked from the command line): |
| 100 |
if __name__ == '__main__': |
| 101 |
demo.configure_traits() |