Skip to content

armgit / krita

Public Imported from GitHub
0

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++

ENHANCEMENT: Expose the live brush position to Python scripts

Moritz Staudingerc42f07284f23
diff --git a/libs/libkis/View.cpp b/libs/libkis/View.cpp
index 899bb2f430..a514242ef0 100644
--- a/libs/libkis/View.cpp
+++ b/libs/libkis/View.cpp
@@ -314,6 +314,25 @@ void View::setBrushRotation(qreal brushRotation)
d->view->resourceProvider()->setBrushRotation(brushRotation);
}
+QPointF View::brushPosition() const
+{
+ if (!d->view) return QPointF();
+ KisCanvasResourceProvider *provider = d->view->resourceProvider();
+ if (!provider || !provider->resourceManager()) return QPointF();
+ const QPointF pixelPos = provider->resourceManager()
+ ->resource(KoCanvasResource::CurrentBrushPosition)
+ .value<QPointF>();
+ if (pixelPos.isNull()) return QPointF();
+ KisCanvas2 *canvas = d->view->canvasBase();
+ if (!canvas) return QPointF();
+ const KisCoordinatesConverter *converter = canvas->coordinatesConverter();
+ if (!converter) return QPointF();
+ // The stored position is in image pixel coordinates. Convert all the way
+ // to canvas-widget coordinates (image -> document -> flake -> widget),
+ // which is what scripts map to the screen with canvasWidget()->mapToGlobal.
+ return converter->imageToWidget(pixelPos);
+}
+
qreal View::paintingFlow() const
{
if (!d->view) return 0.0;
diff --git a/libs/libkis/View.h b/libs/libkis/View.h
index 932c76a62c..a847a05836 100644
--- a/libs/libkis/View.h
+++ b/libs/libkis/View.h
@@ -238,6 +238,15 @@ Application.activeWindow().activeView().setForeGroundColor(color)
*/
void setBrushRotation(qreal brushRotation);
+ /**
+ * @brief return the current brush position on the canvas, in canvas
+ * widget coordinates. This is where the brush actually is during a
+ * stroke, which lags the pointer when brush smoothing is active.
+ *
+ * @return the brush position, or a null QPointF when no stroke is active
+ */
+ QPointF brushPosition() const;
+
/**
* @brief return the current flow for brush
*
diff --git a/libs/resources/KoCanvasResourcesIds.h b/libs/resources/KoCanvasResourcesIds.h
index e2259dab94..3c85ae6ab0 100644
--- a/libs/resources/KoCanvasResourcesIds.h
+++ b/libs/resources/KoCanvasResourcesIds.h
@@ -60,6 +60,9 @@ enum CanvasResourceId {
EffectiveZoom, ///<-Used only by painting tools for non-displaying purposes
EffectivePhysicalZoom, ///<-Used by tool for displaying purposes
BrushRotation,
+ ///< QPointF, the current brush centre in image pixel coordinates,
+ ///< updated while a stroke is being painted.
+ CurrentBrushPosition,
HandleRadius, ///< The handle radius used for drawing handles of any kind
DecorationThickness, ///< Integer, the thickness of single px decorations, will be adjusted by HiDPI settings. Because most of our decorations are drawn without anti-aliasing, this is an integer, so that line thickness won't vary as the painter tries round the floating point.
UsingOtherColor, ///<- a boolean that is controlled by the tool invocation action
diff --git a/libs/ui/kis_canvas_resource_provider.h b/libs/ui/kis_canvas_resource_provider.h
index ee01609e3b..db11c2e62c 100644
--- a/libs/ui/kis_canvas_resource_provider.h
+++ b/libs/ui/kis_canvas_resource_provider.h
@@ -8,6 +8,7 @@
#define KIS_CANVAS_RESOURCE_PROVIDER_H_
#include <QObject>
+#include <QPointF>
#include <KoColor.h>
#include <KoID.h>
diff --git a/libs/ui/tool/kis_tool_freehand_helper.cpp b/libs/ui/tool/kis_tool_freehand_helper.cpp
index 6ae86d7ab0..abdef7a403 100644
--- a/libs/ui/tool/kis_tool_freehand_helper.cpp
+++ b/libs/ui/tool/kis_tool_freehand_helper.cpp
@@ -372,6 +372,16 @@ KoCanvasResourceProvider *KisToolFreehandHelper::resourceManager() const
return m_d->resourceManager;
}
+void KisToolFreehandHelper::updateCurrentBrushPosition(const QPointF &pixelPos)
+{
+ // Store on the flake-level resource provider (the only provider this
+ // helper holds), keyed by a Krita-specific resource id.
+ if (KoCanvasResourceProvider *manager = resourceManager()) {
+ manager->setResource(KoCanvasResource::CurrentBrushPosition,
+ QVariant::fromValue(pixelPos));
+ }
+}
+
void KisToolFreehandHelper::paintBezierSegment(KisPaintInformation pi1, KisPaintInformation pi2,
QPointF tangent1, QPointF tangent2)
{
@@ -1007,6 +1017,7 @@ qreal KisToolFreehandHelper::currentPhysicalZoom() const
void KisToolFreehandHelper::paintAt(int strokeInfoId,
const KisPaintInformation &pi)
{
+ updateCurrentBrushPosition(pi.pos());
m_d->hasPaintAtLeastOnce = true;
m_d->strokesFacade->addJob(m_d->strokeId,
new FreehandStrokeStrategy::Data(strokeInfoId, pi));
@@ -1017,6 +1028,7 @@ void KisToolFreehandHelper::paintLine(int strokeInfoId,
const KisPaintInformation &pi1,
const KisPaintInformation &pi2)
{
+ updateCurrentBrushPosition(pi2.pos());
m_d->hasPaintAtLeastOnce = true;
m_d->strokesFacade->addJob(m_d->strokeId,
new FreehandStrokeStrategy::Data(strokeInfoId, pi1, pi2));
@@ -1029,6 +1041,7 @@ void KisToolFreehandHelper::paintBezierCurve(int strokeInfoId,
const QPointF &control2,
const KisPaintInformation &pi2)
{
+ updateCurrentBrushPosition(pi2.pos());
#ifdef DEBUG_BEZIER_CURVES
KisPaintInformation tpi1;
diff --git a/libs/ui/tool/kis_tool_freehand_helper.h b/libs/ui/tool/kis_tool_freehand_helper.h
index c1d78e9291..24f707c586 100644
--- a/libs/ui/tool/kis_tool_freehand_helper.h
+++ b/libs/ui/tool/kis_tool_freehand_helper.h
@@ -130,8 +130,15 @@ protected:
private:
void paint(KisPaintInformation &info );
void paintBezierSegment(KisPaintInformation pi1, KisPaintInformation pi2,
- QPointF tangent1, QPointF tangent2
- );
+ QPointF tangent1, QPointF tangent2
+ );
+
+ /**
+ * Publishes the current brush position (in image pixel coordinates) to
+ * the canvas resource provider, so scripts can follow where the brush
+ * actually is during a stroke (including stabilization delay).
+ */
+ void updateCurrentBrushPosition(const QPointF &pixelPos);
void stabilizerStart(KisPaintInformation firstPaintInfo);
void stabilizerEnd();
diff --git a/plugins/extensions/pykrita/sip/krita/View.sip b/plugins/extensions/pykrita/sip/krita/View.sip
index 399e9e049b..af0c7c751a 100644
--- a/plugins/extensions/pykrita/sip/krita/View.sip
+++ b/plugins/extensions/pykrita/sip/krita/View.sip
@@ -65,6 +65,8 @@ public Q_SLOTS:
qreal brushRotation() const;
void setBrushRotation(qreal brushRotation);
+ QPointF brushPosition() const;
+
qreal paintingFlow() const;
void setPaintingFlow(qreal flow);