Krita Source Code Documentation
Loading...
Searching...
No Matches
Selection.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#include "Selection.h"
7
8#include <KoColorSpace.h>
9#include <kis_selection.h>
10#include <kis_pixel_selection.h>
11#include <kis_paint_device.h>
13#include <kis_painter.h>
14#include <kis_node.h>
15#include <kis_clipboard.h>
16#include <QByteArray>
17#include <kis_tool_utils.h>
18#include <kis_paint_layer.h>
19#include <kis_image.h>
20
21#include <Node.h>
22
27
28Selection::Selection(KisSelectionSP selection, QObject *parent)
29 : QObject(parent)
30 , d(new Private)
31{
33}
34
35
36Selection::Selection(QObject *parent)
37 : QObject(parent)
38 , d(new Private)
39{
40 d->selection = new KisSelection();
41}
42
44{
45 delete d;
46}
47
48bool Selection::operator==(const Selection &other) const
49{
50 return (d->selection == other.d->selection);
51}
52
53bool Selection::operator!=(const Selection &other) const
54{
55 return !(operator==(other));
56}
57
59{
60 return new Selection(d->selection ? new KisSelection(*d->selection)
61 : new KisSelection());
62}
63
65{
66 if (!d->selection) return 0;
67 return d->selection->selectedExactRect().width();
68}
69
71{
72 if (!d->selection) return 0;
73 return d->selection->selectedExactRect().height();
74}
75
76int Selection::x() const
77{
78 if (!d->selection) return 0;
79 int xPos = d->selection->x();
81 xPos = d->selection->selectedExactRect().x();
82 }
83 return xPos;
84}
85
86int Selection::y() const
87{
88 if (!d->selection) return 0;
89 int yPos = d->selection->y();
91 yPos = d->selection->selectedExactRect().y();
92 }
93 return yPos;
94}
95
96void Selection::move(int x, int y)
97{
98 if (!d->selection) return;
99 d->selection->pixelSelection()->moveTo(QPoint(x, y));
100}
101
102
104{
105 if (!d->selection) return;
106 d->selection->clear();
107}
108
110{
111 if (!d->selection) return;
112 d->selection->pixelSelection()->select(QRect(x(), y(), width() - value, height() - value));
113}
114
116{
117 if (!node) return;
118 if (!d->selection) return;
119 if (node->node()->exactBounds().isEmpty()) return;
120 if (!node->node()->hasEditablePaintDevice()) return;
121
122 KisImageSP image = node->node()->image();
123 if (!image) return;
124
125 KisPaintDeviceSP dev(new KisPaintDevice(*node->node()->paintDevice()));
127
128 KisNodeSP newNode = new KisPaintLayer(image, "selection", OPACITY_OPAQUE_U8, dev);
129 KisNodeList nodes{newNode};
130
131 KisImageSP tempImage = KisToolUtils::buildImageForClipboard(image, nodes);
133}
134
136{
137 if (!node) return;
138 if (!d->selection) return;
139 if (node->node()->exactBounds().isEmpty()) return;
140 if (!node->node()->hasEditablePaintDevice()) return;
141 KisPaintDeviceSP dev = node->node()->paintDevice();
142 copy(node);
145}
146
147void Selection::paste(Node *destination, int x, int y)
148{
149 if (!destination) return;
150 if (!d->selection) return;
151 if (!KisClipboard::instance()->hasClip()) return;
152
153 KisDefaultBoundsBaseSP defaultBounds = destination->node()->paintDevice()->defaultBounds();
154 KisPaintDeviceSP src = KisClipboard::instance()->clip(defaultBounds, false);
155 KisPaintDeviceSP dst = destination->node()->paintDevice();
156 if (!dst || !src) return;
158 src,
159 dst,
160 src->exactBounds(),
161 d->selection);
162 destination->node()->setDirty();
163}
164
166{
167 if (!d->selection) return;
170 esf.process(d->selection->pixelSelection(), rc);
171}
172
174{
175 if (!d->selection) return;
178 dsf.process(d->selection->pixelSelection(), rc);
179}
180
181void Selection::border(int xRadius, int yRadius)
182{
183 if (!d->selection) return;
184 KisBorderSelectionFilter sf(xRadius, yRadius, true);
186 sf.process(d->selection->pixelSelection(), rc);
187}
188
189void Selection::feather(int radius)
190{
191 if (!d->selection) return;
192 KisFeatherSelectionFilter fsf(radius);
194 fsf.process(d->selection->pixelSelection(), rc);
195}
196
197void Selection::grow(int xradius, int yradius)
198{
199 if (!d->selection) return;
200 KisGrowSelectionFilter gsf(xradius, yradius);
202 gsf.process(d->selection->pixelSelection(), rc);
203}
204
205
206void Selection::shrink(int xRadius, int yRadius, bool edgeLock)
207{
208 if (!d->selection) return;
209 KisShrinkSelectionFilter sf(xRadius, yRadius, edgeLock);
211 sf.process(d->selection->pixelSelection(), rc);
212}
213
215{
216 if (!d->selection) return;
219 sf.process(d->selection->pixelSelection(), rc);
220}
221
222
224{
225 if (!d->selection) return;
228 sf.process(d->selection->pixelSelection(), rc);
229}
230
231void Selection::resize(int w, int h)
232{
233 if (!d->selection) return;
234 d->selection->pixelSelection()->select(QRect(x(), y(), w, h));
235}
236
237void Selection::select(int x, int y, int w, int h, int value)
238{
239 if (!d->selection) return;
240 d->selection->pixelSelection()->select(QRect(x, y, w, h), value);
241}
242
244{
245 if (!d->selection) return;
247}
248
250{
251 if (!d->selection) return;
253}
254
255void Selection::add(Selection *selection)
256{
257 if (!d->selection) return;
259}
260
262{
263 if (!d->selection) return;
265}
266
268{
269 if (!d->selection) return;
271}
272
278
279
280QByteArray Selection::pixelData(int x, int y, int w, int h) const
281{
282 QByteArray ba;
283 if (!d->selection) return ba;
285 quint8 *data = new quint8[w * h];
286 dev->readBytes(data, x, y, w, h);
287 ba = QByteArray((const char*)data, (int)(w * h));
288 delete[] data;
289 return ba;
290}
291
292void Selection::setPixelData(QByteArray value, int x, int y, int w, int h)
293{
294 if (!d->selection) return;
296 if (!dev) return;
297 dev->writeBytes((const quint8*)value.constData(), x, y, w, h);
298}
299
301{
302 return d->selection;
303}
304
305
float value(const T *src, size_t ch)
@ SELECTION_REPLACE
@ SELECTION_INTERSECT
@ SELECTION_SYMMETRICDIFFERENCE
@ SELECTION_SUBTRACT
@ SELECTION_ADD
const quint8 OPACITY_OPAQUE_U8
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
static KisClipboard * instance()
void setLayers(KisNodeList nodes, KisImageSP image, const QRect referenceRect, bool forceCopy=false)
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
void clearSelection(KisSelectionSP selection)
KisDefaultBoundsBaseSP defaultBounds() const
void readBytes(quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h) const
void writeBytes(const quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h)
static void copyAreaOptimized(const QPoint &dstPt, KisPaintDeviceSP src, KisPaintDeviceSP dst, const QRect &originalSrcRect)
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
QRect changeRect(const QRect &rect, KisDefaultBoundsBaseSP defaultBounds) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
Definition Node.h:25
KisNodeSP node() const
Definition Node.cpp:828
void move(int x, int y)
Definition Selection.cpp:96
void resize(int w, int h)
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:86
void intersect(Selection *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)
Definition Selection.cpp:28
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:64
void selectAll(Node *node, int value)
KisSelectionSP selection() const
bool operator==(const Selection &other) const
Definition Selection.cpp:48
void clear()
void contract(int value)
void shrink(int xRadius, int yRadius, bool edgeLock)
Selection * duplicate() const
Definition Selection.cpp:58
void smooth()
~Selection() override
Definition Selection.cpp:43
void subtract(Selection *selection)
int x() const
Definition Selection.cpp:76
void dilate()
Private *const d
Definition Selection.h:241
void erode()
void feather(int 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()
void border(int xRadius, int yRadius)
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)
bool operator!=(const Selection &other) const
Definition Selection.cpp:53
void add(Selection *selection)
void select(int x, int y, int w, int h, int value)
int height() const
Definition Selection.cpp:70
void replace(Selection *selection)
void symmetricdifference(Selection *selection)
KisImageSP buildImageForClipboard(KisImageSP srcImage, KisNodeList nodes)
void trimDeviceForClipboard(KisImageSP image, KisPaintDeviceSP device, KisSelectionSP selection, bool makeSharpClip)
virtual QRect exactBounds() const
KisImageWSP image
virtual KisPaintDeviceSP paintDevice() const =0
bool hasEditablePaintDevice() const
virtual void setDirty()
Definition kis_node.cpp:577
void moveTo(const QPoint &pt) override
void select(const QRect &r, quint8 selectedness=MAX_SELECTED)
void applySelection(KisPixelSelectionSP selection, SelectionAction action)
KisPixelSelectionSP projection() const
qint32 x() const
bool hasNonEmptyPixelSelection() const
KisPixelSelectionSP pixelSelection
QRect selectedExactRect() const
Slow, but exact way of determining the rectangle that encloses the selection.
qint32 y() const
KisSelectionSP selection
Definition Selection.cpp:25