From ab689a76ed8fb142cd7d9c5e278f7818685a51f4 Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Tue, 28 May 2024 16:04:16 +0200 Subject: [PATCH] fix(crosshair): fixed signals --- bec_widgets/utils/crosshair.py | 22 +++++---- .../movement_absolute/movement_absolute.py | 48 ++++++++++--------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/bec_widgets/utils/crosshair.py b/bec_widgets/utils/crosshair.py index 43b385cd..193d43e1 100644 --- a/bec_widgets/utils/crosshair.py +++ b/bec_widgets/utils/crosshair.py @@ -8,13 +8,13 @@ from qtpy.QtCore import Signal as pyqtSignal class Crosshair(QObject): # Signal for 1D plot - coordinatesChanged1D = pyqtSignal(float, list) - coordinatesClicked1D = pyqtSignal(float, list) + coordinatesChanged1D = pyqtSignal(tuple) + coordinatesClicked1D = pyqtSignal(tuple) # Signal for 2D plot - coordinatesChanged2D = pyqtSignal(float, float) - coordinatesClicked2D = pyqtSignal(float, float) + coordinatesChanged2D = pyqtSignal(tuple) + coordinatesClicked2D = pyqtSignal(tuple) - def __init__(self, plot_item: pg.PlotItem, precision: int = None, parent=None): + def __init__(self, plot_item: pg.PlotItem, precision: int = 3, parent=None): """ Crosshair for 1D and 2D plots. @@ -174,10 +174,11 @@ class Crosshair(QObject): if isinstance(item, pg.PlotDataItem): if x is None or all(v is None for v in y_values): return - self.coordinatesChanged1D.emit( + coordinance_to_emit = ( round(x, self.precision), [round(y_val, self.precision) for y_val in y_values], ) + self.coordinatesChanged1D.emit(coordinance_to_emit) for i, y_val in enumerate(y_values): self.marker_moved_1d[i].setData( [x if not self.is_log_x else np.log10(x)], @@ -186,7 +187,8 @@ class Crosshair(QObject): elif isinstance(item, pg.ImageItem): if x is None or y_values is None: return - self.coordinatesChanged2D.emit(x, y_values) + coordinance_to_emit = (x, y_values) + self.coordinatesChanged2D.emit(coordinance_to_emit) def mouse_clicked(self, event): """Handles the mouse clicked event, updating the crosshair position and emitting signals. @@ -209,10 +211,11 @@ class Crosshair(QObject): if isinstance(item, pg.PlotDataItem): if x is None or all(v is None for v in y_values): return - self.coordinatesClicked1D.emit( + coordinate_to_emit = ( round(x, self.precision), [round(y_val, self.precision) for y_val in y_values], ) + self.coordinatesClicked1D.emit(coordinate_to_emit) for i, y_val in enumerate(y_values): for marker in self.marker_clicked_1d[i]: marker.setData( @@ -222,7 +225,8 @@ class Crosshair(QObject): elif isinstance(item, pg.ImageItem): if x is None or y_values is None: return - self.coordinatesClicked2D.emit(x, y_values) + coordinate_to_emit = (x, y_values) + self.coordinatesClicked2D.emit(coordinate_to_emit) self.marker_2d.setPos([x, y_values]) def check_log(self): diff --git a/bec_widgets/widgets/motor_control/movement_absolute/movement_absolute.py b/bec_widgets/widgets/motor_control/movement_absolute/movement_absolute.py index 09222894..c6167dd0 100644 --- a/bec_widgets/widgets/motor_control/movement_absolute/movement_absolute.py +++ b/bec_widgets/widgets/motor_control/movement_absolute/movement_absolute.py @@ -1,10 +1,12 @@ import os +from qtpy.QtWidgets import QWidget from qtpy import uic from qtpy.QtCore import Signal as pyqtSignal from qtpy.QtCore import Slot as pyqtSlot -from bec_widgets.widgets.motor_control.motor_control import MotorControlWidget +from bec_widgets.utils import UILoader +from bec_widgets.widgets.motor_control.motor_control import MotorControlWidget, MotorControlErrors class MotorControlAbsolute(MotorControlWidget): @@ -23,26 +25,26 @@ class MotorControlAbsolute(MotorControlWidget): def _load_ui(self): """Load the UI from the .ui file.""" current_path = os.path.dirname(__file__) - uic.loadUi(os.path.join(current_path, "movement_absolute.ui"), self) + self.ui = UILoader().load_ui(os.path.join(current_path, "movement_absolute.ui"), self) def _init_ui(self): """Initialize the UI.""" # Check if there are any motors connected if self.motor_x is None or self.motor_y is None: - self.motorControl_absolute.setEnabled(False) + self.ui.motorControl_absolute.setEnabled(False) return # Move to absolute coordinates - self.pushButton_go_absolute.clicked.connect( + self.ui.pushButton_go_absolute.clicked.connect( lambda: self.move_motor_absolute( - self.spinBox_absolute_x.value(), self.spinBox_absolute_y.value() + self.ui.spinBox_absolute_x.value(), self.ui.spinBox_absolute_y.value() ) ) - self.pushButton_set.clicked.connect(self.save_absolute_coordinates) - self.pushButton_save.clicked.connect(self.save_current_coordinates) - self.pushButton_stop.clicked.connect(self.motor_thread.stop_movement) + self.ui.pushButton_set.clicked.connect(self.save_absolute_coordinates) + self.ui.pushButton_save.clicked.connect(self.save_current_coordinates) + self.ui.pushButton_stop.clicked.connect(self.motor_thread.stop_movement) # Enable/Disable GUI self.motor_thread.lock_gui.connect(self.enable_motor_controls) @@ -80,11 +82,11 @@ class MotorControlAbsolute(MotorControlWidget): """ # Disable or enable all controls within the motorControl_absolute group box - for widget in self.motorControl_absolute.findChildren(QWidget): + for widget in self.ui.motorControl_absolute.findChildren(QWidget): widget.setEnabled(enable) # Enable the pushButton_stop if the motor is moving - self.pushButton_stop.setEnabled(True) + self.ui.pushButton_stop.setEnabled(True) @pyqtSlot(str, str) def change_motors(self, motor_x: str, motor_y: str): @@ -109,8 +111,8 @@ class MotorControlAbsolute(MotorControlWidget): """ self.precision = precision self.config["motor_control"]["precision"] = precision - self.spinBox_absolute_x.setDecimals(precision) - self.spinBox_absolute_y.setDecimals(precision) + self.ui.spinBox_absolute_x.setDecimals(precision) + self.ui.spinBox_absolute_y.setDecimals(precision) def move_motor_absolute(self, x: float, y: float) -> None: """ @@ -122,32 +124,32 @@ class MotorControlAbsolute(MotorControlWidget): # self._enable_motor_controls(False) target_coordinates = (x, y) self.motor_thread.move_absolute(self.motor_x, self.motor_y, target_coordinates) - if self.checkBox_save_with_go.isChecked(): + if self.ui.checkBox_save_with_go.isChecked(): self.save_absolute_coordinates() def _init_keyboard_shortcuts(self): """Initialize the keyboard shortcuts.""" # Go absolute button - self.pushButton_go_absolute.setShortcut("Ctrl+G") - self.pushButton_go_absolute.setToolTip("Ctrl+G") + self.ui.pushButton_go_absolute.setShortcut("Ctrl+G") + self.ui.pushButton_go_absolute.setToolTip("Ctrl+G") # Set absolute coordinates - self.pushButton_set.setShortcut("Ctrl+D") - self.pushButton_set.setToolTip("Ctrl+D") + self.ui.pushButton_set.setShortcut("Ctrl+D") + self.ui.pushButton_set.setToolTip("Ctrl+D") # Save Current coordinates - self.pushButton_save.setShortcut("Ctrl+S") - self.pushButton_save.setToolTip("Ctrl+S") + self.ui.pushButton_save.setShortcut("Ctrl+S") + self.ui.pushButton_save.setToolTip("Ctrl+S") # Stop Button - self.pushButton_stop.setShortcut("Ctrl+X") - self.pushButton_stop.setToolTip("Ctrl+X") + self.ui.pushButton_stop.setShortcut("Ctrl+X") + self.ui.pushButton_stop.setToolTip("Ctrl+X") def save_absolute_coordinates(self): """Emit the setup coordinates from the spinboxes""" - x, y = round(self.spinBox_absolute_x.value(), self.precision), round( - self.spinBox_absolute_y.value(), self.precision + x, y = round(self.ui.spinBox_absolute_x.value(), self.precision), round( + self.ui.spinBox_absolute_y.value(), self.precision ) self.coordinates_signal.emit((x, y))