Skip to content

Commit 967fa6a

Browse files
authored
fix: Disable preset combo scroll (#432)
* feat: Prevent scrolling preset comboboxes * Add a test
1 parent e77f8b3 commit 967fa6a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/pymmcore_widgets/control/_presets_widget.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44

55
from pymmcore_plus import CMMCorePlus, DeviceType
66
from qtpy.QtCore import Qt
7-
from qtpy.QtGui import QBrush
7+
from qtpy.QtGui import QBrush, QWheelEvent
88
from qtpy.QtWidgets import QComboBox, QHBoxLayout, QWidget
99
from superqt.utils import signals_blocked
1010

1111
from pymmcore_widgets._util import block_core
1212

1313

14+
class _PresetComboBox(QComboBox):
15+
"""A QComboBox tailored to selecting group presets."""
16+
17+
def wheelEvent(self, e: QWheelEvent | None) -> None:
18+
# Scrolling over the combobox can easily lead to accidents, e.g. switching the
19+
# objective lens.
20+
pass
21+
22+
1423
class PresetsWidget(QWidget):
1524
"""A Widget to create a QCombobox containing the presets of the specified group.
1625
@@ -52,7 +61,7 @@ def __init__(
5261
# since they must be all the same
5362
self.dev_prop = self._get_preset_dev_prop(self._group, self._presets[0])
5463

55-
self._combo = QComboBox()
64+
self._combo = _PresetComboBox()
5665
self._combo.currentTextChanged.connect(self._update_tooltip)
5766
self._combo.addItems(self._presets)
5867
self._combo.setCurrentText(self._mmc.getCurrentConfig(self._group))

tests/test_presets_widget.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import TYPE_CHECKING
44

55
import pytest
6+
from qtpy.QtCore import QPoint, QPointF, Qt
7+
from qtpy.QtGui import QWheelEvent
68

79
from pymmcore_widgets.control._presets_widget import PresetsWidget
810

@@ -79,3 +81,25 @@ def test_preset_widget(qtbot: QtBot, global_mmcore: CMMCorePlus) -> None:
7981

8082
global_mmcore.deleteConfigGroup("Camera")
8183
assert "Camera" not in global_mmcore.getAvailableConfigGroups()
84+
85+
86+
def test_preset_widget_no_scroll(qtbot: QtBot, global_mmcore: CMMCorePlus) -> None:
87+
# Test that the widget does not respond to scroll events when unfocused.
88+
# It is easy to accidentally scroll the widget when trying to scroll the group
89+
# preset window, which could lead to costly accidents (e.g. switching objectives)
90+
wdg = PresetsWidget("Camera")
91+
qtbot.addWidget(wdg)
92+
93+
previous = wdg._combo.currentText()
94+
e = QWheelEvent(
95+
QPointF(0, 0), # pos
96+
QPointF(0, 0), # globalPos
97+
QPoint(0, 0), # pixelDelta
98+
QPoint(0, -120), # angleDelta
99+
Qt.MouseButton.NoButton, # buttons
100+
Qt.KeyboardModifier.NoModifier, # modifiers
101+
Qt.ScrollPhase.NoScrollPhase, # phase
102+
False, # inverted
103+
)
104+
wdg._combo.wheelEvent(e)
105+
assert wdg._combo.currentText() == previous

0 commit comments

Comments
 (0)