Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracking between axis title, window title, and plot name #36393

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Editing a plot's title will now automatically update its name in the plot selector (and vice versa).
25 changes: 25 additions & 0 deletions qt/applications/workbench/workbench/plotting/figuremanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,30 @@ def show(self):

# Hack to ensure the canvas is up to date
self.canvas.draw_idle()
self._set_up_axes_title_change_callbacks()

if self.toolbar:
self.toolbar.set_buttons_visibility(self.canvas.figure)

def _set_up_axes_title_change_callbacks(self):
# This enables us to use the plot tile as the window title
plot_axes = self._axes_that_are_not_colour_bars()
if len(plot_axes) == 1:
# only set this up for single plots
plot_axes[0].title.add_callback(self._axes_title_changed_callback)

def _axes_title_changed_callback(self, title_artist):
title_text = title_artist.get_text()
if not title_text:
return
title_text_with_number = title_text + "-" + str(self.num)
current_title_text = self.get_window_title()
if current_title_text not in {title_text_with_number, title_text}:
self.set_window_title(title_text)

def _axes_that_are_not_colour_bars(self):
return [ax for ax in self.canvas.figure.get_axes() if not hasattr(ax, "_colorbar")]

def destroy(self, *args):
# check for qApp first, as PySide deletes it in its atexit handler
if QApplication.instance() is None:
Expand Down Expand Up @@ -440,6 +460,11 @@ def set_window_title(self, title):
# to allow getting a handle as plt.figure('Figure Name')
self.canvas.figure.set_label(title)

def set_axes_title(self, title):
plot_axes = self._axes_that_are_not_colour_bars()
if len(plot_axes) == 1:
plot_axes[0].set_title(title)

def fig_visibility_changed(self):
"""
Make a notification in the global figure manager that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,65 @@ def test_window_title(self, mock_qappthread):
fig_mgr = FigureManagerWorkbench(canvas, 1)
self.assertEqual(fig_mgr.get_window_title(), "Figure 1")

@patch("workbench.plotting.figuremanager.QAppThreadCall")
def test_ax_callback_set_up(self, mock_qappthread):
mock_qappthread.return_value = mock_qappthread
fig = MagicMock()
fig.bbox.max = [1, 1]
canvas = MantidFigureCanvas(fig)
fig_mgr = FigureManagerWorkbench(canvas, 1)
with (
patch("workbench.plotting.figuremanager.FigureManagerWorkbench._axes_that_are_not_colour_bars") as plot_axes_mock,
patch("workbench.plotting.figuremanager.FigureManagerWorkbench._axes_title_changed_callback") as callback_mock,
):
ax = MagicMock()
plot_axes_mock.return_value = [ax]
fig_mgr.show()
ax.title.add_callback.assert_called_once_with(callback_mock)

@patch("workbench.plotting.figuremanager.QAppThreadCall")
def test_ax_callback_will_set_window_title_with_new_title(self, mock_qappthread):
mock_qappthread.return_value = mock_qappthread
fig = MagicMock()
fig.bbox.max = [1, 1]
canvas = MantidFigureCanvas(fig)
fig_mgr = FigureManagerWorkbench(canvas, 1)
mock_title_artist = MagicMock()
mock_title_artist.get_text.return_value = "new title"
with patch("workbench.plotting.figuremanager.FigureManagerWorkbench.set_window_title") as set_window_title_mock:
fig_mgr._axes_title_changed_callback(mock_title_artist)
set_window_title_mock.assert_called_once_with("new title")

@patch("workbench.plotting.figuremanager.QAppThreadCall")
def test_ax_callback_will_not_set_window_title_with_current_title(self, mock_qappthread):
mock_qappthread.return_value = mock_qappthread
fig = MagicMock()
fig.bbox.max = [1, 1]
canvas = MantidFigureCanvas(fig)
fig_mgr = FigureManagerWorkbench(canvas, 1)
mock_title_artist = MagicMock()
mock_title_artist.get_text.return_value = "Figure 1"
with patch("workbench.plotting.figuremanager.FigureManagerWorkbench.set_window_title") as set_window_title_mock:
fig_mgr._axes_title_changed_callback(mock_title_artist)
set_window_title_mock.assert_not_called()

@patch("workbench.plotting.figuremanager.QAppThreadCall")
def test_ax_callback_will_not_set_window_title_with_current_title_without_number(self, mock_qappthread):
mock_qappthread.return_value = mock_qappthread
fig = MagicMock()
fig.bbox.max = [1, 1]
canvas = MantidFigureCanvas(fig)
fig_mgr = FigureManagerWorkbench(canvas, 1)
mock_title_artist = MagicMock()
mock_title_artist.get_text.return_value = "EMU00000"
with (
patch("workbench.plotting.figuremanager.FigureManagerWorkbench.set_window_title") as set_window_title_mock,
patch("workbench.plotting.figuremanager.FigureManagerWorkbench.get_window_title") as get_window_title_mock,
):
get_window_title_mock.return_value = "EMU00000-1"
fig_mgr._axes_title_changed_callback(mock_title_artist)
set_window_title_mock.assert_not_called()

def test_reverse_axes_lines(self):
fig, ax = plt.subplots(subplot_kw={"projection": "mantid"})
spec2 = ax.plot(self.ws2d_histo, "rs", specNum=2, linewidth=6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def rename_figure(self, plot_number, new_name):
raise ValueError("Error renaming, could not find a plot with the number {}.".format(plot_number))

figure_manager.set_window_title(new_name)
figure_manager.set_axes_title(new_name)

# ------------------------ Plot Closing -------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,16 @@ def test_renaming_calls_set_window_title(self):
self.model.rename_figure(42, "NewName")
self.figure_manager.set_window_title.assert_called_once_with("NewName")

def test_renaming_calls_set_axis_title(self):
self.model.rename_figure(42, "NewName")
self.figure_manager.set_axes_title.assert_called_once_with("NewName")

def test_renaming_calls_with_invalid_number_raises_value_error(self):
self.assertRaises(ValueError, self.model.rename_figure, 0, "NewName")
self.assertRaisesRegex(
ValueError, "Error renaming, could not find a plot with the number 0", self.model.rename_figure, 0, "NewName"
)
self.figure_manager.set_window_title.assert_not_called()
self.figure_manager.set_axes_title.assert_not_called()

# ------------------------ Plot Closing -------------------------

Expand Down