Skip to content

Move required_interactive_framework to canvas class. #14521

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 1 commit into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/next_api_changes/2019-06-12-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
API changes
```````````

The ``required_interactive_framework`` attribute of backend modules introduced
in Matplotlib 3.0 has been moved to the FigureCanvas class, in order to let it
be inherited by third-party canvas subclasses and to make it easier to know
what interactive framework is required by a canvas class.

``backend_qt4.FigureCanvasQT5``, which is an alias for
``backend_qt5.FigureCanvasQT`` (but only exists under that name in
``backend_qt4``), has been removed.
30 changes: 15 additions & 15 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1560,8 +1560,12 @@ class FigureCanvasBase:
----------
figure : `matplotlib.figure.Figure`
A high-level figure instance

"""

# Set to one of {"qt5", "qt4", "gtk3", "wx", "tk", "macosx"} if an
# interactive framework is required, or None otherwise.
required_interactive_framework = None

events = [
'resize_event',
'draw_event',
Expand Down Expand Up @@ -1633,8 +1637,7 @@ def _fix_ipython_backend2gui(cls):
# In case we ever move the patch to IPython and remove these APIs,
# don't break on our side.
return
backend_mod = sys.modules[cls.__module__]
rif = getattr(backend_mod, "required_interactive_framework", None)
rif = getattr(cls, "required_interactive_framework", None)
backend2gui_rif = {"qt5": "qt", "qt4": "qt", "gtk3": "gtk3",
"wx": "wx", "macosx": "osx"}.get(rif)
if backend2gui_rif:
Expand Down Expand Up @@ -3255,10 +3258,6 @@ class _Backend:
# class FooBackend(_Backend):
# # override the attributes and methods documented below.

# Set to one of {"qt5", "qt4", "gtk3", "wx", "tk", "macosx"} if an
# interactive framework is required, or None otherwise.
required_interactive_framework = None

# `backend_version` may be overridden by the subclass.
backend_version = "unknown"

Expand Down Expand Up @@ -3341,14 +3340,15 @@ def show(cls, block=None):

@staticmethod
def export(cls):
for name in ["required_interactive_framework",
"backend_version",
"FigureCanvas",
"FigureManager",
"new_figure_manager",
"new_figure_manager_given_figure",
"draw_if_interactive",
"show"]:
for name in [
"backend_version",
"FigureCanvas",
"FigureManager",
"new_figure_manager",
"new_figure_manager_given_figure",
"draw_if_interactive",
"show",
]:
setattr(sys.modules[cls.__module__], name, getattr(cls, name))

# For back-compatibility, generate a shim `Show` class.
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def _on_timer(self):


class FigureCanvasTk(FigureCanvasBase):
required_interactive_framework = "tk"

keyvald = {65507: 'control',
65505: 'shift',
65513: 'alt',
Expand Down Expand Up @@ -868,7 +870,6 @@ def trigger(self, *args):

@_Backend.export
class _BackendTk(_Backend):
required_interactive_framework = "tk"
FigureManager = FigureManagerTk

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def _on_timer(self):


class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
required_interactive_framework = "gtk3"

keyvald = {65507: 'control',
65505: 'shift',
65513: 'alt',
Expand Down Expand Up @@ -978,7 +980,6 @@ def error_msg_gtk(msg, parent=None):

@_Backend.export
class _BackendGTK3(_Backend):
required_interactive_framework = "gtk3"
FigureCanvas = FigureCanvasGTK3
FigureManager = FigureManagerGTK3

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ class FigureCanvasMac(_macosx.FigureCanvas, FigureCanvasAgg):
----------
figure : `matplotlib.figure.Figure`
A high-level Figure instance

"""

required_interactive_framework = "macosx"

def __init__(self, figure):
FigureCanvasBase.__init__(self, figure)
width, height = self.get_width_height()
Expand Down Expand Up @@ -172,7 +173,6 @@ def set_message(self, message):

@_Backend.export
class _BackendMac(_Backend):
required_interactive_framework = "macosx"
FigureCanvas = FigureCanvasMac
FigureManager = FigureManagerMac

Expand Down
9 changes: 5 additions & 4 deletions lib/matplotlib/backends/backend_qt4.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from .backend_qt5 import (
backend_version, SPECIAL_KEYS, SUPER, ALT, CTRL, SHIFT, MODIFIER_KEYS,
cursord, _create_qApp, _BackendQT5, TimerQT, MainWindow, FigureManagerQT,
NavigationToolbar2QT, SubplotToolQt, error_msg_qt, exception_handler)
from .backend_qt5 import FigureCanvasQT as FigureCanvasQT5
cursord, _create_qApp, _BackendQT5, TimerQT, MainWindow, FigureCanvasQT,
FigureManagerQT, NavigationToolbar2QT, SubplotToolQt, error_msg_qt,
exception_handler)


@_BackendQT5.export
class _BackendQT4(_BackendQT5):
required_interactive_framework = "qt4"
class FigureCanvas(FigureCanvasQT):
required_interactive_framework = "qt4"
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/backend_qt4agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

@_BackendQT5Agg.export
class _BackendQT4Agg(_BackendQT5Agg):
required_interactive_framework = "qt4"
class FigureCanvas(FigureCanvasQTAgg):
required_interactive_framework = "qt4"
5 changes: 3 additions & 2 deletions lib/matplotlib/backends/backend_qt4cairo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .backend_qt5cairo import _BackendQT5Cairo
from .backend_qt5cairo import _BackendQT5Cairo, FigureCanvasQTCairo


@_BackendQT5Cairo.export
class _BackendQT4Cairo(_BackendQT5Cairo):
required_interactive_framework = "qt4"
class FigureCanvas(FigureCanvasQTCairo):
required_interactive_framework = "qt4"
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def _timer_stop(self):


class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
required_interactive_framework = "qt5"

# map Qt button codes to MouseEvent's ones:
buttond = {QtCore.Qt.LeftButton: MouseButton.LEFT,
Expand Down Expand Up @@ -1029,7 +1030,6 @@ def trigger(self, *args, **kwargs):

@_Backend.export
class _BackendQT5(_Backend):
required_interactive_framework = "qt5"
FigureCanvas = FigureCanvasQT
FigureManager = FigureManagerQT

Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ class _FigureCanvasWxBase(FigureCanvasBase, wx.Panel):
we give a hint as to our preferred minimum size.
"""

required_interactive_framework = "wx"

keyvald = {
wx.WXK_CONTROL: 'control',
wx.WXK_SHIFT: 'shift',
Expand Down Expand Up @@ -1933,7 +1935,6 @@ def OnPrintPage(self, page):

@_Backend.export
class _BackendWx(_Backend):
required_interactive_framework = "wx"
FigureCanvas = FigureCanvasWx
FigureManager = FigureManagerWx
_frame_class = FigureFrameWx
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ def switch_backend(newbackend):
_log.debug("Loaded backend %s version %s.",
newbackend, Backend.backend_version)

required_framework = Backend.required_interactive_framework
required_framework = getattr(
Backend.FigureCanvas, "required_interactive_framework", None)
if required_framework is not None:
current_framework = \
matplotlib.backends._get_running_interactive_framework()
Expand Down