File indexing completed on 2024-05-19 04:26:57

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #ifndef LIBKIS_NODE_H
0007 #define LIBKIS_NODE_H
0008 
0009 #include <QObject>
0010 
0011 #include <kis_types.h>
0012 
0013 #include "kritalibkis_export.h"
0014 #include "libkis.h"
0015 
0016 /**
0017  * Node represents a layer or mask in a Krita image's Node hierarchy. Group layers can contain
0018  * other layers and masks; layers can contain masks.
0019  *
0020  */
0021 class KRITALIBKIS_EXPORT Node : public QObject
0022 {
0023     Q_OBJECT
0024     Q_DISABLE_COPY(Node)
0025 
0026 public:
0027     static Node *createNode(KisImageSP image, KisNodeSP node, QObject *parent = 0);
0028     ~Node() override;
0029     bool operator==(const Node &other) const;
0030     bool operator!=(const Node &other) const;
0031 
0032 public Q_SLOTS:
0033 
0034     /**
0035      * @brief clone clone the current node. The node is not associated with any image.
0036      */
0037     Node *clone() const;
0038 
0039     /**
0040      * @brief alphaLocked checks whether the node is a paint layer and returns whether it is alpha locked
0041      * @return whether the paint layer is alpha locked, or false if the node is not a paint layer
0042      */
0043     bool alphaLocked() const;
0044 
0045     /**
0046      * @brief setAlphaLocked set the layer to value if the node is paint layer.
0047      */
0048     void setAlphaLocked(bool value);
0049 
0050     /**
0051      * @return the blending mode of the layer. The values of the blending modes are defined in @see KoCompositeOpRegistry.h
0052      */
0053     QString blendingMode() const;
0054 
0055     /**
0056      * @brief setBlendingMode set the blending mode of the node to the given value
0057      * @param value one of the string values from @see KoCompositeOpRegistry.h
0058      */
0059     void setBlendingMode(QString value);
0060 
0061     /**
0062      * @brief channels creates a list of Channel objects that can be used individually to
0063      * show or hide certain channels, and to retrieve the contents of each channel in a
0064      * node separately.
0065      *
0066      * Only layers have channels, masks do not, and calling channels on a Node that is a mask
0067      * will return an empty list.
0068      *
0069      * @return the list of channels ordered in by position of the channels in pixel position
0070      */
0071     QList<Channel*> channels() const;
0072 
0073     /**
0074      * @brief childNodes
0075      * @return returns a list of child nodes of the current node. The nodes are ordered from the bottommost up.
0076      * The function is not recursive.
0077      */
0078     QList<Node*> childNodes() const;
0079 
0080     /**
0081      * @brief findChildNodes
0082      * @param name name of the child node to search for. Leaving this blank will return all nodes.
0083      * @param recursive whether or not to search recursively. Defaults to false.
0084      * @param partialMatch return if the name partially contains the string (case insensitive). Defaults to false.
0085      * @param type filter returned nodes based on type
0086      * @param colorLabelIndex filter returned nodes based on color label index
0087      * @return returns a list of child nodes and grand child nodes of the current node that match the search criteria.
0088      */
0089     QList<Node*> findChildNodes(const QString &name = QString(), bool recursive = false, bool partialMatch = false, const QString &type = QString(), int colorLabelIndex = 0) const;
0090 
0091     /**
0092      * @brief addChildNode adds the given node in the list of children.
0093      * @param child the node to be added
0094      * @param above the node above which this node will be placed
0095      * @return false if adding the node failed
0096      */
0097     bool addChildNode(Node *child, Node *above);
0098 
0099     /**
0100      * @brief removeChildNode removes the given node from the list of children.
0101      * @param child the node to be removed
0102      */
0103     bool removeChildNode(Node *child);
0104 
0105     /**
0106      * @brief setChildNodes this replaces the existing set of child nodes with the new set.
0107      * @param nodes The list of nodes that will become children, bottom-up -- the first node,
0108      * is the bottom-most node in the stack.
0109      */
0110     void setChildNodes(QList<Node*> nodes);
0111 
0112     /**
0113      * colorDepth A string describing the color depth of the image:
0114      * <ul>
0115      * <li>U8: unsigned 8 bits integer, the most common type</li>
0116      * <li>U16: unsigned 16 bits integer</li>
0117      * <li>F16: half, 16 bits floating point. Only available if Krita was built with OpenEXR</li>
0118      * <li>F32: 32 bits floating point</li>
0119      * </ul>
0120      * @return the color depth.
0121      */
0122     QString colorDepth() const;
0123 
0124     /**
0125      * @brief colorModel retrieve the current color model of this document:
0126      * <ul>
0127      * <li>A: Alpha mask</li>
0128      * <li>RGBA: RGB with alpha channel (The actual order of channels is most often BGR!)</li>
0129      * <li>XYZA: XYZ with alpha channel</li>
0130      * <li>LABA: LAB with alpha channel</li>
0131      * <li>CMYKA: CMYK with alpha channel</li>
0132      * <li>GRAYA: Gray with alpha channel</li>
0133      * <li>YCbCrA: YCbCr with alpha channel</li>
0134      * </ul>
0135      * @return the internal color model string.
0136      */
0137     QString colorModel() const;
0138 
0139     /**
0140      * @return the name of the current color profile
0141      */
0142     QString colorProfile() const;
0143 
0144     /**
0145      * @brief setColorProfile set the color profile of the image to the given profile. The profile has to
0146      * be registered with krita and be compatible with the current color model and depth; the image data
0147      * is <i>not</i> converted.
0148      * @param colorProfile
0149      * @return if assigning the color profile worked
0150      */
0151     bool setColorProfile(const QString &colorProfile);
0152 
0153     /**
0154      * @brief setColorSpace convert the node to the given colorspace
0155      * @param colorModel A string describing the color model of the node:
0156      * <ul>
0157      * <li>A: Alpha mask</li>
0158      * <li>RGBA: RGB with alpha channel (The actual order of channels is most often BGR!)</li>
0159      * <li>XYZA: XYZ with alpha channel</li>
0160      * <li>LABA: LAB with alpha channel</li>
0161      * <li>CMYKA: CMYK with alpha channel</li>
0162      * <li>GRAYA: Gray with alpha channel</li>
0163      * <li>YCbCrA: YCbCr with alpha channel</li>
0164      * </ul>
0165      * @param colorDepth A string describing the color depth of the image:
0166      * <ul>
0167      * <li>U8: unsigned 8 bits integer, the most common type</li>
0168      * <li>U16: unsigned 16 bits integer</li>
0169      * <li>F16: half, 16 bits floating point. Only available if Krita was built with OpenEXR</li>
0170      * <li>F32: 32 bits floating point</li>
0171      * </ul>
0172      * @param colorProfile a valid color profile for this color model and color depth combination.
0173      */
0174     bool setColorSpace(const QString &colorModel, const QString &colorDepth, const QString &colorProfile);
0175 
0176     /**
0177      * @brief Krita layers can be animated, i.e., have frames.
0178      * @return return true if the layer has frames. Currently, the scripting framework
0179      * does not give access to the animation features.
0180      */
0181     bool animated() const;
0182 
0183     /**
0184      * @brief enableAnimation make the current layer animated, so it can have frames.
0185      */
0186     void enableAnimation() const;
0187 
0188     /**
0189      * @brief Sets whether or not node should be pinned to the Timeline Docker,
0190      * regardless of selection activity.
0191      */
0192     void setPinnedToTimeline(bool pinned) const;
0193 
0194     /**
0195      * @return Returns true if node is pinned to the Timeline Docker or false if it is not.
0196      */
0197     bool isPinnedToTimeline() const;
0198 
0199     /**
0200      * Sets the state of the node to the value of @param collapsed
0201      */
0202     void setCollapsed(bool collapsed);
0203 
0204     /**
0205      * returns the collapsed state of this node
0206      */
0207     bool collapsed() const;
0208 
0209     /**
0210      * Sets a color label index associated to the layer.  The actual
0211      * color of the label and the number of available colors is
0212      * defined by Krita GUI configuration.
0213      */
0214     int colorLabel() const;
0215 
0216     /**
0217      * @brief setColorLabel sets a color label index associated to the layer.  The actual
0218      * color of the label and the number of available colors is
0219      * defined by Krita GUI configuration.
0220      * @param index an integer corresponding to the set of available color labels.
0221      */
0222     void setColorLabel(int index);
0223 
0224     /**
0225      * @brief inheritAlpha checks whether this node has the inherits alpha flag set
0226      * @return true if the Inherit Alpha is set
0227      */
0228     bool inheritAlpha() const;
0229 
0230     /**
0231      * set the Inherit Alpha flag to the given value
0232      */
0233     void setInheritAlpha(bool value);
0234 
0235     /**
0236      * @brief locked checks whether the Node is locked. A locked node cannot be changed.
0237      * @return true if the Node is locked, false if it hasn't been locked.
0238      */
0239     bool locked() const;
0240 
0241     /**
0242      * set the Locked flag to the give value
0243      */
0244     void setLocked(bool value);
0245 
0246     /**
0247      * @brief does the node have any content in it?
0248      * @return if node has any content in it
0249      */
0250     bool hasExtents();
0251 
0252 
0253     /**
0254      * @return the user-visible name of this node.
0255      */
0256     QString name() const;
0257 
0258     /**
0259      * rename the Node to the given name
0260      */
0261     void setName(QString name);
0262 
0263     /**
0264      * return the opacity of the Node. The opacity is a value between 0 and 255.
0265      */
0266     int opacity() const;
0267 
0268     /**
0269      * set the opacity of the Node to the given value. The opacity is a value between 0 and 255.
0270      */
0271     void setOpacity(int value);
0272 
0273     /**
0274      * return the Node that is the parent of the current Node, or 0 if this is the root Node.
0275      */
0276     Node* parentNode() const;
0277 
0278     /**
0279      * @brief type Krita has several types of nodes, split in layers and masks. Group
0280      * layers can contain other layers, any layer can contain masks.
0281      *
0282      * @return The type of the node. Valid types are:
0283      * <ul>
0284      *  <li>paintlayer
0285      *  <li>grouplayer
0286      *  <li>filelayer
0287      *  <li>filterlayer
0288      *  <li>filllayer
0289      *  <li>clonelayer
0290      *  <li>vectorlayer
0291      *  <li>transparencymask
0292      *  <li>filtermask
0293      *  <li>transformmask
0294      *  <li>selectionmask
0295      *  <li>colorizemask
0296      * </ul>
0297      *
0298      * If the Node object isn't wrapping a valid Krita layer or mask object, and
0299      * empty string is returned.
0300      */
0301     virtual QString type() const;
0302 
0303     /**
0304      * @brief icon
0305      * @return the icon associated with the layer.
0306      */
0307     QIcon icon() const;
0308 
0309     /**
0310      * Check whether the current Node is visible in the layer stack
0311      */
0312     bool visible() const;
0313 
0314     /**
0315      * Check to see if frame number on layer is a keyframe
0316      */
0317     bool hasKeyframeAtTime(int frameNumber);
0318 
0319     /**
0320      * Set the visibility of the current node to @param visible
0321      */
0322     void setVisible(bool visible);
0323 
0324     /**
0325      * @brief pixelData reads the given rectangle from the Node's paintable pixels, if those
0326      * exist, and returns it as a byte array. The pixel data starts top-left, and is ordered row-first.
0327      *
0328      * The byte array can be interpreted as follows: 8 bits images have one byte per channel,
0329      * and as many bytes as there are channels. 16 bits integer images have two bytes per channel,
0330      * representing an unsigned short. 16 bits float images have two bytes per channel, representing
0331      * a half, or 16 bits float. 32 bits float images have four bytes per channel, representing a
0332      * float.
0333      *
0334      * You can read outside the node boundaries; those pixels will be transparent black.
0335      *
0336      * The order of channels is:
0337      *
0338      * <ul>
0339      * <li>Integer RGBA: Blue, Green, Red, Alpha
0340      * <li>Float RGBA: Red, Green, Blue, Alpha
0341      * <li>GrayA: Gray, Alpha
0342      * <li>Selection: selectedness
0343      * <li>LabA: L, a, b, Alpha
0344      * <li>CMYKA: Cyan, Magenta, Yellow, Key, Alpha
0345      * <li>XYZA: X, Y, Z, A
0346      * <li>YCbCrA: Y, Cb, Cr, Alpha
0347      * </ul>
0348      *
0349      * The byte array is a copy of the original node data. In Python, you can use bytes, bytearray
0350      * and the struct module to interpret the data and construct, for instance, a Pillow Image object.
0351      *
0352      * If you read the pixeldata of a mask, a filter or generator layer, you get the selection bytes,
0353      * which is one channel with values in the range from 0..255.
0354      *
0355      * If you want to change the pixels of a node you can write the pixels back after manipulation
0356      * with setPixelData(). This will only succeed on nodes with writable pixel data, e.g not on groups
0357      * or file layers.
0358      *
0359      * @param x x position from where to start reading
0360      * @param y y position from where to start reading
0361      * @param w row length to read
0362      * @param h number of rows to read
0363      * @return a QByteArray with the pixel data. The byte array may be empty.
0364 
0365      */
0366     QByteArray pixelData(int x, int y, int w, int h) const;
0367 
0368     /**
0369      * @brief pixelDataAtTime a basic function to get pixeldata from an animated node at a given time.
0370      * @param x the position from the left to start reading.
0371      * @param y the position from the top to start reader
0372      * @param w the row length to read
0373      * @param h the number of rows to read
0374      * @param time the frame number
0375      * @return a QByteArray with the pixel data. The byte array may be empty.
0376      */
0377     QByteArray pixelDataAtTime(int x, int y, int w, int h, int time) const;
0378 
0379     /**
0380      * @brief projectionPixelData reads the given rectangle from the Node's projection (that is, what the node
0381      * looks like after all sub-Nodes (like layers in a group or masks on a layer) have been applied,
0382      * and returns it as a byte array. The pixel data starts top-left, and is ordered row-first.
0383      *
0384      * The byte array can be interpreted as follows: 8 bits images have one byte per channel,
0385      * and as many bytes as there are channels. 16 bits integer images have two bytes per channel,
0386      * representing an unsigned short. 16 bits float images have two bytes per channel, representing
0387      * a half, or 16 bits float. 32 bits float images have four bytes per channel, representing a
0388      * float.
0389      *
0390      * You can read outside the node boundaries; those pixels will be transparent black.
0391      *
0392      * The order of channels is:
0393      *
0394      * <ul>
0395      * <li>Integer RGBA: Blue, Green, Red, Alpha
0396      * <li>Float RGBA: Red, Green, Blue, Alpha
0397      * <li>GrayA: Gray, Alpha
0398      * <li>Selection: selectedness
0399      * <li>LabA: L, a, b, Alpha
0400      * <li>CMYKA: Cyan, Magenta, Yellow, Key, Alpha
0401      * <li>XYZA: X, Y, Z, A
0402      * <li>YCbCrA: Y, Cb, Cr, Alpha
0403      * </ul>
0404      *
0405      * The byte array is a copy of the original node data. In Python, you can use bytes, bytearray
0406      * and the struct module to interpret the data and construct, for instance, a Pillow Image object.
0407      *
0408      * If you read the projection of a mask, you get the selection bytes, which is one channel with
0409      * values in the range from 0..255.
0410      *
0411      * If you want to change the pixels of a node you can write the pixels back after manipulation
0412      * with setPixelData(). This will only succeed on nodes with writable pixel data, e.g not on groups
0413      * or file layers.
0414      *
0415      * @param x x position from where to start reading
0416      * @param y y position from where to start reading
0417      * @param w row length to read
0418      * @param h number of rows to read
0419      * @return a QByteArray with the pixel data. The byte array may be empty.
0420      */
0421     QByteArray projectionPixelData(int x, int y, int w, int h) const;
0422 
0423     /**
0424      * @brief setPixelData writes the given bytes, of which there must be enough, into the
0425      * Node, if the Node has writable pixel data:
0426      *
0427      * <ul>
0428      * <li>paint layer: the layer's original pixels are overwritten
0429      * <li>filter layer, generator layer, any mask: the embedded selection's pixels are overwritten.
0430      * <b>Note:</b> for these
0431      * </ul>
0432      *
0433      * File layers, Group layers, Clone layers cannot be written to. Calling setPixelData on
0434      * those layer types will silently do nothing.
0435      *
0436      * @param value the byte array representing the pixels. There must be enough bytes available.
0437      * Krita will take the raw pointer from the QByteArray and start reading, not stopping before
0438      * (number of channels * size of channel * w * h) bytes are read.
0439      *
0440      * @param x the x position to start writing from
0441      * @param y the y position to start writing from
0442      * @param w the width of each row
0443      * @param h the number of rows to write
0444      * @return true if writing the pixeldata worked
0445      */
0446     bool setPixelData(QByteArray value, int x, int y, int w, int h);
0447 
0448     /**
0449      * @brief bounds return the exact bounds of the node's paint device
0450      * @return the bounds, or an empty QRect if the node has no paint device or is empty.
0451      */
0452     QRect bounds() const;
0453 
0454     /**
0455      *  move the pixels to the given x, y location in the image coordinate space.
0456      */
0457     void move(int x, int y);
0458 
0459     /**
0460      * @brief position returns the position of the paint device of this node. The position is
0461      * always 0,0 unless the layer has been moved. If you want to know the topleft position of
0462      * the rectangle around the actual non-transparent pixels in the node, use bounds().
0463      * @return the top-left position of the node
0464      */
0465     QPoint position() const;
0466 
0467     /**
0468      * @brief remove removes this node from its parent image.
0469      */
0470     bool remove();
0471 
0472     /**
0473      * @brief duplicate returns a full copy of the current node. The node is not inserted in the graphic
0474      * @return a valid Node object or 0 if the node couldn't be duplicated.
0475      */
0476     Node* duplicate();
0477 
0478     /**
0479      * @brief save exports the given node with this filename. The extension of the filename determines the filetype.
0480      * @param filename the filename including extension
0481      * @param xRes the horizontal resolution in pixels per pt (there are 72 pts in an inch)
0482      * @param yRes the horizontal resolution in pixels per pt (there are 72 pts in an inch)
0483      * @param exportConfiguration a configuration object appropriate to the file format.
0484      * @param exportRect the export bounds for saving a node as a QRect
0485      * If \p exportRect is empty, then save exactBounds() of the node. If you'd like to save the image-
0486      * aligned area of the node, just pass image->bounds() there.
0487      * See Document->exportImage for InfoObject details.
0488      * @return true if saving succeeded, false if it failed.
0489      */
0490     bool save(const QString &filename, double xRes, double yRes, const InfoObject &exportConfiguration, const QRect &exportRect = QRect());
0491 
0492     /**
0493      * @brief mergeDown merges the given node with the first visible node underneath this node in the layerstack.
0494      * This will drop all per-layer metadata.
0495      */
0496     Node *mergeDown();
0497 
0498     /**
0499      * @brief scaleNode
0500      * @param origin the origin point
0501      * @param width the width
0502      * @param height the height
0503      * @param strategy the scaling strategy. There's several ones amongst these that aren't available in the regular UI.
0504      * <ul>
0505      * <li>Hermite</li>
0506      * <li>Bicubic - Adds pixels using the color of surrounding pixels. Produces smoother tonal gradations than Bilinear.</li>
0507      * <li>Box - Replicate pixels in the image. Preserves all the original detail, but can produce jagged effects.</li>
0508      * <li>Bilinear - Adds pixels averaging the color values of surrounding pixels. Produces medium quality results when the image is scaled from half to two times the original size.</li>
0509      * <li>Bell</li>
0510      * <li>BSpline</li>
0511      * <li>Lanczos3 - Offers similar results than Bicubic, but maybe a little bit sharper. Can produce light and dark halos along strong edges.</li>
0512      * <li>Mitchell</li>
0513      * </ul>
0514      */
0515     void scaleNode(QPointF origin, int width, int height, QString strategy);
0516 
0517     /**
0518      * @brief rotateNode rotate this layer by the given radians.
0519      * @param radians amount the layer should be rotated in, in radians.
0520      */
0521     void rotateNode(double radians);
0522 
0523     /**
0524      * @brief cropNode crop this layer.
0525      * @param x the left edge of the cropping rectangle.
0526      * @param y the top edge of the cropping rectangle
0527      * @param w the right edge of the cropping rectangle
0528      * @param h the bottom edge of the cropping rectangle
0529      */
0530     void cropNode(int x, int y, int w, int h);
0531 
0532     /**
0533      * @brief shearNode perform a shear operation on this node.
0534      * @param angleX the X-angle in degrees to shear by
0535      * @param angleY the Y-angle in degrees to shear by
0536      */
0537     void shearNode(double angleX, double angleY);
0538 
0539     /**
0540      * @brief thumbnail create a thumbnail of the given dimensions. The thumbnail is sized according
0541      * to the layer dimensions, not the image dimensions. If the requested size is too big a null
0542      * QImage is created. If the current node cannot generate a thumbnail, a transparent QImage of the
0543      * requested size is generated.
0544      * @return a QImage representing the layer contents.
0545      */
0546     QImage thumbnail(int w, int h);
0547 
0548     /**
0549      * @brief layerStyleToAsl retrieve the current layer's style in ASL format.
0550      * @return a QString in ASL format representing the layer style.
0551      */
0552     QString layerStyleToAsl();
0553 
0554     /**
0555      * @brief setLayerStyleFromAsl set a new layer style for this node.
0556      * @param aslContent a string formatted in ASL format containing the layer style
0557      * @return true if layer style was set, false if failed.
0558      */
0559     bool setLayerStyleFromAsl(const QString &asl);
0560 
0561     /**
0562      * @brief index the index of the node inside the parent
0563      * @return an integer representing the node's index inside the parent
0564      */
0565     int index() const;
0566 
0567     /**
0568      * @brief uniqueId uniqueId of the node
0569      * @return a QUuid representing a unique id to identify the node
0570      */
0571     QUuid uniqueId() const;
0572 
0573 
0574 private:
0575 
0576     friend class Filter;
0577     friend class Document;
0578     friend class Selection;
0579     friend class GroupLayer;
0580     friend class FileLayer;
0581     friend class FilterLayer;
0582     friend class FillLayer;
0583     friend class VectorLayer;
0584     friend class FilterMask;
0585     friend class SelectionMask;
0586     friend class TransparencyMask;
0587     friend class TransformMask;
0588     friend class ColorizeMask;
0589     friend class CloneLayer;
0590 
0591     explicit Node(KisImageSP image, KisNodeSP node, QObject *parent = 0);
0592 
0593     /**
0594      * @brief paintDevice gives access to the internal paint device of this Node
0595      * @return the paintdevice or 0 if the node does not have an editable paint device.
0596      */
0597     KisPaintDeviceSP paintDevice() const;
0598     KisImageSP image() const;
0599     KisNodeSP node() const;
0600 
0601     struct Private;
0602     Private *const d;
0603 
0604 };
0605 
0606 typedef QSharedPointer<Node> NodeSP;
0607 
0608 #endif // LIBKIS_NODE_H