+
+ plot = Plot(view, xaxis, yaxis)
+ self._plots.append(plot)
+
+ self.showPlot(plot)
+
+ # Create a separate action object for each plots context menu, so that
+ # we can later find out which plot should be hidden by looking at
+ # 'self._hideActions'.
+ hideAction = QtGui.QAction('Hide', self)
+ hideAction.triggered.connect(self._onHideActionTriggered)
+ self._hideActions[id(hideAction)] = plot
+ view.menu.insertAction(view.menu.actions()[0], hideAction)
+
+ return plot
+
+ def _rowNumber(self, plot):
+ '''Returns the number of the first row a plot occupies.'''
+
+ # Every plot takes up two rows
+ return 2 * self._plots.index(plot)
+
+ @QtCore.Slot()
+ def _onHideActionTriggered(self, checked=False):
+ # The plot that we want to hide.
+ plot = self._hideActions[id(self.sender())]
+ self.hidePlot(plot)
+
+ def hidePlot(self, plot):
+ '''Hides 'plot'.'''
+
+ # Only hiding wouldn't give up the space occupied by the items,
+ # we have to remove them from the layout.
+ self.layout().removeItem(plot.view)
+ self.layout().removeItem(plot.xaxis)
+ self.layout().removeItem(plot.yaxis)
+
+ plot.view.hide()
+ plot.xaxis.hide()
+ plot.yaxis.hide()
+
+ row = self._rowNumber(plot)
+ self.layout().setRowStretchFactor(row, 0)
+ self.layout().setRowStretchFactor(row + 1, 0)
+
+ plot.visible = False
+ self.plotHidden.emit(plot)
+
+ def showPlot(self, plot):
+ '''Adds the items of the plot to the scene's layout and makes
+ them visible.'''
+
+ if plot.visible:
+ return
+
+ row = self._rowNumber(plot)
+ self.layout().addItem(plot.yaxis, row, 0, QtCore.Qt.AlignRight)
+ self.layout().addItem(plot.view, row, 1)
+ self.layout().addItem(plot.xaxis, row + 1, 1)
+
+ plot.view.show()
+ plot.xaxis.show()
+ plot.yaxis.show()