feat: add actor config panel

This commit is contained in:
perl_d committed 2026-11-03 07:48:32 +01:00
1 parent ebb4e1f08a
commit 8e44786bb6
1 file changed
+65 -6
@@ -1,9 +1,17 @@
from __future__ import annotations
import functools
from typing import Callable
from bec_lib.config_values import RedisConfigValue
from bec_lib.logger import bec_logger
from bec_lib.messages import AvailableResourceMessage
from bec_widgets.utils.bec_connector import ConnectionConfig
from bec_widgets.utils.bec_widget import BECWidget
from bec_widgets.utils.error_popups import SafeSlot
from bec_widgets.widgets.utility.toggle.toggle import ToggleSwitch
from qtpy.QtWidgets import QLabel, QVBoxLayout, QWidget
from qtpy.QtCore import Signal
from qtpy.QtWidgets import QGridLayout, QLabel, QWidget
from mx_bec.endpoints import ActorEndpoints
@@ -13,12 +21,19 @@ logger = bec_logger.logger
class MxActorConfigWidget(BECWidget, QWidget):
"""
Widget displaying and managing MX actors - automation steps.
Each available actor gets a row with its name and a toggle bound to the actor's `enabled`
flag in Redis, so the toggle both writes the flag and follows changes made elsewhere.
"""
PLUGIN = True
ICON_NAME = "format_list_bulleted"
USER_ACCESS = ["refresh"]
# Redis callbacks arrive on the connector thread; re-emit so the toggle is updated on the GUI
# thread.
_config_value_changed = Signal(str, bool)
def __init__(
self,
parent: QWidget | None = None,
@@ -28,15 +43,59 @@ class MxActorConfigWidget(BECWidget, QWidget):
**kwargs,
) -> None:
super().__init__(parent=parent, client=client, config=config, gui_id=gui_id, **kwargs)
self._layout = QGridLayout(self)
self._toggles: dict[str, ToggleSwitch] = {}
self._config_values: dict[str, RedisConfigValue[bool]] = {}
# RedisConfigValue only keeps weak references to subscribers, so hold the partials here.
self._config_callbacks: dict[str, Callable[[bool], None]] = {}
self._config_value_changed.connect(self._on_config_value_changed)
self.refresh()
@SafeSlot()
def refresh(self):
"""Reset the layout for currently loaded actors"""
self._layout = QVBoxLayout(self)
self._clear()
available_actors = self.client.connector.get(ActorEndpoints.available_actor_config_eps())
if not isinstance(available_actors, AvailableResourceMessage):
logger.warning("No available MX actors registered")
self._layout.addWidget(QLabel("No available MX actors registered"))
for name, ep in available_actors.resource["endpoints"].items():
self._layout.addWidget(QLabel(name))
self._layout.addWidget(ToggleSwitch())
self._layout.addWidget(QLabel("No available MX actors registered"), 0, 0, 1, 2)
return
for row, (name, ep) in enumerate(available_actors.resource["endpoints"].items()):
config_value: RedisConfigValue[bool] = RedisConfigValue(
connector=self.client.connector, endpoint=ep, wait_for_writes=False
)
toggle = ToggleSwitch(parent=self, checked=config_value.value)
toggle.stateChanged.connect(functools.partial(self._set_config_value, name))
callback = functools.partial(self._config_value_changed.emit, name)
config_value.subscribe(callback)
self._toggles[name] = toggle
self._config_values[name] = config_value
self._config_callbacks[name] = callback
self._layout.addWidget(QLabel(name, parent=self), row, 0)
self._layout.addWidget(toggle, row, 1)
def _set_config_value(self, name: str, enabled: bool):
self._config_values[name].value = enabled
@SafeSlot(str, bool)
def _on_config_value_changed(self, name: str, enabled: bool):
if (toggle := self._toggles.get(name)) is None:
return
# Don't write the value we just received straight back to Redis.
toggle.blockSignals(True)
toggle.setChecked(enabled)
toggle.blockSignals(False)
def _clear(self):
for config_value in self._config_values.values():
config_value.unregister_all()
self._config_values.clear()
self._config_callbacks.clear()
self._toggles.clear()
while (item := self._layout.takeAt(0)) is not None:
if (widget := item.widget()) is not None:
widget.deleteLater()
def cleanup(self):
self._clear()
super().cleanup()