Krita Source Code Documentation
Loading...
Searching...
No Matches
VectorLayer Class Reference

The VectorLayer class A vector layer is a special layer that stores and shows vector shapes. More...

#include <VectorLayer.h>

+ Inheritance diagram for VectorLayer:

Public Slots

QList< Shape * > addShapesFromSvg (const QString &svg)
 addShapesFromSvg add shapes to the layer from a valid svg.
 
ShapecreateGroupShape (const QString &name, QList< Shape * > shapes) const
 createGroupShape combine a list of top level shapes into a group.
 
bool isAntialiased () const
 return antialiasing status for the Vector layer
 
void setAntialiased (const bool antialiased)
 set antialiasing status for the Vector layer
 
ShapeshapeAtPosition (const QPointF &position) const
 shapeAtPoint check if the position is located within any non-group shape's boundingBox() on the current layer.
 
QList< Shape * > shapes () const
 shapes
 
QList< Shape * > shapesInRect (const QRectF &rect, bool omitHiddenShapes=true, bool containedMode=false) const
 shapeInRect get all non-group shapes that the shape's boundingBox() intersects or is contained within a given rectangle on the current layer.
 
QString toSvg ()
 toSvg convert the shapes in the layer to svg.
 
virtual QString type () const override
 type Krita has several types of nodes, split in layers and masks. Group layers can contain other layers, any layer can contain masks.
 
- Public Slots inherited from Node
bool addChildNode (Node *child, Node *above)
 addChildNode adds the given node in the list of children.
 
bool alphaLocked () const
 alphaLocked checks whether the node is a paint layer and returns whether it is alpha locked
 
bool animated () const
 Krita layers can be animated, i.e., have frames.
 
QString blendingMode () const
 
QRect bounds () const
 bounds return the exact bounds of the node's paint device
 
QList< Channel * > channels () const
 channels creates a list of Channel objects that can be used individually to show or hide certain channels, and to retrieve the contents of each channel in a node separately.
 
QList< Node * > childNodes () const
 childNodes
 
Nodeclone () const
 clone clone the current node. The node is not associated with any image.
 
bool collapsed () const
 
QString colorDepth () const
 
int colorLabel () const
 
QString colorModel () const
 colorModel retrieve the current color model of this document:
 
QString colorProfile () const
 
void cropNode (int x, int y, int w, int h)
 cropNode crop this layer.
 
Nodeduplicate ()
 duplicate returns a full copy of the current node. The node is not inserted in the graphic
 
void enableAnimation () const
 enableAnimation make the current layer animated, so it can have frames.
 
QList< Node * > findChildNodes (const QString &name=QString(), bool recursive=false, bool partialMatch=false, const QString &type=QString(), int colorLabelIndex=0) const
 findChildNodes
 
bool hasExtents ()
 does the node have any content in it?
 
bool hasKeyframeAtTime (int frameNumber)
 
QIcon icon () const
 icon
 
int index () const
 index the index of the node inside the parent
 
bool inheritAlpha () const
 inheritAlpha checks whether this node has the inherits alpha flag set
 
bool isPinnedToTimeline () const
 
QString layerStyleToAsl ()
 layerStyleToAsl retrieve the current layer's style in ASL format.
 
bool locked () const
 locked checks whether the Node is locked. A locked node cannot be changed.
 
NodemergeDown ()
 mergeDown merges the given node with the first visible node underneath this node in the layerstack. This will drop all per-layer metadata.
 
void move (int x, int y)
 
QString name () const
 
int opacity () const
 
QString paintAbility ()
 paintAbility can be used to determine whether this node can be painted on with the current brush preset.
 
void paintEllipse (const QRectF &rect, const QString strokeStyle=PaintingResources::defaultStrokeStyle, const QString fillStyle=PaintingResources::defaultFillStyle)
 paint an ellipse on the canvas. Uses current brush preset
 
