File indexing completed on 2024-05-12 16:34:24

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2010  Vidhyapria  Arunkumar <vidhyapria.arunkumar@nokia.com>
0003  * Copyright (C) 2010  Amit Aggarwal <amit.5.aggarwal@nokia.com>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "PluginShape.h"
0022 
0023 #include <KoViewConverter.h>
0024 #include <KoShapeLoadingContext.h>
0025 #include <KoOdfLoadingContext.h>
0026 #include <KoShapeSavingContext.h>
0027 #include <KoXmlWriter.h>
0028 #include <KoXmlNS.h>
0029 
0030 #include <QPainter>
0031 #include <klocalizedstring.h>
0032 
0033 
0034 PluginShape::PluginShape()
0035     : KoFrameShape(KoXmlNS::draw, "plugin")
0036 {
0037     setKeepAspectRatio(true);
0038 }
0039 
0040 PluginShape::~PluginShape()
0041 {
0042 }
0043 
0044 void PluginShape::paint(QPainter &painter, const KoViewConverter &converter, KoShapePaintingContext &)
0045 {
0046     QRectF pixelsF = converter.documentToView(QRectF(QPointF(0,0), size()));
0047     painter.fillRect(pixelsF, QColor(Qt::yellow));
0048     painter.setPen(QPen(Qt::blue, 0));
0049     QString mimetype = i18n("Unknown");
0050     if (!m_mimetype.isEmpty()) {
0051         mimetype = m_mimetype;
0052     }
0053     painter.drawText(pixelsF, Qt::AlignCenter, i18n("Plugin of mimetype: %1", mimetype));
0054 }
0055 
0056 void PluginShape::saveOdf(KoShapeSavingContext &context) const
0057 {
0058     KoXmlWriter &writer = context.xmlWriter();
0059 
0060     writer.startElement("draw:frame");
0061     saveOdfAttributes(context, OdfAllAttributes);
0062     writer.startElement("draw:plugin");
0063     // cannot use "this" as referent for context.xmlid, already done for getting the xml:id for the frame
0064     // so (randomly) choosing m_xlinkhref to get another, separate unique referent for this shape
0065     const QString xmlId = context.xmlid(&m_xlinkhref, QLatin1String("plugin"), KoElementReference::Counter).toString();
0066     writer.addAttribute("xml:id", xmlId);
0067     writer.addAttribute("draw:mime-type", m_mimetype);
0068     writer.addAttribute("xlink:type", m_xlinktype);
0069     writer.addAttribute("xlink:show", m_xlinkshow);
0070     writer.addAttribute("xlink:actuate", m_xlinkactuate);
0071     writer.addAttribute("xlink:href", m_xlinkhref);
0072 
0073     QMap<QString,QString>::const_iterator itr = m_drawParams.constBegin();
0074     while (itr != m_drawParams.constEnd()) {
0075         writer.startElement("draw:param", true);
0076         writer.addAttribute("draw:name", itr.key());
0077         writer.addAttribute("draw:value", itr.value());
0078         writer.endElement(); // draw:param
0079         ++itr;
0080     }
0081     writer.endElement(); // draw:plugin
0082     saveOdfCommonChildElements(context);
0083     writer.endElement(); // draw:frame
0084 
0085 }
0086 
0087 bool PluginShape::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context)
0088 {
0089     loadOdfAttributes(element, context, OdfAllAttributes);
0090     return loadOdfFrame(element, context);
0091 }
0092 
0093 bool PluginShape::loadOdfFrameElement(const KoXmlElement &element, KoShapeLoadingContext &context)
0094 {
0095     Q_UNUSED(context);
0096     if(element.isNull()) {
0097         return false;
0098     }
0099 
0100     if(element.localName() == "plugin") {
0101         m_mimetype  = element.attributeNS(KoXmlNS::draw, "mime-type");
0102         m_xlinktype  = element.attributeNS(KoXmlNS::xlink, "type");
0103         m_xlinkshow  = element.attributeNS(KoXmlNS::xlink, "show");
0104         m_xlinkactuate  = element.attributeNS(KoXmlNS::xlink, "actuate");
0105         m_xlinkhref  = element.attributeNS(KoXmlNS::xlink, "href");
0106         m_drawParams.clear();
0107         if(element.hasChildNodes()) {
0108             KoXmlNode node = element.firstChild();
0109             while(!node.isNull()) {
0110                 if(node.isElement()) {
0111                     KoXmlElement nodeElement = node.toElement();
0112                     if(nodeElement.localName() == "param") {
0113                         QString name = nodeElement.attributeNS(KoXmlNS::draw, "name");
0114                         if(!name.isEmpty()) {
0115                             m_drawParams.insert(name,nodeElement.attributeNS(KoXmlNS::draw, "value"));
0116                         }
0117                     }
0118                 }
0119                 node = node.nextSibling();
0120             }
0121         }
0122         return true;
0123     }
0124     return false;
0125 }