Debian Codegraph @

traitsui/examples/demo/Advanced/Dynamic_EnumEditor_demo.py

raw
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 Dynamic changing an Enum selection list, depending on checked items
13
14 Another way to dynamically change the values shown by an EnumEditor.
15
16 The scenario is a restaurant that at the beginning of the day has a menu list
17 of entrees based upon a fully stocked kitchen. However, as the day progresses,
18 the kitchen's larder gets depleted, and the kitchen may no longer be able to
19 prepare certain entrees, which must then be deleted from the menu. Similarly,
20 deliveries may allow certain entrees to be added back onto the menu.
21
22 The demo is divided into two tabs: Order and Kitchen.
23
24 The Order tab represents a customer's order and consists of a single Entree
25 field, which represents the customer's selection from the drop-down list of
26 entrees that the kitchen can currently prepare.
27
28 The Kitchen tab represents the current set of entrees that the kitchen can
29 prepare, based upon the current contents of its larder.
30
31 As entrees are checked on or off from the Kitchen tab, the customer's Entree
32 drop-down is dynamically updated with the current list of available entrees.
33
34 Notes:
35
36 - The key point of the demo is the use of the 'name' trait in the EnumEditor
37 definition, which links the list of available entrees from the
38 KitchenCapabilities object to the OrderMenu object's entree EnumEditor.
39
40 - The user can freely type any value they want, but only items in the
41 capabilities will be accepted, due to the use of the 'values' argument
42 to the Enum trait. This will be updated as the capabilities change.
43
44 - With the Qt backend, the user can type text and it will be auto-completed
45 to valid values as a the user types. The Wx backend doesn't support this
46 capability in the underlying toolkit.
47
48 - The design will work with any number of active OrderMenu objects, since they
49 all share a common KitchenCapabilities object. As the KitchenCapabilities
50 object is updated, all OrderMenu UI's will automatically update their
51 associated Entree's drop-down list.
52
53 - A careful reader will also observe that this example contains only
54 declarative code. No imperative code is required to handle the automatic
55 updating of the Entree list.
56
57 """
58
59
60 from traits.api import Enum, HasPrivateTraits, Instance, List
61
62 from traitsui.api import (
63 View,
64 Item,
65 VGroup,
66 HSplit,
67 EnumEditor,
68 CheckListEditor,
69 )
70
71 # -- The list of possible entrees -----------------------------------------
72
73 possible_entrees = [
74 'Chicken Fried Steak',
75 'Chicken Fingers',
76 'Chicken Enchiladas',
77 'Cheeseburger',
78 'Pepper Steak',
79 'Beef Tacos',
80 'Club Sandwich',
81 'Ceasar Salad',
82 'Cobb Salad',
83 ]
84
85 # -- The KitchenCapabilities class ----------------------------------------
86
87
88 class KitchenCapabilities(HasPrivateTraits):
89
90 # The current set of entrees the kitchen can make (based on its larder):
91 available = List(possible_entrees)
92
93
94 # The KitchenCapabilities are shared by all waitstaff taking orders:
95 kitchen_capabilities = KitchenCapabilities()
96
97 # -- The OrderMenu class --------------------------------------------------
98
99
100 class OrderMenu(HasPrivateTraits):
101
102 # The person's entree order:
103 entree = Enum(values='capabilities.available')
104
105 # Reference to the restaurant's current entree capabilities:
106 capabilities = Instance(KitchenCapabilities)
107
108 def _capabilities_default(self):
109 return kitchen_capabilities
110
111 # The user interface view:
112 view = View(
113 HSplit(
114 VGroup(
115 Item(
116 'entree',
117 editor=EnumEditor(
118 name='object.capabilities.available',
119 evaluate=str,
120 completion_mode='popup',
121 ),
122 ),
123 label='Order',
124 show_border=True,
125 dock='tab',
126 ),
127 VGroup(
128 Item(
129 'object.capabilities.available',
130 show_label=False,
131 style='custom',
132 editor=CheckListEditor(values=possible_entrees),
133 ),
134 label='Kitchen',
135 show_border=True,
136 dock='tab',
137 ),
138 ),
139 title='Dynamic EnumEditor Demo',
140 resizable=True,
141 )
142
143
144 # -------------------------------------------------------------------------
145
146 # Create the demo:
147 demo = OrderMenu()
148
149 # Run the demo (if invoked from the command line):
150 if __name__ == '__main__':
151 demo.configure_traits()
152 print(demo.entree)