bec_widgets.utils.widget_io =========================== .. py:module:: bec_widgets.utils.widget_io Attributes ---------- .. autoapisummary:: bec_widgets.utils.widget_io.TAncestor Classes ------- .. autoapisummary:: bec_widgets.utils.widget_io.CheckBoxHandler bec_widgets.utils.widget_io.ComboBoxHandler bec_widgets.utils.widget_io.LabelHandler bec_widgets.utils.widget_io.LineEditHandler bec_widgets.utils.widget_io.SlideHandler bec_widgets.utils.widget_io.SpinBoxHandler bec_widgets.utils.widget_io.TableWidgetHandler bec_widgets.utils.widget_io.ToggleSwitchHandler bec_widgets.utils.widget_io.WidgetHandler bec_widgets.utils.widget_io.WidgetHierarchy bec_widgets.utils.widget_io.WidgetIO bec_widgets.utils.widget_io.WidgetTreeNode Functions --------- .. autoapisummary:: bec_widgets.utils.widget_io.hierarchy_example bec_widgets.utils.widget_io.widget_io_signal_example Module Contents --------------- .. py:class:: CheckBoxHandler Bases: :py:obj:`WidgetHandler` Handler for QCheckBox widgets. .. py:method:: connect_change_signal(widget: qtpy.QtWidgets.QCheckBox, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: qtpy.QtWidgets.QCheckBox, **kwargs) Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QCheckBox, value) Set a value on the widget instance. .. py:class:: ComboBoxHandler Bases: :py:obj:`WidgetHandler` Handler for QComboBox widgets. .. py:method:: connect_change_signal(widget: qtpy.QtWidgets.QComboBox, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: qtpy.QtWidgets.QComboBox, as_string: bool = False, **kwargs) -> int | str Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QComboBox, value: int | str) -> None Set a value on the widget instance. .. py:class:: LabelHandler Bases: :py:obj:`WidgetHandler` Handler for QLabel widgets. .. py:method:: get_value(widget: qtpy.QtWidgets.QLabel, **kwargs) Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QLabel, value) Set a value on the widget instance. .. py:class:: LineEditHandler Bases: :py:obj:`WidgetHandler` Handler for QLineEdit widgets. .. py:method:: connect_change_signal(widget: qtpy.QtWidgets.QLineEdit, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: qtpy.QtWidgets.QLineEdit, **kwargs) -> str Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QLineEdit, value: str) -> None Set a value on the widget instance. .. py:class:: SlideHandler Bases: :py:obj:`WidgetHandler` Handler for QCheckBox widgets. .. py:method:: connect_change_signal(widget: qtpy.QtWidgets.QSlider, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: qtpy.QtWidgets.QSlider, **kwargs) Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QSlider, value) Set a value on the widget instance. .. py:class:: SpinBoxHandler Bases: :py:obj:`WidgetHandler` Handler for QSpinBox and QDoubleSpinBox widgets. .. py:method:: connect_change_signal(widget: qtpy.QtWidgets.QSpinBox | qtpy.QtWidgets.QDoubleSpinBox, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: qtpy.QtWidgets.QSpinBox | qtpy.QtWidgets.QDoubleSpinBox, **kwargs) Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QSpinBox | qtpy.QtWidgets.QDoubleSpinBox, value) Set a value on the widget instance. .. py:class:: TableWidgetHandler Bases: :py:obj:`WidgetHandler` Handler for QTableWidget widgets. .. py:method:: connect_change_signal(widget: qtpy.QtWidgets.QTableWidget, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: qtpy.QtWidgets.QTableWidget, **kwargs) -> list Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QTableWidget, value) -> None Set a value on the widget instance. .. py:class:: ToggleSwitchHandler Bases: :py:obj:`WidgetHandler` Handler for ToggleSwitch widgets. .. py:method:: connect_change_signal(widget: bec_widgets.widgets.utility.toggle.toggle.ToggleSwitch, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: bec_widgets.widgets.utility.toggle.toggle.ToggleSwitch, **kwargs) Retrieve value from the widget instance. .. py:method:: set_value(widget: bec_widgets.widgets.utility.toggle.toggle.ToggleSwitch, value) Set a value on the widget instance. .. py:class:: WidgetHandler Bases: :py:obj:`abc.ABC` Abstract base class for all widget handlers. .. py:method:: connect_change_signal(widget: qtpy.QtWidgets.QWidget, slot) Connect a change signal from this widget to the given slot. If the widget type doesn't have a known "value changed" signal, do nothing. slot: a function accepting two arguments (widget, value) .. py:method:: get_value(widget: qtpy.QtWidgets.QWidget, **kwargs) :abstractmethod: Retrieve value from the widget instance. .. py:method:: set_value(widget: qtpy.QtWidgets.QWidget, value) :abstractmethod: Set a value on the widget instance. .. py:class:: WidgetHierarchy .. py:method:: _build_prefix(branch_flags: list[bool]) -> str :staticmethod: .. py:method:: _filtered_children(widget: qtpy.QtWidgets.QWidget, exclude_internal_widgets: bool) -> list[qtpy.QtWidgets.QWidget] :staticmethod: .. py:method:: _iter_widget_tree_nodes(widget: qtpy.QtWidgets.QWidget, parent: qtpy.QtWidgets.QWidget | None, exclude_internal_widgets: bool, visited: set[int], branch_flags: list[bool], depth: int) :staticmethod: .. py:method:: export_config_to_dict(widget: qtpy.QtWidgets.QWidget, config: dict = None, indent: int = 0, grab_values: bool = False, print_hierarchy: bool = False, save_all: bool = True, exclude_internal_widgets: bool = True) -> dict :staticmethod: Export the widget hierarchy to a dictionary. :param widget: Widget to print the hierarchy of. :param config: Dictionary to export the hierarchy to. :type config: dict,optional :param indent: Level of indentation. :type indent: int,optional :param grab_values: Whether to grab the values of the widgets. :type grab_values: bool,optional :param print_hierarchy: Whether to print the hierarchy to the console. :type print_hierarchy: bool,optional :param save_all: Whether to save all widgets or only those with values. :type save_all: bool,optional :param exclude_internal_widgets: Whether to exclude internal widgets (e.g. QComboBox in PyQt6). :type exclude_internal_widgets: bool,optional :returns: Dictionary containing the widget hierarchy. :rtype: config(dict) .. py:method:: find_ancestor(widget: qtpy.QtWidgets.QWidget | bec_widgets.utils.BECConnector, ancestor_class: Type[TAncestor] | str) -> TAncestor | None :staticmethod: Find the closest ancestor of the specified class (or class-name string). :param widget: The starting widget. :type widget: QWidget :param ancestor_class: The ancestor class or class-name string to search for. :type ancestor_class: Type[TAncestor] | str :returns: The closest ancestor of the specified class, or None if not found. :rtype: TAncestor | None .. py:method:: get_bec_connectors_from_parent(widget) -> list :staticmethod: Return all BECConnector instances whose closest BECConnector ancestor is the given widget, including the widget itself if it is a BECConnector. .. py:method:: get_becwidget_ancestor(widget) :staticmethod: Traverse up the parent chain to find the nearest BECConnector. :param widget: Starting widget to find the ancestor for. :returns: The nearest ancestor that is a BECConnector, or None if not found. .. py:method:: import_config_from_dict(widget, config: dict, set_values: bool = False) -> None :staticmethod: Import the widget hierarchy from a dictionary. :param widget: Widget to import the hierarchy to. :param config: :param set_values: .. py:method:: iter_widget_tree(widget: qtpy.QtWidgets.QWidget, *, exclude_internal_widgets: bool = True) :staticmethod: Yield WidgetTreeNode entries for the widget hierarchy. .. py:method:: print_becconnector_hierarchy_from_app() :staticmethod: Enumerate ALL BECConnector objects in the QApplication. Also detect if a widget is a PlotBase, and add any data items (PlotDataItem-like) that are also BECConnector objects. Build a parent->children graph where each child's 'parent' is its closest BECConnector ancestor. Print the entire hierarchy from the root(s). The result is a single, consolidated tree for your entire running GUI, including PlotBase data items that are BECConnector. .. py:method:: print_widget_hierarchy(widget, indent: int = 0, grab_values: bool = False, prefix: str = '', exclude_internal_widgets: bool = True, only_bec_widgets: bool = False, show_parent: bool = True) -> None :staticmethod: Print the widget hierarchy to the console. :param widget: Widget to print the hierarchy of. :param indent: Level of indentation. :type indent: int, optional :param grab_values: Whether to grab the values of the widgets. :type grab_values: bool,optional :param prefix: Custom string prefix for indentation. :type prefix: str,optional :param exclude_internal_widgets: Whether to exclude internal widgets (e.g. QComboBox in PyQt6). :type exclude_internal_widgets: bool,optional :param only_bec_widgets: Whether to print only widgets that are instances of BECWidget. :type only_bec_widgets: bool, optional :param show_parent: Whether to display which BECWidget is the parent of each discovered BECWidget. :type show_parent: bool, optional .. py:class:: WidgetIO Public interface for getting, setting values and connecting signals using handler mapping .. py:method:: _find_handler(widget) :staticmethod: Find the appropriate handler for the widget by checking its base classes. :param widget: Widget instance. :returns: The handler class if found, otherwise None. :rtype: handler_class .. py:method:: check_and_adjust_limits(spin_box: qtpy.QtWidgets.QDoubleSpinBox, number: float) :staticmethod: Check if the new limits are within the current limits, if not adjust the limits. :param number: The new value to check against the limits. :type number: float .. py:method:: connect_widget_change_signal(widget, slot) :staticmethod: Connect the widget's value-changed signal to a generic slot function (widget, value). This now delegates the logic to the widget's handler. .. py:method:: find_widgets(widget_class: qtpy.QtWidgets.QWidget | str, recursive: bool = True) -> list[qtpy.QtWidgets.QWidget] :staticmethod: Return widgets matching the given class (or class-name string). :param widget_class: Either a QWidget subclass or its class-name as a string. :param recursive: If True (default), traverse all top-level widgets and their children; if False, scan app.allWidgets() for a flat list. :returns: List of QWidget instances matching the class or class-name. .. py:method:: get_value(widget, ignore_errors=False, **kwargs) :staticmethod: Retrieve value from the widget instance. :param widget: Widget instance. :param ignore_errors: Whether to ignore if no handler is found. :type ignore_errors: bool, optional .. py:method:: set_value(widget, value, ignore_errors=False) :staticmethod: Set a value on the widget instance. :param widget: Widget instance. :param value: Value to set. :param ignore_errors: Whether to ignore if no handler is found. :type ignore_errors: bool, optional .. py:class:: WidgetTreeNode .. py:attribute:: depth :type: int .. py:attribute:: parent :type: qtpy.QtWidgets.QWidget | None .. py:attribute:: prefix :type: str .. py:attribute:: widget :type: qtpy.QtWidgets.QWidget .. py:function:: hierarchy_example() .. py:function:: widget_io_signal_example() .. py:data:: TAncestor