8000 In Artist.contains, check that moussevents occurred on the right canvas. · matplotlib/matplotlib@a6230cc · GitHub
[go: up one dir, main page]

Skip to content

Commit a6230cc

Browse files
committed
In Artist.contains, check that moussevents occurred on the right canvas.
The old _default_contains check was geared towards custom contains checkers (set via set_contains), which have been removed. Get rid of that and instead, add a check for canvas identity which is more useful.
1 parent 4666bef commit a6230cc

13 files changed

+36
-85
lines changed

lib/matplotlib/artist.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -461,28 +461,22 @@ def get_children(self):
461461
r"""Return a list of the child `.Artist`\s of this `.Artist`."""
462462
return []
463463

464-
def _default_contains(self, mouseevent, figure=None):
464+
def _different_canvas(self, event):
465465
"""
466-
Base impl. for checking whether a mouseevent happened in an artist.
466+
Check whether an *event* occurred on a canvas other that this artist's canvas.
467467
468-
1. If the artist figure is known and the event did not occur in that
469-
figure (by checking its ``canvas`` attribute), reject it.
470-
2. Otherwise, return `None, {}`, indicating that the subclass'
471-
implementation should be used.
468+
If this method returns True, the event definitely occurred on a different
469+
canvas; if it returns False, either it occurred on the same canvas, or we may
470+
not have enough information to know.
472471
473-
Subclasses should start their definition of `contains` as follows:
472+
Subclasses should start their definition of `contains` as follows::
474473
475-
inside, info = self._default_contains(mouseevent)
476-
if inside is not None:
477-
return inside, info
474+
if self._different_canvas(mouseevent):
475+
return False, {}
478476
# subclass-specific implementation follows
479-
480-
The *figure* kwarg is provided for the implementation of
481-
`.Figure.contains`.
482477
"""
483-
if figure is not None and mouseevent.canvas is not figure.canvas:
484-
return False, {}
485-
return None, {}
478+
return (getattr(event, "canvas", None) is not None and self.figure is not None
479+
and event.canvas is not self.figure.canvas)
486480

487481
def contains(self, mouseevent):
488482
"""

lib/matplotlib/axes/_base.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,9 +4271,6 @@ def get_children(self):
42714271

42724272
def contains(self, mouseevent):
42734273
# docstring inherited.
4274-
inside, info = self._default_contains(mouseevent)
4275-
if inside is not None:
4276-
return inside, info
42774274
return self.patch.contains(mouseevent)
42784275

42794276
def contains_point(self, point):

lib/matplotlib/axis.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ def contains(self, mouseevent):
242242
This function always returns false. It is more useful to test if the
243243
axis as a whole contains the mouse rather than the set of tick marks.
244244
"""
245-
inside, info = self._default_contains(mouseevent)
246-
if inside is not None:
247-
return inside, info
248245
return False, {}
249246

250247
def set_pad(self, val):
@@ -2230,10 +2227,8 @@ def _init(self):
22302227

22312228
def contains(self, mouseevent):
22322229
"""Test whether the mouse event occurred in the x-axis."""
2233-
inside, info = self._default_contains(mouseevent)
2234-
if inside is not None:
2235-
return inside, info
2236-
2230+
if self._different_canvas(mouseevent):
2231+
return False, {}
22372232
x, y = mouseevent.x, mouseevent.y
22382233
try:
22392234
trans = self.axes.transAxes.inverted()
@@ -2473,10 +2468,8 @@ def _init(self):
24732468

24742469
def contains(self, mouseevent):
24752470
# docstring inherited
2476-
inside, info = self._default_contains(mouseevent)
2477-
if inside is not None:
2478-
return inside, info
2479-
2471+
if self._different_canvas(mouseevent):
2472+
return False, {}
24802473
x, y = mouseevent.x, mouseevent.y
24812474
try:
24822475
trans = self.axes.transAxes.inverted()

