mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 11:41:49 +02:00
refactor: widget_hierarchy.py changed into general purpose modul to extract values from widgets using handlers
This commit is contained in:
@ -10,40 +10,41 @@ from PyQt5.QtWidgets import (
|
|||||||
QVBoxLayout,
|
QVBoxLayout,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
class WidgetHandler:
|
|
||||||
@staticmethod
|
|
||||||
def get_value(widget):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
@staticmethod
|
class WidgetHandler(ABC):
|
||||||
def set_value(widget, value):
|
"""Abstract base class for all widget handlers."""
|
||||||
raise NotImplementedError
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_value(self, widget: QWidget):
|
||||||
|
"""Retrieve value from the widget instance."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def set_value(self, widget: QWidget, value):
|
||||||
|
"""Set a value on the widget instance."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class LineEditHandler(WidgetHandler):
|
class LineEditHandler(WidgetHandler):
|
||||||
@staticmethod
|
def get_value(self, widget: QLineEdit) -> str:
|
||||||
def get_value(widget):
|
|
||||||
return widget.text()
|
return widget.text()
|
||||||
|
|
||||||
@staticmethod
|
def set_value(self, widget: QLineEdit, value: str) -> None:
|
||||||
def set_value(widget, value):
|
|
||||||
widget.setText(value)
|
widget.setText(value)
|
||||||
|
|
||||||
|
|
||||||
class ComboBoxHandler(WidgetHandler):
|
class ComboBoxHandler(WidgetHandler):
|
||||||
@staticmethod
|
def get_value(self, widget: QComboBox) -> int:
|
||||||
def get_value(widget):
|
|
||||||
return widget.currentIndex()
|
return widget.currentIndex()
|
||||||
|
|
||||||
@staticmethod
|
def set_value(self, widget: QComboBox, value: int) -> None:
|
||||||
def set_value(widget, value):
|
|
||||||
widget.setCurrentIndex(value)
|
widget.setCurrentIndex(value)
|
||||||
|
|
||||||
|
|
||||||
class TableWidgetHandler(WidgetHandler):
|
class TableWidgetHandler(WidgetHandler):
|
||||||
@staticmethod
|
def get_value(self, widget: QTableWidget) -> list:
|
||||||
def get_value(widget):
|
|
||||||
return [
|
return [
|
||||||
[
|
[
|
||||||
widget.item(row, col).text() if widget.item(row, col) else ""
|
widget.item(row, col).text() if widget.item(row, col) else ""
|
||||||
@ -52,8 +53,7 @@ class TableWidgetHandler(WidgetHandler):
|
|||||||
for row in range(widget.rowCount())
|
for row in range(widget.rowCount())
|
||||||
]
|
]
|
||||||
|
|
||||||
@staticmethod
|
def set_value(self, widget: QTableWidget, value) -> None:
|
||||||
def set_value(widget, value):
|
|
||||||
for row, row_values in enumerate(value):
|
for row, row_values in enumerate(value):
|
||||||
for col, cell_value in enumerate(row_values):
|
for col, cell_value in enumerate(row_values):
|
||||||
item = QTableWidgetItem(str(cell_value))
|
item = QTableWidgetItem(str(cell_value))
|
||||||
@ -61,134 +61,186 @@ class TableWidgetHandler(WidgetHandler):
|
|||||||
|
|
||||||
|
|
||||||
class SpinBoxHandler(WidgetHandler):
|
class SpinBoxHandler(WidgetHandler):
|
||||||
@staticmethod
|
def get_value(self, widget):
|
||||||
def get_value(widget):
|
|
||||||
return widget.value()
|
return widget.value()
|
||||||
|
|
||||||
@staticmethod
|
def set_value(self, widget, value):
|
||||||
def set_value(widget, value):
|
|
||||||
widget.setValue(value)
|
widget.setValue(value)
|
||||||
|
|
||||||
|
|
||||||
HANDLERS = {
|
# TODO remove when widgetIO works
|
||||||
QLineEdit: LineEditHandler,
|
# HANDLERS = {
|
||||||
QComboBox: ComboBoxHandler,
|
# QLineEdit: LineEditHandler,
|
||||||
QTableWidget: TableWidgetHandler,
|
# QComboBox: ComboBoxHandler,
|
||||||
QSpinBox: SpinBoxHandler,
|
# QTableWidget: TableWidgetHandler,
|
||||||
QDoubleSpinBox: SpinBoxHandler,
|
# QSpinBox: SpinBoxHandler,
|
||||||
}
|
# QDoubleSpinBox: SpinBoxHandler,
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
|
||||||
|
##################### Public interface for getting and setting values #####################
|
||||||
|
# def get_value(widget):
|
||||||
|
# handler_class = HANDLERS.get(type(widget))
|
||||||
|
# if handler_class:
|
||||||
|
# return handler_class.get_value(widget)
|
||||||
|
# return None
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# def set_value(widget, value):
|
||||||
|
# handler_class = HANDLERS.get(type(widget))
|
||||||
|
# if handler_class:
|
||||||
|
# handler_class.set_value(widget, value)
|
||||||
|
|
||||||
|
|
||||||
def get_value(widget):
|
class WidgetIO:
|
||||||
handler_class = HANDLERS.get(type(widget))
|
"""Public interface for getting and setting values using handler mapping"""
|
||||||
if handler_class:
|
|
||||||
return handler_class.get_value(widget)
|
_handlers = {
|
||||||
return None
|
QLineEdit: LineEditHandler,
|
||||||
|
QComboBox: ComboBoxHandler,
|
||||||
|
QTableWidget: TableWidgetHandler,
|
||||||
|
QSpinBox: SpinBoxHandler,
|
||||||
|
QDoubleSpinBox: SpinBoxHandler,
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_value(widget, ignore_errors=False):
|
||||||
|
"""
|
||||||
|
Retrieve value from the widget instance.
|
||||||
|
Args:
|
||||||
|
widget: Widget instance.
|
||||||
|
ignore_errors(bool, optional): Whether to ignore if no handler is found.
|
||||||
|
"""
|
||||||
|
handler_class = WidgetIO._handlers.get(type(widget))
|
||||||
|
if handler_class:
|
||||||
|
return handler_class().get_value(widget) # Instantiate the handler
|
||||||
|
if not ignore_errors:
|
||||||
|
raise ValueError(f"No handler for widget type: {type(widget)}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set_value(widget, value, ignore_errors=False):
|
||||||
|
"""
|
||||||
|
Set a value on the widget instance.
|
||||||
|
Args:
|
||||||
|
widget: Widget instance.
|
||||||
|
value: Value to set.
|
||||||
|
ignore_errors(bool, optional): Whether to ignore if no handler is found.
|
||||||
|
"""
|
||||||
|
handler_class = WidgetIO._handlers.get(type(widget))
|
||||||
|
if handler_class:
|
||||||
|
handler_class().set_value(widget, value) # Instantiate the handler
|
||||||
|
elif not ignore_errors:
|
||||||
|
raise ValueError(f"No handler for widget type: {type(widget)}")
|
||||||
|
|
||||||
|
|
||||||
def set_value(widget, value):
|
##################### Public interface for exporting and importing widget hierarchies #####################
|
||||||
handler_class = HANDLERS.get(type(widget))
|
|
||||||
if handler_class:
|
|
||||||
handler_class.set_value(widget, value)
|
|
||||||
|
|
||||||
|
|
||||||
def print_widget_hierarchy(
|
class WidgetHierarchy:
|
||||||
widget, indent: int = 0, grab_values: bool = False, prefix: str = ""
|
@staticmethod
|
||||||
) -> None:
|
def print_widget_hierarchy(
|
||||||
"""
|
widget, indent: int = 0, grab_values: bool = False, prefix: str = ""
|
||||||
Print the widget hierarchy to the console.
|
) -> None:
|
||||||
Args:
|
"""
|
||||||
widget: Widget to print the hierarchy of.
|
Print the widget hierarchy to the console.
|
||||||
indent(int, optional): Level of indentation.
|
Args:
|
||||||
grab_values(bool,optional): Whether to grab the values of the widgets.
|
widget: Widget to print the hierarchy of.
|
||||||
prefix(stc,optional): Custom string prefix for indentation.
|
indent(int, optional): Level of indentation.
|
||||||
"""
|
grab_values(bool,optional): Whether to grab the values of the widgets.
|
||||||
widget_info = f"{widget.__class__.__name__} ({widget.objectName()})"
|
prefix(stc,optional): Custom string prefix for indentation.
|
||||||
if grab_values:
|
"""
|
||||||
value = get_value(widget)
|
widget_info = f"{widget.__class__.__name__} ({widget.objectName()})"
|
||||||
value_str = f" [value: {value}]" if value is not None else ""
|
if grab_values:
|
||||||
widget_info += value_str
|
value = WidgetIO.get_value(widget, ignore_errors=True)
|
||||||
|
value_str = f" [value: {value}]" if value is not None else ""
|
||||||
|
widget_info += value_str
|
||||||
|
|
||||||
print(prefix + widget_info)
|
print(prefix + widget_info)
|
||||||
|
|
||||||
children = widget.children()
|
children = widget.children()
|
||||||
for child in children:
|
for child in children:
|
||||||
child_prefix = prefix + " "
|
child_prefix = prefix + " "
|
||||||
arrow = "├─ " if child != children[-1] else "└─ "
|
arrow = "├─ " if child != children[-1] else "└─ "
|
||||||
print_widget_hierarchy(child, indent + 1, grab_values, prefix=child_prefix + arrow)
|
WidgetHierarchy.print_widget_hierarchy(
|
||||||
|
child, indent + 1, grab_values, prefix=child_prefix + arrow
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def export_config_to_dict(
|
||||||
|
widget,
|
||||||
|
config=None,
|
||||||
|
indent=0,
|
||||||
|
grab_values: bool = False,
|
||||||
|
print_hierarchy: bool = False,
|
||||||
|
save_all: bool = True,
|
||||||
|
) -> dict:
|
||||||
|
"""
|
||||||
|
Export the widget hierarchy to a dictionary.
|
||||||
|
Args:
|
||||||
|
widget: Widget to print the hierarchy of.
|
||||||
|
config(dict,optional): Dictionary to export the hierarchy to.
|
||||||
|
indent(int,optional): Level of indentation.
|
||||||
|
grab_values(bool,optional): Whether to grab the values of the widgets.
|
||||||
|
print_hierarchy(bool,optional): Whether to print the hierarchy to the console.
|
||||||
|
save_all(bool,optional): Whether to save all widgets or only those with values.
|
||||||
|
Returns:
|
||||||
|
config(dict): Dictionary containing the widget hierarchy.
|
||||||
|
"""
|
||||||
|
if config is None:
|
||||||
|
config = {}
|
||||||
|
widget_info = f"{widget.__class__.__name__} ({widget.objectName()})"
|
||||||
|
|
||||||
def export_config_to_dict(
|
if grab_values and type(widget) in WidgetIO._handlers:
|
||||||
widget,
|
value = WidgetIO.get_value(widget, ignore_errors=True)
|
||||||
config=None,
|
if value is not None or save_all:
|
||||||
indent=0,
|
if widget_info not in config:
|
||||||
grab_values: bool = False,
|
config[widget_info] = {}
|
||||||
print_hierarchy: bool = False,
|
if value is not None:
|
||||||
save_all: bool = True,
|
config[widget_info]["value"] = value
|
||||||
) -> dict:
|
|
||||||
"""
|
|
||||||
Export the widget hierarchy to a dictionary.
|
|
||||||
Args:
|
|
||||||
widget: Widget to print the hierarchy of.
|
|
||||||
config(dict,optional): Dictionary to export the hierarchy to.
|
|
||||||
indent(int,optional): Level of indentation.
|
|
||||||
grab_values(bool,optional): Whether to grab the values of the widgets.
|
|
||||||
print_hierarchy(bool,optional): Whether to print the hierarchy to the console.
|
|
||||||
save_all(bool,optional): Whether to save all widgets or only those with values.
|
|
||||||
Returns:
|
|
||||||
config(dict): Dictionary containing the widget hierarchy.
|
|
||||||
"""
|
|
||||||
if config is None:
|
|
||||||
config = {}
|
|
||||||
widget_info = f"{widget.__class__.__name__} ({widget.objectName()})"
|
|
||||||
|
|
||||||
if grab_values:
|
if print_hierarchy:
|
||||||
value = get_value(widget)
|
WidgetHierarchy.print_widget_hierarchy(widget, indent, grab_values)
|
||||||
if value is not None or save_all:
|
|
||||||
if widget_info not in config:
|
|
||||||
config[widget_info] = {}
|
|
||||||
if value is not None:
|
|
||||||
config[widget_info]["value"] = value
|
|
||||||
|
|
||||||
if print_hierarchy:
|
for child in widget.children():
|
||||||
print_widget_hierarchy(widget, indent, grab_values)
|
child_config = WidgetHierarchy.export_config_to_dict(
|
||||||
|
child, None, indent + 1, grab_values, print_hierarchy, save_all
|
||||||
|
)
|
||||||
|
if child_config or save_all:
|
||||||
|
if widget_info not in config:
|
||||||
|
config[widget_info] = {}
|
||||||
|
config[widget_info].update(child_config)
|
||||||
|
|
||||||
for child in widget.children():
|
return config
|
||||||
child_config = export_config_to_dict(
|
|
||||||
child, None, indent + 1, grab_values, print_hierarchy, save_all
|
|
||||||
)
|
|
||||||
if child_config or save_all:
|
|
||||||
if widget_info not in config:
|
|
||||||
config[widget_info] = {}
|
|
||||||
config[widget_info].update(child_config)
|
|
||||||
|
|
||||||
return config
|
@staticmethod
|
||||||
|
def import_config_from_dict(widget, config: dict, set_values: bool = False) -> None:
|
||||||
|
"""
|
||||||
def import_config_from_dict(widget, config: dict, set_values: bool = False) -> None:
|
Import the widget hierarchy from a dictionary.
|
||||||
"""
|
Args:
|
||||||
Import the widget hierarchy from a dictionary.
|
widget: Widget to import the hierarchy to.
|
||||||
Args:
|
config:
|
||||||
widget: Widget to import the hierarchy to.
|
set_values:
|
||||||
config:
|
"""
|
||||||
set_values:
|
widget_name = f"{widget.__class__.__name__} ({widget.objectName()})"
|
||||||
"""
|
widget_config = config.get(widget_name, {})
|
||||||
widget_name = f"{widget.__class__.__name__} ({widget.objectName()})"
|
for child in widget.children():
|
||||||
widget_config = config.get(widget_name, {})
|
child_name = f"{child.__class__.__name__} ({child.objectName()})"
|
||||||
for child in widget.children():
|
child_config = widget_config.get(child_name)
|
||||||
child_name = f"{child.__class__.__name__} ({child.objectName()})"
|
if child_config is not None:
|
||||||
child_config = widget_config.get(child_name)
|
value = child_config.get("value")
|
||||||
if child_config is not None:
|
if set_values and value is not None:
|
||||||
value = child_config.get("value")
|
WidgetIO.set_value(child, value)
|
||||||
if set_values and value is not None:
|
WidgetHierarchy.import_config_from_dict(child, widget_config, set_values)
|
||||||
set_value(child, value)
|
|
||||||
import_config_from_dict(child, widget_config, set_values)
|
|
||||||
|
|
||||||
|
|
||||||
# Example application to demonstrate the usage of the functions
|
# Example application to demonstrate the usage of the functions
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = QApplication([])
|
app = QApplication([])
|
||||||
|
|
||||||
|
# Create instance of WidgetHierarchy
|
||||||
|
widget_hierarchy = WidgetHierarchy()
|
||||||
|
|
||||||
# Create a simple widget hierarchy for demonstration purposes
|
# Create a simple widget hierarchy for demonstration purposes
|
||||||
main_widget = QWidget()
|
main_widget = QWidget()
|
||||||
layout = QVBoxLayout(main_widget)
|
layout = QVBoxLayout(main_widget)
|
||||||
@ -210,7 +262,9 @@ if __name__ == "__main__":
|
|||||||
print(30 * "#")
|
print(30 * "#")
|
||||||
print(f"Widget hierarchy for {main_widget.objectName()}:")
|
print(f"Widget hierarchy for {main_widget.objectName()}:")
|
||||||
print(30 * "#")
|
print(30 * "#")
|
||||||
config_dict = export_config_to_dict(main_widget, grab_values=True, print_hierarchy=True)
|
config_dict = widget_hierarchy.export_config_to_dict(
|
||||||
|
main_widget, grab_values=True, print_hierarchy=True
|
||||||
|
)
|
||||||
print(30 * "#")
|
print(30 * "#")
|
||||||
print(f"Config dict: {config_dict}")
|
print(f"Config dict: {config_dict}")
|
||||||
|
|
||||||
@ -223,10 +277,12 @@ if __name__ == "__main__":
|
|||||||
"QSpinBox ()": {"value": 10},
|
"QSpinBox ()": {"value": 10},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
import_config_from_dict(main_widget, new_config_dict, set_values=True)
|
widget_hierarchy.import_config_from_dict(main_widget, new_config_dict, set_values=True)
|
||||||
print(30 * "#")
|
print(30 * "#")
|
||||||
config_dict_new = export_config_to_dict(main_widget, grab_values=True, print_hierarchy=True)
|
config_dict_new = widget_hierarchy.export_config_to_dict(
|
||||||
config_dict_new_reduced = export_config_to_dict(
|
main_widget, grab_values=True, print_hierarchy=True
|
||||||
|
)
|
||||||
|
config_dict_new_reduced = widget_hierarchy.export_config_to_dict(
|
||||||
main_widget, grab_values=True, print_hierarchy=True, save_all=False
|
main_widget, grab_values=True, print_hierarchy=True, save_all=False
|
||||||
)
|
)
|
||||||
print(30 * "#")
|
print(30 * "#")
|
||||||
|
Reference in New Issue
Block a user