|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from PySide6.QtCore import QPoint, Qt |
| 4 | +from PySide6.QtWidgets import QDoubleSpinBox, QSpinBox |
| 5 | + |
| 6 | + |
| 7 | +class _ScrubMixin: |
| 8 | + """ |
| 9 | + Shared scrubbing behavior for spinboxes. |
| 10 | +
|
| 11 | + Requires the subclass to implement: |
| 12 | + - _scrub_get_value() -> float |
| 13 | + - _scrub_set_value(v: float) -> None |
| 14 | + - _scrub_get_step() -> float |
| 15 | + - _scrub_coerce_step(step: float) -> float |
| 16 | + """ |
| 17 | + |
| 18 | + def _scrub_init(self, *, scrub_button=Qt.LeftButton, pixels_per_step: int = 6) -> None: |
| 19 | + self._scrub_button = scrub_button |
| 20 | + self._pixels_per_step = max(1, int(pixels_per_step)) |
| 21 | + self._dragging = False |
| 22 | + self._press_pos: QPoint | None = None |
| 23 | + self._press_value: float = 0.0 |
| 24 | + self._accum_dx: int = 0 |
| 25 | + |
| 26 | + # Nice UX: don’t immediately rewrite value while typing |
| 27 | + self.setKeyboardTracking(False) |
| 28 | + |
| 29 | + # Optional: give a hint cursor |
| 30 | + self.setCursor(Qt.SizeHorCursor) |
| 31 | + |
| 32 | + # Initialize tooltip (keeps your pattern) |
| 33 | + self.setToolTip("") |
| 34 | + |
| 35 | + def setToolTip(self, text: str, disable_instructions: bool = False) -> None: |
| 36 | + """Override to optionally include scrubbing instructions in the tooltip. |
| 37 | +
|
| 38 | + Args: |
| 39 | + text: The main tooltip text to show (can be empty). |
| 40 | + disable_instructions: If True, do not include scrubbing instructions in the tooltip. |
| 41 | + """ |
| 42 | + if disable_instructions: |
| 43 | + super().setToolTip(text) |
| 44 | + return |
| 45 | + |
| 46 | + # Add usage instructions to the tooltip (real HTML, not escaped entities) |
| 47 | + scrub_hint = "<i>Drag to adjust Shift=slow Ctrl=fast</i>" |
| 48 | + |
| 49 | + if not text: |
| 50 | + super().setToolTip(f"<qt>{scrub_hint}</qt>") |
| 51 | + else: |
| 52 | + super().setToolTip(f"<qt>{text}<br>{scrub_hint}</qt>") |
| 53 | + |
| 54 | + def mousePressEvent(self, event): |
| 55 | + if event.button() == self._scrub_button: |
| 56 | + self._press_pos = event.pos() |
| 57 | + self._press_value = float(self._scrub_get_value()) |
| 58 | + self._accum_dx = 0 |
| 59 | + self._dragging = False |
| 60 | + event.accept() |
| 61 | + return |
| 62 | + super().mousePressEvent(event) |
| 63 | + |
| 64 | + def mouseMoveEvent(self, event): |
| 65 | + if self._press_pos is None: |
| 66 | + super().mouseMoveEvent(event) |
| 67 | + return |
| 68 | + |
| 69 | + dx = event.pos().x() - self._press_pos.x() |
| 70 | + |
| 71 | + # Only start scrubbing after a small threshold to preserve normal click-to-edit behavior |
| 72 | + if not self._dragging and abs(dx) < 3: |
| 73 | + super().mouseMoveEvent(event) |
| 74 | + return |
| 75 | + |
| 76 | + self._dragging = True |
| 77 | + |
| 78 | + # Convert pixel movement into steps (with accumulation so it feels smooth) |
| 79 | + self._accum_dx += dx |
| 80 | + self._press_pos = event.pos() |
| 81 | + |
| 82 | + steps = int(self._accum_dx / self._pixels_per_step) |
| 83 | + if steps == 0: |
| 84 | + event.accept() |
| 85 | + return |
| 86 | + |
| 87 | + # Consume used delta |
| 88 | + self._accum_dx -= steps * self._pixels_per_step |
| 89 | + |
| 90 | + # Base step size comes from singleStep() |
| 91 | + step = float(self._scrub_get_step()) |
| 92 | + |
| 93 | + # Modifiers for fine/coarse control |
| 94 | + mods = event.modifiers() |
| 95 | + if mods & Qt.ShiftModifier: |
| 96 | + step *= 0.1 |
| 97 | + if mods & Qt.ControlModifier: |
| 98 | + step *= 10.0 |
| 99 | + |
| 100 | + # Type-specific step constraints (e.g., int step must stay >= 1) |
| 101 | + step = float(self._scrub_coerce_step(step)) |
| 102 | + |
| 103 | + new_value = float(self._scrub_get_value()) + steps * step |
| 104 | + self._scrub_set_value(new_value) |
| 105 | + event.accept() |
| 106 | + |
| 107 | + def mouseReleaseEvent(self, event): |
| 108 | + if self._press_pos is not None and event.button() == self._scrub_button: |
| 109 | + # If we were dragging, prevent the release from selecting text/clicking arrows etc. |
| 110 | + if self._dragging: |
| 111 | + event.accept() |
| 112 | + self._press_pos = None |
| 113 | + self._dragging = False |
| 114 | + return |
| 115 | + super().mouseReleaseEvent(event) |
| 116 | + |
| 117 | + |
| 118 | +class ScrubSpinBox(_ScrubMixin, QSpinBox): |
| 119 | + """ |
| 120 | + QSpinBox with click-drag scrubbing: |
| 121 | + - Drag horizontally to adjust the value |
| 122 | + - Shift: fine control |
| 123 | + - Ctrl: coarse control |
| 124 | + """ |
| 125 | + |
| 126 | + def __init__(self, *args, scrub_button=Qt.LeftButton, pixels_per_step=6, **kwargs): |
| 127 | + super().__init__(*args, **kwargs) |
| 128 | + self._scrub_init(scrub_button=scrub_button, pixels_per_step=pixels_per_step) |
| 129 | + |
| 130 | + # ---- type-specific hooks ---- |
| 131 | + def _scrub_get_value(self) -> float: |
| 132 | + return float(int(self.value())) |
| 133 | + |
| 134 | + def _scrub_set_value(self, v: float) -> None: |
| 135 | + self.setValue(int(round(v))) |
| 136 | + |
| 137 | + def _scrub_get_step(self) -> float: |
| 138 | + # QSpinBox.singleStep() is int |
| 139 | + return float(int(self.singleStep())) |
| 140 | + |
| 141 | + def _scrub_coerce_step(self, step: float) -> float: |
| 142 | + # For integers, ensure at least 1 step |
| 143 | + s = int(round(step)) |
| 144 | + return float(max(1, s)) |
| 145 | + |
| 146 | + |
| 147 | +class ScrubDoubleSpinBox(_ScrubMixin, QDoubleSpinBox): |
| 148 | + """ |
| 149 | + QDoubleSpinBox with click-drag scrubbing: |
| 150 | + - Drag horizontally to adjust the value |
| 151 | + - Shift: fine control |
| 152 | + - Ctrl: coarse control |
| 153 | + """ |
| 154 | + |
| 155 | + def __init__(self, *args, scrub_button=Qt.LeftButton, pixels_per_step=6, **kwargs): |
| 156 | + super().__init__(*args, **kwargs) |
| 157 | + self._scrub_init(scrub_button=scrub_button, pixels_per_step=pixels_per_step) |
| 158 | + |
| 159 | + # ---- type-specific hooks ---- |
| 160 | + def _scrub_get_value(self) -> float: |
| 161 | + return float(self.value()) |
| 162 | + |
| 163 | + def _scrub_set_value(self, v: float) -> None: |
| 164 | + self.setValue(float(v)) |
| 165 | + |
| 166 | + def _scrub_get_step(self) -> float: |
| 167 | + return float(self.singleStep()) |
| 168 | + |
| 169 | + def _scrub_coerce_step(self, step: float) -> float: |
| 170 | + # For doubles, allow fractional steps (but avoid zero) |
| 171 | + return step if abs(step) > 1e-12 else float(self.singleStep()) |
0 commit comments