Warning, file /office/calligra/libs/flake/KoMarker.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 "KoMarker.h" 0021 0022 #include <KoXmlReader.h> 0023 #include <KoXmlNS.h> 0024 #include <KoGenStyle.h> 0025 #include <KoGenStyles.h> 0026 #include "KoPathShape.h" 0027 #include "KoPathShapeLoader.h" 0028 #include "KoShapeLoadingContext.h" 0029 #include "KoShapeSavingContext.h" 0030 #include "KoOdfWorkaround.h" 0031 0032 #include <QString> 0033 #include <QUrl> 0034 #include <QPainterPath> 0035 0036 class Q_DECL_HIDDEN KoMarker::Private 0037 { 0038 public: 0039 Private() 0040 {} 0041 0042 QString name; 0043 QString d; 0044 QPainterPath path; 0045 QRect viewBox; 0046 }; 0047 0048 KoMarker::KoMarker() 0049 : d(new Private()) 0050 { 0051 } 0052 0053 KoMarker::~KoMarker() 0054 { 0055 delete d; 0056 } 0057 0058 bool KoMarker::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context) 0059 { 0060 Q_UNUSED(context); 0061 // A shape uses draw:marker-end="Arrow" draw:marker-end-width="0.686cm" draw:marker-end-center="true" which marker and how the marker is used 0062 0063 //<draw:marker draw:name="Arrow" svg:viewBox="0 0 20 30" svg:d="m10 0-10 30h20z"/> 0064 //<draw:marker draw:name="Arrowheads_20_1" draw:display-name="Arrowheads 1" svg:viewBox="0 0 10 10" svg:d="m0 0h10v10h-10z"/> 0065 0066 d->d =element.attributeNS(KoXmlNS::svg, "d"); 0067 if (d->d.isEmpty()) { 0068 return false; 0069 } 0070 0071 #ifndef NWORKAROUND_ODF_BUGS 0072 KoOdfWorkaround::fixMarkerPath(d->d); 0073 #endif 0074 0075 KoPathShape pathShape; 0076 KoPathShapeLoader loader(&pathShape); 0077 loader.parseSvg(d->d, true); 0078 0079 d->path = pathShape.outline(); 0080 d->viewBox = KoPathShape::loadOdfViewbox(element); 0081 0082 QString displayName(element.attributeNS(KoXmlNS::draw, "display-name")); 0083 if (displayName.isEmpty()) { 0084 displayName = element.attributeNS(KoXmlNS::draw, "name"); 0085 } 0086 d->name = displayName; 0087 return true; 0088 } 0089 0090 QString KoMarker::saveOdf(KoShapeSavingContext &context) const 0091 { 0092 KoGenStyle style(KoGenStyle::MarkerStyle); 0093 style.addAttribute("draw:display-name", d->name); 0094 style.addAttribute("svg:d", d->d); 0095 const QString viewBox = QString::fromLatin1("%1 %2 %3 %4") 0096 .arg(d->viewBox.x()).arg(d->viewBox.y()) 0097 .arg(d->viewBox.width()).arg(d->viewBox.height()); 0098 style.addAttribute(QLatin1String("svg:viewBox"), viewBox); 0099 QString name = QString(QUrl::toPercentEncoding(d->name, "", " ")).replace('%', '_'); 0100 return context.mainStyles().insert(style, name, KoGenStyles::DontAddNumberToName); 0101 } 0102 0103 QString KoMarker::name() const 0104 { 0105 return d->name; 0106 } 0107 0108 QPainterPath KoMarker::path(qreal width) const 0109 { 0110 if (!d->viewBox.isValid() || width == 0) { 0111 return QPainterPath(); 0112 } 0113 0114 // TODO: currently the <min-x>, <min-y> properties of viewbox are ignored, why? OOo-compat? 0115 qreal height = width * d->viewBox.height() / d->viewBox.width(); 0116 0117 QTransform transform; 0118 transform.scale(width / d->viewBox.width(), height / d->viewBox.height()); 0119 return transform.map(d->path); 0120 } 0121 0122 bool KoMarker::operator==(const KoMarker &other) const 0123 { 0124 return (d->d == other.d->d && d->viewBox ==other.d->viewBox); 0125 }