Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tool_select_rectangular.cc
Go to the documentation of this file.
1/*
2 * kis_tool_select_rectangular.cc -- part of Krita
3 *
4 * SPDX-FileCopyrightText: 1999 Michael Koch <koch@kde.org>
5 * SPDX-FileCopyrightText: 2001 John Califf <jcaliff@compuzone.net>
6 * SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.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_painter.h"
18#include "kis_canvas2.h"
19#include "kis_pixel_selection.h"
22#include <kis_default_bounds.h>
23
24#include "KisViewManager.h"
26#include <kis_command_utils.h>
28
31 KisCursor::load("tool_rectangular_selection_cursor.png", 6, 6))
32{
33 setObjectName("tool_select_rectangular");
34}
35
36
40
41void KisToolSelectRectangular::finishRect(const QRectF& rect, qreal roundCornersX, qreal roundCornersY)
42{
43 KisCanvas2 * kisCanvas = dynamic_cast<KisCanvas2*>(canvas());
44 if (!kisCanvas)
45 return;
46
47 KisSelectionToolHelper helper(kisCanvas, kundo2_i18n("Select Rectangle"));
48
49 QRect rc(rect.normalized().toRect());
50
52 return;
53 }
54
55 if (helper.canShortcutToNoop(rc, selectionAction())) {
56 return;
57 }
58
59 const SelectionMode mode =
60 helper.tryOverrideSelectionMode(kisCanvas->viewManager()->selection(),
63
64 if (!rc.isValid()) {
65 return;
66 }
67 if (mode == PIXEL_SELECTION) {
72 kundo2_i18n("Select Rectangle"));
73
74 KisPixelSelectionSP tmpSel =
76
77 const bool antiAlias = antiAliasSelection();
78 const int grow = growSelection();
79 const int feather = featherSelection();
80
81 QPainterPath path;
82 if (roundCornersX > 0 || roundCornersY > 0) {
83 path.addRoundedRect(rc, roundCornersX, roundCornersY);
84 } else {
85 path.addRect(rc);
86 }
87 getRotatedPath(path, rc.center(), getRotationAngle());
88
90 [tmpSel, antiAlias, grow, feather, path]() mutable
91 -> KUndo2Command * {
92 KisPainter painter(tmpSel);
93 painter.setPaintColor(KoColor(Qt::black, tmpSel->colorSpace()));
94 // Since the feathering already smooths the selection, the
95 // antiAlias is not applied if we must feather
96 painter.setAntiAliasPolygonFill(antiAlias && feather == 0);
99
100 painter.paintPainterPath(path);
101
102 if (grow > 0) {
103 KisGrowSelectionFilter biggy(grow, grow);
104 biggy.process(tmpSel,
105 tmpSel->selectedRect().adjusted(-grow,
106 -grow,
107 grow,
108 grow));
109 } else if (grow < 0) {
110 KisShrinkSelectionFilter tiny(-grow, -grow, false);
111 tiny.process(tmpSel, tmpSel->selectedRect());
112 }
113 if (feather > 0) {
114 KisFeatherSelectionFilter feathery(feather);
115 feathery.process(tmpSel,
116 tmpSel->selectedRect().adjusted(-feather,
117 -feather,
118 feather,
119 feather));
120 }
121
122 if (grow == 0 && feather == 0) {
123 tmpSel->setOutlineCache(path);
124 } else {
125 tmpSel->invalidateOutlineCache();
126 }
127
128 return 0;
129 });
130
132 helper.selectPixelSelection(applicator, tmpSel, selectionAction());
133 applicator.end();
134
135 } else {
136 QRectF documentRect = convertToPt(rc);
137 const qreal docRoundCornersX = convertToPt(roundCornersX);
138 const qreal docRoundCornersY = convertToPt(roundCornersY);
139
141 docRoundCornersX,
142 docRoundCornersY);
143 shape->rotate(qRadiansToDegrees(getRotationAngle()));
144 helper.addSelectionShape(shape, selectionAction());
145 }
146}
147
152
157
159{
161 useCursor(KisCursor::load("tool_rectangular_selection_cursor_add.png", 6, 6));
162 } else if (selectionAction() == SELECTION_SUBTRACT) {
163 useCursor(KisCursor::load("tool_rectangular_selection_cursor_sub.png", 6, 6));
164 } else if (selectionAction() == SELECTION_INTERSECT) {
165 useCursor(KisCursor::load("tool_rectangular_selection_cursor_inter.png", 6, 6));
167 useCursor(KisCursor::load("tool_rectangular_selection_cursor_symdiff.png", 6, 6));
168 } else {
170 }
171}
172
QVector< KisImageSignalType > KisImageSignalVector
SelectionMode
@ PIXEL_SELECTION
@ SELECTION_INTERSECT
@ SELECTION_SYMMETRICDIFFERENCE
@ SELECTION_SUBTRACT
@ SELECTION_ADD
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)
bool canShortcutToNoop(const QRect &rect, SelectionAction action)
SelectionMode tryOverrideSelectionMode(KisSelectionSP activeSelection, SelectionMode currentMode, SelectionAction currentAction) const
bool tryDeselectCurrentSelection(const QRectF selectionViewRect, SelectionAction action)
static KoShape * createRectangleShape(const QRectF &rect, qreal roundCornersX, qreal roundCornersY)
void process(KisPixelSelectionSP pixelSelection, const QRect &rect) override
void getRotatedPath(QPainterPath &path, const QPointF &center, const qreal &angle)
void finishRect(const QRectF &rect, qreal roundCornersX, qreal roundCornersY) override
KisToolSelectRectangular(KoCanvasBase *canvas)
KisSelectionSP selection()
void rotate(qreal angle)
Rotate the shape (relative)
Definition KoShape.cpp:250
void useCursor(const QCursor &cursor)
KUndo2MagicString kundo2_i18n(const char *text)
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
QRectF convertToPt(const QRectF &rect)
Definition kis_tool.cc:252
KisCanvas2 * canvas