Skip to content

Commit

Permalink
Switch grabbed clim on overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Oct 30, 2024
1 parent 7841111 commit 5a7097b
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/ndv/histogram/_vispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def __init__(self, parent: Any = None) -> None:
self._gamma_handle_grabbed: bool = False

Check warning on line 329 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L327-L329

Added lines #L327 - L329 were not covered by tests

self._clims: tuple[float, float] | None = None
self._clim_handle_grabbed: int = 0
self._clim_handle_grabbed: int = -1

Check warning on line 332 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L331-L332

Added lines #L331 - L332 were not covered by tests
# The gamma handle appears halfway between the clims

self.plot._view.add(self._hist)
Expand Down Expand Up @@ -463,17 +463,17 @@ def on_mouse_press(self, event: SceneMouseEvent) -> None:
# determine if a clim_handle was clicked
self._clim_handle_grabbed = self._pos_is_clim(event)
self._gamma_handle_grabbed = self._pos_is_gamma(event)
if self._clim_handle_grabbed or self._gamma_handle_grabbed:
if self._clim_handle_grabbed > -1 or self._gamma_handle_grabbed:

Check warning on line 466 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L464-L466

Added lines #L464 - L466 were not covered by tests
# disconnect the pan/zoom mouse events until handle is dropped
self.plot.camera.interactive = False

Check warning on line 468 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L468

Added line #L468 was not covered by tests

def on_mouse_release(self, event: SceneMouseEvent) -> None:
self._clim_handle_grabbed = 0
self._clim_handle_grabbed = -1
self._gamma_handle_grabbed = False
self.plot.camera.interactive = True

Check warning on line 473 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L470-L473

Added lines #L470 - L473 were not covered by tests

def _pos_is_clim(self, event: SceneMouseEvent, tolerance: int = 3) -> int:

Check warning on line 475 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L475

Added line #L475 was not covered by tests
"""Returns 1 if x is near clims[0], 2 if near clims[1], else 0
"""Returns i if x is near clims[i], else -1
event is expected to to have an attribute 'pos' giving the mouse
position be in window coordinates.
"""
Expand All @@ -489,10 +489,10 @@ def _pos_is_clim(self, event: SceneMouseEvent, tolerance: int = 3) -> int:
clim1, _ = self._to_window_coords((self._clims[1],))
clim0, _ = self._to_window_coords((self._clims[0],))
if abs(clim1 - x) < tolerance:
return 2
if abs(clim0 - x) < tolerance:
return 1
return 0
if abs(clim0 - x) < tolerance:
return 0
return -1

Check warning on line 495 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L488-L495

Added lines #L488 - L495 were not covered by tests

def _pos_is_gamma(self, event: SceneMouseEvent, tolerance: int = 4) -> bool:

Check warning on line 497 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L497

Added line #L497 was not covered by tests
"""Returns True if value is near the gamma handle.
Expand All @@ -516,13 +516,17 @@ def on_mouse_move(self, event: SceneMouseEvent) -> None:

# event.pos == (0,0) is top left corner of window

if self._clim_handle_grabbed:
if self._clim_handle_grabbed > -1:
newlims = list(self._clims)
if self.vertical:
c = self._to_plot_coords(event.pos)[1]

Check warning on line 522 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L519-L522

Added lines #L519 - L522 were not covered by tests
else:
c = self._to_plot_coords(event.pos)[0]
newlims[self._clim_handle_grabbed - 1] = c
newlims[self._clim_handle_grabbed] = c
if newlims[0] > newlims[1]:
newlims[:] = newlims[::-1]
self._clim_handle_grabbed += 1
self._clim_handle_grabbed %= 2
self.climsChanged.emit(newlims)
return

Check warning on line 531 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L524-L531

Added lines #L524 - L531 were not covered by tests

Expand All @@ -537,7 +541,7 @@ def on_mouse_move(self, event: SceneMouseEvent) -> None:

self.native.unsetCursor()

Check warning on line 542 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L542

Added line #L542 was not covered by tests

if self._pos_is_clim(event):
if self._pos_is_clim(event) > -1:
if self.vertical:
cursor = Qt.CursorShape.SplitVCursor

Check warning on line 546 in src/ndv/histogram/_vispy.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_vispy.py#L544-L546

Added lines #L544 - L546 were not covered by tests
else:
Expand Down

0 comments on commit 5a7097b

Please sign in to comment.