Skip to content

Commit

Permalink
feat: enable/disable spinbox with right-click
Browse files Browse the repository at this point in the history
  • Loading branch information
fdrgsp committed Nov 20, 2024
1 parent d27b6d7 commit ce9d62c
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/pymmcore_widgets/control/_stage_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

from fonticon_mdi6 import MDI6
from pymmcore_plus import CMMCorePlus, DeviceType, Keyword
from qtpy.QtCore import Qt, QTimerEvent, Signal
from qtpy.QtCore import QEvent, QObject, QPoint, Qt, QTimerEvent, Signal
from qtpy.QtWidgets import (
QCheckBox,
QDoubleSpinBox,
QGridLayout,
QHBoxLayout,
QLabel,
QMenu,
QPushButton,
QRadioButton,
QSpinBox,
Expand Down Expand Up @@ -94,6 +95,28 @@ def __init__(
self.setButtonSymbols(QSpinBox.ButtonSymbols.NoButtons)
self.setAlignment(Qt.AlignmentFlag.AlignCenter)

# enable custom context menu handling for right-click events
self.setContextMenuPolicy(Qt.ContextMenuPolicy.NoContextMenu)
self.installEventFilter(self)

def eventFilter(self, obj: QObject, event: QEvent) -> bool:
# listen to right-click events even when the spinbox is disabled
if obj is self and event.type() == QEvent.Type.ContextMenu:
self._on_context_menu(event.globalPos())
return True

Check warning on line 106 in src/pymmcore_widgets/control/_stage_widget.py

View check run for this annotation

Codecov / codecov/patch

src/pymmcore_widgets/control/_stage_widget.py#L105-L106

Added lines #L105 - L106 were not covered by tests
return super().eventFilter(obj, event) # type: ignore [no-any-return]

def _on_context_menu(self, global_pos: QPoint) -> None:
menu = QMenu(self)

Check warning on line 110 in src/pymmcore_widgets/control/_stage_widget.py

View check run for this annotation

Codecov / codecov/patch

src/pymmcore_widgets/control/_stage_widget.py#L110

Added line #L110 was not covered by tests
# update label based on the current state
label = "Disable Editing" if self.isEnabled() else "Enable Editing"
toggle_action = menu.addAction(label)
toggle_action.triggered.connect(self._toggle_enabled)
menu.exec(global_pos)

Check warning on line 115 in src/pymmcore_widgets/control/_stage_widget.py

View check run for this annotation

Codecov / codecov/patch

src/pymmcore_widgets/control/_stage_widget.py#L112-L115

Added lines #L112 - L115 were not covered by tests

def _toggle_enabled(self) -> None:
self.setEnabled(not self.isEnabled())

Check warning on line 118 in src/pymmcore_widgets/control/_stage_widget.py

View check run for this annotation

Codecov / codecov/patch

src/pymmcore_widgets/control/_stage_widget.py#L118

Added line #L118 was not covered by tests


class HaltButton(QPushButton):
def __init__(self, device: str, core: CMMCorePlus, parent: QWidget | None = None):
Expand Down

0 comments on commit ce9d62c

Please sign in to comment.