Coverage for graphica/gui/widgets/slider_spin.py: 98.0%

47 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-25 19:22 +0000

1"""スライダー+今のスピンボックス。値の持ち主はスピンボックスのまま(バインディングはそちらを見る)。""" 

2from PySide6.QtCore import QEvent, Qt 

3from PySide6.QtWidgets import QAbstractSpinBox, QHBoxLayout, QSlider, QWidget 

4 

5SPIN_WIDTH = 56 

6 

7 

8class SliderSpin(QWidget): 

9 """spinbox を中に取り込み、左にスライダーを付ける。 

10 

11 スライダーの範囲(slider_min〜slider_max)はよく使う範囲で、スピンボックスはそれより外の値も受け付ける 

12 (そのときスライダーは端に止まる)。スピンボックスがシグナルを止めて書き換えられた(バインディングの 

13 「戻す」)後は、refresh() でスライダーを合わせる。 

14 """ 

15 

16 def __init__(self, spinbox, slider_min, slider_max, step=None, parent=None): 

17 super().__init__(parent) 

18 self.spinbox = spinbox 

19 # QSpinBox(整数)には decimals() が無い 

20 self._scale = 10 ** (spinbox.decimals() if hasattr(spinbox, 'decimals') else 0) 

21 self.slider = QSlider(Qt.Orientation.Horizontal, self) 

22 self.slider.setObjectName("slider_spin_slider") 

23 self.slider.setRange(round(slider_min * self._scale), round(slider_max * self._scale)) 

24 self.slider.setSingleStep(max(1, round((step or spinbox.singleStep()) * self._scale))) 

25 self.slider.setCursor(Qt.CursorShape.PointingHandCursor) 

26 layout = QHBoxLayout(self) 

27 layout.setContentsMargins(0, 0, 0, 0) 

28 layout.setSpacing(10) 

29 layout.addWidget(self.slider, 1) 

30 layout.addWidget(spinbox) 

31 # 見本どおり、上下の矢印を消した数値だけの小さな欄にする(矢印の分の余白は QSS の sliderValue で外す) 

32 spinbox.setButtonSymbols(QAbstractSpinBox.ButtonSymbols.NoButtons) 

33 spinbox.setAlignment(Qt.AlignmentFlag.AlignCenter) 

34 spinbox.setProperty("sliderValue", True) 

35 # 既に表示に使われた部品は、プロパティを変えても QSS を当て直さないと見た目が変わらない 

36 spinbox.style().unpolish(spinbox) 

37 spinbox.style().polish(spinbox) 

38 spinbox.setFixedWidth(SPIN_WIDTH) 

39 self.slider.valueChanged.connect(self._on_slider_moved) 

40 spinbox.valueChanged.connect(lambda _value: self.refresh()) 

41 # 行の出し入れは数値の欄(値の持ち主)に対して行われるので、それに合わせてスライダーごと出し入れする 

42 spinbox.installEventFilter(self) 

43 self.refresh() 

44 

45 def eventFilter(self, watched, event): 

46 if watched is self.spinbox and event.type() in (QEvent.Type.HideToParent, QEvent.Type.ShowToParent): 

47 self.setVisible(event.type() == QEvent.Type.ShowToParent) 

48 return super().eventFilter(watched, event) 

49 

50 def setVisible(self, visible): 

51 # 行ごと出し直されても、数値の欄が隠されたままなら出ない(欄はもう隠れているので、隠す合図が来ない) 

52 spinbox = self.spinbox 

53 spinbox_hidden = (spinbox.testAttribute(Qt.WidgetAttribute.WA_WState_ExplicitShowHide) 

54 and spinbox.testAttribute(Qt.WidgetAttribute.WA_WState_Hidden)) 

55 super().setVisible(visible and not spinbox_hidden) 

56 

57 def refresh(self): 

58 blocked = self.slider.blockSignals(True) 

59 self.slider.setValue(round(self.spinbox.value() * self._scale)) 

60 self.slider.blockSignals(blocked) 

61 

62 def _on_slider_moved(self, position): 

63 value = position / self._scale 

64 if abs(value - self.spinbox.value()) >= 0.5 / self._scale: 64 ↛ exitline 64 didn't return from function '_on_slider_moved' because the condition on line 64 was always true

65 self.spinbox.setValue(value) 

66 

67 def setEnabled(self, enabled): 

68 super().setEnabled(enabled) 

69 self.spinbox.setEnabled(enabled)