diff --git a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py index bfdd8e91..f4d1c9e1 100644 --- a/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py +++ b/csaxs_bec/bec_ipython_client/plugins/LamNI/lamni_alignment_mixin.py @@ -64,10 +64,12 @@ class LamNIAlignmentMixin: # ------------------------------------------------------------------ def reset_correction(self): - """Reset the look-up-table corrections to empty (iteration 1).""" + """Reset the look-up-table corrections to empty (iterations 1 and 2), + same as FlOMNI's reset_correction().""" self.corr_pos_x = [] self.corr_pos_y = [] self.corr_angle = [] + self.reset_correction_2() def reset_correction_2(self): """Reset the second iteration look-up-table correction to empty.""" @@ -90,6 +92,8 @@ class LamNIAlignmentMixin: existing.append("X-ray eye alignment fit") if self.corr_pos_x: existing.append("fine-alignment correction") + if self.corr_pos_x_2: + existing.append("fine-alignment correction 2") return existing # ------------------------------------------------------------------ @@ -102,8 +106,8 @@ class LamNIAlignmentMixin: # to be the same data, just the same persistence pattern. # # No default correction file exists for LamNI (unlike FlOMNI's - # default_correction_file): reset_correction()/reset_correction_2() just - # clear to empty, nothing is auto-loaded. + # default_correction_file): reset_correction() clears both iterations, + # reset_correction_2() only the second, nothing is auto-loaded. # ------------------------------------------------------------------ @property diff --git a/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py b/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py index d47410e5..fe38600e 100644 --- a/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py +++ b/csaxs_bec/bec_widgets/widgets/tomo_params/tomo_params.py @@ -52,8 +52,10 @@ from typing import Any, Optional from bec_lib import bec_logger from bec_lib.endpoints import MessageEndpoints from bec_widgets import BECWidget, SafeSlot -from qtpy.QtCore import Qt, QTimer +from qtpy.QtCore import QEvent, QObject, Qt, QTimer from qtpy.QtWidgets import ( + QAbstractSpinBox, + QApplication, QCheckBox, QComboBox, QDialog, @@ -292,6 +294,30 @@ def _hline() -> QFrame: return line +class _NoWheelFilter(QObject): + """Stop the mouse wheel from changing spin/combo box values; the wheel + event goes to the scroll area instead, so scrolling the panel over a + field scrolls the panel and cannot change a value unnoticed.""" + + def __init__(self, viewport: QWidget): + super().__init__(viewport) + self._viewport = viewport + + def eventFilter(self, obj, event): + if event.type() == QEvent.Type.Wheel: + QApplication.sendEvent(self._viewport, event) + return True + return super().eventFilter(obj, event) + + +def _block_wheel_edits(container: QWidget, viewport: QWidget) -> None: + """Install a _NoWheelFilter on every spin and combo box in *container*.""" + wheel_filter = _NoWheelFilter(viewport) + for w in container.findChildren(QAbstractSpinBox) + container.findChildren(QComboBox): + w.setFocusPolicy(Qt.FocusPolicy.StrongFocus) + w.installEventFilter(wheel_filter) + + # ── main widget ────────────────────────────────────────────────────────────── @@ -470,6 +496,7 @@ class OMNY_TomoParams(BECWidget, QWidget): params_vbox.addLayout(self._build_edit_buttons()) params_vbox.addStretch() params_scroll.setWidget(params_inner) + _block_wheel_edits(params_inner, params_scroll.viewport()) root.addWidget(params_scroll) self._btn_queue = QPushButton("☰ Queue control…") diff --git a/tests/tests_bec_ipython_client/test_lamni_reset_correction.py b/tests/tests_bec_ipython_client/test_lamni_reset_correction.py new file mode 100644 index 00000000..aaa6ce60 --- /dev/null +++ b/tests/tests_bec_ipython_client/test_lamni_reset_correction.py @@ -0,0 +1,55 @@ +"""lamni.reset_correction() must clear both lookup-table iterations, so a +correction 2 from the previous sample is not applied to the next one.""" + +from csaxs_bec.bec_ipython_client.plugins.LamNI.lamni import LamNI + + +class FakeClient: + """Minimal in-memory stand-in for BEC's global-var store.""" + + def __init__(self): + self._vars = {} + + def get_global_var(self, key): + return self._vars.get(key) + + def set_global_var(self, key, value): + self._vars[key] = value + + +def make_lamni(): + obj = object.__new__(LamNI) + obj.client = FakeClient() + return obj + + +def _fill(lamni): + lamni.corr_pos_x, lamni.corr_pos_y, lamni.corr_angle = [0.1], [0.2], [0.0] + lamni.corr_pos_x_2, lamni.corr_pos_y_2, lamni.corr_angle_2 = [0.3], [0.4], [90.0] + + +def test_reset_correction_clears_both_iterations(): + lamni = make_lamni() + _fill(lamni) + + lamni.reset_correction() + + assert lamni.corr_pos_x == lamni.corr_pos_y == lamni.corr_angle == [] + assert lamni.corr_pos_x_2 == lamni.corr_pos_y_2 == lamni.corr_angle_2 == [] + assert lamni.compute_additional_correction_2(90.0) == (0, 0) + + +def test_reset_correction_2_keeps_iteration_1(): + lamni = make_lamni() + _fill(lamni) + + lamni.reset_correction_2() + + assert lamni.corr_pos_x == [0.1] + assert lamni.corr_pos_x_2 == [] + + +def test_existing_alignment_reports_correction_2(): + lamni = make_lamni() + lamni.corr_pos_x_2 = [0.3] + assert "fine-alignment correction 2" in lamni._existing_alignment() diff --git a/tests/tests_bec_widgets/test_tomo_params_no_wheel.py b/tests/tests_bec_widgets/test_tomo_params_no_wheel.py new file mode 100644 index 00000000..7c157260 --- /dev/null +++ b/tests/tests_bec_widgets/test_tomo_params_no_wheel.py @@ -0,0 +1,90 @@ +"""The mouse wheel must not change tomo_params values: users scroll the +panel and fields changed without anyone noticing. The full widget needs a +live BEC client, so this exercises _block_wheel_edits() on a plain scroll +area, the same setup as OMNY_TomoParams._build_ui().""" + +import os + +import pytest + +os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") + +from qtpy.QtCore import QPoint, QPointF, Qt # noqa: E402 +from qtpy.QtGui import QWheelEvent # noqa: E402 +from qtpy.QtWidgets import ( # noqa: E402 + QApplication, + QComboBox, + QDoubleSpinBox, + QScrollArea, + QSpinBox, + QVBoxLayout, + QWidget, +) + +from csaxs_bec.bec_widgets.widgets.tomo_params.tomo_params import ( # noqa: E402 + _block_wheel_edits, +) + + +@pytest.fixture(scope="module") +def qapp(): + return QApplication.instance() or QApplication([]) + + +def _wheel(widget, delta=120): + event = QWheelEvent( + QPointF(5, 5), + QPointF(widget.mapToGlobal(QPoint(5, 5))), + QPoint(0, 0), + QPoint(0, delta), + Qt.MouseButton.NoButton, + Qt.KeyboardModifier.NoModifier, + Qt.ScrollPhase.NoScrollPhase, + False, + ) + QApplication.sendEvent(widget, event) + + +def _panel(): + scroll = QScrollArea() + scroll.setWidgetResizable(True) + inner = QWidget() + layout = QVBoxLayout(inner) + dspin, ispin, combo = QDoubleSpinBox(), QSpinBox(), QComboBox() + dspin.setRange(0, 100) + dspin.setValue(50) + ispin.setRange(0, 100) + ispin.setValue(50) + combo.addItems(["1", "2", "3"]) + combo.setCurrentIndex(1) + for w in (dspin, ispin, combo): + layout.addWidget(w) + scroll.setWidget(inner) + return scroll, inner, dspin, ispin, combo + + +def test_wheel_changes_values_without_filter(qapp): + # sanity check that the synthetic wheel event does reach the widgets + scroll, _, dspin, ispin, combo = _panel() # keep scroll alive + for w in (dspin, ispin, combo): + _wheel(w, delta=-120) + assert dspin.value() != 50 or ispin.value() != 50 or combo.currentIndex() != 1 + + +def test_wheel_does_not_change_values(qapp): + scroll, inner, dspin, ispin, combo = _panel() + _block_wheel_edits(inner, scroll.viewport()) + for w in (dspin, ispin, combo): + _wheel(w) + _wheel(w, delta=-120) + assert dspin.value() == 50 + assert ispin.value() == 50 + assert combo.currentIndex() == 1 + assert dspin.focusPolicy() == Qt.FocusPolicy.StrongFocus + + +def test_keyboard_still_edits(qapp): + scroll, inner, dspin, _, _ = _panel() + _block_wheel_edits(inner, scroll.viewport()) + dspin.stepUp() + assert dspin.value() == 51