Skip to content

Commit

Permalink
WIP: Add roi deletion with keypress
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Jun 13, 2024
1 parent bef8d2b commit d0e72e4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/ndv/viewer/_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import cmap
import numpy as np
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QHBoxLayout, QLabel, QPushButton, QVBoxLayout, QWidget
from superqt import QCollapsible, QElidingLabel, QIconifyIcon, ensure_main_thread
from superqt.utils import qthrottled, signals_blocked
Expand All @@ -28,7 +29,7 @@
from concurrent.futures import Future
from typing import Any, Callable, Hashable, Iterable, Sequence, TypeAlias

from qtpy.QtGui import QCloseEvent, QMouseEvent
from qtpy.QtGui import QCloseEvent, QKeyEvent, QMouseEvent

from ._backends._protocols import PCanvas, PImageHandle, PRoiHandle
from ._dims_slider import DimKey, Indices, Sizes
Expand Down Expand Up @@ -185,6 +186,10 @@ def __init__(
self._canvas.qwidget().mouseReleaseEvent = self._wrap_canvas_mouse_release(
self._canvas.qwidget().mouseReleaseEvent
)
# FIXME - vispy likes to eat all the key presses
# self._canvas.qwidget().keyPressEvent = self._wrap_canvas_key_press(
# self._canvas.qwidget().keyPressEvent
# )

self._lut_drop = QCollapsible("LUTs", self)
self._lut_drop.setCollapsedIcon(QIconifyIcon("bi:chevron-down", color=MID_GRAY))
Expand Down Expand Up @@ -594,6 +599,11 @@ def _is_idle(self) -> bool:
"""Return True if no futures are running. Used for testing, and debugging."""
return self._last_future is None

def keyPressEvent(self, a0: QKeyEvent) -> None:
if a0.key() == Qt.Key_Delete and self._roi is not None:
self._roi.remove()
self._roi = None

Check warning on line 605 in src/ndv/viewer/_viewer.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L603-L605

Added lines #L603 - L605 were not covered by tests

def _wrap_canvas_mouse_release(
self, old_method: Callable[[QMouseEvent], None]
) -> Callable[[QMouseEvent], None]:
Expand All @@ -605,3 +615,14 @@ def new_release(event: QMouseEvent) -> None:
return old_method(event)

Check warning on line 615 in src/ndv/viewer/_viewer.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L615

Added line #L615 was not covered by tests

return new_release

def _wrap_canvas_key_press(
self, old_method: Callable[[QMouseEvent], None]
) -> Callable[[QMouseEvent], None]:
def new_key_press(event: QMouseEvent) -> None:

Check warning on line 622 in src/ndv/viewer/_viewer.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L622

Added line #L622 was not covered by tests
# If in EDIT_ROI mode, a release should untoggle the ROI button
self.keyPressEvent(event)

Check warning on line 624 in src/ndv/viewer/_viewer.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L624

Added line #L624 was not covered by tests
# Proceed with normal mouse release
return old_method(event)

Check warning on line 626 in src/ndv/viewer/_viewer.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L626

Added line #L626 was not covered by tests

return new_key_press

Check warning on line 628 in src/ndv/viewer/_viewer.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L628

Added line #L628 was not covered by tests

0 comments on commit d0e72e4

Please sign in to comment.