""" open/dulcinea/lib/ui/form/physical_value_widget.qpy """ from dulcinea.physical_unit import PhysicalUnit from dulcinea.physical_value import PhysicalValue from dulcinea.ui.form.number_widget import NumberWidget from qp.fill.widget import SingleSelectWidget, CompositeWidget from qp.fill.widget import WidgetValueError import sys class PhysicalValueWidget(CompositeWidget): def __init__(self, name, value=None, is_scalar=1, accept_range=0, size=10, maxlength=None, default_unit=None, allowed_units=None, **kwargs): CompositeWidget.__init__(self, name, value, **kwargs) # Check units. assert default_unit is None or isinstance(default_unit, PhysicalUnit), ( 'form element %r default unit must be None nor a PhysicalUnit: ' 'got %r' % (name, default_unit)) if type(allowed_units) is list: assert allowed_units, ( 'form element %r allowed units must not be an empty ' 'list' % name) for allowed_unit in allowed_units: assert (allowed_unit is None or isinstance(allowed_unit, PhysicalUnit)), ( 'form element %r allowed units includes an element ' 'that is neither None nor a PhysicalUnit: ' 'got %r' % (name, allowed_unit)) if default_unit is None or default_unit not in allowed_units: default_unit = allowed_units[0] else: assert allowed_units is None, ( 'form element %r allowed units is neither a list nor none: ' 'got %r' % (name, allowed_units)) # Check value type. if type(value) is list: assert value, ( 'form element %r value must not be an empty ' 'list' % name) number = [] unit = value[0].get_unit() if allowed_units and unit not in allowed_units: unit = default_unit for val in value: assert isinstance(val, PhysicalValue), ( 'form element %r value not a PhysicalValue: ' 'got %r' % (name, val)) number.append(val.convert(unit).get_value()) elif value is not None: assert isinstance(value, PhysicalValue), ( 'form element %r value not a PhysicalValue or list: ' 'got %r' % (name, value)) unit = value.get_unit() if allowed_units and unit not in allowed_units: unit = default_unit number = value.convert(unit).get_value() else: number = None unit = default_unit # Set self.allowed_units. if allowed_units: self.allowed_units = allowed_units else: self.allowed_units = [unit] show_unit = self.allowed_units != [None] self.add(NumberWidget, 'value', value=number, is_scalar=is_scalar, accept_range=accept_range, size=size, maxlength=maxlength, render_br=not show_unit) # Set self.unit_widget. if show_unit: options = [] for allowed_unit in self.allowed_units: if allowed_unit is None: options.append((allowed_unit, '', '')) else: options.append((allowed_unit, allowed_unit.get_name(html=1), allowed_unit)) self.add(SingleSelectWidget, 'unit', value=unit, options=options, sort=1) def _parse(self, request): value = self.get('value') if self.get_widget('unit'): unit = self.get('unit') else: unit = None if value is None: self.value = None elif type(value) is list: self.value = [PhysicalValue(val, unit) for val in value] else: try: self.value = PhysicalValue(value, unit) except TypeError: exc = sys.exc_info()[1] raise WidgetValueError(exc) def format_physical_value_css:str(): """ div.ToleranceWidget div.title { padding-left: 1ex; } body.IE5 div.PhysicalValueWidget div.content, body.IE6 div.PhysicalValueWidget div.content, body.IE7 div.PhysicalValueWidget div.content { height: 1%; } div.ToleranceWidget div, div.PhysicalValueWidget div.content div { float: left; } div.PhysicalValueWidget div.content div.close { float: none; } div.PhysicalValueWidget div.content div.error, div.ToleranceWidget div.content div.error { clear: left; } div.CompositeWidget div.PhysicalValueWidget div.close, div.CompositeWidget div.ToleranceWidget div.close { display: none; } """