Skip to content

Commit

Permalink
broken, but not sure why yet
Browse files Browse the repository at this point in the history
  • Loading branch information
alisterburt committed Jun 21, 2023
1 parent 8f83091 commit 0599a91
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/superqt/sliders/_labeled.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _setValue(self, value: float):
self._slider.setMaximum(value)
elif self._slider.minimum() > value:
self._slider.setMinimum(value)
self._slider.setValue(int(value))
self._slider.setValue(value)

def _rename_signals(self):
# for subclasses
Expand Down Expand Up @@ -214,6 +214,10 @@ def setEdgeLabelMode(self, opt: EdgeLabelMode) -> None:

QApplication.processEvents()

def setLabelSpinBoxRange(self, min: int, max: int):
"""Change valid label values independently of slider range."""
self._label.setRange(int(min), int(max))


class QLabeledDoubleSlider(QLabeledSlider):
_slider_class = QDoubleSlider
Expand All @@ -227,9 +231,17 @@ def __init__(self, *args, **kwargs) -> None:
self.setDecimals(2)

def _setValue(self, value: float):
"""Convert the value from float to int before setting the slider value."""
"""Override method in the subclass which converts float to int."""
if self._slider.maximum() < value:
self._slider.setMaximum(value)
elif self._slider.minimum() > value:
self._slider.setMinimum(value)
self._slider.setValue(value)

def setLabelSpinBoxRange(self, min: float, max: float):
"""Change valid label values independently of slider range."""
self._label.setRange(min, max)

def _rename_signals(self):
self.valueChanged = self._fvalueChanged
self.sliderMoved = self._fsliderMoved
Expand Down Expand Up @@ -420,9 +432,9 @@ def _on_range_changed(self, min, max):
self._max_label.setValue(max)
self._reposition_labels()

# def setValue(self, value) -> None:
# super().setValue(value)
# self.sliderChange(QSlider.SliderValueChange)
def setValue(self, value) -> None:
super().setValue(value)
self.sliderChange(QSlider.SliderValueChange)

def setRange(self, min, max) -> None:
self._on_range_changed(min, max)
Expand Down Expand Up @@ -579,8 +591,8 @@ def setMode(self, opt: EdgeLabelMode):
with contextlib.suppress(Exception):
self._slider.rangeChanged.disconnect(self.setRange)
else:
self.setMinimum(self._slider.minimum())
self.setMaximum(self._slider.maximum())
self.setMinimum(min(self._slider.minimum(), self.minimum()))
self.setMaximum(max(self._slider.maximum(), self.maximum()))
self._slider.rangeChanged.connect(self.setRange)
self._update_size()

Expand Down
40 changes: 40 additions & 0 deletions tests/test_sliders/test_labeled_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,43 @@ def test_editing_float(qtbot):
slider._label.setValue(0.5)
slider._label.editingFinished.emit()
assert slider.value() == 0.5


@pytest.mark.parametrize("cls", [QLabeledSlider, QLabeledDoubleSlider])
def test_extended_label_spinbox_range(cls, qtbot):
slider = cls()
mock = Mock()
qtbot.addWidget(slider)
slider.setRange(0, 10)
slider.rangeChanged.connect(mock)
assert slider._label.minimum() == 0
assert slider._label.maximum() == 10
assert slider.minimum() == 0
assert slider.maximum() == 10

# changing slider value without changing spinbox range should return a clipped value
# slider._label.setValue(20)
# slider._label.editingFinished.emit() # simulate editing the label manually
# qtbot.wait(20)
# assert slider.value() == 10
# assert slider.maximum() == 10

# after changing label range, setting the label to a value outside the range should
# update the slider min/max as appropriate
slider.setLabelSpinBoxRange(-10, 20)

with qtbot.waitSignal(slider.rangeChanged):
slider._label.setValue(15)
assert slider._label.minimum() == -10
slider._label.editingFinished.emit() # simulate editing the label manually
assert slider._label.minimum() == -10
assert slider.value() == 15
assert slider.minimum() == 0 # unchanged
assert slider.maximum() == 15 # changed

with qtbot.waitSignal(slider.rangeChanged):
slider._label.setValue(-5)
slider._label.editingFinished.emit() # simulate editing the label manually
assert slider.value() == -5
assert slider.minimum() == -5 # changed
assert slider.maximum() == 15 # unchanged

0 comments on commit 0599a91

Please sign in to comment.