Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSvgPaste.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "KoSvgPaste.h"
8
9#include <QApplication>
10#include <QClipboard>
11#include <QMimeData>
12
13#include <SvgParser.h>
15#include <FlakeDebug.h>
16#include <QRectF>
17#include <KoMarker.h>
18
20{
21public:
23 : mimeData(QApplication::clipboard()->mimeData())
24 {
25
26 }
27
28 const QMimeData *mimeData;
29};
30
32 : d(new Private)
33{
34}
35
37{
38 delete(d);
39}
40
42{
43 bool hasSvg = false;
44 if (d->mimeData) {
45 Q_FOREACH(const QString &format, d->mimeData->formats()) {
46 if (format.toLower().contains("svg")) {
47 hasSvg = true;
48 break;
49 }
50 }
51 }
52
53 return hasSvg;
54}
55
56QList<KoShape*> KoSvgPaste::fetchShapes(const QRectF viewportInPx, qreal resolutionPPI, QSizeF *fragmentSize)
57{
58 QList<KoShape*> shapes;
59
60 if (!d->mimeData) return shapes;
61
62 QByteArray data;
63
64 Q_FOREACH(const QString &format, d->mimeData->formats()) {
65 if (format.toLower().contains("svg")) {
66 data = d->mimeData->data(format);
67 break;
68 }
69 }
70
71 if (data.isEmpty()) {
72 return shapes;
73 }
74
75 return fetchShapesFromData(data, viewportInPx, resolutionPPI, fragmentSize);
76
77}
78
79QList<KoShape*> KoSvgPaste::fetchShapesFromData(const QByteArray &data, const QRectF viewportInPx, qreal resolutionPPI, QSizeF *fragmentSize)
80{
81 QList<KoShape*> shapes;
82
83 if (data.isEmpty()) {
84 return shapes;
85 }
86
87
88
89 QString errorMsg;
90 int errorLine = 0;
91 int errorColumn = 0;
92
93 QDomDocument doc = SvgParser::createDocumentFromSvg(data, &errorMsg, &errorLine, &errorColumn);
94 if (doc.isNull()) {
95 qWarning() << "Failed to process an SVG file at"
96 << errorLine << ":" << errorColumn << "->" << errorMsg;
97 return shapes;
98 }
99
100 KoDocumentResourceManager resourceManager;
101 SvgParser parser(&resourceManager);
102 parser.setResolution(viewportInPx, resolutionPPI);
103
104 shapes = parser.parseSvg(doc.documentElement(), fragmentSize);
105
106 return shapes;
107}
const QMimeData * mimeData
QList< KoShape * > fetchShapes(QRectF viewportInPx, qreal resolutionPPI, QSizeF *fragmentSize=nullptr)
bool hasShapes()
virtual ~KoSvgPaste()
Private *const d
Definition KoSvgPaste.h:31
static QList< KoShape * > fetchShapesFromData(const QByteArray &data, QRectF viewportInPx, qreal resolutionPPI, QSizeF *fragmentSize=nullptr)
static QDomDocument createDocumentFromSvg(QIODevice *device, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
QList< KoShape * > parseSvg(const QDomElement &e, QSizeF *fragmentSize=0)
Parses a svg fragment, returning the list of top level child shapes.
void setResolution(const QRectF boundsInPixels, qreal pixelsPerInch)