Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Set the canvas cursor when using a SpanSelector #20743

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

Merged
merged 2 commits into from
Jul 27, 2021
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
37 changes: 36 additions & 1 deletion lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import matplotlib as mpl
from matplotlib import docstring
from . import _api, cbook, colors, ticker
from . import _api, backend_tools, cbook, colors, ticker
from .lines import Line2D
from .patches import Circle, Rectangle, Ellipse

Expand Down Expand Up @@ -2192,8 +2192,26 @@ def _setup_edge_handle(self, props):
useblit=self.useblit)
self.artists.extend([line for line in self._edge_handles.artists])

def _set_cursor(self, enabled):
"""Update the canvas cursor based on direction of the selector."""
if enabled:
cursor = (backend_tools.Cursors.RESIZE_HORIZONTAL
if self.direction == 'horizontal' else
backend_tools.Cursors.RESIZE_VERTICAL)
else:
cursor = backend_tools.Cursors.POINTER

self.ax.figure.canvas.set_cursor(cursor)

def connect_default_events(self):
# docstring inherited
super().connect_default_events()
if getattr(self, '_interactive', False):
self.connect_event('motion_notify_event', self._hover)

def _press(self, event):
"""Button press event handler."""
self._set_cursor(True)
if self._interactive and self._rect.get_visible():
self._set_active_handle(event)
else:
Expand Down Expand Up @@ -2248,6 +2266,7 @@ def direction(self, direction):

def _release(self, event):
"""Button release event handler."""
self._set_cursor(False)
if not self._interactive:
self._rect.set_visible(False)

Expand All @@ -2264,8 +2283,23 @@ def _release(self, event):
# self._pressv is deprecated but we still need to maintain it
self._pressv = None

self._active_handle = None

return False

def _hover(self, event):
"""Update the canvas cursor if it's over a handle."""
if self.ignore(event):
return

if self._active_handle is not None:
# Do nothing if button is pressed and a handle is active, which may
# occur with drag_from_anywhere=True.
return

_, e_dist = self._edge_handles.closest(event.x, event.y)
self._set_cursor(e_dist <= self.grab_range)

def _onmove(self, event):
"""Motion notify event handler."""

Expand Down Expand Up @@ -2805,6 +2839,7 @@ def _release(self, event):
# call desired function
self.onselect(self._eventpress, self._eventrelease)
self.update()
self._active_handle = None

return False

Expand Down