libs/flake

KoShapeLoadingContext.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
00003    Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "KoShapeLoadingContext.h"
00022 #include "KoShape.h"
00023 #include "KoShapeContainer.h"
00024 #include "KoSharedLoadingData.h"
00025 #include "KoShapeControllerBase.h"
00026 #include "KoDataCenter.h"
00027 #include "KoImageCollection.h"
00028 #include "KoLoadingShapeUpdater.h"
00029 
00030 #include <kdebug.h>
00031 
00032 uint qHash(const KoShapeLoadingContext::AdditionalAttributeData & attributeData)
00033 {
00034     return qHash(attributeData.name);
00035 }
00036 
00037 static QSet<KoShapeLoadingContext::AdditionalAttributeData> s_additionlAttributes;
00038 
00039 class KoShapeLoadingContext::Private
00040 {
00041 public:
00042     Private(KoOdfLoadingContext &c, const QMap<QString, KoDataCenter *> & dataCenterMap)
00043             : context(c)
00044             , zIndex(0)
00045             , dataCenterMap(dataCenterMap) {}
00046     ~Private() {
00047         foreach(KoSharedLoadingData * data, sharedData) {
00048             delete data;
00049         }
00050     }
00051     KoOdfLoadingContext &context;
00052     QMap<QString, KoShapeLayer*> layers;
00053     QMap<QString, KoShape*> drawIds;
00054     QMap<QString, KoSharedLoadingData*> sharedData;
00055     int zIndex;
00056     QMap<QString, KoDataCenter *> dataCenterMap;
00057     QMap<QString, KoLoadingShapeUpdater*> updaterById;
00058     QMap<KoShape *, KoLoadingShapeUpdater*> updaterByShape;
00059 };
00060 
00061 KoShapeLoadingContext::KoShapeLoadingContext(KoOdfLoadingContext & context, const QMap<QString, KoDataCenter *> & dataCenterMap)
00062         : d(new Private(context, dataCenterMap))
00063 {
00064 }
00065 
00066 KoShapeLoadingContext::~KoShapeLoadingContext()
00067 {
00068     delete d;
00069 }
00070 
00071 KoOdfLoadingContext & KoShapeLoadingContext::odfLoadingContext()
00072 {
00073     return d->context;
00074 }
00075 
00076 KoShapeLayer * KoShapeLoadingContext::layer(const QString & layerName)
00077 {
00078     return d->layers.value(layerName, 0);
00079 }
00080 
00081 void KoShapeLoadingContext::addLayer(KoShapeLayer * layer, const QString & layerName)
00082 {
00083     d->layers[ layerName ] = layer;
00084 }
00085 
00086 void KoShapeLoadingContext::clearLayers()
00087 {
00088     d->layers.clear();
00089 }
00090 
00091 void KoShapeLoadingContext::addShapeId(KoShape * shape, const QString & id)
00092 {
00093     d->drawIds.insert(id, shape);
00094     QMap<QString, KoLoadingShapeUpdater*>::iterator it(d->updaterById.find(id));
00095     while (it != d->updaterById.end() && it.key() == id) {
00096         d->updaterByShape.insertMulti(shape, it.value());
00097         it = d->updaterById.erase(it);
00098     }
00099 }
00100 
00101 KoShape * KoShapeLoadingContext::shapeById(const QString & id)
00102 {
00103     return d->drawIds.value(id, 0);
00104 }
00105 
00106 // TODO make sure to remove the shape from the loading context when loading for it failed and it was deleted. This can also happen when the parent is deleted
00107 void KoShapeLoadingContext::updateShape(const QString & id, KoLoadingShapeUpdater * shapeUpdater)
00108 {
00109     d->updaterById.insertMulti(id, shapeUpdater);
00110 }
00111 
00112 void KoShapeLoadingContext::shapeLoaded(KoShape * shape)
00113 {
00114     QMap<KoShape*, KoLoadingShapeUpdater*>::iterator it(d->updaterByShape.find(shape));
00115     while (it != d->updaterByShape.end() && it.key() == shape) {
00116         it.value()->update(shape);
00117         delete it.value();
00118         it = d->updaterByShape.erase(it);
00119     }
00120 }
00121 
00122 KoImageCollection * KoShapeLoadingContext::imageCollection()
00123 {
00124     return dynamic_cast<KoImageCollection*>(d->dataCenterMap.value("ImageCollection", 0));
00125 }
00126 
00127 int KoShapeLoadingContext::zIndex()
00128 {
00129     return d->zIndex++;
00130 }
00131 
00132 void KoShapeLoadingContext::setZIndex(int index)
00133 {
00134     d->zIndex = index;
00135 }
00136 
00137 void KoShapeLoadingContext::addSharedData(const QString & id, KoSharedLoadingData * data)
00138 {
00139     QMap<QString, KoSharedLoadingData*>::iterator it(d->sharedData.find(id));
00140     // data will not be overwritten
00141     if (it == d->sharedData.end()) {
00142         d->sharedData.insert(id, data);
00143     } else {
00144         kWarning(30006) << "The id" << id << "is already registered. Data not inserted";
00145         Q_ASSERT(it == d->sharedData.end());
00146     }
00147 }
00148 
00149 KoSharedLoadingData * KoShapeLoadingContext::sharedData(const QString & id) const
00150 {
00151     KoSharedLoadingData * data = 0;
00152     QMap<QString, KoSharedLoadingData*>::const_iterator it(d->sharedData.find(id));
00153     if (it != d->sharedData.constEnd()) {
00154         data = it.value();
00155     }
00156     return data;
00157 }
00158 
00159 void KoShapeLoadingContext::addAdditionalAttributeData(const AdditionalAttributeData & attributeData)
00160 {
00161     s_additionlAttributes.insert(attributeData);
00162 }
00163 
00164 QSet<KoShapeLoadingContext::AdditionalAttributeData> KoShapeLoadingContext::additionalAttributeData()
00165 {
00166     return s_additionlAttributes;
00167 }
00168 
00169 KoDataCenter * KoShapeLoadingContext::dataCenter(const QString & dataCenterName)
00170 {
00171     return d->dataCenterMap.value(dataCenterName, 0);
00172 }
00173 
00174 QMap<QString, KoDataCenter *> KoShapeLoadingContext::dataCenterMap() const
00175 {
00176     return d->dataCenterMap;
00177 }