|
8 | 8 |
|
9 | 9 | import unittest
|
10 | 10 | from unittest.mock import patch
|
| 11 | +import numpy as np |
11 | 12 |
|
12 | 13 | import matplotlib
|
13 | 14 |
|
|
19 | 20 | from workbench.plotting.figuremanager import MantidFigureCanvas, FigureManagerWorkbench
|
20 | 21 | from workbench.plotting.toolbar import WorkbenchNavigationToolbar
|
21 | 22 | from mantid.plots.plotfunctions import plot
|
22 |
| -from mantid.simpleapi import CreateSampleWorkspace, CreateMDHistoWorkspace |
| 23 | +from mantid.simpleapi import CreateSampleWorkspace, CreateMDHistoWorkspace, Load |
23 | 24 |
|
24 | 25 |
|
25 | 26 | @start_qapplication
|
@@ -117,6 +118,39 @@ def test_button_checked_for_plot_with_grid(self, mock_qappthread):
|
117 | 118 | # Grid button should be ON because we enabled the grid.
|
118 | 119 | self.assertTrue(self._is_grid_button_checked(fig))
|
119 | 120 |
|
| 121 | + @patch("workbench.plotting.figuremanager.QAppThreadCall") |
| 122 | + def test_button_checked_for_plot_with_no_crosshair(self, mock_qappthread): |
| 123 | + mock_qappthread.return_value = mock_qappthread |
| 124 | + |
| 125 | + fig, axes = plt.subplots(subplot_kw={"projection": "mantid"}) |
| 126 | + axes.plot([-10, 10], [1, 2]) |
| 127 | + # crosshair button should be OFF because we have not enabled the crosshair. |
| 128 | + self.assertFalse(self._is_crosshair_button_checked(fig)) |
| 129 | + # crosshair button should be visible when there is only 1 axis |
| 130 | + self.assertTrue(self._is_crosshair_button_visible(fig)) |
| 131 | + |
| 132 | + @patch("workbench.plotting.figuremanager.QAppThreadCall") |
| 133 | + def test_button_hiden_for_tiled_plots(self, mock_qappthread): |
| 134 | + mock_qappthread.return_value = mock_qappthread |
| 135 | + |
| 136 | + fig, axes = plt.subplots(2) |
| 137 | + axes[0].plot([-10, 10], [1, 2]) |
| 138 | + axes[1].plot([3, 2, 1], [1, 2, 3]) |
| 139 | + # crosshair button should be hidden because this is a tiled plot |
| 140 | + self.assertFalse(self._is_crosshair_button_visible(fig)) |
| 141 | + self.assertFalse(self._is_crosshair_button_checked(fig)) |
| 142 | + |
| 143 | + @patch("workbench.plotting.figuremanager.QAppThreadCall") |
| 144 | + def test_button_enabled_for_contour_plots(self, mock_qappthread): |
| 145 | + mock_qappthread.return_value = mock_qappthread |
| 146 | + data = Load("SANSLOQCan2D.nxs") |
| 147 | + fig, axes = plt.subplots(subplot_kw={"projection": "mantid"}) |
| 148 | + axes.imshow(data, origin="lower", cmap="viridis", aspect="auto") |
| 149 | + axes.contour(data, levels=np.linspace(10, 60, 6), colors="yellow", alpha=0.5) |
| 150 | + # crosshair button should be visible because this is a contour plot |
| 151 | + self.assertTrue(self._is_crosshair_button_visible(fig)) |
| 152 | + self.assertFalse(self._is_crosshair_button_checked(fig)) |
| 153 | + |
120 | 154 | @patch("workbench.plotting.figuremanager.QAppThreadCall")
|
121 | 155 | def test_button_checked_for_plot_with_grid_using_kwargs(self, mock_qappthread):
|
122 | 156 | mock_qappthread.return_value = mock_qappthread
|
@@ -266,6 +300,32 @@ def _is_button_enabled(cls, fig, button):
|
266 | 300 | fig_manager.toolbar.set_buttons_visibility(fig)
|
267 | 301 | return fig_manager.toolbar._actions[button].isEnabled()
|
268 | 302 |
|
| 303 | + @classmethod |
| 304 | + def _is_crosshair_button_checked(cls, fig): |
| 305 | + """ |
| 306 | + Create the figure manager and check whether its toolbar is toggled on or off for the given figure. |
| 307 | + We have to explicitly call set_button_visibility() here, which would otherwise be called within the show() |
| 308 | + function. |
| 309 | + """ |
| 310 | + canvas = MantidFigureCanvas(fig) |
| 311 | + fig_manager = FigureManagerWorkbench(canvas, 1) |
| 312 | + # This is only called when show() is called on the figure manager, so we have to manually call it here. |
| 313 | + fig_manager.toolbar.set_buttons_visibility(fig) |
| 314 | + return fig_manager.toolbar._actions["toggle_crosshair"].isChecked() |
| 315 | + |
| 316 | + @classmethod |
| 317 | + def _is_crosshair_button_visible(cls, fig): |
| 318 | + """ |
| 319 | + Create the figure manager and check whether its toolbar is visible for the given figure. |
| 320 | + We have to explicitly call set_button_visibility() here, which would otherwise be called within the show() |
| 321 | + function. |
| 322 | + """ |
| 323 | + canvas = MantidFigureCanvas(fig) |
| 324 | + fig_manager = FigureManagerWorkbench(canvas, 1) |
| 325 | + # This is only called when show() is called on the figure manager, so we have to manually call it here. |
| 326 | + fig_manager.toolbar.set_buttons_visibility(fig) |
| 327 | + return fig_manager.toolbar._actions["toggle_crosshair"].isVisible() |
| 328 | + |
269 | 329 |
|
270 | 330 | if __name__ == "__main__":
|
271 | 331 | unittest.main()
|
0 commit comments