File indexing completed on 2024-06-09 04:28:46

0001 /*
0002  *  kis_tool_polygon.cc -- part of Krita
0003  *
0004  *  SPDX-FileCopyrightText: 2004 Michael Thaler <michael.thaler@physik.tu-muenchen.de>
0005  *  SPDX-FileCopyrightText: 2009 Lukáš Tvrdý <lukast.dev@gmail.com>
0006  *  SPDX-FileCopyrightText: 2010 Cyrille Berger <cberger@cberger.net>
0007  *  SPDX-FileCopyrightText: 2010 Boudewijn Rempt <boud@valdyas.org>
0008  *
0009  *  SPDX-License-Identifier: GPL-2.0-or-later
0010  */
0011 
0012 #include "kis_tool_polygon.h"
0013 
0014 #include <KoPointerEvent.h>
0015 #include <KoCanvasBase.h>
0016 #include <KoPathShape.h>
0017 #include <KoShapeStroke.h>
0018 
0019 #include <brushengine/kis_paintop_registry.h>
0020 #include "kis_figure_painting_tool_helper.h"
0021 
0022 KisToolPolygon::KisToolPolygon(KoCanvasBase *canvas)
0023         : KisToolPolylineBase(canvas,  KisToolPolylineBase::PAINT, KisCursor::load("tool_polygon_cursor.png", 6, 6))
0024 {
0025     setObjectName("tool_polygon");
0026     setSupportOutline(true);
0027 }
0028 
0029 KisToolPolygon::~KisToolPolygon()
0030 {
0031 }
0032 
0033 void KisToolPolygon::resetCursorStyle()
0034 {
0035     if (isEraser()) {
0036         useCursor(KisCursor::load("tool_polygon_eraser_cursor.png", 6, 6));
0037     } else {
0038         KisToolPolylineBase::resetCursorStyle();
0039     }
0040 
0041     overrideCursorIfNotEditable();
0042 }
0043 
0044 void KisToolPolygon::finishPolyline(const QVector<QPointF>& points)
0045 {
0046     const KisToolShape::ShapeAddInfo info =
0047         shouldAddShape(currentNode());
0048 
0049     if (!info.shouldAddShape) {
0050         KisFigurePaintingToolHelper helper(kundo2_i18n("Draw Polygon"),
0051                                            image(),
0052                                            currentNode(),
0053                                            canvas()->resourceManager(),
0054                                            strokeStyle(),
0055                                            fillStyle(),
0056                                            fillTransform());
0057         helper.paintPolygon(points);
0058     } else {
0059         // remove the last point if it overlaps with the first
0060         QVector<QPointF> newPoints = points;
0061         if (newPoints.size() > 1 && newPoints.first() == newPoints.last()) {
0062             newPoints.removeLast();
0063         }
0064         KoPathShape* path = new KoPathShape();
0065         path->setShapeId(KoPathShapeId);
0066 
0067         QTransform resolutionMatrix;
0068         resolutionMatrix.scale(1 / currentImage()->xRes(), 1 / currentImage()->yRes());
0069         path->moveTo(resolutionMatrix.map(newPoints[0]));
0070         for (int i = 1; i < newPoints.size(); i++)
0071             path->lineTo(resolutionMatrix.map(newPoints[i]));
0072         path->close();
0073         path->normalize();
0074 
0075         info.markAsSelectionShapeIfNeeded(path);
0076 
0077         addShape(path);
0078     }
0079 }
0080