Krita Source Code Documentation
Loading...
Searching...
No Matches
SplineAssistant Class Reference

#include <SplineAssistant.h>

+ Inheritance diagram for SplineAssistant:

Classes

struct  Private
 

Public Member Functions

void adjustLine (QPointF &point, QPointF &strokeBegin) override
 
QPointF adjustPosition (const QPointF &point, const QPointF &strokeBegin, const bool snapToAny, qreal moveThresholdPt) override
 
KisPaintingAssistantSP clone (QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > &handleMap) const override
 
QPointF getDefaultEditorPosition () const override
 
bool isAssistantComplete () const override
 
int numHandles () const override
 
 SplineAssistant ()
 
- Public Member Functions inherited from KisPaintingAssistant
void addHandle (KisPaintingAssistantHandleSP handle, HandleType type)
 
bool areTwoPointsClose (const QPointF &pointOne, const QPointF &pointTwo)
 
QColor assistantCustomColor ()
 
KisPaintingAssistantHandleSP bottomLeft ()
 
const KisPaintingAssistantHandleSP bottomLeft () const
 
KisPaintingAssistantHandleSP bottomMiddle ()
 
const KisPaintingAssistantHandleSP bottomMiddle () const
 
KisPaintingAssistantHandleSP bottomRight ()
 
const KisPaintingAssistantHandleSP bottomRight () const
 
virtual bool canBeLocal () const
 canBeLocal
 
KisPaintingAssistantHandleSP closestCornerHandleFromPoint (QPointF point)
 
void copySharedData (KisPaintingAssistantSP assistant)
 
void drawError (QPainter &painter, const QPainterPath &path)
 
void drawPath (QPainter &painter, const QPainterPath &path, bool drawActive=true)
 
void drawPreview (QPainter &painter, const QPainterPath &path)
 
void drawX (QPainter &painter, const QPointF &pt)
 
QPointF editorWidgetOffset ()
 
QColor effectiveAssistantColor () const
 
virtual void endStroke ()
 
void findPerspectiveAssistantHandleLocation ()
 
virtual QPointF getEditorPosition () const
 
QList< KisPaintingAssistantHandleSPhandles ()
 
const QList< KisPaintingAssistantHandleSP > & handles () const
 
const QString & id () const
 
bool isDuplicating ()
 isDuplicating
 
bool isLocal () const
 isLocal
 
bool isLocked ()
 isLocked
 
bool isSnappingActive () const
 
 KisPaintingAssistant (const QString &id, const QString &name)
 
KisPaintingAssistantHandleSP leftMiddle ()
 
const KisPaintingAssistantHandleSP leftMiddle () const
 
virtual bool loadCustomXml (QXmlStreamReader *xml)
 
void loadXml (KoStore *store, QMap< int, KisPaintingAssistantHandleSP > &handleMap, QString path)
 
const QString & name () const
 
KisPaintingAssistantHandleSP oppHandleOne ()
 
void replaceHandle (KisPaintingAssistantHandleSP _handle, KisPaintingAssistantHandleSP _with)
 
KisPaintingAssistantHandleSP rightMiddle ()
 
const KisPaintingAssistantHandleSP rightMiddle () const
 
virtual void saveCustomXml (QXmlStreamWriter *xml)
 
QByteArray saveXml (QMap< KisPaintingAssistantHandleSP, int > &handleMap)
 
void saveXmlList (QDomDocument &doc, QDomElement &assistantsElement, int count)
 
virtual void setAdjustedBrushPosition (const QPointF position)
 
void setAssistantCustomColor (QColor color)
 
void setAssistantGlobalColorCache (const QColor &color)
 
void setDecorationThickness (int thickness)
 
void setDuplicating (bool value)
 setDuplicating
 
void setEditorWidgetOffset (QPointF offset)
 
virtual void setFollowBrushPosition (bool follow)
 
void setLocal (bool value)
 setLocal
 
void setLocked (bool value)
 setLocked
 