void paintLine (const QPointF pointOne, const QPointF pointTwo, double pressureOne=1.0, double pressureTwo=1.0, const QString strokeStyle=PaintingResources::defaultStrokeStyle)
 paint a line on the canvas. Uses current brush preset
 
void paintPath (const QPainterPath &path, const QString strokeStyle=PaintingResources::defaultStrokeStyle, const QString fillStyle=PaintingResources::defaultFillStyle)
 paint a custom path on the canvas. Uses current brush preset
 
void paintPolygon (const QList< QPointF > points, const QString strokeStyle=PaintingResources::defaultStrokeStyle, const QString fillStyle=PaintingResources::defaultFillStyle)
 paint a polygon on the canvas. Uses current brush preset
 
void paintRectangle (const QRectF &rect, const QString strokeStyle=PaintingResources::defaultStrokeStyle, const QString fillStyle=PaintingResources::defaultFillStyle)
 paint a rectangle on the canvas. Uses current brush preset
 
NodeparentNode () const
 
QByteArray pixelData (int x, int y, int w, int h) const
 pixelData reads the given rectangle from the Node's paintable pixels, if those exist, and returns it as a byte array. The pixel data starts top-left, and is ordered row-first.
 
QByteArray pixelDataAtTime (int x, int y, int w, int h, int time) const
 pixelDataAtTime a basic function to get pixeldata from an animated node at a given time.
 
QPoint position () const
 position returns the position of the paint device of this node. The position is always 0,0 unless the layer has been moved. If you want to know the topleft position of the rectangle around the actual non-transparent pixels in the node, use bounds().
 
QByteArray projectionPixelData (int x, int y, int w, int h) const
 projectionPixelData reads the given rectangle from the Node's projection (that is, what the node looks like after all sub-Nodes (like layers in a group or masks on a layer) have been applied, and returns it as a byte array. The pixel data starts top-left, and is ordered row-first.
 
bool remove ()
 remove removes this node from its parent image.
 
bool removeChildNode (Node *child)
 removeChildNode removes the given node from the list of children.
 
void rotateNode (double radians)
 rotateNode rotate this layer by the given radians.
 
bool save (const QString &filename, double xRes, double yRes, const InfoObject &exportConfiguration, const QRect &exportRect=QRect())
 save exports the given node with this filename. The extension of the filename determines the filetype.
 
void scaleNode (QPointF origin, int width, int height, QString strategy)
 scaleNode
 
void setAlphaLocked (bool value)
 setAlphaLocked set the layer to value if the node is paint layer.
 
void setBlendingMode (QString value)
 setBlendingMode set the blending mode of the node to the given value
 
void setChildNodes (QList< Node * > nodes)
 setChildNodes this replaces the existing set of child nodes with the new set.
 
void setCollapsed (bool collapsed)
 
void setColorLabel (int index)
 setColorLabel sets a color label index associated to the layer. The actual color of the label and the number of available colors is defined by Krita GUI configuration.
 
bool setColorProfile (const QString &colorProfile)
 setColorProfile set the color profile of the image to the given profile. The profile has to be registered with krita and be compatible with the current color model and depth; the image data is not converted.
 
bool setColorSpace (const QString &colorModel, const QString &colorDepth, const QString &colorProfile)
 setColorSpace convert the node to the given colorspace
 
void setInheritAlpha (bool value)
 
bool setLayerStyleFromAsl (const QString &asl)
 setLayerStyleFromAsl set a new layer style for this node.
 
void setLocked (bool value)
 
void setName (QString name)
 
void setOpacity (int value)
 
void setPinnedToTimeline (bool pinned) const
 Sets whether or not node should be pinned to the Timeline Docker, regardless of selection activity.
 
bool setPixelData (QByteArray value, int x, int y, int w, int h)
 setPixelData writes the given bytes, of which there must be enough, into the Node, if the Node has writable pixel data:
 
void setVisible (bool visible)
 
void shearNode (double angleX, double angleY)
 shearNode perform a shear operation on this node.
 
