Krita Source Code Documentation
Loading...
Searching...
No Matches
Shape.cpp
Go to the documentation of this file.
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>
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
29};
30
31Shape::Shape(KoShape *shape, QObject *parent)
32 : QObject(parent)
33 , d(new Private)
34{
35 d->shape = shape;
36}
37
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
53QString Shape::name() const
54{
55 return d->shape->name();
56}
57
58void Shape::setName(const QString &name)
59{
61}
62
63QString Shape::type() const
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{
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{
106}
107
108QRectF Shape::boundingBox() const
109{
110 return d->shape->boundingRect();
111}
112
113QPointF Shape::position() const
114{
115 return d->shape->position();
116}
117
118void Shape::setPosition(QPointF point)
119{
120 d->shape->setPosition(point);
121}
122
123QTransform Shape::transformation() const
124{
125 return d->shape->transformation();
126}
127
128void Shape::setTransformation(QTransform matrix)
129{
130 d->shape->setTransformation(matrix);
131}
132
134{
135 return d->shape->absoluteTransformation();
136}
137
139{
140 return d->shape->update();
141}
142
143void Shape::updateAbsolute(QRectF box)
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
198 KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
199
200 selection->select(d->shape);
201}
202
204{
205 if (!d->shape) return;
206
208 KoSelection *selection = activeView->canvasBase()->shapeManager()->selection();
209
210 selection->deselect(d->shape);
211}
212
214{
215 if (!d->shape) return false;
216
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
237{
238 return d->shape;
239}
KoShapeManager shapeManager
QPointer< KisView > activeView
static KisPart * instance()
Definition KisPart.cpp:131
KisMainWindow * currentMainwindow() const
Definition KisPart.cpp:483
QList< QPointer< KisView > > views
Definition KisPart.cpp:106
static void runSingleCommandStroke(KisImageSP image, KUndo2Command *cmd, KisStrokeJobData::Sequentiality sequentiality=KisStrokeJobData::SEQUENTIAL, KisStrokeJobData::Exclusivity exclusivity=KisStrokeJobData::NORMAL)
runSingleCommandStroke creates a stroke and runs cmd in it. The text() field of cmd is used as a titl...
KisCanvas2 * canvasBase() const
Definition KisView.cpp:427
bool isSelected(const KoShape *shape) const
return true if the shape is selected
void deselect(KoShape *shape)
void select(KoShape *shape)
KoSelection * selection
void setName(const QString &name)
Definition KoShape.cpp:1155
void setZIndex(qint16 zIndex)
Definition KoShape.cpp:954
QString shapeId() const
Definition KoShape.cpp:1057
bool isSelectable() const
Definition KoShape.cpp:1014
bool isGeometryProtected() const
Definition KoShape.cpp:1024
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:335
void setSelectable(bool selectable)
Definition KoShape.cpp:1009
virtual void update() const
Definition KoShape.cpp:605
KoShapeContainer * parent() const
Definition KoShape.cpp:1039
QTransform absoluteTransformation() const
Definition KoShape.cpp:382
void setTransformation(const QTransform &matrix)
Definition KoShape.cpp:417
QTransform transformation() const
Returns the shapes local transformation matrix.
Definition KoShape.cpp:424
virtual void setPosition(const QPointF &position)
Set the position of the shape in pt.
Definition KoShape.cpp:295
virtual void updateAbsolute(const QRectF &rect) const
Definition KoShape.cpp:616
void setGeometryProtected(bool on)
Definition KoShape.cpp:1019
void setVisible(bool on)
Definition KoShape.cpp:972
qint16 zIndex() const
Definition KoShape.cpp:600
QString name() const
Definition KoShape.cpp:1150
bool isVisible(bool recursive=true) const
Definition KoShape.cpp:979
QPointF position() const
Get the position of the shape in pt.
Definition KoShape.cpp:825
Document * activeDocument() const
Definition Krita.cpp:104
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:390
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 operator==(const Shape &other) const
Definition Shape.cpp:43
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
bool operator!=(const Shape &other) const
Definition Shape.cpp:48
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
friend class GroupShape
Definition Shape.h:202
KoShape * shape()
Definition Shape.cpp:236
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
Private *const d
Definition Shape.h:206
~Shape()
Definition Shape.cpp:38
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
Shape(KoShape *shape, QObject *parent=0)
Definition Shape.cpp:31
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
Context for saving svg files.
void setStrippedTextMode(bool value)
Implements exporting shapes to SVG.
Definition SvgWriter.h:33
bool saveDetached(QIODevice &outputDevice)
KoShape * shape
Definition Shape.cpp:28