14
14
from pathlib import Path
15
15
from ast import literal_eval
16
16
from collections import OrderedDict
17
- from contextlib import contextmanager
18
17
from copy import copy
19
18
from functools import partial
20
19
from os .path import getsize
56
55
_check_option )
57
56
58
57
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
70
59
71
60
name = 'pyqtgraph'
72
61
@@ -76,64 +65,6 @@ def capture_exceptions():
76
65
# grads and mags here.
77
66
DATA_CH_TYPES_ORDER = ('grad' , 'mag' , * _DATA_CH_TYPES_ORDER_DEFAULT [2 :])
78
67
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
-
137
68
138
69
# Mostly chosen manually from
139
70
# https://matplotlib.org/3.1.0/gallery/color/named_colors.html
@@ -216,6 +147,7 @@ def wrapper(*args, **kwargs):
216
147
217
148
218
149
def _safe_splash (meth ):
150
+ @functools .wraps (meth )
219
151
def func (self , * args , ** kwargs ):
220
152
try :
221
153
meth (self , * args , ** kwargs )
@@ -224,6 +156,11 @@ def func(self, *args, **kwargs):
224
156
self .mne .splash .close ()
225
157
except Exception :
226
158
pass
159
+ finally :
160
+ try :
161
+ del self .mne .splash
162
+ except Exception :
163
+ pass
227
164
return func
228
165
229
166
@@ -1586,7 +1523,6 @@ def show(self, center=True):
1586
1523
cp = _screen (self ).availableGeometry ().center ()
1587
1524
qr .moveCenter (cp )
1588
1525
self .move (qr .topLeft ())
1589
- self .activateWindow ()
1590
1526
1591
1527
def keyPressEvent (self , event ):
1592
1528
if event .key () == Qt .Key_Escape :
@@ -1602,6 +1538,14 @@ def closeEvent(self, event):
1602
1538
self .mne .child_figs .remove (self )
1603
1539
event .accept ()
1604
1540
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
+
1605
1549
1606
1550
class SettingsDialog (_BaseDialog ):
1607
1551
"""Shows additional settings."""
@@ -2738,6 +2682,9 @@ def _hidpi_mkPen(*args, **kwargs):
2738
2682
else :
2739
2683
QIcon .setThemeName ('light' )
2740
2684
2685
+ # control raising with _qt_raise_window
2686
+ self .setAttribute (Qt .WA_ShowWithoutActivating , True )
2687
+
2741
2688
# Initialize attributes which are only used by pyqtgraph, not by
2742
2689
# matplotlib and add them to MNEBrowseParams.
2743
2690
@@ -4584,17 +4531,7 @@ def _get_scale_bar_texts(self):
4584
4531
def show (self ):
4585
4532
# Set raise_window like matplotlib if possible
4586
4533
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 )
4598
4535
4599
4536
def _close_event (self , fig = None ):
4600
4537
"""Force calling of the MPL figure close event."""
0 commit comments