summaryrefslogtreecommitdiff
path: root/silx/gui/plot/PrintPreviewToolButton.py
blob: d857c181350a1a43f37ad62eaea5bb046392ce9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""
This modules provides tool buttons to send the content of a plot to a
print preview page.
The plot content can then be moved on the page and resized prior to printing.

Classes
-------

- :class:`PrintPreviewToolButton`
- :class:`SingletonPrintPreviewToolButton`

Examples
--------

Simple example
++++++++++++++

.. code-block:: python

    from silx.gui import qt
    from silx.gui.plot import PlotWidget
    from silx.gui.plot.PrintPreviewToolButton import PrintPreviewToolButton
    import numpy

    app = qt.QApplication([])

    pw = PlotWidget()
    toolbar = qt.QToolBar(pw)
    toolbutton = PrintPreviewToolButton(parent=toolbar, plot=pw)
    pw.addToolBar(toolbar)
    toolbar.addWidget(toolbutton)
    pw.show()

    x = numpy.arange(1000)
    y = x / numpy.sin(x)
    pw.addCurve(x, y)

    app.exec_()

Singleton example
+++++++++++++++++

This example illustrates how to print the content of several different
plots on the same page. The plots all instantiate a
:class:`SingletonPrintPreviewToolButton`, which relies on a singleton widget
(:class:`silx.gui.widgets.PrintPreview.SingletonPrintPreviewDialog`).

.. image:: img/printPreviewMultiPlot.png

.. code-block:: python

    from silx.gui import qt
    from silx.gui.plot import PlotWidget
    from silx.gui.plot.PrintPreviewToolButton import SingletonPrintPreviewToolButton
    import numpy

    app = qt.QApplication([])

    plot_widgets = []

    for i in range(3):
        pw = PlotWidget()
        toolbar = qt.QToolBar(pw)
        toolbutton = SingletonPrintPreviewToolButton(parent=toolbar,
                                                     plot=pw)
        pw.addToolBar(toolbar)
        toolbar.addWidget(toolbutton)
        pw.show()
        plot_widgets.append(pw)

    x = numpy.arange(1000)

    plot_widgets[0].addCurve(x, numpy.sin(x * 2 * numpy.pi / 1000))
    plot_widgets[1].addCurve(x, numpy.cos(x * 2 * numpy.pi / 1000))
    plot_widgets[2].addCurve(x, numpy.tan(x * 2 * numpy.pi / 1000))

    app.exec_()

