| 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 |
Another demo showing how to use a TabularEditor to create a multi-select list |
| 20 |
box. This demo creates a reusable StringListEditor class and uses that instead |
| 21 |
of defining the editor as part of the demo class. |
| 22 |
|
| 23 |
This approach greatly simplifies the actual demo class and shows how to |
| 24 |
construct a reusable Traits UI-based editor that can be used in other |
| 25 |
applications. |
| 26 |
""" |
| 27 |
# Issue related to the demo warning: enthought/traitsui#960 |
| 28 |
|
| 29 |
from traits.api import HasPrivateTraits, List, Str, Property, observe |
| 30 |
from traits.etsconfig.api import ETSConfig |
| 31 |
|
| 32 |
from traitsui.api import ( |
| 33 |
BasicEditorFactory, |
| 34 |
HGroup, |
| 35 |
Item, |
| 36 |
TabularAdapter, |
| 37 |
TabularEditor, |
| 38 |
View, |
| 39 |
) |
| 40 |
|
| 41 |
if ETSConfig.toolkit == 'wx': |
| 42 |
from traitsui.wx.ui_editor import UIEditor |
| 43 |
else: |
| 44 |
from traitsui.qt.ui_editor import UIEditor |
| 45 |
|
| 46 |
|
| 47 |
# -- Define the reusable StringListEditor class and its helper classes -------- |
| 48 |
|
| 49 |
# Define the tabular adapter used by the Traits UI string list editor: |
| 50 |
class MultiSelectAdapter(TabularAdapter): |
| 51 |
|
| 52 |
# The columns in the table (just the string value): |
| 53 |
columns = [('Value', 'value')] |
| 54 |
|
| 55 |
# The text property used for the 'value' column: |
| 56 |
value_text = Property() |
| 57 |
|
| 58 |
def _get_value_text(self): |
| 59 |
return self.item |
| 60 |
|
| 61 |
|
| 62 |
# Define the actual Traits UI string list editor: |
| 63 |
class _StringListEditor(UIEditor): |
| 64 |
|
| 65 |
# Indicate that the editor is scrollable/resizable: |
| 66 |
scrollable = True |
| 67 |
|
| 68 |
# The list of available editor choices: |
| 69 |
choices = List(Str) |
| 70 |
|
| 71 |
# The list of currently selected items: |
| 72 |
selected = List(Str) |
| 73 |
|
| 74 |
# The traits UI view used by the editor: |
| 75 |
traits_view = View( |
| 76 |
Item( |
| 77 |
'choices', |
| 78 |
show_label=False, |
| 79 |
editor=TabularEditor( |
| 80 |
show_titles=False, |
| 81 |
selected='selected', |
| 82 |
editable=False, |
| 83 |
multi_select=True, |
| 84 |
adapter=MultiSelectAdapter(), |
| 85 |
), |
| 86 |
), |
| 87 |
id='string_list_editor', |
| 88 |
resizable=True, |
| 89 |
) |
| 90 |
|
| 91 |
def init_ui(self, parent): |
| 92 |
|
| 93 |
self.sync_value(self.factory.choices, 'choices', 'from', is_list=True) |
| 94 |
self.selected = self.value |
| 95 |
|
| 96 |
return self.edit_traits(parent=parent, kind='subpanel') |
| 97 |
|
| 98 |
@observe('selected') |
| 99 |
def _selected_modified(self, event): |
| 100 |
self.value = self.selected |
| 101 |
|
| 102 |
|
| 103 |
# Define the StringListEditor class used by client code: |
| 104 |
class StringListEditor(BasicEditorFactory): |
| 105 |
|
| 106 |
# The editor implementation class: |
| 107 |
klass = _StringListEditor |
| 108 |
|
| 109 |
# The extended trait name containing the editor's set of choices: |
| 110 |
choices = Str() |
| 111 |
|
| 112 |
|
| 113 |
# -- Define the demo class ---------------------------------------------------- |
| 114 |
class MultiSelect(HasPrivateTraits): |
| 115 |
"""This class demonstrates using the StringListEditor to select a set |
| 116 |
of string values from a set of choices. |
| 117 |
""" |
| 118 |
|
| 119 |
# The list of choices to select from: |
| 120 |
choices = List(Str) |
| 121 |
|
| 122 |
# The currently selected list of choices: |
| 123 |
selected = List(Str) |
| 124 |
|
| 125 |
# A dummy result so that we can display the selection using the same |
| 126 |
# StringListEditor: |
| 127 |
result = List(Str) |
| 128 |
|
| 129 |
# A traits view showing the list of choices on the left-hand side, and |
| 130 |
# the currently selected choices on the right-hand side: |
| 131 |
traits_view = View( |
| 132 |
HGroup( |
| 133 |
Item( |
| 134 |
'selected', |
| 135 |
show_label=False, |
| 136 |
editor=StringListEditor(choices='choices'), |
| 137 |
), |
| 138 |
Item( |
| 139 |
'result', |
| 140 |
show_label=False, |
| 141 |
editor=StringListEditor(choices='selected'), |
| 142 |
), |
| 143 |
), |
| 144 |
width=0.20, |
| 145 |
height=0.25, |
| 146 |
) |
| 147 |
|
| 148 |
|
| 149 |
# Create the demo: |
| 150 |
demo = MultiSelect( |
| 151 |
choices=[ |
| 152 |
'one', |
| 153 |
'two', |
| 154 |
'three', |
| 155 |
'four', |
| 156 |
'five', |
| 157 |
'six', |
| 158 |
'seven', |
| 159 |
'eight', |
| 160 |
'nine', |
| 161 |
'ten', |
| 162 |
], |
| 163 |
selected=['two', 'five', 'nine'], |
| 164 |
) |
| 165 |
|
| 166 |
# Run the demo (if invoked from the command line): |
| 167 |
if __name__ == '__main__': |
| 168 |
demo.configure_traits() |