Warning, file /office/calligra/libs/flake/KoShapePaste.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) 2007 Thorsten Zachmann <zachmann@kde.org> 0003 Copyright (C) 2009 Thomas Zander <zander@kde.org> 0004 Copyright (C) 2010-2011 Jan Hambrecht <jaham@gmx.net> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or (at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #include "KoShapePaste.h" 0023 0024 #include <FlakeDebug.h> 0025 #include <klocalizedstring.h> 0026 0027 #include <KoOdfLoadingContext.h> 0028 #include <KoOdfReadStore.h> 0029 0030 #include "KoCanvasBase.h" 0031 #include "KoShapeController.h" 0032 #include "KoShape.h" 0033 #include "KoShapeLayer.h" 0034 #include "KoShapeLoadingContext.h" 0035 #include "KoShapeManager.h" 0036 #include "KoShapeBasedDocumentBase.h" 0037 #include "KoShapeRegistry.h" 0038 #include "KoCanvasController.h" 0039 #include "KoDocumentResourceManager.h" 0040 #include "commands/KoShapeCreateCommand.h" 0041 0042 class Q_DECL_HIDDEN KoShapePaste::Private 0043 { 0044 public: 0045 Private(KoCanvasBase *cb, KoShapeLayer *l) : canvas(cb), layer(l) {} 0046 0047 KoCanvasBase *canvas; 0048 KoShapeLayer *layer; 0049 QList<KoShape*> pastedShapes; 0050 }; 0051 0052 KoShapePaste::KoShapePaste(KoCanvasBase *canvas, KoShapeLayer *layer) 0053 : d(new Private(canvas, layer)) 0054 { 0055 } 0056 0057 KoShapePaste::~KoShapePaste() 0058 { 0059 delete d; 0060 } 0061 0062 bool KoShapePaste::process(const KoXmlElement & body, KoOdfReadStore & odfStore) 0063 { 0064 d->pastedShapes.clear(); 0065 KoOdfLoadingContext loadingContext(odfStore.styles(), odfStore.store()); 0066 KoShapeLoadingContext context(loadingContext, d->canvas->shapeController()->resourceManager()); 0067 0068 QList<KoShape*> shapes(d->layer ? d->layer->shapes(): d->canvas->shapeManager()->topLevelShapes()); 0069 0070 int zIndex = 0; 0071 if (!shapes.isEmpty()) { 0072 zIndex = shapes.first()->zIndex(); 0073 foreach (KoShape * shape, shapes) { 0074 zIndex = qMax(zIndex, shape->zIndex()); 0075 } 0076 ++zIndex; 0077 } 0078 context.setZIndex(zIndex); 0079 0080 KoDocumentResourceManager *rm = d->canvas->shapeController()->resourceManager(); 0081 Q_ASSERT(rm); 0082 0083 QPointF pasteOffset(rm->pasteOffset(), rm->pasteOffset()); 0084 const bool pasteAtCursor = rm->pasteAtCursor(); 0085 0086 // get hold of the canvas' shape manager 0087 KoShapeManager *sm = d->canvas->shapeManager(); 0088 Q_ASSERT(sm); 0089 0090 // TODO if this is a text create a text shape and load the text inside the new shape. 0091 // create the shape from the clipboard 0092 KoXmlElement element; 0093 forEachElement(element, body) { 0094 debugFlake << "loading shape" << element.localName(); 0095 0096 KoShape * shape = KoShapeRegistry::instance()->createShapeFromOdf(element, context); 0097 if (shape) { 0098 d->pastedShapes << shape; 0099 } 0100 } 0101 0102 if (d->pastedShapes.isEmpty()) 0103 return true; 0104 0105 // position shapes 0106 if (pasteAtCursor) { 0107 QRectF bbox; 0108 // determine bounding rect of all pasted shapes 0109 foreach (KoShape *shape, d->pastedShapes) { 0110 if (bbox.isEmpty()) 0111 bbox = shape->boundingRect(); 0112 else 0113 bbox |= shape->boundingRect(); 0114 } 0115 // where is the cursor now? 0116 QWidget *canvasWidget = d->canvas->canvasWidget(); 0117 KoCanvasController *cc = d->canvas->canvasController(); 0118 // map mouse screen position to the canvas widget coordinates 0119 QPointF mouseCanvasPos = canvasWidget->mapFromGlobal(QCursor::pos()); 0120 // apply the canvas offset 0121 mouseCanvasPos -= QPoint(cc->canvasOffsetX(), cc->canvasOffsetY()); 0122 // apply offset of document origin 0123 mouseCanvasPos -= d->canvas->documentOrigin(); 0124 // convert to document coordinates 0125 QPointF mouseDocumentPos = d->canvas->viewConverter()->viewToDocument(mouseCanvasPos); 0126 // now we can determine the offset to apply, with the center of the pasted shapes 0127 // bounding rect at the current mouse position 0128 QPointF pasteOffset = mouseDocumentPos - bbox.center(); 0129 foreach (KoShape *shape, d->pastedShapes) { 0130 QPointF move(pasteOffset); 0131 d->canvas->clipToDocument(shape, move); 0132 if (move.x() != 0 || move.y() != 0) { 0133 shape->setPosition(shape->position() + move); 0134 } 0135 } 0136 } else { 0137 foreach (KoShape *shape, d->pastedShapes) { 0138 bool done = true; 0139 do { 0140 // find a nice place for our shape. 0141 done = true; 0142 foreach (const KoShape *s, sm->shapesAt(shape->boundingRect()) + d->pastedShapes) { 0143 if (d->layer && s->parent() != d->layer) 0144 continue; 0145 if (s->name() != shape->name()) 0146 continue; 0147 if (qAbs(s->position().x() - shape->position().x()) > 0.001) 0148 continue; 0149 if (qAbs(s->position().y() - shape->position().y()) > 0.001) 0150 continue; 0151 if (qAbs(s->size().width() - shape->size().width()) > 0.001) 0152 continue; 0153 if (qAbs(s->size().height() - shape->size().height()) > 0.001) 0154 continue; 0155 // move it and redo our iteration. 0156 QPointF move(pasteOffset); 0157 d->canvas->clipToDocument(shape, move); 0158 if (move.x() != 0 || move.y() != 0) { 0159 shape->setPosition(shape->position() + move); 0160 done = false; 0161 break; 0162 } 0163 } 0164 } while (!done); 0165 } 0166 } 0167 0168 KUndo2Command *cmd = new KUndo2Command(kundo2_i18n("Paste Shapes")); 0169 if (!cmd) { 0170 qDeleteAll(d->pastedShapes); 0171 d->pastedShapes.clear(); 0172 return false; 0173 } 0174 0175 // add shapes to the document 0176 foreach (KoShape *shape, d->pastedShapes) { 0177 if (!shape->parent()) { 0178 shape->setParent(d->layer); 0179 } 0180 d->canvas->shapeController()->addShapeDirect(shape, cmd); 0181 } 0182 0183 d->canvas->addCommand(cmd); 0184 0185 return true; 0186 } 0187 0188 QList<KoShape*> KoShapePaste::pastedShapes() const 0189 { 0190 return d->pastedShapes; 0191 }