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

Skip to content

Commit 959e76f

Browse files
authored
Merge pull request #22000 from anntzer/gtkclean
Some gtk cleanups.
2 parents 72f4046 + 2a71a27 commit 959e76f

File tree

3 files changed

+29
-46
lines changed

3 files changed

+29
-46
lines changed

lib/matplotlib/backends/_backend_gtk.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import logging
66

77
import matplotlib as mpl
8-
from matplotlib import backend_tools, cbook
9-
from matplotlib.backend_bases import (
10-
_Backend, NavigationToolbar2, TimerBase,
11-
)
8+
from matplotlib import _api, backend_tools, cbook
9+
from matplotlib.backend_bases import _Backend, NavigationToolbar2, TimerBase
10+
from matplotlib.backend_tools import Cursors
1211

1312
# The GTK3/GTK4 backends will have already called `gi.require_version` to set
1413
# the desired GTK.
@@ -62,6 +61,18 @@ def _create_application():
6261
return _application
6362

6463

64+
def mpl_to_gtk_cursor_name(mpl_cursor):
65+
return _api.check_getitem({
66+
Cursors.MOVE: "move",
67+
Cursors.HAND: "pointer",
68+
Cursors.POINTER: "default",
69+
Cursors.SELECT_REGION: "crosshair",
70+
Cursors.WAIT: "wait",
71+
Cursors.RESIZE_HORIZONTAL: "ew-resize",
72+
Cursors.RESIZE_VERTICAL: "ns-resize",
73+
}, cursor=mpl_cursor)
74+
75+
6576
class TimerGTK(TimerBase):
6677
"""Subclass of `.TimerBase` using GTK timer events."""
6778

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
from matplotlib import _api, backend_tools, cbook
99
from matplotlib._pylab_helpers import Gcf
1010
from matplotlib.backend_bases import (
11-
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
12-
TimerBase, ToolContainerBase)
11+
FigureCanvasBase, FigureManagerBase, ToolContainerBase)
1312
from matplotlib.backend_tools import Cursors
1413
from matplotlib.figure import Figure
15-
from matplotlib.widgets import SubplotTool
1614

1715
try:
1816
import gi
@@ -31,7 +29,6 @@
3129
from gi.repository import Gio, GLib, GObject, Gtk, Gdk
3230
from . import _backend_gtk
3331
from ._backend_gtk import (
34-
_create_application, _shutdown_application,
3532
backend_version, _BackendGTK, _NavigationToolbar2GTK,
3633
TimerGTK as TimerGTK3,
3734
)
@@ -68,16 +65,9 @@ def cursord(self):
6865

6966
@functools.lru_cache()
7067
def _mpl_to_gtk_cursor(mpl_cursor):
71-
name = _api.check_getitem({
72-
Cursors.MOVE: "move",
73-
Cursors.HAND: "pointer",
74-
Cursors.POINTER: "default",
75-
Cursors.SELECT_REGION: "crosshair",
76-
Cursors.WAIT: "wait",
77-
Cursors.RESIZE_HORIZONTAL: "ew-resize",
78-
Cursors.RESIZE_VERTICAL: "ns-resize",
79-
}, cursor=mpl_cursor)
80-
return Gdk.Cursor.new_from_name(Gdk.Display.get_default(), name)
68+
return Gdk.Cursor.new_from_name(
69+
Gdk.Display.get_default(),
70+
_backend_gtk.mpl_to_gtk_cursor_name(mpl_cursor))
8171

8272

