File indexing completed on 2024-05-12 16:35:53

0001 /* This file is part of the KDE project
0002    Copyright 2006 Stefan Nikolaus <stefan.nikolaus@kdemail.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 
0020 // Local
0021 #include "TableShapeDeferredFactory.h"
0022 
0023 #include <QStringList>
0024 
0025 #include <kpluginfactory.h>
0026 
0027 #include <KoDocumentResourceManager.h>
0028 #include <KoToolRegistry.h>
0029 #include <KoShapeLoadingContext.h>
0030 #include <KoXmlNS.h>
0031 
0032 #include <Map.h>
0033 
0034 #include "TableShape.h"
0035 #include "TableToolFactory.h"
0036 #include "TableShapeFactory.h"
0037 
0038 using namespace Calligra::Sheets;
0039 
0040 K_PLUGIN_FACTORY_WITH_JSON(TableShapePluginFactory, "calligra_shape_spreadsheet-deferred.json",
0041                            registerPlugin<TableDeferredShapeFactory>();)
0042 
0043 TableDeferredShapeFactory::TableDeferredShapeFactory(QObject *parent, const QVariantList&)
0044         : KoDeferredShapeFactoryBase(parent)
0045 {
0046     // only create the tool when this plugin gets loaded.
0047     KoToolRegistry::instance()->addDeferred(new TableToolFactory());
0048 
0049     m_stubFactory = qobject_cast<KoShapeFactoryBase*>(parent);
0050 }
0051 
0052 TableDeferredShapeFactory::~TableDeferredShapeFactory()
0053 {
0054 }
0055 
0056 KoShape *TableDeferredShapeFactory::createDefaultShape(KoDocumentResourceManager *documentResources) const
0057 {
0058     QList<KoDocumentResourceManager *>resourceManagers = m_stubFactory->documentResourceManagers();
0059     foreach(KoDocumentResourceManager *documentResources, resourceManagers) {
0060         if (!documentResources->hasResource(MapResourceId)) {
0061             // One spreadsheet map for all inserted tables to allow referencing cells among them.
0062             QVariant variant;
0063             Map* map = new Map();
0064             // Make the KoDocumentResourceManager manage this Map, since we cannot delete it ourselves
0065             map->setParent(documentResources);
0066             QObject::connect(documentResources, SIGNAL(destroyed()), map, SLOT(deleteLater()));
0067             variant.setValue<void*>(map);
0068             documentResources->setResource(MapResourceId, variant);
0069         }
0070     }
0071 
0072     TableShape *shape = new TableShape();
0073     shape->setShapeId(TableShapeId);
0074     if (documentResources) {
0075         Q_ASSERT(documentResources->hasResource(MapResourceId));
0076         Map *map = static_cast<Map*>(documentResources->resource(MapResourceId).value<void*>());
0077         shape->setMap(map);
0078     }
0079     return shape;
0080 }
0081 
0082 #include "TableShapeDeferredFactory.moc"