File indexing completed on 2024-05-26 04:34:40

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