QImage thumbnail (int w, int h)
 thumbnail create a thumbnail of the given dimensions. The thumbnail is sized according to the layer dimensions, not the image dimensions. If the requested size is too big a null QImage is created. If the current node cannot generate a thumbnail, a transparent QImage of the requested size is generated.
 
virtual QString type () const
 type Krita has several types of nodes, split in layers and masks. Group layers can contain other layers, any layer can contain masks.
 
QUuid uniqueId () const
 uniqueId uniqueId of the node
 
bool visible () const
 

Public Member Functions

 VectorLayer (KisShapeLayerSP layer, QObject *parent=0)
 
 VectorLayer (KoShapeControllerBase *shapeController, KisImageSP image, QString name, QObject *parent=0)
 
 ~VectorLayer () override
 
- Public Member Functions inherited from Node
bool operator!= (const Node &other) const
 
bool operator== (const Node &other) const
 
 ~Node () override
 

Additional Inherited Members

- Static Public Member Functions inherited from Node
static NodecreateNode (KisImageSP image, KisNodeSP node, QObject *parent=0)
 

Detailed Description

The VectorLayer class A vector layer is a special layer that stores and shows vector shapes.

Vector shapes all have their coordinates in points, which is a unit that represents 1/72th of an inch. Keep this in mind wen parsing the bounding box and position data.

Definition at line 31 of file VectorLayer.h.

Constructor & Destructor Documentation

◆ VectorLayer() [1/2]

VectorLayer::VectorLayer ( KoShapeControllerBase * shapeController,
KisImageSP image,
QString name,
QObject * parent = 0 )
explicit

Definition at line 24 of file VectorLayer.cpp.

24 :
25 Node(image, new KisShapeLayer(shapeController, image, name, OPACITY_OPAQUE_U8), parent)
26{
27
28}
const quint8 OPACITY_OPAQUE_U8
Node(KisImageSP image, KisNodeSP node, QObject *parent=0)
Definition Node.cpp:83
KisImageSP image() const
Definition Node.cpp:822
QString name() const
Definition Node.cpp:428

◆ VectorLayer() [2/2]

VectorLayer::VectorLayer ( KisShapeLayerSP layer,
QObject * parent = 0 )
explicit

Definition at line 30 of file VectorLayer.cpp.

30 :
31 Node(layer->image(), layer, parent)
32{
33
34}
KisImageWSP image

◆ ~VectorLayer()

VectorLayer::~VectorLayer ( )
override

Definition at line 36 of file VectorLayer.cpp.

37{
38
39}

Member Function Documentation

◆ addShapesFromSvg

QList< Shape * > VectorLayer::addShapesFromSvg ( const QString & svg)
slot

addShapesFromSvg add shapes to the layer from a valid svg.

Parameters
svgvalid svg string.
Returns
the list of shapes added to the layer from the svg.

Definition at line 93 of file VectorLayer.cpp.

