Show More
@@ -1,113 +1,118 b'' | |||||
1 | """SelectionWidget classes. |
|
1 | """SelectionWidget classes. | |
2 |
|
2 | |||
3 | Represents an enumeration using a widget. |
|
3 | Represents an enumeration using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from collections import OrderedDict |
|
17 | from collections import OrderedDict | |
18 | from threading import Lock |
|
18 | from threading import Lock | |
19 |
|
19 | |||
20 | from .widget import DOMWidget |
|
20 | from .widget import DOMWidget | |
21 | from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict, TraitError |
|
21 | from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict, TraitError | |
22 | from IPython.utils.py3compat import unicode_type |
|
22 | from IPython.utils.py3compat import unicode_type | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # SelectionWidget |
|
25 | # SelectionWidget | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | class _SelectionWidget(DOMWidget): |
|
27 | class _SelectionWidget(DOMWidget): | |
28 | """Base class for Selection widgets |
|
28 | """Base class for Selection widgets | |
29 |
|
29 | |||
30 | ``values`` can be specified as a list or dict. If given as a list, |
|
30 | ``values`` can be specified as a list or dict. If given as a list, | |
31 | it will be transformed to a dict of the form ``{str(value):value}``. |
|
31 | it will be transformed to a dict of the form ``{str(value):value}``. | |
32 | """ |
|
32 | """ | |
33 |
|
33 | |||
34 | value = Any(help="Selected value") |
|
34 | value = Any(help="Selected value") | |
35 | values = Dict(help="""Dictionary of {name: value} the user can select. |
|
35 | values = Dict(help="""Dictionary of {name: value} the user can select. | |
36 |
|
36 | |||
37 | The keys of this dictionary are the strings that will be displayed in the UI, |
|
37 | The keys of this dictionary are the strings that will be displayed in the UI, | |
38 | representing the actual Python choices. |
|
38 | representing the actual Python choices. | |
39 |
|
39 | |||
40 | The keys of this dictionary are also available as value_names. |
|
40 | The keys of this dictionary are also available as value_names. | |
41 | """) |
|
41 | """) | |
42 | value_name = Unicode(help="The name of the selected value", sync=True) |
|
42 | value_name = Unicode(help="The name of the selected value", sync=True) | |
43 |
value_names = List(Unicode, help=""" |
|
43 | value_names = List(Unicode, help="""Read-only list of names for each value. | |
44 |
|
44 | |||
45 | If values is specified as a list, this is the string representation of each element. |
|
45 | If values is specified as a list, this is the string representation of each element. | |
46 | Otherwise, it is the keys of the values dictionary. |
|
46 | Otherwise, it is the keys of the values dictionary. | |
47 |
|
47 | |||
48 | These strings are used to display the choices in the front-end.""", sync=True) |
|
48 | These strings are used to display the choices in the front-end.""", sync=True) | |
49 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
49 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
50 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
50 | description = Unicode(help="Description of the value this widget represents", sync=True) | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | def __init__(self, *args, **kwargs): |
|
53 | def __init__(self, *args, **kwargs): | |
54 | self.value_lock = Lock() |
|
54 | self.value_lock = Lock() | |
|
55 | self._in_values_changed = False | |||
55 | if 'values' in kwargs: |
|
56 | if 'values' in kwargs: | |
56 | values = kwargs['values'] |
|
57 | values = kwargs['values'] | |
57 | # convert list values to an dict of {str(v):v} |
|
58 | # convert list values to an dict of {str(v):v} | |
58 | if isinstance(values, list): |
|
59 | if isinstance(values, list): | |
59 | # preserve list order with an OrderedDict |
|
60 | # preserve list order with an OrderedDict | |
60 | kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values) |
|
61 | kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values) | |
61 | DOMWidget.__init__(self, *args, **kwargs) |
|
62 | DOMWidget.__init__(self, *args, **kwargs) | |
62 |
|
63 | |||
63 | def _values_changed(self, name, old, new): |
|
64 | def _values_changed(self, name, old, new): | |
64 | """Handles when the values dict has been changed. |
|
65 | """Handles when the values dict has been changed. | |
65 |
|
66 | |||
66 | Setting values implies setting value names from the keys of the dict. |
|
67 | Setting values implies setting value names from the keys of the dict. | |
67 | """ |
|
68 | """ | |
68 |
self. |
|
69 | self._in_values_changed = True | |
|
70 | try: | |||
|
71 | self.value_names = list(new.keys()) | |||
|
72 | finally: | |||
|
73 | self._in_values_changed = False | |||
69 |
|
74 | |||
70 | def _value_names_changed(self, name, old, new): |
|
75 | def _value_names_changed(self, name, old, new): | |
71 |
if |
|
76 | if not self._in_values_changed: | |
72 | raise TraitError("Expected %i value names, got %i." % (len(self.values), len(new))) |
|
77 | raise TraitError("value_names is a read-only proxy to values.keys(). Use the values dict instead.") | |
73 |
|
78 | |||
74 | def _value_changed(self, name, old, new): |
|
79 | def _value_changed(self, name, old, new): | |
75 | """Called when value has been changed""" |
|
80 | """Called when value has been changed""" | |
76 | if self.value_lock.acquire(False): |
|
81 | if self.value_lock.acquire(False): | |
77 | try: |
|
82 | try: | |
78 | # Make sure the value is one of the options |
|
83 | # Make sure the value is one of the options | |
79 | for k,v in self.values.items(): |
|
84 | for k,v in self.values.items(): | |
80 | if new == v: |
|
85 | if new == v: | |
81 | # set the selected value name |
|
86 | # set the selected value name | |
82 | self.value_name = k |
|
87 | self.value_name = k | |
83 | return |
|
88 | return | |
84 | raise TraitError('Value not found: %r' % new) |
|
89 | raise TraitError('Value not found: %r' % new) | |
85 | finally: |
|
90 | finally: | |
86 | self.value_lock.release() |
|
91 | self.value_lock.release() | |
87 |
|
92 | |||
88 | def _value_name_changed(self, name, old, new): |
|
93 | def _value_name_changed(self, name, old, new): | |
89 | """Called when the value name has been changed (typically by the frontend).""" |
|
94 | """Called when the value name has been changed (typically by the frontend).""" | |
90 | if self.value_lock.acquire(False): |
|
95 | if self.value_lock.acquire(False): | |
91 | try: |
|
96 | try: | |
92 | if new in self.values: |
|
97 | if new in self.values: | |
93 | self.value = self.values[new] |
|
98 | self.value = self.values[new] | |
94 | else: |
|
99 | else: | |
95 | self.value = None |
|
100 | self.value = None | |
96 | finally: |
|
101 | finally: | |
97 | self.value_lock.release() |
|
102 | self.value_lock.release() | |
98 |
|
103 | |||
99 |
|
104 | |||
100 | class ToggleButtonsWidget(_SelectionWidget): |
|
105 | class ToggleButtonsWidget(_SelectionWidget): | |
101 | _view_name = Unicode('ToggleButtonsView', sync=True) |
|
106 | _view_name = Unicode('ToggleButtonsView', sync=True) | |
102 |
|
107 | |||
103 |
|
108 | |||
104 | class DropdownWidget(_SelectionWidget): |
|
109 | class DropdownWidget(_SelectionWidget): | |
105 | _view_name = Unicode('DropdownView', sync=True) |
|
110 | _view_name = Unicode('DropdownView', sync=True) | |
106 |
|
111 | |||
107 |
|
112 | |||
108 | class RadioButtonsWidget(_SelectionWidget): |
|
113 | class RadioButtonsWidget(_SelectionWidget): | |
109 | _view_name = Unicode('RadioButtonsView', sync=True) |
|
114 | _view_name = Unicode('RadioButtonsView', sync=True) | |
110 |
|
115 | |||
111 |
|
116 | |||
112 | class SelectWidget(_SelectionWidget): |
|
117 | class SelectWidget(_SelectionWidget): | |
113 | _view_name = Unicode('SelectView', sync=True) |
|
118 | _view_name = Unicode('SelectView', sync=True) |
General Comments 0
You need to be logged in to leave comments.
Login now