|
29 | 29 |
|
30 | 30 | # mantid imports
|
31 | 31 | 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 |
33 | 33 | from mantid.plots.utility import zoom, MantidAxType, legend_set_draggable
|
34 | 34 | from mantidqt.plotting.figuretype import FigureType, figure_type
|
35 | 35 | from mantidqt.plotting.markers import SingleMarker
|
@@ -254,6 +254,30 @@ def on_mouse_button_release(self, event):
|
254 | 254 | self.marker_selected_in_double_click_event = None
|
255 | 255 | self.double_click_event = None
|
256 | 256 |
|
| 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 | + |
257 | 281 | def on_leave(self, event):
|
258 | 282 | """
|
259 | 283 | When leaving the axis or canvas, restore cursor to default one
|
|
0 commit comments