From 55be644bf373e4de9b197d1c817889376027ad93 Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Tue, 2 Jul 2024 14:43:23 +0200 Subject: [PATCH] refactor(motor_map_widget): moved to top level --- bec_widgets/cli/client.py | 74 +++++++++- .../figure/plots/motor_map/motor_map.py | 10 +- bec_widgets/widgets/motor_map/__init__.py | 0 .../motor_map/assets/connection.svg | 0 .../plots => }/motor_map/assets/settings.svg | 0 .../widgets/motor_map/motor_map_toolbar.py | 43 ++++++ .../plots => }/motor_map/motor_map_widget.py | 133 ++++++++++++------ 7 files changed, 206 insertions(+), 54 deletions(-) create mode 100644 bec_widgets/widgets/motor_map/__init__.py rename bec_widgets/widgets/{figure/plots => }/motor_map/assets/connection.svg (100%) rename bec_widgets/widgets/{figure/plots => }/motor_map/assets/settings.svg (100%) create mode 100644 bec_widgets/widgets/motor_map/motor_map_toolbar.py rename bec_widgets/widgets/{figure/plots => }/motor_map/motor_map_widget.py (53%) diff --git a/bec_widgets/cli/client.py b/bec_widgets/cli/client.py index cd7a1454..3d7ac7f2 100644 --- a/bec_widgets/cli/client.py +++ b/bec_widgets/cli/client.py @@ -18,6 +18,7 @@ class Widgets(str, enum.Enum): BECDock = "BECDock" BECDockArea = "BECDockArea" BECFigure = "BECFigure" + BECMotorMapWidget = "BECMotorMapWidget" RingProgressBar = "RingProgressBar" ScanControl = "ScanControl" TextBox = "TextBox" @@ -1192,6 +1193,7 @@ class BECMotorMap(RPCBase): def get_data(self) -> "dict": """ Get the data of the motor map. + Returns: dict: Data of the motor map. """ @@ -1213,12 +1215,12 @@ class BECMotorMapWidget(RPCBase): @rpc_call def change_motors( self, - motor_x: str, - motor_y: str, - motor_x_entry: str = None, - motor_y_entry: str = None, - validate_bec: bool = True, - ) -> None: + motor_x: "str", + motor_y: "str", + motor_x_entry: "str" = None, + motor_y_entry: "str" = None, + validate_bec: "bool" = True, + ) -> "None": """ Change the active motors for the plot. @@ -1230,6 +1232,66 @@ class BECMotorMapWidget(RPCBase): validate_bec(bool, optional): If True, validate the signal with BEC. Defaults to True. """ + @rpc_call + def set_max_points(self, max_points: "int") -> "None": + """ + Set the maximum number of points to display on the motor map. + + Args: + max_points(int): Maximum number of points to display. + """ + + @rpc_call + def set_precision(self, precision: "int") -> "None": + """ + Set the precision of the motor map. + + Args: + precision(int): Precision to set. + """ + + @rpc_call + def set_num_dim_points(self, num_dim_points: "int") -> "None": + """ + Set the number of points to display on the motor map. + + Args: + num_dim_points(int): Number of points to display. + """ + + @rpc_call + def set_background_value(self, background_value: "int") -> "None": + """ + Set the background value of the motor map. + + Args: + background_value(int): Background value of the motor map. + """ + + @rpc_call + def set_scatter_size(self, scatter_size: "int") -> "None": + """ + Set the scatter size of the motor map. + + Args: + scatter_size(int): Scatter size of the motor map. + """ + + @rpc_call + def get_data(self) -> "dict": + """ + Get the data of the motor map. + + Returns: + dict: Data of the motor map. + """ + + @rpc_call + def reset_history(self) -> "None": + """ + Reset the history of the motor map. + """ + class BECPlotBase(RPCBase): @property diff --git a/bec_widgets/widgets/figure/plots/motor_map/motor_map.py b/bec_widgets/widgets/figure/plots/motor_map/motor_map.py index 799b4b53..9782153d 100644 --- a/bec_widgets/widgets/figure/plots/motor_map/motor_map.py +++ b/bec_widgets/widgets/figure/plots/motor_map/motor_map.py @@ -10,7 +10,7 @@ from pydantic import Field, ValidationError, field_validator from pydantic_core import PydanticCustomError from qtpy import QtCore, QtGui from qtpy.QtCore import Signal as pyqtSignal -from qtpy.QtCore import Slot as pyqtSlot +from qtpy.QtCore import Slot from qtpy.QtWidgets import QWidget from bec_widgets.utils import Colors, EntryValidator @@ -121,7 +121,7 @@ class BECMotorMap(BECPlotBase): motor_y_entry=self.config.signals.y.entry, ) - @pyqtSlot(str, str, str, str, bool) + @Slot(str, str, str, str, bool) def change_motors( self, motor_x: str, @@ -165,6 +165,7 @@ class BECMotorMap(BECPlotBase): def get_data(self) -> dict: """ Get the data of the motor map. + Returns: dict: Data of the motor map. """ @@ -179,7 +180,7 @@ class BECMotorMap(BECPlotBase): self.database_buffer["y"] = [self.database_buffer["y"][-1]] self.update_signal.emit() - def set_color(self, color: [str | tuple]): + def set_color(self, color: str | tuple): """ Set color of the motor trace. @@ -437,6 +438,7 @@ class BECMotorMap(BECPlotBase): print(f"The device '{motor}' does not have defined limits.") return None + @Slot() def _update_plot(self): """Update the motor map plot.""" # If the number of points exceeds max_points, delete the oldest points @@ -486,7 +488,7 @@ class BECMotorMap(BECPlotBase): f"Motor position: ({round(float(current_x),precision)}, {round(float(current_y),precision)})" ) - @pyqtSlot(dict) + @Slot(dict) def on_device_readback(self, msg: dict) -> None: """ Update the motor map plot with the new motor position. diff --git a/bec_widgets/widgets/motor_map/__init__.py b/bec_widgets/widgets/motor_map/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bec_widgets/widgets/figure/plots/motor_map/assets/connection.svg b/bec_widgets/widgets/motor_map/assets/connection.svg similarity index 100% rename from bec_widgets/widgets/figure/plots/motor_map/assets/connection.svg rename to bec_widgets/widgets/motor_map/assets/connection.svg diff --git a/bec_widgets/widgets/figure/plots/motor_map/assets/settings.svg b/bec_widgets/widgets/motor_map/assets/settings.svg similarity index 100% rename from bec_widgets/widgets/figure/plots/motor_map/assets/settings.svg rename to bec_widgets/widgets/motor_map/assets/settings.svg diff --git a/bec_widgets/widgets/motor_map/motor_map_toolbar.py b/bec_widgets/widgets/motor_map/motor_map_toolbar.py new file mode 100644 index 00000000..9eacb632 --- /dev/null +++ b/bec_widgets/widgets/motor_map/motor_map_toolbar.py @@ -0,0 +1,43 @@ +import os + +from qtpy.QtCore import QSize +from qtpy.QtGui import QAction, QIcon +from qtpy.QtWidgets import QHBoxLayout, QLabel, QWidget + +from bec_widgets.widgets.device_inputs import DeviceComboBox +from bec_widgets.widgets.toolbar.toolbar import ToolBarAction + + +class SettingsAction(ToolBarAction): + def add_to_toolbar(self, toolbar, target): + current_path = os.path.dirname(__file__) + icon = QIcon() + icon.addFile(os.path.join(current_path, "assets", "settings.svg"), size=QSize(20, 20)) + action = QAction(icon, "Config", target) + action.triggered.connect(lambda: print(target.config_dict)) + toolbar.addAction(action) + + +class DeviceSelectionAction(ToolBarAction): + def __init__(self, label: str): + self.label = label + self.device_combobox = DeviceComboBox(device_filter="Positioner") + + def add_to_toolbar(self, toolbar, target): + widget = QWidget() + layout = QHBoxLayout(widget) + + label = QLabel(f"{self.label}") + + layout.addWidget(label) + layout.addWidget(self.device_combobox) + toolbar.addWidget(widget) + + +class ConnectAction(ToolBarAction): + def add_to_toolbar(self, toolbar, target): + current_path = os.path.dirname(__file__) + icon = QIcon() + icon.addFile(os.path.join(current_path, "assets", "connection.svg"), size=QSize(20, 20)) + self.action = QAction(icon, "Connect Motors", target) + toolbar.addAction(self.action) diff --git a/bec_widgets/widgets/figure/plots/motor_map/motor_map_widget.py b/bec_widgets/widgets/motor_map/motor_map_widget.py similarity index 53% rename from bec_widgets/widgets/figure/plots/motor_map/motor_map_widget.py rename to bec_widgets/widgets/motor_map/motor_map_widget.py index e4173d22..a95f9933 100644 --- a/bec_widgets/widgets/figure/plots/motor_map/motor_map_widget.py +++ b/bec_widgets/widgets/motor_map/motor_map_widget.py @@ -1,54 +1,29 @@ -import os +from __future__ import annotations -from qtpy.QtCore import QSize, Slot -from qtpy.QtGui import QAction, QIcon -from qtpy.QtWidgets import QHBoxLayout, QLabel, QVBoxLayout, QWidget +from qtpy.QtWidgets import QVBoxLayout, QWidget from bec_widgets.utils import BECConnector -from bec_widgets.widgets.device_inputs import DeviceComboBox from bec_widgets.widgets.figure import BECFigure from bec_widgets.widgets.figure.plots.motor_map.motor_map import MotorMapConfig +from bec_widgets.widgets.motor_map.motor_map_toolbar import ( + ConnectAction, + DeviceSelectionAction, + SettingsAction, +) from bec_widgets.widgets.toolbar import ModularToolBar -from bec_widgets.widgets.toolbar.toolbar import ToolBarAction - - -class SettingsAction(ToolBarAction): - def add_to_toolbar(self, toolbar, target): - current_path = os.path.dirname(__file__) - icon = QIcon() - icon.addFile(os.path.join(current_path, "assets", "settings.svg"), size=QSize(20, 20)) - action = QAction(icon, "Config", target) - action.triggered.connect(lambda: print(target.config_dict)) - toolbar.addAction(action) - - -class DeviceSelectionAction(ToolBarAction): - def __init__(self, label: str): - self.label = label - self.device_combobox = DeviceComboBox(device_filter="Positioner") - - def add_to_toolbar(self, toolbar, target): - widget = QWidget() - layout = QHBoxLayout(widget) - - label = QLabel(f"{self.label}") - - layout.addWidget(label) - layout.addWidget(self.device_combobox) - toolbar.addWidget(widget) - - -class ConnectAction(ToolBarAction): - def add_to_toolbar(self, toolbar, target): - current_path = os.path.dirname(__file__) - icon = QIcon() - icon.addFile(os.path.join(current_path, "assets", "connection.svg"), size=QSize(20, 20)) - self.action = QAction(icon, "Connect Motors", target) - toolbar.addAction(self.action) class BECMotorMapWidget(BECConnector, QWidget): - USER_ACCESS = ["change_motors"] + USER_ACCESS = [ + "change_motors", + "set_max_points", + "set_precision", + "set_num_dim_points", + "set_background_value", + "set_scatter_size", + "get_data", + "reset_history", + ] def __init__( self, @@ -98,6 +73,10 @@ class BECMotorMapWidget(BECConnector, QWidget): motor_y = self.toolbar.widgets["motor_y"].device_combobox.currentText() self.change_motors(motor_x, motor_y, None, None, True) + ################################### + # User Access Methods from MotorMap + ################################### + def change_motors( self, motor_x: str, @@ -118,8 +97,74 @@ class BECMotorMapWidget(BECConnector, QWidget): """ self.map.change_motors(motor_x, motor_y, motor_x_entry, motor_y_entry, validate_bec) - def set(self, **kwargs): - self.map.set(**kwargs) + def get_data(self) -> dict: + """ + Get the data of the motor map. + + Returns: + dict: Data of the motor map. + """ + return self.map.get_data() + + def reset_history(self) -> None: + """ + Reset the history of the motor map. + """ + self.map.reset_history() + + def set_color(self, color: str | tuple): + """ + Set the color of the motor map. + + Args: + color(str, tuple): Color to set. + """ + self.map.set_color(color) + + def set_max_points(self, max_points: int) -> None: + """ + Set the maximum number of points to display on the motor map. + + Args: + max_points(int): Maximum number of points to display. + """ + self.map.set_max_points(max_points) + + def set_precision(self, precision: int) -> None: + """ + Set the precision of the motor map. + + Args: + precision(int): Precision to set. + """ + self.map.set_precision(precision) + + def set_num_dim_points(self, num_dim_points: int) -> None: + """ + Set the number of points to display on the motor map. + + Args: + num_dim_points(int): Number of points to display. + """ + self.map.set_num_dim_points(num_dim_points) + + def set_background_value(self, background_value: int) -> None: + """ + Set the background value of the motor map. + + Args: + background_value(int): Background value of the motor map. + """ + self.map.set_background_value(background_value) + + def set_scatter_size(self, scatter_size: int) -> None: + """ + Set the scatter size of the motor map. + + Args: + scatter_size(int): Scatter size of the motor map. + """ + self.map.set_scatter_size(scatter_size) if __name__ == "__main__":