Skip to content

Commit

Permalink
MAINT: Refactor for mne-qt-browser
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Mar 5, 2022
1 parent a93c198 commit be0899b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
32 changes: 4 additions & 28 deletions mne/viz/backends/_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
_AbstractWindow, _AbstractMplCanvas, _AbstractPlayback,
_AbstractBrainMplCanvas, _AbstractMplInterface,
_AbstractWidgetList, _AbstractAction, _AbstractDialog)
from ._utils import _init_qt_resources, _qt_disable_paint, _qt_raise_window
from ..utils import logger, _check_option, safe_event
from ._utils import (_init_qt_resources, _qt_disable_paint,
_qt_get_stylesheet, _detect_theme, _qt_raise_window)
from ..utils import _check_option, safe_event


class _QtDialog(_AbstractDialog):
Expand Down Expand Up @@ -674,24 +675,7 @@ def _window_ensure_minimum_sizes(self):
self._process_events()

def _window_set_theme(self, theme):
if theme == 'auto':
theme = _detect_theme()

if theme == 'dark':
try:
import qdarkstyle
except ModuleNotFoundError:
logger.info('For Dark-Mode "qdarkstyle" has to be installed! '
'You can install it with `pip install qdarkstyle`')
stylesheet = None
else:
stylesheet = qdarkstyle.load_stylesheet()
elif theme != 'light':
with open(theme, 'r') as file:
stylesheet = file.read()
else:
stylesheet = None

stylesheet = _qt_get_stylesheet(theme)
self._window.setStyleSheet(stylesheet)


Expand Down Expand Up @@ -878,14 +862,6 @@ def _create_dock_widget(window, name, area, *, max_width=None):
return dock, dock_layout


def _detect_theme():
try:
import darkdetect
return darkdetect.theme().lower()
except Exception:
return 'light'


@contextmanager
def _testing_context(interactive):
from . import renderer
Expand Down
29 changes: 29 additions & 0 deletions mne/viz/backends/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,35 @@ def _qt_app_exec(app):
signal.signal(signal.SIGINT, old_signal)


def _qt_get_stylesheet(theme='auto'):
from ..utils import logger
if theme == 'auto':
theme = _detect_theme()
if theme == 'dark':
try:
import qdarkstyle
except ModuleNotFoundError:
logger.info('For Dark-Mode "qdarkstyle" has to be installed! '
'You can install it with `pip install qdarkstyle`')
stylesheet = None
else:
stylesheet = qdarkstyle.load_stylesheet()
elif theme != 'light':
with open(theme, 'r') as file:
stylesheet = file.read()
else:
stylesheet = None
return stylesheet


def _detect_theme():
try:
import darkdetect
return darkdetect.theme().lower()
except Exception:
return 'light'


def _qt_raise_window(widget):
# Set raise_window like matplotlib if possible
try:
Expand Down

0 comments on commit be0899b

Please sign in to comment.