##// END OF EJS Templates
Support both value tuple and upper, lower traits for both int and float widgets
Support both value tuple and upper, lower traits for both int and float widgets

File last commit:

r17682:eed350de
r17682:eed350de
Show More
widget_float.py
165 lines | 6.3 KiB | text/x-python | PythonLexer
Gordon Ball
Add float implementation of range widget
r17060 """FloatWidget class.
Jonathan Frederic
Cleaned up Python widget code.
r14283
Represents an unbounded float using a widget.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Jonathan Frederic
s/Widget/DOMWidget s/BaseWidget/Widget
r14540 from .widget import DOMWidget
Gordon Ball
Add float implementation of range widget
r17060 from IPython.utils.traitlets import Unicode, CFloat, Bool, Enum, Tuple
Jonathan Frederic
Added float widget
r14265
Jonathan Frederic
Cleaned up Python widget code.
r14283 #-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
Jonathan Frederic
Create base widget classes
r14670 class _FloatWidget(DOMWidget):
Gordon Ball
Add float implementation of range widget
r17060 value = CFloat(0.0, help="Float value", sync=True)
Jonathan Frederic
sync=True isntead of a keys list
r14588 disabled = Bool(False, help="Enable or disable user changes", sync=True)
description = Unicode(help="Description of the value this widget represents", sync=True)
Jonathan Frederic
Create base widget classes
r14670
class _BoundedFloatWidget(_FloatWidget):
max = CFloat(100.0, help="Max value", sync=True)
min = CFloat(0.0, help="Min value", sync=True)
step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True)
def __init__(self, *pargs, **kwargs):
"""Constructor"""
DOMWidget.__init__(self, *pargs, **kwargs)
Raffaele De Feo
Validate initial value of "_BoundedFloatWidget".
r17008 self._validate('value', None, self.value)
Jonathan Frederic
Create base widget classes
r14670 self.on_trait_change(self._validate, ['value', 'min', 'max'])
def _validate(self, name, old, new):
"""Validate value, max, min."""
if self.min > new or new > self.max:
self.value = min(max(new, self.min), self.max)
class FloatTextWidget(_FloatWidget):
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('FloatTextView', sync=True)
Jonathan Frederic
Create base widget classes
r14670
class BoundedFloatTextWidget(_BoundedFloatWidget):
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('FloatTextView', sync=True)
Jonathan Frederic
Create base widget classes
r14670
class FloatSliderWidget(_BoundedFloatWidget):
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('FloatSliderView', sync=True)
Gordon Ball
Add float implementation of range widget
r17060 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
Jonathan Frederic
Create base widget classes
r14670 help="Vertical or horizontal.", sync=True)
Gordon Ball
Add float implementation of range widget
r17060 range = Bool(False, help="Display a range selector", sync=True)
MinRK
set readout=True as default on SliderWidgets
r15159 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
Jonathan Frederic
Create base widget classes
r14670
class FloatProgressWidget(_BoundedFloatWidget):
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('ProgressView', sync=True)
Gordon Ball
Add float implementation of range widget
r17060
class _FloatRangeWidget(_FloatWidget):
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True)
lower = CFloat(0.0, help="Lower bound", sync=False)
upper = CFloat(1.0, help="Upper bound", sync=False)
def __init__(self, *pargs, **kwargs):
if 'value' in kwargs and ('lower' in kwargs or 'upper' in kwargs):
raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget")
value_given = 'value' in kwargs
DOMWidget.__init__(self, *pargs, **kwargs)
# ensure the traits match, preferring whichever (if any) was given in kwargs
if value_given:
self.lower, self.upper = self.value
else:
self.value = (self.lower, self.upper)
self.on_trait_change(self._validate, ['value', 'upper', 'lower'])
def _validate(self, name, old, new):
print '_IntRangeWidget::_validate', name, old, new
if name == 'value':
self.lower, self.upper = min(new), max(new)
elif name == 'lower':
self.value = (new, self.value[1])
elif name == 'upper':
self.value = (self.value[0], new)
Gordon Ball
Add float implementation of range widget
r17060
class _BoundedFloatRangeWidget(_FloatRangeWidget):
step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True)
max = CFloat(100.0, help="Max value", sync=True)
min = CFloat(0.0, help="Min value", sync=True)
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682
Gordon Ball
Add float implementation of range widget
r17060 def __init__(self, *pargs, **kwargs):
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs
_FloatRangeWidget.__init__(self, *pargs, **kwargs)
# ensure a minimal amount of sanity
if self.min > self.max:
raise ValueError("min must be <= max")
# ensure the initial values within bounds
if self.lower < self.min:
self.lower = self.min
if self.upper > self.max:
self.upper = self.max
# if no value is set, use 25-75% to avoid the handles overlapping
if not any_value_given:
Gordon Ball
Default to 25-75% of min-max if no value is set instead of 0-1
r17591 self.value = (0.75*self.min + 0.25*self.max,
0.25*self.min + 0.75*self.max)
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 # callback already set for 'value', 'lower', 'upper'
self.on_trait_change(self._validate, ['min', 'max'])
Gordon Ball
Add float implementation of range widget
r17060
def _validate(self, name, old, new):
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 if name == "min":
if new > self.max:
raise ValueError("setting min > max")
self.min = new
elif name == "max":
if new < self.min:
raise ValueError("setting max < min")
self.max = new
low, high = self.value
Gordon Ball
Add float implementation of range widget
r17060 if name == "value":
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 low, high = min(new), max(new)
elif name == "upper":
if new < self.lower:
raise ValueError("setting upper < lower")
high = new
elif name == "lower":
if new > self.upper:
raise ValueError("setting lower > upper")
low = new
low = max(self.min, min(low, self.max))
high = min(self.max, max(high, self.min))
# determine the order in which we should update the
# lower, upper traits to avoid a temporary inverted overlap
lower_first = high < self.lower
self.value = (low, high)
if lower_first:
self.lower = low
self.upper = high
Gordon Ball
Fix validate logic if min/max are changed
r17127 else:
Gordon Ball
Support both value tuple and upper, lower traits for both int and float widgets
r17682 self.upper = high
self.lower = low
Gordon Ball
Add float implementation of range widget
r17060
class FloatRangeSliderWidget(_BoundedFloatRangeWidget):
_view_name = Unicode('FloatSliderView', sync=True)
orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
help="Vertical or horizontal.", sync=True)
range = Bool(True, help="Display a range selector", sync=True)
readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)