Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSelectionBasedProcessingHelper.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
6
7#include "kis_paint_device.h"
8#include "kis_painter.h"
9#include "kis_selection.h"
11#include "kis_transaction.h"
12#include "kis_undo_adapter.h"
13
15 : m_selection(selection),
16 m_func(func)
17{
18}
19
24
26{
27 if (!m_selection) return 0;
28
29 struct ProcessSelectionCommand : KisTransactionBasedCommand {
30 ProcessSelectionCommand(KisSelectionSP selection,
31 KisSelectionSP cutSelection,
32 std::function<void(KisPaintDeviceSP)> func)
33 : m_selection(selection),
34 m_cutSelection(cutSelection),
35 m_func(func)
36 {
37 }
38
39 KUndo2Command* paint() override {
40 m_cutSelection->pixelSelection()->makeCloneFromRough(m_selection->pixelSelection(), m_selection->selectedRect());
41
42 KisTransaction t(m_selection->pixelSelection());
43 m_func(m_selection->pixelSelection());
44
45 return t.endAndTake();
46 }
47
48 KisSelectionSP m_selection;
49 KisSelectionSP m_cutSelection;
50 Functor m_func;
51 };
52
54 return new ProcessSelectionCommand(m_selection, m_cutSelection, func);
55}
56
61
66
68{
70
72 // we have already processed the selection in the init command so try to skip it
73 if (device != static_cast<KisPaintDevice*>(m_selection->pixelSelection().data())) {
74 KisTransaction transaction(device);
75
76 const QRect cutBounds = m_cutSelection->selectedExactRect();
77 const QRect pasteBounds = m_selection->selectedExactRect();
78
79
80 KisPaintDeviceSP tempDev = new KisPaintDevice(device->colorSpace());
81 tempDev->makeCloneFromRough(device, cutBounds);
82
83 func(tempDev);
84
86 KisPainter::copyAreaOptimized(pasteBounds.topLeft(), tempDev, device, pasteBounds, m_selection);
87 transaction.commit(undoAdapter);
88 }
89 } else {
90 KisTransaction transaction(device);
91 func(device);
92 transaction.commit(undoAdapter);
93 }
94}
void makeCloneFromRough(KisPaintDeviceSP src, const QRect &minimalRect)
const KoColorSpace * colorSpace() const
void clearSelection(KisSelectionSP selection)
static void copyAreaOptimized(const QPoint &dstPt, KisPaintDeviceSP src, KisPaintDeviceSP dst, const QRect &originalSrcRect)
void transformPaintDevice(KisPaintDeviceSP device, KisUndoAdapter *undoAdapter)
std::function< void(KisPaintDeviceSP)> Functor
KisSelectionBasedProcessingHelper(KisSelectionSP selection, Functor func)
KUndo2Command * endAndTake()
void commit(KisUndoAdapter *undoAdapter)
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
KisPixelSelectionSP pixelSelection
QRect selectedExactRect() const
Slow, but exact way of determining the rectangle that encloses the selection.