Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSvgSymbolCollectionResource.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2
3 SPDX-FileCopyrightText: 2017 L. E. Segovia <amy@amyspark.me>
4
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7 */
9
10#include <QDebug>
11#include <QVector>
12#include <QFile>
13#include <QBuffer>
14#include <QByteArray>
15#include <QImage>
16#include <QPainter>
17
18#include <klocalizedstring.h>
19#include <KoMarker.h>
20#include <KoStore.h>
22#include "kis_debug.h"
23
24#include <KoShape.h>
25#include <KoShapeGroup.h>
26#include <KoShapeManager.h>
27#include <SvgParser.h>
28#include <KoMD5Generator.h>
29
30#include <FlakeDebug.h>
31
32QImage KoSvgSymbol::icon(int size = 0)
33{
34 KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(shape);
36
37 QRectF rc = group->boundingRect().normalized();
38
39 if (size == 0) {
40 size = 128;
41 }
42
43 int maxDim = qMax(rc.width(), rc.height());
44 // no need to customize it, most of the time the code being able to set the size
45 // doesn't even know where this image will end up
46 // because it's saved into the database and then used in widgets
47 const qreal margin = 0.05;
48
49 QImage image(size, size, QImage::Format_ARGB32_Premultiplied);
50 qreal symbolScale = (qreal)(size*(1.0 - 2*margin))/maxDim;
51 QPainter gc(&image);
52 gc.setRenderHint(QPainter::Antialiasing, true);
53 image.fill(Qt::gray);
54
55// debugFlake << "Going to render. Original bounding rect:" << group->boundingRect()
56// << "Normalized: " << rc
57// << "Scale W" << 256 / rc.width() << "Scale H" << 256 / rc.height();
58 gc.translate(size*margin, size*margin); // go inwards (towards the center, or bottom-right corner) by one margin
59 gc.scale(symbolScale, symbolScale);
60 gc.translate(-rc.x(), -rc.y());
61
62 gc.translate((maxDim - rc.width())/2.0, (maxDim - rc.height())/2.0);
64 gc.end();
65 return image;
66}
67
68
69
76
77
79 : KoResource(filename)
80 , d(new Private())
81{
82}
83
89
91 : KoResource(QString())
92 , d(new Private(*rhs.d))
93{
94 setFilename(rhs.filename());
95
96 Q_FOREACH(KoSvgSymbol *symbol, rhs.d->symbols) {
97 d->symbols << new KoSvgSymbol(*symbol);
98 }
99
100 setValid(true);
101}
102
107
112
114{
115 Q_UNUSED(resourcesInterface);
116
117 if (!dev->isOpen()) {
118 dev->open(QIODevice::ReadOnly);
119 }
120
121 d->data = dev->readAll();
123
124 dev->seek(0);
125
126 QString errorMsg;
127 int errorLine = 0;
128 int errorColumn;
129
130 QDomDocument doc = SvgParser::createDocumentFromSvg(dev, &errorMsg, &errorLine, &errorColumn);
131 if (doc.isNull()) {
132
133 errKrita << "Parsing error in " << filename() << "! Aborting!" << Qt::endl
134 << " In line: " << errorLine << ", column: " << errorColumn << Qt::endl
135 << " Error message: " << errorMsg << Qt::endl;
136 errKrita << i18n("Parsing error in the main document at line %1, column %2\nError message: %3"
137 , errorLine , errorColumn , errorMsg);
138 return false;
139 }
140
142 SvgParser parser(&manager);
143 parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values
144 QSizeF fragmentSize;
145 // We're not interested in the shapes themselves
146 qDeleteAll(parser.parseSvg(doc.documentElement(), &fragmentSize));
147 d->symbols = parser.takeSymbols();
148// debugFlake << "Loaded" << filename() << "\n\t"
149// << "Title" << parser.documentTitle() << "\n\t"
150// << "Description" << parser.documentDescription()
151// << "\n\tgot" << d->symbols.size() << ResourceType::Symbols
152// << d->symbols[0]->shape->outlineRect()
153// << d->symbols[0]->shape->size();
154
155 d->title = parser.documentTitle();
156 if (d->title.isEmpty()) {
157 d->title = filename();
158 }
159 setName(d->title);
160 d->description = parser.documentDescription();
161
162 if (d->symbols.size() < 1) {
163 setValid(false);
164 return false;
165 }
166 setValid(true);
167 setImage(d->symbols[0]->icon(256));
168 return true;
169}
170
172{
173 dev->open(QIODevice::WriteOnly);
174 dev->write(d->data);
175 dev->close();
176 return true;
177}
178
180{
181 return QString(".svg");
182}
183
185{
186 return d->title;
187}
188
190{
191 return d->description;
192}
193
195{
196 return "";
197}
198
200{
201 return "";
202}
203
205{
206 return "";
207}
208
213
215{
216 return "";
217}
218
223
QList< QString > QStringList
static QString generateHash(const QString &filename)
generateHash reads the given file and generates a hex-encoded md5sum for the file.
QRectF boundingRect() const override
a group's boundingRect
static void renderSingleShape(KoShape *shape, QPainter &painter)
renderSingleShape renders a shape on painter. This method includes all the needed steps for painting ...
KoSvgSymbolCollectionResource()
Create an empty color set.
bool loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP resourcesInterface) override
QVector< KoSvgSymbol * > symbols() const
const QScopedPointer< Private > d
bool saveToDevice(QIODevice *dev) const override
static QDomDocument createDocumentFromSvg(QIODevice *device, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
QVector< KoSvgSymbol * > takeSymbols()
QString documentTitle() const
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)
QString documentDescription() const
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define errKrita
Definition kis_debug.h:107
QSharedPointer< KoResource > KoResourceSP
void setValid(bool valid)
void setName(const QString &name)
void setMD5Sum(const QString &md5sum)
Set the md5sum of this resource. It must be in hex-encoded string format.
void setFilename(const QString &filename)
QString filename
void setImage(const QImage &image)