Ejemplo n.º 1
0
def _test_FloatSlider_wheel():

    frame = wx.GetApp().GetTopWindow()
    slider = floatslider.FloatSlider(frame,
                                     value=50,
                                     minValue=0,
                                     maxValue=100,
                                     style=floatslider.FS_MOUSEWHEEL)

    called = [False]

    def handler(ev):
        called[0] = True

    slider.Bind(wx.EVT_SLIDER, handler)

    ev = mock.MagicMock()

    # UIActionSimulator don't do mouse wheel,
    # so we fake a mousewheel event
    val = slider.GetValue()
    ev.GetWheelRotation.return_value = 1
    slider._FloatSlider__onMouseWheel(ev)
    wx.Yield()
    assert called[0]
    assert slider.GetValue() > val

    val = slider.GetValue()

    called[0] = False
    ev.GetWheelRotation.return_value = -1
    slider._FloatSlider__onMouseWheel(ev)
    wx.Yield()
    assert called[0]
    assert slider.GetValue() < val

    val = slider.GetValue()

    called[0] = False
    ev.GetWheelRotation.return_value = 0
    slider._FloatSlider__onMouseWheel(ev)
    wx.Yield()
    assert not called[0]
    assert np.isclose(slider.GetValue(), val)
Ejemplo n.º 2
0
def _test_FloatSlider_mouse():

    sim = wx.UIActionSimulator()
    frame = wx.GetApp().GetTopWindow()
    minval = 0
    init = 50
    maxval = 100
    slider = floatslider.FloatSlider(frame,
                                     value=init,
                                     minValue=minval,
                                     maxValue=maxval)
    sizer = wx.BoxSizer(wx.HORIZONTAL)
    sizer.Add(slider, flag=wx.EXPAND, proportion=1)
    frame.SetSizer(sizer)
    frame.Layout()
    wx.Yield()

    called = [0]

    def handler(ev):
        called[0] += 1

    slider.Bind(wx.EVT_SLIDER, handler)

    xposes = [0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95]

    for xpos in xposes:

        called[0] = 0

        # Make sure clicking on the same spot
        # doesn't result in multiple events
        simclick(sim, slider, pos=[xpos, 0.5])

        expected = minval + xpos * (maxval - minval)

        # Be tolerant with the slider value,
        # because the above xposes may not
        # take into account widget borders.
        tol = (maxval - minval) * 0.05
        assert called[0] == 1
        assert abs(slider.GetValue() - expected) <= tol
Ejemplo n.º 3
0
def _test_FloatSlider_changeRange():
    frame = wx.GetApp().GetTopWindow()
    slider = floatslider.FloatSlider(frame)
    _test_widget_changeRange(slider)
Ejemplo n.º 4
0
def _test_FloatSlider_logic_integer():
    frame = wx.GetApp().GetTopWindow()
    slider = floatslider.FloatSlider(frame, style=floatslider.FS_INTEGER)
    _test_widget_logic_integer(slider)
Ejemplo n.º 5
0
def _test_FloatSlider_logic():
    frame = wx.GetApp().GetTopWindow()
    slider = floatslider.FloatSlider(frame)
    _test_widget_logic(slider)
Ejemplo n.º 6
0
def _makeSlider(parent, hasProps, propObj, propVal, showSpin, showLimits,
                editLimits, mousewheel, spinWidth):
    """Used by the :func:`_Number` function.

    Creates and returns a :class:`.FloatSlider` or :class:`.SliderSpinPanel`,
    and binds it to the given :class:`.PropertyValue` instance.

    See :func:`_Number` for details on the parameters.
    """

    value = propVal.get()
    minval = propVal.getAttribute('minval')
    maxval = propVal.getAttribute('maxval')
    limited = propVal.getAttribute('clamped')

    if isinstance(propObj, ptypes.Int): real = False
    elif isinstance(propObj, ptypes.Real): real = True

    if not showSpin:

        if mousewheel: style = floatslider.FS_MOUSEWHEEL
        else: style = 0

        if not real:
            style |= floatslider.FS_INTEGER

        evt = wx.EVT_SLIDER
        slider = floatslider.FloatSlider(parent,
                                         value=value,
                                         minValue=minval,
                                         maxValue=maxval,
                                         style=style)

    else:
        evt = floatslider.EVT_SSP_VALUE
        style = 0

        if not real: style |= floatslider.SSP_INTEGER
        if not limited: style |= floatslider.SSP_NO_LIMITS
        if showLimits: style |= floatslider.SSP_SHOW_LIMITS
        if editLimits: style |= floatslider.SSP_EDIT_LIMITS
        if mousewheel: style |= floatslider.SSP_MOUSEWHEEL

        slider = floatslider.SliderSpinPanel(parent,
                                             value=value,
                                             minValue=minval,
                                             maxValue=maxval,
                                             style=style,
                                             spinWidth=spinWidth)

    # bind the slider value to the property value
    widgets._propBind(hasProps, propObj, propVal, slider, evt)

    # Update slider min/max bounds and labels
    # whenever the property attributes change.
    def updateSliderRange(*a):
        minval = propVal.getAttribute('minval')
        maxval = propVal.getAttribute('maxval')

        log.debug('Updating {} range from {}.{}: {} - {}'.format(
            type(slider).__name__,
            type(hasProps).__name__, propVal._name, minval, maxval))

        slider.SetRange(minval, maxval)
        # TODO check that value has changed due to the range change?

    listenerName = 'widgets_number_py_updateRange_{}'.format(id(slider))
    propVal.addAttributeListener(listenerName, updateSliderRange, weak=False)

    # remove the listener when the slider is destroyed
    def onDestroy(ev):
        propVal.removeAttributeListener(listenerName)
        ev.Skip()

    slider.Bind(wx.EVT_WINDOW_DESTROY, onDestroy)

    if editLimits:

        # When the user edits the slider bounds,
        # update the property attributes
        def updatePropRange(ev):
            propVal.setAttribute('minval', ev.min)
            propVal.setAttribute('maxval', ev.max)

        slider.Bind(floatslider.EVT_SSP_LIMIT, updatePropRange)

    return slider