Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_select_polygonal.cc
Go to the documentation of this file.
1/*
2 * kis_tool_select_polygonal.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 <KoPathShape.h>
16
17#include "kis_algebra_2d.h"
18#include "kis_painter.h"
21#include "kis_canvas2.h"
22#include "kis_pixel_selection.h"
25#include <kis_default_bounds.h>
26
27#include "KisViewManager.h"
29#include <kis_command_utils.h>
31
34 KisCursor::load("tool_polygonal_selection_cursor.png", 6, 6))
35{
36 setObjectName("tool_select_polygonal");
37}
38
39
44
46{
47 KisCanvas2 * kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
48 Q_ASSERT(kisCanvas);
49 if (!kisCanvas)
50 return;
51
52 const QRectF boundingViewRect = pixelToView(KisAlgebra2D::accumulateBounds(points));
53
54 KisSelectionToolHelper helper(kisCanvas, kundo2_i18n("Select Polygon"));
55
56 if (helper.tryDeselectCurrentSelection(pixelToView(boundingViewRect), selectionAction())) {
57 return;
58 }
59
60 const SelectionMode mode =
61 helper.tryOverrideSelectionMode(kisCanvas->viewManager()->selection(),
64
65 if (mode == PIXEL_SELECTION) {
70 kundo2_i18n("Select Polygon"));
71
72 KisPixelSelectionSP tmpSel =
74
75 const bool antiAlias = antiAliasSelection();
76 const int grow = growSelection();
77 const int feather = featherSelection();
78
79 QPainterPath path;
80 path.addPolygon(points);
81 path.closeSubpath();
82
84 [tmpSel, antiAlias, grow, feather, path]() mutable
85 -> KUndo2Command * {
86 KisPainter painter(tmpSel);
87 painter.setPaintColor(KoColor(Qt::black, tmpSel->colorSpace()));
88 // Since the feathering already smooths the selection, the
89 // antiAlias is not applied if we must feather
90 painter.setAntiAliasPolygonFill(antiAlias && feather == 0);
93
94 painter.paintPainterPath(path);
95
96 if (grow > 0) {
97 KisGrowSelectionFilter biggy(grow, grow);
98 biggy.process(tmpSel,
99 tmpSel->selectedRect().adjusted(-grow,
100 -grow,
101 grow,
102 grow));
103 } else if (grow < 0) {
104 KisShrinkSelectionFilter tiny(-grow, -grow, false);
105 tiny.process(tmpSel, tmpSel->selectedRect());
106 }
107 if (feather > 0) {
108 KisFeatherSelectionFilter feathery(feather);
109 feathery.process(tmpSel,
110 tmpSel->selectedRect().adjusted(-feather,
111 -feather,
112 feather,
113 feather));
114 }
115
116 if (grow == 0 && feather == 0) {
117 tmpSel->setOutlineCache(path);
118 } else {
119 tmpSel->invalidateOutlineCache();
120 }
121
122 return 0;
123 });
124
126 helper.selectPixelSelection(applicator, tmpSel, selectionAction());
127 applicator.end();
128
129 } else {
130 KoPathShape* path = new KoPathShape();
131 path->setShapeId(KoPathShapeId);
132
133 QTransform resolutionMatrix;
134 resolutionMatrix.scale(1 / currentImage()->xRes(), 1 / currentImage()->yRes());
135 path->moveTo(resolutionMatrix.map(points[0]));
136 for (int i = 1; i < points.count(); i++)
137 path->lineTo(resolutionMatrix.map(points[i]));
138 path->close();
139 path->normalize();
140
141 helper.addSelectionShape(path, selectionAction());
142 }
143}
144
149
154
156{
158 useCursor(KisCursor::load("tool_polygonal_selection_cursor_add.png", 6, 6));
159 } else if (selectionAction() == SELECTION_SUBTRACT) {
160 useCursor(KisCursor::load("tool_polygonal_selection_cursor_sub.png", 6, 6));
161 } else if (selectionAction() == SELECTION_INTERSECT) {
162 useCursor(KisCursor::load("tool_polygonal_selection_cursor_inter.png", 6, 6));
164 useCursor(KisCursor::load("tool_polygonal_selection_cursor_symdiff.png", 6, 6));
165 } else {
167 }
168}
169
QVector< KisImageSignalType > KisImageSignalVector
SelectionMode
@ PIXEL_SELECTION
@ SELECTION_INTERSECT
@ SELECTION_SYMMETRICDIFFERENCE
@ SELECTION_SUBTRACT
@ SELECTION_ADD
#define KoPathShapeId
Definition KoPathShape.h:20
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
void finishPolyline(const QVector< QPointF > &points) override
KisToolSelectPolygonal(KoCanvasBase *canvas)
KisSelectionSP selection()
The position of a path point within a path shape.
Definition KoPathShape.h:63
void useCursor(const QCursor &cursor)
__KisToolSelectPolygonalLocal(KoCanvasBase *canvas)
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
KisCanvas2 * canvas