Krita Source Code Documentation
Loading...
Searching...
No Matches
KoMarkerCollection.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2011 Thorsten Zachmann <zachmann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
8
9#include <QFile>
10
11#include <klocalizedstring.h>
12#include "KoMarker.h"
13#include <FlakeDebug.h>
14#include <KoResourcePaths.h>
15#include <SvgParser.h>
16#include <QFileInfo>
18
19#include "kis_debug.h"
20
21// WARNING: there is a bug in GCC! It doesn't warn that we are
22// deleting an uninitialized type here!
23#include <KoShape.h>
24
25
26class Q_DECL_HIDDEN KoMarkerCollection::Private
27{
28public:
30 {
31 }
32
34};
35
37: QObject(parent)
38, d(new Private)
39{
40 // Add no marker so the user can remove a marker from the line.
41 d->markers.append(QExplicitlySharedDataPointer<KoMarker>(0));
42 // Add default markers
44}
45
50
51void KoMarkerCollection::loadMarkersFromFile(const QString &svgFile)
52{
53 QFile file(svgFile);
54 if (!file.exists()) return;
55
56 if (!file.open(QIODevice::ReadOnly)) return;
57
58 QString errorMsg;
59 int errorLine = 0;
60 int errorColumn;
61
62 QDomDocument doc = SvgParser::createDocumentFromSvg(&file, &errorMsg, &errorLine, &errorColumn);
63 if (doc.isNull()) {
64 errKrita << "Parsing error in " << svgFile << "! Aborting!" << Qt::endl
65 << " In line: " << errorLine << ", column: " << errorColumn << Qt::endl
66 << " Error message: " << errorMsg << Qt::endl;
67 errKrita << i18n("Parsing error in the main document at line %1, column %2\nError message: %3"
68 , errorLine , errorColumn , errorMsg);
69 return;
70 }
71
73 SvgParser parser(&manager);
74 parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values
75 parser.setXmlBaseDir(QFileInfo(svgFile).absolutePath());
76
77 parser.setFileFetcher(
78 [](const QString &fileName) {
79 QFile file(fileName);
80 if (!file.exists()) return QByteArray();
81
82 file.open(QIODevice::ReadOnly);
83 return file.readAll();
84 });
85
86 QSizeF fragmentSize;
87 QList<KoShape*> shapes = parser.parseSvg(doc.documentElement(), &fragmentSize);
88 qDeleteAll(shapes);
89
90 Q_FOREACH (const QExplicitlySharedDataPointer<KoMarker> &marker, parser.knownMarkers()) {
91 addMarker(marker.data());
92 }
93}
94
96{
97 QString filePath = KoResourcePaths::findAsset("markers", "markers.svg");
98 loadMarkersFromFile(filePath);
99}
100
102{
103 QList<KoMarker*> markerList;
104 foreach (const QExplicitlySharedDataPointer<KoMarker>& m, d->markers){
105 markerList.append(m.data());
106 }
107 return markerList;
108}
109
111{
112 foreach (const QExplicitlySharedDataPointer<KoMarker>& m, d->markers) {
113 if (marker == m.data()) {
114 return marker;
115 }
116 if (m && *marker == *m) {
117 debugFlake << "marker is the same as other";
118 return m.data();
119 }
120 }
121 d->markers.append(QExplicitlySharedDataPointer<KoMarker>(marker));
122 return marker;
123}
#define debugFlake
Definition FlakeDebug.h:15
QList< QExplicitlySharedDataPointer< KoMarker > > markers
void loadDefaultMarkers()
load the markers that are available per default.
KoMarkerCollection(QObject *parent=0)
KoMarker * addMarker(KoMarker *marker)
void loadMarkersFromFile(const QString &svgFile)
static QString findAsset(const QString &type, const QString &fileName)
static QDomDocument createDocumentFromSvg(QIODevice *device, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
void setXmlBaseDir(const QString &baseDir)
Sets the initial xml base directory (the directory form where the file is read)
QList< KoShape * > parseSvg(const QDomElement &e, QSizeF *fragmentSize=0)
Parses a svg fragment, returning the list of top level child shapes.
void setFileFetcher(FileFetcherFunc func)
void setResolution(const QRectF boundsInPixels, qreal pixelsPerInch)
QList< QExplicitlySharedDataPointer< KoMarker > > knownMarkers() const
#define errKrita
Definition kis_debug.h:107