From 2fa175517058c2fe8f41f3596b4855782e5c0a86 Mon Sep 17 00:00:00 2001 From: wyzula-jan <133381102+wyzula-jan@users.noreply.github.com> Date: Thu, 17 Aug 2023 15:53:51 +0200 Subject: [PATCH] test: crosshair mouse_moved signals for 1D and 2D --- bec_widgets/qt_utils/crosshair.py | 3 +- tests/test_crosshair.py | 149 ++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 tests/test_crosshair.py diff --git a/bec_widgets/qt_utils/crosshair.py b/bec_widgets/qt_utils/crosshair.py index bdab911a..82c80adf 100644 --- a/bec_widgets/qt_utils/crosshair.py +++ b/bec_widgets/qt_utils/crosshair.py @@ -54,7 +54,8 @@ class Crosshair(QObject): self.marker_2d = None for item in self.plot_item.items: if isinstance(item, pg.PlotDataItem): # 1D plot - color = item.opts["pen"].color() + pen = item.opts["pen"] + color = pen.color() if hasattr(pen, "color") else pg.mkColor(pen) marker_moved = pg.ScatterPlotItem( size=10, pen=pg.mkPen(color), brush=pg.mkBrush(None) ) diff --git a/tests/test_crosshair.py b/tests/test_crosshair.py new file mode 100644 index 00000000..78a8d8ac --- /dev/null +++ b/tests/test_crosshair.py @@ -0,0 +1,149 @@ +import numpy as np +import pyqtgraph as pg +from PyQt5.QtCore import QPointF + +from bec_widgets.qt_utils import Crosshair + + +def test_mouse_moved_lines(qtbot): + # Create a PlotWidget and add a PlotItem + plot_widget = pg.PlotWidget(title="1D PlotWidget with multiple curves") + plot_item = plot_widget.getPlotItem() + plot_item.plot([1, 2, 3], [4, 5, 6]) + + # Create a Crosshair instance + crosshair = Crosshair(plot_item=plot_item, precision=2) + + # Connect the signals to slots that will store the emitted values + emitted_values_1D = [] + crosshair.coordinatesChanged1D.connect(emitted_values_1D.append) + + # Simulate a mouse moved event at a specific position + pos_in_view = QPointF(2, 5) + pos_in_scene = plot_item.vb.mapViewToScene(pos_in_view) + event_mock = [pos_in_scene] + + # Call the mouse_moved method + crosshair.mouse_moved(event_mock) + + # Assert the expected behavior + assert crosshair.v_line.pos().x() == 2 + assert crosshair.h_line.pos().y() == 5 + + +def test_mouse_moved_signals(qtbot): + # Create a PlotWidget and add a PlotItem + plot_widget = pg.PlotWidget(title="1D PlotWidget with multiple curves") + plot_item = plot_widget.getPlotItem() + plot_item.plot([1, 2, 3], [4, 5, 6]) + + # Create a Crosshair instance + crosshair = Crosshair(plot_item=plot_item, precision=2) + + # Create a slot that will store the emitted values as tuples + emitted_values_1D = [] + + def slot(x, y_values): + emitted_values_1D.append((x, y_values)) + + # Connect the signal to the custom slot + crosshair.coordinatesChanged1D.connect(slot) + + # Simulate a mouse moved event at a specific position + pos_in_view = QPointF(2, 5) + pos_in_scene = plot_item.vb.mapViewToScene(pos_in_view) + event_mock = [pos_in_scene] + + # Call the mouse_moved method + crosshair.mouse_moved(event_mock) + + # Assert the expected behavior + assert emitted_values_1D == [(2.0, [5.0])] + + +def test_mouse_moved_signals_outside(qtbot): + # Create a PlotWidget and add a PlotItem + plot_widget = pg.PlotWidget(title="1D PlotWidget with multiple curves") + plot_item = plot_widget.getPlotItem() + plot_item.plot([1, 2, 3], [4, 5, 6]) + + # Create a Crosshair instance + crosshair = Crosshair(plot_item=plot_item, precision=2) + + # Create a slot that will store the emitted values as tuples + emitted_values_1D = [] + + def slot(x, y_values): + emitted_values_1D.append((x, y_values)) + + # Connect the signal to the custom slot + crosshair.coordinatesChanged1D.connect(slot) + + # Simulate a mouse moved event at a specific position + pos_in_view = QPointF(22, 55) + pos_in_scene = plot_item.vb.mapViewToScene(pos_in_view) + event_mock = [pos_in_scene] + + # Call the mouse_moved method + crosshair.mouse_moved(event_mock) + + # Assert the expected behavior + assert emitted_values_1D == [] + + +def test_mouse_moved_signals_2D(qtbot): + # write similar test for 2D plot + + # Create a PlotWidget and add a PlotItem + plot_widget = pg.PlotWidget(title="2D plot with crosshair and ROI square") + data_2D = np.random.random((100, 200)) + plot_item = plot_widget.getPlotItem() + image_item = pg.ImageItem(data_2D) + plot_item.addItem(image_item) + # Create a Crosshair instance + crosshair = Crosshair(plot_item=plot_item) + # Create a slot that will store the emitted values as tuples + emitted_values_2D = [] + + def slot(x, y): + emitted_values_2D.append((x, y)) + + # Connect the signal to the custom slot + crosshair.coordinatesChanged2D.connect(slot) + # Simulate a mouse moved event at a specific position + pos_in_view = QPointF(22.0, 55.0) + pos_in_scene = plot_item.vb.mapViewToScene(pos_in_view) + event_mock = [pos_in_scene] + # Call the mouse_moved method + crosshair.mouse_moved(event_mock) + # Assert the expected behavior + assert emitted_values_2D == [(22.0, 55.0)] + + +def test_mouse_moved_signals_2D_outside(qtbot): + # write similar test for 2D plot + + # Create a PlotWidget and add a PlotItem + plot_widget = pg.PlotWidget(title="2D plot with crosshair and ROI square") + data_2D = np.random.random((100, 200)) + plot_item = plot_widget.getPlotItem() + image_item = pg.ImageItem(data_2D) + plot_item.addItem(image_item) + # Create a Crosshair instance + crosshair = Crosshair(plot_item=plot_item, precision=2) + # Create a slot that will store the emitted values as tuples + emitted_values_2D = [] + + def slot(x, y): + emitted_values_2D.append((x, y)) + + # Connect the signal to the custom slot + crosshair.coordinatesChanged2D.connect(slot) + # Simulate a mouse moved event at a specific position + pos_in_view = QPointF(220.0, 555.0) + pos_in_scene = plot_item.vb.mapViewToScene(pos_in_view) + event_mock = [pos_in_scene] + # Call the mouse_moved method + crosshair.mouse_moved(event_mock) + # Assert the expected behavior + assert emitted_values_2D == []