void setSnappingActive (bool set)
 
void setUseCustomColor (bool useCustomColor)
 
QList< KisPaintingAssistantHandleSPsideHandles ()
 
const QList< KisPaintingAssistantHandleSP > & sideHandles () const
 
KisPaintingAssistantHandleSP topLeft ()
 
const KisPaintingAssistantHandleSP topLeft () const
 
KisPaintingAssistantHandleSP topMiddle ()
 
const KisPaintingAssistantHandleSP topMiddle () const
 
KisPaintingAssistantHandleSP topRight ()
 
const KisPaintingAssistantHandleSP topRight () const
 
virtual void transform (const QTransform &transform)
 
void uncache ()
 
bool useCustomColor ()
 
QPointF viewportConstrainedEditorPosition (const KisCoordinatesConverter *converter, const QSize editorSize)
 
virtual ~KisPaintingAssistant ()
 

Protected Member Functions

void drawAssistant (QPainter &gc, const QRectF &updateRect, const KisCoordinatesConverter *converter, bool cached, KisCanvas2 *canvas, bool assistantVisible=true, bool previewVisible=true) override
 
void drawCache (QPainter &gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override
 performance layer where the graphics can be drawn from a cache instead of generated every render update
 
- Protected Member Functions inherited from KisPaintingAssistant
virtual QRect boundingRect () const
 
QPointF effectiveBrushPosition (const KisCoordinatesConverter *converter, KisCanvas2 *canvas) const
 Query the effective brush position to be used for preview lines. This is intended to be used for painting the dynamic preview lines for assistants that feature them. Affected by setAdjustedBrushPosition() and setFollowBrushPosition().
 
virtual KisPaintingAssistantHandleSP firstLocalHandle () const
 firstLocalHandle Note: this doesn't guarantee it will be the topleft corner! For that, use getLocalRect().topLeft() The only purpose of those functions to exist is to be able to put getLocalRect() function in the KisPaintingAssistant instead of reimplementing it in every specific assistant.
 
QRectF getLocalRect () const
 getLocalRect The function deals with local handles not being topLeft and bottomRight gracefully and returns a correct rectangle. Thanks to that the user can place handles in a "wrong" order or move them around but the local rectangle will still be correct.
 
void initHandles (QList< KisPaintingAssistantHandleSP > _handles)
 
 KisPaintingAssistant (const KisPaintingAssistant &rhs, QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > &handleMap)
 
QPointF pixelToView (const QPoint pixelCoords) const
 
virtual KisPaintingAssistantHandleSP secondLocalHandle () const
 secondLocalHandle Note: this doesn't guarantee it will be the bottomRight corner! For that, use getLocalRect().bottomRight() (and remember that for QRect bottomRight() works differently than for QRectF, so don't convert to QRect before accessing the corner)
 

Private Member Functions

QPointF project (const QPointF &pt, const QPointF &strokeBegin) const
 
 SplineAssistant (const SplineAssistant &rhs, QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > &handleMap)
 

Private Attributes

KisCanvas2m_canvas {nullptr}
 used for getting the decoration so the bezier handles aren't drawn while editing
 
const QScopedPointer< Privatem_d
 

Additional Inherited Members

- Static Public Member Functions inherited from KisPaintingAssistant
static QList< KisPaintingAssistantSPcloneAssistantList (const QList< KisPaintingAssistantSP > &list)
 
static double norm2 (const QPointF &p)
 
- Protected Attributes inherited from KisPaintingAssistant
QList< KisPaintingAssistantHandleSPm_handles
 
bool m_hasBeenInsideLocalRect {false}
 

Detailed Description

Definition at line 15 of file SplineAssistant.h.

Constructor & Destructor Documentation

◆ SplineAssistant() [1/2]

SplineAssistant::SplineAssistant ( )

Definition at line 74 of file SplineAssistant.cc.

