Esempio n. 1
0
def quit_app():
    """Close all windows and quit the QApplication if napari started it."""
    QApplication.closeAllWindows()
    # if we started the application then the app will be named 'napari'.
    if (QApplication.applicationName() == 'napari'
            and not _ipython_has_eventloop()):
        QApplication.quit()

    # otherwise, something else created the QApp before us (such as
    # %gui qt IPython magic).  If we quit the app in this case, then
    # *later* attempts to instantiate a napari viewer won't work until
    # the event loop is restarted with app.exec_().  So rather than
    # quit just close all the windows (and clear our app icon).
    else:
        QApplication.setWindowIcon(QIcon())

    if perf.USE_PERFMON:
        # Write trace file before exit, if we were writing one.
        # Is there a better place to make sure this is done on exit?
        perf.timers.stop_trace_file()

    if config.monitor:
        # Stop the monitor service if we were using it
        from ..components.experimental.monitor import monitor

        monitor.stop()

    if config.async_loading:
        # Shutdown the chunkloader
        from ..components.experimental.chunk import chunk_loader

        chunk_loader.shutdown()
Esempio n. 2
0
def gui_qt(app_name):
    """Start a Qt event loop in which to run the application.

    Parameters
    ----------
    app_name: str

    Notes
    -----
    This context manager is not needed if running the app within an interactive
    IPython session. In this case, use the ``%gui qt`` magic command, or start
    IPython with the Qt GUI event loop enabled by default by using
    ``ipython --gui=qt``.
    """
    app = QApplication.instance()
    if not app:
        # automatically determine monitor DPI.
        # Note: this MUST be set before the QApplication is instantiated
        QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
        # if this is the first time the Qt app is being instantiated, we set
        # the name, so that we know whether to raise_ in Window.show()
        app = QApplication([app_name])
        global _our_app_name
        _our_app_name = app_name
    else:
        app._existed = True
    app.aboutToQuit.connect(wait_for_workers_to_quit)
    yield app
    # if the application already existed before this function was called,
    # there's no need to start it again.  By avoiding unnecessary calls to
    # ``app.exec_``, we avoid blocking.
    if app.applicationName() == app_name:
        app.exec_()
Esempio n. 3
0
def gui_qt(*, startup_logo=False):
    """Start a Qt event loop in which to run the application.

    Parameters
    ----------
    startup_logo : bool
        Show a splash screen with the napari logo during startup.

    Notes
    -----
    This context manager is not needed if running napari within an interactive
    IPython session. In this case, use the ``%gui qt`` magic command, or start
    IPython with the Qt GUI event loop enabled by default by using
    ``ipython --gui=qt``.
    """
    splash_widget = None
    app = QApplication.instance()
    if not app:
        # if this is the first time the Qt app is being instantiated, we set
        # the name, so that we know whether to raise_ in Window.show()
        app = QApplication(sys.argv)
        app.setApplicationName('napari')
        if startup_logo:
            logopath = join(dirname(__file__), '..', 'resources', 'logo.png')
            splash_widget = QSplashScreen(QPixmap(logopath).scaled(400, 400))
            splash_widget.show()
    yield app
    # if the application already existed before this function was called,
    # there's no need to start it again.  By avoiding unnecessary calls to
    # ``app.exec_``, we avoid blocking.
    if app.applicationName() == 'napari':
        if splash_widget and startup_logo:
            splash_widget.close()
        app.exec_()
Esempio n. 4
0
 def show_info(self):
     QMessageBox.about(
         self, QApplication.applicationName(),
         "%s %s\n"
         "Copyright (c) by %s" %
         (
             QCoreApplication.applicationName(),
             QCoreApplication.applicationVersion(),
             QCoreApplication.organizationName(),
         )
     )
Esempio n. 5
0
 def handle_exit():
     # if the event loop was started in gui_qt() then the app will be
     # named 'napari'. Since the Qapp was started by us, just close it.
     if QApplication.applicationName() == 'napari':
         QApplication.closeAllWindows()
         QApplication.quit()
     # otherwise, something else created the QApp before us (such as
     # %gui qt IPython magic).  If we quit the app in this case, then
     # *later* attemps to instantiate a napari viewer won't work until
     # the event loop is restarted with app.exec_().  So rather than
     # quit just close all the windows (and clear our app icon).
     else:
         QApplication.setWindowIcon(QIcon())
         self.close()
Esempio n. 6
0
        def handle_exit():
            # if the event loop was started in gui_qt() then the app will be
            # named 'napari'. Since the Qapp was started by us, just close it.
            if QApplication.applicationName() == 'napari':
                QApplication.closeAllWindows()
                QApplication.quit()
            # otherwise, something else created the QApp before us (such as
            # %gui qt IPython magic).  If we quit the app in this case, then
            # *later* attempts to instantiate a napari viewer won't work until
            # the event loop is restarted with app.exec_().  So rather than
            # quit just close all the windows (and clear our app icon).
            else:
                QApplication.setWindowIcon(QIcon())
                self.close()

            if perf.USE_PERFMON:
                # Write trace file before exit, if we were writing one.
                # Is there a better place to make sure this is done on exit?
                perf.timers.stop_trace_file()
Esempio n. 7
0
 def child_title(self, item):
     """Return data item title combined with QApplication title"""
     app_name = QApplication.applicationName()
     if not app_name:
         app_name = to_text_string(self.title())
     return "%s - %s" % (app_name, item.label())
Esempio n. 8
0
 def child_title(self, item):
     """Return data item title combined with QApplication title"""
     app_name = QApplication.applicationName()
     if not app_name:
         app_name = self.instance.get_title()
     return "%s - %s" % (app_name, item.label())