8000 Qt Gcf refactor · OceanWolf/matplotlib@73bb8cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 73bb8cf

Browse files
committed
Qt Gcf refactor
1 parent 4df6923 commit 73bb8cf

File tree

5 files changed

+106
-20
lines changed

5 files changed

+106
-20
lines changed

lib/matplotlib/backend_managers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ def _get_toolbar(self):
9898
# must be inited after the window, drawingArea and figure
9999
# attrs are set
100100
if rcParams['toolbar'] == 'toolbar2':
101-
toolbar = Toolbar2(self.canvas, self.window)
101+
# Short term hack until toolbar2 gets removed.
102+
if 'qt' in str(FigureCanvas):
103+
toolbar = Toolbar2(self.canvas, self.window, False)
104+
else:
105+
toolbar = Toolbar2(self.canvas, self.window)
102106
else:
103107
toolbar = None
104108
return toolbar

lib/matplotlib/backends/backend_qt4.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
SHIFT, MODIFIER_KEYS, fn_name, cursord,
3737
draw_if_interactive, _create_qApp, show, TimerQT,
3838
MainWindow, FigureManagerQT, NavigationToolbar2QT,
39-
SubplotToolQt, error_msg_qt, exception_handler)
39+
SubplotToolQt, error_msg_qt, exception_handler,
40+
Window, MainLoop)
4041

4142
from .backend_qt5 import FigureCanvasQT as FigureCanvasQT5
4243

@@ -62,14 +63,14 @@ def new_figure_manager_given_figure(num, figure):
6263

6364
class FigureCanvasQT(FigureCanvasQT5):
6465

65-
def __init__(self, figure):
66+
def __init__(self, figure, manager=None):
6667
if DEBUG:
6768
print('FigureCanvasQt qt4: ', figure)
6869
_create_qApp()
6970

7071
# Note different super-calling style to backend_qt5
7172
QtWidgets.QWidget.__init__(self)
72-
FigureCanvasBase.__init__(self, figure)
73+
FigureCanvasBase.__init__(self, figure, manager)
7374
self.figure = figure
7475
self.setMouseTracking(True)
7576
self._idle = True
@@ -78,6 +79,17 @@ def __init__(self, figure):
7879
w, h = self.get_width_height()
7980
self.resize(w, h)
8081

82+
# Give the keyboard focus to the figure instead of the
83+
# manager; StrongFocus accepts both tab and click to focus and
84+
# will enable the canvas to process event w/o clicking.
85+
# ClickFocus only takes the focus is the window has been
86+
# clicked
87+
# on. http://qt-project.org/doc/qt-4.8/qt.html#FocusPolicy-enum or
88+
# http://doc.qt.digia.com/qt/qt.html#FocusPolicy-enum
89+
if manager:
90+
self.setFocusPolicy(QtCore.Qt.StrongFocus)
91+
self.setFocus()
92+
8193
def wheelEvent(self, event):
8294
x = event.x()
8395
# flipy so y=0 is bottom of canvas