94{
96 QList<KoShape*> originalShapes;
97
98 if (svgData.isEmpty() || !svgData.contains("<svg") ) {
99 return shapes;
100 }
101
102 KoShapeContainer *container = dynamic_cast<KoShapeContainer*>(this->node().data());
103
104 if (container) {
105 QSizeF fragmentSize;
106 QString errorMsg;
107 int errorLine = 0;
108 int errorColumn = 0;
109
110 QDomDocument dom = SvgParser::createDocumentFromSvg(svgData, &errorMsg, &errorLine, &errorColumn);
111
112 if (dom.isNull()) {
113 qWarning() << "Failed to process an SVG string at"
114 << errorLine << ":" << errorColumn << "->" << errorMsg;
115 return shapes;
116 }
117
118 Document *document = Krita::instance()->activeDocument();
119
120 if (!document) {
121 document = LibKisUtils::findNodeInDocuments(this->node());
122 if (!document) {
123 return shapes;
124 }
125 }
126
127 SvgParser parser(document->document()->shapeController()->resourceManager());
128
129 parser.setResolution(this->node()->image()->bounds(), this->node()->image()->xRes() * 72.0);
130
131 originalShapes = parser.parseSvg(dom.documentElement(), &fragmentSize);
132
133 KUndo2Command *cmd = new KoShapeCreateCommand(document->document()->shapeController(), originalShapes, container);
134
136 this->node()->image()->waitForDone();
137 delete document;
138
139 std::sort(originalShapes.begin(), originalShapes.end(), KoShape::compareShapeZIndex);
140 for (int i=0; i<originalShapes.size(); i++) {
141 if (dynamic_cast<KoShapeGroup*>(originalShapes.at(i))) {
142 shapes << new GroupShape(dynamic_cast<KoShapeGroup*>(originalShapes.at(i)));
143 } else {
144 shapes << new Shape(originalShapes.at(i));
145 }
146 }
147
148 }
149
150 return shapes;
151}
void waitForDone()
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...
The undo / redo command for creating shapes.
static bool compareShapeZIndex(KoShape *s1, KoShape *s2)
Definition KoShape.cpp:434
Document * activeDocument() const
Definition Krita.cpp:104
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:390
KisNodeSP node() const
Definition Node.cpp:827
QRect bounds() const
bounds return the exact bounds of the node's paint device
Definition Node.cpp:615
The Shape class The shape class is a wrapper around Krita's vector objects.
Definition Shape.h:38
static QDomDocument createDocumentFromSvg(QIODevice *device, QString *errorMsg=0, int *errorLine=0, int *errorColumn=0)
QList< Shape * > shapes() const
shapes
Document * findNodeInDocuments(KisNodeSP kisnode)

References Krita::activeDocument(), Node::bounds(), KoShape::compareShapeZIndex(), SvgParser::createDocumentFromSvg(), KisSharedPtr< T >::data(), LibKisUtils::findNodeInDocuments(), KisBaseNode::image, Node::image(), Krita::instance(), Node::node(), SvgParser::parseSvg(), KisProcessingApplicator::runSingleCommandStroke(), SvgParser::setResolution(), shapes(), and KisImage::waitForDone().

◆ createGroupShape

Shape * VectorLayer::createGroupShape ( const QString & name,
QList< Shape * > shapes ) const
slot

createGroupShape combine a list of top level shapes into a group.

Parameters
namethe name of the shape.
shapeslist of top level shapes.
Returns
if successful, a GroupShape object will be returned.

Definition at line 190 of file VectorLayer.cpp.

191{
192 if (shapes.isEmpty()) return 0;
193
194 QList<KoShape *> originalShapes;
195 KoShapeContainer *container = dynamic_cast<KoShapeContainer*>(this->node().data());
196
197 if (!container) return 0;
198
199 for (Shape* shape : shapes) {
200 KoShape *originalShape = shape->shape();
201
202 if (originalShape && originalShape->parent() == container) {
203 originalShapes << originalShape;
204 } else {
205 qWarning() << "Attempt to add an invalid shape.";
206 return 0;
207 }
208 }
209
210 if (originalShapes.isEmpty()) return 0;
211
212 Document *document = Krita::instance()->activeDocument();
213
214 if (!document) {
215 document = LibKisUtils::findNodeInDocuments(this->node());
216 if (!document) return 0;
217 }
218
219 KoShapeGroup *group = new KoShapeGroup();
220 const int groupZIndex = originalShapes.last()->zIndex();
221
222 group->setZIndex(groupZIndex);
223 group->setName(name);
224
225 KUndo2Command *cmd = new KUndo2Command(kundo2_i18n("Group shapes"));
226 new KoShapeCreateCommand(document->document()->shapeController(), group, container, cmd);
227 new KoShapeGroupCommand(group, originalShapes, true, cmd);
228
230 this->node()->image()->waitForDone();
231 delete document;
232
233 return new GroupShape(group);
234}
The GroupShape class A group shape is a vector object with child shapes.
Definition GroupShape.h:21
The undo / redo command for grouping shapes.
void setName(const QString &name)
Definition KoShape.cpp:1155
void setZIndex(qint16 zIndex)
Definition KoShape.cpp:954
KoShapeContainer * parent() const
Definition KoShape.cpp:1039
KUndo2MagicString kundo2_i18n(const char *text)

