diff --git a/src/sas/qtgui/Plotting/Plotter.py b/src/sas/qtgui/Plotting/Plotter.py index af7a81ce92..dda2970a8b 100644 --- a/src/sas/qtgui/Plotting/Plotter.py +++ b/src/sas/qtgui/Plotting/Plotter.py @@ -271,11 +271,15 @@ def createContextMenu(self): self.actionAddText = self.contextMenu.addAction("Add Text") self.actionRemoveText = self.contextMenu.addAction("Remove Text") self.contextMenu.addSeparator() + if self.show_legend: + self.actionToggleLegend = self.contextMenu.addAction("Toggle Legend") + self.contextMenu.addSeparator() self.actionChangeScale = self.contextMenu.addAction("Change Scale") self.contextMenu.addSeparator() self.actionSetGraphRange = self.contextMenu.addAction("Set Graph Range") self.actionResetGraphRange =\ self.contextMenu.addAction("Reset Graph Range") + # Add the title change for dialogs self.contextMenu.addSeparator() self.actionWindowTitle = self.contextMenu.addAction("Window Title") @@ -290,6 +294,8 @@ def createContextMenu(self): self.actionResetGraphRange.triggered.connect(self.onResetGraphRange) self.actionWindowTitle.triggered.connect(self.onWindowsTitle) self.actionToggleMenu.triggered.connect(self.onToggleMenu) + if self.show_legend: + self.actionToggleLegend.triggered.connect(self.onToggleLegend) def addPlotsToContextMenu(self): """ @@ -357,6 +363,10 @@ def createContextMenuQuick(self): self.actionToggleGrid.triggered.connect(self.onGridToggle) self.actionChangeScale.triggered.connect(self.onScaleChange) + if self.show_legend: + self.actionToggleLegend = self.contextMenu.addAction("Toggle Legend") + self.actionToggleLegend.triggered.connect(self.onToggleLegend) + def onScaleChange(self): """ Show a dialog allowing axes rescaling @@ -641,6 +651,17 @@ def onFitDisplay(self, fit_data): # Plot the line self.plot(data=self.fit_result, marker='-', hide_error=True) + def onToggleLegend(self): + """ + Toggle legend visibility in the chart + """ + if not self.showLegend: + return + + visible = self.legend.get_visible() + self.legend.set_visible(not visible) + self.canvas.draw_idle() + def onMplMouseDown(self, event): """ Left button down and ready to drag diff --git a/src/sas/qtgui/Plotting/UnitTesting/PlotterTest.py b/src/sas/qtgui/Plotting/UnitTesting/PlotterTest.py index 93819c69cb..3566fcf27c 100644 --- a/src/sas/qtgui/Plotting/UnitTesting/PlotterTest.py +++ b/src/sas/qtgui/Plotting/UnitTesting/PlotterTest.py @@ -103,7 +103,7 @@ def testCreateContextMenuQuick(self): """ Test the right click menu """ self.plotter.createContextMenuQuick() actions = self.plotter.contextMenu.actions() - self.assertEqual(len(actions), 7) + self.assertEqual(len(actions), 8) # Trigger Print Image and make sure the method is called self.assertEqual(actions[1].text(), "Print Image") @@ -444,6 +444,48 @@ def notestOnModifyPlot(self): self.plotter.onModifyPlot(2) self.plotter.figure.clf() + def testOnToggleLegend(self): + """ + Make sure Legend can be switched on/off + """ + # Prepare new data + data2 = Data1D(x=[1.0, 2.0, 3.0], + y=[11.0, 12.0, 13.0], + dx=[0.1, 0.2, 0.3], + dy=[0.1, 0.2, 0.3]) + data2.title="Test data 2" + data2.name="Test name 2" + data2.id = 2 + data2.custom_color = None + data2.symbol = 1 + data2.markersize = 11 + + self.plotter.plot(data2) + + self.assertTrue(self.plotter.showLegend) + # assure we see the legend + self.assertTrue(self.plotter.legend.get_visible()) + + # toggle legend + self.plotter.onToggleLegend() + + # now we don't see the legend + self.assertFalse(self.plotter.legend.get_visible()) + + # toggle again + self.plotter.onToggleLegend() + + # see the legend again + self.assertTrue(self.plotter.legend.get_visible()) + + # switch the visibility of the legend + self.plotter.showLegend = False + + # see that the legend setting is not done + self.plotter.legend.set_visible = MagicMock() + + self.plotter.onToggleLegend() + self.plotter.legend.set_visible.assert_not_called() if __name__ == "__main__": unittest.main()