Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_select_similar.cc
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 1999 Matthias Elter <me@kde.org>
3 * SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
4 * SPDX-FileCopyrightText: 2005 Boudewijn Rempt <boud@valdyas.org>
5 * SPDX-FileCopyrightText: 2015 Michael Abrahams <miabraha@gmail.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
11
12#include <QApplication>
13#include <QHBoxLayout>
14#include <QVBoxLayout>
15
16#include <ksharedconfig.h>
17
18#include <KoColorSpace.h>
21
22#include "kis_canvas2.h"
23#include "kis_command_utils.h"
24#include "kis_image.h"
25#include "kis_iterator_ng.h"
27#include "kis_slider_spin_box.h"
28#include "krita_utils.h"
29#include <KoPointerEvent.h>
30#include <kis_cursor.h>
31#include <kis_paint_device.h>
32#include <kis_pixel_selection.h>
36#include <kis_default_bounds.h>
37#include <kis_fill_painter.h>
38
40 : KisToolSelect(canvas,
41 KisCursor::loadWithSize("tool_similar_selection_cursor.svg", 32, 32, 6, 6),
42 i18n("Similar Color Selection"))
43 , m_threshold(20)
44 , m_opacitySpread(100)
45 , m_previousTime(0)
46{
47}
48
49void KisToolSelectSimilar::activate(const QSet<KoShape*> &shapes)
50{
52 m_configGroup = KSharedConfig::openConfig()->group(toolId());
53}
54
61
63{
65 if (isMovingSelection()) {
66 return;
67 }
68
70
71 if (!currentNode() ||
72 !(currentNode()->projection()) ||
73 !selectionEditable()) {
74
75 event->ignore();
76 return;
77 }
78
79 KisCanvas2 * kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
81
83
84 KisCursorOverrideLock cursorLock(KisCursor::waitCursor());
85
86 // Create the stroke
88 new KisStrokeStrategyUndoCommandBased(kundo2_i18n("Select Similar Color"), false, image().data());
89 strategy->setSupportsWrapAroundMode(false);
90 KisStrokeId strokeId = image()->startStroke(strategy);
91
92 // Construct the reference device
94 m_referencePaintDevice = currentNode()->projection();
95 } else if (sampleLayersMode() == SampleCurrentGroup) {
96 m_referencePaintDevice = currentNode()->parent()->projection();
97 } else if (sampleLayersMode() == SampleAllLayers) {
98 m_referencePaintDevice = currentImage()->projection();
100 if (!m_referenceNodeList) {
101 m_referencePaintDevice = KisMergeLabeledLayersCommand::createRefPaintDevice(image(), "Similar Colors Selection Tool Reference Result Paint Device");
103 }
104 KisPaintDeviceSP newReferencePaintDevice = KisMergeLabeledLayersCommand::createRefPaintDevice(image(), "Similar Colors Selection Tool Reference Result Paint Device");
106 const int currentTime = image()->animationInterface()->currentTime();
107
108 image()->addJob(
109 strokeId,
112 image(),
114 newReferenceNodeList,
116 newReferencePaintDevice,
119 m_previousTime != currentTime
120 )),
121 false,
124 )
125 );
126
127 m_referencePaintDevice = newReferencePaintDevice;
128 m_referenceNodeList = newReferenceNodeList;
129 m_previousTime = currentTime;
130 }
131
132 // Get the color of the pixel where the user clicked
134 const QPoint pos = convertToImagePixelCoordFloored(event);
135 QSharedPointer<KoColor> referenceColor = QSharedPointer<KoColor>(new KoColor(sourceDevice->colorSpace()));
136 // We need to obtain the reference color from the reference paint
137 // device, but it is produced in a stroke, so we must get the color
138 // after the device is ready. So we get it in the stroke
139 image()->addJob(
140 strokeId,
143 [sourceDevice, referenceColor, pos]() -> KUndo2Command*
144 {
145 *referenceColor = sourceDevice->pixel(pos);
146 return 0;
147 }
148 )),
149 false,
152 )
153 );
154
155 // Get the similar colors selection
156 KisFillPainter painter;
157 QRect bounds = currentImage()->bounds();
159 progressHelper(new KisProcessingVisitor::ProgressHelper(currentNode()));
160 KisPixelSelectionSP tmpSel = new KisPixelSelection(new KisSelectionDefaultBounds(currentNode()->projection()));
161
165 painter.setSizemod(growSelection());
167 painter.setFeather(featherSelection());
168
171 tmpSel, referenceColor, sourceDevice,
172 bounds, nullptr, progressHelper
173 );
174
175 for (KisStrokeJobData *job : jobs) {
176 image()->addJob(strokeId, job);
177 }
178
179 image()->addJob(
180 strokeId,
183 [tmpSel]() mutable -> KUndo2Command*
184 {
185 tmpSel->invalidateOutlineCache();
186 return 0;
187 }
188 )),
189 false,
192 )
193 );
194
195 image()->endStroke(strokeId);
196
197 // Apply selection
198 KisSelectionToolHelper helper(kisCanvas, kundo2_i18n("Select Similar Color"));
199 helper.selectPixelSelection(tmpSel, selectionAction());
200}
201
211
213{
214 m_threshold = threshold;
215 m_configGroup.writeEntry("threshold", threshold);
216}
217
219{
220 m_opacitySpread = opacitySpread;
221 m_configGroup.writeEntry("opacitySpread", opacitySpread);
222}
223
225{
227 KisSelectionOptions *selectionWidget = selectionOptionWidget();
228
229 selectionWidget->setStopGrowingAtDarkestPixelButtonVisible(true);
230
231 // Create widgets
232 KisSliderSpinBox *sliderThreshold = new KisSliderSpinBox;
233 sliderThreshold->setPrefix(i18nc(
234 "The 'threshold' spinbox prefix in similar selection tool options",
235 "Threshold: "));
236 sliderThreshold->setRange(1, 100);
237 sliderThreshold->setSingleStep(1);
238 sliderThreshold->setToolTip(
239 i18n("Set the color similarity tolerance of the selection. "
240 "Increasing threshold increases the range of similar colors to be selected."));
241
242 KisSliderSpinBox *sliderSpread = new KisSliderSpinBox;
243 sliderSpread->setRange(0, 100);
245 i18nc("The 'spread' spinbox in similar color selection tool options; {n} is the "
246 "number value, % is the percent sign",
247 "Spread: {n}%"));
248
249 // Set the tooltips
250 sliderThreshold->setToolTip(
251 i18n("Set the color similarity tolerance of the selection. "
252 "Increasing threshold increases the range of similar colors to be selected."));
253 sliderSpread->setToolTip(
254 i18n("Set the extent of the opaque portion of the selection. "
255 "Decreasing spread decreases opacity of selection areas depending on color similarity."));
256
257 // Construct the option widget
258 KisOptionCollectionWidgetWithHeader *sectionSelectionExtent =
260 i18nc("The 'selection extent' section label in similar selection "
261 "tool options",
262 "Selection extent"));
263 sectionSelectionExtent->appendWidget("sliderThreshold", sliderThreshold);
264 sectionSelectionExtent->appendWidget("sliderSpread", sliderSpread);
265 selectionWidget->insertWidget(3, "sectionSelectionExtent", sectionSelectionExtent);
266
267 // load setting from config
268 if (m_configGroup.hasKey("threshold")) {
269 m_threshold = m_configGroup.readEntry("threshold", 20);
270 } else {
271 m_threshold = m_configGroup.readEntry("fuzziness", 20);
272 }
273 sliderThreshold->setValue(m_threshold);
274
275 m_opacitySpread = m_configGroup.readEntry("opacitySpread", 100);
276 sliderSpread->setValue(m_opacitySpread);
277
278 // Make connections
279 connect(sliderThreshold,
280 SIGNAL(valueChanged(int)),
281 this,
282 SLOT(slotSetThreshold(int)));
283 connect(sliderSpread,
284 SIGNAL(valueChanged(int)),
285 this,
286 SLOT(slotSetOpacitySpread(int)));
287
288 return selectionWidget;
289}
290
292{
294 useCursor(KisCursor::loadWithSize("tool_similar_selection_cursor_add.svg", 32, 32, 6, 6));
295 } else if (selectionAction() == SELECTION_SUBTRACT) {
296 useCursor(KisCursor::loadWithSize("tool_similar_selection_cursor_sub.svg", 32, 32, 6, 6));
297 } else if (selectionAction() == SELECTION_INTERSECT) {
298 useCursor(KisCursor::loadWithSize("tool_similar_selection_cursor_inter.svg", 32, 32, 6, 6));
300 useCursor(KisCursor::loadWithSize("tool_similar_selection_cursor_symdiff.svg", 32, 32, 6, 6));
301 } else {
302 KisToolSelect::resetCursorStyle();
303 }
304}
@ SELECTION_INTERSECT
@ SELECTION_SYMMETRICDIFFERENCE
@ SELECTION_SUBTRACT
@ SELECTION_ADD
static QCursor loadWithSize(const QString &cursorName, int width, int height, int hotspotX=-1, int hotspotY=-1)
static QCursor waitCursor()
Definition kis_cursor.cc:54
void setSizemod(int sizemod)
void setFillThreshold(int threshold)
void setFeather(int feather)
QVector< KisStrokeJobData * > createSimilarColorsSelectionJobs(KisPixelSelectionSP outSelection, const QSharedPointer< KoColor > referenceColor, KisPaintDeviceSP referenceDevice, const QRect &rect, KisPixelSelectionSP mask, QSharedPointer< KisProcessingVisitor::ProgressHelper > progressHelper=nullptr)
void setAntiAlias(bool antiAlias)
void setStopGrowingAtDarkestPixel(bool stopGrowingAtDarkestPixel)
void setOpacitySpread(int opacitySpread)
static KisPaintDeviceSP createRefPaintDevice(KisImageSP originalImage, QString name="Merge Labeled Layers Reference Paint Device")
@ GroupSelectionPolicy_SelectIfColorLabeled
Groups will be taken into account only if they have set an explicit color label. This ignores groups ...
Wrapper class around a KisOptionCollectionWidget that also provide a header with a title label and an...
void appendWidget(const QString &id, QWidget *widget)
Insert the given widget with the given id at the end of the list. The list widget takes ownership of ...
void insertWidget(int index, const QString &id, QWidget *widget)
Insert the given widget with the given id at the given position. The list widget takes ownership of t...
const KoColorSpace * colorSpace() const
bool pixel(qint32 x, qint32 y, QColor *c) const
void setStopGrowingAtDarkestPixelButtonVisible(bool visible)
void selectPixelSelection(KisProcessingApplicator &applicator, KisPixelSelectionSP selection, SelectionAction action)
This class is a spinbox in which you can click and drag to set the value. A slider like bar is displa...
void setValue(int newValue)
void setRange(int newMinimum, int newMaximum, bool computeNewFastSliderStep=true)
Set the minimum and the maximum values of the range, computing a new "fast slider step" based on the ...
KisSelectionOptions * selectionOptionWidget()
SampleLayersMode sampleLayersMode() const
SelectionAction selectionAction() const
void beginPrimaryAction(KoPointerEvent *event) override
QList< int > colorLabelsSelected() const
void activate(const QSet< KoShape * > &shapes) override
void endPrimaryAction(KoPointerEvent *event) override
bool stopGrowingAtDarkestPixel() const
void deactivate() override
QWidget * createOptionWidget() override
bool antiAliasSelection() const
void beginPrimaryAction(KoPointerEvent *event) override
KisToolSelectSimilar(KoCanvasBase *canvas)
QWidget * createOptionWidget() override
KisMergeLabeledLayersCommand::ReferenceNodeInfoListSP m_referenceNodeList
void activate(const QSet< KoShape * > &shapes) override
KisPaintDeviceSP m_referencePaintDevice
void endPrimaryAction(KoPointerEvent *event) override
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
#define bounds(x, a, b)
QSharedPointer< KUndo2Command > KUndo2CommandSP
Definition kis_types.h:262
KUndo2MagicString kundo2_i18n(const char *text)
void setText(QSpinBox *spinBox, const QStringView textTemplate)
The LambdaCommand struct is a shorthand for creation of AggregateCommand commands using C++ lambda fe...