8373
class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
@@ -317,10 +307,10 @@ class FigureManagerGTK3(FigureManagerBase):
317307
The Gtk.VBox containing the canvas and toolbar
318308
window : Gtk.Window
319309
The Gtk.Window
320-
321310
"""
311+
322312
def __init__(self, canvas, num):
323-
app = _create_application()
313+
app = _backend_gtk._create_application()
324314
self.window = Gtk.Window()
325315
app.add_window(self.window)
326316
super().__init__(canvas, num)
@@ -481,7 +471,7 @@ def __init__(self, canvas, window):
481471

482472
self.show_all()
483473

484-
NavigationToolbar2.__init__(self, canvas)
474+
_NavigationToolbar2GTK.__init__(self, canvas)
485475

486476
def save_figure(self, *args):
487477
dialog = Gtk.FileChooserDialog(
@@ -744,7 +734,7 @@ def error_msg_gtk(msg, parent=None):
744734
FigureCanvasGTK3, _backend_gtk.RubberbandGTK)
745735

746736

747-
@_Backend.export
737+
@_BackendGTK.export
748738
class _BackendGTK3(_BackendGTK):
749739
FigureCanvas = FigureCanvasGTK3
750740
FigureManager = FigureManagerGTK3

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
import io
33
import os
44
from pathlib import Path
5-
import sys
65

76
import matplotlib as mpl
87
from matplotlib import _api, backend_tools, cbook
98
from matplotlib._pylab_helpers import Gcf
109
from matplotlib.backend_bases import (
11-
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
12-
TimerBase, ToolContainerBase)
13-
from matplotlib.backend_tools import Cursors
14-
from matplotlib.figure import Figure
15-
from matplotlib.widgets import SubplotTool
10+
FigureCanvasBase, FigureManagerBase, ToolContainerBase)
1611

1712
try:
1813
import gi
@@ -31,24 +26,11 @@
3126
from gi.repository import Gio, GLib, GObject, Gtk, Gdk, GdkPixbuf
3227
from . import _backend_gtk
3328
from ._backend_gtk import (
34-
_create_application, _shutdown_application,
3529
backend_version, _BackendGTK, _NavigationToolbar2GTK,
3630
TimerGTK as TimerGTK4,
3731
)
3832

3933

40-
def _mpl_to_gtk_cursor(mpl_cursor):
41-
return _api.check_getitem({
42-
Cursors.MOVE: "move",
43-
Cursors.HAND: "pointer",
44-
Cursors.POINTER: "default",
45-
Cursors.SELECT_REGION: "crosshair",
46-
Cursors.WAIT: "wait",
47-
Cursors.RESIZE_HORIZONTAL: "ew-resize",
48-
Cursors.RESIZE_VERTICAL: "ns-resize",
49-
}, cursor=mpl_cursor)
50-
51-
5234
class FigureCanvasGTK4(Gtk.DrawingArea, FigureCanvasBase):
5335
required_interactive_framework = "gtk4"
5436
supports_blit = False
@@ -108,7 +90,7 @@ def destroy(self):
10890

10991
def set_cursor(self, cursor):
11092
# docstring inherited
111-
self.set_cursor_from_name(_mpl_to_gtk_cursor(cursor))
93+
self.set_cursor_from_name(_backend_gtk.mpl_to_gtk_cursor_name(cursor))
11294

11395
def _mouse_event_coords(self, x, y):
11496
"""
@@ -281,10 +263,10 @@ class FigureManagerGTK4(FigureManagerBase):
281263
The Gtk.VBox containing the canvas and toolbar
282264
window : Gtk.Window
283265
The Gtk.Window
284-
285266
"""
267+
286268
def __init__(self, canvas, num):
287-
app = _create_application()
269+
app = _backend_gtk._create_application()
288270
self.window = Gtk.Window()
289271
app.add_window(self.window)
290272
super().__init__(canvas, num)
@@ -422,7 +404,7 @@ def __init__(self, canvas, window):
422404
self.message = Gtk.Label()
423405
self.append(self.message)
424406

425-
NavigationToolbar2.__init__(self, canvas)
407+
_NavigationToolbar2GTK.__init__(self, canvas)
426408

427409
def save_figure(self, *args):
428410
dialog = Gtk.FileChooserNative(
@@ -688,7 +670,7 @@ def trigger(self, *args, **kwargs):
688670
Toolbar = ToolbarGTK4
689671

690672

691-
@_Backend.export
673+
@_BackendGTK.export
692674
class _BackendGTK4(_BackendGTK):
693675
FigureCanvas = FigureCanvasGTK4
694676
FigureManager = FigureManagerGTK4

0 commit comments

Comments
 (0)