Krita

Selection.h
1/*
2 * SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#ifndef LIBKIS_SELECTION_H
7#define LIBKIS_SELECTION_H
8
9#include <QObject>
10
11#include "kritalibkis_export.h"
12#include "libkis.h"
13#include <kis_types.h>
14
15/**
16 * Selection represents a selection on Krita. A selection is
17 * not necessarily associated with a particular Node or Image.
18 *
19 * @code
20 * from krita import *
21 *
22 * d = Application.activeDocument()
23 * n = d.activeNode()
24 * r = n.bounds()
25 * s = Selection()
26 * s.select(r.width() / 3, r.height() / 3, r.width() / 3, r.height() / 3, 255)
27 * s.cut(n)
28 * @endcode
29 */
30class KRITALIBKIS_EXPORT Selection : public QObject
31{
33
34
35public:
36
37 /**
38 * For internal use only.
39 */
40 Selection(KisSelectionSP selection, QObject *parent = 0);
41
42 /**
43 * Create a new, empty selection object.
44 */
45 explicit Selection(QObject *parent = 0);
46 ~Selection() override;
47
48 bool operator==(const Selection &other) const;
49 bool operator!=(const Selection &other) const;
50
51public Q_SLOTS:
52
53 /**
54 * @return a duplicate of the selection
55 */
56 Selection *duplicate() const;
57
58 /**
59 * @return the width of the selection
60 */
61 int width() const;
62
63 /**
64 * @return the height of the selection
65 */
66 int height() const;
67
68 /**
69 * @return the left-hand position of the selection.
70 */
71 int x() const;
72
73 /**
74 * @return the top position of the selection.
75 */
76 int y() const;
77
78 /**
79 * Move the selection's top-left corner to the given coordinates.
80 */
81 void move(int x, int y);
82
83 /**
84 * Make the selection entirely unselected.
85 */
86 void clear();
87
88 /**
89 * Make the selection's width and height smaller by the given value.
90 * This will not move the selection's top-left position.
91 */
92 void contract(int value);
93
94 /**
95 * @brief copy copies the area defined by the selection from the node to the clipboard.
96 * @param node the node from where the pixels will be copied.
97 */
98 void copy(Node *node);
99
100 /**
101 * @brief cut erases the area defined by the selection from the node and puts a copy on the clipboard.
102 * @param node the node from which the selection will be cut.
103 */
104 void cut(Node *node);
105
106 /**
107 * @brief paste pastes the content of the clipboard to the given node, limited by the area of the current
108 * selection.
109 * @param destination the node where the pixels will be written
110 * @param x: the x position at which the clip will be written
111 * @param y: the y position at which the clip will be written
112 */
113 void paste(Node *destination, int x, int y);
114
115 /**
116 * Erode the selection with a radius of 1 pixel.
117 */
118 void erode();
119
120 /**
121 * Dilate the selection with a radius of 1 pixel.
122 */
123 void dilate();
124
125 /**
126 * Border the selection with the given radius.
127 */
128 void border(int xRadius, int yRadius);
129
130 /**
131 * Feather the selection with the given radius.
132 */
133 void feather(int radius);
134
135 /**
136 * Grow the selection with the given radius.
137 */
138 void grow(int xradius, int yradius);
139
140 /**
141 * Shrink the selection with the given radius.
142 */
143 void shrink(int xRadius, int yRadius, bool edgeLock);
144
145 /**
146 * Smooth the selection.
147 */
148 void smooth();
149
150 /**
151 * Invert the selection.
152 */
153 void invert();
154
155 /**
156 * Resize the selection to the given width and height. The top-left position will not be moved.
157 */
158 void resize(int w, int h);
159
160 /**
161 * Select the given area. The value can be between 0 and 255; 0 is
162 * totally unselected, 255 is totally selected.
163 */
164 void select(int x, int y, int w, int h, int value);
165
166 /**
167 * Select all pixels in the given node. The value can be between 0 and 255; 0 is
168 * totally unselected, 255 is totally selected.
169 */
170 void selectAll(Node *node, int value);
171
172 /**
173 * Replace the current selection's selection with the one of the given selection.
174 */
175 void replace(Selection *selection);
176
177 /**
178 * Add the given selection's selected pixels to the current selection.
179 */
180 void add(Selection *selection);
181
182 /**
183 * Subtract the given selection's selected pixels from the current selection.
184 */
185 void subtract(Selection *selection);
186
187 /**
188 * Intersect the given selection with this selection.
189 */
190 void intersect(Selection *selection);
191
192 /**
193 * Intersect with the inverse of the given selection with this selection.
194 */
195 void symmetricdifference(Selection *selection);
196
197 /**
198 * @brief pixelData reads the given rectangle from the Selection's mask and returns it as a
199 * byte array. The pixel data starts top-left, and is ordered row-first.
200 *
201 * The byte array will contain one byte for every pixel, representing the selectedness. 0
202 * is totally unselected, 255 is fully selected.
203 *
204 * You can read outside the Selection's boundaries; those pixels will be unselected.
205 *
206 * The byte array is a copy of the original selection data.
207 * @param x x position from where to start reading
208 * @param y y position from where to start reading
209 * @param w row length to read
210 * @param h number of rows to read
211 * @return a QByteArray with the pixel data. The byte array may be empty.
212 */
213 QByteArray pixelData(int x, int y, int w, int h) const;
214
215 /**
216 * @brief setPixelData writes the given bytes, of which there must be enough, into the
217 * Selection.
218 *
219 * @param value the byte array representing the pixels. There must be enough bytes available.
220 * Krita will take the raw pointer from the QByteArray and start reading, not stopping before
221 * (w * h) bytes are read.
222 *
223 * @param x the x position to start writing from
224 * @param y the y position to start writing from
225 * @param w the width of each row
226 * @param h the number of rows to write
227 */
228 void setPixelData(QByteArray value, int x, int y, int w, int h);
229
230private:
231 friend class Document;
232 friend class FilterLayer;
233 friend class FillLayer;
234 friend class SelectionMask;
235 friend class TransparencyMask;
236 friend class FilterMask;
237
238 KisSelectionSP selection() const;
239
240 struct Private;
241 Private *const d;
242
243};
244
245#endif // LIBKIS_SELECTION_H
Node represents a layer or mask in a Krita image's Node hierarchy.
Definition Node.h:24
void move(int x, int y)
Move the selection's top-left corner to the given coordinates.
Definition Selection.cpp:94
void resize(int w, int h)
Resize the selection to the given width and height.
void cut(Node *node)
cut erases the area defined by the selection from the node and puts a copy on the clipboard.
int y() const
Definition Selection.cpp:84
void intersect(Selection *selection)
Intersect the given selection with this selection.
void copy(Node *node)
copy copies the area defined by the selection from the node to the clipboard.
Selection(KisSelectionSP selection, QObject *parent=0)
For internal use only.
Definition Selection.cpp:26
void paste(Node *destination, int x, int y)
paste pastes the content of the clipboard to the given node, limited by the area of the current selec...
int width() const
Definition Selection.cpp:62
void selectAll(Node *node, int value)
Select all pixels in the given node.
void clear()
Make the selection entirely unselected.
void contract(int value)
Make the selection's width and height smaller by the given value.
void shrink(int xRadius, int yRadius, bool edgeLock)
Shrink the selection with the given radius.
Selection * duplicate() const
Definition Selection.cpp:56
void smooth()
Smooth the selection.
void subtract(Selection *selection)
Subtract the given selection's selected pixels from the current selection.
int x() const
Definition Selection.cpp:74
void dilate()
Dilate the selection with a radius of 1 pixel.
void erode()
Erode the selection with a radius of 1 pixel.
void feather(int radius)
Feather the selection with the given radius.
void setPixelData(QByteArray value, int x, int y, int w, int h)
setPixelData writes the given bytes, of which there must be enough, into the Selection.
void invert()
Invert the selection.
void border(int xRadius, int yRadius)
Border the selection with the given radius.
QByteArray pixelData(int x, int y, int w, int h) const
pixelData reads the given rectangle from the Selection's mask and returns it as a byte array.
void grow(int xradius, int yradius)
Grow the selection with the given radius.
void add(Selection *selection)
Add the given selection's selected pixels to the current selection.
void select(int x, int y, int w, int h, int value)
Select the given area.
int height() const
Definition Selection.cpp:68
void replace(Selection *selection)
Replace the current selection's selection with the one of the given selection.
void symmetricdifference(Selection *selection)
Intersect with the inverse of the given selection with this selection.
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:51:04 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.