11
11
import matplotlib
12
12
13
13
from matplotlib .cbook import is_string_like
14
- from matplotlib .backend_bases import FigureManagerBase
14
+ from matplotlib .backend_bases import WindowBase , FigureManagerBase
15
15
from matplotlib .backend_bases import FigureCanvasBase
16
16
from matplotlib .backend_bases import NavigationToolbar2
17
17
18
18
from matplotlib .backend_bases import cursors
19
19
from matplotlib .backend_bases import TimerBase
20
- from matplotlib .backend_bases import ShowBase
20
+ from matplotlib .backend_bases import MainLoopBase , ShowBase
21
21
22
22
from matplotlib ._pylab_helpers import Gcf
23
23
from matplotlib .figure import Figure
@@ -143,6 +143,18 @@ def _create_qApp():
143
143
qApp = app
144
144
145
145
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
+
146
158
class Show (ShowBase ):
147
159
def mainloop (self ):
148
160
# allow KeyboardInterrupt exceptions to close the plot window.
@@ -226,15 +238,14 @@ class FigureCanvasQT(QtWidgets.QWidget, FigureCanvasBase):
226
238
# QtCore.Qt.XButton2: None,
227
239
}
228
240
229
- def __init__ (self , figure ):
241
+ def __init__ (self , figure , manager = None ):
230
242
if DEBUG :
231
243
print ('FigureCanvasQt qt5: ' , figure )
232
244
_create_qApp ()
233
-
234
245
# NB: Using super for this call to avoid a TypeError:
235
246
# __init__() takes exactly 2 arguments (1 given) on QWidget
236
247
# PyQt5
237
- super (FigureCanvasQT , self ).__init__ (figure = figure )
248
+ super (FigureCanvasQT , self ).__init__ (figure = figure , manager = manager )
238
249
self .figure = figure
239
250
self .setMouseTracking (True )
240
251
self ._idle = True
@@ -243,6 +254,17 @@ def __init__(self, figure):
243
254
w , h = self .get_width_height ()
244
255
self .resize (w , h )
245
256
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
+
246
268
def __timerEvent (self , event ):
247
269
# hide until we can test and fix
248
270
self .mpl_idle_event (event )
@@ -442,6 +464,52 @@ def closeEvent(self, event):
442
464
QtWidgets .QMainWindow .closeEvent (self , event )
443
465
444
466
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
+
445
513
class FigureManagerQT (FigureManagerBase ):
446
514
"""
447
515
Public attributes
@@ -473,7 +541,7 @@ def __init__(self, canvas, num):
473
541
# clicked
474
542
# on. http://qt-project.org/doc/qt-4.8/qt.html#FocusPolicy-enum or
475
543
# 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
477
545
self .canvas .setFocus ()
478
546
479
547
self .window ._destroying = False
0 commit comments