75 : KisPaintingAssistant("spline", i18n("Spline assistant"))
76 , m_d(new Private)
77{
78}
KisPaintingAssistant(const QString &id, const QString &name)
const QScopedPointer< Private > m_d

◆ SplineAssistant() [2/2]

SplineAssistant::SplineAssistant ( const SplineAssistant & rhs,
QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > & handleMap )
explicitprivate

Definition at line 80 of file SplineAssistant.cc.

81 : KisPaintingAssistant(rhs, handleMap)
82 , m_canvas(rhs.m_canvas)
83 , m_d(new Private)
84{
85}
KisCanvas2 * m_canvas
used for getting the decoration so the bezier handles aren't drawn while editing

Member Function Documentation

◆ adjustLine()

void SplineAssistant::adjustLine ( QPointF & point,
QPointF & strokeBegin )
overridevirtual

Implements KisPaintingAssistant.

Definition at line 215 of file SplineAssistant.cc.

216{
217 point = QPointF();
218 strokeBegin = QPointF();
219}

◆ adjustPosition()

QPointF SplineAssistant::adjustPosition ( const QPointF & point,
const QPointF & strokeBegin,
const bool snapToAny,
qreal moveThresholdPt )
overridevirtual

Adjust the position given in parameter.

Parameters
pointthe coordinates in point in the document reference
strokeBeginthe coordinates of the beginning of the stroke
snapToAnybecause now assistants can be composited out of multiple inside assistants. snapToAny true means that you can use any of the inside assistant, while it being false means you should use the last used one. The logic determining when it happens (first stroke etc.) is in the decoration, so those two options are enough.
moveThresholdPtthe threshold for the "move" of the cursor measured in pt (usually equals to 2px in screen coordinates converted to pt)

Implements KisPaintingAssistant.

Definition at line 210 of file SplineAssistant.cc.

211{
212 return project(pt, strokeBegin);
213}
QPointF project(const QPointF &pt, const QPointF &strokeBegin) const

References project().

◆ clone()

KisPaintingAssistantSP SplineAssistant::clone ( QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > & handleMap) const
overridevirtual

Implements KisPaintingAssistant.

Definition at line 87 of file SplineAssistant.cc.

88{
89 return KisPaintingAssistantSP(new SplineAssistant(*this, handleMap));
90}
QSharedPointer< KisPaintingAssistant > KisPaintingAssistantSP
Definition kis_types.h:189

References SplineAssistant().

◆ drawAssistant()

void SplineAssistant::drawAssistant ( QPainter & gc,
const QRectF & updateRect,
const KisCoordinatesConverter * converter,
bool cached,
KisCanvas2 * canvas,
bool assistantVisible = true,
bool previewVisible = true )
overrideprotectedvirtual

Reimplemented from KisPaintingAssistant.

Definition at line 221 of file SplineAssistant.cc.

