fix(tomo_params): select spin box value on focus so typed numbers replace it
CI for csaxs_bec / test (push) Successful in 5m10s
CI for csaxs_bec / test (push) Successful in 5m10s
Clicking into a spin box left the cursor at the end, so typed digits were appended and silently rejected when above the maximum (e.g. golden-ratio bunch size 100 + '5' -> 1005 > 200) or the decimal count, leaving only the arrows to change the value. Applies to all spin boxes in the parameter panel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017NrH3poJsYBCTVq2Y9fuHZ
This commit is contained in:
1 parent
ea74909ee0
commit
39a75750f6
2 files changed
+101
No files matched your search
@@ -321,6 +321,27 @@ def _block_wheel_edits(container: QWidget, viewport: QWidget) -> None:
|
||||
w.installEventFilter(wheel_filter)
|
||||
|
||||
|
||||
class _SelectAllOnFocusFilter(QObject):
|
||||
"""Select a spin box's whole text when it gets focus, so a typed number
|
||||
replaces the value. Otherwise a click leaves the cursor at the end and
|
||||
the typed digits are appended: e.g. bunch size 100 + "5" -> 1005, above
|
||||
the maximum, so the spin box silently rejects every keystroke."""
|
||||
|
||||
def eventFilter(self, obj, event):
|
||||
if event.type() == QEvent.Type.FocusIn:
|
||||
# deferred: the mouse press that gave focus places the cursor
|
||||
# (and clears the selection) after this event
|
||||
QTimer.singleShot(0, obj.selectAll)
|
||||
return super().eventFilter(obj, event)
|
||||
|
||||
|
||||
def _select_all_on_focus(container: QWidget) -> None:
|
||||
"""Install a _SelectAllOnFocusFilter on every spin box in *container*."""
|
||||
focus_filter = _SelectAllOnFocusFilter(container)
|
||||
for w in container.findChildren(QAbstractSpinBox):
|
||||
w.installEventFilter(focus_filter)
|
||||
|
||||
|
||||
# ── main widget ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -500,6 +521,7 @@ class OMNY_TomoParams(BECWidget, QWidget):
|
||||
params_vbox.addStretch()
|
||||
params_scroll.setWidget(params_inner)
|
||||
_block_wheel_edits(params_inner, params_scroll.viewport())
|
||||
_select_all_on_focus(params_inner)
|
||||
root.addWidget(params_scroll)
|
||||
|
||||
self._btn_queue = QPushButton("☰ Queue control…")
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
"""Clicking into a tomo_params spin box and typing must replace the value.
|
||||
Without select-on-focus the cursor sits at the end and digits are appended:
|
||||
golden-ratio bunch size 100 (range 30..200) + "5" -> 1005, which the spin
|
||||
box silently rejects, so only the arrows could change it. The full widget
|
||||
needs a live BEC client, so this exercises _select_all_on_focus() on a plain
|
||||
panel, the same setup as OMNY_TomoParams._build_ui()."""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from qtpy.QtCore import QPoint, Qt # noqa: E402
|
||||
from qtpy.QtTest import QTest # noqa: E402
|
||||
from qtpy.QtWidgets import ( # noqa: E402
|
||||
QApplication,
|
||||
QDoubleSpinBox,
|
||||
QLineEdit,
|
||||
QSpinBox,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from csaxs_bec.bec_widgets.widgets.tomo_params.tomo_params import ( # noqa: E402
|
||||
_select_all_on_focus,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def qapp():
|
||||
return QApplication.instance() or QApplication([])
|
||||
|
||||
|
||||
def _panel():
|
||||
panel = QWidget()
|
||||
layout = QVBoxLayout(panel)
|
||||
# takes the initial focus, so clicking a spin box is a real focus change
|
||||
layout.addWidget(QLineEdit())
|
||||
bunch = QSpinBox()
|
||||
bunch.setRange(30, 200)
|
||||
bunch.setValue(100)
|
||||
dspin = QDoubleSpinBox()
|
||||
dspin.setRange(0, 1e6)
|
||||
dspin.setDecimals(0)
|
||||
dspin.setValue(1000)
|
||||
layout.addWidget(bunch)
|
||||
layout.addWidget(dspin)
|
||||
return panel, bunch, dspin
|
||||
|
||||
|
||||
def _show(qapp, panel):
|
||||
panel.show()
|
||||
panel.activateWindow()
|
||||
qapp.processEvents()
|
||||
|
||||
|
||||
def _click_and_type(qapp, spin, text):
|
||||
le = spin.lineEdit()
|
||||
QTest.mouseClick(le, Qt.MouseButton.LeftButton, pos=QPoint(le.width() - 3, le.height() // 2))
|
||||
qapp.processEvents()
|
||||
QTest.keyClicks(spin, text)
|
||||
qapp.processEvents()
|
||||
return spin.text()
|
||||
|
||||
|
||||
def test_click_and_type_is_rejected_without_filter(qapp):
|
||||
# documents the original problem: the appended digit exceeds the maximum
|
||||
panel, bunch, _ = _panel()
|
||||
_show(qapp, panel)
|
||||
assert _click_and_type(qapp, bunch, "5") == "100"
|
||||
|
||||
|
||||
def test_click_and_type_replaces_value(qapp):
|
||||
panel, bunch, dspin = _panel()
|
||||
_select_all_on_focus(panel)
|
||||
_show(qapp, panel)
|
||||
assert _click_and_type(qapp, bunch, "150") == "150"
|
||||
assert _click_and_type(qapp, dspin, "42") == "42"
|
||||
Reference in new issue
Block a user