File indexing completed on 2024-05-12 16:34:24
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2007 Thomas Zander <zander@kde.org> 0003 * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net> 0004 * Copyright (C) 2011 Boudewijn Rempt <boud@valdyas.org> 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Library General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Library General Public License 0017 * along with this library; see the file COPYING.LIB. If not, write to 0018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #include "PictureShapeFactory.h" 0023 0024 #include "PictureShape.h" 0025 #include "PictureShapeConfigWidget.h" 0026 #include "PictureDebug.h" 0027 0028 #include <QByteArray> 0029 #include <QBuffer> 0030 #include <QImage> 0031 0032 #include <KoXmlNS.h> 0033 #include "KoShapeBasedDocumentBase.h" 0034 #include <KoShapeLoadingContext.h> 0035 #include <KoOdfLoadingContext.h> 0036 #include <KoDocumentResourceManager.h> 0037 #include <KoImageCollection.h> 0038 #include <KoImageData.h> 0039 #include <KoProperties.h> 0040 #include <KoIcon.h> 0041 #include <klocalizedstring.h> 0042 0043 PictureShapeFactory::PictureShapeFactory() 0044 : KoShapeFactoryBase(PICTURESHAPEID, i18n("Image")) 0045 { 0046 setToolTip(i18n("Image shape that can display jpg, png etc.")); 0047 setIconName(koIconName("x-shape-image")); 0048 setLoadingPriority(1); 0049 0050 QList<QPair<QString, QStringList> > elementNamesList; 0051 elementNamesList.append(qMakePair(QString(KoXmlNS::draw), QStringList("image"))); 0052 elementNamesList.append(qMakePair(QString(KoXmlNS::svg), QStringList("image"))); 0053 setXmlElements(elementNamesList); 0054 } 0055 0056 KoShape *PictureShapeFactory::createDefaultShape(KoDocumentResourceManager *documentResources) const 0057 { 0058 PictureShape * defaultShape = new PictureShape(); 0059 defaultShape->setShapeId(PICTURESHAPEID); 0060 if (documentResources) { 0061 defaultShape->setImageCollection(documentResources->imageCollection()); 0062 } 0063 return defaultShape; 0064 } 0065 0066 KoShape *PictureShapeFactory::createShape(const KoProperties *params, KoDocumentResourceManager *documentResources) const 0067 { 0068 PictureShape *shape = static_cast<PictureShape*>(createDefaultShape(documentResources)); 0069 if (params->contains("qimage")) { 0070 QImage image = params->property("qimage").value<QImage>(); 0071 Q_ASSERT(!image.isNull()); 0072 0073 if (shape->imageCollection()) { 0074 KoImageData *data = shape->imageCollection()->createImageData(image); 0075 shape->setUserData(data); 0076 shape->setSize(data->imageSize()); 0077 shape->update(); 0078 } 0079 } 0080 return shape; 0081 0082 } 0083 0084 bool PictureShapeFactory::supports(const KoXmlElement &e, KoShapeLoadingContext &context) const 0085 { 0086 if (e.localName() == "image" && e.namespaceURI() == KoXmlNS::draw) { 0087 QString href = e.attribute("href"); 0088 if (!href.isEmpty()) { 0089 // check the mimetype 0090 if (href.startsWith(QLatin1String("./"))) { 0091 href.remove(0, 2); 0092 } 0093 QString mimetype = context.odfLoadingContext().mimeTypeForPath(href); 0094 if (!mimetype.isEmpty()) { 0095 return mimetype.startsWith("image"); 0096 } 0097 else { 0098 return ( href.endsWith("bmp") || 0099 href.endsWith("jpg") || 0100 href.endsWith("gif") || 0101 href.endsWith("eps") || 0102 href.endsWith("png") || 0103 href.endsWith("tif") || 0104 href.endsWith("tiff")); 0105 } 0106 } 0107 else { 0108 return !KoXml::namedItemNS(e, KoXmlNS::office, "binary-data").isNull(); 0109 } 0110 } 0111 return false; 0112 } 0113 0114 QList<KoShapeConfigWidgetBase*> PictureShapeFactory::createShapeOptionPanels() 0115 { 0116 QList<KoShapeConfigWidgetBase*> panels; 0117 panels.append( new PictureShapeConfigWidget() ); 0118 return panels; 0119 } 0120 0121 void PictureShapeFactory::newDocumentResourceManager(KoDocumentResourceManager *manager) const 0122 { 0123 if (!manager->imageCollection()) 0124 manager->setImageCollection(new KoImageCollection(manager)); 0125 }