lib/matplotlib/backends/backend_qt4agg.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
from .backend_agg import FigureCanvasAgg
2121
from .backend_qt4 import QtCore
22-
from .backend_qt4 import FigureManagerQT
22+
from .backend_qt4 import FigureManagerQT, Window
2323
from .backend_qt4 import FigureCanvasQT
2424
from .backend_qt4 import NavigationToolbar2QT
2525
##### not used
26-
from .backend_qt4 import show
26+
from .backend_qt4 import show, MainLoop
2727
from .backend_qt4 import draw_if_interactive
2828
from .backend_qt4 import backend_version
2929
######
@@ -65,11 +65,11 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase,
6565
figure - A Figure instance
6666
"""
6767

68-
def __init__(self, figure):
68+
def __init__(self, figure, manager=None):
6969
if DEBUG:
7070
print('FigureCanvasQtAgg: ', figure)
71-
FigureCanvasQT.__init__(self, figure)
72-
FigureCanvasAgg.__init__(self, figure)
71+
FigureCanvasQT.__init__(self, figure, manager)
72+
FigureCanvasAgg.__init__(self, figure, manager)
7373
self._drawRect = None
7474
self.blitbox = None
7575
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
@@ -93,3 +93,4 @@ def __init__(self, figure):
9393

9494
FigureCanvas = FigureCanvasQTAgg
9595
FigureManager = FigureManagerQT
96+
Toolbar2 = NavigationToolbar2QT

lib/matplotlib/backends/backend_qt5.py

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import matplotlib
1212

1313
from matplotlib.cbook import is_string_like
14-
from matplotlib.backend_bases import FigureManagerBase
14+
from matplotlib.backend_bases import WindowBase, FigureManagerBase
1515
from matplotlib.backend_bases import FigureCanvasBase
1616
from matplotlib.backend_bases import NavigationToolbar2
1717

1818
from matplotlib.backend_bases import cursors
1919
from matplotlib.backend_bases import TimerBase
20-
from matplotlib.backend_bases import ShowBase
20+
from matplotlib.backend_bases import MainLoopBase, ShowBase
2121

2222
from matplotlib._pylab_helpers import Gcf
2323
from matplotlib.figure import Figure
@@ -143,6 +143,18 @@ def _create_qApp():
143143
qApp = app
144144

145145

146+
class MainLoop(MainLoopBase):
147+
def __init__(self):
148+
MainLoopBase.__init__(self)
149+
_create_qApp()
150+
151+
def begin(self):
152+
# allow KeyboardInterrupt exceptions to close the plot window.
153+
signal.signal(signal.SIGINT, signal.SIG_DFL)
154+
global qApp
155+
qApp.exec_()
156+
157+
146158
class Show(ShowBase):
147159
def mainloop(self):
148160
# allow KeyboardInterrupt exceptions to close the plot window.
@@ -226,15 +238,14 @@ class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
226238
# QtCore.Qt.XButton2: None,
227239
}
228240

229-
def __init__(self, figure):
241+
def __init__(self, figure, manager=None):
230242
if DEBUG:
231243
print('FigureCanvasQt qt5: ', figure)
232244
_create_qApp()
233-
234245
# NB: Using super for this call to avoid a TypeError:
235246
# __init__() takes exactly 2 arguments (1 given) on QWidget
236247
# PyQt5
237-
super(FigureCanvasQT, self).__init__(figure=figure)
248+
super(FigureCanvasQT, self).__init__(figure=figure, manager=manager)
238249
self.figure = figure
239250
self.setMouseTracking(True)
240251
self._idle = True
@@ -243,6 +254,17 @@ def __init__(self, figure):
243254
w, h = self.get_width_height()
244255
self.resize(w, h)
245256

257+
# Give the keyboard focus to the figure instead of the
258+
# manager; StrongFocus accepts both tab and click to focus and
259+
# will enable the canvas to process event w/o clicking.
260+
# ClickFocus only takes the focus is the window has been
261+
# clicked
262+
# on. http://qt-project.org/doc/qt-4.8/qt.html#FocusPolicy-enum or
263+
# http://doc.qt.digia.com/qt/qt.html#FocusPolicy-enum
264+
if manager:
265+
self.setFocusPolicy(QtCore.Qt.StrongFocus)
266+
self.setFocus()
267+
246268
def __timerEvent(self, event):
247269
# hide until we can test and fix
248270
self.mpl_idle_event(event)
@@ -442,6 +464,52 @@ def closeEvent(self, event):
442464
QtWidgets.QMainWindow.closeEvent(self, event)
443465

444466

467+
class Window(WindowBase, MainWindow):
468+
def __init__(self, title):
469+
WindowBase.__init__(self, title)
470+
MainWindow.__init__(self)
471+
self.closing.connect(self.destroy_event)
472+
473+
self.setWindowTitle(title)
474+
image = os.path.join(matplotlib.rcParams['datapath'],
475+
'images', 'matplotlib.png')
476+
self.setWindowIcon(QtGui.QIcon(image))
477+
478+
def add_element_to_window(self, element, expand, fill, pad, side='bottom'):
479+
h = element.sizeHint().height()
480+
# TODO Hack until NavigationToolbar2 becomes obsolete
481+
if type(element) == NavigationToolbar2QT:
482+
self.addToolBar(element)
483+
sb = self.statusBar()
484+
element.message.connect(self._show_message)
485+
return h + sb.sizeHint().height()
486+
elif isinstance(element, FigureCanvasBase):
487+
self.setCentralWidget(element)
488+
return h
489+
490+
def show(self):
491+
MainWindow.show(self)
492+
493+
def destroy(self, *args):
494+
self.close()
495+
496+
def set_fullscreen(self, fullscreen):
497+
if fullscreen:
498+
self.window.showFullScreen()
499+
else:
500+
self.window.showNormal()
501+
502+
def get_window_title(self):
503+
return str(self.windowTitle())
504+
505+
def set_window_title(self, title):
506+
self.setWindowTitle(title)
507+
508+
@QtCore.Slot(str)
509+
def _show_message(self, s):
510+
# Fixes a PySide segfault.
511+
self.statusBar().showMessage(s)
512+
445513
class FigureManagerQT(FigureManagerBase):
446514
"""
447515
Public attributes
@@ -473,7 +541,7 @@ def __init__(self, canvas, num):
473541
# clicked
474542
# on. http://qt-project.org/doc/qt-4.8/qt.html#FocusPolicy-enum or
475543
# http://doc.qt.digia.com/qt/qt.html#FocusPolicy-enum
476-
self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus)
544+
self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus) # TODO take a look at this
477545
self.canvas.setFocus()
478546

479547
self.window._destroying = False

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
from .backend_agg import FigureCanvasAgg
1515
from .backend_qt5 import QtCore
1616
from .backend_qt5 import QtGui
17-
from .backend_qt5 import FigureManagerQT
17+
from .backend_qt5 import Window, FigureManagerQT
1818
from .backend_qt5 import NavigationToolbar2QT
1919
##### Modified Qt5 backend import
2020
from .backend_qt5 import FigureCanvasQT
2121
##### not used
22-
from .backend_qt5 import show
22+
from .backend_qt5 import MainLoop, show
2323
from .backend_qt5 import draw_if_interactive
2424
from .backend_qt5 import backend_version
2525
######
@@ -170,11 +170,11 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase,
170170
figure - A Figure instance
171171
"""
172172

173-
def __init__(self, figure):
173+
def __init__(self, figure, manager=None):
174174
if DEBUG:
175175
print('FigureCanvasQtAgg: ', figure)
176-
FigureCanvasQT.__init__(self, figure)
177-
FigureCanvasAgg.__init__(self, figure)
176+
FigureCanvasQT.__init__(self, figure, manager)
177+
FigureCanvasAgg.__init__(self, figure, manager)
178178
self._drawRect = None
179179
self.blitbox = None
180180
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
@@ -198,3 +198,4 @@ def __init__(self, figure):
198198

199199
FigureCanvas = FigureCanvasQTAgg
200200
FigureManager = FigureManagerQT
201+
Toolbar2 = NavigationToolbar2QT

0 commit comments

Comments
 (0)
0