References Krita::activeDocument(), KisSharedPtr< T >::data(), LibKisUtils::findNodeInDocuments(), KisBaseNode::image, Node::image(), Krita::instance(), kundo2_i18n(), Node::name(), Node::node(), KoShape::parent(), KisProcessingApplicator::runSingleCommandStroke(), KoShape::setName(), KoShape::setZIndex(), shapes(), and KisImage::waitForDone().

◆ isAntialiased

bool VectorLayer::isAntialiased ( ) const
slot

return antialiasing status for the Vector layer

Returns
True if antialiasing is active for the layer

Definition at line 237 of file VectorLayer.cpp.

238{
239 KisShapeLayerSP vectorLayer = KisShapeLayerSP(dynamic_cast<KisShapeLayer*>(this->node().data()));
240 return vectorLayer->antialiased();
241}
bool antialiased() const
KisSharedPtr< KisShapeLayer > KisShapeLayerSP
Definition kis_types.h:125

References KisShapeLayer::antialiased(), and Node::node().

◆ setAntialiased

void VectorLayer::setAntialiased ( const bool antialiased)
slot

set antialiasing status for the Vector layer

Parameters
antialiasedset to True to activate antialiasing

Definition at line 243 of file VectorLayer.cpp.

244{
245 KisShapeLayerSP vectorLayer = KisShapeLayerSP(dynamic_cast<KisShapeLayer*>(this->node().data()));
246 vectorLayer->setAntialiased(antialiased);
247}
void setAntialiased(const bool antialiased)

References Node::node(), and KisShapeLayer::setAntialiased().

◆ shapeAtPosition

Shape * VectorLayer::shapeAtPosition ( const QPointF & position) const
slot

shapeAtPoint check if the position is located within any non-group shape's boundingBox() on the current layer.

Parameters
positiona QPointF of the position.
Returns
the shape at the position, or None if no shape is found.

Definition at line 153 of file VectorLayer.cpp.

154{
155 KisShapeLayerSP vector = KisShapeLayerSP(dynamic_cast<KisShapeLayer*>(this->node().data()));
156
157 if (!vector) return 0;
158
159 KoShape* shape = vector->shapeManager()->shapeAt(position);
160
161 if (!shape) return 0;
162
163 if (dynamic_cast<KoShapeGroup*>(shape)) {
164 return new GroupShape(dynamic_cast<KoShapeGroup*>(shape));
165 } else {
166 return new Shape(shape);
167 }
168
169}
KoShapeManager * shapeManager() const
KoShape * shapeAt(const QPointF &position, KoFlake::ShapeSelection selection=KoFlake::ShapeOnTop, bool omitHiddenShapes=true)
QPoint position() const
position returns the position of the paint device of this node. The position is always 0,...
Definition Node.cpp:628

References Node::node(), Node::position(), KoShapeManager::shapeAt(), and KisShapeLayer::shapeManager().

◆ shapes

QList< Shape * > VectorLayer::shapes ( ) const
slot

shapes

Returns
the list of top-level shapes in this vector layer.

Definition at line 46 of file VectorLayer.cpp.

47{
49 KisShapeLayerSP vector = KisShapeLayerSP(dynamic_cast<KisShapeLayer*>(this->node().data()));
50 if (vector) {
51 QList<KoShape*> originalShapes = vector->shapes();
52 std::sort(originalShapes.begin(), originalShapes.end(), KoShape::compareShapeZIndex);
53 for (int i=0; i<vector->shapeCount(); i++) {
54 if (dynamic_cast<KoShapeGroup*>(originalShapes.at(i))) {
55 shapes << new GroupShape(dynamic_cast<KoShapeGroup*>(originalShapes.at(i)));
56 } else {
57 shapes << new Shape(originalShapes.at(i));
58 }
59 }
60 }
61 return shapes;
62}
QList< KoShape * > shapes() const

