Krita

Shape.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <[email protected]>
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 
26 struct Shape::Private {
27  Private() {}
28  KoShape *shape {0};
29 };
30 
31 Shape::Shape(KoShape *shape, QObject *parent)
32  : QObject(parent)
33  , d(new Private)
34 {
35  d->shape = shape;
36 }
37 
38 Shape::~Shape()
39 {
40  delete d;
41 }
42 
43 bool Shape::operator==(const Shape &other) const
44 {
45  return (d->shape == other.d->shape);
46 }
47 
48 bool Shape::operator!=(const Shape &other) const
49 {
50  return !(operator==(other));
51 }
52 
54 {
55  return d->shape->name();
56 }
57 
58 void Shape::setName(const QString &name)
59 {
60  d->shape->setName(name);
61 }
62 
64 {
65  return d->shape->shapeId();
66 }
67 
68 int Shape::zIndex() const
69 {
70  return d->shape->zIndex();
71 }
72 
73 void Shape::setZIndex(int zindex)
74 {
75  d->shape->setZIndex(zindex);
76 }
77 
78 bool Shape::selectable() const
79 {
80  return d->shape->isSelectable();
81 }
82 
83 void Shape::setSelectable(bool selectable)
84 {
85  d->shape->setSelectable(selectable);
86 }
87 
89 {
90  return d->shape->isGeometryProtected();
91 }
92 
93 void Shape::setGeometryProtected(bool protect)
94 {
95  d->shape->setGeometryProtected(protect);
96 }
97 
98 bool Shape::visible() const
99 {
100  return d->shape->isVisible();
101 }
102 
103 void 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 
172 QString 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 
236 KoShape *Shape::shape()
237 {
238  return d->shape;
239 }
QString toSvg(bool prependStyles=false, bool stripTextMode=true)
toSvg convert the shape to svg, will not include style definitions.
Definition: Shape.cpp:172
void updateAbsolute(QRectF box)
updateAbsolute queue the shape update in the specified rectangle.
Definition: Shape.cpp:143
virtual bool open(QIODevice::OpenMode flags) override
QString fromUtf8(const char *str, int size)
void setSelectable(bool selectable)
setSelectable
Definition: Shape.cpp:83
const QByteArray & data() const const
QTransform absoluteTransformation() const
transformation the 2D transformation matrix of the shape including all grandparent transforms.
Definition: Shape.cpp:133
void setZIndex(int zindex)
setZIndex
Definition: Shape.cpp:73
The Document class encapsulates a Krita Document/Image.
Definition: Document.h:33
The Shape class The shape class is a wrapper around Krita's vector objects.
Definition: Shape.h:37
bool geometryProtected() const
geometryProtected
Definition: Shape.cpp:88
void setPosition(QPointF point)
setPosition set the position of the shape.
Definition: Shape.cpp:118
virtual QString type() const
type
Definition: Shape.cpp:63
QString name() const
name
Definition: Shape.cpp:53
void select()
select selects the shape.
Definition: Shape.cpp:193
bool isSelected()
isSelected
Definition: Shape.cpp:213
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition: Krita.cpp:402
QTransform transformation() const
transformation the 2D transformation matrix of the shape.
Definition: Shape.cpp:123
void setVisible(bool visible)
setVisible
Definition: Shape.cpp:103
QPointF position() const
position the position of the shape in points.
Definition: Shape.cpp:113
The GroupShape class A group shape is a vector object with child shapes.
Definition: GroupShape.h:20
void setName(const QString &name)
setName
Definition: Shape.cpp:58
QRectF boundingBox() const
boundingBox the bounding box of the shape in points
Definition: Shape.cpp:108
virtual void close() override
Shape * parentShape() const
parentShape
Definition: Shape.cpp:223
int zIndex() const
zIndex
Definition: Shape.cpp:68
void deselect()
deselect deselects the shape.
Definition: Shape.cpp:203
Document * activeDocument() const
Definition: Krita.cpp:104
bool remove()
remove delete the shape.
Definition: Shape.cpp:148
bool selectable() const
selectable
Definition: Shape.cpp:78
void setTransformation(QTransform matrix)
setTransformation set the 2D transformation matrix of the shape.
Definition: Shape.cpp:128
void setGeometryProtected(bool protect)
setGeometryProtected
Definition: Shape.cpp:93
void update()
update queue the shape update.
Definition: Shape.cpp:138
bool visible() const
visible
Definition: Shape.cpp:98
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Sep 22 2023 04:09:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.