Warning, file /office/calligra/libs/text/KoTextPaste.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) 2008 Thorsten Zachmann <zachmann@kde.org>
0003  * Copyright (C) 2011 Boudewijn Rempt <boud@valdyas.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoTextPaste.h"
0022 
0023 #include <KoTextDocument.h>
0024 #include <KoOdfReadStore.h>
0025 #include <KoOdfLoadingContext.h>
0026 #include <KoShapeLoadingContext.h>
0027 #include <KoShapeController.h>
0028 #include <KoShape.h>
0029 #include <KoCanvasBase.h>
0030 #include <KoTextEditor.h>
0031 #include <opendocument/KoTextLoader.h>
0032 #include <KoTextSharedLoadingData.h>
0033 #include <KoSectionModel.h>
0034 
0035 #include "TextDebug.h"
0036 #ifdef SHOULD_BUILD_RDF
0037 #include "KoTextRdfCore.h"
0038 #include <Soprano/Soprano>
0039 #endif
0040 
0041 class Q_DECL_HIDDEN KoTextPaste::Private
0042 {
0043 public:
0044     Private(KoTextEditor *editor, KoShapeController *shapeCont, QSharedPointer<Soprano::Model> _rdfModel,
0045         KoCanvasBase *c, KUndo2Command *cmd
0046     )
0047         : editor(editor)
0048         , resourceManager(shapeCont->resourceManager())
0049         , rdfModel(_rdfModel)
0050         , shapeController(shapeCont)
0051         , command(cmd)
0052         , canvas(c)
0053     {
0054     }
0055 
0056     KoTextEditor *editor;
0057     KoDocumentResourceManager *resourceManager;
0058     QSharedPointer<Soprano::Model> rdfModel;
0059     KoShapeController *shapeController;
0060     KUndo2Command *command;
0061     KoCanvasBase *canvas;
0062 };
0063 
0064 KoTextPaste::KoTextPaste(KoTextEditor *editor, KoShapeController *shapeController, QSharedPointer<Soprano::Model> rdfModel, KoCanvasBase *c, KUndo2Command *cmd)
0065         : d(new Private(editor, shapeController, rdfModel, c, cmd))
0066 {
0067 }
0068 
0069 KoTextPaste::~KoTextPaste()
0070 {
0071     delete d;
0072 }
0073 
0074 bool KoTextPaste::process(const KoXmlElement &body, KoOdfReadStore &odfStore)
0075 {
0076     bool ok = true;
0077     KoOdfLoadingContext loadingContext(odfStore.styles(), odfStore.store());
0078     KoShapeLoadingContext context(loadingContext, d->resourceManager);
0079     context.setSectionModel(KoTextDocument(d->editor->document()).sectionModel());
0080 
0081     KoTextLoader loader(context);
0082 
0083     debugText << "text paste";
0084     // load the paste directly into the editor's cursor -- which breaks encapsulation
0085     loader.loadBody(body, *d->editor->cursor(), KoTextLoader::PasteMode);   // now let's load the body from the ODF KoXmlElement.
0086 
0087 //     context.sectionModel()->invalidate(); FIXME!!
0088 
0089 #ifdef SHOULD_BUILD_RDF
0090     debugText << "text paste, rdf handling" << d->rdfModel;
0091     // RDF: Grab RDF metadata from ODF file if present & load it into rdfModel
0092     if (d->rdfModel)
0093     {
0094         QSharedPointer<Soprano::Model> tmpmodel(Soprano::createModel());
0095         ok = KoTextRdfCore::loadManifest(odfStore.store(), tmpmodel);
0096         debugText << "ok:" << ok << " tmpmodel.sz:" << tmpmodel->statementCount();
0097         debugText << "existing rdf model.sz:" << d->rdfModel->statementCount();
0098 #ifndef NDEBUG
0099         KoTextRdfCore::dumpModel("RDF from C+P", tmpmodel);
0100 #endif
0101         d->rdfModel->addStatements(tmpmodel->listStatements().allElements());
0102         debugText << "done... existing rdf model.sz:" << d->rdfModel->statementCount();
0103 #ifndef NDEBUG
0104         KoTextRdfCore::dumpModel("Imported RDF after C+P", d->rdfModel);
0105 #endif
0106     }
0107 #endif
0108 
0109     KoTextSharedLoadingData *sharedData = static_cast<KoTextSharedLoadingData *>(context.sharedData(KOTEXT_SHARED_LOADING_ID));
0110 
0111     // add shapes to the document
0112     foreach (KoShape *shape, sharedData->insertedShapes()) {
0113         QPointF move;
0114         d->canvas->clipToDocument(shape, move);
0115         if (move.x() != 0 || move.y() != 0) {
0116             shape->setPosition(shape->position() + move);
0117         }
0118 
0119         // During load we make page anchored shapes invisible, because otherwise
0120         // they leave empty rects in the text if there is run-around
0121         // now is the time to make them visible again
0122         shape->setVisible(true);
0123 
0124         d->editor->addCommand(d->shapeController->addShapeDirect(shape, d->command));
0125     }
0126 
0127     return ok;
0128 }