libs/flake

KoShapePaste.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) 2009 Thomas Zander <zander@kde.org>
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 "KoShapePaste.h"
00022 
00023 #include <kdebug.h>
00024 #include <klocale.h>
00025 
00026 #include <KoOdfLoadingContext.h>
00027 #include <KoOdfReadStore.h>
00028 
00029 #include "KoCanvasBase.h"
00030 #include "KoShapeController.h"
00031 #include "KoShape.h"
00032 #include "KoShapeLayer.h"
00033 #include "KoShapeLoadingContext.h"
00034 #include "KoShapeManager.h"
00035 #include "KoShapeControllerBase.h"
00036 #include "KoShapeRegistry.h"
00037 #include "commands/KoShapeCreateCommand.h"
00038 
00039 struct KoShapePaste::Private {
00040     Private(KoCanvasBase *cb, KoShapeLayer *l) : canvas(cb), layer(l) {}
00041 
00042     KoCanvasBase *canvas;
00043     KoShapeLayer *layer;
00044     QList<KoShape*> pastedShapes;
00045 };
00046 
00047 KoShapePaste::KoShapePaste(KoCanvasBase *canvas, KoShapeLayer *layer)
00048         : d(new Private(canvas, layer))
00049 {
00050 }
00051 
00052 KoShapePaste::~KoShapePaste()
00053 {
00054     delete d;
00055 }
00056 
00057 bool KoShapePaste::process(const KoXmlElement & body, KoOdfReadStore & odfStore)
00058 {
00059     d->pastedShapes.clear();
00060     KoOdfLoadingContext loadingContext(odfStore.styles(), odfStore.store());
00061     KoShapeLoadingContext context(loadingContext, d->canvas->shapeController()->dataCenterMap());
00062 
00063     QList<KoShape*> shapes(d->layer ? d->layer->childShapes(): d->canvas->shapeManager()->topLevelShapes());
00064 
00065     int zIndex = 0;
00066     if (!shapes.isEmpty()) {
00067         zIndex = shapes.first()->zIndex();
00068         foreach (KoShape * shape, shapes) {
00069             zIndex = qMax(zIndex, shape->zIndex());
00070         }
00071         ++zIndex;
00072     }
00073     context.setZIndex(zIndex);
00074 
00075     QUndoCommand *cmd = 0;
00076 
00077     // TODO if this is a text create a text shape and load the text inside the new shape.
00078     KoXmlElement element;
00079     forEachElement(element, body) {
00080         kDebug(30006) << "loading shape" << element.localName();
00081 
00082         KoShape * shape = KoShapeRegistry::instance()->createShapeFromOdf(element, context);
00083         if (shape) {
00084             if (!cmd)
00085                 cmd = new QUndoCommand(i18n("Paste Shapes"));
00086             if (! shape->parent()) {
00087                 shape->setParent(d->layer);
00088             }
00089             KoShapeManager *sm = d->canvas->shapeManager();
00090             Q_ASSERT(sm);
00091             bool done = true;
00092             do {
00093                 // find a nice place for our shape.
00094                 done = true;
00095                 foreach (const KoShape *s, sm->shapesAt(shape->boundingRect()) + d->pastedShapes) {
00096                     if (d->layer && s->parent() != d->layer)
00097                         continue;
00098                     if (s->name() != shape->name())
00099                         continue;
00100                     if (qAbs(s->position().x() - shape->position().x()) > 0.001)
00101                         continue;
00102                     if (qAbs(s->position().y() - shape->position().y()) > 0.001)
00103                         continue;
00104                     if (qAbs(s->size().width() - shape->size().width()) > 0.001)
00105                         continue;
00106                     if (qAbs(s->size().height() - shape->size().height()) > 0.001)
00107                         continue;
00108                     // move it and redo our iteration.
00109                     QPointF move(10, 10);
00110                     d->canvas->clipToDocument(shape, move);
00111                     if (move.x() != 0 || move.y() != 0) {
00112                         shape->setPosition(shape->position() + move);
00113                         done = false;
00114                         break;
00115                     }
00116                 }
00117             } while (!done);
00118 
00119             d->canvas->shapeController()->addShapeDirect(shape, cmd);
00120             d->pastedShapes << shape;
00121         }
00122     }
00123     if (cmd)
00124         d->canvas->addCommand(cmd);
00125     return true;
00126 }
00127 
00128 QList<KoShape*> KoShapePaste::pastedShapes() const
00129 {
00130     return d->pastedShapes;
00131 }