<s> from pyface . qt import QtGui , QtCore <EOL> from traits . api import Float , Any , Str , Trait <EOL> from traitsui . editors . api import RangeEditor <EOL> from traitsui . qt4 . editor import Editor <EOL> from traitsui . qt4 . extra . range_slider import RangeSlider <EOL> class _BoundsEditor ( Editor ) : <EOL> evaluate = Any <EOL> min = Any <EOL> max = Any <EOL> low = Any <EOL> high = Any <EOL> format = Str <EOL> def init ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> factory = self . factory <EOL> if not factory . low_name : <EOL> self . low = factory . low <EOL> if not factory . high_name : <EOL> self . high = factory . high <EOL> self . max = factory . max <EOL> self . min = factory . min <EOL> self . format = factory . format <EOL> self . evaluate = factory . evaluate <EOL> self . sync_value ( factory . evaluate_name , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . sync_value ( factory . low_name , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . sync_value ( factory . high_name , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . control = QtGui . QWidget ( ) <EOL> panel = QtGui . QHBoxLayout ( self . control ) <EOL> panel . setContentsMargins ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . _label_lo = QtGui . QLineEdit ( self . format % self . low ) <EOL> QtCore . QObject . connect ( self . _label_lo , QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . update_low_on_enter ) <EOL> panel . addWidget ( self . _label_lo ) <EOL> sh = self . _label_lo . sizeHint ( ) <EOL> sh . setWidth ( sh . width ( ) / <NUM_LIT:2> ) <EOL> self . _label_lo . setMaximumSize ( sh ) <EOL> self . control . slider = slider = RangeSlider ( QtCore . Qt . Horizontal ) <EOL> slider . setTracking ( factory . auto_set ) <EOL> slider . setMinimum ( <NUM_LIT:0> ) <EOL> slider . setMaximum ( <NUM_LIT> ) <EOL> slider . setPageStep ( <NUM_LIT:1000> ) <EOL> slider . setSingleStep ( <NUM_LIT:100> ) <EOL> slider . setLow ( self . _convert_to_slider ( self . low ) ) <EOL> slider . setHigh ( self . _convert_to_slider ( self . high ) ) <EOL> QtCore . QObject . connect ( slider , QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . update_object_on_scroll ) <EOL> panel . addWidget ( slider ) <EOL> self . _label_hi = QtGui . QLineEdit ( self . format % self . high ) <EOL> QtCore . QObject . connect ( self . _label_hi , QtCore . SIGNAL ( '<STR_LIT>' ) , <EOL> self . update_high_on_enter ) <EOL> panel . addWidget ( self . _label_hi ) <EOL> sh = self . _label_hi . sizeHint ( ) <EOL> sh . setWidth ( sh . width ( ) / <NUM_LIT:2> ) <EOL> self . _label_hi . setMaximumSize ( sh ) <EOL> self . set_tooltip ( slider ) <EOL> self . set_tooltip ( self . _label_lo ) <EOL> self . set_tooltip ( self . _label_hi ) <EOL> def update_low_on_enter ( self ) : <EOL> try : <EOL> try : <EOL> low = eval ( unicode ( self . _label_lo . text ( ) ) . strip ( ) ) <EOL> if self . evaluate is not None : <EOL> low = self . evaluate ( low ) <EOL> except Exception as ex : <EOL> low = self . low <EOL> self . _label_lo . setText ( self . format % self . low ) <EOL> if not self . factory . is_float : <EOL> low = int ( low ) <EOL> if low > self . high : <EOL> low = self . high - self . _step_size ( ) <EOL> self . _label_lo . setText ( self . format % low ) <EOL> self . control . slider . setLow ( self . _convert_to_slider ( low ) ) <EOL> self . low = low <EOL> except : <EOL> pass <EOL> def update_high_on_enter ( self ) : <EOL> try : <EOL> try : <EOL> high = eval ( unicode ( self . _label_hi . text ( ) ) . strip ( ) ) <EOL> if self . evaluate is not None : <EOL> high = self . evaluate ( high ) <EOL> except : <EOL> high = self . high <EOL> self . _label_hi . setText ( self . format % self . high ) <EOL> if not self . factory . is_float : <EOL> high = int ( high ) <EOL> if high < self . low : <EOL> high = self . low + self . _step_size ( ) <EOL> self . _label_hi . setText ( self . format % high ) <EOL> self . control . slider . setHigh ( self . _convert_to_slider ( high ) ) <EOL> self . high = high <EOL> except : <EOL> pass <EOL> def update_object_on_scroll ( self , pos ) : <EOL> low = self . _convert_from_slider ( self . control . slider . low ( ) ) <EOL> high = self . _convert_from_slider ( self . control . slider . high ( ) ) <EOL> if self . factory . is_float : <EOL> self . low = low <EOL> self . high = high <EOL> else : <EOL> self . low = int ( low ) <EOL> self . high = int ( high ) <EOL> self . control . slider . setLow ( self . _convert_to_slider ( low ) ) <EOL> self . control . slider . setHigh ( self . _convert_to_slider ( high ) ) <EOL> def update_editor ( self ) : <EOL> return <EOL> def _check_max_and_min ( self ) : <EOL> if self . max is None : <EOL> self . max = self . high <EOL> if self . min is None : <EOL> self . min = self . low <EOL> def _step_size ( self ) : <EOL> slider_delta = self . control . slider . maximum ( ) - self . control . slider . minimum ( ) <EOL> range_delta = self . max - self . min <EOL> return float ( range_delta ) / slider_delta <EOL> def _convert_from_slider ( self , slider_val ) : <EOL> self . _check_max_and_min ( ) <EOL> return self . min + slider_val * self . _step_size ( ) <EOL> def _convert_to_slider ( self , value ) : <EOL> self . _check_max_and_min ( ) <EOL> return self . control . slider . minimum ( ) + ( value - self . min ) / self . _step_size ( ) <EOL> def _low_changed ( self , low ) : <EOL> if self . control is None : <EOL> return <EOL> if self . _label_lo is not None : <EOL> self . _label_lo . setText ( self . format % low ) <EOL> self . control . slider . setLow ( self . _convert_to_slider ( low ) ) <EOL> def _high_changed ( self , high ) : <EOL> if self . control is None : <EOL> return <EOL> if self . _label_hi is not None : <EOL> self . _label_hi . setText ( self . format % high ) <EOL> self . control . slider . setHigh ( self . _convert_to_slider ( self . high ) ) <EOL> class BoundsEditor ( RangeEditor ) : <EOL> min = Trait ( None , Float ) <EOL> max = Trait ( None , Float ) <EOL> def _get_simple_editor_class ( self ) : <EOL> return _BoundsEditor <EOL> def _get_custom_editor_class ( self ) : <EOL> return _BoundsEditor </s>
