File indexing completed on 2024-05-12 15:59:08

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #ifndef LIBKIS_SELECTION_H
0007 #define LIBKIS_SELECTION_H
0008 
0009 #include <QObject>
0010 
0011 #include "kritalibkis_export.h"
0012 #include "libkis.h"
0013 #include <kis_types.h>
0014 
0015 /**
0016  * Selection represents a selection on Krita. A selection is
0017  * not necessarily associated with a particular Node or Image.
0018  * 
0019  * @code
0020  * from krita import *
0021  *
0022  * d = Application.activeDocument()
0023  * n = d.activeNode()
0024  * r = n.bounds() 
0025  * s = Selection()
0026  * s.select(r.width() / 3, r.height() / 3, r.width() / 3, r.height() / 3, 255)
0027  * s.cut(n)
0028  * @endcode
0029  */
0030 class KRITALIBKIS_EXPORT Selection : public QObject
0031 {
0032     Q_OBJECT
0033 
0034 
0035 public:
0036     
0037     /**
0038      * For internal use only.
0039      */
0040     Selection(KisSelectionSP selection, QObject *parent = 0);
0041     
0042     /**
0043      * Create a new, empty selection object.
0044      */
0045     explicit Selection(QObject *parent = 0);
0046     ~Selection() override;
0047     
0048     bool operator==(const Selection &other) const;
0049     bool operator!=(const Selection &other) const;
0050 
0051 public Q_SLOTS:
0052 
0053     /**
0054      * @return a duplicate of the selection
0055      */
0056     Selection *duplicate() const;
0057 
0058     /**
0059      * @return the width of the selection
0060      */
0061     int width() const;
0062 
0063     /**
0064      * @return the height of the selection
0065      */
0066     int height() const;
0067 
0068     /**
0069      * @return the left-hand position of the selection.
0070      */
0071     int x() const;
0072 
0073     /**
0074      * @return the top position of the selection.
0075      */
0076     int y() const;
0077 
0078     /**
0079      * Move the selection's top-left corner to the given coordinates.
0080      */
0081     void move(int x, int y);
0082 
0083     /**
0084      * Make the selection entirely unselected.
0085      */
0086     void clear();
0087 
0088     /**
0089      * Make the selection's width and height smaller by the given value.
0090      * This will not move the selection's top-left position.
0091      */
0092     void contract(int value);
0093 
0094     /**
0095      * @brief copy copies the area defined by the selection from the node to the clipboard.
0096      * @param node the node from where the pixels will be copied.
0097      */
0098     void copy(Node *node);
0099 
0100     /**
0101      * @brief cut erases the area defined by the selection from the node and puts a copy on the clipboard.
0102      * @param node the node from which the selection will be cut.
0103      */
0104     void cut(Node *node);
0105 
0106     /**
0107      * @brief paste pastes the content of the clipboard to the given node, limited by the area of the current
0108      * selection.
0109      * @param destination the node where the pixels will be written
0110      * @param x: the x position at which the clip will be written
0111      * @param y: the y position at which the clip will be written
0112      */
0113     void paste(Node *destination, int x, int y);
0114    
0115     /**
0116      * Erode the selection with a radius of 1 pixel.
0117      */
0118     void erode();
0119 
0120     /**
0121      * Dilate the selection with a radius of 1 pixel.
0122      */
0123     void dilate();
0124 
0125     /**
0126      * Border the selection with the given radius.
0127      */
0128     void border(int xRadius, int yRadius);
0129 
0130     /**
0131      * Feather the selection with the given radius.
0132      */
0133     void feather(int radius);
0134 
0135     /**
0136      * Grow the selection with the given radius.
0137      */
0138     void grow(int xradius, int yradius);
0139 
0140     /**
0141      * Shrink the selection with the given radius.
0142      */
0143     void shrink(int xRadius, int yRadius, bool edgeLock);
0144 
0145     /**
0146      * Smooth the selection.
0147      */
0148     void smooth();
0149 
0150     /** 
0151      * Invert the selection.
0152      */
0153     void invert();
0154 
0155     /**
0156      * Resize the selection to the given width and height. The top-left position will not be moved.
0157      */
0158     void resize(int w, int h);
0159 
0160     /**
0161      * Select the given area. The value can be between 0 and 255; 0 is 
0162      * totally unselected, 255 is totally selected.
0163      */ 
0164     void select(int x, int y, int w, int h, int value);
0165 
0166     /**
0167      * Select all pixels in the given node. The value can be between 0 and 255; 0 is 
0168      * totally unselected, 255 is totally selected.
0169      */
0170     void selectAll(Node *node, int value);
0171 
0172     /**
0173      * Replace the current selection's selection with the one of the given selection.
0174      */
0175     void replace(Selection *selection);
0176 
0177     /**
0178      * Add the given selection's selected pixels to the current selection.
0179      */
0180     void add(Selection *selection);
0181     
0182     /**
0183      * Subtract the given selection's selected pixels from the current selection.
0184      */
0185     void subtract(Selection *selection);
0186 
0187     /**
0188      * Intersect the given selection with this selection.
0189      */
0190     void intersect(Selection *selection);
0191 
0192         /**
0193      * Intersect with the inverse of the given selection with this selection.
0194      */
0195     void symmetricdifference(Selection *selection);
0196 
0197     /**
0198      * @brief pixelData reads the given rectangle from the Selection's mask and returns it as a
0199      * byte array. The pixel data starts top-left, and is ordered row-first.
0200      *
0201      * The byte array will contain one byte for every pixel, representing the selectedness. 0
0202      * is totally unselected, 255 is fully selected.
0203      *
0204      * You can read outside the Selection's boundaries; those pixels will be unselected.
0205      *
0206      * The byte array is a copy of the original selection data.
0207      * @param x x position from where to start reading
0208      * @param y y position from where to start reading
0209      * @param w row length to read
0210      * @param h number of rows to read
0211      * @return a QByteArray with the pixel data. The byte array may be empty.
0212      */
0213     QByteArray pixelData(int x, int y, int w, int h) const;
0214 
0215     /**
0216      * @brief setPixelData writes the given bytes, of which there must be enough, into the
0217      * Selection.
0218      *
0219      * @param value the byte array representing the pixels. There must be enough bytes available.
0220      * Krita will take the raw pointer from the QByteArray and start reading, not stopping before
0221      * (w * h) bytes are read.
0222      *
0223      * @param x the x position to start writing from
0224      * @param y the y position to start writing from
0225      * @param w the width of each row
0226      * @param h the number of rows to write
0227      */
0228     void setPixelData(QByteArray value, int x, int y, int w, int h);
0229 
0230 private:
0231     friend class Document;
0232     friend class FilterLayer;
0233     friend class FillLayer;
0234     friend class SelectionMask;
0235 
0236     KisSelectionSP selection() const;
0237 
0238     struct Private;
0239     Private *const d;
0240 
0241 };
0242 
0243 #endif // LIBKIS_SELECTION_H