lib/matplotlib/collections.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,24 +454,16 @@ def contains(self, mouseevent):
454454
Returns ``bool, dict(ind=itemlist)``, where every item in itemlist
455455
contains the event.
456456
"""
457-
inside, info = self._default_contains(mouseevent)
458-
if inside is not None:
459-
return inside, info
460-
461-
if not self.get_visible():
457+
if self._different_canvas(mouseevent) or not self.get_visible():
462458
return False, {}
463-
464459
pickradius = (
465460
float(self._picker)
466461
if isinstance(self._picker, Number) and
467462
self._picker is not True # the bool, not just nonzero or 1
468463
else self._pickradius)
469-
470464
if self.axes:
471465
self.axes._unstale_viewLim()
472-
473466
transform, offset_trf, offsets, paths = self._prepare_points()
474-
475467
# Tests if the point is contained on one of the polygons formed
476468
# by the control points of each of the paths. A point is considered
477469
# "on" a path if it would lie within a stroke of width 2*pickradius
@@ -481,7 +473,6 @@ def contains(self, mouseevent):
481473
mouseevent.x, mouseevent.y, pickradius,
482474
transform.frozen(), paths, self.get_transforms(),
483475
offsets, offset_trf, pickradius <= 0)
484-
485476
return len(ind) > 0, dict(ind=ind)
486477

487478
def set_urls(self, urls):

lib/matplotlib/figure.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,8 @@ def contains(self, mouseevent):
289289
-------
290290
bool, {}
291291
"""
292-
inside, info = self._default_contains(mouseevent, figure=self)
293-
if inside is not None:
294-
return inside, info
292+
if self._different_canvas(mouseevent):
293+
return False, {}
295294
inside = self.bbox.contains(mouseevent.x, mouseevent.y)
296295
return inside, {}
297296

lib/matplotlib/image.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,8 @@ def draw(self, renderer, *args, **kwargs):
654654

655655
def contains(self, mouseevent):
656656
"""Test whether the mouse event occurred within the image."""
657-
inside, info = self._default_contains(mouseevent)
658-
if inside is not None:
659-
return inside, info
657+
if self._different_canvas(mouseevent):
658+
return False, {}
660659
# 1) This doesn't work for figimage; but figimage also needs a fix
661660
# below (as the check cannot use x/ydata and extents).
662661
# 2) As long as the check below uses x/ydata, we need to test axes
@@ -1468,16 +1467,10 @@ def get_window_extent(self, renderer=None):
14681467

14691468
def contains(self, mouseevent):
14701469
"""Test whether the mouse event occurred within the image."""
1471-
inside, info = self._default_contains(mouseevent)
1472-
if inside is not None:
1473-
return inside, info
1474-
1475-
if not self.get_visible(): # or self.get_figure()._renderer is None:
1470+
if self._different_canvas(mouseevent) or not self.get_visible():
14761471
return False, {}
1477-
14781472
x, y = mouseevent.x, mouseevent.y
14791473
inside = self.get_window_extent().contains(x, y)
1480-
14811474
return inside, {}
14821475

14831476
def make_image(self, renderer, magnification=1.0, unsampled=False):

lib/matplotlib/legend.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,9 +1174,6 @@ def _find_best_position(self, width, height, renderer, consider=None):
11741174
return l, b
11751175

11761176
def contains(self, event):
1177-
inside, info = self._default_contains(event)
1178-
if inside is not None:
1179-
return inside, info
11801177
return self.legendPatch.contains(event)
11811178

11821179
def set_draggable(self, state, use_blit=False, update='loc'):

lib/matplotlib/lines.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,8 @@ def contains(self, mouseevent):
447447
448448
TODO: sort returned indices by distance
449449
"""
450-
inside, info = self._default_contains(mouseevent)
451-
if inside is not None:
452-
return inside, info
450+
if self._different_canvas(mouseevent):
451+
return False, {}
453452

454453
# Make sure we have data to plot
455454
if self._invalidy or self._invalidx:

lib/matplotlib/offsetbox.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ def contains(self, mouseevent):
268268
--------
269269
.Artist.contains
270270
"""
271-
inside, info = self._default_contains(mouseevent)
272-
if inside is not None:
273-
return inside, info
271+
if self._different_canvas(mouseevent):
272+
return False, {}
274273
for c in self.get_children():
275274
a, b = c.contains(mouseevent)
276275
if a:
@@ -1353,9 +1352,8 @@ def anncoords(self, coords):
13531352
self.stale = True
13541353

