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, const KoColorDisplayRendererInterface *displayRenderInterface)
 
void drawPath (QPainter &painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface, bool drawActive=true)
 
void drawPreview (QPainter &painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface)
 
void drawX (QPainter &painter, const QPointF &pt, const KoColorDisplayRendererInterface *displayRenderInterface)
 
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, const KoColorDisplayRendererInterface *displayRenderInterface, bool cached, KisCanvas2 *canvas, bool assistantVisible=true, bool previewVisible=true) override
 
void drawCache (QPainter &gc, const KisCoordinatesConverter *converter, const KoColorDisplayRendererInterface *displayRenderInterface, 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 75 of file SplineAssistant.cc.

76 : KisPaintingAssistant("spline", i18n("Spline assistant"))
77 , m_d(new Private)
78{
79}
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 81 of file SplineAssistant.cc.

82 : KisPaintingAssistant(rhs, handleMap)
83 , m_canvas(rhs.m_canvas)
84 , m_d(new Private)
85{
86}
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 216 of file SplineAssistant.cc.

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

◆ 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 211 of file SplineAssistant.cc.

212{
213 return project(pt, strokeBegin);
214}
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 88 of file SplineAssistant.cc.

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

References SplineAssistant().

◆ drawAssistant()

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

Reimplemented from KisPaintingAssistant.

Definition at line 222 of file SplineAssistant.cc.

223{
224 gc.save();
225 gc.resetTransform();
226 QPoint mousePos;
227
228 if (canvas){
229 //simplest, cheapest way to get the mouse-position//
230 mousePos= canvas->canvasWidget()->mapFromGlobal(QCursor::pos());
231 m_canvas = canvas;
232 }
233 else {
234 //...of course, you need to have access to a canvas-widget for that.//
235 mousePos = QCursor::pos();//this'll give an offset//
236 dbgFile<<"canvas does not exist in spline, you may have passed arguments incorrectly:"<<canvas;
237 }
238
239
240 if (handles().size() > 1) {
241
242 QTransform initialTransform = converter->documentToWidgetTransform();
243
244 // first we find the path that our point create.
245 QPointF pts[4];
246 pts[0] = *handles()[0];
247 pts[1] = *handles()[1];
248 pts[2] = (handles().size() >= 3) ? (*handles()[2]) : (*handles()[0]);
249 pts[3] = (handles().size() >= 4) ? (*handles()[3]) : (handles().size() >= 3) ? (*handles()[2]) : (*handles()[1]);
250 gc.setTransform(initialTransform);
251
252 // Draw the spline
253 QPainterPath path;
254 path.moveTo(pts[0]);
255 path.cubicTo(pts[2], pts[3], pts[1]);
256
257 //then we use this path to check the bounding rectangle//
258 if (isSnappingActive() && path.boundingRect().contains(initialTransform.inverted().map(mousePos)) && previewVisible==true){
259 drawPreview(gc, path, displayRenderInterface);//and we draw the preview.
260 }
261 }
262 gc.restore();
263
264 // 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
265 // this issue only exists with this spline assistant...none of the others
266 if (handles().size() > 2) {
267 KisPaintingAssistant::drawAssistant(gc, updateRect, converter, displayRenderInterface, cached, canvas, assistantVisible, previewVisible);
268 }
269}
KisAbstractCanvasWidget * canvasWidget
void drawPreview(QPainter &painter, const QPainterPath &path, const KoColorDisplayRendererInterface *displayRenderInterface)
virtual void drawAssistant(QPainter &gc, const QRectF &updateRect, const KisCoordinatesConverter *converter, const KoColorDisplayRendererInterface *displayRenderInterface, 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,
const KoColorDisplayRendererInterface * displayRenderInterface,
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 271 of file SplineAssistant.cc.

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

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

◆ getDefaultEditorPosition()

QPointF SplineAssistant::getDefaultEditorPosition ( ) const
overridevirtual

Implements KisPaintingAssistant.

Definition at line 326 of file SplineAssistant.cc.

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

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 331 of file SplineAssistant.cc.

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

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 177 of file SplineAssistant.cc.

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