Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_select_base.h
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2009 Boudewijn Rempt <boud@valdyas.org>
3 * SPDX-FileCopyrightText: 2015 Michael Abrahams <miabraha@gmail.com>
4 * SPDX-FileCopyrightText: 2025 Ricky Ringler <ricky.ringler@proton.me>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9#ifndef KISTOOLSELECTBASE_H
10#define KISTOOLSELECTBASE_H
11
12#include "KoPointerEvent.h"
13#include "kis_tool.h"
14#include "kis_canvas2.h"
15#include "kis_selection.h"
18#include "KisViewManager.h"
23#include "kis_image.h"
24#include "kis_cursor.h"
25#include "kis_action_manager.h"
26#include "kis_action.h"
29#include "kis_assert.h"
31
73template <class BaseClass>
74class KisToolSelectBase : public BaseClass
75{
76
77public:
78
79 KisToolSelectBase(KoCanvasBase* canvas, const QString toolName)
80 : BaseClass(canvas)
81 , m_widgetHelper(toolName)
83 {
85 }
86
87 KisToolSelectBase(KoCanvasBase* canvas, const QCursor cursor, const QString toolName)
88 : BaseClass(canvas, cursor)
89 , m_widgetHelper(toolName)
91 {
93 }
94
95 KisToolSelectBase(KoCanvasBase* canvas, QCursor cursor, QString toolName, KoToolBase *delegateTool)
96 : BaseClass(canvas, cursor, delegateTool)
97 , m_widgetHelper(toolName)
99 {
101 }
102
110
118
121 if (widget) {
124 this->action("selection_tool_mode_replace")->shortcut());
127 this->action("selection_tool_mode_add")->shortcut());
130 this->action("selection_tool_mode_subtract")->shortcut());
133 this->action("selection_tool_mode_intersect")->shortcut());
134 }
135 }
136
137 void activate(const QSet<KoShape *> &shapes) override
138 {
139 BaseClass::activate(shapes);
140
142 this->action("selection_tool_mode_replace"), SIGNAL(triggered()),
143 &m_widgetHelper, SLOT(slotReplaceModeRequested()));
144
146 this->action("selection_tool_mode_add"), SIGNAL(triggered()),
147 &m_widgetHelper, SLOT(slotAddModeRequested()));
148
150 this->action("selection_tool_mode_subtract"), SIGNAL(triggered()),
151 &m_widgetHelper, SLOT(slotSubtractModeRequested()));
152
154 this->action("selection_tool_mode_intersect"), SIGNAL(triggered()),
155 &m_widgetHelper, SLOT(slotIntersectModeRequested()));
156
158
160 if (isPixelOnly()) {
163 true);
164 }
167 }
168 }
169
170 void deactivate() override
171 {
173 BaseClass::deactivate();
175 }
176
177 QWidget *createOptionWidget() override
178 {
179 m_widgetHelper.createOptionWidget(this->toolId());
181
182 this->connect(this, SIGNAL(isActiveChanged(bool)), &m_widgetHelper, SLOT(slotToolActivatedChanged(bool)));
183 this->connect(&m_widgetHelper,
184 SIGNAL(selectionActionChanged(SelectionAction)),
185 this,
186 SLOT(resetCursorStyle()));
187
190 m_widgetHelper.optionWidget()->setContentsMargins(0, 10, 0, 10);
191 if (isPixelOnly()) {
194 true);
195 }
198 }
199
201 }
202
204 {
206 }
207
215
217 {
219 }
220
222 {
224 }
225
226 int growSelection() const
227 {
229 }
230
235
237 {
239 }
240
245
247 {
250 if (referenceLayers == KisSelectionOptions::AllLayers) {
251 return SampleAllLayers;
252 } else if (referenceLayers == KisSelectionOptions::CurrentGroup) {
253 return SampleCurrentGroup;
254 } else if (referenceLayers == KisSelectionOptions::CurrentLayer) {
255 return SampleCurrentLayer;
256 } else if (referenceLayers == KisSelectionOptions::ColorLabeledLayers) {
258 }
260 return SampleAllLayers;
261 }
262
267
272
274 {
276 }
277
279 {
280 Q_UNUSED(action);
281 BaseClass::activatePrimaryAction();
282 }
283
285 {
286 Q_UNUSED(action);
287 BaseClass::deactivatePrimaryAction();
288 }
289
291 KisTool::AlternateAction action) override
292 {
293 Q_UNUSED(action);
294 beginPrimaryAction(event);
295 }
296
298 KisTool::AlternateAction action) override
299 {
300 Q_UNUSED(action);
302 }
303
305 KisTool::AlternateAction action) override
306 {
307 Q_UNUSED(action);
308 endPrimaryAction(event);
309 }
310
312 {
314 }
315
317 {
319 }
320
321 void requestStrokeEnd() override
322 {
324 }
325
329
333
336 if (!cancel) {
337 this->image()->endStroke(m_moveStrokeId);
338 } else {
339 this->image()->cancelStroke(m_moveStrokeId);
340 }
341 m_moveStrokeId.clear();
342 m_accumulatedOffset = QPoint();
343 m_dragStartOffset = QPoint();
345 return;
346 }
347 }
348
349 KisNodeSP locateSelectionMaskUnderCursor(const QPointF &pos, Qt::KeyboardModifiers modifiers) {
350 if (modifiers != Qt::NoModifier) return 0;
351
352 KisCanvas2* canvas = dynamic_cast<KisCanvas2*>(this->canvas());
354
355 KisSelectionSP selection = canvas->viewManager()->selection();
356 if (selection &&
357 selection->outlineCacheValid()) {
358
359 const qreal handleRadius = qreal(this->handleRadius()) / canvas->coordinatesConverter()->effectiveZoom();
360 QPainterPath samplePath;
361 samplePath.addEllipse(pos, handleRadius, handleRadius);
362
363 const QPainterPath selectionPath = selection->outlineCache();
364
365 if (selectionPath.intersects(samplePath) && !selectionPath.contains(samplePath)) {
366 KisNodeSP parent = selection->parentNode();
367 if (parent && parent->isEditable()) {
368 return parent;
369 }
370 }
371 }
372
373 return 0;
374 }
375
376 void keyPressEvent(QKeyEvent *event) override
377 {
378 /* Moving content is one, continuous stroke. Attempting to manipulate
379 * anything mid-stroke wouldn't make sense. */
380 if (isMovingContent()) {
381 return;
382 }
383
385 // Assume all the modifiers were unpressed...
386 m_currentModifiers = Qt::NoModifier;
387 // ...and add those which are right now
388 if (key == Qt::Key_Control || event->modifiers().testFlag(Qt::ControlModifier)) {
389 m_currentModifiers.setFlag(Qt::ControlModifier);
390 }
391 if (key == Qt::Key_Shift || event->modifiers().testFlag(Qt::ShiftModifier)) {
392 m_currentModifiers.setFlag(Qt::ShiftModifier);
393 }
394 if (key == Qt::Key_Alt || event->modifiers().testFlag(Qt::AltModifier)) {
395 m_currentModifiers.setFlag(Qt::AltModifier);
396 }
397
398 // Avoid changing the cursor if the user is interacting
399 if (isSelecting()) {
400 BaseClass::keyPressEvent(event);
401 return;
402 }
403
405 updateCursor();
406 }
407
408 void keyReleaseEvent(QKeyEvent *event) override
409 {
411 // Assume all the modifiers were pressed...
412 m_currentModifiers = Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier;
413 // ...and remove those which aren't right now
414 if (key == Qt::Key_Control || !event->modifiers().testFlag(Qt::ControlModifier)) {
415 m_currentModifiers.setFlag(Qt::ControlModifier, false);
416 }
417 if (key == Qt::Key_Shift || !event->modifiers().testFlag(Qt::ShiftModifier)) {
418 m_currentModifiers.setFlag(Qt::ShiftModifier, false);
419 }
420 if (key == Qt::Key_Alt || !event->modifiers().testFlag(Qt::AltModifier)) {
421 m_currentModifiers.setFlag(Qt::AltModifier, false);
422 }
423
424 // Avoid changing the selection mode and cursor if the user is interacting
425 if (isSelecting()) {
426 BaseClass::keyReleaseEvent(event);
427 return;
428 }
429 if (isMovingSelection()) {
430 return;
431 }
432
434 if (m_currentModifiers == Qt::NoModifier) {
435 updateCursor();
436 }
437 else {
438 this->resetCursorStyle();
439 }
440 }
441
442 void mouseMoveEvent(KoPointerEvent *event) override
443 {
444 m_currentPos = this->convertToPixelCoord(event->point);
445 m_currentModifiers = event->modifiers();
446
448 /* User disabled option while actively moving content? Commit and end */
450 }
451
452 updateCursor();
453 BaseClass::mouseMoveEvent(event);
454 }
455
456 CursorHit checkCursorHit(const QPointF &pos, Qt::KeyboardModifiers modifiers) const
457 {
458 /* User still drawing selection? Then there's really nothing to "hit" per se */
459 if (isSelecting()) {
460 return CursorHit_None;
461 }
462
463 KisCanvas2 *canvas = dynamic_cast<KisCanvas2*>(this->canvas());
465 KisSelectionSP selection = canvas->viewManager()->selection();
466
467 if (!selection || !selection->outlineCacheValid()) {
468 return CursorHit_Outside;
469 }
470
471 const QPainterPath selectionPath = selection->outlineCache();
472
473 if (modifiers == Qt::NoModifier) {
474 const qreal handleRadius = qreal(this->handleRadius()) / canvas->coordinatesConverter()->effectiveZoom();
475
476 QPainterPath samplePath;
477 samplePath.addEllipse(pos, handleRadius, handleRadius);
478
479 if (selectionPath.intersects(samplePath) && !selectionPath.contains(samplePath)) {
480 return CursorHit_Border;
481 }
482 }
483
484 if (selectionPath.contains(pos)) {
485 return CursorHit_Inside;
486 }
487
488 return CursorHit_Outside;
489 }
490
491 inline bool canBeginNewAction(KoPointerEvent *event, const QPointF &pos, CursorHit hit)
492 {
493 /* Prevent interrupting while selecting a region */
494 if (isSelecting()) {
495 BaseClass::beginPrimaryAction(event);
496 return false;
497 }
498
499 /* Prevent interrupting while moving */
500 if (isMovingContent()) {
501 /* We must update the offsets here, so the offset is sane later */
502 m_dragStartPos = pos;
504 /* User clicked outside? Commit changes and start a new stroke */
505 /* This eliminates extra keystrokes to start a new transaction */
506 if (hit == CursorHit_Outside) {
508 BaseClass::beginPrimaryAction(event);
509 return false;
510 }
511 }
512
513 return true;
514 }
515
516 void beginPrimaryAction(KoPointerEvent *event) override
517 {
518 const QPointF pos = this->convertToPixelCoord(event->point);
519 const CursorHit hit = checkCursorHit(pos, event->modifiers());
520
521 if (!canBeginNewAction(event, pos, hit)) {
522 return;
523 }
524
525 KisCanvas2* canvas = dynamic_cast<KisCanvas2*>(this->canvas());
527
528 if (hit == CursorHit_Inside && this->moveSelectedContent()) {
529 KisSelectionSP selection = canvas->viewManager()->selection();
530 KisPaintLayerSP layer = dynamic_cast<KisPaintLayer*>(this->currentNode().data());
531 if (this->beginMoveContentInteraction() && selection && layer) {
532 KisStrokeStrategy *strategy =
533 new MoveSelectionStrokeStrategy(layer, selection, this->image().data(), this->image().data());
534 initializeStrokeAttributes(pos, strategy, true);
535 updateCursor();
536 }
537 return;
538 }
539
541 (hit == CursorHit_Inside || hit == CursorHit_Border)) {
542 // we shouldn't pass the control to the parent tool
543 // when we have the already started the move content action
544 return;
545 }
546
547 KisNodeSP selectionMask = locateSelectionMaskUnderCursor(pos, event->modifiers());
548 if (selectionMask) {
549 if (this->beginMoveSelectionInteraction()) {
550 KisStrokeStrategy *strategy =
551 new MoveStrokeStrategy({selectionMask}, this->image().data(), this->image().data());
552 initializeStrokeAttributes(pos, strategy, true);
553 updateCursor();
554 return;
555 }
556 }
557
558 m_didMove = false;
559 BaseClass::beginPrimaryAction(event);
560 }
561
563 {
565 const QPointF pos = this->convertToPixelCoord(event->point);
566 const QPoint delta = (pos - m_dragStartPos).toPoint();
567 const QPoint offset = m_dragStartOffset + delta;
568 m_accumulatedOffset = offset;
569 this->image()->addJob(m_moveStrokeId, new MoveStrokeStrategy::Data(offset));
570 return;
571 }
572
573 BaseClass::continuePrimaryAction(event);
574 }
575
576 void endPrimaryAction(KoPointerEvent *event) override
577 {
578 if (isMovingContent()) {
579 const QPointF pos = this->convertToPixelCoord(event->point);
580 const QPoint delta = (pos - m_dragStartPos).toPoint();
582 return;
583 }
584 if (isMovingSelection()) {
585 this->image()->endStroke(m_moveStrokeId);
586 m_moveStrokeId.clear();
588 return;
589 }
590
591 BaseClass::endPrimaryAction(event);
592 }
593
594 bool selectionDidMove() const
595 {
596 return m_didMove;
597 }
598
599 QMenu *popupActionsMenu() override
600 {
601 if (isSelecting()) {
602 return BaseClass::popupActionsMenu();
603 }
604
605 KisCanvas2 * kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
607
609 }
610
612 {
613 if (isSelecting()) {
614 return BaseClass::popupWidget();
615 }
616 return nullptr;
617 }
618
619 inline void initializeStrokeAttributes(const QPointF &pos, KisStrokeStrategy *strategy, bool moved) {
620 m_moveStrokeId = this->image()->startStroke(strategy);
621 m_dragStartPos = pos;
622 m_didMove = moved;
623 m_accumulatedOffset = QPoint();
624 m_dragStartOffset = QPoint();
625 }
626
629 return false;
630 }
632 return true;
633 }
634
636 if (!isMovingSelection()) {
637 return false;
638 }
641 return true;
642 }
643
646 return false;
647 }
649 return true;
650 }
651
653 if (!isMovingContent()) {
654 return false;
655 }
658 return true;
659 }
660
663 return false;
664 }
666 return true;
667 }
668
670 if (!isSelecting()) {
671 return false;
672 }
675 return true;
676 }
677
681
685
686 bool isSelecting() const {
688 }
689
691 /* User could disable the option in the middle of an action */
693 }
694
696 {
697
698 const Interaction interaction = currentInteraction();
700
701 switch (interaction)
702 {
704 switch (hit)
705 {
706 case CursorHit_Border:
707 this->useCursor(KisCursor::moveCursor());
708 break;
709 case CursorHit_Inside:
710 this->useCursor(KisCursor::moveCursor());
711 break;
713 this->resetCursorStyle();
714 break;
715 case CursorHit_None:
716 default:
717 this->resetCursorStyle();
718 break;
719 }
720 break;
722 switch (hit)
723 {
724 case CursorHit_Border:
725 this->useCursor(KisCursor::moveSelectionCursor());
726 break;
727 case CursorHit_Inside:
728 this->resetCursorStyle();
729 break;
731 this->resetCursorStyle();
732 break;
733 case CursorHit_None:
734 default:
735 this->resetCursorStyle();
736 break;
737 }
738 break;
740 this->resetCursorStyle();
741 break;
742 case Interaction_None:
743 if (hit == CursorHit_Border){
744 this->useCursor(KisCursor::moveSelectionCursor());
745 }
746 else if (hit == CursorHit_Inside && this->moveSelectedContent()) {
747 this->useCursor(KisCursor::moveCursor());
748 } else {
749 this->resetCursorStyle();
750 }
751 break;
752 default:
753 this->resetCursorStyle();
754 break;
755 }
756 }
757
760 QTimer::singleShot(100, Qt::CoarseTimer,
761 this,
762 [this]()
763 {
764 updateCursor();
765 }
766 );
767 }
768
769protected:
770 using BaseClass::canvas;
773
774 virtual bool isPixelOnly() const {
775 return false;
776 }
777
778 virtual bool usesColorLabels() const {
779 return false;
780 }
781
782private:
790
792
796
797 Qt::KeyboardModifiers m_currentModifiers;
798
804 bool m_didMove = false;
805
807};
808
810{
812 : KisTool(canvas, QCursor())
813 {
814 }
815
816 FakeBaseTool(KoCanvasBase* canvas, const QString &toolName)
817 : KisTool(canvas, QCursor())
818 {
819 Q_UNUSED(toolName);
820 }
821
824 {
825 }
826};
827
828
830
831
832#endif // KISTOOLSELECTBASE_H
SelectionMode
SelectionAction
@ SELECTION_REPLACE
@ SELECTION_INTERSECT
@ SELECTION_DEFAULT
@ SELECTION_SUBTRACT
@ SELECTION_ADD
KisCoordinatesConverter * coordinatesConverter
KisViewManager * viewManager() const
static QCursor moveSelectionCursor()
static QCursor moveCursor()
static Qt::Key workaroundShiftAltMetaHell(const QKeyEvent *keyEvent)
The PopupWidgetInterface abstract class defines the basic interface that will be used by all popup wi...
void updateActionButtonToolTip(SelectionAction action, const QKeySequence &shortcut)
void setModeSectionVisible(bool visible)
void setAdjustmentsSectionVisible(bool visible)
void setReferenceSectionVisible(bool visible)
KisSelectionOptions::ReferenceLayers referenceLayers() const
static QMenu * getSelectionContextMenu(KisCanvas2 *canvas)
void addUniqueConnection(Sender sender, Signal signal, Receiver receiver, Method method)
void initializeStrokeAttributes(const QPointF &pos, KisStrokeStrategy *strategy, bool moved)
SelectionAction alternateSelectionAction() const
KisSelectionOptions * selectionOptionWidget()
SampleLayersMode sampleLayersMode() const
KisToolSelectBase(KoCanvasBase *canvas, const QString toolName)
void keyReleaseEvent(QKeyEvent *event) override
void requestStrokeCancellation() override
Interaction currentInteraction() const
void mouseMoveEvent(KoPointerEvent *event) override
SelectionAction selectionAction() const
void beginAlternateAction(KoPointerEvent *event, KisTool::AlternateAction action) override
void beginPrimaryAction(KoPointerEvent *event) override
void endAlternateAction(KoPointerEvent *event, KisTool::AlternateAction action) override
virtual void setAlternateSelectionAction(SelectionAction action)
void explicitUserStrokeEndRequest() override
void requestStrokeEnd() override
QList< int > colorLabelsSelected() const
virtual bool isPixelOnly() const
KisToolSelectBase(KoCanvasBase *canvas, const QCursor cursor, const QString toolName)
void commitMoveSelectionStrokeImpl(bool cancel)
void activate(const QSet< KoShape * > &shapes) override
virtual bool usesColorLabels() const
KisSelectionToolConfigWidgetHelper m_widgetHelper
void deactivateAlternateAction(KisTool::AlternateAction action) override
KisSignalAutoConnectionsStore m_modeConnections
SelectionMode selectionMode() const
CursorHit checkCursorHit(const QPointF &pos, Qt::KeyboardModifiers modifiers) const
void keyPressEvent(QKeyEvent *event) override
void continuePrimaryAction(KoPointerEvent *event) override
void activateAlternateAction(KisTool::AlternateAction action) override
KisNodeSP locateSelectionMaskUnderCursor(const QPointF &pos, Qt::KeyboardModifiers modifiers)
void endPrimaryAction(KoPointerEvent *event) override
void continueAlternateAction(KoPointerEvent *event, KisTool::AlternateAction action) override
QMenu * popupActionsMenu() override
bool stopGrowingAtDarkestPixel() const
void deactivate() override
bool moveSelectedContent() const
bool canBeginNewAction(KoPointerEvent *event, const QPointF &pos, CursorHit hit)
QWidget * createOptionWidget() override
bool antiAliasSelection() const
SelectionAction m_selectionActionAlternate
bool disabledMoveContentWhileMovingContent() const
Qt::KeyboardModifiers m_currentModifiers
KisPopupWidgetInterface * popupWidget() override
KisToolSelectBase(KoCanvasBase *canvas, QCursor cursor, QString toolName, KoToolBase *delegateTool)
KisSelectionSP selection()
Qt::KeyboardModifiers modifiers() const
QPointF point
The point in document coordinates.
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
KisToolSelectBase< FakeBaseTool > KisToolSelect
FakeBaseTool(KoCanvasBase *canvas)
FakeBaseTool(KoCanvasBase *canvas, const QString &toolName)
FakeBaseTool(KoCanvasBase *canvas, const QCursor &cursor)
static KisSelectionModifierMapper * instance()
SelectionAction map(Qt::KeyboardModifiers m)
bool outlineCacheValid() const
KisNodeWSP parentNode
QPainterPath outlineCache() const
QCursor cursor
Definition kis_tool.cc:64
AlternateAction
Definition kis_tool.h:136
KisCanvas2 * canvas