Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Expose stroke and fill styles to painting API
Freya Lupend43bdf4d8861
diff --git a/libs/libkis/Node.cpp b/libs/libkis/Node.cppindex cac2c181e2..7e15b58c2c 100644--- a/libs/libkis/Node.cpp+++ b/libs/libkis/Node.cpp@@ -863,7 +863,7 @@ QString Node::paintAbility()return "UNPAINTABLE";}-void Node::paintLine(const QPointF pointOne, const QPointF pointTwo)+void Node::paintLine(const QPointF pointOne, const QPointF pointTwo, const QString strokeStyle){if (paintAbility() != "PAINT") {dbgScript << "Script attempted to use Node::paintLine() on an unpaintable node, ignoring.";@@ -878,12 +878,12 @@ void Node::paintLine(const QPointF pointOne, const QPointF pointTwo)pointTwoInfo.setPressure(1.0);pointTwoInfo.setPos(pointTwo);- KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image);+ KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image, strokeStyle);helper.paintLine(pointOneInfo, pointTwoInfo);}-void Node::paintRectangle(const QRectF &rect)+void Node::paintRectangle(const QRectF &rect, const QString strokeStyle, const QString fillStyle){if (paintAbility() != "PAINT") {dbgScript << "Script attempted to use Node::paintRectangle() on an unpaintable node, ignoring.";@@ -893,12 +893,12 @@ void Node::paintRectangle(const QRectF &rect)// reference class where this stuff is being done. Maybe can use the "facade" like that does for setup?// void KisFigurePaintingToolHelper::paintRect(const QRectF &rect)- KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image);+ KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image, strokeStyle, fillStyle);helper.paintRect(rect);}-void Node::paintPolygon(const QList<QPointF> listPoint)+void Node::paintPolygon(const QList<QPointF> listPoint, const QString strokeStyle, const QString fillStyle){if (paintAbility() != "PAINT") {dbgScript << "Script attempted to use Node::paintPolygon() on an unpaintable node, ignoring.";@@ -907,30 +907,30 @@ void Node::paintPolygon(const QList<QPointF> listPoint)// strategy needs points in vPointF formatQVector<QPointF> points = points.fromList(listPoint);- KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image);+ KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image, strokeStyle, fillStyle);helper.paintPolygon(points);}-void Node::paintEllipse(const QRectF &rect)+void Node::paintEllipse(const QRectF &rect, const QString strokeStyle, const QString fillStyle){if (paintAbility() != "PAINT") {dbgScript << "Script attempted to use Node::paintEllipse() on an unpaintable node, ignoring.";return;}- KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image);+ KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image, strokeStyle, fillStyle);helper.paintEllipse(rect);}-void Node::paintPath(const QPainterPath &path)+void Node::paintPath(const QPainterPath &path, const QString strokeStyle, const QString fillStyle){if (paintAbility() != "PAINT") {dbgScript << "Script attempted to use Node::paintPath() on an unpaintable node, ignoring.";return;}- KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image);+ KisFigurePaintingToolHelper helper = PaintingResources::createHelper(d->image, strokeStyle, fillStyle);helper.paintPainterPath(path);}diff --git a/libs/libkis/Node.h b/libs/libkis/Node.hindex 89a6f405e0..ecf9845a22 100644--- a/libs/libkis/Node.h+++ b/libs/libkis/Node.h@@ -13,6 +13,8 @@#include "kritalibkis_export.h"#include "libkis.h"+#include "PaintingResources.h"+/*** Node represents a layer or mask in a Krita image's Node hierarchy. Group layers can contain* other layers and masks; layers can contain masks.@@ -574,33 +576,101 @@ public Q_SLOTS:* @brief paint a line on the canvas. Uses current brush preset* @param pointOne starting point* @param pointTwo end point+ * @param strokeStyle appearance of the outline, one of:+ * <ul>+ * <li>None - will use Foreground Color, since line would be invisible otherwise+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * </ul>*/- void paintLine(const QPointF pointOne, const QPointF pointTwo);+ void paintLine(const QPointF pointOne, const QPointF pointTwo, const QString strokeStyle = PaintingResources::defaultStrokeStyle);/*** @brief paint a rectangle on the canvas. Uses current brush preset* @param rect QRect with x, y, width, and height+ * @param strokeStyle appearance of the outline, one of:+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * </ul>+ * Default is ForegroundColor.+ * @param fillStyle appearance of the fill, one of:+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * <li>Pattern</li>+ * </ul>+ * Default is None.*/- void paintRectangle(const QRectF &rect);+ void paintRectangle(const QRectF &rect,+ const QString strokeStyle = PaintingResources::defaultStrokeStyle,+ const QString fillStyle = PaintingResources::defaultFillStyle);/*** @brief paint a polygon on the canvas. Uses current brush preset* @param list of Qpoints+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * </ul>+ * Default is ForegroundColor.+ * @param fillStyle appearance of the fill, one of:+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * <li>Pattern</li>+ * </ul>+ * Default is None.*/- void paintPolygon(const QList<QPointF> points);-+ void paintPolygon(const QList<QPointF> points,+ const QString strokeStyle = PaintingResources::defaultStrokeStyle,+ const QString fillStyle = PaintingResources::defaultFillStyle);/*** @brief paint an ellipse on the canvas. Uses current brush preset* @param rect QRect with x, y, width, and height+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * </ul>+ * Default is ForegroundColor.+ * @param fillStyle appearance of the fill, one of:+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * <li>Pattern</li>+ * </ul>+ * Default is None.*/- void paintEllipse(const QRectF &rect);-+ void paintEllipse(const QRectF &rect,+ const QString strokeStyle = PaintingResources::defaultStrokeStyle,+ const QString fillStyle = PaintingResources::defaultFillStyle);/*** @brief paint a custom path on the canvas. Uses current brush preset* @param path QPainterPath to determine path+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * </ul>+ * Default is ForegroundColor.+ * @param fillStyle appearance of the fill, one of:+ * <ul>+ * <li>None+ * <li>ForegroundColor</li>+ * <li>BackgroundColor</li>+ * <li>Pattern</li>+ * </ul>+ * Default is None.*/- void paintPath(const QPainterPath &path);-+ void paintPath(const QPainterPath &path,+ const QString strokeStyle = PaintingResources::defaultStrokeStyle,+ const QString fillStyle = PaintingResources::defaultFillStyle);/*** @brief paintAbility can be used to determine whether this node can be painted on with the current brush preset.* @return QString, one of the following:@@ -610,6 +680,7 @@ public Q_SLOTS:* <li>PAINT - This node is paintable by the current brush preset.</li>* <li>UNPAINTABLE - This node is not paintable, or a null preset is somehow selected./li>* <li>MYPAINTBRUSH_UNPAINTABLE - This node's non-RGBA colorspace cannot be painted on by the currently selected MyPaint brush.</li>+ * </ul>*/QString paintAbility();diff --git a/libs/libkis/PaintingResources.cpp b/libs/libkis/PaintingResources.cppindex 06dc5c09b2..2865bedaf3 100644--- a/libs/libkis/PaintingResources.cpp+++ b/libs/libkis/PaintingResources.cpp@@ -11,9 +11,25 @@#include "KisViewManager.h"#include "KisMainWindow.h"#include "kis_image.h"+#include "KisToolShapeUtils.h"-KisFigurePaintingToolHelper PaintingResources::createHelper(KisImageWSP image)+const QStringList StrokeStyle = {+ "None", // 0 = KisToolShapeUtils::StrokeStyle::StrokeStyleNone+ "ForegroundColor", // KisToolShapeUtils::StrokeStyle::StrokeStyleForeground+ "BackgroundColor" // KisToolShapeUtils::StrokeStyle::StrokeStyleBackground+};++const QStringList FillStyle = {+ "None", // 0 = KisToolShapeUtils::FillStyle::FillStyleNone+ "ForegroundColor", // KisToolShapeUtils::FillStyle::FillStyleForegroundColor+ "BackgroundColor", // KisToolShapeUtils::FillStyle::FillStyleBackgroundColor+ "Pattern" // KisToolShapeUtils::FillStyle::FillStylePattern+};++KisFigurePaintingToolHelper PaintingResources::createHelper(KisImageWSP image,+ const QString strokeStyleString,+ const QString fillStyleString){// need to grab the resource providerKisView *activeView = KisPart::instance()->currentMainwindow()->activeView();@@ -22,13 +38,35 @@ KisFigurePaintingToolHelper PaintingResources::createHelper(KisImageWSP image)// grab the image and current layerKisNodeSP node = activeView->currentNode();+ int strokeIndex = StrokeStyle.indexOf(strokeStyleString);+ if (strokeIndex == -1) {+ dbgScript << "Script tried to paint with invalid strokeStyle" << strokeStyleString << ", ignoring and using" << defaultStrokeStyle << ".";+ strokeIndex = StrokeStyle.indexOf(defaultStrokeStyle);+ if (strokeIndex == -1) {+ warnScript << "PaintingResources::createHelper(): defaultStrokeStyle" << defaultStrokeStyle << "is invalid!";+ strokeIndex = 1;+ }+ }+ KisToolShapeUtils::StrokeStyle strokeStyle = (KisToolShapeUtils::StrokeStyle) strokeIndex;++ int fillIndex = FillStyle.indexOf(fillStyleString);+ if (fillIndex == -1) {+ dbgScript << "Script tried to paint with invalid fillStyle" << fillStyleString << ", ignoring and using" << defaultFillStyle << ".";+ fillIndex = FillStyle.indexOf(defaultFillStyle);+ if (fillIndex == -1) {+ warnScript << "PaintingResources::createHelper(): defaultFillStyle" << defaultFillStyle << " is invalid!";+ fillIndex = 0;+ }+ }+ KisToolShapeUtils::FillStyle fillStyle = (KisToolShapeUtils::FillStyle) fillIndex;+const KUndo2MagicString name = kundo2_i18n("Scripted Brush Stroke");KisFigurePaintingToolHelper helper(name,image,node, resourceManager,- KisToolShapeUtils::StrokeStyle::StrokeStyleForeground,- KisToolShapeUtils::FillStyle::FillStyleNone+ strokeStyle,+ fillStyle);return helper;diff --git a/libs/libkis/PaintingResources.h b/libs/libkis/PaintingResources.hindex 781691837e..470a3779d8 100644--- a/libs/libkis/PaintingResources.h+++ b/libs/libkis/PaintingResources.h@@ -11,12 +11,19 @@/*** @brief The PaintingResources namespace* Sets up information related to making painting strokes.- * Used primarily in the Document class+ * Used primarily in the Node class**/namespace PaintingResources{- KisFigurePaintingToolHelper createHelper(KisImageWSP image);+ // These are set in Node.sip+ const QString defaultStrokeStyle = "ForegroundColor";+ const QString defaultFillStyle = "None";++ KisFigurePaintingToolHelper createHelper(KisImageWSP image,+ const QString strokeStyle = defaultStrokeStyle,+ const QString fillStyle = defaultFillStyle);+};#endif // LIBKIS_PAINTINGRESOURCES_Hdiff --git a/plugins/extensions/pykrita/sip/krita/Node.sip b/plugins/extensions/pykrita/sip/krita/Node.sipindex ce483e4569..a01a6bce6f 100644--- a/plugins/extensions/pykrita/sip/krita/Node.sip+++ b/plugins/extensions/pykrita/sip/krita/Node.sip@@ -82,11 +82,11 @@ public Q_SLOTS:int index() const;QUuid uniqueId() const;- void paintLine(const QPoint pointOne, const QPoint pointTwo);- void paintRectangle(const QRectF &rect);- void paintPolygon(const QList<QPointF> points);- void paintEllipse(const QRectF &rect);- void paintPath(const QPainterPath &path);+ void paintLine(const QPoint pointOne, const QPoint pointTwo, const QString strokeStyle = "ForegroundColor");+ void paintRectangle(const QRectF &rect, const QString strokeStyle = "ForegroundColor", const QString fillStyle = "None");+ void paintPolygon(const QList<QPointF> points, const QString strokeStyle = "ForegroundColor", const QString fillStyle = "None");+ void paintEllipse(const QRectF &rect, const QString strokeStyle = "ForegroundColor", const QString fillStyle = "None");+ void paintPath(const QPainterPath &path, const QString strokeStyle = "ForegroundColor", const QString fillStyle = "None");QString paintAbility();Q_SIGNALS:private: