mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-06-19 03:21:04 +02:00
feat(widget_io): register handler
This commit is contained in:
@@ -208,6 +208,14 @@ class WidgetIO:
|
||||
QSlider: SlideHandler,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def register_handler(cls, widget_class: type[QWidget], handler: type[WidgetHandler]) -> None:
|
||||
"""
|
||||
Register a handler for a widget class. Widget modules that cannot be imported here
|
||||
(e.g. to avoid circular imports) register their handlers on import.
|
||||
"""
|
||||
cls._handlers[widget_class] = handler
|
||||
|
||||
@staticmethod
|
||||
def get_value(widget, ignore_errors=False, **kwargs):
|
||||
"""
|
||||
|
||||
@@ -16,6 +16,7 @@ from bec_widgets.utils.bec_connector import ConnectionConfig
|
||||
from bec_widgets.utils.bec_widget import BECWidget
|
||||
from bec_widgets.utils.error_popups import SafeProperty, SafeSlot
|
||||
from bec_widgets.utils.filter_io import get_bec_signals_for_classes, replace_combobox_items
|
||||
from bec_widgets.utils.widget_io import DeviceComboBoxHandler, WidgetIO
|
||||
|
||||
logger = bec_logger.logger
|
||||
|
||||
@@ -632,6 +633,9 @@ class DeviceComboBox(BECWidget, QComboBox):
|
||||
return text
|
||||
|
||||
|
||||
WidgetIO.register_handler(DeviceComboBox, DeviceComboBoxHandler)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
from qtpy.QtWidgets import (
|
||||
QApplication,
|
||||
|
||||
@@ -18,6 +18,7 @@ from bec_widgets.utils.filter_io import (
|
||||
signal_items_for_kind,
|
||||
)
|
||||
from bec_widgets.utils.ophyd_kind_util import Kind
|
||||
from bec_widgets.utils.widget_io import SignalComboBoxHandler, WidgetIO
|
||||
|
||||
logger = bec_logger.logger
|
||||
|
||||
@@ -468,7 +469,8 @@ class SignalComboBox(BECWidget, QComboBox):
|
||||
True if an enabled item was found and selected.
|
||||
"""
|
||||
for index in range(self.count()):
|
||||
if self._item_is_enabled(index):
|
||||
item = self.model().item(index)
|
||||
if item is not None and item.isEnabled():
|
||||
self.setCurrentIndex(index)
|
||||
return True
|
||||
return False
|
||||
@@ -649,37 +651,15 @@ class SignalComboBox(BECWidget, QComboBox):
|
||||
if self._config_signals:
|
||||
index = offset + len(self._hinted_signals) + len(self._normal_signals)
|
||||
self.insertItem(index, "Config Signals")
|
||||
self._set_item_enabled(index, False)
|
||||
self.model().item(index).setEnabled(False)
|
||||
if self._normal_signals:
|
||||
index = offset + len(self._hinted_signals)
|
||||
self.insertItem(index, "Normal Signals")
|
||||
self._set_item_enabled(index, False)
|
||||
self.model().item(index).setEnabled(False)
|
||||
if self._hinted_signals:
|
||||
index = offset
|
||||
self.insertItem(index, "Hinted Signals")
|
||||
self._set_item_enabled(index, False)
|
||||
|
||||
def _standard_item(self, index: int):
|
||||
model = self.model()
|
||||
item_getter = getattr(model, "item", None)
|
||||
if callable(item_getter):
|
||||
return item_getter(index)
|
||||
return None
|
||||
|
||||
def _item_is_enabled(self, index: int) -> bool:
|
||||
item = self._standard_item(index)
|
||||
if item is not None:
|
||||
return item.isEnabled()
|
||||
|
||||
model_index = self.model().index(index, self.modelColumn())
|
||||
if not model_index.isValid():
|
||||
return True
|
||||
return bool(self.model().flags(model_index) & Qt.ItemFlag.ItemIsEnabled)
|
||||
|
||||
def _set_item_enabled(self, index: int, enabled: bool) -> None:
|
||||
item = self._standard_item(index)
|
||||
if item is not None:
|
||||
item.setEnabled(enabled)
|
||||
self.model().item(index).setEnabled(False)
|
||||
|
||||
def _display_text_for_signal(self, signal: str) -> str | None:
|
||||
for entry in self._signals:
|
||||
@@ -711,6 +691,9 @@ class SignalComboBox(BECWidget, QComboBox):
|
||||
return -1
|
||||
|
||||
|
||||
WidgetIO.register_handler(SignalComboBox, SignalComboBoxHandler)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
from qtpy.QtWidgets import QApplication, QVBoxLayout, QWidget
|
||||
|
||||
|
||||
Reference in New Issue
Block a user