From 5d6672069ea1cbceb62104f66c127e4e3c23e4a4 Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Wed, 19 Jun 2024 11:43:44 +0200 Subject: [PATCH] fix(plot_base): font size is set with setScale which is scaling the whole legend window --- bec_widgets/widgets/figure/plots/plot_base.py | 14 ++++---------- tests/unit_tests/test_waveform1d.py | 13 ++++++------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/bec_widgets/widgets/figure/plots/plot_base.py b/bec_widgets/widgets/figure/plots/plot_base.py index a646f184..abd35787 100644 --- a/bec_widgets/widgets/figure/plots/plot_base.py +++ b/bec_widgets/widgets/figure/plots/plot_base.py @@ -139,16 +139,10 @@ class BECPlotBase(BECConnector, pg.GraphicsLayout): if self.config.axis.legend_label_size or size: if size: self.config.axis.legend_label_size = size - style = { - "color": self.get_text_color(), - "size": f"{self.config.axis.legend_label_size}pt", - } - else: - style = {} - for item in self.plot_item.legend.items: - for single_item in item: - if isinstance(single_item, pg.graphicsItems.LabelItem.LabelItem): - single_item.setText(single_item.text, **style) + scale = ( + size / 9 + ) # 9 is the default font size of the legend, so we always scale it against 9 + self.plot_item.legend.setScale(scale) def get_text_color(self): return "#FFF" if self.figure.config.theme == "dark" else "#000" diff --git a/tests/unit_tests/test_waveform1d.py b/tests/unit_tests/test_waveform1d.py index 32b93cc5..6a461b16 100644 --- a/tests/unit_tests/test_waveform1d.py +++ b/tests/unit_tests/test_waveform1d.py @@ -201,13 +201,12 @@ def test_change_legend_font_size(bec_figure): plot = bec_figure.add_plot() w1 = plot.add_curve_scan(x_name="samx", y_name="bpm4i") - my_func = plot.plot_item.legend.items[0][1] - with mock.patch.object(my_func, "setText") as mock_set_text: - plot.set_legend_label_size(16) - assert plot.config.axis.legend_label_size == 16 - assert mock_set_text.call_count == 1 - style = {"color": plot.get_text_color(), "size": "16pt"} - assert mock_set_text.call_args == mock.call(my_func.text, **style) + my_func = plot.plot_item.legend + with mock.patch.object(my_func, "setScale") as mock_set_scale: + plot.set_legend_label_size(18) + assert plot.config.axis.legend_label_size == 18 + assert mock_set_scale.call_count == 1 + assert mock_set_scale.call_args == mock.call(2) def test_remove_curve(bec_figure):