Skip to content

Commit deb0a67

Browse files
authored
ENH: Respect activation (#116)
* ENH: Respect activation * FIX: Raise too * FIX: Doc * FIX: Always show * FIX: Correct spot * FIX: Revert
1 parent 40a3e8e commit deb0a67

File tree

3 files changed

+123
-82
lines changed

3 files changed

+123
-82
lines changed

.coveragerc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[run]
2+
branch = True
3+
source = mne_qt_browser
4+
omit =
5+
*/setup.py
6+
*/mne_qt_browser/_fixes.py
7+
*/mne_qt_browser/conftest.py

mne_qt_browser/_fixes.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -*- coding: utf-8 -*-
2+
"""Backports for MNE versions (mostly)."""
3+
4+
# Author: Martin Schulz <[email protected]>
5+
#
6+
# License: BSD-3-Clause
7+
8+
from contextlib import contextmanager
9+
import platform
10+
import sys
11+
12+
from mne.utils import logger
13+
14+
###############################################################################
15+
# MNE 1.0+
16+
try:
17+
from mne.viz.backends._utils import _qt_raise_window
18+
except ImportError:
19+
def _qt_raise_window(win):
20+
try:
21+
from matplotlib import rcParams
22+
raise_window = rcParams['figure.raise_window']
23+
except ImportError:
24+
raise_window = True
25+
if raise_window:
26+
win.activateWindow()
27+
win.raise_()
28+
29+
try:
30+
from mne.viz.backends._utils import _init_mne_qtapp
31+
except ImportError:
32+
from mne.viz.backends._utils import _init_qt_resources
33+
34+
def _init_mne_qtapp(enable_icon=True, pg_app=False):
35+
"""Get QApplication-instance for MNE-Python.
36+
37+
Parameter
38+
---------
39+
enable_icon: bool
40+
If to set an MNE-icon for the app.
41+
pg_app: bool
42+
If to create the QApplication with pyqtgraph. For an until know
43+
undiscovered reason the pyqtgraph-browser won't show without
44+
mkQApp from pyqtgraph.
45+
46+
Returns
47+
-------
48+
app: ``qtpy.QtWidgets.QApplication``
49+
Instance of QApplication.
50+
"""
51+
from qtpy.QtWidgets import QApplication
52+
from qtpy.QtGui import QIcon
53+
54+
app_name = 'MNE-Python'
55+
organization_name = 'MNE'
56+
57+
# Fix from cbrnr/mnelab for app name in menu bar
58+
if sys.platform.startswith("darwin"):
59+
try:
60+
# set bundle name on macOS (app name shown in the menu bar)
61+
from Foundation import NSBundle
62+
bundle = NSBundle.mainBundle()
63+
info = (bundle.localizedInfoDictionary()
64+
or bundle.infoDictionary())
65+
info["CFBundleName"] = app_name
66+
except ModuleNotFoundError:
67+
pass
68+
69+
if pg_app:
70+
from pyqtgraph import mkQApp
71+
app = mkQApp(app_name)
72+
else:
73+
app = (QApplication.instance()
74+
or QApplication(sys.argv or [app_name]))
75+
app.setApplicationName(app_name)
76+
app.setOrganizationName(organization_name)
77+
78+
if enable_icon:
79+
# Set icon
80+
_init_qt_resources()
81+
kind = 'bigsur-' if platform.mac_ver()[0] >= '10.16' else ''
82+
app.setWindowIcon(QIcon(f":/mne-{kind}icon.png"))
83+
84+
return app
85+
86+
###############################################################################
87+
# pytestqt
88+
try:
89+
from pytestqt.exceptions import capture_exceptions
90+
except ImportError:
91+
logger.debug('If pytest-qt is not installed, the errors from inside '
92+
'the Qt-loop will be occluded and it will be harder '
93+
'to trace back the cause.')
94+
95+
@contextmanager
96+
def capture_exceptions():
97+
yield []

mne_qt_browser/_pg_figure.py

+19-82
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pathlib import Path
1515
from ast import literal_eval
1616
from collections import OrderedDict
17-
from contextlib import contextmanager
1817
from copy import copy
1918
from functools import partial
2019
from os.path import getsize
@@ -56,17 +55,7 @@
5655
_check_option)
5756

5857
from . import _browser_instances
59-
60-
try:
61-
from pytestqt.exceptions import capture_exceptions
62-
except ImportError:
63-
logger.debug('If pytest-qt is not installed, the errors from inside '
64-
'the Qt-loop will be occluded and it will be harder '
65-
'to trace back the cause.')
66-
67-
@contextmanager
68-
def capture_exceptions():
69-
yield []
58+
from ._fixes import capture_exceptions, _qt_raise_window, _init_mne_qtapp
7059

7160
name = 'pyqtgraph'
7261

@@ -76,64 +65,6 @@ def capture_exceptions():
7665
# grads and mags here.
7766
DATA_CH_TYPES_ORDER = ('grad', 'mag', *_DATA_CH_TYPES_ORDER_DEFAULT[2:])
7867

