141 lines
5.1 KiB
Python
141 lines
5.1 KiB
Python
# Copyright (C) 2026 Seth Hillbrand
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""Parameter slider widget with label, slider, and spinbox."""
|
|
|
|
from PySide6.QtCore import Qt, Signal
|
|
from PySide6.QtWidgets import (
|
|
QWidget, QHBoxLayout, QLabel, QSlider, QSpinBox, QDoubleSpinBox, QSizePolicy,
|
|
)
|
|
|
|
|
|
class ParamSlider(QWidget):
|
|
"""Row layout with label, slider, and spinbox for adjusting a parameter."""
|
|
|
|
value_changed = Signal(float)
|
|
|
|
def __init__(self, label: str, min_val: float = 0, max_val: float = 100,
|
|
default: float = 50, step: float = 1, decimals: int = 0,
|
|
suffix: str = "", parent=None):
|
|
super().__init__(parent)
|
|
self._min = min_val
|
|
self._max = max_val
|
|
self._step = step
|
|
self._decimals = decimals
|
|
self._block_signals = False
|
|
|
|
layout = QHBoxLayout(self)
|
|
layout.setContentsMargins(0, 2, 0, 2)
|
|
layout.setSpacing(8)
|
|
|
|
self._label = QLabel(label)
|
|
self._label.setFixedWidth(110)
|
|
self._label.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
|
|
layout.addWidget(self._label)
|
|
|
|
self._slider = QSlider(Qt.Orientation.Horizontal)
|
|
self._slider.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
|
|
|
|
if decimals > 0:
|
|
self._scale = 10 ** decimals
|
|
self._slider.setMinimum(int(min_val * self._scale))
|
|
self._slider.setMaximum(int(max_val * self._scale))
|
|
self._slider.setSingleStep(int(step * self._scale))
|
|
self._slider.setValue(int(default * self._scale))
|
|
self._spin = QDoubleSpinBox()
|
|
self._spin.setDecimals(decimals)
|
|
self._spin.setMinimum(min_val)
|
|
self._spin.setMaximum(max_val)
|
|
self._spin.setSingleStep(step)
|
|
self._spin.setValue(default)
|
|
if suffix:
|
|
self._spin.setSuffix(suffix)
|
|
self._slider.valueChanged.connect(self._slider_moved_float)
|
|
self._spin.valueChanged.connect(self._spin_changed_float)
|
|
else:
|
|
self._scale = 1
|
|
self._slider.setMinimum(int(min_val))
|
|
self._slider.setMaximum(int(max_val))
|
|
self._slider.setSingleStep(int(step))
|
|
self._slider.setValue(int(default))
|
|
self._spin = QSpinBox()
|
|
self._spin.setMinimum(int(min_val))
|
|
self._spin.setMaximum(int(max_val))
|
|
self._spin.setSingleStep(int(step))
|
|
self._spin.setValue(int(default))
|
|
if suffix:
|
|
self._spin.setSuffix(suffix)
|
|
self._slider.valueChanged.connect(self._slider_moved_int)
|
|
self._spin.valueChanged.connect(self._spin_changed_int)
|
|
|
|
layout.addWidget(self._slider, 1)
|
|
self._spin.setFixedWidth(70)
|
|
self._spin.setAlignment(Qt.AlignmentFlag.AlignRight)
|
|
layout.addWidget(self._spin)
|
|
|
|
def _slider_moved_int(self, val: int):
|
|
if self._block_signals:
|
|
return
|
|
self._block_signals = True
|
|
self._spin.setValue(val)
|
|
self._block_signals = False
|
|
self.value_changed.emit(float(val))
|
|
|
|
def _spin_changed_int(self, val: int):
|
|
if self._block_signals:
|
|
return
|
|
self._block_signals = True
|
|
self._slider.setValue(val)
|
|
self._block_signals = False
|
|
self.value_changed.emit(float(val))
|
|
|
|
def _slider_moved_float(self, val: int):
|
|
if self._block_signals:
|
|
return
|
|
fval = val / self._scale
|
|
self._block_signals = True
|
|
self._spin.setValue(fval)
|
|
self._block_signals = False
|
|
self.value_changed.emit(fval)
|
|
|
|
def _spin_changed_float(self, val: float):
|
|
if self._block_signals:
|
|
return
|
|
self._block_signals = True
|
|
self._slider.setValue(int(val * self._scale))
|
|
self._block_signals = False
|
|
self.value_changed.emit(val)
|
|
|
|
def value(self) -> float:
|
|
if self._decimals > 0:
|
|
return self._spin.value()
|
|
return float(self._spin.value())
|
|
|
|
def set_value(self, val: float, emit: bool = True):
|
|
self._block_signals = True
|
|
if self._decimals > 0:
|
|
self._spin.setValue(val)
|
|
self._slider.setValue(int(val * self._scale))
|
|
else:
|
|
self._spin.setValue(int(val))
|
|
self._slider.setValue(int(val))
|
|
self._block_signals = False
|
|
if emit:
|
|
self.value_changed.emit(val)
|
|
|
|
def set_enabled(self, enabled: bool):
|
|
self._slider.setEnabled(enabled)
|
|
self._spin.setEnabled(enabled)
|