0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

fix(theme): update pg axes on theme update

This commit is contained in:
2024-08-31 09:11:43 +02:00
committed by wyzula_j
parent 0bf1cf9b8a
commit af23e74f71
2 changed files with 35 additions and 1 deletions

View File

@ -29,6 +29,7 @@ def set_theme(theme: Literal["dark", "light", "auto"]):
app = QApplication.instance()
bec_qthemes.setup_theme(theme)
pg.setConfigOption("background", "w" if app.theme["theme"] == "light" else "k")
app.theme_signal.theme_updated.emit(theme)
apply_theme(theme)
# pylint: disable=protected-access
@ -37,6 +38,7 @@ def set_theme(theme: Literal["dark", "light", "auto"]):
def callback():
app.theme["theme"] = listener._theme.lower()
app.theme_signal.theme_updated.emit(app.theme["theme"])
apply_theme(listener._theme.lower())
listener = OSThemeSwitchListener(callback)
@ -53,6 +55,7 @@ def apply_theme(theme: Literal["dark", "light"]):
children = itertools.chain.from_iterable(
top.findChildren(pg.GraphicsLayoutWidget) for top in app.topLevelWidgets()
)
pg.setConfigOptions(foreground="d" if theme == "dark" else "k")
for pg_widget in children:
pg_widget.setBackground("k" if theme == "dark" else "w")

View File

@ -2,10 +2,11 @@ from __future__ import annotations
from typing import Literal, Optional
import bec_qthemes
import pyqtgraph as pg
from pydantic import BaseModel, Field
from qtpy.QtCore import Signal, Slot
from qtpy.QtWidgets import QWidget
from qtpy.QtWidgets import QApplication, QWidget
from bec_widgets.utils import BECConnector, ConnectionConfig
from bec_widgets.utils.crosshair import Crosshair
@ -98,6 +99,36 @@ class BECPlotBase(BECConnector, pg.GraphicsLayout):
self.add_legend()
self.crosshair = None
self.connect_to_theme_change()
self.apply_theme()
def connect_to_theme_change(self):
"""Connect to the theme change signal."""
qapp = QApplication.instance()
if hasattr(qapp, "theme_signal"):
qapp.theme_signal.theme_updated.connect(self.apply_theme)
@Slot(str)
@Slot()
def apply_theme(self, theme: str | None = None):
"""
Apply the theme to the plot widget.
Args:
theme(str, optional): The theme to be applied.
"""
if theme is None:
qapp = QApplication.instance()
if hasattr(qapp, "theme"):
theme = qapp.theme["theme"]
else:
theme = "dark"
palette = bec_qthemes.load_palette(theme)
text_pen = pg.mkPen(color=palette.text().color())
for axis in ["left", "bottom", "right", "top"]:
self.plot_item.getAxis(axis).setPen(text_pen)
self.plot_item.getAxis(axis).setTextPen(text_pen)
def set(self, **kwargs) -> None:
"""