Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/napari_spatialdata/_scatterwidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
from pyqtgraph.widgets.ColorButton import ColorButton
from qtpy.QtCore import QSize, Qt, Signal
from qtpy.QtGui import QColor, QIcon
from qtpy.QtWidgets import QPushButton
from qtpy.QtWidgets import QLabel, QPushButton, QSpinBox
from scipy.spatial import cKDTree
from shapely.geometry import Point, Polygon
from spatialdata._types import ArrayLike

import napari_spatialdata.constants.config
from napari_spatialdata._model import DataModel
from napari_spatialdata._widgets import AListWidget, ComponentWidget
from napari_spatialdata.constants.config import POINT_SIZE_SCATTERPLOT_WIDGET

__all__ = [
"PlotWidget",
Expand Down Expand Up @@ -334,6 +334,7 @@ def __init__(self, viewer: Viewer | None, model: DataModel) -> None:
self.lut: pg.HistogramLUTItem | None = None
self.discrete_color_widget: DiscreteColorWidget | None = None
self.wrapped_widget: QtWidgets.QGraphicsProxyWidget | None = None
self._point_size: int = napari_spatialdata.constants.config.POINT_SIZE_SCATTERPLOT_WIDGET

self.scatter_plot = self.addPlot(title="")
self.scatter_plot.setLabel("bottom", self.x_label)
Expand All @@ -344,7 +345,10 @@ def __init__(self, viewer: Viewer | None, model: DataModel) -> None:

# Create a separate ScatterPlotItem for the highlighted point
self.hovered_point = pg.ScatterPlotItem(
pen=pg.mkPen("r", width=2), symbol="o", size=POINT_SIZE_SCATTERPLOT_WIDGET, brush=pg.mkBrush(255, 0, 0)
pen=pg.mkPen("r", width=2),
symbol="o",
size=self._point_size + 2,
brush=pg.mkBrush(255, 0, 0),
)
self.scatter_plot.addItem(self.hovered_point)

Expand Down Expand Up @@ -417,6 +421,19 @@ def __init__(self, viewer: Viewer | None, model: DataModel) -> None:
self.rectangle_mode_button.setToolTip("Add rectangular ROIs.")
self.rectangle_mode_button.move(50, 10) # Adjust position as needed

# Point size control
self.point_size_label = QLabel("Size:", self)
self.point_size_label.setStyleSheet(f"QLabel {{ color: rgb{self.color}; background-color: transparent; }}")
self.point_size_label.move(130, 14)

self.point_size_spinbox = QSpinBox(self)
self.point_size_spinbox.setRange(1, 50)
self.point_size_spinbox.setValue(self._point_size)
self.point_size_spinbox.setFixedWidth(50)
self.point_size_spinbox.setToolTip("Adjust point size in scatter plot")
self.point_size_spinbox.valueChanged.connect(self._on_point_size_changed)
self.point_size_spinbox.move(165, 10)

# Connect mouse events
self.scatter_plot.setMouseEnabled(x=True, y=True)
self.scatter_plot.scene().sigMouseMoved.connect(self.mouseMoveEvent)
Expand Down Expand Up @@ -461,6 +478,25 @@ def update_proximity_sensitivity(self) -> None:
logger.debug("Updating proximity sensitivity...")
self.dist_threshold = [x * 15 for x in self.scatter.pixelSize()] # Adjust this factor as needed

@property
def point_size(self) -> int:
"""Get the current point size for the scatter plot."""
return self._point_size

@point_size.setter
def point_size(self, value: int) -> None:
"""Set the point size for the scatter plot."""
self._point_size = value
self.point_size_spinbox.setValue(value)

def _on_point_size_changed(self, value: int) -> None:
"""Handle point size spinbox value change."""
self._point_size = value
# Update hovered point indicator size to be slightly larger
self.hovered_point.setSize(value + 2)
if self.scatter is not None:
self.plot()

def update_hover_highlight(self, x: float, y: float) -> None:
"""Update the hover highlight based on the cursor position."""
if self.kd_tree is not None and self.x_data is not None and self.y_data is not None:
Expand Down Expand Up @@ -759,6 +795,7 @@ def plot(self, event: Any = None) -> None:
pen=None,
symbolPen=self.symbolPen,
symbol="o",
symbolSize=self._point_size,
clear=True,
symbolBrush=self.brushes,
)
Expand All @@ -773,6 +810,7 @@ def plot(self, event: Any = None) -> None:
fillLevel=0,
pen=None,
symbolPen=self.symbolPen,
symbolSize=self._point_size,
symbolBrush=self.brushes,
clear=True,
)
Expand All @@ -785,6 +823,7 @@ def plot(self, event: Any = None) -> None:
fillLevel=0,
pen=None,
symbolPen=self.symbolPen,
symbolSize=self._point_size,
symbolBrush=self.brushes,
clear=True,
)
Expand Down
Loading