def export_to_svg(self, filename: Path) -> bool: generator = QSvgGenerator() generator.setFileName(str(filename)) generator.setSize(self.size()) generator.setViewBox(self.rect()) self.render(generator) return True
def export(self, filename, w=1920, h=1080): """ Save the grid to a png file """ name, extension = os.path.splitext(filename.lower()) if extension == '.png': image = QImage(w, h, QImage.Format_ARGB32_Premultiplied) image.fill(Qt.transparent) painter = QPainter(image) painter.setRenderHint(QPainter.Antialiasing) self.diagramScene.render(painter) image.save(filename) painter.end() elif extension == '.svg': svg_gen = QSvgGenerator() svg_gen.setFileName(filename) svg_gen.setSize(QSize(w, h)) svg_gen.setViewBox(QRect(0, 0, w, h)) svg_gen.setTitle("Electrical grid schematic") svg_gen.setDescription("An SVG drawing created by GridCal") painter = QPainter(svg_gen) self.diagramScene.render(painter) painter.end() else: raise Exception('Extension ' + str(extension) + ' not supported :(')
def from_scene(cls, scene): buffer = QBuffer() generator = QSvgGenerator() generator.setOutputDevice(buffer) scene_rect = scene.sceneRect() generator.setViewBox(scene_rect) painter = QPainter(generator) scene.render(painter, scene_rect, scene_rect) painter.end() buffer.open(QBuffer.ReadOnly) renderer = cls(buffer.readAll()) buffer.close() renderer.scene = scene return renderer