File indexing completed on 2024-12-22 04:12:58

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_figure_painting_tool_helper.h"
0008 
0009 #include <KoCanvasResourceProvider.h>
0010 
0011 #include "kis_resources_snapshot.h"
0012 #include <kis_distance_information.h>
0013 #include "kis_image.h"
0014 #include "kis_painter.h"
0015 #include <strokes/KisFreehandStrokeInfo.h>
0016 #include "KisAsynchronousStrokeUpdateHelper.h"
0017 
0018 
0019 KisFigurePaintingToolHelper::KisFigurePaintingToolHelper(const KUndo2MagicString &name,
0020                                                          KisImageWSP image,
0021                                                          KisNodeSP currentNode,
0022                                                          KoCanvasResourceProvider *resourceManager,
0023                                                          KisToolShapeUtils::StrokeStyle strokeStyle,
0024                                                          KisToolShapeUtils::FillStyle fillStyle,
0025                                                          QTransform fillTransform)
0026 {
0027     m_strokesFacade = image.data();
0028 
0029     m_resources =
0030         new KisResourcesSnapshot(image,
0031                                  currentNode,
0032                                  resourceManager);
0033 
0034     setupPaintStyles(m_resources, strokeStyle, fillStyle, fillTransform);
0035 
0036     KisFreehandStrokeInfo *strokeInfo = new KisFreehandStrokeInfo();
0037 
0038     KisStrokeStrategy *stroke =
0039         new FreehandStrokeStrategy(m_resources, strokeInfo, name);
0040 
0041     m_strokeId = m_strokesFacade->startStroke(stroke);
0042 }
0043 
0044 void KisFigurePaintingToolHelper::setupPaintStyles(KisResourcesSnapshotSP resources,
0045                                                    KisToolShapeUtils::StrokeStyle strokeStyle,
0046                                                    KisToolShapeUtils::FillStyle fillStyle,
0047                                                    QTransform fillTransform)
0048 {
0049     using namespace KisToolShapeUtils;
0050 
0051     const KoColor fgColor = resources->currentFgColor();
0052     const KoColor bgColor = resources->currentBgColor();
0053 
0054     switch (strokeStyle) {
0055     case StrokeStyleNone:
0056         resources->setStrokeStyle(KisPainter::StrokeStyleNone);
0057         break;
0058     case StrokeStyleForeground:
0059         resources->setStrokeStyle(KisPainter::StrokeStyleBrush);
0060         break;
0061     case StrokeStyleBackground:
0062         resources->setStrokeStyle(KisPainter::StrokeStyleBrush);
0063 
0064         resources->setFGColorOverride(bgColor);
0065         resources->setBGColorOverride(fgColor);
0066 
0067         if (fillStyle == FillStyleForegroundColor) {
0068             fillStyle = FillStyleBackgroundColor;
0069         } else if (fillStyle == FillStyleBackgroundColor) {
0070             fillStyle = FillStyleForegroundColor;
0071         }
0072 
0073         break;
0074     };
0075 
0076     switch (fillStyle) {
0077     case FillStyleForegroundColor:
0078         resources->setFillStyle(KisPainter::FillStyleForegroundColor);
0079         break;
0080     case FillStyleBackgroundColor:
0081         resources->setFillStyle(KisPainter::FillStyleBackgroundColor);
0082         break;
0083     case FillStylePattern:
0084         resources->setFillStyle(KisPainter::FillStylePattern);
0085         break;
0086     case FillStyleNone:
0087         resources->setFillStyle(KisPainter::FillStyleNone);
0088         break;
0089     }
0090 
0091     resources->setFillTransform(fillTransform);
0092 }
0093 
0094 KisFigurePaintingToolHelper::~KisFigurePaintingToolHelper()
0095 {
0096     m_strokesFacade->addJob(m_strokeId,
0097         new KisAsynchronousStrokeUpdateHelper::UpdateData(true));
0098     m_strokesFacade->endStroke(m_strokeId);
0099 }
0100 
0101 void KisFigurePaintingToolHelper::paintLine(const KisPaintInformation &pi0,
0102                                             const KisPaintInformation &pi1)
0103 {
0104     m_strokesFacade->addJob(m_strokeId,
0105         new FreehandStrokeStrategy::Data(0,
0106                                          pi0, pi1));
0107 }
0108 
0109 void KisFigurePaintingToolHelper::paintPolyline(const vQPointF &points)
0110 {
0111     m_strokesFacade->addJob(m_strokeId,
0112         new FreehandStrokeStrategy::Data(0,
0113                                          FreehandStrokeStrategy::Data::POLYLINE,
0114                                          points));
0115 }
0116 
0117 void KisFigurePaintingToolHelper::paintPolygon(const vQPointF &points)
0118 {
0119     m_strokesFacade->addJob(m_strokeId,
0120         new FreehandStrokeStrategy::Data(0,
0121                                          FreehandStrokeStrategy::Data::POLYGON,
0122                                          points));
0123 }
0124 
0125 void KisFigurePaintingToolHelper::paintRect(const QRectF &rect)
0126 {
0127     m_strokesFacade->addJob(m_strokeId,
0128         new FreehandStrokeStrategy::Data(0,
0129                                          FreehandStrokeStrategy::Data::RECT,
0130                                          rect));
0131 }
0132 
0133 void KisFigurePaintingToolHelper::paintEllipse(const QRectF &rect)
0134 {
0135     m_strokesFacade->addJob(m_strokeId,
0136         new FreehandStrokeStrategy::Data(0,
0137                                          FreehandStrokeStrategy::Data::ELLIPSE,
0138                                          rect));
0139 }
0140 
0141 void KisFigurePaintingToolHelper::paintPainterPath(const QPainterPath &path)
0142 {
0143     m_strokesFacade->addJob(m_strokeId,
0144         new FreehandStrokeStrategy::Data(0,
0145                                          FreehandStrokeStrategy::Data::PAINTER_PATH,
0146                                          path));
0147 }
0148 
0149 void KisFigurePaintingToolHelper::setFGColorOverride(const KoColor &color)
0150 {
0151     m_resources->setFGColorOverride(color);
0152 }
0153 
0154 void KisFigurePaintingToolHelper::setBGColorOverride(const KoColor &color)
0155 {
0156     m_resources->setBGColorOverride(color);
0157 }
0158 
0159 void KisFigurePaintingToolHelper::setSelectionOverride(KisSelectionSP m_selection)
0160 {
0161     m_resources->setSelectionOverride(m_selection);
0162 }
0163 
0164 void KisFigurePaintingToolHelper::setBrush(const KisPaintOpPresetSP &brush)
0165 {
0166     m_resources->setBrush(brush);
0167 }
0168 
0169 void KisFigurePaintingToolHelper::paintPainterPathQPen(const QPainterPath path, const QPen &pen, const KoColor &color)
0170 {
0171     m_strokesFacade->addJob(m_strokeId,
0172         new FreehandStrokeStrategy::Data(0,
0173                                          FreehandStrokeStrategy::Data::QPAINTER_PATH,
0174                                          path, pen, color));
0175 }
0176 
0177 void KisFigurePaintingToolHelper::paintPainterPathQPenFill(const QPainterPath path, const QPen &pen, const KoColor &color)
0178 {
0179     m_strokesFacade->addJob(m_strokeId,
0180         new FreehandStrokeStrategy::Data(0,
0181                                          FreehandStrokeStrategy::Data::QPAINTER_PATH_FILL,
0182                                          path, pen, color));
0183 }