Warning, file /office/calligra/libs/flake/KoShapeFactoryBase.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) 2006 Boudewijn Rempt (boud@valdyas.org)
0003  * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
0004  * Copyright (C) 2008 C. Boemann <cbo@boemann.dk>
0005  * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public License
0018  * along with this library; see the file COPYING.LIB.  If not, write to
0019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021  */
0022 
0023 #include "KoShapeFactoryBase.h"
0024 
0025 #include "KoDocumentResourceManager.h"
0026 #include "KoDeferredShapeFactoryBase.h"
0027 #include "KoShape.h"
0028 #include "KoShapeLoadingContext.h"
0029 
0030 #include <KoOdfLoadingContext.h>
0031 #include <KoProperties.h>
0032 #include <KoStyleStack.h>
0033 #include <KoPluginLoader.h>
0034 
0035 #include <KPluginFactory>
0036 #include <QPluginLoader>
0037 #include <QMutexLocker>
0038 #include <QMutex>
0039 
0040 
0041 
0042 #include <FlakeDebug.h>
0043 
0044 class Q_DECL_HIDDEN KoShapeFactoryBase::Private
0045 {
0046 public:
0047     Private(const QString &_id, const QString &_name, const QString &_deferredPluginName)
0048         : deferredFactory(0),
0049           deferredPluginName(_deferredPluginName),
0050           id(_id),
0051           name(_name),
0052           loadingPriority(0),
0053           hidden(false)
0054     {
0055     }
0056 
0057     ~Private() {
0058         foreach(const KoShapeTemplate & t, templates)
0059             delete t.properties;
0060         templates.clear();
0061     }
0062 
0063     KoDeferredShapeFactoryBase *deferredFactory;
0064     QMutex pluginLoadingMutex;
0065     QString deferredPluginName;
0066     QList<KoShapeTemplate> templates;
0067     QList<KoShapeConfigFactoryBase*> configPanels;
0068     const QString id;
0069     const QString name;
0070     QString family;
0071     QString tooltip;
0072     QString iconName;
0073     int loadingPriority;
0074     QList<QPair<QString, QStringList> > xmlElements; // xml name space -> xml element names
0075     bool hidden;
0076     QList<KoDocumentResourceManager *> resourceManagers;
0077 };
0078 
0079 
0080 KoShapeFactoryBase::KoShapeFactoryBase(const QString &id, const QString &name, const QString &deferredPluginName)
0081     : d(new Private(id, name, deferredPluginName))
0082 {
0083 }
0084 
0085 KoShapeFactoryBase::~KoShapeFactoryBase()
0086 {
0087     delete d;
0088 }
0089 
0090 QString KoShapeFactoryBase::toolTip() const
0091 {
0092     return d->tooltip;
0093 }
0094 
0095 QString KoShapeFactoryBase::iconName() const
0096 {
0097     return d->iconName;
0098 }
0099 
0100 QString KoShapeFactoryBase::name() const
0101 {
0102     return d->name;
0103 }
0104 
0105 QString KoShapeFactoryBase::family() const
0106 {
0107     return d->family;
0108 }
0109 
0110 int KoShapeFactoryBase::loadingPriority() const
0111 {
0112     return d->loadingPriority;
0113 }
0114 
0115 QList<QPair<QString, QStringList> > KoShapeFactoryBase::odfElements() const
0116 {
0117     return d->xmlElements;
0118 }
0119 
0120 void KoShapeFactoryBase::addTemplate(const KoShapeTemplate &params)
0121 {
0122     KoShapeTemplate tmplate = params;
0123     tmplate.id = d->id;
0124     d->templates.append(tmplate);
0125 }
0126 
0127 void KoShapeFactoryBase::setToolTip(const QString & tooltip)
0128 {
0129     d->tooltip = tooltip;
0130 }
0131 
0132 void KoShapeFactoryBase::setIconName(const QString &iconName)
0133 {
0134     d->iconName = iconName;
0135 }
0136 
0137 void KoShapeFactoryBase::setFamily(const QString & family)
0138 {
0139     d->family = family;
0140 }
0141 
0142 QString KoShapeFactoryBase::id() const
0143 {
0144     return d->id;
0145 }
0146 
0147 void KoShapeFactoryBase::setOptionPanels(const QList<KoShapeConfigFactoryBase*> &panelFactories)
0148 {
0149     d->configPanels = panelFactories;
0150 }
0151 
0152 QList<KoShapeConfigFactoryBase*> KoShapeFactoryBase::panelFactories() const
0153 {
0154     return d->configPanels;
0155 }
0156 
0157 QList<KoShapeTemplate> KoShapeFactoryBase::templates() const
0158 {
0159     return d->templates;
0160 }
0161 
0162 void KoShapeFactoryBase::setLoadingPriority(int priority)
0163 {
0164     d->loadingPriority = priority;
0165 }
0166 
0167 void KoShapeFactoryBase::setXmlElementNames(const QString & nameSpace, const QStringList & names)
0168 {
0169     d->xmlElements.clear();
0170     d->xmlElements.append(QPair<QString, QStringList>(nameSpace, names));
0171 }
0172 
0173 void KoShapeFactoryBase::setXmlElements(const QList<QPair<QString, QStringList> > &elementNamesList)
0174 {
0175     d->xmlElements = elementNamesList;
0176 }
0177 
0178 bool KoShapeFactoryBase::hidden() const
0179 {
0180     return d->hidden;
0181 }
0182 
0183 void KoShapeFactoryBase::setHidden(bool hidden)
0184 {
0185     d->hidden = hidden;
0186 }
0187 
0188 void KoShapeFactoryBase::newDocumentResourceManager(KoDocumentResourceManager *manager) const
0189 {
0190     d->resourceManagers.append(manager);
0191     connect(manager, SIGNAL(destroyed(QObject*)), this, SLOT(pruneDocumentResourceManager(QObject*)));
0192 }
0193 
0194 QList<KoDocumentResourceManager *> KoShapeFactoryBase::documentResourceManagers() const
0195 {
0196     return d->resourceManagers;
0197 }
0198 
0199 KoShape *KoShapeFactoryBase::createDefaultShape(KoDocumentResourceManager *documentResources) const
0200 {
0201     if (!d->deferredPluginName.isEmpty()) {
0202         const_cast<KoShapeFactoryBase*>(this)->getDeferredPlugin();
0203         Q_ASSERT(d->deferredFactory);
0204         if (d->deferredFactory) {
0205             return d->deferredFactory->createDefaultShape(documentResources);
0206         }
0207     }
0208     return 0;
0209 }
0210 
0211 KoShape *KoShapeFactoryBase::createShape(const KoProperties* properties,
0212                                          KoDocumentResourceManager *documentResources) const
0213 {
0214     if (!d->deferredPluginName.isEmpty()) {
0215         const_cast<KoShapeFactoryBase*>(this)->getDeferredPlugin();
0216         Q_ASSERT(d->deferredFactory);
0217         if (d->deferredFactory) {
0218             return d->deferredFactory->createShape(properties, documentResources);
0219         }
0220     }
0221     return createDefaultShape(documentResources);
0222 }
0223 
0224 KoShape *KoShapeFactoryBase::createShapeFromOdf(const KoXmlElement &element, KoShapeLoadingContext &context)
0225 {
0226     KoShape *shape = createDefaultShape(context.documentResourceManager());
0227     if (!shape)
0228         return 0;
0229 
0230     if (shape->shapeId().isEmpty())
0231         shape->setShapeId(id());
0232 
0233     context.odfLoadingContext().styleStack().save();
0234     bool loaded = shape->loadOdf(element, context);
0235     context.odfLoadingContext().styleStack().restore();
0236 
0237     if (!loaded) {
0238         delete shape;
0239         return 0;
0240     }
0241 
0242     return shape;
0243 }
0244 
0245 void KoShapeFactoryBase::getDeferredPlugin()
0246 {
0247     QMutexLocker(&d->pluginLoadingMutex);
0248     if (d->deferredFactory) return;
0249 
0250     const QList<KPluginFactory *> pluginFactories =
0251         KoPluginLoader::instantiatePluginFactories(QStringLiteral("calligra/deferred"));
0252     Q_ASSERT(pluginFactories.size() > 0);
0253     foreach (KPluginFactory* factory, pluginFactories) {
0254         KoDeferredShapeFactoryBase *plugin = factory->create<KoDeferredShapeFactoryBase>(this, QVariantList());
0255 
0256         if (plugin && plugin->deferredPluginName() == d->deferredPluginName) {
0257             d->deferredFactory = plugin;
0258         } else {
0259             // not our/valid plugin, so delete the created object
0260             plugin->deleteLater();
0261         }
0262     }
0263 
0264 }
0265 
0266 void KoShapeFactoryBase::pruneDocumentResourceManager(QObject *obj)
0267 {
0268     KoDocumentResourceManager *r = qobject_cast<KoDocumentResourceManager*>(obj);
0269     d->resourceManagers.removeAll(r);
0270 }