From 0363fd5194320a7ea868ef883f8022ea464d0298 Mon Sep 17 00:00:00 2001 From: wyzula-jan <133381102+wyzula-jan@users.noreply.github.com> Date: Fri, 23 Feb 2024 15:27:09 +0100 Subject: [PATCH] feat(widgets/figure): clear_all method for BECFigure --- bec_widgets/cli/client.py | 32 ++++++++++++++++++++++------ bec_widgets/widgets/figure/figure.py | 12 ++++++++++- setup.py | 9 +++++++- tests/test_bec_figure.py | 14 +++++++++++- tests/test_plot_base.py | 1 + 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/bec_widgets/cli/client.py b/bec_widgets/cli/client.py index 44494d9f..2b83eba1 100644 --- a/bec_widgets/cli/client.py +++ b/bec_widgets/cli/client.py @@ -62,19 +62,33 @@ class BECPlotBase(RPCBase): """ @rpc_call - def set_x_lim(self, x_lim: "tuple") -> "None": + def set_x_lim(self, *args) -> "None": """ - Set the limits of the x-axis. + Set the limits of the x-axis. This method can accept either two separate arguments + for the minimum and maximum x-axis values, or a single tuple containing both limits. + + Usage: + set_x_lim(x_min, x_max) + set_x_lim((x_min, x_max)) + Args: - x_lim(tuple): Limits of the x-axis. + *args: A variable number of arguments. Can be two integers (x_min and x_max) + or a single tuple with two integers. """ @rpc_call - def set_y_lim(self, y_lim: "tuple") -> "None": + def set_y_lim(self, *args) -> "None": """ - Set the limits of the y-axis. + Set the limits of the y-axis. This method can accept either two separate arguments + for the minimum and maximum y-axis values, or a single tuple containing both limits. + + Usage: + set_y_lim(y_min, y_max) + set_y_lim((y_min, y_max)) + Args: - y_lim(tuple): Limits of the y-axis. + *args: A variable number of arguments. Can be two integers (y_min and y_max) + or a single tuple with two integers. """ @rpc_call @@ -273,6 +287,12 @@ class BECFigure(RPCBase, BECFigureClientMixin): theme(Literal["dark","light"]): The theme to set for the figure widget. """ + @rpc_call + def clear_all(self): + """ + Clear all widgets from the figure and reset to default state + """ + class BECCurve(RPCBase): @rpc_call diff --git a/bec_widgets/widgets/figure/figure.py b/bec_widgets/widgets/figure/figure.py index 4747d3ee..8bf423d5 100644 --- a/bec_widgets/widgets/figure/figure.py +++ b/bec_widgets/widgets/figure/figure.py @@ -83,7 +83,7 @@ class WidgetHandler: class BECFigure(BECConnector, pg.GraphicsLayoutWidget): - USER_ACCESS = ["add_plot", "remove", "change_layout", "change_theme"] + USER_ACCESS = ["add_plot", "remove", "change_layout", "change_theme", "clear_all"] def __init__( self, @@ -381,6 +381,16 @@ class BECFigure(BECConnector, pg.GraphicsLayoutWidget): self._reindex_grid() # This method should be updated to handle reshuffling correctly self._replot_layout() # Assumes this method re-adds widgets to the layout based on self.grid + def clear_all(self): + """Clear all widgets from the figure and reset to default state""" + self.clear() + self.widgets = defaultdict(dict) + self.grid = [] + theme = self.config.theme + self.config = FigureConfig( + widget_class=self.__class__.__name__, gui_id=self.gui_id, theme=theme + ) + def start(self): import sys diff --git a/setup.py b/setup.py index bd749f0e..abbff0b2 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,14 @@ if __name__ == "__main__": "pyqtdarktheme", ], extras_require={ - "dev": ["pytest", "pytest-random-order", "coverage", "pytest-qt", "black"], + "dev": [ + "pytest", + "pytest-random-order", + "pytest-timeout", + "coverage", + "pytest-qt", + "black", + ], "pyqt5": ["PyQt5>=5.9"], "pyqt6": ["PyQt6>=6.0"], }, diff --git a/tests/test_bec_figure.py b/tests/test_bec_figure.py index cfcfae68..5e262a01 100644 --- a/tests/test_bec_figure.py +++ b/tests/test_bec_figure.py @@ -1,4 +1,4 @@ -# pylint: disable = no-name-in-module,missing-class-docstring, missing-module-docstring +# pylint: disable=missing-function-docstring, missing-module-docstring, unused-import import os import numpy as np @@ -212,3 +212,15 @@ def test_change_layout(bec_figure): assert bec_figure[0, 1] == w2 assert bec_figure[0, 2] == w3 assert bec_figure[0, 3] == w4 + + +def test_clear_all(bec_figure): + bec_figure.add_plot(widget_id="test_waveform_1", row=0, col=0) + bec_figure.add_plot(widget_id="test_waveform_2", row=0, col=1) + bec_figure.add_plot(widget_id="test_waveform_3", row=1, col=0) + bec_figure.add_plot(widget_id="test_waveform_4", row=1, col=1) + + bec_figure.clear_all() + + assert len(bec_figure.widgets) == 0 + assert np.shape(bec_figure.grid) == (0,) diff --git a/tests/test_plot_base.py b/tests/test_plot_base.py index fdad2d96..fc6db32e 100644 --- a/tests/test_plot_base.py +++ b/tests/test_plot_base.py @@ -1,3 +1,4 @@ +# pylint: disable=missing-function-docstring, missing-module-docstring, unused-import import pytest from .client_mocks import mocked_client from .test_bec_figure import bec_figure