Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_select_outline.cc
Go to the documentation of this file.
1/*
2 * kis_tool_select_freehand.h - part of Krayon^WKrita
3 *
4 * SPDX-FileCopyrightText: 2000 John Califf <jcaliff@compuzone.net>
5 * SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
6 * SPDX-FileCopyrightText: 2004 Boudewijn Rempt <boud@valdyas.org>
7 * SPDX-FileCopyrightText: 2007 Sven Langkamp <sven.langkamp@gmail.com>
8 * SPDX-FileCopyrightText: 2015 Michael Abrahams <miabraha@gmail.com>
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
14
15#include <kis_debug.h>
16#include <klocalizedstring.h>
17
18#include <KoPointerEvent.h>
19#include <KoShapeController.h>
20#include <KoPathShape.h>
21#include <KoColorSpace.h>
22#include <KoCompositeOp.h>
23#include <KoViewConverter.h>
24
25#include <kis_layer.h>
27#include <kis_cursor.h>
28#include <kis_image.h>
29#include <kis_default_bounds.h>
30
31#include "canvas/kis_canvas2.h"
32#include "kis_painter.h"
33#include "kis_pixel_selection.h"
36#include <kis_command_utils.h>
39
40#include "kis_algebra_2d.h"
41
43 : KisToolOutlineBase(canvas, KisToolOutlineBase::SELECT,
44 KisCursor::load("tool_outline_selection_cursor.png", 5, 5))
45{
46 setObjectName("tool_select_outline");
47}
48
49
53
55{
56 KisCanvas2 * kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
58 kisCanvas->updateCanvas();
59
60 const QRectF boundingRect = KisAlgebra2D::accumulateBounds(points);
61 const QRectF boundingViewRect = pixelToView(boundingRect);
62
63 KisSelectionToolHelper helper(kisCanvas, kundo2_i18n("Freehand Selection"));
64
65 if (helper.tryDeselectCurrentSelection(boundingViewRect, selectionAction())) {
67 return;
68 }
69
70 if (points.count() < 3) {
71 return;
72 }
73
74 KisCursorOverrideLock cursorLock(Qt::WaitCursor);
75
76 const SelectionMode mode =
77 helper.tryOverrideSelectionMode(kisCanvas->viewManager()->selection(),
80
81 if (mode == PIXEL_SELECTION) {
86 kundo2_i18n("Freehand Selection"));
87
88 KisPixelSelectionSP tmpSel =
90
91 const bool antiAlias = antiAliasSelection();
92 const int grow = growSelection();
93 const int feather = featherSelection();
94
95 QPainterPath path;
96 path.addPolygon(points);
97 path.closeSubpath();
98
100 [tmpSel, antiAlias, grow, feather, path]() mutable
101 -> KUndo2Command * {
102 KisPainter painter(tmpSel);
103 painter.setPaintColor(KoColor(Qt::black, tmpSel->colorSpace()));
104 // Since the feathering already smooths the selection, the
105 // antiAlias is not applied if we must feather
106 painter.setAntiAliasPolygonFill(antiAlias && feather == 0);
109
110 painter.paintPainterPath(path);
111
112 if (grow > 0) {
113 KisGrowSelectionFilter biggy(grow, grow);
114 biggy.process(tmpSel,
115 tmpSel->selectedRect().adjusted(-grow,
116 -grow,
117 grow,
118 grow));
119 } else if (grow < 0) {
120 KisShrinkSelectionFilter tiny(-grow, -grow, false);
121 tiny.process(tmpSel, tmpSel->selectedRect());
122 }
123 if (feather > 0) {
124 KisFeatherSelectionFilter feathery(feather);
125 feathery.process(tmpSel,
126 tmpSel->selectedRect().adjusted(-feather,
127 -feather,
128 feather,
129 feather));
130 }
131
132 if (grow == 0 && feather == 0) {
133 tmpSel->setOutlineCache(path);
134 } else {
135 tmpSel->invalidateOutlineCache();
136 }
137
138 return 0;
139 });
140
142 helper.selectPixelSelection(applicator, tmpSel, selectionAction());
143 applicator.end();
144
145 } else {
146 KoPathShape *path = new KoPathShape();
147 path->setShapeId(KoPathShapeId);
148
149 QTransform resolutionMatrix;
150 resolutionMatrix.scale(1 / currentImage()->xRes(),
151 1 / currentImage()->yRes());
152 path->moveTo(resolutionMatrix.map(points[0]));
153 for (int i = 1; i < points.count(); i++)
154 path->lineTo(resolutionMatrix.map(points[i]));
155 path->close();
156 path->normalize();
157
158 helper.addSelectionShape(path, selectionAction());
159 }
160}
161
166
171
176
188
190{
192 useCursor(KisCursor::load("tool_outline_selection_cursor_add.png", 5, 5));
193 } else if (selectionAction() == SELECTION_SUBTRACT) {
194 useCursor(KisCursor::load("tool_outline_selection_cursor_sub.png", 5, 5));
195 } else if (selectionAction() == SELECTION_INTERSECT) {
196 useCursor(KisCursor::load("tool_outline_selection_cursor_inter.png", 5, 5));
198 useCursor(KisCursor::load("tool_outline_selection_cursor_symdiff.png", 5, 5));
199 } else {
201 }
202}
203
QVector< KisImageSignalType > KisImageSignalVector
SelectionMode
@ PIXEL_SELECTION
@ SELECTION_INTERSECT
@ SELECTION_SYMMETRICDIFFERENCE
@ SELECTION_SUBTRACT
@ SELECTION_ADD
#define KoPathShapeId
Definition KoPathShape.h:20
void updateCanvas(const QRectF &rc) override
KisViewManager * viewManager() const
static QCursor load(const QString &cursorName, int hotspotX=-1, int hotspotY=-1)
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
const KoColorSpace * colorSpace() const
@ FillStyleForegroundColor
void paintPainterPath(const QPainterPath &path)
void setStrokeStyle(StrokeStyle strokeStyle)
Set the current brush stroke style.
void setFillStyle(FillStyle fillStyle)
Set the current style with which to fill.
void setPaintColor(const KoColor &color)
void setAntiAliasPolygonFill(bool antiAliasPolygonFill)
Set whether a polygon's filled area should be anti-aliased or not. The default is true.
void applyCommand(KUndo2Command *command, KisStrokeJobData::Sequentiality sequentiality=KisStrokeJobData::SEQUENTIAL, KisStrokeJobData::Exclusivity exclusivity=KisStrokeJobData::NORMAL)
void addSelectionShape(KoShape *shape, SelectionAction action=SELECTION_DEFAULT)
void selectPixelSelection(KisProcessingApplicator &applicator, KisPixelSelectionSP selection, SelectionAction action)
SelectionMode tryOverrideSelectionMode(KisSelectionSP activeSelection, SelectionMode currentMode, SelectionAction currentAction) const
bool tryDeselectCurrentSelection(const QRectF selectionViewRect, SelectionAction action)
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
KisToolSelectOutline(KoCanvasBase *canvas)
bool primaryActionSupportsHiResEvents() const override
bool alternateActionSupportsHiResEvents(KisTool::AlternateAction action) const override
void finishOutline(const QVector< QPointF > &points) override
KisSelectionSP selection()
The position of a path point within a path shape.
Definition KoPathShape.h:63
void useCursor(const QCursor &cursor)
QAction * action(const QString &name) const
__KisToolSelectOutlineLocal(KoCanvasBase *canvas)
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
KUndo2MagicString kundo2_i18n(const char *text)
void accumulateBounds(const Point &pt, Rect *bounds)
The LambdaCommand struct is a shorthand for creation of AggregateCommand commands using C++ lambda fe...
void setOutlineCache(const QPainterPath &cache)
virtual ToolMode mode() const
Definition kis_tool.cc:407
KisImageWSP currentImage()
Definition kis_tool.cc:393
QPointF pixelToView(const QPoint &pixelCoord) const
Definition kis_tool.cc:269
KisNodeSP currentNode() const
Definition kis_tool.cc:370
AlternateAction
Definition kis_tool.h:134
KisCanvas2 * canvas