Skip to content

Commit

Permalink
Added context menu item for legend toggle, for quick and regular 1D
Browse files Browse the repository at this point in the history
plots. #1523
  • Loading branch information
rozyczko committed May 29, 2020
1 parent a07b1cc commit 2993813
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/sas/qtgui/Plotting/Plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
44 changes: 43 additions & 1 deletion src/sas/qtgui/Plotting/UnitTesting/PlotterTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()

0 comments on commit 2993813

Please # to comment.