mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 19:21:50 +02:00
wip
This commit is contained in:
@ -1,6 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from decimal import Decimal
|
|
||||||
from types import NoneType
|
from types import NoneType
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
|
|
||||||
@ -171,14 +170,13 @@ class PydanticModelForm(TypedForm):
|
|||||||
enabled: bool = True,
|
enabled: bool = True,
|
||||||
pretty_display: bool = False,
|
pretty_display: bool = False,
|
||||||
client=None,
|
client=None,
|
||||||
**kwargs,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
A form generated from a pydantic model.
|
A form generated from a pydantic model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data_model (type[BaseModel]): the model class for which to generate a form.
|
data_model (type[BaseModel]): the model class for which to generate a form.
|
||||||
enabled (bool): whether fields are enabled for editing.
|
enabled (bool, optional): whether fields are enabled for editing.
|
||||||
pretty_display (bool, optional): Whether to use a pretty display for the widget. Defaults to False. If True, disables the widget, doesn't add a clear button, and adapts the stylesheet for non-editable display.
|
pretty_display (bool, optional): Whether to use a pretty display for the widget. Defaults to False. If True, disables the widget, doesn't add a clear button, and adapts the stylesheet for non-editable display.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
from bec_lib.atlas_models import Device as DeviceConfigModel
|
||||||
|
from bec_lib.logger import bec_logger
|
||||||
|
from qtpy.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout
|
||||||
|
|
||||||
|
from bec_widgets.utils.bec_widget import BECWidget
|
||||||
|
from bec_widgets.widgets.services.device_browser.device_item.device_config_form import (
|
||||||
|
DeviceConfigForm,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = bec_logger.logger
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceConfigDialog(BECWidget, QDialog):
|
||||||
|
def __init__(self, parent=None, device: str | None = None):
|
||||||
|
super().__init__(parent=parent)
|
||||||
|
self._layout = QVBoxLayout()
|
||||||
|
self.setLayout(self._layout)
|
||||||
|
self._form = DeviceConfigForm()
|
||||||
|
self._layout.addWidget(self._form)
|
||||||
|
if device:
|
||||||
|
self._device = device
|
||||||
|
self._fill_form()
|
||||||
|
|
||||||
|
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
|
||||||
|
button_box.accepted.connect(self.accept)
|
||||||
|
button_box.rejected.connect(self.reject)
|
||||||
|
self._layout.addWidget(button_box)
|
||||||
|
|
||||||
|
def _fill_form(self):
|
||||||
|
self._form.set_data(self._pull_config())
|
||||||
|
|
||||||
|
def _pull_config(self) -> DeviceConfigModel:
|
||||||
|
return DeviceConfigModel.model_validate(
|
||||||
|
{
|
||||||
|
"name": "test_device",
|
||||||
|
"enabled": False,
|
||||||
|
"deviceClass": "TestDevice",
|
||||||
|
"deviceConfig": {},
|
||||||
|
"readoutPriority": "baseline",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main(): # pragma: no cover
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from qtpy.QtWidgets import QApplication, QLineEdit, QPushButton, QWidget
|
||||||
|
|
||||||
|
from bec_widgets.utils.colors import set_theme
|
||||||
|
|
||||||
|
dialog = None
|
||||||
|
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
set_theme("light")
|
||||||
|
widget = QWidget()
|
||||||
|
widget.setLayout(QVBoxLayout())
|
||||||
|
|
||||||
|
device = QLineEdit()
|
||||||
|
widget.layout().addWidget(device)
|
||||||
|
|
||||||
|
def _destroy_dialog(*_):
|
||||||
|
nonlocal dialog
|
||||||
|
dialog = None
|
||||||
|
|
||||||
|
def accept(*args):
|
||||||
|
logger.success(f"submitted device config form {dialog} {args}")
|
||||||
|
_destroy_dialog()
|
||||||
|
|
||||||
|
def _show_dialog(*_):
|
||||||
|
nonlocal dialog
|
||||||
|
if dialog is None:
|
||||||
|
dialog = DeviceConfigDialog(device=device.text())
|
||||||
|
dialog.accepted.connect(accept)
|
||||||
|
dialog.rejected.connect(_destroy_dialog)
|
||||||
|
dialog.open()
|
||||||
|
|
||||||
|
button = QPushButton("Show device dialog")
|
||||||
|
widget.layout().addWidget(button)
|
||||||
|
button.clicked.connect(_show_dialog)
|
||||||
|
widget.show()
|
||||||
|
sys.exit(app.exec_())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from bec_lib.atlas_models import Device as DeviceConfigModel
|
from bec_lib.atlas_models import Device as DeviceConfigModel
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from bec_widgets.utils.forms_from_types.forms import PydanticModelForm
|
from bec_widgets.utils.forms_from_types.forms import PydanticModelForm
|
||||||
|
|
||||||
@ -18,3 +19,9 @@ class DeviceConfigForm(PydanticModelForm):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
self._validity.setVisible(False)
|
self._validity.setVisible(False)
|
||||||
|
|
||||||
|
def set_schema(self, schema: type[BaseModel]):
|
||||||
|
raise TypeError("This class doesn't support changing the schema")
|
||||||
|
|
||||||
|
def set_data(self, data: DeviceConfigModel): # type: ignore # This class locks the type
|
||||||
|
super().set_data(data)
|
||||||
|
@ -18,6 +18,7 @@ from bec_widgets.widgets.utility.visual.dark_mode_button.dark_mode_button import
|
|||||||
if TYPE_CHECKING: # pragma: no cover
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
from qtpy.QtGui import QMouseEvent
|
from qtpy.QtGui import QMouseEvent
|
||||||
|
|
||||||
|
|
||||||
logger = bec_logger.logger
|
logger = bec_logger.logger
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user