<s> from pyface . qt import QtGui , QtCore <EOL> class RangeSlider ( QtGui . QSlider ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> super ( RangeSlider , self ) . __init__ ( * args ) <EOL> self . _low = self . minimum ( ) <EOL> self . _high = self . maximum ( ) <EOL> self . pressed_control = QtGui . QStyle . SC_None <EOL> self . hover_control = QtGui . QStyle . SC_None <EOL> self . click_offset = <NUM_LIT:0> <EOL> self . active_slider = <NUM_LIT:0> <EOL> def low ( self ) : <EOL> return self . _low <EOL> def setLow ( self , low ) : <EOL> self . _low = low <EOL> self . update ( ) <EOL> def high ( self ) : <EOL> return self . _high <EOL> def setHigh ( self , high ) : <EOL> self . _high = high <EOL> self . update ( ) <EOL> def paintEvent ( self , event ) : <EOL> painter = QtGui . QPainter ( self ) <EOL> style = QtGui . QApplication . style ( ) <EOL> for i , value in enumerate ( [ self . _low , self . _high ] ) : <EOL> opt = QtGui . QStyleOptionSlider ( ) <EOL> self . initStyleOption ( opt ) <EOL> if i == <NUM_LIT:0> : <EOL> opt . subControls = QtGui . QStyle . SC_SliderGroove | QtGui . QStyle . SC_SliderHandle <EOL> else : <EOL> opt . subControls = QtGui . QStyle . SC_SliderHandle <EOL> if self . tickPosition ( ) != self . NoTicks : <EOL> opt . subControls |= QtGui . QStyle . SC_SliderTickmarks <EOL> if self . pressed_control : <EOL> opt . activeSubControls = self . pressed_control <EOL> opt . state |= QtGui . QStyle . State_Sunken <EOL> else : <EOL> opt . activeSubControls = self . hover_control <EOL> opt . sliderPosition = value <EOL> opt . sliderValue = value <EOL> style . drawComplexControl ( QtGui . QStyle . CC_Slider , opt , painter , self ) <EOL> def mousePressEvent ( self , event ) : <EOL> event . accept ( ) <EOL> style = QtGui . QApplication . style ( ) <EOL> button = event . button ( ) <EOL> if button : <EOL> opt = QtGui . QStyleOptionSlider ( ) <EOL> self . initStyleOption ( opt ) <EOL> self . active_slider = - <NUM_LIT:1> <EOL> for i , value in enumerate ( [ self . _low , self . _high ] ) : <EOL> opt . sliderPosition = value <EOL> hit = style . hitTestComplexControl ( style . CC_Slider , opt , event . pos ( ) , self ) <EOL> if hit == style . SC_SliderHandle : <EOL> self . active_slider = i <EOL> self . pressed_control = hit <EOL> self . triggerAction ( self . SliderMove ) <EOL> self . setRepeatAction ( self . SliderNoAction ) <EOL> self . setSliderDown ( True ) <EOL> break <EOL> if self . active_slider < <NUM_LIT:0> : <EOL> self . pressed_control = QtGui . QStyle . SC_SliderHandle <EOL> self . click_offset = self . __pixelPosToRangeValue ( self . __pick ( event . pos ( ) ) ) <EOL> self . triggerAction ( self . SliderMove ) <EOL> self . setRepeatAction ( self . SliderNoAction ) <EOL> else : <EOL> event . ignore ( ) <EOL> def mouseMoveEvent ( self , event ) : <EOL> if self . pressed_control != QtGui . QStyle . SC_SliderHandle : <EOL> event . ignore ( ) <EOL> return <EOL> event . accept ( ) <EOL> new_pos = self . __pixelPosToRangeValue ( self . __pick ( event . pos ( ) ) ) <EOL> opt = QtGui . QStyleOptionSlider ( ) <EOL> self . initStyleOption ( opt ) <EOL> if self . active_slider < <NUM_LIT:0> : <EOL> offset = new_pos - self . click_offset <EOL> self . _high += offset <EOL> self . _low += offset <EOL> if self . _low < self . minimum ( ) : <EOL> diff = self . minimum ( ) - self . _low <EOL> self . _low += diff <EOL> self . _high += diff <EOL> if self . _high > self . maximum ( ) : <EOL> diff = self . maximum ( ) - self . _high <EOL> self . _low += diff <EOL> self . _high += diff <EOL> elif self . active_slider == <NUM_LIT:0> : <EOL> if new_pos >= self . _high : <EOL> new_pos = self . _high - <NUM_LIT:1> <EOL> self . _low = new_pos <EOL> else : <EOL> if new_pos <= self . _low : <EOL> new_pos = self . _low + <NUM_LIT:1> <EOL> self . _high = new_pos <EOL> self . click_offset = new_pos <EOL> self . update ( ) <EOL> self . emit ( QtCore . SIGNAL ( '<STR_LIT>' ) , new_pos ) <EOL> def __pick ( self , pt ) : <EOL> if self . orientation ( ) == QtCore . Qt . Horizontal : <EOL> return pt . x ( ) <EOL> else : <EOL> return pt . y ( ) <EOL> def __pixelPosToRangeValue ( self , pos ) : <EOL> opt = QtGui . QStyleOptionSlider ( ) <EOL> self . initStyleOption ( opt ) <EOL> style = QtGui . QApplication . style ( ) <EOL> gr = style . subControlRect ( style . CC_Slider , opt , style . SC_SliderGroove , self ) <EOL> sr = style . subControlRect ( style . CC_Slider , opt , style . SC_SliderHandle , self ) <EOL> if self . orientation ( ) == QtCore . Qt . Horizontal : <EOL> slider_length = sr . width ( ) <EOL> slider_min = gr . x ( ) <EOL> slider_max = gr . right ( ) - slider_length + <NUM_LIT:1> <EOL> else : <EOL> slider_length = sr . height ( ) <EOL> slider_min = gr . y ( ) <EOL> slider_max = gr . bottom ( ) - slider_length + <NUM_LIT:1> <EOL> return style . sliderValueFromPosition ( self . minimum ( ) , self . maximum ( ) , <EOL> pos - slider_min , slider_max - slider_min , <EOL> opt . upsideDown ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> def echo ( value ) : <EOL> print value <EOL> app = QtGui . QApplication ( sys . argv ) <EOL> slider = RangeSlider ( ) <EOL> slider . setMinimum ( <NUM_LIT:0> ) <EOL> slider . setMaximum ( <NUM_LIT> ) <EOL> slider . setLow ( <NUM_LIT:0> ) <EOL> slider . setHigh ( <NUM_LIT> ) <EOL> QtCore . QObject . connect ( slider , QtCore . SIGNAL ( '<STR_LIT>' ) , echo ) <EOL> slider . show ( ) <EOL> app . exec_ ( ) </s>
