Skip to content

Commit

Permalink
feat: add step() and setStep() methods to StageWidget (#241)
Browse files Browse the repository at this point in the history
* feat: add step() method to StageWidget

* test: fix tests
  • Loading branch information
tlambert03 authored Nov 1, 2023
1 parent 00c9bd1 commit 308309e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
21 changes: 15 additions & 6 deletions src/pymmcore_widgets/_stage_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class StageWidget(QWidget):
Stage device.
levels: int | None:
Number of "arrow" buttons per widget per direction, by default, 2.
step: float | None:
Starting step size to use for the spinbox in the middle, by default, 10.
parent : QWidget | None
Optional parent widget.
mmcore : CMMCorePlus | None
Expand Down Expand Up @@ -96,6 +98,7 @@ def __init__(
device: str,
levels: int | None = 2,
*,
step: float = 10,
parent: QWidget | None = None,
mmcore: CMMCorePlus | None = None,
):
Expand All @@ -109,18 +112,24 @@ def __init__(
self._dtype = self._mmc.getDeviceType(self._device)
assert self._dtype in STAGE_DEVICES, f"{self._dtype} not in {STAGE_DEVICES}"

self._create_widget()

self._create_widget(step)
self._connect_events()

self._set_as_default()

self.destroyed.connect(self._disconnect)

def _create_widget(self) -> None:
def step(self) -> float:
"""Return the current step size."""
return self._step.value() # type: ignore

def setStep(self, step: float) -> None:
"""Set the step size."""
self._step.setValue(step)

def _create_widget(self, step: float) -> None:
self._step = QDoubleSpinBox()
self._step.setValue(10)
self._step.setMaximum(9999)
self._step.setValue(step)
self._step.setMaximum(99999)
self._step.valueChanged.connect(self._update_ttips)
self._step.clearFocus()
self._step.setAttribute(Qt.WidgetAttribute.WA_MacShowFocusRect, 0)
Expand Down
21 changes: 10 additions & 11 deletions tests/test_stage_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
def test_stage_widget(qtbot: QtBot, global_mmcore: CMMCorePlus):
# test XY stage
stage_xy = StageWidget("XY", levels=3)

qtbot.addWidget(stage_xy)

assert global_mmcore.getXYStageDevice() == "XY"
Expand All @@ -26,8 +25,8 @@ def test_stage_widget(qtbot: QtBot, global_mmcore: CMMCorePlus):
assert global_mmcore.getXYStageDevice() == "XY"
assert stage_xy.radiobutton.isChecked()

stage_xy._step.setValue(5.0)
assert stage_xy._step.value() == 5.0
stage_xy.setStep(5.0)
assert stage_xy.step() == 5.0
assert stage_xy._readout.text() == "XY: -0.0, -0.0"

x_pos = global_mmcore.getXPosition()
Expand All @@ -38,9 +37,9 @@ def test_stage_widget(qtbot: QtBot, global_mmcore: CMMCorePlus):
xy_up_3 = stage_xy._btns.layout().itemAtPosition(0, 3)
xy_up_3.widget().click()
assert (
(y_pos + (stage_xy._step.value() * 3)) - 1
(y_pos + (stage_xy.step() * 3)) - 1
< global_mmcore.getYPosition()
< (y_pos + (stage_xy._step.value() * 3)) + 1
< (y_pos + (stage_xy.step() * 3)) + 1
)
label_x = round(global_mmcore.getXPosition(), 2)
label_y = round(global_mmcore.getYPosition(), 2)
Expand All @@ -50,9 +49,9 @@ def test_stage_widget(qtbot: QtBot, global_mmcore: CMMCorePlus):
global_mmcore.waitForDevice("XY")
xy_left_1.widget().click()
assert (
(x_pos - stage_xy._step.value()) - 1
(x_pos - stage_xy.step()) - 1
< global_mmcore.getXPosition()
< (x_pos - stage_xy._step.value()) + 1
< (x_pos - stage_xy.step()) + 1
)
label_x = round(global_mmcore.getXPosition(), 2)
label_y = round(global_mmcore.getYPosition(), 2)
Expand Down Expand Up @@ -90,8 +89,8 @@ def test_stage_widget(qtbot: QtBot, global_mmcore: CMMCorePlus):
assert stage_z.radiobutton.isChecked()
assert not stage_z1.radiobutton.isChecked()

stage_z._step.setValue(15.0)
assert stage_z._step.value() == 15.0
stage_z.setStep(15.0)
assert stage_z.step() == 15.0
assert stage_z._readout.text() == "Z: 0.0"

z_pos = global_mmcore.getPosition()
Expand All @@ -100,9 +99,9 @@ def test_stage_widget(qtbot: QtBot, global_mmcore: CMMCorePlus):
z_up_2 = stage_z._btns.layout().itemAtPosition(1, 3)
z_up_2.widget().click()
assert (
(z_pos + (stage_z._step.value() * 2)) - 1
(z_pos + (stage_z.step() * 2)) - 1
< global_mmcore.getPosition()
< (z_pos + (stage_z._step.value() * 2)) + 1
< (z_pos + (stage_z.step() * 2)) + 1
)
assert stage_z._readout.text() == f"Z: {round(global_mmcore.getPosition(), 2)}"

Expand Down

0 comments on commit 308309e

Please sign in to comment.