diff --git a/docs/source/release/v6.12.0/Workbench/Bugfixes/38580.rst b/docs/source/release/v6.12.0/Workbench/Bugfixes/38580.rst new file mode 100644 index 000000000000..9f0e2ee683c7 --- /dev/null +++ b/docs/source/release/v6.12.0/Workbench/Bugfixes/38580.rst @@ -0,0 +1 @@ +- Legends now automatically snap back to the default position if dragged off-screen. diff --git a/qt/applications/workbench/workbench/plotting/figureinteraction.py b/qt/applications/workbench/workbench/plotting/figureinteraction.py index 020dc6f3578a..98925342ea50 100644 --- a/qt/applications/workbench/workbench/plotting/figureinteraction.py +++ b/qt/applications/workbench/workbench/plotting/figureinteraction.py @@ -29,7 +29,7 @@ # mantid imports from mantid.api import AnalysisDataService as ads -from mantid.plots import datafunctions, MantidAxes, axesfunctions, MantidAxes3D +from mantid.plots import datafunctions, MantidAxes, axesfunctions, MantidAxes3D, LegendProperties from mantid.plots.utility import zoom, MantidAxType, legend_set_draggable from mantidqt.plotting.figuretype import FigureType, figure_type from mantidqt.plotting.markers import SingleMarker @@ -254,6 +254,30 @@ def on_mouse_button_release(self, event): self.marker_selected_in_double_click_event = None self.double_click_event = None + self.legend_bounds_check(event) + + @staticmethod + def legend_bounds_check(event): + fig = event.canvas.figure + + for ax in fig.get_axes(): + legend1 = ax.get_legend() + if legend1 is None: + continue + + bbox_fig = fig.get_window_extent() + bbox_legend = legend1.get_window_extent() + + outside_window = ( + bbox_legend.x1 < bbox_fig.x0 or bbox_legend.x0 > bbox_fig.x1 or bbox_legend.y1 < bbox_fig.y0 or bbox_legend.y0 > bbox_fig.y1 + ) + + # Snap back legend + if outside_window: + props = LegendProperties.from_legend(legend1) + LegendProperties.create_legend(props, legend1.axes) + fig.canvas.draw_idle() + def on_leave(self, event): """ When leaving the axis or canvas, restore cursor to default one