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

Skip to content

Commit 5d104e7

Browse files
committed
Move set_cursor from the toolbar to FigureCanvas.
While the toolbar does need to track what it wants to set the cursor to based on the active tool, actually setting it is an act on the canvas (or sometimes the window, but this is toolkit dependent, so we won't worry about that.)
1 parent b7d0591 commit 5d104e7

File tree

7 files changed

+66
-43
lines changed

7 files changed

+66
-43
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,24 @@ def release_mouse(self, ax):
20112011
if self.mouse_grabber is ax:
20122012
self.mouse_grabber = None
20132013

2014+
def set_cursor(self, cursor):
2015+
"""
2016+
Set the current cursor.
2017+
2018+
This may have no effect if the backend does not display anything.
2019+
2020+
If required by the backend, this method should trigger an update in
2021+
the backend event loop after the cursor is set, as this method may be
2022+
called e.g. before a long-running task during which the GUI is not
2023+
updated.
2024+
2025+
Parameters
2026+
----------
2027+
cursor : `.Cursors`
2028+
The cursor to dispay over the canvas. Note: some backends may
2029+
change the cursor for the entire window.
2030+
"""
2031+
20142032
def draw(self, *args, **kwargs):
20152033
"""
20162034
Render the `.Figure`.
@@ -2984,14 +3002,14 @@ def _update_cursor(self, event):
29843002
if self.mode and event.inaxes and event.inaxes.get_navigate():
29853003
if (self.mode == _Mode.ZOOM
29863004
and self._lastCursor != cursors.SELECT_REGION):
2987-
self.set_cursor(cursors.SELECT_REGION)
3005+
self.canvas.set_cursor(cursors.SELECT_REGION)
29883006
self._lastCursor = cursors.SELECT_REGION
29893007
elif (self.mode == _Mode.PAN
29903008
and self._lastCursor != cursors.MOVE):
2991-
self.set_cursor(cursors.MOVE)
3009+
self.canvas.set_cursor(cursors.MOVE)
29923010
self._lastCursor = cursors.MOVE
29933011
elif self._lastCursor != cursors.POINTER:
2994-
self.set_cursor(cursors.POINTER)
3012+
self.canvas.set_cursor(cursors.POINTER)
29953013
self._lastCursor = cursors.POINTER
29963014

29973015
@contextmanager
@@ -3009,10 +3027,10 @@ def _wait_cursor_for_draw_cm(self):
30093027
time.time(), getattr(self, "_draw_time", -np.inf))
30103028
if self._draw_time - last_draw_time > 1:
30113029
try:
3012-
self.set_cursor(cursors.WAIT)
3030+
self.canvas.set_cursor(cursors.WAIT)
30133031
yield
30143032
finally:
3015-
self.set_cursor(self._lastCursor)
3033+
self.canvas.set_cursor(self._lastCursor)
30163034
else:
30173035
yield
30183036

lib/matplotlib/backend_tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,15 @@ def _add_tool_cbk(self, event):
266266
self._add_tool(event.tool)
267267

268268
def _set_cursor_cbk(self, event):
269-
if not event:
269+
if not event or not self.canvas:
270270
return
271271
if (self._current_tool and getattr(event, "inaxes", None)
272272
and event.inaxes.get_navigate()):
273273
if self._last_cursor != self._current_tool.cursor:
274-
self.set_cursor(self._current_tool.cursor)
274+
self.canvas.set_cursor(self._current_tool.cursor)
275275
self._last_cursor = self._current_tool.cursor
276276
elif self._last_cursor != self._default_cursor:
277-
self.set_cursor(self._default_cursor)
277+
self.canvas.set_cursor(self._default_cursor)
278278
self._last_cursor = self._default_cursor
279279

280280
def set_cursor(self, cursor):

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ def _create_application():
8989

9090
@functools.lru_cache()
9191
def _mpl_to_gtk_cursor(mpl_cursor):
92-
name = {
92+
name = _api.check_getitem({
9393
cursors.MOVE: "move",
9494
cursors.HAND: "pointer",
9595
cursors.POINTER: "default",
9696
cursors.SELECT_REGION: "crosshair",
9797
cursors.WAIT: "wait",
9898
cursors.RESIZE_HORIZONTAL: "ew-resize",
9999
cursors.RESIZE_VERTICAL: "ns-resize",
100-
}[mpl_cursor]
100+
}, cursor=mpl_cursor)
101101
return Gdk.Cursor.new_from_name(Gdk.Display.get_default(), name)
102102

103103

@@ -187,6 +187,14 @@ def __init__(self, figure=None):
187187
def destroy(self):
188188
self.close_event()
189189

190+
def set_cursor(self, cursor):
191+
# docstring inherited
192+
window = self.get_property("window")
193+
if window is not None:
194+
window.set_cursor(_mpl_to_gtk_cursor(cursor))
195+
context = GLib.MainContext.default()
196+
context.iteration(True)
197+
190198
def scroll_event(self, widget, event):
191199
x = event.x
192200
# flipy so y=0 is bottom of canvas
@@ -532,13 +540,6 @@ def set_message(self, s):
532540
escaped = GLib.markup_escape_text(s)
533541
self.message.set_markup(f'<small>{escaped}</small>')
534542

535-
def set_cursor(self, cursor):
536-
window = self.canvas.get_property("window")
537-
if window is not None:
538-
window.set_cursor(_mpl_to_gtk_cursor(cursor))
539-
context = GLib.MainContext.default()
540-
context.iteration(True)
541-
542543
def draw_rubberband(self, event, x0, y0, x1, y1):
543544
height = self.canvas.figure.bbox.height
544545
y1 = height - y1

lib/matplotlib/backends/backend_macosx.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def _set_device_scale(self, value):
3939
self._dpi_ratio, old_value = value, self._dpi_ratio
4040
self.figure.dpi = self.figure.dpi / old_value * self._dpi_ratio
4141

42+
def set_cursor(self, cursor):
43+
# docstring inherited
44+
_macosx.set_cursor(cursor)
45+
4246
def _draw(self):
4347
renderer = self.get_renderer(cleared=self.figure.stale)
4448
if self.figure.stale:
@@ -108,9 +112,6 @@ def release_zoom(self, event):
108112
super().release_zoom(event)
109113
self.canvas.remove_rubberband()
110114

111-
def set_cursor(self, cursor):
112-
_macosx.set_cursor(cursor)
113-
114115
def save_figure(self, *args):
115116
filename = _macosx.choose_save_file('Save the figure',
116117
self.canvas.get_default_filename())

lib/matplotlib/backends/backend_qt5.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ def showEvent(self, event):
241241
window.screenChanged.connect(self._update_screen)
242242
self._update_screen(window.screen())
243243

244+
def set_cursor(self, cursor):
245+
# docstring inherited
246+
self.setCursor(_api.check_getitem(cursord, cursor=cursor))
247+
244248
def enterEvent(self, event):
245249
try:
246250
x, y = self.mouseEventCoords(event.pos())
@@ -702,9 +706,6 @@ def set_message(self, s):
702706
if self.coordinates:
703707
self.locLabel.setText(s)
704708

705-
def set_cursor(self, cursor):
706-
self.canvas.setCursor(cursord[cursor])
707-
708709
def draw_rubberband(self, event, x0, y0, x1, y1):
709710
height = self.canvas.figure.bbox.height
710711
y1 = height - y1

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ def blit(self, bbox=None):
157157
def draw_idle(self):
158158
self.send_event("draw")
159159

160+
def set_cursor(self, cursor):
161+
# docstring inherited
162+
cursor = _api.check_getitem({
163+
backend_tools.Cursors.HAND: 'pointer',
164+
backend_tools.Cursors.POINTER: 'default',
165+
backend_tools.Cursors.SELECT_REGION: 'crosshair',
166+
backend_tools.Cursors.MOVE: 'move',
167+
backend_tools.Cursors.WAIT: 'wait',
168+
backend_tools.Cursors.RESIZE_HORIZONTAL: 'ew-resize',
169+
backend_tools.Cursors.RESIZE_VERTICAL: 'ns-resize',
170+
}, cursor=cursor)
171+
self.send_event('cursor', cursor=cursor)
172+
160173
def set_image_mode(self, mode):
161174
"""
162175
Set the image mode for any subsequent images which will be sent
@@ -362,30 +375,18 @@ class NavigationToolbar2WebAgg(backend_bases.NavigationToolbar2):
362375
if name_of_method in _ALLOWED_TOOL_ITEMS
363376
]
364377

