widget_float.py
171 lines
| 6.6 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r17598 | """Float class. | ||
Jonathan Frederic
|
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
|
r14540 | from .widget import DOMWidget | ||
Gordon Ball
|
r17060 | from IPython.utils.traitlets import Unicode, CFloat, Bool, Enum, Tuple | ||
Jonathan Frederic
|
r17598 | from IPython.utils.warn import DeprecatedClass | ||
Jonathan Frederic
|
r14265 | |||
Jonathan Frederic
|
r14283 | #----------------------------------------------------------------------------- | ||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r17598 | class _Float(DOMWidget): | ||
Jonathan Frederic
|
r14603 | value = CFloat(0.0, help="Float value", sync=True) | ||
Jonathan Frederic
|
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
|
r14670 | |||
Jonathan Frederic
|
r17598 | class _BoundedFloat(_Float): | ||
Jonathan Frederic
|
r14670 | 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
|
r17008 | self._validate('value', None, self.value) | ||
Jonathan Frederic
|
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) | ||||
Jonathan Frederic
|
r17598 | class FloatText(_Float): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('FloatTextView', sync=True) | ||
Jonathan Frederic
|
r14670 | |||
Jonathan Frederic
|
r17598 | class BoundedFloatText(_BoundedFloat): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('FloatTextView', sync=True) | ||
Jonathan Frederic
|
r14670 | |||
Jonathan Frederic
|
r17598 | class FloatSlider(_BoundedFloat): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('FloatSliderView', sync=True) | ||
Jonathan Frederic
|
r14670 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', | ||
help="Vertical or horizontal.", sync=True) | ||||
Gordon Ball
|
r17703 | _range = Bool(False, help="Display a range selector", sync=True) | ||
MinRK
|
r15159 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | ||
Jonathan Frederic
|
r17723 | slider_color = Unicode(sync=True) | ||
Jonathan Frederic
|
r14670 | |||
Jonathan Frederic
|
r17598 | class FloatProgress(_BoundedFloat): | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('ProgressView', sync=True) | ||
Gordon Ball
|
r17060 | |||
Gordon Ball
|
r17698 | class _FloatRange(_Float): | ||
Gordon Ball
|
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): | ||||
value_given = 'value' in kwargs | ||||
Gordon Ball
|
r17705 | lower_given = 'lower' in kwargs | ||
upper_given = 'upper' in kwargs | ||||
if value_given and (lower_given or upper_given): | ||||
raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") | ||||
if lower_given != upper_given: | ||||
raise ValueError("Must specify both 'lower' and 'upper' for range widget") | ||||
Gordon Ball
|
r17682 | |||
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): | ||||
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
|
r17060 | |||
Gordon Ball
|
r17698 | class _BoundedFloatRange(_FloatRange): | ||
Gordon Ball
|
r17060 | 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
|
r17682 | |||
Gordon Ball
|
r17060 | def __init__(self, *pargs, **kwargs): | ||
Gordon Ball
|
r17682 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs | ||
Gordon Ball
|
r17698 | _FloatRange.__init__(self, *pargs, **kwargs) | ||
Gordon Ball
|
r17682 | |||
# ensure a minimal amount of sanity | ||||
if self.min > self.max: | ||||
raise ValueError("min must be <= max") | ||||
Gordon Ball
|
r17705 | if any_value_given: | ||
# if a value was given, clamp it within (min, max) | ||||
self._validate("value", None, self.value) | ||||
else: | ||||
# otherwise, set it to 25-75% to avoid the handles overlapping | ||||
Gordon Ball
|
r17591 | self.value = (0.75*self.min + 0.25*self.max, | ||
0.25*self.min + 0.75*self.max) | ||||
Gordon Ball
|
r17682 | # callback already set for 'value', 'lower', 'upper' | ||
self.on_trait_change(self._validate, ['min', 'max']) | ||||
Gordon Ball
|
r17060 | |||
def _validate(self, name, old, new): | ||||
Gordon Ball
|
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
|
r17060 | if name == "value": | ||
Gordon Ball
|
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
|
r17127 | else: | ||
Gordon Ball
|
r17682 | self.upper = high | ||
self.lower = low | ||||
Gordon Ball
|
r17060 | |||
Gordon Ball
|
r17698 | class FloatRangeSlider(_BoundedFloatRange): | ||
Gordon Ball
|
r17060 | _view_name = Unicode('FloatSliderView', sync=True) | ||
orientation = Enum([u'horizontal', u'vertical'], u'horizontal', | ||||
help="Vertical or horizontal.", sync=True) | ||||
Gordon Ball
|
r17703 | _range = Bool(True, help="Display a range selector", sync=True) | ||
Gordon Ball
|
r17060 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | ||
Jonathan Frederic
|
r17602 | |||
# Remove in IPython 4.0 | ||||
Jonathan Frederic
|
r17598 | FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget') | ||
BoundedFloatTextWidget = DeprecatedClass(BoundedFloatText, 'BoundedFloatTextWidget') | ||||
FloatSliderWidget = DeprecatedClass(FloatSlider, 'FloatSliderWidget') | ||||
FloatProgressWidget = DeprecatedClass(FloatProgress, 'FloatProgressWidget') | ||||