79-
# This can be removed when mne==1.0 is released.
80-
try:
81-
from mne.viz.backends._utils import _init_mne_qtapp
82-
except ImportError:
83-
from mne.viz.backends._utils import _init_qt_resources
84-
85-
def _init_mne_qtapp(enable_icon=True, pg_app=False):
86-
"""Get QApplication-instance for MNE-Python.
87-
88-
Parameter
89-
---------
90-
enable_icon: bool
91-
If to set an MNE-icon for the app.
92-
pg_app: bool
93-
If to create the QApplication with pyqtgraph. For an until know
94-
undiscovered reason the pyqtgraph-browser won't show without
95-
mkQApp from pyqtgraph.
96-
97-
Returns
98-
-------
99-
app: ``qtpy.QtWidgets.QApplication``
100-
Instance of QApplication.
101-
"""
102-
from qtpy.QtWidgets import QApplication
103-
from qtpy.QtGui import QIcon
104-
105-
app_name = 'MNE-Python'
106-
organization_name = 'MNE'
107-
108-
# Fix from cbrnr/mnelab for app name in menu bar
109-
if sys.platform.startswith("darwin"):
110-
try:
111-
# set bundle name on macOS (app name shown in the menu bar)
112-
from Foundation import NSBundle
113-
bundle = NSBundle.mainBundle()
114-
info = (bundle.localizedInfoDictionary()
115-
or bundle.infoDictionary())
116-
info["CFBundleName"] = app_name
117-
except ModuleNotFoundError:
118-
pass
119-
120-
if pg_app:
121-
from pyqtgraph import mkQApp
122-
app = mkQApp(app_name)
123-
else:
124-
app = (QApplication.instance()
125-
or QApplication(sys.argv or [app_name]))
126-
app.setApplicationName(app_name)
127-
app.setOrganizationName(organization_name)
128-
129-
if enable_icon:
130-
# Set icon
131-
_init_qt_resources()
132-
kind = 'bigsur-' if platform.mac_ver()[0] >= '10.16' else ''
133-
app.setWindowIcon(QIcon(f":/mne-{kind}icon.png"))
134-
135-
return app
136-
13768

13869
# Mostly chosen manually from
13970
# https://matplotlib.org/3.1.0/gallery/color/named_colors.html
@@ -216,6 +147,7 @@ def wrapper(*args, **kwargs):
216147

217148

218149
def _safe_splash(meth):
150+
@functools.wraps(meth)
219151
def func(self, *args, **kwargs):
220152
try:
221153
meth(self, *args, **kwargs)
@@ -224,6 +156,11 @@ def func(self, *args, **kwargs):
224156
self.mne.splash.close()
225157
except Exception:
226158
pass
159+
finally:
160+
try:
161+
del self.mne.splash
162+
except Exception:
163+
pass
227164
return func
228165

229166

@@ -1586,7 +1523,6 @@ def show(self, center=True):
15861523
cp = _screen(self).availableGeometry().center()
15871524
qr.moveCenter(cp)
15881525
self.move(qr.topLeft())
1589-
self.activateWindow()
15901526

15911527
def keyPressEvent(self, event):
15921528
if event.key() == Qt.Key_Escape:
@@ -1602,6 +1538,14 @@ def closeEvent(self, event):
16021538
self.mne.child_figs.remove(self)
16031539
event.accept()
16041540

1541+
# If this widget gets activated (e.g., the user clicks away from the
1542+
# browser but then returns to it by clicking in a selection window),
1543+
# the main window should be raised as well
1544+
def event(self, event):
1545+
if event.type() == QEvent.WindowActivate:
1546+
self.main.raise_()
1547+
return super().event(event)
1548+
16051549

16061550
class SettingsDialog(_BaseDialog):
16071551
"""Shows additional settings."""
@@ -2738,6 +2682,9 @@ def _hidpi_mkPen(*args, **kwargs):
27382682
else:
27392683
QIcon.setThemeName('light')
27402684

2685+
# control raising with _qt_raise_window
2686+
self.setAttribute(Qt.WA_ShowWithoutActivating, True)
2687+
27412688
# Initialize attributes which are only used by pyqtgraph, not by
27422689
# matplotlib and add them to MNEBrowseParams.
27432690

@@ -4584,17 +4531,7 @@ def _get_scale_bar_texts(self):
45844531
def show(self):
45854532
# Set raise_window like matplotlib if possible
45864533
super().show()
4587-
try:
4588-
from matplotlib import rcParams
4589-
raise_window = rcParams['figure.raise_window']
4590-
except ImportError:
4591-
raise_window = True
4592-
if raise_window:
4593-
self.activateWindow()
4594-
self.raise_()
4595-
if getattr(self.mne, 'splash', None):
4596-
self.mne.splash.close()
4597-
del self.mne.splash
4534+
_qt_raise_window(self)
45984535

45994536
def _close_event(self, fig=None):
46004537
"""Force calling of the MPL figure close event."""

0 commit comments

Comments
 (0)