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

Skip to content

Commit bad102e

Browse files
committed
Merge remote-tracking branch 'upstream/v1.2.x'
Conflicts: lib/matplotlib/backends/backend_qt4.py
2 parents daf551d + 7147317 commit bad102e

File tree

3 files changed

+66
-24
lines changed

3 files changed

+66
-24
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ def tk_window_focus():
11281128
'matplotlib.tests.test_triangulation',
11291129
'matplotlib.tests.test_transforms',
11301130
'matplotlib.tests.test_arrow_patches',
1131+
'matplotlib.tests.test_backend_qt4',
11311132
]
11321133

11331134

lib/matplotlib/backends/backend_qt4.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,14 @@ def idle_draw(*args):
367367
self._idle = True
368368
if d: QtCore.QTimer.singleShot(0, idle_draw)
369369

370+
370371
class MainWindow(QtGui.QMainWindow):
371372
def closeEvent(self, event):
372373
self.emit(QtCore.SIGNAL('closing()'))
373374
QtGui.QMainWindow.closeEvent(self, event)
374375

375-
class FigureManagerQT( FigureManagerBase ):
376+
377+
class FigureManagerQT(FigureManagerBase):
376378
"""
377379
Public attributes
378380
@@ -382,29 +384,31 @@ class FigureManagerQT( FigureManagerBase ):
382384
window : The qt.QMainWindow
383385
"""
384386

385-
def __init__( self, canvas, num ):
386-
if DEBUG: print('FigureManagerQT.%s' % fn_name())
387-
FigureManagerBase.__init__( self, canvas, num )
387+
def __init__(self, canvas, num):
388+
if DEBUG:
389+
print('FigureManagerQT.%s' % fn_name())
390+
FigureManagerBase.__init__(self, canvas, num)
388391
self.canvas = canvas
389392
self.window = MainWindow()
390393
self.window.connect(self.window, QtCore.SIGNAL('closing()'),
391-
canvas.close_event)
394+
canvas.close_event)
395+
self.window.connect(self.window, QtCore.SIGNAL('closing()'),
396+
self._widgetclosed)
392397

393398
self.window.setWindowTitle("Figure %d" % num)
394-
image = os.path.join( matplotlib.rcParams['datapath'],'images','matplotlib.png' )
395-
self.window.setWindowIcon(QtGui.QIcon( image ))
399+
image = os.path.join(matplotlib.rcParams['datapath'], 'images', 'matplotlib.png')
400+
self.window.setWindowIcon(QtGui.QIcon(image))
396401

397402
# Give the keyboard focus to the figure instead of the
398403
# manager; StrongFocus accepts both tab and click to focus and
399404
# will enable the canvas to process event w/o clicking.
400405
# ClickFocus only takes the focus is the window has been
401406
# clicked
402-
# on. http://developer.qt.nokia.com/doc/qt-4.8/qt.html#FocusPolicy-enum
403-
self.canvas.setFocusPolicy( QtCore.Qt.StrongFocus )
407+
# on. http://qt-project.org/doc/qt-4.8/qt.html#FocusPolicy-enum or
408+
# http://doc.qt.digia.com/qt/qt.html#FocusPolicy-enum
409+
self.canvas.setFocusPolicy(QtCore.Qt.StrongFocus)
404410
self.canvas.setFocus()
405411

406-
QtCore.QObject.connect( self.window, QtCore.SIGNAL( 'destroyed()' ),
407-
self._widgetclosed )
408412
self.window._destroying = False
409413

410414
self.toolbar = self._get_toolbar(self.canvas, self.window)
@@ -420,7 +424,7 @@ def __init__( self, canvas, num ):
420424
# requested size:
421425
cs = canvas.sizeHint()
422426
sbs = self.window.statusBar().sizeHint()
423-
self._status_and_tool_height = tbs_height+sbs.height()
427+
self._status_and_tool_height = tbs_height + sbs.height()
424428
height = cs.height() + self._status_and_tool_height
425429
self.window.resize(cs.width(), height)
426430

@@ -429,14 +433,14 @@ def __init__( self, canvas, num ):
429433
if matplotlib.is_interactive():
430434
self.window.show()
431435

432-
def notify_axes_change( fig ):
436+
def notify_axes_change(fig):
433437
# This will be called whenever the current axes is changed
434438
if self.toolbar is not None:
435439
self.toolbar.update()
436-
self.canvas.figure.add_axobserver( notify_axes_change )
440+
self.canvas.figure.add_axobserver(notify_axes_change)
437441

438442
@QtCore.Slot()
439-
def _show_message(self,s):
443+
def _show_message(self, s):
440444
# Fixes a PySide segfault.
441445
self.window.statusBar().showMessage(s)
442446

@@ -446,8 +450,9 @@ def full_screen_toggle(self):
446450
else:
447451
self.window.showFullScreen()
448452

449-
def _widgetclosed( self ):
450-
if self.window._destroying: return
453+
def _widgetclosed(self):
454+
if self.window._destroying:
455+
return
451456
self.window._destroying = True
452457
try:
453458
Gcf.destroy(self.num)
@@ -475,15 +480,19 @@ def resize(self, width, height):
475480
def show(self):
476481
self.window.show()
477482

478-
def destroy( self, *args ):
483+
def destroy(self, *args):
479484
# check for qApp first, as PySide deletes it in its atexit handler
480-
if QtGui.QApplication.instance() is None: return
481-
if self.window._destroying: return
485+
if QtGui.QApplication.instance() is None:
486+
return
487+
if self.window._destroying:
488+
return
482489
self.window._destroying = True
483-
QtCore.QObject.disconnect( self.window, QtCore.SIGNAL( 'destroyed()' ),
484-
self._widgetclosed )
485-
if self.toolbar: self.toolbar.destroy()
486-
if DEBUG: print("destroy figure manager")
490+
QtCore.QObject.disconnect(self.window, QtCore.SIGNAL('destroyed()'),
491+
self._widgetclosed)
492+
if self.toolbar:
493+
self.toolbar.destroy()
494+
if DEBUG:
495+
print("destroy figure manager")
487496
self.window.close()
488497

489498
def get_window_title(self):
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from matplotlib import pyplot as plt
2+
from matplotlib.testing.decorators import cleanup
3+
from matplotlib.testing.decorators import knownfailureif
4+
from matplotlib._pylab_helpers import Gcf
5+
import copy
6+
7+
try:
8+
import matplotlib.backends.qt4_compat
9+
HAS_QT = True
10+
except ImportError:
11+
HAS_QT = False
12+
13+
14+
@cleanup
15+
@knownfailureif(not HAS_QT)
16+
def test_fig_close():
17+
# force switch to the Qt4 backend
18+
plt.switch_backend('Qt4Agg')
19+
20+
#save the state of Gcf.figs
21+
init_figs = copy.copy(Gcf.figs)
22+
23+
# make a figure using pyplot interface
24+
fig = plt.figure()
25+
26+
# simulate user clicking the close button by reaching in
27+
# and calling close on the underlying Qt object
28+
fig.canvas.manager.window.close()
29+
30+
# assert that we have removed the reference to the FigureManager
31+
# that got added by plt.figure()
32+
assert(init_figs == Gcf.figs)

0 commit comments

Comments
 (0)