From be0899b0ed156419c79461e103fd7e089633ec00 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Fri, 4 Mar 2022 13:56:59 -0500 Subject: [PATCH] MAINT: Refactor for mne-qt-browser --- mne/viz/backends/_qt.py | 32 ++++---------------------------- mne/viz/backends/_utils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/mne/viz/backends/_qt.py b/mne/viz/backends/_qt.py index 1301a3c4c21..3ecf25c4d23 100644 --- a/mne/viz/backends/_qt.py +++ b/mne/viz/backends/_qt.py @@ -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): @@ -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) @@ -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 diff --git a/mne/viz/backends/_utils.py b/mne/viz/backends/_utils.py index 54e8c01224b..cc4a3a5c4d9 100644 --- a/mne/viz/backends/_utils.py +++ b/mne/viz/backends/_utils.py @@ -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: