diff --git a/mne_qt_browser/_pg_figure.py b/mne_qt_browser/_pg_figure.py index b87cc476..752622d4 100644 --- a/mne_qt_browser/_pg_figure.py +++ b/mne_qt_browser/_pg_figure.py @@ -36,11 +36,10 @@ from pyqtgraph import (AxisItem, GraphicsView, InfLineLabel, InfiniteLine, LinearRegionItem, PlotCurveItem, PlotItem, Point, TextItem, ViewBox, mkBrush, - mkPen, setConfigOption, mkQApp, mkColor) + mkPen, setConfigOption, mkColor) from scipy.stats import zscore from mne.viz import plot_sensors -from mne.viz.backends._utils import _init_qt_resources from mne.viz._figure import BrowserBase from mne.viz.utils import _simplify_float, _merge_annotations from mne.annotations import _sync_onset @@ -64,6 +63,65 @@ def capture_exceptions(): name = 'pyqtgraph' +# This can be removed when mne==1.0 is released. +try: + from mne.viz.backends._utils import _init_mne_qtapp +except ImportError: + from mne.viz.backends._utils import _init_qt_resources + + def _init_mne_qtapp(enable_icon=True, pg_app=False): + """Get QApplication-instance for MNE-Python. + + Parameter + --------- + enable_icon: bool + If to set an MNE-icon for the app. + pg_app: bool + If to create the QApplication with pyqtgraph. For an until know + undiscovered reason the pyqtgraph-browser won't show without + mkQApp from pyqtgraph. + + Returns + ------- + app: ``PyQt5.QtWidgets.QApplication`` + Instance of QApplication. + """ + from PyQt5.QtWidgets import QApplication + from PyQt5.QtGui import QIcon + + app_name = 'MNE-Python' + organization_name = 'MNE' + + # Fix from cbrnr/mnelab for app name in menu bar + if sys.platform.startswith("darwin"): + try: + # set bundle name on macOS (app name shown in the menu bar) + from Foundation import NSBundle + bundle = NSBundle.mainBundle() + info = (bundle.localizedInfoDictionary() + or bundle.infoDictionary()) + info["CFBundleName"] = app_name + except ModuleNotFoundError: + pass + + if pg_app: + from pyqtgraph import mkQApp + app = mkQApp(app_name) + else: + app = (QApplication.instance() + or QApplication(sys.argv or [app_name])) + app.setApplicationName(app_name) + app.setOrganizationName(organization_name) + + if enable_icon: + # Set icon + _init_qt_resources() + kind = 'bigsur-' if platform.mac_ver()[0] >= '10.16' else '' + app.setWindowIcon(QIcon(f":/mne-{kind}icon.png")) + + return app + + def _get_std_icon(icon_name): return QApplication.instance().style().standardIcon( getattr(QStyle, icon_name)) @@ -3138,7 +3196,10 @@ def _update_data(self): def _get_zscore(self, data): # Reshape data to reasonable size for display - max_pixel_width = QApplication.desktop().screenGeometry().width() + if QApplication.desktop() is None: + max_pixel_width = 3840 # default=UHD + else: + max_pixel_width = QApplication.desktop().screenGeometry().width() collapse_by = data.shape[1] // max_pixel_width data = data[:, :max_pixel_width * collapse_by] if collapse_by > 0: @@ -3765,12 +3826,7 @@ def _mouseDrag(widget, positions, button, modifier=None): def _init_browser(**kwargs): setConfigOption('enableExperimental', True) - app = mkQApp() - app.setApplicationName('MNE-Python') - app.setOrganizationName('MNE') - _init_qt_resources() - kind = 'bigsur-' if platform.mac_ver()[0] >= '10.16' else '' - app.setWindowIcon(QIcon(f":/mne-{kind}icon.png")) + _init_mne_qtapp(pg_app=True) browser = PyQtGraphBrowser(**kwargs) return browser