Skip to content

Commit

Permalink
WIP: Limit to one ROI
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Jun 12, 2024
1 parent 0a7cb81 commit 5a750a0
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/ndv/viewer/_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -144,7 +144,7 @@ def __init__(
self._ndims: Literal[2, 3] = 2

# ROI selection
self._rois: list[PRoiHandle] = []
self._roi: PRoiHandle | None = None
# Current mode
self._mode: CanvasMode = CanvasMode.PAN_ZOOM

Expand Down Expand Up @@ -185,6 +185,7 @@ def __init__(
self._canvas.qwidget().mouseReleaseEvent = self._wrap_canvas_mouse_release(
self._canvas.qwidget().mouseReleaseEvent
)
# self._canvas.qwidget().keyPressEvent = self.keyPressEvent

self._lut_drop = QCollapsible("LUTs", self)
self._lut_drop.setCollapsedIcon(QIconifyIcon("bi:chevron-down", color=MID_GRAY))
Expand Down Expand Up @@ -297,6 +298,30 @@ def set_data(
# update the data info label
self._data_info_label.setText(self._data_wrapper.summary_info())

def add_roi(
self,
vertices: list[tuple[float, float]] | None = None,
color: cmap.Color | None = None,
border_color: cmap.Color | None = None,
) -> None:
"""Set the datastore, and, optionally, the sizes of the data.
Properties
----------
vertices : list[tuple[float, float]] | None
The vertices of the ROI, listed in
initial_index : Indices | None
The initial index to display. This is a mapping of dimensions to integers
or slices that define the slice of the data to display. If not provided,
the initial index will be set to the middle of the data.
"""
if self._roi is not None:
self._roi.remove()

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

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L318-L319

Added lines #L318 - L319 were not covered by tests
# TODO: Should we return the handle? We don't expose protocols yet
self._roi = self._canvas.add_polygon(

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

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L321

Added line #L321 was not covered by tests
vertices=vertices, color=color, border_color=border_color
)

def set_visualized_dims(self, dims: Iterable[DimKey]) -> None:
"""Set the dimensions that will be visualized.
Expand Down Expand Up @@ -411,7 +436,7 @@ def _on_add_roi_clicked(self, checked: bool) -> None:
# Disable canvas pan/zoom while dragging roi
self._mode = CanvasMode.EDIT_ROI

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

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L437

Added line #L437 was not covered by tests
# Add new roi
self._rois.append(self._canvas.add_polygon())
self.add_roi()

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

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L439

Added line #L439 was not covered by tests
else:
# Enable canvas pan/zoom when done
self._mode = CanvasMode.PAN_ZOOM
Expand Down Expand Up @@ -462,6 +487,12 @@ def _update_data_for_index(self, index: Indices) -> None:
self._progress_spinner.show()
f.add_done_callback(self._on_data_slice_ready)

def keyPressEvent(self, a0: QKeyEvent) -> None:
print(a0.key())

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

View check run for this annotation

Codecov / codecov/patch

src/ndv/viewer/_viewer.py#L491

Added line #L491 was not covered by tests
# if a0.key() is Qt.Key_Delete and self._roi is not None:
# self._roi.remove()
# self._roi = None

def closeEvent(self, a0: QCloseEvent | None) -> None:
if self._last_future is not None:
self._last_future.cancel()
Expand Down

0 comments on commit 5a750a0

Please sign in to comment.