libs/flake
KoShapePaste.cppGo to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
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 }
|