File indexing completed on 2024-05-12 15:56:42

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2011 Thorsten Zachmann <zachmann@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "KoMarkerCollection.h"
0008 
0009 #include <QFile>
0010 
0011 #include <klocalizedstring.h>
0012 #include "KoMarker.h"
0013 #include <FlakeDebug.h>
0014 #include <KoResourcePaths.h>
0015 #include <SvgParser.h>
0016 #include <QFileInfo>
0017 #include <KoDocumentResourceManager.h>
0018 
0019 #include "kis_debug.h"
0020 
0021 // WARNING: there is a bug in GCC! It doesn't warn that we are
0022 //          deleting an uninitialized type here!
0023 #include <KoShape.h>
0024 
0025 
0026 class Q_DECL_HIDDEN KoMarkerCollection::Private
0027 {
0028 public:
0029     ~Private()
0030     {
0031     }
0032 
0033     QList<QExplicitlySharedDataPointer<KoMarker> > markers;
0034 };
0035 
0036 KoMarkerCollection::KoMarkerCollection(QObject *parent)
0037 : QObject(parent)
0038 , d(new Private)
0039 {
0040     // Add no marker so the user can remove a marker from the line.
0041     d->markers.append(QExplicitlySharedDataPointer<KoMarker>(0));
0042     // Add default markers
0043     loadDefaultMarkers();
0044 }
0045 
0046 KoMarkerCollection::~KoMarkerCollection()
0047 {
0048     delete d;
0049 }
0050 
0051 void KoMarkerCollection::loadMarkersFromFile(const QString &svgFile)
0052 {
0053     QFile file(svgFile);
0054     if (!file.exists()) return;
0055 
0056     if (!file.open(QIODevice::ReadOnly)) return;
0057 
0058     QString errorMsg;
0059     int errorLine = 0;
0060     int errorColumn;
0061 
0062     QDomDocument doc = SvgParser::createDocumentFromSvg(&file, &errorMsg, &errorLine, &errorColumn);
0063     if (doc.isNull()) {
0064         errKrita << "Parsing error in " << svgFile << "! Aborting!" << endl
0065         << " In line: " << errorLine << ", column: " << errorColumn << endl
0066         << " Error message: " << errorMsg << endl;
0067         errKrita << i18n("Parsing error in the main document at line %1, column %2\nError message: %3"
0068                          , errorLine , errorColumn , errorMsg);
0069         return;
0070     }
0071 
0072     KoDocumentResourceManager manager;
0073     SvgParser parser(&manager);
0074     parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values
0075     parser.setXmlBaseDir(QFileInfo(svgFile).absolutePath());
0076 
0077     parser.setFileFetcher(
0078         [](const QString &fileName) {
0079             QFile file(fileName);
0080             if (!file.exists()) return QByteArray();
0081 
0082             file.open(QIODevice::ReadOnly);
0083             return file.readAll();
0084         });
0085 
0086     QSizeF fragmentSize;
0087     QList<KoShape*> shapes = parser.parseSvg(doc.documentElement(), &fragmentSize);
0088     qDeleteAll(shapes);
0089 
0090     Q_FOREACH (const QExplicitlySharedDataPointer<KoMarker> &marker, parser.knownMarkers()) {
0091         addMarker(marker.data());
0092     }
0093 }
0094 
0095 void KoMarkerCollection::loadDefaultMarkers()
0096 {
0097     QString filePath = KoResourcePaths::findAsset("markers", "markers.svg");
0098     loadMarkersFromFile(filePath);
0099 }
0100 
0101 QList<KoMarker*> KoMarkerCollection::markers() const
0102 {
0103     QList<KoMarker*> markerList;
0104     foreach (const QExplicitlySharedDataPointer<KoMarker>& m, d->markers){
0105         markerList.append(m.data());
0106     }
0107     return markerList;
0108 }
0109 
0110 KoMarker * KoMarkerCollection::addMarker(KoMarker *marker)
0111 {
0112     foreach (const QExplicitlySharedDataPointer<KoMarker>& m, d->markers) {
0113         if (marker == m.data()) {
0114             return marker;
0115         }
0116         if (m && *marker == *m) {
0117             debugFlake << "marker is the same as other";
0118             return m.data();
0119         }
0120     }
0121     d->markers.append(QExplicitlySharedDataPointer<KoMarker>(marker));
0122     return marker;
0123 }