Skip to content

Commit 6584721

Browse files
Merge pull request #38580 from mantidproject/restore-lost-legend
Restore lost legend when dragged out of figure
2 parents c7f74cc + 462310b commit 6584721

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Legends now automatically snap back to the default position if dragged off-screen.

qt/applications/workbench/workbench/plotting/figureinteraction.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# mantid imports
3131
from mantid.api import AnalysisDataService as ads
32-
from mantid.plots import datafunctions, MantidAxes, axesfunctions, MantidAxes3D
32+
from mantid.plots import datafunctions, MantidAxes, axesfunctions, MantidAxes3D, LegendProperties
3333
from mantid.plots.utility import zoom, MantidAxType, legend_set_draggable
3434
from mantidqt.plotting.figuretype import FigureType, figure_type
3535
from mantidqt.plotting.markers import SingleMarker
@@ -254,6 +254,30 @@ def on_mouse_button_release(self, event):
254254
self.marker_selected_in_double_click_event = None
255255
self.double_click_event = None
256256

257+
self.legend_bounds_check(event)
258+
259+
@staticmethod
260+
def legend_bounds_check(event):
261+
fig = event.canvas.figure
262+
263+
for ax in fig.get_axes():
264+
legend1 = ax.get_legend()
265+
if legend1 is None:
266+
continue
267+
268+
bbox_fig = fig.get_window_extent()
269+
bbox_legend = legend1.get_window_extent()
270+
271+
outside_window = (
272+
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
273+
)
274+
275+
# Snap back legend
276+
if outside_window:
277+
props = LegendProperties.from_legend(legend1)
278+
LegendProperties.create_legend(props, legend1.axes)
279+
fig.canvas.draw_idle()
280+
257281
def on_leave(self, event):
258282
"""
259283
When leaving the axis or canvas, restore cursor to default one

0 commit comments

Comments
 (0)