Skip to content

Commit

Permalink
fix: fix issues with SliderLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Jun 3, 2024
1 parent 1f4d908 commit fc960ca
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/superqt/sliders/_labeled.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import contextlib
from enum import IntEnum, IntFlag, auto
from functools import partial
from typing import Any, Iterable, overload
from typing import Any, Iterable, cast, overload

from qtpy import QtGui
from qtpy.QtCore import Property, QPoint, QSize, Qt, Signal
Expand Down Expand Up @@ -183,7 +182,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.setFocusPolicy(Qt.FocusPolicy(fp))

self._slider = self._slider_class(parent=self)
self._label = SliderLabel(self._slider, connect=self._setValue, parent=self)
self._label = SliderLabel(self._slider, parent=self)
self._label.editingFinished.connect(self._on_label_edited)

self._edge_label_mode: EdgeLabelMode = EdgeLabelMode.LabelIsValue

self._rename_signals()
Expand Down Expand Up @@ -265,6 +266,8 @@ def setEdgeLabelMode(self, opt: EdgeLabelMode) -> None:
EdgeLabelMode = EdgeLabelMode

# --------------------- private api --------------------
def _on_label_edited(self) -> None:
self._setValue(self._label.value())

def _on_slider_range_changed(self, min_: int, max_: int) -> None:
slash = " / " if self._edge_label_mode & EdgeLabelMode.LabelIsValue else ""
Expand Down Expand Up @@ -359,15 +362,14 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.sliderMoved = self._slider._slidersMoved

self._min_label = SliderLabel(
self._slider,
alignment=Qt.AlignmentFlag.AlignLeft,
connect=self._min_label_edited,
self._slider, alignment=Qt.AlignmentFlag.AlignLeft
)
self._min_label.editingFinished.connect(self._min_label_edited)
self._max_label = SliderLabel(
self._slider,
alignment=Qt.AlignmentFlag.AlignRight,
connect=self._max_label_edited,
self._slider, parent=self, alignment=Qt.AlignmentFlag.AlignRight
)
self._max_label.editingFinished.connect(self._max_label_edited)

self._min_label.editingFinished.connect(self.editingFinished)
self._max_label.editingFinished.connect(self.editingFinished)
self.setEdgeLabelMode(EdgeLabelMode.LabelIsRange)
Expand Down Expand Up @@ -542,7 +544,8 @@ def _reposition_labels(self) -> None:
label.show()
self.update()

def _min_label_edited(self, val: float) -> None:
def _min_label_edited(self) -> None:
val = self._min_label.value()
if self._edge_label_mode == EdgeLabelMode.LabelIsRange:
self.setMinimum(val)
else:
Expand All @@ -551,7 +554,8 @@ def _min_label_edited(self, val: float) -> None:
self.setValue(v)
self._reposition_labels()

def _max_label_edited(self, val: float) -> None:
def _max_label_edited(self) -> None:
val = self._max_label.value()

Check warning on line 558 in src/superqt/sliders/_labeled.py

View check run for this annotation

Codecov / codecov/patch

src/superqt/sliders/_labeled.py#L558

Added line #L558 was not covered by tests
if self._edge_label_mode == EdgeLabelMode.LabelIsRange:
self.setMaximum(val)
else:
Expand All @@ -571,16 +575,20 @@ def _on_value_changed(self, v: tuple[int, ...]) -> None:
lbl.deleteLater()
self._handle_labels.clear()
for n, val in enumerate(self._slider.value()):
_cb = partial(self._slider.setSliderPosition, index=n)
s = SliderLabel(self._slider, parent=self, connect=_cb)
s.editingFinished.connect(self.editingFinished)
s = SliderLabel(self._slider, parent=self, index=n)
s.editingFinished.connect(self._on_slider_label_edited)
s.setValue(val)
self._handle_labels.append(s)
else:
for val, label in zip(v, self._handle_labels):
label.setValue(val)
self._reposition_labels()

def _on_slider_label_edited(self):
label = cast("SliderLabel", self.sender())
self._slider.setSliderPosition(label.value(), label.index)
self.editingFinished.emit()

Check warning on line 590 in src/superqt/sliders/_labeled.py

View check run for this annotation

Codecov / codecov/patch

src/superqt/sliders/_labeled.py#L588-L590

Added lines #L588 - L590 were not covered by tests

def _on_range_changed(self, min: int, max: int) -> None:
if (min, max) != (self._slider.minimum(), self._slider.maximum()):
self._slider.setRange(min, max)
Expand Down Expand Up @@ -640,12 +648,13 @@ class SliderLabel(QDoubleSpinBox):
def __init__(
self,
slider: QSlider,
parent=None,
alignment=Qt.AlignmentFlag.AlignCenter,
connect=None,
parent: QWidget | None = None,
alignment: Qt.AlignmentFlag = Qt.AlignmentFlag.AlignCenter,
index: int = 0,
) -> None:
super().__init__(parent=parent)
super().__init__(parent)
self._slider = slider
self.index = index
self.setFocusPolicy(Qt.FocusPolicy.ClickFocus)
self.setMode(EdgeLabelMode.LabelIsValue)
self.setDecimals(0)
Expand All @@ -655,8 +664,6 @@ def __init__(
self.setAlignment(alignment)
self.setButtonSymbols(QSpinBox.ButtonSymbols.NoButtons)
self.setStyleSheet("background:transparent; border: 0;")
if connect is not None:
self.editingFinished.connect(lambda: connect(self.value()))
self.editingFinished.connect(self._silent_clear_focus)
self._update_size()

Expand Down

0 comments on commit fc960ca

Please sign in to comment.