222{
223 gc.save();
224 gc.resetTransform();
225 QPoint mousePos;
226
227 if (canvas){
228 //simplest, cheapest way to get the mouse-position//
229 mousePos= canvas->canvasWidget()->mapFromGlobal(QCursor::pos());
230 m_canvas = canvas;
231 }
232 else {
233 //...of course, you need to have access to a canvas-widget for that.//
234 mousePos = QCursor::pos();//this'll give an offset//
235 dbgFile<<"canvas does not exist in spline, you may have passed arguments incorrectly:"<<canvas;
236 }
237
238
239 if (handles().size() > 1) {
240
241 QTransform initialTransform = converter->documentToWidgetTransform();
242
243 // first we find the path that our point create.
244 QPointF pts[4];
245 pts[0] = *handles()[0];
246 pts[1] = *handles()[1];
247 pts[2] = (handles().size() >= 3) ? (*handles()[2]) : (*handles()[0]);
248 pts[3] = (handles().size() >= 4) ? (*handles()[3]) : (handles().size() >= 3) ? (*handles()[2]) : (*handles()[1]);
249 gc.setTransform(initialTransform);
250
251 // Draw the spline
252 QPainterPath path;
253 path.moveTo(pts[0]);
254 path.cubicTo(pts[2], pts[3], pts[1]);
255
256 //then we use this path to check the bounding rectangle//
257 if (isSnappingActive() && path.boundingRect().contains(initialTransform.inverted().map(mousePos)) && previewVisible==true){
258 drawPreview(gc, path);//and we draw the preview.
259 }
260 }
261 gc.restore();
262
263 // there is some odd rectangle that is getting rendered when there is only one point, so don't start rendering the line until after 2
264 // this issue only exists with this spline assistant...none of the others
265 if (handles().size() > 2) {
266 KisPaintingAssistant::drawAssistant(gc, updateRect, converter, cached, canvas, assistantVisible, previewVisible);
267 }
268}
KisAbstractCanvasWidget * canvasWidget
void drawPreview(QPainter &painter, const QPainterPath &path)
virtual void drawAssistant(QPainter &gc, const QRectF &updateRect, const KisCoordinatesConverter *converter, bool cached, KisCanvas2 *canvas=0, bool assistantVisible=true, bool previewVisible=true)
const QList< KisPaintingAssistantHandleSP > & handles() const
#define dbgFile
Definition kis_debug.h:53
int size(const Forest< T > &forest)
Definition KisForest.h:1232

References KisCanvas2::canvasWidget, dbgFile, KisCoordinatesConverter::documentToWidgetTransform(), KisPaintingAssistant::drawAssistant(), KisPaintingAssistant::drawPreview(), KisPaintingAssistant::handles(), KisPaintingAssistant::isSnappingActive(), and m_canvas.

◆ drawCache()

void SplineAssistant::drawCache ( QPainter & gc,
const KisCoordinatesConverter * converter,
bool assistantVisible = true )
overrideprotectedvirtual

performance layer where the graphics can be drawn from a cache instead of generated every render update

Implements KisPaintingAssistant.

Definition at line 270 of file SplineAssistant.cc.

271{
272 if (assistantVisible == false || handles().size() < 2 ){
273 return;
274 }
275
276 QTransform initialTransform = converter->documentToWidgetTransform();
277
278 QPointF pts[4];
279 pts[0] = *handles()[0];
280 pts[1] = *handles()[1];
281 pts[2] = (handles().size() >= 3) ? (*handles()[2]) : (*handles()[0]);
282 pts[3] = (handles().size() >= 4) ? (*handles()[3]) : (handles().size() >= 3) ? (*handles()[2]) : (*handles()[1]);
283
284 gc.setTransform(initialTransform);
285
286
287 { // Draw bezier handles control lines only if we are editing the assistant
288 gc.save();
289 QColor assistantColor = effectiveAssistantColor();
290 QPen bezierlinePen(assistantColor);
291 bezierlinePen.setStyle(Qt::DotLine);
292 bezierlinePen.setWidth(2);
293
295
296 if (!isSnappingActive()) {
297 QColor snappingColor = assistantColor;
298 snappingColor.setAlpha(snappingColor.alpha() * 0.2);
299
300 bezierlinePen.setColor(snappingColor);
301 }
302 bezierlinePen.setCosmetic(true);
303
304 gc.setPen(bezierlinePen);
305 gc.drawLine(pts[0], pts[2]);
306
307 if (isAssistantComplete()) {
308 gc.drawLine(pts[1], pts[3]);
309 }
310 gc.setPen(QColor(0, 0, 0, 125));
311 }
312 gc.restore();
313 }
314
315
316 // Draw the spline
317 QPainterPath path;
318 path.moveTo(pts[0]);
319 path.cubicTo(pts[2], pts[3], pts[1]);
320 drawPath(gc, path, isSnappingActive());
321
322
323}
KisPaintingAssistantsDecorationSP paintingAssistantsDecoration() const
void drawPath(QPainter &painter, const QPainterPath &path, bool drawActive=true)
bool isAssistantComplete() const override

