From fe4102337065a26b552cdaffea037e228ebf9824 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Tue, 4 Sep 2012 22:07:25 -0400 Subject: [PATCH 1/2] STY: Refactor initialization of QApplication. --- skimage/viewer/utils/core.py | 21 +++++++++++++++++++-- skimage/viewer/viewers/core.py | 16 +++++----------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/skimage/viewer/utils/core.py b/skimage/viewer/utils/core.py index 5c5f878c..74fd333f 100644 --- a/skimage/viewer/utils/core.py +++ b/skimage/viewer/utils/core.py @@ -17,8 +17,25 @@ except ImportError: print("Could not import PyQt4 -- skimage.viewer not available.") -__all__ = ['figimage', 'LinearColormap', 'ClearColormap', 'MatplotlibCanvas', - 'RequiredAttr'] +__all__ = ['init_qtapp', 'start_qtapp', 'RequiredAttr', 'figimage', + 'LinearColormap', 'ClearColormap', 'MatplotlibCanvas'] + + +QApp = None + + +def init_qtapp(): + """Initialize QAppliction. + + The QApplication needs to be initialized before creating any QWidgets + """ + global QApp + if QApp is None: + QApp = QtGui.QApplication([]) + +def start_qtapp(): + """Start Qt mainloop""" + QApp.exec_() class RequiredAttr(object): diff --git a/skimage/viewer/viewers/core.py b/skimage/viewer/viewers/core.py index d29f950e..7c4b4753 100644 --- a/skimage/viewer/viewers/core.py +++ b/skimage/viewer/viewers/core.py @@ -1,8 +1,6 @@ """ ImageViewer class for viewing and interacting with images. """ -import sys - try: from PyQt4 import QtGui, QtCore from PyQt4.QtGui import QMainWindow @@ -11,20 +9,18 @@ except ImportError: print("Could not import PyQt4 -- skimage.viewer not available.") from skimage.util.dtype import dtype_range -from ..utils import figimage, MatplotlibCanvas +from .. import utils from ..widgets import Slider __all__ = ['ImageViewer', 'CollectionViewer'] -qApp = None - -class ImageCanvas(MatplotlibCanvas): +class ImageCanvas(utils.MatplotlibCanvas): """Canvas for displaying images.""" def __init__(self, parent, image, **kwargs): - self.fig, self.ax = figimage(image, **kwargs) + self.fig, self.ax = utils.figimage(image, **kwargs) super(ImageCanvas, self).__init__(parent, self.fig, **kwargs) @@ -60,9 +56,7 @@ class ImageViewer(QMainWindow): """ def __init__(self, image): # Start main loop - global qApp - if qApp is None: - qApp = QtGui.QApplication(sys.argv) + utils.init_qtapp() super(ImageViewer, self).__init__() #TODO: Add ImageViewer to skimage.io window manager @@ -136,7 +130,7 @@ class ImageViewer(QMainWindow): for p in self.plugins: p.show() super(ImageViewer, self).show() - qApp.exec_() + utils.start_qtapp() def redraw(self): self.canvas.draw_idle() From d1e012ea30798fe0fa636a18aca7deb9e0525420 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Tue, 4 Sep 2012 22:07:59 -0400 Subject: [PATCH 2/2] BUG: Initialize QApplication when creating Plugin. QWidgets cannot be initialized unless QApplication has been created. In cases where the Plugin is created before the ImageViewer, ensure that QApplication exists. --- skimage/viewer/plugins/base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/skimage/viewer/plugins/base.py b/skimage/viewer/plugins/base.py index ae240c20..074e18d4 100644 --- a/skimage/viewer/plugins/base.py +++ b/skimage/viewer/plugins/base.py @@ -14,7 +14,7 @@ try: except ImportError: print("Could not import matplotlib -- skimage.viewer not available.") -from ..utils import RequiredAttr +from ..utils import RequiredAttr, init_qtapp class Plugin(QDialog): @@ -82,6 +82,7 @@ class Plugin(QDialog): draws_on_image = False def __init__(self, image_filter=None, height=0, width=400, useblit=None): + init_qtapp() super(Plugin, self).__init__() self.image_viewer = None