378+
cursor = _api.deprecate_privatize_attribute("3.5")
379+
365380
def __init__(self, canvas):
366381
self.message = ''
367-
self.cursor = None
382+
self._cursor = None # Remove with deprecation.
368383
super().__init__(canvas)
369384

370385
def set_message(self, message):
371386
if message != self.message:
372387
self.canvas.send_event("message", message=message)
373388
self.message = message
374389

375-
def set_cursor(self, cursor):
376-
if cursor != self.cursor:
377-
cursor = {
378-
backend_tools.Cursors.HAND: 'pointer',
379-
backend_tools.Cursors.POINTER: 'default',
380-
backend_tools.Cursors.SELECT_REGION: 'crosshair',
381-
backend_tools.Cursors.MOVE: 'move',
382-
backend_tools.Cursors.WAIT: 'wait',
383-
backend_tools.Cursors.RESIZE_HORIZONTAL: 'ew-resize',
384-
backend_tools.Cursors.RESIZE_VERTICAL: 'ns-resize',
385-
}[cursor]
386-
self.canvas.send_event("cursor", cursor=cursor)
387-
self.cursor = cursor
388-
389390
def draw_rubberband(self, event, x0, y0, x1, y1):
390391
self.canvas.send_event(
391392
"rubberband", x0=x0, y0=y0, x1=x1, y1=y1)

lib/matplotlib/backends/backend_wx.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ def _onKeyUp(self, event):
719719
if self:
720720
event.Skip()
721721

722+
def set_cursor(self, cursor):
723+
# docstring inherited
724+
cursor = wx.Cursor(_api.check_getitem(cursord, cursor=cursor))
725+
self.SetCursor(cursor)
726+
self.Update()
727+
722728
def _set_capture(self, capture=True):
723729
"""Control wx mouse capture."""
724730
if self.HasCapture():
@@ -1155,11 +1161,6 @@ def save_figure(self, *args):
11551161
except Exception as e:
11561162
error_msg_wx(str(e))
11571163

1158-
def set_cursor(self, cursor):
1159-
cursor = wx.Cursor(cursord[cursor])
1160-
self.canvas.SetCursor(cursor)
1161-
self.canvas.Update()
1162-
11631164
def draw_rubberband(self, event, x0, y0, x1, y1):
11641165
height = self.canvas.figure.bbox.height
11651166
self.canvas._rubberband_rect = (x0, height - y0, x1, height - y1)

0 commit comments

Comments
 (0)