mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-05-12 09:35:43 +02:00
refactor: make sorting more generic
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
from typing import Any, Callable, Generator, Iterable, TypeVar
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_RT = TypeVar("_RT")
|
||||
|
||||
|
||||
def yield_only_passing(fn: Callable[[_T], _RT], vals: Iterable[_T]) -> Generator[_RT, Any, None]:
|
||||
for v in vals:
|
||||
try:
|
||||
yield fn(v)
|
||||
except BaseException:
|
||||
pass
|
||||
+5
-7
@@ -6,14 +6,12 @@ from qtpy.QtCore import QSize
|
||||
from qtpy.QtWidgets import QFrame, QHBoxLayout, QLabel, QListWidgetItem, QVBoxLayout, QWidget
|
||||
|
||||
from bec_widgets.utils.expandable_frame import ExpandableGroupFrame
|
||||
from bec_widgets.widgets.control.device_manager.components.available_device_resources.available_device_group_ui import (
|
||||
Ui_AvailableDeviceGroup,
|
||||
)
|
||||
from bec_widgets.widgets.control.device_manager.components.available_device_resources.device_resource_backend import (
|
||||
HashableDevice,
|
||||
)
|
||||
from bec_widgets.widgets.control.device_manager.components.available_device_resources.device_tag_group_ui import (
|
||||
Ui_DeviceTagGroup,
|
||||
)
|
||||
|
||||
DEVICE_HASH_ROLE = 101
|
||||
|
||||
|
||||
def _warning_string(spec: HashableDevice):
|
||||
@@ -107,7 +105,7 @@ class _DeviceEntry(NamedTuple):
|
||||
widget: _DeviceEntryWidget
|
||||
|
||||
|
||||
class DeviceTagGroup(ExpandableGroupFrame, Ui_DeviceTagGroup):
|
||||
class AvailableDeviceGroup(ExpandableGroupFrame, Ui_AvailableDeviceGroup):
|
||||
def __init__(
|
||||
self, parent=None, name: str = "TagGroupTitle", data: set[HashableDevice] = set(), **kwargs
|
||||
):
|
||||
@@ -184,7 +182,7 @@ if __name__ == "__main__":
|
||||
from qtpy.QtWidgets import QApplication
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
widget = DeviceTagGroup(name="Tag group 1")
|
||||
widget = AvailableDeviceGroup(name="Tag group 1")
|
||||
for item in [
|
||||
HashableDevice(
|
||||
**{
|
||||
+15
-15
@@ -6,42 +6,42 @@ from qtpy.QtCore import QMetaObject
|
||||
from qtpy.QtWidgets import QLabel, QListWidget, QToolButton, QVBoxLayout
|
||||
|
||||
|
||||
class Ui_DeviceTagGroup(object):
|
||||
def setupUi(self, DeviceTagGroup):
|
||||
if not DeviceTagGroup.objectName():
|
||||
DeviceTagGroup.setObjectName("DeviceTagGroup")
|
||||
DeviceTagGroup.setMinimumWidth(150)
|
||||
class Ui_AvailableDeviceGroup(object):
|
||||
def setupUi(self, AvailableDeviceGroup):
|
||||
if not AvailableDeviceGroup.objectName():
|
||||
AvailableDeviceGroup.setObjectName("AvailableDeviceGroup")
|
||||
AvailableDeviceGroup.setMinimumWidth(150)
|
||||
self.verticalLayout = QVBoxLayout()
|
||||
self.verticalLayout.setObjectName("verticalLayout")
|
||||
DeviceTagGroup.set_layout(self.verticalLayout)
|
||||
AvailableDeviceGroup.set_layout(self.verticalLayout)
|
||||
|
||||
title_layout = DeviceTagGroup.get_title_layout()
|
||||
title_layout = AvailableDeviceGroup.get_title_layout()
|
||||
|
||||
self.n_included = QLabel(DeviceTagGroup, text="...")
|
||||
self.n_included = QLabel(AvailableDeviceGroup, text="...")
|
||||
self.n_included.setObjectName("n_included")
|
||||
title_layout.addWidget(self.n_included)
|
||||
|
||||
self.delete_tag_button = QToolButton(DeviceTagGroup)
|
||||
self.delete_tag_button = QToolButton(AvailableDeviceGroup)
|
||||
self.delete_tag_button.setObjectName("delete_tag_button")
|
||||
title_layout.addWidget(self.delete_tag_button)
|
||||
|
||||
self.remove_from_composition_button = QToolButton(DeviceTagGroup)
|
||||
self.remove_from_composition_button = QToolButton(AvailableDeviceGroup)
|
||||
self.remove_from_composition_button.setObjectName("remove_from_composition_button")
|
||||
title_layout.addWidget(self.remove_from_composition_button)
|
||||
|
||||
self.add_to_composition_button = QToolButton(DeviceTagGroup)
|
||||
self.add_to_composition_button = QToolButton(AvailableDeviceGroup)
|
||||
self.add_to_composition_button.setObjectName("add_to_composition_button")
|
||||
title_layout.addWidget(self.add_to_composition_button)
|
||||
|
||||
self.remove_all_button = QToolButton(DeviceTagGroup)
|
||||
self.remove_all_button = QToolButton(AvailableDeviceGroup)
|
||||
self.remove_all_button.setObjectName("remove_all_from_composition_button")
|
||||
title_layout.addWidget(self.remove_all_button)
|
||||
|
||||
self.add_all_button = QToolButton(DeviceTagGroup)
|
||||
self.add_all_button = QToolButton(AvailableDeviceGroup)
|
||||
self.add_all_button.setObjectName("add_all_to_composition_button")
|
||||
title_layout.addWidget(self.add_all_button)
|
||||
|
||||
self.device_list = QListWidget(DeviceTagGroup)
|
||||
self.device_list = QListWidget(AvailableDeviceGroup)
|
||||
self.device_list.setObjectName("device_list")
|
||||
self.device_list.setFrameStyle(0)
|
||||
|
||||
@@ -49,7 +49,7 @@ class Ui_DeviceTagGroup(object):
|
||||
|
||||
self.set_icons()
|
||||
|
||||
QMetaObject.connectSlotsByName(DeviceTagGroup)
|
||||
QMetaObject.connectSlotsByName(AvailableDeviceGroup)
|
||||
|
||||
def set_icons(self):
|
||||
icon = partial(material_icon, size=(15, 15), convert_to_pixmap=False)
|
||||
+5
-15
@@ -1,10 +1,13 @@
|
||||
from random import randint
|
||||
from typing import Any, Callable, Generator, Iterable, TypeVar
|
||||
from typing import Any, Iterable
|
||||
|
||||
from qtpy.QtWidgets import QWidget
|
||||
|
||||
from bec_widgets.utils.bec_widget import BECWidget
|
||||
from bec_widgets.utils.error_popups import SafeSlot
|
||||
from bec_widgets.widgets.control.device_manager.components.available_device_resources._util import (
|
||||
yield_only_passing,
|
||||
)
|
||||
from bec_widgets.widgets.control.device_manager.components.available_device_resources.available_device_resources_ui import (
|
||||
Ui_availableDeviceResources,
|
||||
)
|
||||
@@ -13,17 +16,6 @@ from bec_widgets.widgets.control.device_manager.components.available_device_reso
|
||||
get_backend,
|
||||
)
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_RT = TypeVar("_RT")
|
||||
|
||||
|
||||
def _yield_only_passing(fn: Callable[[_T], _RT], vals: Iterable[_T]) -> Generator[_RT, Any, None]:
|
||||
for v in vals:
|
||||
try:
|
||||
yield fn(v)
|
||||
except BaseException:
|
||||
pass
|
||||
|
||||
|
||||
class AvailableDeviceResources(BECWidget, QWidget, Ui_availableDeviceResources):
|
||||
def __init__(self, parent=None, **kwargs):
|
||||
@@ -60,9 +52,7 @@ class AvailableDeviceResources(BECWidget, QWidget, Ui_availableDeviceResources):
|
||||
|
||||
@SafeSlot(list)
|
||||
def update_devices_state(self, config_list: list[dict[str, Any]]):
|
||||
self.set_devices_state(
|
||||
_yield_only_passing(HashableDevice.model_validate, config_list), True
|
||||
)
|
||||
self.set_devices_state(yield_only_passing(HashableDevice.model_validate, config_list), True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+5
-3
@@ -3,8 +3,8 @@ from qtpy.QtCore import QMetaObject, Qt
|
||||
from qtpy.QtWidgets import QAbstractItemView, QListView, QListWidget, QVBoxLayout
|
||||
|
||||
from bec_widgets.utils.list_of_expandable_frames import ListOfExpandableFrames
|
||||
from bec_widgets.widgets.control.device_manager.components.available_device_resources.device_tag_group import (
|
||||
DeviceTagGroup,
|
||||
from bec_widgets.widgets.control.device_manager.components.available_device_resources.available_device_group import (
|
||||
AvailableDeviceGroup,
|
||||
)
|
||||
|
||||
|
||||
@@ -21,7 +21,9 @@ class Ui_availableDeviceResources(object):
|
||||
self.search_box = QLineEdit()
|
||||
self.search_layout.addWidget(self.search_box)
|
||||
|
||||
self.tag_groups_list = ListOfExpandableFrames(availableDeviceResources, DeviceTagGroup)
|
||||
self.tag_groups_list = ListOfExpandableFrames(
|
||||
availableDeviceResources, AvailableDeviceGroup
|
||||
)
|
||||
self.tag_groups_list.setObjectName("tag_groups_list")
|
||||
self.tag_groups_list.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection)
|
||||
self.tag_groups_list.setVerticalScrollMode(QAbstractItemView.ScrollMode.ScrollPerPixel)
|
||||
|
||||
Reference in New Issue
Block a user