References KoShape::compareShapeZIndex(), Node::node(), KoShapeContainer::shapeCount(), KoShapeContainer::shapes(), and shapes().

◆ shapesInRect

QList< Shape * > VectorLayer::shapesInRect ( const QRectF & rect,
bool omitHiddenShapes = true,
bool containedMode = false ) const
slot

shapeInRect get all non-group shapes that the shape's boundingBox() intersects or is contained within a given rectangle on the current layer.

Parameters
recta QRectF
omitHiddenShapestrue if non-visible() shapes should be omitted, false if they should be included. omitHiddenShapes defaults to true.
containedModefalse if only shapes that are within or intersect with the outline should be included, true if only shapes that are fully contained within the outline should be included. containedMode defaults to false
Returns
returns a list of shapes.

Definition at line 171 of file VectorLayer.cpp.

171 {
173 KisShapeLayerSP vector = KisShapeLayerSP(dynamic_cast<KisShapeLayer*>(this->node().data()));
174
175 if (vector) {
176 QList<KoShape *> originalShapes = vector->shapeManager()->shapesAt(rect, omitHiddenShapes, containedMode);
177
178 std::sort(originalShapes.begin(), originalShapes.end(), KoShape::compareShapeZIndex);
179 for (int i=0; i<originalShapes.size(); i++) {
180 if (dynamic_cast<KoShapeGroup*>(originalShapes.at(i))) {
181 shapes << new GroupShape(dynamic_cast<KoShapeGroup*>(originalShapes.at(i)));
182 } else {
183 shapes << new Shape(originalShapes.at(i));
184 }
185 }
186 }
187 return shapes;
188}
QList< KoShape * > shapesAt(const QRectF &rect, bool omitHiddenShapes=true, bool containedMode=false)

References KoShape::compareShapeZIndex(), Node::node(), KisShapeLayer::shapeManager(), shapes(), and KoShapeManager::shapesAt().

◆ toSvg

QString VectorLayer::toSvg ( )
slot

toSvg convert the shapes in the layer to svg.

Returns
the svg in a string.

Definition at line 64 of file VectorLayer.cpp.

65{
66 QString svgData;
67 KisShapeLayerSP vector = KisShapeLayerSP(dynamic_cast<KisShapeLayer*>(this->node().data()));
68
69 if (vector) {
70 QBuffer buffer;
71 QList<KoShape*> originalShapes = vector->shapes();
72
73 std::sort(originalShapes.begin(), originalShapes.end(), KoShape::compareShapeZIndex);
74
75 const QSizeF sizeInPx = this->node()->image()->bounds().size();
76 const QSizeF pageSize(sizeInPx.width() / this->node()->image()->xRes(),
77 sizeInPx.height() / this->node()->image()->yRes());
78
79 buffer.open(QIODevice::WriteOnly);
80
81 SvgWriter writer(originalShapes);
82
83 writer.save(buffer, pageSize);
84 buffer.close();
85
86 svgData = QString::fromUtf8(buffer.data());
87 }
88
89 return svgData;
90
91}
QRect bounds() const override
Implements exporting shapes to SVG.
Definition SvgWriter.h:33

References KisImage::bounds(), KoShape::compareShapeZIndex(), KisBaseNode::image, Node::node(), SvgWriter::save(), and KoShapeContainer::shapes().

◆ type

QString VectorLayer::type ( ) const
overridevirtualslot

type Krita has several types of nodes, split in layers and masks. Group layers can contain other layers, any layer can contain masks.

Returns
vectorlayer

Definition at line 41 of file VectorLayer.cpp.

42{
43 return "vectorlayer";
44}

The documentation for this class was generated from the following files: