Пример #1
0
    def exportAsSvg(self):
        self.repaintModel(controls=False)
        filename, fileFilter = QFileDialog.getSaveFileName(self,
                                                           self.tr('Save Model As SVG'), '',
                                                           self.tr('SVG files (*.svg *.SVG)'))
        if not filename:
            return

        if not filename.lower().endswith('.svg'):
            filename += '.svg'

        totalRect = self.scene.itemsBoundingRect()
        totalRect.adjust(-10, -10, 10, 10)
        svgRect = QRectF(0, 0, totalRect.width(), totalRect.height())

        svg = QSvgGenerator()
        svg.setFileName(filename)
        svg.setSize(QSize(totalRect.width(), totalRect.height()))
        svg.setViewBox(svgRect)
        svg.setTitle(self.model.displayName())

        painter = QPainter(svg)
        self.scene.render(painter, svgRect, totalRect)
        painter.end()

        self.bar.pushMessage("", self.tr("Model was correctly exported as SVG"), level=Qgis.Success, duration=5)
        self.repaintModel(controls=True)
Пример #2
0
    def exportAsSvg(self):
        self.repaintModel(controls=False)
        filename, fileFilter = QFileDialog.getSaveFileName(self,
                                                           self.tr('Save Model As SVG'), '',
                                                           self.tr('SVG files (*.svg *.SVG)'))
        if not filename:
            return

        if not filename.lower().endswith('.svg'):
            filename += '.svg'

        totalRect = self.scene.itemsBoundingRect()
        totalRect.adjust(-10, -10, 10, 10)
        svgRect = QRectF(0, 0, totalRect.width(), totalRect.height())

        svg = QSvgGenerator()
        svg.setFileName(filename)
        svg.setSize(QSize(totalRect.width(), totalRect.height()))
        svg.setViewBox(svgRect)
        svg.setTitle(self.model.displayName())

        painter = QPainter(svg)
        self.scene.render(painter, svgRect, totalRect)
        painter.end()

        self.bar.pushMessage("", self.tr("Successfully exported model as SVG to <a href=\"{}\">{}</a>").format(QUrl.fromLocalFile(filename).toString(), QDir.toNativeSeparators(filename)), level=Qgis.Success, duration=5)
        self.repaintModel(controls=True)
Пример #3
0
    def _get_composer_svg_image(self, width, height, dpi):
        # from qgscomposer.cpp, QgsComposer::on_mActionExportAsSVG_triggered,
        # near end of function
        svgpath = getTempfilePath('svg')
        temp_size = os.path.getsize(svgpath)

        svg_g = QSvgGenerator()
        # noinspection PyArgumentList
        svg_g.setTitle(QgsProject.instance().title())
        svg_g.setFileName(svgpath)
        svg_g.setSize(QSize(width, height))
        svg_g.setViewBox(QRect(0, 0, width, height))
        svg_g.setResolution(dpi)

        sp = QPainter(svg_g)
        self._c.renderPage(sp, 0)
        sp.end()

        if temp_size == os.path.getsize(svgpath):
            return False, ''

        image = QImage(width, height, self._TestMapSettings.outputImageFormat())
        image.fill(QColor(152, 219, 249).rgb())
        image.setDotsPerMeterX(dpi / 25.4 * 1000)
        image.setDotsPerMeterY(dpi / 25.4 * 1000)

        svgr = QSvgRenderer(svgpath)
        p = QPainter(image)
        p.setRenderHint(
            QPainter.Antialiasing,
            self._TestMapSettings.testFlag(QgsMapSettings.Antialiasing)
        )
        p.setRenderHint(QPainter.TextAntialiasing)
        svgr.render(p)
        p.end()

        filepath = getTempfilePath('png')
        res = image.save(filepath, 'png')
        if not res:
            os.unlink(filepath)
            filepath = ''
        # TODO: remove .svg file as well?

        return res, filepath
Пример #4
0
    def _get_layout_svg_image(self, width, height, dpi):
        svgpath = getTempfilePath('svg')
        temp_size = os.path.getsize(svgpath)

        svg_g = QSvgGenerator()
        # noinspection PyArgumentList
        svg_g.setTitle(QgsProject.instance().title())
        svg_g.setFileName(svgpath)
        svg_g.setSize(QSize(width, height))
        svg_g.setViewBox(QRect(0, 0, width, height))
        svg_g.setResolution(dpi)

        sp = QPainter(svg_g)
        exporter = QgsLayoutExporter(self._c)
        exporter.renderPage(sp, 0)
        sp.end()

        if temp_size == os.path.getsize(svgpath):
            return False, ''

        image = QImage(width, height, self._TestMapSettings.outputImageFormat())
        image.fill(QColor(152, 219, 249).rgb())
        image.setDotsPerMeterX(dpi / 25.4 * 1000)
        image.setDotsPerMeterY(dpi / 25.4 * 1000)

        svgr = QSvgRenderer(svgpath)
        p = QPainter(image)
        p.setRenderHint(
            QPainter.Antialiasing,
            self._TestMapSettings.testFlag(QgsMapSettings.Antialiasing)
        )
        p.setRenderHint(QPainter.TextAntialiasing)
        svgr.render(p)
        p.end()

        filepath = getTempfilePath('png')
        res = image.save(filepath, 'png')
        if not res:
            os.unlink(filepath)
            filepath = ''
        # TODO: remove .svg file as well?

        return res, filepath
