From 8308115f3646245d825fc47ab57297d3460bbcf5 Mon Sep 17 00:00:00 2001 From: Mathias Guijarro Date: Wed, 10 Jul 2024 13:28:58 +0200 Subject: [PATCH] fix: replace pyqtdarktheme by qdarkstyle, add 'apply_theme' function (in utils/colors.py) --- bec_widgets/utils/colors.py | 27 +++++++++++++++++++++++++++ bec_widgets/widgets/figure/figure.py | 5 ++--- pyproject.toml | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/bec_widgets/utils/colors.py b/bec_widgets/utils/colors.py index cd888b61..7642a82a 100644 --- a/bec_widgets/utils/colors.py +++ b/bec_widgets/utils/colors.py @@ -1,10 +1,37 @@ +import itertools import re from typing import Literal import numpy as np import pyqtgraph as pg +import qdarkstyle from pydantic_core import PydanticCustomError +from qdarkstyle import DarkPalette, LightPalette from qtpy.QtGui import QColor +from qtpy.QtWidgets import QApplication + +CURRENT_THEME = "dark" + + +def get_theme_palette(): + return DarkPalette if CURRENT_THEME == "dark" else LightPalette + + +def apply_theme(theme: Literal["dark", "light"]): + global CURRENT_THEME + CURRENT_THEME = theme + + app = QApplication.instance() + # go through all pyqtgraph widgets and set background + children = itertools.chain.from_iterable( + top.findChildren(pg.GraphicsLayoutWidget) for top in app.topLevelWidgets() + ) + for pg_widget in children: + pg_widget.setBackground("k" if theme == "dark" else "w") + + # now define stylesheet according to theme and apply it + style = qdarkstyle.load_stylesheet(palette=get_theme_palette()) + app.setStyleSheet(style) class Colors: diff --git a/bec_widgets/widgets/figure/figure.py b/bec_widgets/widgets/figure/figure.py index 3954fcb7..9280ed32 100644 --- a/bec_widgets/widgets/figure/figure.py +++ b/bec_widgets/widgets/figure/figure.py @@ -7,13 +7,13 @@ from typing import Literal, Optional import numpy as np import pyqtgraph as pg -import qdarktheme from pydantic import Field, ValidationError, field_validator from qtpy.QtCore import Signal as pyqtSignal from qtpy.QtWidgets import QWidget from typeguard import typechecked from bec_widgets.utils import BECConnector, ConnectionConfig, WidgetContainerUtils +from bec_widgets.utils.colors import apply_theme from bec_widgets.widgets.figure.plots.image.image import BECImageShow, ImageConfig from bec_widgets.widgets.figure.plots.motor_map.motor_map import BECMotorMap, MotorMapConfig from bec_widgets.widgets.figure.plots.plot_base import BECPlotBase, SubplotConfig @@ -670,9 +670,8 @@ class BECFigure(BECConnector, pg.GraphicsLayoutWidget): Args: theme(Literal["dark","light"]): The theme to set for the figure widget. """ - qdarktheme.setup_theme(theme) - self.setBackground("k" if theme == "dark" else "w") self.config.theme = theme + apply_theme(theme) for plot in self.widget_list: plot.set_x_label(plot.plot_item.getAxis("bottom").label.toPlainText()) plot.set_y_label(plot.plot_item.getAxis("left").label.toPlainText()) diff --git a/pyproject.toml b/pyproject.toml index d4b188a0..00e6fd8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ dependencies = [ "isort~=5.13, >=5.13.2", # needed for bw-generate-cli "pydantic~=2.0", "pyqtgraph~=0.13", - "pyqtdarktheme~=2.1", + "qdarkstyle>=3.2.2", "qtconsole~=5.5, >=5.5.1", # needed for jupyter console "qtpy~=2.4", "pyte", # needed for vt100 console