Krita

Shape.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#include "Shape.h"
7#include <kis_icon_utils.h>
8#include <SvgWriter.h>
9#include <SvgParser.h>
10#include <SvgSavingContext.h>
11#include <QBuffer>
12#include <KoDocumentResourceManager.h>
13#include <kis_processing_applicator.h>
14#include <KisPart.h>
15#include <KisView.h>
16#include <KisDocument.h>
17#include <kis_canvas2.h>
18#include <KisMainWindow.h>
19#include <KoShapeController.h>
20#include <KoSelection.h>
21
22#include "Krita.h"
23#include "Document.h"
24#include "GroupShape.h"
25
26struct Shape::Private {
27 Private() {}
28 KoShape *shape {0};
29};
30
31Shape::Shape(KoShape *shape, QObject *parent)
32 : QObject(parent)
33 , d(new Private)
34{
35 d->shape = shape;
36}
37
38Shape::~Shape()
39{
40 delete d;
41}
42
43bool Shape::operator==(const Shape &other) const
44{
45 return (d->shape == other.d->shape);
46}
47
48bool Shape::operator!=(const Shape &other) const
49{
50 return !(operator==(other));
51}
52
54{
55 return d->shape->name();
56}
57
58void Shape::setName(const QString &name)
59{
60 d->shape->setName(name);
61}
62
64{
65 return d->shape->shapeId();
66}
67
68int Shape::zIndex() const
69{
70 return d->shape->zIndex();
71}
72
73void Shape::setZIndex(int zindex)
74{
75 d->shape->setZIndex(zindex);
76}
77
79{
80 return d->shape->isSelectable();
81}
82
83void Shape::setSelectable(bool selectable)
84{
85 d->shape->setSelectable(selectable);
86}
87
89{
90 return d->shape->isGeometryProtected();
91}
92
94{
95 d->shape->setGeometryProtected(protect);
96}
97
98bool Shape::visible() const
99{
100 return d->shape->isVisible();
101}
102
103void Shape::setVisible(bool visible)
104{
105 d->shape->setVisible(visible);
106}
107
109{
110 return d->shape->boundingRect();
111}
112
114{
115 return d->shape->position();
116}
117
119{
120 d->shape->setPosition(point);
121}
122
124{
125 return d->shape->transformation();
126}
127
129{
130 d->shape->setTransformation(matrix);
131}
132
134{
135 return d->shape->absoluteTransformation();
136}
137
139{
140 return d->shape->update();
141}
142
144{
145 return d->shape->updateAbsolute(box);
146}
147
149{
150 if (!d->shape) return false;
151 if (!d->shape->parent()) return false;
152
153 bool removeStatus = false;
154 Document *document = Krita::instance()->activeDocument();
155
156 if (KisPart::instance()->viewCount(document->document()) > 0) {
157 for (QPointer<KisView> view : KisPart::instance()->views()) {
158 if (view && view->document() == document->document()) {
159 KisProcessingApplicator::runSingleCommandStroke(view->image(), view->canvasBase()->shapeController()->removeShape(d->shape));
160 view->image()->waitForDone();
161 removeStatus = true;
162 break;
163 }
164 }
165 }
166
167 delete document;
168
169 return removeStatus;
170}
171
172QString Shape::toSvg(bool prependStyles, bool stripTextMode)
173{
174 QBuffer shapesBuffer;
175 QBuffer stylesBuffer;
176
177 shapesBuffer.open(QIODevice::WriteOnly);
178 stylesBuffer.open(QIODevice::WriteOnly);
179
180 {
181 SvgSavingContext savingContext(shapesBuffer, stylesBuffer);
182 savingContext.setStrippedTextMode(stripTextMode);
183 SvgWriter writer({d->shape});
184 writer.saveDetached(savingContext);
185 }
186
187 shapesBuffer.close();
188 stylesBuffer.close();
189
190 return (prependStyles ? QString::fromUtf8(stylesBuffer.data()):"") + QString::fromUtf8(shapesBuffer.data());
191}
192
194{
195 if (!d->shape) return;
196
197 KisView *activeView = KisPart::instance()->currentMainwindow()->activeView();
198 KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
199
200 selection->select(d->shape);
201}
202
204{
205 if (!d->shape) return;
206
207 KisView *activeView = KisPart::instance()->currentMainwindow()->activeView();
208 KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
209
210 selection->deselect(d->shape);
211}
212
214{
215 if (!d->shape) return false;
216
217 KisView *activeView = KisPart::instance()->currentMainwindow()->activeView();
218 KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
219
220 return selection->isSelected(d->shape);
221}
222
224{
225 if (!d->shape) return 0;
226 if (!d->shape->parent()) return 0;
227
228 if (dynamic_cast<KoShapeGroup*>(d->shape->parent())) {
229 return new GroupShape(dynamic_cast<KoShapeGroup*>(d->shape->parent()));
230 } else {
231 return 0;
232 }
233}
234
235
236KoShape *Shape::shape()
237{
238 return d->shape;
239}
The Document class encapsulates a Krita Document/Image.
Definition Document.h:37
The GroupShape class A group shape is a vector object with child shapes.
Definition GroupShape.h:21
Document * activeDocument() const
Definition Krita.cpp:104
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:402
The Shape class The shape class is a wrapper around Krita's vector objects.
Definition Shape.h:38
void setVisible(bool visible)
setVisible
Definition Shape.cpp:103
bool remove()
remove delete the shape.
Definition Shape.cpp:148
QTransform absoluteTransformation() const
transformation the 2D transformation matrix of the shape including all grandparent transforms.
Definition Shape.cpp:133
void update()
update queue the shape update.
Definition Shape.cpp:138
QTransform transformation() const
transformation the 2D transformation matrix of the shape.
Definition Shape.cpp:123
bool selectable() const
selectable
Definition Shape.cpp:78
QRectF boundingBox() const
boundingBox the bounding box of the shape in points
Definition Shape.cpp:108
bool isSelected()
isSelected
Definition Shape.cpp:213
void setTransformation(QTransform matrix)
setTransformation set the 2D transformation matrix of the shape.
Definition Shape.cpp:128
virtual QString type() const
type
Definition Shape.cpp:63
int zIndex() const
zIndex
Definition Shape.cpp:68
void setName(const QString &name)
setName
Definition Shape.cpp:58
void setZIndex(int zindex)
setZIndex
Definition Shape.cpp:73
Shape * parentShape() const
parentShape
Definition Shape.cpp:223
void updateAbsolute(QRectF box)
updateAbsolute queue the shape update in the specified rectangle.
Definition Shape.cpp:143
void setPosition(QPointF point)
setPosition set the position of the shape.
Definition Shape.cpp:118
QString name() const
name
Definition Shape.cpp:53
QPointF position() const
position the position of the shape in points.
Definition Shape.cpp:113
QString toSvg(bool prependStyles=false, bool stripTextMode=true)
toSvg convert the shape to svg, will not include style definitions.
Definition Shape.cpp:172
bool geometryProtected() const
geometryProtected
Definition Shape.cpp:88
void setGeometryProtected(bool protect)
setGeometryProtected
Definition Shape.cpp:93
void setSelectable(bool selectable)
setSelectable
Definition Shape.cpp:83
void select()
select selects the shape.
Definition Shape.cpp:193
bool visible() const
visible
Definition Shape.cpp:98
void deselect()
deselect deselects the shape.
Definition Shape.cpp:203
virtual void close() override
const QByteArray & data() const const
virtual bool open(OpenMode flags) override
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:51:16 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.