Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_select_contiguous.cc
Go to the documentation of this file.
1/*
2 * kis_tool_select_contiguous - part of Krayon^WKrita
3 *
4 * SPDX-FileCopyrightText: 1999 Michael Koch <koch@kde.org>
5 * SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
6 * SPDX-FileCopyrightText: 2004 Boudewijn Rempt <boud@valdyas.org>
7 * SPDX-FileCopyrightText: 2012 José Luis Vergara <pentalis@gmail.com>
8 * SPDX-FileCopyrightText: 2015 Michael Abrahams <miabraha@gmail.com>
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
14#include <QPainter>
15#include <QLayout>
16#include <QApplication>
17#include <QCheckBox>
18#include <QVBoxLayout>
19
22#include <KoGroupButton.h>
24#include <kis_color_button.h>
25
26#include <kis_debug.h>
27#include <klocalizedstring.h>
28#include <ksharedconfig.h>
29
30#include "KoPointerEvent.h"
31#include "KoViewConverter.h"
32
33#include "kis_cursor.h"
35#include "kis_image.h"
36#include "canvas/kis_canvas2.h"
37#include "kis_layer.h"
39#include "kis_paint_device.h"
40#include "kis_fill_painter.h"
41#include "kis_pixel_selection.h"
43#include "kis_slider_spin_box.h"
45#include "kis_image.h"
46#include "kis_undo_stores.h"
52
53#include "kis_command_utils.h"
54
57 canvas,
58 KisCursor::loadWithSize("tool_contiguous_selection_cursor.svg", 32, 32, 6, 6),
59 i18n("Contiguous Area Selection"))
60 , m_threshold(8)
61 , m_opacitySpread(100)
62 , m_useSelectionAsBoundary(false)
63 , m_previousTime(0)
64{
65 setObjectName("tool_select_contiguous");
66}
67
71
72void KisToolSelectContiguous::activate(const QSet<KoShape*> &shapes)
73{
75 m_configGroup = KSharedConfig::openConfig()->group(toolId());
76}
77
84
86{
88 if (isMovingSelection()) {
89 return;
90 }
91
93
94 if (!currentNode() ||
95 !(dev = currentNode()->projection()) ||
96 !selectionEditable()) {
97 event->ignore();
98 return;
99 }
100
102
103 KisCursorOverrideLock cursorLock(KisCursor::waitCursor());
104
105 // -------------------------------
106
107 KisProcessingApplicator applicator(currentImage(), currentNode(),
110 kundo2_i18n("Select Contiguous Area"));
111
112 QPoint pos = convertToImagePixelCoordFloored(event);
113 QRect rc = currentImage()->bounds();
114
115 KisPaintDeviceSP sourceDevice;
116
118 sourceDevice = m_referencePaintDevice = dev;
119 } else if (sampleLayersMode() == SampleCurrentGroup) {
120 sourceDevice = m_referencePaintDevice = currentNode()->parent()->projection();
121 } else if (sampleLayersMode() == SampleAllLayers) {
122 sourceDevice = m_referencePaintDevice = currentImage()->projection();
124 if (!m_referenceNodeList) {
125 m_referencePaintDevice = KisMergeLabeledLayersCommand::createRefPaintDevice(image(), "Contiguous Selection Tool Reference Result Paint Device");
127 }
128 KisPaintDeviceSP newReferencePaintDevice = KisMergeLabeledLayersCommand::createRefPaintDevice(image(), "Contiguous Selection Tool Reference Result Paint Device");
130 const int currentTime = image()->animationInterface()->currentTime();
131 applicator.applyCommand(
133 image(),
135 newReferenceNodeList,
137 newReferencePaintDevice,
140 m_previousTime != currentTime
141 ),
144 );
145 sourceDevice = m_referencePaintDevice = newReferencePaintDevice;
146 m_referenceNodeList = newReferenceNodeList;
147 m_previousTime = currentTime;
148 }
149
151 // Reset this so that the device from color labeled layers gets
152 // regenerated when that mode is selected again
153 m_referenceNodeList.reset();
154 }
155
156 KisPixelSelectionSP selection =
158
159 ContiguousSelectionMode contiguousSelectionMode = m_contiguousSelectionMode;
160 KoColor contiguousSelectionBoundaryColor = m_contiguousSelectionBoundaryColor;
161 int threshold = m_threshold;
162 int opacitySpread = m_opacitySpread;
163 int closeGap = m_closeGap;
164 bool useSelectionAsBoundary = m_useSelectionAsBoundary;
165 bool antiAlias = antiAliasSelection();
166 int grow = growSelection();
168 int feather = featherSelection();
169
170 KisCanvas2 * kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
171 KIS_SAFE_ASSERT_RECOVER(kisCanvas) {
172 applicator.cancel();
173 return;
174 };
175
176 KisPixelSelectionSP existingSelection;
177 if (kisCanvas->imageView() && kisCanvas->imageView()->selection())
178 {
179 existingSelection = kisCanvas->imageView()->selection()->pixelSelection();
180 }
181
183 *cmd =
185 [dev,
186 rc,
187 contiguousSelectionMode,
188 contiguousSelectionBoundaryColor,
189 threshold,
190 opacitySpread,
191 closeGap,
192 antiAlias,
193 feather,
194 grow,
196 useSelectionAsBoundary,
197 selection,
198 pos,
199 sourceDevice,
200 existingSelection]() mutable -> KUndo2Command * {
201 KisFillPainter fillpainter(dev);
202 fillpainter.setHeight(rc.height());
203 fillpainter.setWidth(rc.width());
204 fillpainter.setRegionFillingMode(
205 contiguousSelectionMode == FloodFill
208 );
209 if (contiguousSelectionMode == BoundaryFill) {
210 fillpainter.setRegionFillingBoundaryColor(contiguousSelectionBoundaryColor);
211 }
212 fillpainter.setFillThreshold(threshold);
213 fillpainter.setOpacitySpread(opacitySpread);
214 fillpainter.setAntiAlias(antiAlias);
215 fillpainter.setFeather(feather);
216 fillpainter.setCloseGap(closeGap);
217 fillpainter.setSizemod(grow);
219 fillpainter.setUseCompositing(true);
220
221 useSelectionAsBoundary &=
222 existingSelection &&
223 !existingSelection->isEmpty() &&
224 existingSelection->pixel(pos).opacityU8() != OPACITY_TRANSPARENT_U8;
225
226 fillpainter.setUseSelectionAsBoundary(useSelectionAsBoundary);
227 fillpainter.createFloodSelection(selection, pos.x(), pos.y(), sourceDevice, existingSelection);
228
229 selection->invalidateOutlineCache();
230
231 return 0;
232 });
234
235
236
237 KisSelectionToolHelper helper(kisCanvas, kundo2_i18n("Select Contiguous Area"));
238
239 helper.selectPixelSelection(applicator, selection, selectionAction());
240
241 applicator.end();
242
243}
244
254
255void KisToolSelectContiguous::paint(QPainter &painter, const KoViewConverter &converter)
256{
257 Q_UNUSED(painter);
258 Q_UNUSED(converter);
259}
260
262 ContiguousSelectionMode contiguousSelectionMode)
263{
264 if (contiguousSelectionMode == m_contiguousSelectionMode) {
265 return;
266 }
267 m_contiguousSelectionMode = contiguousSelectionMode;
268 m_configGroup.writeEntry(
269 "contiguousSelectionMode",
270 contiguousSelectionMode == FloodFill
271 ? "floodFill"
272 : "boundaryFill"
273 );
274}
275
277 const KoColor &color)
278{
280 return;
281 }
283 m_configGroup.writeEntry("contiguousSelectionBoundaryColor", color.toXML());
284}
285
287{
288 m_threshold = threshold;
289 m_configGroup.writeEntry("threshold", threshold);
290}
291
293{
294 m_opacitySpread = opacitySpread;
295 m_configGroup.writeEntry("opacitySpread", opacitySpread);
296}
297
299{
300 m_closeGap = closeGap;
301 m_configGroup.writeEntry("closeGapAmount", closeGap);
302}
303
305{
306 m_useSelectionAsBoundary = useSelectionAsBoundary;
307 m_configGroup.writeEntry("useSelectionAsBoundary", useSelectionAsBoundary);
308}
309
312 bool checked)
313{
314 if (!checked) {
315 return;
316 }
317
318 KisOptionCollectionWidgetWithHeader *sectionSelectionExtent =
320 "sectionSelectionExtent"
321 );
322 const KoGroupButton *buttonContiguousSelectionModeBoundaryFill =
323 sectionSelectionExtent->primaryWidgetAs<KisOptionButtonStrip*>()->button(1);
324 const bool visible = button == buttonContiguousSelectionModeBoundaryFill;
325 sectionSelectionExtent->setWidgetVisible(
326 "buttonContiguousSelectionBoundaryColor", visible
327 );
328
330 button == buttonContiguousSelectionModeBoundaryFill
332 : FloodFill
333 );
334}
335
337{
338 const QString xmlColor =
339 m_configGroup.readEntry("contiguousSelectionBoundaryColor", QString());
340 QDomDocument doc;
341 if (doc.setContent(xmlColor)) {
342 QDomElement e = doc.documentElement().firstChild().toElement();
343 QString channelDepthID =
344 doc.documentElement().attribute("channeldepth",
346 bool ok;
347 if (e.hasAttribute("space") || e.tagName().toLower() == "srgb") {
348 return KoColor::fromXML(e, channelDepthID, &ok);
349 } else if (doc.documentElement().hasAttribute("space") ||
350 doc.documentElement().tagName().toLower() == "srgb") {
351 return KoColor::fromXML(doc.documentElement(), channelDepthID, &ok);
352 }
353 }
354 return KoColor();
355}
356
358{
360 KisSelectionOptions *selectionWidget = selectionOptionWidget();
361
362 selectionWidget->setStopGrowingAtDarkestPixelButtonVisible(true);
363
364 // Create widgets
365 KisOptionButtonStrip *optionButtonStripContiguousSelectionMode =
367 KoGroupButton *buttonContiguousSelectionModeFloodFill =
368 optionButtonStripContiguousSelectionMode->addButton(
369 KisIconUtils::loadIcon("region-filling-flood-fill")
370 );
371 KoGroupButton *buttonContiguousSelectionModeBoundaryFill =
372 optionButtonStripContiguousSelectionMode->addButton(
373 KisIconUtils::loadIcon("region-filling-boundary-fill")
374 );
375 buttonContiguousSelectionModeFloodFill->setChecked(true);
376 KisColorButton *buttonContiguousSelectionBoundaryColor = new KisColorButton;
377 KisSliderSpinBox *sliderThreshold = new KisSliderSpinBox;
378 sliderThreshold->setPrefix(i18nc(
379 "The 'threshold' spinbox prefix in contiguous selection tool options",
380 "Threshold: "));
381 sliderThreshold->setRange(1, 100);
382 KisSliderSpinBox *sliderSpread = new KisSliderSpinBox;
383 sliderSpread->setRange(0, 100);
385 sliderSpread,
386 i18nc(
387 "The 'spread' spinbox in contiguous selection tool options; {n} is the number value, % is the percent sign",
388 "Spread: {n}%"));
389 QCheckBox *checkBoxSelectionAsBoundary = new QCheckBox(i18nc(
390 "The 'use selection as boundary' checkbox in contiguous selection tool "
391 "to use selection borders as boundary when filling",
392 "Use selection as boundary"));
393 checkBoxSelectionAsBoundary->setSizePolicy(QSizePolicy::Ignored,
394 QSizePolicy::Preferred);
395
396 // Set the tooltips
397 buttonContiguousSelectionModeFloodFill->setToolTip(
398 i18n("Select regions similar in color to the clicked region"));
399 buttonContiguousSelectionModeBoundaryFill->setToolTip(
400 i18n("Select all regions until a specific boundary color"));
401 buttonContiguousSelectionBoundaryColor->setToolTip(i18n("Boundary color"));
402 sliderThreshold->setToolTip(
403 i18n("Set the color similarity tolerance of the selection. "
404 "Increasing threshold increases the range of similar colors to be selected."));
405 sliderSpread->setToolTip(i18n(
406 "Set the extent of the opaque portion of the selection. "
407 "Decreasing spread decreases opacity of selection areas depending on color similarity."));
408 checkBoxSelectionAsBoundary->setToolTip(
409 i18n("Set if the contour of the active selection should be treated as "
410 "a boundary when making a new selection"));
411
412
413 KisSliderSpinBox *sliderCloseGap = new KisSliderSpinBox;
414 sliderCloseGap->setPrefix(i18nc("The 'close gap' spinbox prefix in contiguous selection tool options", "Close Gap: "));
415 sliderCloseGap->setRange(0, 32);
416 sliderCloseGap->setSuffix(i18n(" px"));
417
418 // Construct the option widget
419 KisOptionCollectionWidgetWithHeader *sectionSelectionExtent =
421 i18nc("The 'selection extent' section label in contiguous "
422 "selection tool options",
423 "Selection extent"));
424 sectionSelectionExtent->setPrimaryWidget(
425 optionButtonStripContiguousSelectionMode
426 );
427 sectionSelectionExtent->appendWidget(
428 "buttonContiguousSelectionBoundaryColor",
429 buttonContiguousSelectionBoundaryColor
430 );
431 sectionSelectionExtent->setWidgetVisible(
432 "buttonContiguousSelectionBoundaryColor",
433 false
434 );
435 sectionSelectionExtent->appendWidget("sliderThreshold", sliderThreshold);
436 sectionSelectionExtent->appendWidget("sliderSpread", sliderSpread);
437 sectionSelectionExtent->appendWidget("sliderCloseGap", sliderCloseGap);
438 sectionSelectionExtent->appendWidget("checkBoxSelectionAsBoundary",
439 checkBoxSelectionAsBoundary);
440
441 selectionWidget->insertWidget(3,
442 "sectionSelectionExtent",
443 sectionSelectionExtent);
444
445 // Load configuration settings into tool options
446 const QString contiguousSelectionModeStr =
447 m_configGroup.readEntry<QString>("contiguousSelectionMode", "");
449 contiguousSelectionModeStr == "boundaryFill"
451 : FloodFill;
454 if (m_configGroup.hasKey("threshold")) {
455 m_threshold = m_configGroup.readEntry("threshold", 8);
456 } else {
457 m_threshold = m_configGroup.readEntry("fuzziness", 8);
458 }
459 m_opacitySpread = m_configGroup.readEntry("opacitySpread", 100);
460 m_closeGap = m_configGroup.readEntry("closeGapAmount", 0);
462 m_configGroup.readEntry("useSelectionAsBoundary", false);
463
465 buttonContiguousSelectionModeBoundaryFill->setChecked(true);
466 sectionSelectionExtent->setWidgetVisible(
467 "buttonContiguousSelectionBoundaryColor",
468 true
469 );
470 }
471 buttonContiguousSelectionBoundaryColor->setColor(
473 );
474 sliderThreshold->setValue(m_threshold);
475 sliderSpread->setValue(m_opacitySpread);
476 sliderCloseGap->setValue(m_closeGap);
477 checkBoxSelectionAsBoundary->setChecked(m_useSelectionAsBoundary);
478
479 // Make connections
480 connect(optionButtonStripContiguousSelectionMode,
481 SIGNAL(buttonToggled(KoGroupButton*, bool)),
483 KoGroupButton*, bool)));
484 connect(buttonContiguousSelectionBoundaryColor,
485 SIGNAL(changed(const KoColor&)),
487 connect(sliderThreshold,
488 SIGNAL(valueChanged(int)),
489 this,
490 SLOT(slotSetThreshold(int)));
491 connect(sliderSpread,
492 SIGNAL(valueChanged(int)),
493 this,
494 SLOT(slotSetOpacitySpread(int)));
495 connect(sliderCloseGap,
496 SIGNAL(valueChanged(int)),
497 this,
498 SLOT(slotSetCloseGap(int)));
499 connect(checkBoxSelectionAsBoundary,
500 SIGNAL(toggled(bool)),
501 this,
503
504 return selectionWidget;
505}
506
508{
510 useCursor(KisCursor::loadWithSize("tool_contiguous_selection_cursor_add.svg", 32, 32, 6, 6));
511 } else if (selectionAction() == SELECTION_SUBTRACT) {
512 useCursor(KisCursor::loadWithSize("tool_contiguous_selection_cursor_sub.svg", 32, 32, 6, 6));
513 } else if (selectionAction() == SELECTION_INTERSECT) {
514 useCursor(KisCursor::loadWithSize("tool_contiguous_selection_cursor_inter.svg", 32, 32, 6, 6));
516 useCursor(KisCursor::loadWithSize("tool_contiguous_selection_cursor_symdiff.svg", 32, 32, 6, 6));
517 } else {
518 KisToolSelect::resetCursorStyle();
519 }
520}
QVector< KisImageSignalType > KisImageSignalVector
@ SELECTION_INTERSECT
@ SELECTION_SYMMETRICDIFFERENCE
@ SELECTION_SUBTRACT
@ SELECTION_ADD
const KoID Integer16BitsColorDepthID("U16", ki18n("16-bit integer/channel"))
const quint8 OPACITY_TRANSPARENT_U8
QPointer< KisView > imageView() const
A pushbutton to display or allow user selection of a color.
void setColor(const KoColor &c)
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 setWidth(int w)
void setSizemod(int sizemod)
void setHeight(int h)
void setFillThreshold(int threshold)
void setFeather(int feather)
void setCloseGap(int gap)
void setRegionFillingMode(RegionFillingMode regionFillingMode)
KisPixelSelectionSP createFloodSelection(int startX, int startY, KisPaintDeviceSP sourceDevice, KisPaintDeviceSP existingSelection)
void setUseSelectionAsBoundary(bool useSelectionAsBoundary)
void setRegionFillingBoundaryColor(const KoColor &regionFillingBoundaryColor)
void setUseCompositing(bool useCompositing)
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 ...
Provides a list of consecutive tool buttons.
KoGroupButton * addButton(const QIcon &icon, const QString &text=QString())
Wrapper class around a KisOptionCollectionWidget that also provide a header with a title label and an...
void setPrimaryWidget(QWidget *widget)
Set the primary widget. The list widget takes ownership of it.
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 ...
T primaryWidgetAs() const
Get the primary widget casted to some other class.
void setWidgetVisible(int index, bool visible)
Set the visibility of the widget that is at the given position.
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...
T widgetAs(int index) const
Get the widget that is at the given position casted to some other class.
bool pixel(qint32 x, qint32 y, QColor *c) const
void applyCommand(KUndo2Command *command, KisStrokeJobData::Sequentiality sequentiality=KisStrokeJobData::SEQUENTIAL, KisStrokeJobData::Exclusivity exclusivity=KisStrokeJobData::NORMAL)
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
KisMergeLabeledLayersCommand::ReferenceNodeInfoListSP m_referenceNodeList
KisToolSelectContiguous(KoCanvasBase *canvas)
void slotSetContiguousSelectionMode(ContiguousSelectionMode)
void activate(const QSet< KoShape * > &shapes) override
void paint(QPainter &painter, const KoViewConverter &converter) override
void beginPrimaryAction(KoPointerEvent *event) override
ContiguousSelectionMode m_contiguousSelectionMode
void slotSetContiguousSelectionBoundaryColor(const KoColor &)
void endPrimaryAction(KoPointerEvent *event) override
void slot_optionButtonStripContiguousSelectionMode_buttonToggled(KoGroupButton *, bool)
static KoColor fromXML(const QDomElement &elt, const QString &channelDepthId)
Definition KoColor.cpp:350
void toXML(QDomDocument &doc, QDomElement &colorElt) const
Definition KoColor.cpp:304
QString id() const
Definition KoID.cpp:63
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
QString button(const QWheelEvent &ev)
KUndo2MagicString kundo2_i18n(const char *text)
QIcon loadIcon(const QString &name)
void setText(QSpinBox *spinBox, const QStringView textTemplate)
The LambdaCommand struct is a shorthand for creation of AggregateCommand commands using C++ lambda fe...
bool isEmpty() const override