"""
from __future__ import absolute_import

import logging
from io import StringIO

from .. import qt
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__ = "20/12/2018"

_logger = logging.getLogger(__name__)
# _logger.setLevel(logging.DEBUG)


class PrintPreviewToolButton(qt.QToolButton):
    """QToolButton to open a :class:`PrintPreviewDialog` (if not already open)
    and add the current plot to its page to be printed.

    :param parent: See :class:`QAction`
    :param plot: :class:`.PlotWidget` instance on which to operate
    """
    def __init__(self, parent=None, plot=None):
        super(PrintPreviewToolButton, self).__init__(parent)

        if not isinstance(plot, PlotWidget):
            raise TypeError("plot parameter must be a PlotWidget")
        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'))
        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'))
        printPreviewAction.triggered.connect(self._plotToPrintPreview)

        menu = qt.QMenu(self)
        menu.addAction(printGeomAction)
        menu.addAction(printPreviewAction)
        self.setMenu(menu)
        self.setPopupMode(qt.QToolButton.InstantPopup)

        self._printPreviewDialog = None
        self._printConfigurationDialog = None

        self._printGeometry = {"xOffset": 0.1,
                               "yOffset": 0.1,
                               "width": 0.9,
                               "height": 0.9,
                               "units": "page",
                               "keepAspectRatio": True}

    @property
    def printPreviewDialog(self):
        """Lazy loaded :class:`PrintPreviewDialog`"""
        # if changes are made here, don't forget making them in
        # SingletonPrintPreviewToolButton.printPreviewDialog as well
        if self._printPreviewDialog is None:
            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,
                                               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())
            else:
                # PyQt5 and hopefully PyQt6+
                pixmap = self._plot.centralWidget().grab()
            self.printPreviewDialog.addPixmap(pixmap,
                                              title=self.getTitle(),
                                              comment=comment,
                                              commentPosition=commentPosition)
        self.printPreviewDialog.show()
        self.printPreviewDialog.raise_()

    def _getSvgRendererAndViewbox(self):
        """Return a SVG renderer displaying the plot and its viewbox
        (interactively specified by the user the first time this is called).

        The size of the renderer is adjusted to the printer configuration
        and to the geometry configuration (width, height, ratio) specified
        by the user."""
        imgData = StringIO()
        assert self._plot.saveGraph(imgData, fileFormat="svg"), \
            "Unable to save graph"
        imgData.flush()
        imgData.seek(0)
        svgData = imgData.read()

        svgRenderer = qt.QSvgRenderer()

        viewbox = self._getViewBox()

        svgRenderer.setViewBox(viewbox)

        xml_stream = qt.QXmlStreamReader(svgData.encode(errors="replace"))

        # This is for PyMca compatibility, to share a print preview with PyMca plots
        svgRenderer._viewBox = viewbox
        svgRenderer._svgRawData = svgData.encode(errors="replace")
        svgRenderer._svgRendererData = xml_stream

        if not svgRenderer.load(xml_stream):
            raise RuntimeError("Cannot interpret svg data")

        return svgRenderer, viewbox

    def _getViewBox(self):
        """
        """
        printer = self.printPreviewDialog.printer
        dpix = printer.logicalDpiX()
        dpiy = printer.logicalDpiY()
        availableWidth = printer.width()
        availableHeight = printer.height()

        config = self._printGeometry
        width = config['width']
        height = config['height']
        xOffset = config['xOffset']
        yOffset = config['yOffset']
        units = config['units']
        keepAspectRatio = config['keepAspectRatio']
        aspectRatio = self._getPlotAspectRatio()

        # convert the offsets to dots
        if units.lower() in ['inch', 'inches']:
            xOffset = xOffset * dpix
            yOffset = yOffset * dpiy
            if width is not None:
                width = width * dpix
            if height is not None:
                height = height * dpiy
        elif units.lower() in ['cm', 'centimeters']:
            xOffset = (xOffset / 2.54) * dpix
            yOffset = (yOffset / 2.54) * dpiy
            if width is not None:
                width = (width / 2.54) * dpix
            if height is not None:
                height = (height / 2.54) * dpiy
        else:
            # page units
            xOffset = availableWidth * xOffset
            yOffset = availableHeight * yOffset
            if width is not None:
                width = availableWidth * width
            if height is not None:
                height = availableHeight * height

        availableWidth -= xOffset
        availableHeight -= yOffset

        if width is not None:
            if (availableWidth + 0.1) < width:
                txt = "Available width  %f is less than requested width %f" % \
                      (availableWidth, width)
                raise ValueError(txt)
        if height is not None:
            if (availableHeight + 0.1) < height:
                txt = "Available height  %f is less than requested height %f" % \
                      (availableHeight, height)
                raise ValueError(txt)

        if keepAspectRatio:
            bodyWidth = width or availableWidth
            bodyHeight = bodyWidth * aspectRatio

            if bodyHeight > availableHeight:
                bodyHeight = availableHeight
                bodyWidth = bodyHeight / aspectRatio

        else:
            bodyWidth = width or availableWidth
            bodyHeight = height or availableHeight

        return qt.QRectF(xOffset,
                         yOffset,
                         bodyWidth,
                         bodyHeight)

    def _setPrintConfiguration(self):
        """Open a dialog to prompt the user to adjust print
        geometry parameters."""
        self.printPreviewDialog.ensurePrinterIsSet()
        if self._printConfigurationDialog is None:
            self._printConfigurationDialog = PrintGeometryDialog(self.parent())

        self._printConfigurationDialog.setPrintGeometry(self._printGeometry)
        if self._printConfigurationDialog.exec_():
            self._printGeometry = self._printConfigurationDialog.getPrintGeometry()

    def _getPlotAspectRatio(self):
        widget = self._plot.centralWidget()
        graphWidth = float(widget.width())
        graphHeight = float(widget.height())
        return graphHeight / graphWidth


class SingletonPrintPreviewToolButton(PrintPreviewToolButton):
    """This class is similar to its parent class :class:`PrintPreviewToolButton`
    but it uses a singleton print preview widget.

    This allows for several plots to send their content to the
    same print page, and for users to arrange them."""
    def __init__(self, parent=None, plot=None):
        PrintPreviewToolButton.__init__(self, parent, plot)

    @property
    def printPreviewDialog(self):
        if self._printPreviewDialog is None:
            self._printPreviewDialog = SingletonPrintPreviewDialog(self.parent())
        return self._printPreviewDialog


if __name__ == '__main__':
    import numpy
    app = qt.QApplication([])

    pw = PlotWidget()
    toolbar = qt.QToolBar(pw)
    toolbutton = PrintPreviewToolButton(parent=toolbar,
                                        plot=pw)
    pw.addToolBar(toolbar)
    toolbar.addWidget(toolbutton)
    pw.show()

    x = numpy.arange(1000)
    y = x / numpy.sin(x)
    pw.addCurve(x, y)

    app.exec_()