mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
fix(widget_io): ToggleSwitchHandler added
This commit is contained in:
@ -15,6 +15,8 @@ from qtpy.QtWidgets import (
|
|||||||
QWidget,
|
QWidget,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from bec_widgets.widgets.utility.toggle.toggle import ToggleSwitch
|
||||||
|
|
||||||
|
|
||||||
class WidgetHandler(ABC):
|
class WidgetHandler(ABC):
|
||||||
"""Abstract base class for all widget handlers."""
|
"""Abstract base class for all widget handlers."""
|
||||||
@ -125,6 +127,19 @@ class CheckBoxHandler(WidgetHandler):
|
|||||||
widget.toggled.connect(lambda val, w=widget: slot(w, val))
|
widget.toggled.connect(lambda val, w=widget: slot(w, val))
|
||||||
|
|
||||||
|
|
||||||
|
class ToggleSwitchHandler(WidgetHandler):
|
||||||
|
"""Handler for ToggleSwitch widgets."""
|
||||||
|
|
||||||
|
def get_value(self, widget, **kwargs):
|
||||||
|
return widget.checked
|
||||||
|
|
||||||
|
def set_value(self, widget, value):
|
||||||
|
widget.checked = value
|
||||||
|
|
||||||
|
def connect_change_signal(self, widget: ToggleSwitch, slot):
|
||||||
|
widget.enabled.connect(lambda val, w=widget: slot(w, val))
|
||||||
|
|
||||||
|
|
||||||
class LabelHandler(WidgetHandler):
|
class LabelHandler(WidgetHandler):
|
||||||
"""Handler for QLabel widgets."""
|
"""Handler for QLabel widgets."""
|
||||||
|
|
||||||
@ -149,6 +164,7 @@ class WidgetIO:
|
|||||||
QDoubleSpinBox: SpinBoxHandler,
|
QDoubleSpinBox: SpinBoxHandler,
|
||||||
QCheckBox: CheckBoxHandler,
|
QCheckBox: CheckBoxHandler,
|
||||||
QLabel: LabelHandler,
|
QLabel: LabelHandler,
|
||||||
|
ToggleSwitch: ToggleSwitchHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -12,6 +12,7 @@ from qtpy.QtWidgets import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from bec_widgets.utils.widget_io import WidgetHierarchy, WidgetIO
|
from bec_widgets.utils.widget_io import WidgetHierarchy, WidgetIO
|
||||||
|
from bec_widgets.widgets.utility.toggle.toggle import ToggleSwitch
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
@ -23,10 +24,13 @@ def example_widget(qtbot):
|
|||||||
combo_box = QComboBox(main_widget)
|
combo_box = QComboBox(main_widget)
|
||||||
table_widget = QTableWidget(2, 2, main_widget)
|
table_widget = QTableWidget(2, 2, main_widget)
|
||||||
spin_box = QSpinBox(main_widget)
|
spin_box = QSpinBox(main_widget)
|
||||||
|
toggle = ToggleSwitch(main_widget)
|
||||||
|
|
||||||
layout.addWidget(line_edit)
|
layout.addWidget(line_edit)
|
||||||
layout.addWidget(combo_box)
|
layout.addWidget(combo_box)
|
||||||
layout.addWidget(table_widget)
|
layout.addWidget(table_widget)
|
||||||
layout.addWidget(spin_box)
|
layout.addWidget(spin_box)
|
||||||
|
layout.addWidget(toggle)
|
||||||
|
|
||||||
# Add text items to the combo box
|
# Add text items to the combo box
|
||||||
combo_box.addItems(["Option 1", "Option 2", "Option 3"])
|
combo_box.addItems(["Option 1", "Option 2", "Option 3"])
|
||||||
@ -60,44 +64,46 @@ def test_export_import_config(example_widget):
|
|||||||
|
|
||||||
expected_full = {
|
expected_full = {
|
||||||
"QWidget ()": {
|
"QWidget ()": {
|
||||||
"QVBoxLayout ()": {},
|
"QComboBox ()": {"QStandardItemModel ()": {}, "value": 1},
|
||||||
"QLineEdit ()": {"value": "New Text", "QObject ()": {}},
|
"QLineEdit ()": {"QObject ()": {}, "value": "New Text"},
|
||||||
"QComboBox ()": {"value": 1, "QStandardItemModel ()": {}},
|
"QSpinBox ()": {
|
||||||
|
"QLineEdit (qt_spinbox_lineedit)": {"QObject ()": {}, "value": "10"},
|
||||||
|
"QValidator (qt_spinboxvalidator)": {},
|
||||||
|
"value": 10,
|
||||||
|
},
|
||||||
"QTableWidget ()": {
|
"QTableWidget ()": {
|
||||||
"value": [["a", "b"], ["c", "d"]],
|
|
||||||
"QWidget (qt_scrollarea_viewport)": {},
|
|
||||||
"QStyledItemDelegate ()": {},
|
|
||||||
"QHeaderView ()": {
|
|
||||||
"QWidget (qt_scrollarea_viewport)": {},
|
|
||||||
"QWidget (qt_scrollarea_hcontainer)": {
|
|
||||||
"QScrollBar ()": {},
|
|
||||||
"QBoxLayout ()": {},
|
|
||||||
},
|
|
||||||
"QWidget (qt_scrollarea_vcontainer)": {
|
|
||||||
"QScrollBar ()": {},
|
|
||||||
"QBoxLayout ()": {},
|
|
||||||
},
|
|
||||||
"QItemSelectionModel ()": {},
|
|
||||||
},
|
|
||||||
"QAbstractButton ()": {},
|
"QAbstractButton ()": {},
|
||||||
"QAbstractTableModel ()": {},
|
"QAbstractTableModel ()": {},
|
||||||
|
"QHeaderView ()": {
|
||||||
|
"QItemSelectionModel ()": {},
|
||||||
|
"QWidget (qt_scrollarea_hcontainer)": {
|
||||||
|
"QBoxLayout ()": {},
|
||||||
|
"QScrollBar ()": {},
|
||||||
|
},
|
||||||
|
"QWidget (qt_scrollarea_vcontainer)": {
|
||||||
|
"QBoxLayout ()": {},
|
||||||
|
"QScrollBar ()": {},
|
||||||
|
},
|
||||||
|
"QWidget (qt_scrollarea_viewport)": {},
|
||||||
|
},
|
||||||
"QItemSelectionModel ()": {},
|
"QItemSelectionModel ()": {},
|
||||||
"QWidget (qt_scrollarea_hcontainer)": {"QScrollBar ()": {}, "QBoxLayout ()": {}},
|
"QStyledItemDelegate ()": {},
|
||||||
"QWidget (qt_scrollarea_vcontainer)": {"QScrollBar ()": {}, "QBoxLayout ()": {}},
|
"QWidget (qt_scrollarea_hcontainer)": {"QBoxLayout ()": {}, "QScrollBar ()": {}},
|
||||||
},
|
"QWidget (qt_scrollarea_vcontainer)": {"QBoxLayout ()": {}, "QScrollBar ()": {}},
|
||||||
"QSpinBox ()": {
|
"QWidget (qt_scrollarea_viewport)": {},
|
||||||
"value": 10,
|
"value": [["a", "b"], ["c", "d"]],
|
||||||
"QLineEdit (qt_spinbox_lineedit)": {"value": "10", "QObject ()": {}},
|
|
||||||
"QValidator (qt_spinboxvalidator)": {},
|
|
||||||
},
|
},
|
||||||
|
"QVBoxLayout ()": {},
|
||||||
|
"ToggleSwitch ()": {"value": True},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expected_reduced = {
|
expected_reduced = {
|
||||||
"QWidget ()": {
|
"QWidget ()": {
|
||||||
"QLineEdit ()": {"value": "New Text"},
|
|
||||||
"QComboBox ()": {"value": 1},
|
"QComboBox ()": {"value": 1},
|
||||||
|
"QLineEdit ()": {"value": "New Text"},
|
||||||
|
"QSpinBox ()": {"QLineEdit (qt_spinbox_lineedit)": {"value": "10"}, "value": 10},
|
||||||
"QTableWidget ()": {"value": [["a", "b"], ["c", "d"]]},
|
"QTableWidget ()": {"value": [["a", "b"], ["c", "d"]]},
|
||||||
"QSpinBox ()": {"value": 10, "QLineEdit (qt_spinbox_lineedit)": {"value": "10"}},
|
"ToggleSwitch ()": {"value": True},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,6 +117,7 @@ def test_widget_io_get_set_value(example_widget):
|
|||||||
combo_box = example_widget.findChild(QComboBox)
|
combo_box = example_widget.findChild(QComboBox)
|
||||||
table_widget = example_widget.findChild(QTableWidget)
|
table_widget = example_widget.findChild(QTableWidget)
|
||||||
spin_box = example_widget.findChild(QSpinBox)
|
spin_box = example_widget.findChild(QSpinBox)
|
||||||
|
toggle = example_widget.findChild(ToggleSwitch)
|
||||||
|
|
||||||
# Check initial values
|
# Check initial values
|
||||||
assert WidgetIO.get_value(line_edit) == ""
|
assert WidgetIO.get_value(line_edit) == ""
|
||||||
@ -120,18 +127,21 @@ def test_widget_io_get_set_value(example_widget):
|
|||||||
["Initial C", "Initial D"],
|
["Initial C", "Initial D"],
|
||||||
]
|
]
|
||||||
assert WidgetIO.get_value(spin_box) == 0
|
assert WidgetIO.get_value(spin_box) == 0
|
||||||
|
assert WidgetIO.get_value(toggle) == True
|
||||||
|
|
||||||
# Set new values
|
# Set new values
|
||||||
WidgetIO.set_value(line_edit, "Hello")
|
WidgetIO.set_value(line_edit, "Hello")
|
||||||
WidgetIO.set_value(combo_box, "Option 2")
|
WidgetIO.set_value(combo_box, "Option 2")
|
||||||
WidgetIO.set_value(table_widget, [["X", "Y"], ["Z", "W"]])
|
WidgetIO.set_value(table_widget, [["X", "Y"], ["Z", "W"]])
|
||||||
WidgetIO.set_value(spin_box, 5)
|
WidgetIO.set_value(spin_box, 5)
|
||||||
|
WidgetIO.set_value(toggle, False)
|
||||||
|
|
||||||
# Check updated values
|
# Check updated values
|
||||||
assert WidgetIO.get_value(line_edit) == "Hello"
|
assert WidgetIO.get_value(line_edit) == "Hello"
|
||||||
assert WidgetIO.get_value(combo_box, as_string=True) == "Option 2"
|
assert WidgetIO.get_value(combo_box, as_string=True) == "Option 2"
|
||||||
assert WidgetIO.get_value(table_widget) == [["X", "Y"], ["Z", "W"]]
|
assert WidgetIO.get_value(table_widget) == [["X", "Y"], ["Z", "W"]]
|
||||||
assert WidgetIO.get_value(spin_box) == 5
|
assert WidgetIO.get_value(spin_box) == 5
|
||||||
|
assert WidgetIO.get_value(toggle) == False
|
||||||
|
|
||||||
|
|
||||||
def test_widget_io_signal(qtbot, example_widget):
|
def test_widget_io_signal(qtbot, example_widget):
|
||||||
@ -140,6 +150,7 @@ def test_widget_io_signal(qtbot, example_widget):
|
|||||||
combo_box = example_widget.findChild(QComboBox)
|
combo_box = example_widget.findChild(QComboBox)
|
||||||
spin_box = example_widget.findChild(QSpinBox)
|
spin_box = example_widget.findChild(QSpinBox)
|
||||||
table_widget = example_widget.findChild(QTableWidget)
|
table_widget = example_widget.findChild(QTableWidget)
|
||||||
|
toggle = example_widget.findChild(ToggleSwitch)
|
||||||
|
|
||||||
# We'll store changes in a list to verify the slot is called
|
# We'll store changes in a list to verify the slot is called
|
||||||
changes = []
|
changes = []
|
||||||
@ -152,6 +163,7 @@ def test_widget_io_signal(qtbot, example_widget):
|
|||||||
WidgetIO.connect_widget_change_signal(combo_box, universal_slot)
|
WidgetIO.connect_widget_change_signal(combo_box, universal_slot)
|
||||||
WidgetIO.connect_widget_change_signal(spin_box, universal_slot)
|
WidgetIO.connect_widget_change_signal(spin_box, universal_slot)
|
||||||
WidgetIO.connect_widget_change_signal(table_widget, universal_slot)
|
WidgetIO.connect_widget_change_signal(table_widget, universal_slot)
|
||||||
|
WidgetIO.connect_widget_change_signal(toggle, universal_slot)
|
||||||
|
|
||||||
# Trigger changes
|
# Trigger changes
|
||||||
line_edit.setText("NewText")
|
line_edit.setText("NewText")
|
||||||
@ -173,3 +185,8 @@ def test_widget_io_signal(qtbot, example_widget):
|
|||||||
qtbot.waitUntil(lambda: len(changes) > 3)
|
qtbot.waitUntil(lambda: len(changes) > 3)
|
||||||
# The entire table value should be retrieved
|
# The entire table value should be retrieved
|
||||||
assert changes[-1][1][0][0] == "ChangedCell"
|
assert changes[-1][1][0][0] == "ChangedCell"
|
||||||
|
|
||||||
|
# Test the toggle switch
|
||||||
|
toggle.checked = False
|
||||||
|
qtbot.waitUntil(lambda: len(changes) > 4)
|
||||||
|
assert changes[-1][1] == False
|
||||||
|
Reference in New Issue
Block a user