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

Skip to content

Test addition for hover #3

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 11 commits into from
May 2, 2023
Merged
2 changes: 2 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ def set_hover(self, hover):
artist will fire a hover event if the mouse event is hovering over
the artist.
"""
if not self.figure and hover is not None:
raise ValueError("Cannot hover without an existing figure")
self._hover = hover

def get_hover(self):
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3016,8 +3016,9 @@ def mouse_move(self, event):
self.set_message(self._mouse_event_to_message(event))

if callable(getattr(self, 'set_hover_message', None)):
for a in self.canvas.figure.findobj(match=lambda x: not isinstance(x,
Rectangle), include_self=False):
def nonrect(x):
return not isinstance(x, Rectangle)
for a in self.canvas.figure.findobj(match=nonrect, include_self=False):
inside, prop = a.contains(event)
if inside:
self.set_hover_message(self._mouse_event_to_message(event))
Expand Down
13 changes: 12 additions & 1 deletion lib/matplotlib/backends/backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from matplotlib.backend_bases import (
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
TimerBase, cursors, ToolContainerBase, MouseButton,
CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent)
CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent, HoverEvent)
import matplotlib.backends.qt_editor.figureoptions as figureoptions
from . import qt_compat
from .qt_compat import (
Expand Down Expand Up @@ -305,6 +305,17 @@ def mouseReleaseEvent(self, event):
modifiers=self._mpl_modifiers(),
guiEvent=event)._process()

def mouseOverEvent(self, event):
artist = event.artist
if not artist.get_hover:
thismouse = MouseEvent("motion_hover_event", self,
*self.mouseEventCoords(event),
modifiers=self._mpl_modifiers(),
guiEvent=event)
hovering = HoverEvent("motion_hover_event", self,
thismouse, artist, None)
hovering._process()

def wheelEvent(self, event):
# from QWheelEvent::pixelDelta doc: pixelDelta is sometimes not
# provided (`isNull()`) and is unreliable on X11 ("xcb").
Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,24 @@ def test_set_alpha_for_array():
art._set_alpha_for_array([0.5, np.nan])


def test_set_hover():
art = martist.Artist() # blank canvas
with pytest.raises(ValueError, match="Cannot hover without an existing figure"):
art.set_hover(True)

fig, ax = plt.subplots()
im = ax.imshow(np.arange(36).reshape(6, 6)) # non-blank canvas

im.set_hover(True) # set hover variable to possible values given a figure exists
assert im.get_hover()
im.set_hover(False)
assert not im.get_hover()
im.set_hover(None)
assert im.get_hover() is None

im.remove()


def test_callbacks():
def func(artist):
func.counter += 1
Expand Down