Пример #5
0
 def _svgPaintDevice(self, name, sourceRect, targetRect):
     """Get QSvgGenerator as paint device"""
     generator = QSvgGenerator()
     generator.setDescription("This SVG was generated with the geoCore "
         "plugin of QGIS, written by T-Systems on site services GmbH")
     generator.setTitle("geoCore")
     generator.setSize(sourceRect.size().toSize())
     generator.setViewBox(targetRect)
     generator.setFileName(name)
     return generator
Пример #6
0
def append_CharacterMarkerSymbolLayerAsSvg(symbol, layer, context: Context):  # pylint: disable=too-many-locals
    """
    Appends a CharacterMarkerSymbolLayer to a symbol, rendering the font character
    to an SVG file.
    """
    font_family = layer.font
    character = chr(layer.unicode)
    color = symbol_color_to_qcolor(layer.color)
    angle = convert_angle(layer.angle)

    font = QFont(font_family)
    font.setPointSizeF(layer.size)

    # Using the rect of a painter path gives better results then using font metrics
    path = QPainterPath()
    path.setFillRule(Qt.WindingFill)
    path.addText(0, 0, font, character)

    rect = path.boundingRect()

    font_bounding_rect = QFontMetricsF(font).boundingRect(character)

    # adjust size -- marker size in esri is the font size, svg marker size in qgis is the svg rect size
    scale = rect.width() / font_bounding_rect.width()

    gen = QSvgGenerator()
    svg_path = symbol_name_to_filename(context.symbol_name,
                                       context.picture_folder, 'svg')
    gen.setFileName(svg_path)
    gen.setViewBox(rect)

    painter = QPainter(gen)
    painter.setFont(font)

    # todo -- size!

    if context.parameterise_svg:
        painter.setBrush(QBrush(QColor(255, 0, 0)))
    else:
        painter.setBrush(QBrush(color))
    painter.setPen(Qt.NoPen)
    painter.drawPath(path)
    painter.end()

    if context.parameterise_svg:
        with open(svg_path, 'r') as f:
            t = f.read()

        t = t.replace('#ff0000', 'param(fill)')
        t = t.replace('fill-opacity="1" ',
                      'fill-opacity="param(fill-opacity)"')
        t = t.replace(
            'stroke="none"',
            'stroke="param(outline)" stroke-opacity="param(outline-opacity) 1" stroke-width="param(outline-width) 0"'
        )
        with open(svg_path, 'w') as f:
            f.write(t)

    svg_path = context.convert_path(svg_path)

    out = QgsSvgMarkerSymbolLayer(svg_path)

    out.setSizeUnit(context.units)
    out.setSize(context.convert_size(scale * rect.width()))
    out.setAngle(angle)
    out.setFillColor(color)
    out.setStrokeWidth(0)

    out.setEnabled(layer.enabled)
    out.setLocked(layer.locked)

    out.setOffset(
        adjust_offset_for_rotation(
            QPointF(context.convert_size(layer.x_offset),
                    -context.convert_size(layer.y_offset)), layer.angle))
    out.setOffsetUnit(context.units)

    symbol.appendSymbolLayer(out)
    def set_text_image(self, text, color=Qt.gray):
        """
        Set the item picture based on an SVG file created from the given text.
        :param text: Text to be rendered as SVG.
        :type text: str
        :param color: Font color
        :type color: QColor
        """
        w, h = 200, 50
        svg_path = self._gen_svg_path()
        svg_gen = QSvgGenerator()
        svg_gen.setFileName(svg_path)
        svg_gen.setTitle('QrBarCodeLayoutItem')
        svg_gen.setDescription('Image generated by QrBarCodeLayoutItem plugin')
        svg_gen.setSize(QSize(w, h))
        svg_gen.setViewBox(QRect(0, 0, w, h))

        # Paint text
        font = QFont('Arial', 14, QFont.Bold)
        p = QPainter()
        p.begin(svg_gen)
        p.setFont(font)
        p.setPen(color)
        p.drawText(QPointF(10, 20), text)
        p.end()

        # Set picture path
        self.setPicturePath(svg_path)