File indexing completed on 2024-05-12 15:59:08

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #include "Shape.h"
0007 #include <kis_icon_utils.h>
0008 #include <SvgWriter.h>
0009 #include <SvgParser.h>
0010 #include <SvgSavingContext.h>
0011 #include <QBuffer>
0012 #include <KoDocumentResourceManager.h>
0013 #include <kis_processing_applicator.h>
0014 #include <KisPart.h>
0015 #include <KisView.h>
0016 #include <KisDocument.h>
0017 #include <kis_canvas2.h>
0018 #include <KisMainWindow.h>
0019 #include <KoShapeController.h>
0020 #include <KoSelection.h>
0021 
0022 #include "Krita.h"
0023 #include "Document.h"
0024 
0025 struct Shape::Private {
0026     Private() {}
0027     KoShape *shape {0};
0028 };
0029 
0030 Shape::Shape(KoShape *shape, QObject *parent)
0031     : QObject(parent)
0032     , d(new Private)
0033 {
0034     d->shape = shape;
0035 }
0036 
0037 Shape::~Shape()
0038 {
0039     delete d;
0040 }
0041 
0042 QString Shape::name() const
0043 {
0044     return d->shape->name();
0045 }
0046 
0047 void Shape::setName(const QString &name)
0048 {
0049     d->shape->setName(name);
0050 }
0051 
0052 QString Shape::type() const
0053 {
0054     return d->shape->shapeId();
0055 }
0056 
0057 int Shape::zIndex() const
0058 {
0059     return d->shape->zIndex();
0060 }
0061 
0062 void Shape::setZIndex(int zindex)
0063 {
0064     d->shape->setZIndex(zindex);
0065 }
0066 
0067 bool Shape::selectable() const
0068 {
0069     return d->shape->isSelectable();
0070 }
0071 
0072 void Shape::setSelectable(bool selectable)
0073 {
0074     d->shape->setSelectable(selectable);
0075 }
0076 
0077 bool Shape::geometryProtected() const
0078 {
0079     return d->shape->isGeometryProtected();
0080 }
0081 
0082 void Shape::setGeometryProtected(bool protect)
0083 {
0084     d->shape->setGeometryProtected(protect);
0085 }
0086 
0087 bool Shape::visible() const
0088 {
0089     return d->shape->isVisible();
0090 }
0091 
0092 void Shape::setVisible(bool visible)
0093 {
0094     d->shape->setVisible(visible);
0095 }
0096 
0097 QRectF Shape::boundingBox() const
0098 {
0099     return d->shape->boundingRect();
0100 }
0101 
0102 QPointF Shape::position() const
0103 {
0104     return d->shape->position();
0105 }
0106 
0107 void Shape::setPosition(QPointF point)
0108 {
0109     d->shape->setPosition(point);
0110 }
0111 
0112 QTransform Shape::transformation() const
0113 {
0114     return d->shape->transformation();
0115 }
0116 
0117 void Shape::setTransformation(QTransform matrix)
0118 {
0119     d->shape->setTransformation(matrix);
0120 }
0121 
0122 void Shape::update()
0123 {
0124     return d->shape->update();
0125 }
0126 
0127 void Shape::updateAbsolute(QRectF box)
0128 {
0129     return d->shape->updateAbsolute(box);
0130 }
0131 
0132 bool Shape::remove()
0133 {
0134     if (!d->shape) return false;
0135     if (!d->shape->parent()) return false;
0136 
0137     bool removeStatus = false;
0138     Document *document = Krita::instance()->activeDocument();
0139 
0140     if (KisPart::instance()->viewCount(document->document()) > 0) {
0141         for (QPointer<KisView> view : KisPart::instance()->views()) {
0142             if (view && view->document() == document->document()) {
0143                 KisProcessingApplicator::runSingleCommandStroke(view->image(), view->canvasBase()->shapeController()->removeShape(d->shape));
0144                 view->image()->waitForDone();
0145                 removeStatus = true;
0146                 break;
0147             }
0148         }
0149     }
0150 
0151     delete document;
0152 
0153     return removeStatus;
0154 }
0155 
0156 QString Shape::toSvg(bool prependStyles, bool stripTextMode)
0157 {
0158     QBuffer shapesBuffer;
0159     QBuffer stylesBuffer;
0160 
0161     shapesBuffer.open(QIODevice::WriteOnly);
0162     stylesBuffer.open(QIODevice::WriteOnly);
0163 
0164     {
0165         SvgSavingContext savingContext(shapesBuffer, stylesBuffer);
0166         savingContext.setStrippedTextMode(stripTextMode);
0167         SvgWriter writer({d->shape});
0168         writer.saveDetached(savingContext);
0169     }
0170 
0171     shapesBuffer.close();
0172     stylesBuffer.close();
0173 
0174     return (prependStyles ? QString::fromUtf8(stylesBuffer.data()):"") + QString::fromUtf8(shapesBuffer.data());
0175 }
0176 
0177 void Shape::select()
0178 {
0179     if (!d->shape) return;
0180 
0181     KisView *activeView = KisPart::instance()->currentMainwindow()->activeView();
0182     KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
0183 
0184     selection->select(d->shape);
0185 }
0186 
0187 void Shape::deselect()
0188 {
0189     if (!d->shape) return;
0190 
0191     KisView *activeView = KisPart::instance()->currentMainwindow()->activeView();
0192     KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
0193 
0194     selection->deselect(d->shape);
0195 }
0196 
0197 bool Shape::isSelected()
0198 {
0199     if (!d->shape) return false;
0200 
0201     KisView *activeView = KisPart::instance()->currentMainwindow()->activeView();
0202     KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
0203 
0204     return selection->isSelected(d->shape);
0205 }
0206 
0207 KoShape *Shape::shape()
0208 {
0209     return d->shape;
0210 }