Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_selection_options.cc
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2005 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QCheckBox>
10#include <QToolButton>
11
12#include <kis_icon.h>
13#include "kis_config.h"
14#include "kis_config_notifier.h"
15#include "kis_paint_device.h"
16
17#include <ksharedconfig.h>
18#include <kconfiggroup.h>
19
21#include <KoGroupButton.h>
23#include <kis_slider_spin_box.h>
24
26{
27public:
31 QCheckBox *checkBoxMoveSelectedContent{nullptr};
32 QCheckBox *checkBoxAntiAliasSelection{nullptr};
34 QToolButton *buttonStopGrowingAtDarkestPixel {nullptr};
38 QCheckBox* checkBoxSelectionActionsPanel{nullptr};
39
41 {
42 return mode == PIXEL_SELECTION ? 0 : 1;
43 }
44
46 {
47 return index == 0 ? PIXEL_SELECTION : SHAPE_PROTECTION;
48 }
49
51 {
52 switch (action) {
54 return 0;
56 return 1;
57 case SELECTION_ADD:
58 return 2;
60 return 3;
62 return 4;
63 default:
64 return 0;
65 }
66 }
67
69 {
70 switch (index) {
71 case 0:
72 return SELECTION_REPLACE;
73 case 1:
75 case 2:
76 return SELECTION_ADD;
77 case 3:
78 return SELECTION_SUBTRACT;
79 case 4:
81 default:
82 return SELECTION_REPLACE;
83 }
84 }
85
87 {
88 switch (action) {
89 case CurrentLayer:
90 return 0;
91 case CurrentGroup:
92 return 1;
93 case AllLayers:
94 return 2;
96 return 3;
97 default:
98 return 0;
99 }
100 }
101
103 {
104 switch (index) {
105 case 0:
106 return CurrentLayer;
107 case 1:
108 return CurrentGroup;
109 case 2:
110 return AllLayers;
111 case 3:
112 return ColorLabeledLayers;
113 default:
114 return CurrentLayer;
115 }
116 }
117
118 void on_optionButtonStripMode_buttonToggled(int index, bool checked)
119 {
120 if (!checked) {
121 return;
122 }
123 const SelectionMode mode = buttonIndexToMode(index);
125 Q_EMIT q->modeChanged(mode);
126 }
127
128 void on_optionButtonStripAction_buttonToggled(int index, bool checked)
129 {
130 if (!checked) {
131 return;
132 }
133 Q_EMIT q->actionChanged(buttonIndexToAction(index));
134 }
135
137 {
138 if (!checked) {
139 return;
140 }
143 KisOptionCollectionWidgetWithHeader *sectionReference =
145 "sectionReference");
146 sectionReference->setWidgetVisible("widgetLabels",
149
151 }
152};
153
156 , m_d(new Private)
157{
158 m_d->q = this;
159 // Create widgets
160 m_d->optionButtonStripMode = new KisOptionButtonStrip;
161 m_d->optionButtonStripMode->addButton(
162 KisIconUtils::loadIcon("select-pixel"));
163 m_d->optionButtonStripMode->addButton(
164 KisIconUtils::loadIcon("select-shape"));
165 m_d->optionButtonStripMode->button(0)->setChecked(true);
166
167 m_d->optionButtonStripAction = new KisOptionButtonStrip;
168 m_d->optionButtonStripAction->addButton(
169 KisIconUtils::loadIcon("selection_replace"));
170 m_d->optionButtonStripAction->addButton(
171 KisIconUtils::loadIcon("selection_intersect"));
172 m_d->optionButtonStripAction->addButton(
173 KisIconUtils::loadIcon("selection_add"));
174 m_d->optionButtonStripAction->addButton(
175 KisIconUtils::loadIcon("selection_subtract"));
176 m_d->optionButtonStripAction->addButton(
177 KisIconUtils::loadIcon("selection_symmetric_difference"));
178 m_d->optionButtonStripAction->button(0)->setChecked(true);
179
180 m_d->checkBoxSelectionActionsPanel = new QCheckBox(i18n("Enable Selection Actions Bar"));
181 m_d->checkBoxSelectionActionsPanel->setToolTip(
182 i18n("When enabled, any selections will produce a small floating panel of useful selection-related actions."));
183
184 KisConfig cfg(true);
185
186 if (cfg.selectionActionBar()) {
187 m_d->checkBoxSelectionActionsPanel->setCheckState(Qt::CheckState::Checked);
188 } else {
189 m_d->checkBoxSelectionActionsPanel->setCheckState(Qt::CheckState::Unchecked);
190 }
191
192 m_d->checkBoxMoveSelectedContent = new QCheckBox(
193 i18nc("The move-selected-content checkbox in selection tool options",
194 "Move Selected Content"));
195
196 m_d->checkBoxAntiAliasSelection = new QCheckBox(
197 i18nc("The anti-alias checkbox in fill tool options", "Anti-aliasing"));
198 KisOptionCollectionWidget *containerGrowSelection = new KisOptionCollectionWidget;
199 m_d->sliderGrowSelection = new KisSliderSpinBox;
200 m_d->sliderGrowSelection->setPrefix(
201 i18nc("The 'grow/shrink' spinbox prefix in selection tools options",
202 "Grow: "));
203 m_d->sliderGrowSelection->setRange(-400, 400);
204 m_d->sliderGrowSelection->setSoftRange(-40, 40);
205 m_d->sliderGrowSelection->setSuffix(i18n(" px"));
206 m_d->buttonStopGrowingAtDarkestPixel = new QToolButton;
207 m_d->buttonStopGrowingAtDarkestPixel->setAutoRaise(true);
208 m_d->buttonStopGrowingAtDarkestPixel->setCheckable(true);
209 m_d->buttonStopGrowingAtDarkestPixel->setIcon(KisIconUtils::loadIcon("stop-at-boundary"));
210 containerGrowSelection->appendWidget("sliderGrowSelection", m_d->sliderGrowSelection);
211 containerGrowSelection->appendWidget("buttonStopGrowingAtDarkestPixel", m_d->buttonStopGrowingAtDarkestPixel);
212 containerGrowSelection->setOrientation(Qt::Horizontal);
213 containerGrowSelection->setWidgetVisible("buttonStopGrowingAtDarkestPixel", false);
214 m_d->sliderFeatherSelection = new KisSliderSpinBox;
215 m_d->sliderFeatherSelection->setPrefix(
216 i18nc("The 'feather' spinbox prefix in selection tools options",
217 "Feather: "));
218 m_d->sliderFeatherSelection->setRange(0, 400);
219 m_d->sliderFeatherSelection->setSoftRange(0, 40);
220 m_d->sliderFeatherSelection->setSuffix(i18n(" px"));
221
222 m_d->optionButtonStripReference = new KisOptionButtonStrip;
223 m_d->optionButtonStripReference->addButton(
224 KisIconUtils::loadIcon("current-layer"));
225 m_d->optionButtonStripReference->addButton(
226 KisIconUtils::loadIcon("current-group"));
227 m_d->optionButtonStripReference->addButton(
228 KisIconUtils::loadIcon("all-layers"));
229 m_d->optionButtonStripReference->addButton(KisIconUtils::loadIcon("tag"));
230 m_d->optionButtonStripReference->button(0)->setChecked(true);
231 m_d->widgetLabels = new KisColorLabelSelectorWidget;
232 m_d->widgetLabels->setExclusive(false);
233 m_d->widgetLabels->setButtonSize(20);
234 m_d->widgetLabels->setButtonWrapEnabled(true);
235 m_d->widgetLabels->setMouseDragEnabled(true);
236
237 // Set the tooltips
238 m_d->optionButtonStripMode->button(0)->setToolTip(
239 i18nc("@info:tooltip", "Pixel Selection"));
240 m_d->optionButtonStripMode->button(1)->setToolTip(
241 i18nc("@info:tooltip", "Vector Selection"));
242
243 m_d->optionButtonStripAction->button(0)->setToolTip(
244 i18nc("@info:tooltip", "Replace"));
245 m_d->optionButtonStripAction->button(1)->setToolTip(
246 i18nc("@info:tooltip", "Intersect"));
247 m_d->optionButtonStripAction->button(2)->setToolTip(
248 i18nc("@info:tooltip", "Add"));
249 m_d->optionButtonStripAction->button(3)->setToolTip(
250 i18nc("@info:tooltip", "Subtract"));
251 m_d->optionButtonStripAction->button(4)->setToolTip(
252 i18nc("@info:tooltip", "Symmetric Difference"));
253
254 m_d->checkBoxMoveSelectedContent->setToolTip(
255 i18nc("@info:tooltip", "Move Selected Content"));
256
257 m_d->checkBoxAntiAliasSelection->setToolTip(
258 i18n("Smooths the edges of the selection"));
259 m_d->sliderGrowSelection->setToolTip(
260 i18n("Grow or shrink the selection by the set amount"));
261 m_d->buttonStopGrowingAtDarkestPixel->setToolTip(
262 i18n("Stop growing at the darkest and/or most opaque pixels"));
263 m_d->sliderFeatherSelection->setToolTip(
264 i18n("Blur the selection by the set amount"));
265
266 m_d->optionButtonStripReference->button(0)->setToolTip(
267 i18n("Select regions found from the active layer"));
268 m_d->optionButtonStripReference->button(1)->setToolTip(
269 i18n("Select regions found from the active group"));
270 m_d->optionButtonStripReference->button(2)->setToolTip(
271 i18n("Select regions found from the merging of all layers"));
272 m_d->optionButtonStripReference->button(3)->setToolTip(
273 i18n("Select regions found from the merging of layers with specific color labels"));
274
275 // Construct the option widget
277
280 i18nc("The 'mode' section label in selection tools options",
281 "Mode"));
282 sectionMode->setPrimaryWidget(m_d->optionButtonStripMode);
283 appendWidget("sectionMode", sectionMode);
284
287 i18nc("The 'action' section label in selection tools options",
288 "Action"));
289 sectionAction->setPrimaryWidget(m_d->optionButtonStripAction);
290 appendWidget("sectionAction", sectionAction);
291
292 KisOptionCollectionWidgetWithHeader *sectionReference =
294 i18nc("The 'reference' section label in selection tools options",
295 "Reference"));
296 sectionReference->setPrimaryWidget(m_d->optionButtonStripReference);
297 sectionReference->appendWidget("widgetLabels", m_d->widgetLabels);
298 sectionReference->setWidgetVisible("widgetLabels", false);
299 appendWidget("sectionReference", sectionReference);
300
301 appendWidget("sapCheckBox", m_d->checkBoxSelectionActionsPanel);
302
303 appendWidget("checkBoxMoveSelectedContent", m_d->checkBoxMoveSelectedContent);
304
305 KisOptionCollectionWidgetWithHeader *sectionAdjustments =
307 i18nc("The 'adjustments' section label in selection tools options",
308 "Adjustments"));
309 sectionAdjustments->appendWidget("checkBoxAntiAliasSelection",
310 m_d->checkBoxAntiAliasSelection);
311 sectionAdjustments->appendWidget("containerGrowSelection", containerGrowSelection);
312 sectionAdjustments->appendWidget("sliderFeather",
313 m_d->sliderFeatherSelection);
314 appendWidget("sectionAdjustments", sectionAdjustments);
315
316
317 // Make connections
318 connect(m_d->optionButtonStripMode,
319 QOverload<int, bool>::of(&KisOptionButtonStrip::buttonToggled),
320 this,
321 [this](int i, int c) {
322 m_d->on_optionButtonStripMode_buttonToggled(i, c);
323 });
324 connect(m_d->optionButtonStripAction,
325 QOverload<int, bool>::of(&KisOptionButtonStrip::buttonToggled),
326 this,
327 [this](int i, int c) {
328 m_d->on_optionButtonStripAction_buttonToggled(i, c);
329 });
330 connect(m_d->checkBoxMoveSelectedContent,
331 SIGNAL(toggled(bool)),
332 SIGNAL(moveSelectedContentChanged(bool)));
333 connect(m_d->checkBoxAntiAliasSelection,
334 SIGNAL(toggled(bool)),
335 SIGNAL(antiAliasSelectionChanged(bool)));
336 connect(m_d->sliderGrowSelection,
337 SIGNAL(valueChanged(int)),
338 SIGNAL(growSelectionChanged(int)));
339 connect(m_d->buttonStopGrowingAtDarkestPixel,
340 SIGNAL(toggled(bool)),
342 connect(m_d->sliderFeatherSelection,
343 SIGNAL(valueChanged(int)),
344 SIGNAL(featherSelectionChanged(int)));
345 connect(m_d->optionButtonStripReference,
346 QOverload<int, bool>::of(&KisOptionButtonStrip::buttonToggled),
347 this,
348 [this](int i, int c) {
349 m_d->on_optionButtonStripReference_buttonToggled(i, c);
350 });
351 connect(m_d->widgetLabels,
352 SIGNAL(selectionChanged()),
354
355 connect(KisConfigNotifier::instance(), SIGNAL(configChanged()), SLOT(slotConfigChanged()));
356 connect(m_d->checkBoxSelectionActionsPanel,
357 SIGNAL(toggled(bool)),
359}
360
364
366{
367 return m_d->buttonIndexToMode(
368 m_d->optionButtonStripMode->checkedButtonIndex());
369}
370
372{
373 return m_d->buttonIndexToAction(
374 m_d->optionButtonStripAction->checkedButtonIndex());
375}
376
378{
379 return m_d->checkBoxMoveSelectedContent->isChecked();
380}
381
383{
384 return m_d->checkBoxAntiAliasSelection->isChecked();
385}
386
388{
389 return m_d->sliderGrowSelection->value();
390}
391
393{
394 return m_d->buttonStopGrowingAtDarkestPixel->isChecked();
395}
396
398{
399 return m_d->sliderFeatherSelection->value();
400}
401
404{
405 return m_d->buttonIndexToReferenceLayers(
406 m_d->optionButtonStripReference->checkedButtonIndex());
407}
408
410{
411 return m_d->widgetLabels->selection();
412}
413
415{
417 m_d->optionButtonStripMode->button(m_d->modeToButtonIndex(newMode));
419
420 button->setChecked(true);
421}
422
424{
425 KoGroupButton *button = m_d->optionButtonStripAction->button(
426 m_d->actionToButtonIndex(newAction));
428
429 button->setChecked(true);
430}
431
432void KisSelectionOptions::setMoveSelectedContent(bool newMoveSelectedContent)
433{
434 m_d->checkBoxMoveSelectedContent->setChecked(newMoveSelectedContent);
435}
436
437void KisSelectionOptions::setAntiAliasSelection(bool newAntiAliasSelection)
438{
439 m_d->checkBoxAntiAliasSelection->setChecked(newAntiAliasSelection);
440}
441
443{
444 m_d->sliderGrowSelection->setValue(newGrowSelection);
445}
446
447void KisSelectionOptions::setStopGrowingAtDarkestPixel(bool newStopGrowingAtDarkestPixel)
448{
449 m_d->buttonStopGrowingAtDarkestPixel->setChecked(newStopGrowingAtDarkestPixel);
450}
451
452void KisSelectionOptions::setFeatherSelection(int newFeatherSelection)
453{
454 m_d->sliderFeatherSelection->setValue(newFeatherSelection);
455}
456
458{
459 KoGroupButton *button = m_d->optionButtonStripReference->button(
460 m_d->referenceLayersToButtonIndex(newReferenceLayers));
462
463 button->setChecked(true);
464}
465
467 const QList<int> &newSelectedColorLabels)
468{
469 m_d->widgetLabels->setSelection(newSelectedColorLabels);
470}
471
473{
474 setWidgetVisible("sectionMode", visible);
475}
476
478{
479 setWidgetVisible("sectionAction", visible);
480}
481
483{
484 widgetAs<KisOptionCollectionWidgetWithHeader*>("sectionAdjustments")
485 ->widgetAs<KisOptionCollectionWidget*>("containerGrowSelection")
486 ->setWidgetVisible("buttonStopGrowingAtDarkestPixel", visible);
487}
488
490{
491 setWidgetVisible("sectionAdjustments", visible);
492}
493
495{
496 setWidgetVisible("sectionReference", visible);
497}
498
500 SelectionAction action,
501 const QKeySequence &shortcut)
502{
503 const QString shortcutString = shortcut.toString(QKeySequence::NativeText);
504 QString toolTipText;
505 const int buttonIndex = m_d->actionToButtonIndex(action);
506
507 switch (action) {
510 toolTipText = shortcutString.isEmpty()
511 ? i18nc("@info:tooltip", "Replace")
512 : i18nc("@info:tooltip", "Replace (%1)", shortcutString);
513 break;
514 case SELECTION_ADD:
515 toolTipText = shortcutString.isEmpty()
516 ? i18nc("@info:tooltip", "Add")
517 : i18nc("@info:tooltip", "Add (%1)", shortcutString);
518 break;
520 toolTipText = shortcutString.isEmpty()
521 ? i18nc("@info:tooltip", "Subtract")
522 : i18nc("@info:tooltip", "Subtract (%1)", shortcutString);
523 break;
525 toolTipText = shortcutString.isEmpty()
526 ? i18nc("@info:tooltip", "Intersect")
527 : i18nc("@info:tooltip", "Intersect (%1)", shortcutString);
528 break;
530 toolTipText = shortcutString.isEmpty()
531 ? i18nc("@info:tooltip", "Symmetric Difference")
532 : i18nc("@info:tooltip",
533 "Symmetric Difference (%1)",
534 shortcutString);
535 break;
536 }
537
538 m_d->optionButtonStripAction->button(buttonIndex)->setToolTip(toolTipText);
539}
540
542{
543 KisConfig cfg(true);
544
545 if (cfg.selectionActionBar()) {
546 m_d->checkBoxSelectionActionsPanel->setCheckState(Qt::Checked);
547 } else {
548 m_d->checkBoxSelectionActionsPanel->setCheckState(Qt::Unchecked);
549 }
550}
551
553{
554 KisConfig cfg(false);
556
557 blockSignals(true);
559 blockSignals(false);
560}
float value(const T *src, size_t ch)
SelectionMode
@ PIXEL_SELECTION
@ SHAPE_PROTECTION
SelectionAction
@ SELECTION_REPLACE
@ SELECTION_INTERSECT
@ SELECTION_SYMMETRICDIFFERENCE
@ SELECTION_DEFAULT
@ SELECTION_SUBTRACT
@ SELECTION_ADD
void setExclusive(bool exclusive)
Set if the button selection is mutually exclusive. This allows switching between single or multiple s...
static KisConfigNotifier * instance()
bool selectionActionBar(bool defaultValue=false) const
void setSelectionActionBar(bool value)
Provides a list of consecutive tool buttons.
void buttonToggled(KoGroupButton *button, bool checked)
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 ...
void setWidgetVisible(int index, bool visible)
Set the visibility of the widget that is at the given position.
Class providing a list of widgets with some addons such as separators, orientation or individual widg...
T widgetAs(int index) const
Get the widget that is at the given position casted to some other class.
void setOrientation(Qt::Orientation orientation, bool recursive=false)
Set the orientation of the list of widgets.
KisOptionCollectionWidget(QWidget *parent=nullptr)
void setWidgetVisible(int index, bool visible)
Set the visibility of the widget that is at the given position. Use this function instead of the widg...
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 setSeparatorsVisible(bool visible)
Set the visibility of the separators.
void on_optionButtonStripAction_buttonToggled(int index, bool checked)
SelectionAction buttonIndexToAction(int index) const
KisOptionButtonStrip * optionButtonStripReference
KisColorLabelSelectorWidget * widgetLabels
ReferenceLayers buttonIndexToReferenceLayers(int index) const
void on_optionButtonStripReference_buttonToggled(int index, bool checked)
int modeToButtonIndex(SelectionMode mode) const
KisOptionButtonStrip * optionButtonStripAction
int referenceLayersToButtonIndex(ReferenceLayers action) const
void on_optionButtonStripMode_buttonToggled(int index, bool checked)
SelectionMode buttonIndexToMode(int index) const
KisOptionButtonStrip * optionButtonStripMode
int actionToButtonIndex(SelectionAction action) const
void updateActionButtonToolTip(SelectionAction action, const QKeySequence &shortcut)
void setStopGrowingAtDarkestPixelButtonVisible(bool visible)
KisSelectionOptions(QWidget *parent=nullptr)
void setModeSectionVisible(bool visible)
void setAdjustmentsSectionVisible(bool visible)
void setMode(SelectionMode newMode)
void setGrowSelection(int newGrowSelection)
void setAntiAliasSelection(bool newAntiAliasSelection)
void slotSelectionActionsPanelCheckboxToggled(bool value)
void actionChanged(SelectionAction action)
void antiAliasSelectionChanged(bool antiAliasSelection)
void setAction(SelectionAction newAction)
void setMoveSelectedContent(bool newMoveSelectedContent)
SelectionMode mode() const
void setSelectedColorLabels(const QList< int > &newSelectedColorLabels)
void featherSelectionChanged(int featherSelection)
void setReferenceLayers(ReferenceLayers newReferenceLayers)
void selectedColorLabelsChanged()
void setFeatherSelection(int newFeatherSelection)
QList< int > selectedColorLabels() const
void setReferenceSectionVisible(bool visible)
ReferenceLayers referenceLayers() const
void referenceLayersChanged(ReferenceLayers referenceLayers)
QScopedPointer< Private > m_d
void setActionSectionVisible(bool visible)
void stopGrowingAtDarkestPixelChanged(bool stopGrowingAtDarkestPixel)
void growSelectionChanged(int growSelection)
void setStopGrowingAtDarkestPixel(bool newStopGrowingAtDarkestPixel)
SelectionAction action() const
void modeChanged(SelectionMode mode)
void moveSelectedContentChanged(bool moveSelectedContent)
This class is a spinbox in which you can click and drag to set the value. A slider like bar is displa...
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 ...
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
QString button(const QWheelEvent &ev)
QIcon loadIcon(const QString &name)