References KisCoordinatesConverter::documentToWidgetTransform(), KisPaintingAssistant::drawPath(), KisPaintingAssistant::effectiveAssistantColor(), KisPaintingAssistant::handles(), isAssistantComplete(), KisPaintingAssistantsDecoration::isEditingAssistants(), KisPaintingAssistant::isSnappingActive(), m_canvas, and KisCanvas2::paintingAssistantsDecoration().

◆ getDefaultEditorPosition()

QPointF SplineAssistant::getDefaultEditorPosition ( ) const
overridevirtual

Implements KisPaintingAssistant.

Definition at line 325 of file SplineAssistant.cc.

326{
327 return B(0.5, *handles()[0], *handles()[2], *handles()[3], *handles()[1]);
328}

References B, and KisPaintingAssistant::handles().

◆ isAssistantComplete()

bool SplineAssistant::isAssistantComplete ( ) const
overridevirtual

determines if the assistant has enough handles to be considered created new assistants get in a "creation" phase where they are currently being made on the canvas it will return false if we are in the middle of creating the assistant.

Reimplemented from KisPaintingAssistant.

Definition at line 330 of file SplineAssistant.cc.

331{
332 return handles().size() >= 4; // specify 4 corners to make assistant complete
333}

References KisPaintingAssistant::handles().

◆ numHandles()

int SplineAssistant::numHandles ( ) const
inlineoverridevirtual

Implements KisPaintingAssistant.

Definition at line 23 of file SplineAssistant.h.

23{ return 4; }

◆ project()

QPointF SplineAssistant::project ( const QPointF & pt,
const QPointF & strokeBegin ) const
private

Definition at line 176 of file SplineAssistant.cc.

177{
178 Q_ASSERT(isAssistantComplete());
179
180 // minimize d(t), but keep t in the same neighbourhood as before (unless starting a new stroke)
181 bool stayClose = (m_d->prevStrokebegin == strokeBegin)? true : false;
182 qreal min_t;
183
184 QList<QPointF> refs;
185 QVector<int> hindex = {0,2,3,1}; // order handles as expected by KisBezierUtils
186 Q_FOREACH(int i, hindex) {
187 refs.append(*handles()[i]);
188 }
189
190 if (stayClose){
191 // Search in the vicinity of previous t value.
192 // This ensure unimodality for proper goldenSearch algorithm
193 qreal delta = 1/10.0;
194 qreal lbound = qBound(0.0,1.0, m_d->prev_t - delta);
195 qreal ubound = qBound(0.0,1.0, m_d->prev_t + delta);
196 min_t = goldenSearch(pt,handles(), lbound , ubound, 1e-6,1e+2);
197
198 } else {
199 min_t = KisBezierUtils::nearestPoint(refs,pt);
200 }
201
202 QPointF draw_pos = B(min_t, *handles()[0], *handles()[2], *handles()[3], *handles()[1]);
203
204 m_d->prev_t = min_t;
205 m_d->prevStrokebegin = strokeBegin;
206
207 return draw_pos;
208}
qreal goldenSearch(const QPointF &pt, const QList< KisPaintingAssistantHandleSP > handles, qreal low, qreal high, qreal tolerance, uint max_iter)
qreal nearestPoint(const QList< QPointF > controlPoints, const QPointF &point, qreal *resultDistance, QPointF *resultPoint)

References B, goldenSearch(), KisPaintingAssistant::handles(), isAssistantComplete(), m_d, and KisBezierUtils::nearestPoint().

Member Data Documentation

◆ m_canvas

KisCanvas2* SplineAssistant::m_canvas {nullptr}
private

used for getting the decoration so the bezier handles aren't drawn while editing

Definition at line 34 of file SplineAssistant.h.

34{nullptr};

◆ m_d

const QScopedPointer<Private> SplineAssistant::m_d
private

Definition at line 38 of file SplineAssistant.h.


The documentation for this class was generated from the following files: