Skip to content

Commit

Permalink
New widget CueLabelSlider both int and float slider (#1280)
Browse files Browse the repository at this point in the history
* feat: New widget `CueLabelSlider` both int and float slider

* fix: remove feature from another branch

* Add `signals`, `getter` and `setter`

This will be used in branch `cuesubmit-jobs-from-config-file`, done here to avoid pulling the whole widget

Signed-off-by: Kern Attila GERMAIN <[email protected]>

---------

Signed-off-by: Kern Attila GERMAIN <[email protected]>
  • Loading branch information
KernAttila authored May 3, 2024
1 parent 38671f0 commit ccd3a15
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions cuesubmit/cuesubmit/ui/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,84 @@ def __init__(self, spacerType, width=20, height=40):
"""
super(CueSpacerItem, self).__init__(width, height, spacerType[0], spacerType[1])

class CueLabelSlider(QtWidgets.QWidget):
"""Container widget that holds a label and an int or float slider.
Behaves as a float slider when providing a float_precision
"""

valueChanged = QtCore.Signal(int)
sliderMoved = QtCore.Signal(int)
sliderReleased = QtCore.Signal()
actionTriggered = QtCore.Signal(int)
rangeChanged = QtCore.Signal(int, int)

def __init__(self, label=None, parent=None,
default_value=0,
min_value=0,
max_value=999,
float_precision=None):
super(CueLabelSlider, self).__init__(parent=parent)
self._labelValue = f'{label} ({{value}})'
self.float_mult = 1
if float_precision:
self.float_mult = 10**float_precision
self.mainLayout = QtWidgets.QHBoxLayout()
self.label = QtWidgets.QLabel(self._labelValue.format(value=default_value), parent=self)
self.label.setMinimumWidth(120)
self.label.setAlignment(QtCore.Qt.AlignVCenter)
self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal, parent=self)
self.slider.setMinimumWidth(120)
self.slider.setMinimum(min_value*self.float_mult)
self.slider.setMaximum(max_value*self.float_mult)
self.setValue(default_value)
self.slider.setSingleStep(1)
self.signals = [self.valueChanged]
self.getter = self.getValue
self.setter = self.setValue
self.setupUi()
self.setupConnections()

def setupUi(self):
"""Creates the widget layout."""
self.setLayout(self.mainLayout)
self.mainLayout.addWidget(self.label)
self.mainLayout.addWidget(self.slider)

def setupConnections(self):
"""Sets up widget signals."""
self.valueChanged.connect(self.updateLabelValue)
self.slider.valueChanged.connect(self.valueChanged.emit)
self.slider.sliderMoved.connect(self.sliderMoved.emit)
self.slider.sliderReleased.connect(self.sliderReleased.emit)
self.slider.actionTriggered.connect(self.actionTriggered.emit)
self.slider.rangeChanged.connect(self.rangeChanged.emit)

def updateLabelValue(self, value):
""" Updates the label with the slider's value at the end
:param value: current slider integer value
:type value: int
"""
if self.float_mult!=1:
value = value*1./self.float_mult
self.label.setText(self._labelValue.format(value=value))

def getValue(self):
""" Query the slider's value
:returns: slider's value
:rtype: int or float
"""
if self.float_mult!=1:
return self.slider.value()*1./self.float_mult
return self.slider.value()

def setValue(self, value):
""" Set the slider's value (consider the float multiplier)
:param value: current slider integer value
:type value: int
"""
self.slider.setValue(value*self.float_mult)

class CueLabelToggle(QtWidgets.QWidget):
"""Container widget that holds a label and a toggle."""
Expand Down

0 comments on commit ccd3a15

Please sign in to comment.