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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
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 #include "KoFormulaShapeFactory.h"
0020 
0021 #include <klocalizedstring.h>
0022 
0023 #include <KoIcon.h>
0024 #include <KoShapeFactoryBase.h>
0025 #include <KoShapeLoadingContext.h>
0026 #include <KoOdfLoadingContext.h>
0027 #include <KoXmlNS.h>
0028 
0029 #include "FormulaDebug.h"
0030 #include "KoFormulaShape.h"
0031 
0032 
0033 KoFormulaShapeFactory::KoFormulaShapeFactory()
0034     : KoShapeFactoryBase(KoFormulaShapeId, i18n( "Formula" ) )
0035 {
0036     setToolTip(i18n( "A formula"));
0037     setIconName(koIconName("x-shape-formula"));
0038 
0039     // The following lines let the formula shape load both embedded and
0040     // inline formulas.
0041     //
0042     // The line below tells the shape registry which XML elements that
0043     // the shape supports.
0044     //
0045     // FIXME: Find out if inline formulas are supported by ODF.
0046 
0047     QList<QPair<QString, QStringList> > elementNamesList;
0048     elementNamesList.append(qMakePair(QString(KoXmlNS::draw), QStringList("object")));
0049     elementNamesList.append(qMakePair(QString(KoXmlNS::math), QStringList("math")));
0050     setXmlElements(elementNamesList);
0051 
0052     setLoadingPriority( 1 );
0053 /*    KoShapeTemplate t;
0054     t.id = KoFormulaShapeId;
0055     t.name = i18n("Formula");
0056     t.toolTip = i18n("A formula");
0057     t.icon = ""; //TODO add it
0058     props = new KoProperties();
0059     t.properties = props;
0060     addTemplate( t );*/
0061 }
0062 
0063 KoFormulaShapeFactory::~KoFormulaShapeFactory()
0064 {}
0065 
0066 KoShape *KoFormulaShapeFactory::createDefaultShape(KoDocumentResourceManager *resourceManager) const
0067 {
0068     KoFormulaShape* formula = new KoFormulaShape(resourceManager);
0069     formula->setShapeId( KoFormulaShapeId );
0070     return formula;
0071 }
0072 
0073 bool KoFormulaShapeFactory::supports(const KoXmlElement& e, KoShapeLoadingContext &context) const
0074 {
0075     Q_UNUSED(context);
0076     if ((e.localName() == "math" && e.namespaceURI() == KoXmlNS::math)) {
0077         return true;
0078     }
0079 
0080     if (e.localName() == "object" && e.namespaceURI() == KoXmlNS::draw) {
0081         QString href = e.attribute("href");
0082         if (!href.isEmpty()) {
0083             // check the mimetype
0084             if (href.startsWith(QLatin1String("./"))) {
0085                 href.remove(0, 2);
0086             }
0087 
0088             const QString mimetype = context.odfLoadingContext().mimeTypeForPath(href);
0089             return mimetype.isEmpty() || mimetype == "application/vnd.oasis.opendocument.formula";
0090         }
0091     }
0092 
0093     return false;
0094 }