File indexing completed on 2024-05-12 15:56:40

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2007-2008 Thorsten Zachmann <zachmann@kde.org>
0003  * SPDX-FileCopyrightText: 2009 Thomas Zander <zander@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "KoDrag.h"
0009 
0010 #include <QApplication>
0011 #include <QBuffer>
0012 #include <QByteArray>
0013 #include <QClipboard>
0014 #include <QMimeData>
0015 #include <QString>
0016 #include <QTransform>
0017 
0018 #include <FlakeDebug.h>
0019 
0020 #include <KoStore.h>
0021 #include <KoXmlWriter.h>
0022 #include "KoShapeSavingContext.h"
0023 
0024 #include <KoShapeContainer.h>
0025 #include <KoShape.h>
0026 
0027 #include <QRect>
0028 #include <SvgWriter.h>
0029 
0030 
0031 class KoDragPrivate {
0032 public:
0033     KoDragPrivate() : mimeData(0) { }
0034     ~KoDragPrivate() { delete mimeData; }
0035     QMimeData *mimeData;
0036 };
0037 
0038 KoDrag::KoDrag()
0039     : d(new KoDragPrivate())
0040 {
0041 }
0042 
0043 KoDrag::~KoDrag()
0044 {
0045     delete d;
0046 }
0047 
0048 bool KoDrag::setSvg(const QList<KoShape *> originalShapes)
0049 {
0050     QRectF boundingRect;
0051     QList<KoShape*> shapes;
0052 
0053     Q_FOREACH (KoShape *shape, originalShapes) {
0054         boundingRect |= shape->boundingRect();
0055 
0056         KoShape *clonedShape = shape->cloneShape();
0057 
0058         /**
0059          * The shape is cloned without its parent's transformation, so we should
0060          * adjust it manually.
0061          */
0062         KoShape *oldParentShape = shape->parent();
0063         if (oldParentShape) {
0064             clonedShape->applyAbsoluteTransformation(oldParentShape->absoluteTransformation());
0065         }
0066 
0067         shapes.append(clonedShape);
0068     }
0069 
0070     std::sort(shapes.begin(), shapes.end(), KoShape::compareShapeZIndex);
0071 
0072     QBuffer buffer;
0073     QLatin1String mimeType("image/svg+xml");
0074 
0075     buffer.open(QIODevice::WriteOnly);
0076 
0077     const QSizeF pageSize(boundingRect.right(), boundingRect.bottom());
0078     SvgWriter writer(shapes);
0079     writer.save(buffer, pageSize);
0080 
0081     buffer.close();
0082 
0083     qDeleteAll(shapes);
0084 
0085     setData(mimeType, buffer.data());
0086     return true;
0087 }
0088 
0089 void KoDrag::setData(const QString &mimeType, const QByteArray &data)
0090 {
0091     if (d->mimeData == 0) {
0092         d->mimeData = new QMimeData();
0093     }
0094     d->mimeData->setData(mimeType, data);
0095 }
0096 
0097 void KoDrag::addToClipboard()
0098 {
0099     if (d->mimeData) {
0100         QApplication::clipboard()->setMimeData(d->mimeData);
0101         d->mimeData = 0;
0102     }
0103 }
0104 
0105 QMimeData * KoDrag::mimeData()
0106 {
0107     QMimeData *mimeData = d->mimeData;
0108     d->mimeData = 0;
0109     return mimeData;
0110 }