8000 Tweak callbacks to generate pick events. · matplotlib/matplotlib@c32efd9 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c32efd9

Browse files
committed
Tweak callbacks to generate pick events.
Currently, pick events are generated by button_press and scroll events, which call canvas.pick, which itself checks the widgetlock and then calls Figure.pick, which is actually Artist.pick; Artist.pick checks whether to emit a PickEvent for the current artist, then calls itself recursively on children artists. This PR gets rid of the intermediate canvas.pick layer, moving the widgetlock check to Figure.pick (Figure.pick still calls the super() pick, i.e. Artist.pick, after the check). The advantages are that 1) this avoids colliding with pick methods that may be present in the native GUI classes (this is not a theoretical case: see the changes in the GTK4 case) and 2) this makes it easier to fix button_pick_id and scroll_pick_id to use picklable connections (which they should do). In the old implementation, lambdas were not picklable, and one could not write e.g. `mpl_connect("button_press_event", self.canvas.pick)` because that would not handle canvas swapping.
1 parent 51ce677 commit c32efd9

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``FigureCanvasBase.pick``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated. Directly call `.Figure.pick`, which has taken over the
4+
responsibility of checking the canvas widgetlock as well.

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,7 @@ def is_saving(self):
16971697
"""
16981698
return self._is_saving
16991699

1700+
@_api.deprecated("3.6", alternative="canvas.figure.pick")
17001701
def pick(self, mouseevent):
17011702
if not self.widgetlock.locked():
17021703
self.figure.pick(mouseevent)

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ def __init__(self, figure=None):
7979
style_ctx.add_provider(css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
8080
style_ctx.add_class("matplotlib-canvas")
8181

82-
def pick(self, mouseevent):
83-
# GtkWidget defines pick in GTK4, so we need to override here to work
84-
# with the base implementation we want.
85-
FigureCanvasBase.pick(self, mouseevent)
86-
8782
def destroy(self):
8883
self.close_event()
8984

lib/matplotlib/figure.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,10 +2372,10 @@ def __init__(self,
23722372
# pickling.
23732373
self._canvas_callbacks = cbook.CallbackRegistry(
23742374
signals=FigureCanvasBase.events)
2375-
self._button_pick_id = self._canvas_callbacks.connect(
2376-
'button_press_event', lambda event: self.canvas.pick(event))
2377-
self._scroll_pick_id = self._canvas_callbacks.connect(
2378-
'scroll_event', lambda event: self.canvas.pick(event))
2375+
self._button_pick_id = self._canvas_callbacks._connect_picklable(
2376+
'button_press_event', self.pick)
2377+
self._scroll_pick_id = self._canvas_callbacks._connect_picklable(
2378+
'scroll_event', self.pick)
23792379

23802380
if figsize is None:
23812381
figsize = mpl.rcParams['figure.figsize']
@@ -2423,6 +2423,10 @@ def __init__(self,
24232423
# list of child gridspecs for this figure
24242424
self._gridspecs = []
24252425

2426+
def pick(self, mouseevent):
2427+
if not self.canvas.widgetlock.locked():
2428+
super().pick(mouseevent)
2429+
24262430
def _check_layout_engines_compat(self, old, new):
24272431
"""
24282432
Helper for set_layout engine

0 commit comments

Comments
 (0)
0