From 972d985421af2306c819017add06eff12c51abf7 Mon Sep 17 00:00:00 2001 From: Gabriele Buondonno Date: Fri, 16 Oct 2020 14:57:48 +0200 Subject: [PATCH] [SVGExporter] Fix background color --- pyqtgraph/exporters/SVGExporter.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pyqtgraph/exporters/SVGExporter.py b/pyqtgraph/exporters/SVGExporter.py index 84af1a19..751a10c1 100644 --- a/pyqtgraph/exporters/SVGExporter.py +++ b/pyqtgraph/exporters/SVGExporter.py @@ -18,7 +18,18 @@ class SVGExporter(Exporter): def __init__(self, item): Exporter.__init__(self, item) tr = self.getTargetRect() + + if isinstance(item, QtGui.QGraphicsItem): + scene = item.scene() + else: + scene = item + bgbrush = scene.views()[0].backgroundBrush() + bg = bgbrush.color() + if bgbrush.style() == QtCore.Qt.NoBrush: + bg.setAlpha(0) + self.params = Parameter(name='params', type='group', children=[ + {'name': 'background', 'type': 'color', 'value': bg}, {'name': 'width', 'type': 'float', 'value': tr.width(), 'limits': (0, None)}, {'name': 'height', 'type': 'float', 'value': tr.height(), 'limits': (0, None)}, #{'name': 'viewbox clipping', 'type': 'bool', 'value': True}, @@ -51,6 +62,7 @@ class SVGExporter(Exporter): ## Instead, we will use Qt to generate SVG for each item independently, ## then manually reconstruct the entire document. options = {ch.name():ch.value() for ch in self.params.children()} + options['background'] = self.params['background'] options['width'] = self.params['width'] options['height'] = self.params['height'] xml = generateSvg(self.item, options) @@ -103,7 +115,9 @@ def generateSvg(item, options={}): defsXml += d.toprettyxml(indent=' ') defsXml += "\n" svgAttributes = f' viewBox ="0 0 {options["width"]} {options["height"]}"' - return (xmlHeader % svgAttributes) + defsXml + node.toprettyxml(indent=' ') + "\n\n" + c = options['background'] + backgroundtag = f'\n' + return (xmlHeader % svgAttributes) + backgroundtag + defsXml + node.toprettyxml(indent=' ') + "\n\n" def _generateItemSvg(item, nodes=None, root=None, options={}):