Skip to content

Commit

Permalink
Merge pull request #38580 from mantidproject/restore-lost-legend
Browse files Browse the repository at this point in the history
Restore lost legend when dragged out of figure
  • Loading branch information
peterfpeterson authored Jan 13, 2025
2 parents c7f74cc + 462310b commit 6584721
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/release/v6.12.0/Workbench/Bugfixes/38580.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Legends now automatically snap back to the default position if dragged off-screen.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6584721

Please sign in to comment.