Warning, file /office/calligra/libs/flake/KoMarkerCollection.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2011 Thorsten Zachmann <zachmann@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoMarkerCollection.h"
0021 
0022 #include "KoMarker.h"
0023 #include "KoMarkerSharedLoadingData.h"
0024 #include <KoXmlReader.h>
0025 #include <KoShapeLoadingContext.h>
0026 #include <KoOdfLoadingContext.h>
0027 #include <KoOdfReadStore.h>
0028 #include <QStandardPaths>
0029 #include <FlakeDebug.h>
0030 
0031 class Q_DECL_HIDDEN KoMarkerCollection::Private
0032 {
0033 public:
0034     ~Private()
0035     {
0036     }
0037 
0038     QList<QExplicitlySharedDataPointer<KoMarker> > markers;
0039 };
0040 
0041 KoMarkerCollection::KoMarkerCollection(QObject *parent)
0042 : QObject(parent)
0043 , d(new Private)
0044 {
0045     // Add no marker so the user can remove a marker from the line.
0046     d->markers.append(QExplicitlySharedDataPointer<KoMarker>(0));
0047     // Add default markers
0048     loadDefaultMarkers();
0049 }
0050 
0051 KoMarkerCollection::~KoMarkerCollection()
0052 {
0053     delete d;
0054 }
0055 
0056 bool KoMarkerCollection::loadOdf(KoShapeLoadingContext &context)
0057 {
0058     debugFlake;
0059     QHash<QString, KoMarker*> lookupTable;
0060 
0061     const QHash<QString, KoXmlElement*> markers = context.odfLoadingContext().stylesReader().drawStyles("marker");
0062     loadOdfMarkers(markers, context, lookupTable);
0063 
0064     KoMarkerSharedLoadingData * sharedMarkerData = new KoMarkerSharedLoadingData(lookupTable);
0065     context.addSharedData(MARKER_SHARED_LOADING_ID, sharedMarkerData);
0066 
0067     return true;
0068 }
0069 
0070 void KoMarkerCollection::loadDefaultMarkers()
0071 {
0072     // use the same mechanism for loading the markers that are available
0073     // per default as when loading the normal markers.
0074     KoOdfStylesReader markerReader;
0075     KoOdfLoadingContext odfContext(markerReader, 0);
0076     KoShapeLoadingContext shapeContext(odfContext, 0);
0077     KoXmlDocument doc;
0078     QString filePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "calligra/styles/markers.xml");
0079 
0080     if (!filePath.isEmpty()) {
0081         QFile file(filePath);
0082         QString errorMessage;
0083         if (KoOdfReadStore::loadAndParse(&file, doc, errorMessage, filePath)) {
0084             markerReader.createStyleMap(doc, true);
0085 
0086             QHash<QString, KoMarker*> lookupTable;
0087             const QHash<QString, KoXmlElement*> defaultMarkers = markerReader.drawStyles("marker");
0088             loadOdfMarkers(defaultMarkers, shapeContext, lookupTable);
0089         }
0090         else {
0091             warnFlake << "reading of" << filePath << "failed:" << errorMessage;
0092         }
0093     }
0094     else {
0095         debugFlake << "markers.xml not found";
0096     }
0097 }
0098 
0099 void KoMarkerCollection::loadOdfMarkers(const QHash<QString, KoXmlElement*> &markers, KoShapeLoadingContext &context, QHash<QString, KoMarker*> &lookupTable)
0100 {
0101     QHash<QString, KoXmlElement*>::const_iterator it(markers.constBegin());
0102     for (; it != markers.constEnd(); ++it) {
0103         KoMarker *marker = new KoMarker();
0104         if (marker->loadOdf(*(it.value()), context)) {
0105             KoMarker *m = addMarker(marker);
0106             lookupTable.insert(it.key(), m);
0107             debugFlake << "loaded marker" << it.key() << marker << m;
0108             if (m != marker) {
0109                 delete marker;
0110             }
0111         }
0112         else {
0113             delete marker;
0114         }
0115     }
0116 }
0117 
0118 QList<KoMarker*> KoMarkerCollection::markers() const
0119 {
0120     QMap<QString, KoMarker*> markerMap;
0121     foreach (const QExplicitlySharedDataPointer<KoMarker>& m, d->markers) {
0122         const auto name = m ? m->name() : QString();
0123         markerMap[name] = m.data();
0124     }
0125     return markerMap.values();
0126 }
0127 
0128 KoMarker * KoMarkerCollection::addMarker(KoMarker *marker)
0129 {
0130     foreach (const QExplicitlySharedDataPointer<KoMarker>& m, d->markers) {
0131         if (marker == m.data()) {
0132             return marker;
0133         }
0134         if (m && *marker == *m) {
0135             debugFlake << "marker is the same as other";
0136             return m.data();
0137         }
0138     }
0139     d->markers.append(QExplicitlySharedDataPointer<KoMarker>(marker));
0140     return marker;
0141 }