Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enables functionality of holding SHIFT to draw circle or square #37

Merged
merged 1 commit into from
Nov 18, 2024
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
40 changes: 38 additions & 2 deletions src/biaplotter/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from abc import ABC, abstractmethod
from matplotlib.path import Path as mplPath
from matplotlib.widgets import LassoSelector, RectangleSelector, EllipseSelector
from qtpy.QtGui import QGuiApplication
from qtpy.QtCore import Qt


if TYPE_CHECKING:
Expand Down Expand Up @@ -64,6 +66,23 @@
self._selector = None


class MplRectangleSelector(RectangleSelector):
"""Custom rectangle selector class.

Sub-class of matplotlib RectangleSelector to ensure, via 'qtpy', the option to draw a square when holding the SHIFT key.

Note: matplotlib RectangleSelector already has this functionality via the 'state_modifier_keys' argument, but it doesn't work if the canvas is used inside napari.
"""
def _onmove(self, event):
modifiers = QGuiApplication.keyboardModifiers()
if modifiers == Qt.ShiftModifier:
self.add_state('square')

Check warning on line 79 in src/biaplotter/selectors.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/selectors.py#L77-L79

Added lines #L77 - L79 were not covered by tests
else:
if 'square' in self._state:
self._state.remove('square')
super()._onmove(event)

Check warning on line 83 in src/biaplotter/selectors.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/selectors.py#L81-L83

Added lines #L81 - L83 were not covered by tests


class BaseRectangleSelector(Selector):
"""Base class for creating a rectangle selector.

Expand Down Expand Up @@ -131,11 +150,28 @@
Interactive is set to True to allow for interaction.
Drag from anywhere is set to True to allow for drawing from any point.
"""
self._selector = RectangleSelector(self.ax, self.on_select, useblit=True, button=[
self._selector = MplRectangleSelector(self.ax, self.on_select, useblit=True, button=[
1], minspanx=5, minspany=5, spancoords='pixels', interactive=True, drag_from_anywhere=True,
props=dict(facecolor='#00c18c', edgecolor='#00c18c', alpha=0.3, fill=True, linewidth=2.5, linestyle='--'))


class MplEllipseSelector(EllipseSelector):
"""Custom ellipse selector class.

Sub-class of matplotlib EllipseSelector to ensure, via 'qtpy', the option to draw a circle when holding the SHIFT key.

Note: matplotlib EllipeseSelector already has this functionality via the 'state_modifier_keys' argument, but it doesn't work if the canvas is used inside napari.
"""
def _onmove(self, event):
modifiers = QGuiApplication.keyboardModifiers()
if modifiers == Qt.ShiftModifier:
self.add_state('square')

Check warning on line 168 in src/biaplotter/selectors.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/selectors.py#L166-L168

Added lines #L166 - L168 were not covered by tests
else:
if 'square' in self._state:
self._state.remove('square')
super()._onmove(event)

Check warning on line 172 in src/biaplotter/selectors.py

View check run for this annotation

Codecov / codecov/patch

src/biaplotter/selectors.py#L170-L172

Added lines #L170 - L172 were not covered by tests


class BaseEllipseSelector(Selector):
"""Base class for creating an ellipse selector.

Expand Down Expand Up @@ -204,7 +240,7 @@
Interactive is set to True to allow for interaction.
Drag from anywhere is set to True to allow for drawing from any point.
"""
self._selector = EllipseSelector(self.ax, self.on_select, useblit=True, button=[
self._selector = MplEllipseSelector(self.ax, self.on_select, useblit=True, button=[
1], minspanx=5, minspany=5, spancoords='pixels', interactive=True, drag_from_anywhere=True,
props=dict(facecolor='#00c18c', edgecolor='#00c18c', alpha=0.3, fill=True, linewidth=2.5, linestyle='--'))

Expand Down
Loading