13551354
def contains(self, mouseevent):
1356-
inside, info = self._default_contains(mouseevent)
1357-
if inside is not None:
1358-
return inside, info
1355+
if self._different_canvas(mouseevent):
1356+
return False, {}
13591357
if not self._check_xy(None):
13601358
return False, {}
13611359
return self.offsetbox.contains(mouseevent)

lib/matplotlib/patches.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ def contains(self, mouseevent, radius=None):
132132
-------
133133
(bool, empty dict)
134134
"""
135-
inside, info = self._default_contains(mouseevent)
136-
if inside is not None:
137-
return inside, info
135+
if self._different_canvas(mouseevent):
136+
return False, {}
138137
radius = self._process_radius(radius)
139138
codes = self.get_path().codes
140139
if codes is not None:

lib/matplotlib/quiver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,8 @@ def set_figure(self, fig):
374374
self.text.set_figure(fig)
375375

376376
def contains(self, mouseevent):
377-
inside, info = self._default_contains(mouseevent)
378-
if inside is not None:
379-
return inside, info
377+
if self._different_canvas(mouseevent):
378+
return False, {}
380379
# Maybe the dictionary should allow one to
381380
# distinguish between a text hit and a vector hit.
382381
if (self.text.contains(mouseevent)[0] or

lib/matplotlib/table.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,8 @@ def _get_grid_bbox(self, renderer):
426426

427427
def contains(self, mouseevent):
428428
# docstring inherited
429-
inside, info = self._default_contains(mouseevent)
430-
if inside is not None:
431-
return inside, info
429+
if self._different_canvas(mouseevent):
430+
return False, {}
432431
# TODO: Return index of the cell containing the cursor so that the user
433432
# doesn't have to bind to each one individually.
434433
renderer = self.figure._get_renderer()

lib/matplotlib/text.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,28 +213,22 @@ def contains(self, mouseevent):
213213
Return whether the mouse event occurred inside the axis-aligned
214214
bounding-box of the text.
215215
"""
216-
inside, info = self._default_contains(mouseevent)
217-
if inside is not None:
218-
return inside, info
219-
220-
if not self.get_visible() or self._renderer is None:
216+
if (self._different_canvas(mouseevent) or not self.get_visible()
217+
or self._renderer is None):
221218
return False, {}
222-
223219
# Explicitly use Text.get_window_extent(self) and not
224220
# self.get_window_extent() so that Annotation.contains does not
225221
# accidentally cover the entire annotation bounding box.
226222
bbox = Text.get_window_extent(self)
227223
inside = (bbox.x0 <= mouseevent.x <= bbox.x1
228224
and bbox.y0 <= mouseevent.y <= bbox.y1)
229-
230225
cattr = {}
231226
# if the text has a surrounding patch CC7D , also check containment for it,
232227
# and merge the results with the results for the text.
233228
if self._bbox_patch:
234229
patch_inside, patch_cattr = self._bbox_patch.contains(mouseevent)
235230
inside = inside or patch_inside
236231
cattr["bbox_patch"] = patch_cattr
237-
238232
return inside, cattr
239233

240234
def _get_xy_display(self):
@@ -1855,9 +1849,8 @@ def transform(renderer) -> Transform
18551849
Text.__init__(self, x, y, text, **kwargs)
18561850

18571851
def contains(self, event):
1858-
inside, info = self._default_contains(event)
1859-
if inside is not None:
1860-
return inside, info
1852+
if self._different_canvas(event):
1853+
return False, {}
18611854
contains, tinfo = Text.contains(self, event)
18621855
if self.arrow_patch is not None:
18631856
in_patch, _ = self.arrow_patch.contains(event)

0 commit comments

Comments
 (0)
0