widget_int.py
207 lines
| 8.2 KiB
| text/x-python
|
PythonLexer
Sylvain Corlay
|
r20950 | """Int class. | ||
Jonathan Frederic
|
r14283 | |||
Represents an unbounded int 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 | ||||
#----------------------------------------------------------------------------- | ||||
Sylvain Corlay
|
r18531 | from .widget import DOMWidget, register | ||
Sylvain Corlay
|
r20797 | from .trait_types import Color | ||
Sylvain Corlay
|
r20950 | from IPython.utils.traitlets import (Unicode, CInt, Bool, CaselessStrEnum, | ||
Tuple, TraitError) | ||||
Jonathan Frederic
|
r17598 | from IPython.utils.warn import DeprecatedClass | ||
Jonathan Frederic
|
r14266 | |||
Jonathan Frederic
|
r14283 | #----------------------------------------------------------------------------- | ||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r17598 | class _Int(DOMWidget): | ||
Jonathan Frederic
|
r17602 | """Base class used to create widgets that represent an int.""" | ||
Sylvain Corlay
|
r20227 | value = CInt(0, help="Int 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 | |||
Jason Goad
|
r19620 | def __init__(self, value=None, **kwargs): | ||
if value is not None: | ||||
kwargs['value'] = value | ||||
super(_Int, self).__init__(**kwargs) | ||||
Jonathan Frederic
|
r14670 | |||
Sylvain Corlay
|
r20950 | |||
Jonathan Frederic
|
r17598 | class _BoundedInt(_Int): | ||
Jonathan Frederic
|
r17602 | """Base class used to create widgets that represent a int that is bounded | ||
by a minium and maximum.""" | ||||
Sylvain Corlay
|
r20950 | step = CInt(1, help="Minimum step to increment the value (ignored by some views)", sync=True) | ||
Jonathan Frederic
|
r14670 | max = CInt(100, help="Max value", sync=True) | ||
min = CInt(0, help="Min value", sync=True) | ||||
def __init__(self, *pargs, **kwargs): | ||||
"""Constructor""" | ||||
Jason Goad
|
r19621 | super(_BoundedInt, self).__init__(*pargs, **kwargs) | ||
Jonathan Frederic
|
r14670 | |||
Sylvain Corlay
|
r20950 | def _value_validate(self, value, trait): | ||
"""Cap and floor value""" | ||||
if self.min > value or self.max < value: | ||||
value = min(max(value, self.min), self.max) | ||||
return value | ||||
Jonathan Frederic
|
r14670 | |||
Sylvain Corlay
|
r20950 | def _min_validate(self, min, trait): | ||
"""Enforce min <= value <= max""" | ||||
if min > self.max: | ||||
raise TraitError("Setting min > max") | ||||
if min > self.value: | ||||
self.value = min | ||||
return min | ||||
Jonathan Frederic
|
r17852 | |||
Sylvain Corlay
|
r20950 | def _max_validate(self, max, trait): | ||
"""Enforce min <= value <= max""" | ||||
if max < self.min: | ||||
raise TraitError("setting max < min") | ||||
if max < self.value: | ||||
self.value = max | ||||
return max | ||||
Jonathan Frederic
|
r14670 | |||
Sylvain Corlay
|
r18533 | @register('IPython.IntText') | ||
Jonathan Frederic
|
r17598 | class IntText(_Int): | ||
Jonathan Frederic
|
r17602 | """Textbox widget that represents a int.""" | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('IntTextView', sync=True) | ||
Jonathan Frederic
|
r14670 | |||
Sylvain Corlay
|
r18533 | @register('IPython.BoundedIntText') | ||
Jonathan Frederic
|
r17598 | class BoundedIntText(_BoundedInt): | ||
Jonathan Frederic
|
r17602 | """Textbox widget that represents a int bounded by a minimum and maximum value.""" | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('IntTextView', sync=True) | ||
Jonathan Frederic
|
r14670 | |||
Sylvain Corlay
|
r18533 | @register('IPython.IntSlider') | ||
Jonathan Frederic
|
r17598 | class IntSlider(_BoundedInt): | ||
Jonathan Frederic
|
r17602 | """Slider widget that represents a int bounded by a minimum and maximum value.""" | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('IntSliderView', sync=True) | ||
Jonathan Frederic
|
r17729 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], | ||
Sylvain Corlay
|
r20483 | default_value='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) | ||
Sylvain Corlay
|
r20796 | slider_color = Color(None, allow_none=True, sync=True) | ||
Jonathan Frederic
|
r14670 | |||
Sylvain Corlay
|
r18533 | @register('IPython.IntProgress') | ||
Jonathan Frederic
|
r17598 | class IntProgress(_BoundedInt): | ||
Jonathan Frederic
|
r17602 | """Progress bar that represents a int bounded by a minimum and maximum value.""" | ||
Jonathan Frederic
|
r14701 | _view_name = Unicode('ProgressView', sync=True) | ||
Gordon Ball
|
r17059 | |||
Jonathan Frederic
|
r17729 | bar_style = CaselessStrEnum( | ||
values=['success', 'info', 'warning', 'danger', ''], | ||||
default_value='', allow_none=True, sync=True, help="""Use a | ||||
predefined styling for the progess bar.""") | ||||
Gordon Ball
|
r17698 | class _IntRange(_Int): | ||
Gordon Ball
|
r17682 | value = Tuple(CInt, CInt, default_value=(0, 1), help="Tuple of (lower, upper) bounds", sync=True) | ||
lower = CInt(0, help="Lower bound", sync=False) | ||||
upper = CInt(1, 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 | |||
Thomas Kluyver
|
r19622 | super(_IntRange, self).__init__(*pargs, **kwargs) | ||
Gordon Ball
|
r17682 | |||
# 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
|
r17059 | |||
Gordon Ball
|
r17698 | class _BoundedIntRange(_IntRange): | ||
Gordon Ball
|
r17059 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) | ||
max = CInt(100, help="Max value", sync=True) | ||||
min = CInt(0, help="Min value", sync=True) | ||||
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 | _IntRange.__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
|
r17059 | |||
def _validate(self, name, old, new): | ||||
Gordon Ball
|
r17682 | if name == "min": | ||
if new > self.max: | ||||
raise ValueError("setting min > max") | ||||
elif name == "max": | ||||
if new < self.min: | ||||
raise ValueError("setting max < min") | ||||
low, high = self.value | ||||
Gordon Ball
|
r17059 | 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
|
r17059 | |||
Sylvain Corlay
|
r18533 | @register('IPython.IntRangeSlider') | ||
Gordon Ball
|
r17698 | class IntRangeSlider(_BoundedIntRange): | ||
Thomas Kluyver
|
r19970 | """Slider widget that represents a pair of ints between a minimum and maximum value.""" | ||
Gordon Ball
|
r17059 | _view_name = Unicode('IntSliderView', sync=True) | ||
Jonathan Frederic
|
r17731 | orientation = CaselessStrEnum(values=['horizontal', 'vertical'], | ||
Sylvain Corlay
|
r20483 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | ||
Gordon Ball
|
r17703 | _range = Bool(True, help="Display a range selector", sync=True) | ||
Gordon Ball
|
r17059 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | ||
Sylvain Corlay
|
r20796 | slider_color = Color(None, allow_none=True, sync=True) | ||
Jonathan Frederic
|
r17602 | |||
# Remove in IPython 4.0 | ||||
Jonathan Frederic
|
r17598 | IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget') | ||
BoundedIntTextWidget = DeprecatedClass(BoundedIntText, 'BoundedIntTextWidget') | ||||
IntSliderWidget = DeprecatedClass(IntSlider, 'IntSliderWidget') | ||||
IntProgressWidget = DeprecatedClass(IntProgress, 'IntProgressWidget') | ||||