diff --git a/debye_bec/bec_widgets/widgets/__init__.py b/debye_bec/bec_widgets/widgets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/debye_bec/bec_widgets/widgets/label_box/__init__.py b/debye_bec/bec_widgets/widgets/label_box/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/debye_bec/bec_widgets/widgets/label_box/label_box.py b/debye_bec/bec_widgets/widgets/label_box/label_box.py new file mode 100644 index 0000000..a027641 --- /dev/null +++ b/debye_bec/bec_widgets/widgets/label_box/label_box.py @@ -0,0 +1,150 @@ +from qtpy.QtCore import Signal +from qtpy.QtWidgets import QGridLayout, QSizePolicy, QVBoxLayout, QPushButton +from qtpy.QtWidgets import QWidget +from qtpy.QtWidgets import QGroupBox +from qtpy.QtWidgets import QLabel + +from bec_widgets import BECWidget, SafeProperty, SafeSlot +from bec_widgets.utils import ConnectionConfig + + +class BECLabelBox(BECWidget, QGroupBox): + PLUGIN = True + RPC = True + ICON_NAME = "inventory_2" + USER_ACCESS = ["qroup_box_title", "qroup_box_title.setter", "add_label", "labels"] + + def __init__( + self, + parent: QWidget | None = None, + config: ConnectionConfig | None = None, + client=None, + gui_id: str | None = None, + group_box_title: str = "Label", + label_config: dict = None, + **kwargs, + ) -> None: + if config is None: + config = ConnectionConfig(widget_class=self.__class__.__name__) + super().__init__(parent=parent, client=client, gui_id=gui_id, config=config, **kwargs) + + self.setTitle(group_box_title) + self.layout = QGridLayout(self) + self._labels = [] + if label_config is not None: + self.apply_label_config(label_config) + + def apply_label_config(self, label_config: dict): + """Apply the label configuration.""" + for label, config in label_config.items(): + defaults_value = config.get("defaults_value", 0.0) + units = config.get("units", None) + self.add_label(label, defaults_value=defaults_value, units=units) + + @property + def labels(self): + """Return the list of labels.""" + return self._labels + + @SafeProperty(str) + def qroup_box_title(self): + """Label of the group box.""" + return self.title() + + @qroup_box_title.setter + def qroup_box_title(self, value: str): + """Set the label of the group box.""" + self.setTitle(value) + + def add_label(self, label: str, defaults_value: float = 0.0, units: str = None): + """Add a label to the group box.""" + text_label = QLabel(parent=self) + text_label.setText(label) + value_label = QLabel(parent=self) + value_label.setText(str(defaults_value)) + unit_label = QLabel(parent=self) + unit_label.setText(units if units else "") + + spacer = QWidget(self) + spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) + + self.layout.addWidget(text_label, len(self.labels), 0) + self.layout.addWidget(spacer, len(self.labels), 1) + self.layout.addWidget(value_label, len(self.labels), 2) + self.layout.addWidget(unit_label, len(self.labels), 3) + + label_holder = { + "text_label": text_label, + "value_label": value_label, + "unit_label": unit_label, + } + + self._labels.append(label_holder) + + @SafeSlot(float, int) + def update_label(self, value: float, index: int): + """Update the label value.""" + if index < len(self.labels): + self.labels[index]["value_label"].setText(str(value)) + else: + raise IndexError("Index out of range for labels list.") + + +class DemoGUI(QWidget): + update_signal = Signal(float, int) + + def __init__(self): + super().__init__() + self.setWindowTitle("BECLabelBox Demo") + self.layout = QVBoxLayout(self) + + label_config = { + "Label 1": {"defaults_value": 0.0, "units": "m"}, + "Label 2": {"defaults_value": 1.0, "units": "cm"}, + "Label 3": {"defaults_value": 2.0, "units": "mm"}, + } + self.label_box = BECLabelBox( + self, group_box_title="Demo Label Box", label_config=label_config + ) + self.layout.addWidget(self.label_box) + + # self.label_box.apply_label_config(label_config) + + # alternative approach to add labels without dict config + # self.label_box.add_label("Label 1", defaults_value=0.0, units="m") + # self.label_box.add_label("Label 2", defaults_value=1.0, units="cm") + # self.label_box.add_label("Label 3", defaults_value=2.0, units="mm") + # + self.button = QPushButton("Update Labels", self) + self.button.clicked.connect(self.update_labels) + self.layout.addWidget(self.button) + + self.update_signal.connect(self.label_box.update_label) + + def update_labels(self): + """Update the labels with new values.""" + for i, label in enumerate(self.label_box.labels): + new_value = (i + 1) * 10.0 + self.update_signal.emit(new_value, i) + + +if __name__ == "__main__": + import sys + from qtpy.QtWidgets import QApplication + + app = QApplication(sys.argv) + demo = DemoGUI() + demo.show() + sys.exit(app.exec_()) +# +# if __name__ == "__main__": +# import sys +# from qtpy.QtWidgets import QApplication +# +# app = QApplication(sys.argv) +# widget = BECLabelBox(group_box_title="Test Label Box") +# widget.show() +# widget.add_label("Test Label 1") +# widget.add_label("Test Label 2", units="m") +# widget.add_label("Test Label 3", defaults_value=1.23456789, units="m") +# sys.exit(app.exec_())