diff options
Diffstat (limited to 'silx/gui/plot/PrintPreviewToolButton.py')
-rw-r--r-- | silx/gui/plot/PrintPreviewToolButton.py | 61 |
1 files changed, 51 insertions, 10 deletions
diff --git a/silx/gui/plot/PrintPreviewToolButton.py b/silx/gui/plot/PrintPreviewToolButton.py index b48505d..d857c18 100644 --- a/silx/gui/plot/PrintPreviewToolButton.py +++ b/silx/gui/plot/PrintPreviewToolButton.py @@ -111,10 +111,11 @@ from .. import icons from . import PlotWidget from ..widgets.PrintPreview import PrintPreviewDialog, SingletonPrintPreviewDialog from ..widgets.PrintGeometryDialog import PrintGeometryDialog +from silx.utils.deprecation import deprecated __authors__ = ["P. Knobel"] __license__ = "MIT" -__date__ = "18/07/2017" +__date__ = "20/12/2018" _logger = logging.getLogger(__name__) # _logger.setLevel(logging.DEBUG) @@ -132,19 +133,19 @@ class PrintPreviewToolButton(qt.QToolButton): if not isinstance(plot, PlotWidget): raise TypeError("plot parameter must be a PlotWidget") - self.plot = plot + self._plot = plot self.setIcon(icons.getQIcon('document-print')) printGeomAction = qt.QAction("Print geometry", self) printGeomAction.setToolTip("Define a print geometry prior to sending " "the plot to the print preview dialog") - printGeomAction.setIcon(icons.getQIcon('shape-rectangle')) # fixme: icon not displayed in menu + printGeomAction.setIcon(icons.getQIcon('shape-rectangle')) printGeomAction.triggered.connect(self._setPrintConfiguration) printPreviewAction = qt.QAction("Print preview", self) printPreviewAction.setToolTip("Send plot to the print preview dialog") - printPreviewAction.setIcon(icons.getQIcon('document-print')) # fixme: icon not displayed + printPreviewAction.setIcon(icons.getQIcon('document-print')) printPreviewAction.triggered.connect(self._plotToPrintPreview) menu = qt.QMenu(self) @@ -172,24 +173,64 @@ class PrintPreviewToolButton(qt.QToolButton): self._printPreviewDialog = PrintPreviewDialog(self.parent()) return self._printPreviewDialog + def getTitle(self): + """Implement this method to fetch the title in the plot. + + :return: Title to be printed above the plot, or None (no title added) + :rtype: str or None + """ + return None + + def getCommentAndPosition(self): + """Implement this method to fetch the legend to be printed below the + figure and its position. + + :return: Legend to be printed below the figure and its position: + "CENTER", "LEFT" or "RIGHT" + :rtype: (str, str) or (None, None) + """ + return None, None + + @property + @deprecated(since_version="0.10", + replacement="getPlot()") + def plot(self): + return self._plot + + def getPlot(self): + """Return the :class:`.PlotWidget` associated with this tool button. + + :rtype: :class:`.PlotWidget` + """ + return self._plot + def _plotToPrintPreview(self): """Grab the plot widget and send it to the print preview dialog. Make sure the print preview dialog is shown and raised.""" if not self.printPreviewDialog.ensurePrinterIsSet(): return + comment, commentPosition = self.getCommentAndPosition() + if qt.HAS_SVG: svgRenderer, viewBox = self._getSvgRendererAndViewbox() self.printPreviewDialog.addSvgItem(svgRenderer, - viewBox=viewBox) + title=self.getTitle(), + comment=comment, + commentPosition=commentPosition, + viewBox=viewBox, + keepRatio=self._printGeometry["keepAspectRatio"]) else: _logger.warning("Missing QtSvg library, using a raster image") if qt.BINDING in ["PyQt4", "PySide"]: - pixmap = qt.QPixmap.grabWidget(self.plot.centralWidget()) + pixmap = qt.QPixmap.grabWidget(self._plot.centralWidget()) else: # PyQt5 and hopefully PyQt6+ - pixmap = self.plot.centralWidget().grab() - self.printPreviewDialog.addPixmap(pixmap) + pixmap = self._plot.centralWidget().grab() + self.printPreviewDialog.addPixmap(pixmap, + title=self.getTitle(), + comment=comment, + commentPosition=commentPosition) self.printPreviewDialog.show() self.printPreviewDialog.raise_() @@ -201,7 +242,7 @@ class PrintPreviewToolButton(qt.QToolButton): and to the geometry configuration (width, height, ratio) specified by the user.""" imgData = StringIO() - assert self.plot.saveGraph(imgData, fileFormat="svg"), \ + assert self._plot.saveGraph(imgData, fileFormat="svg"), \ "Unable to save graph" imgData.flush() imgData.seek(0) @@ -310,7 +351,7 @@ class PrintPreviewToolButton(qt.QToolButton): self._printGeometry = self._printConfigurationDialog.getPrintGeometry() def _getPlotAspectRatio(self): - widget = self.plot.centralWidget() + widget = self._plot.centralWidget() graphWidth = float(widget.width()) graphHeight = float(widget.height()) return graphHeight / graphWidth |