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

The KisHandlePainterHelper class is a special helper for painting handles around objects. It ensures the handles are painted with the same size and line width whatever transformation is setup in the painter. The handles will also be rotated/skewed if the object itself has these transformations. More...

#include <KisHandlePainterHelper.h>

Public Member Functions

void drawConnectionLine (const QLineF &line)
 
void drawConnectionLine (const QPointF &p1, const QPointF &p2)
 
void drawGradientArrow (const QPointF &start, const QPointF &end, qreal radius)
 
void drawGradientCrossHandle (const QPointF &center, qreal radius)
 
void drawGradientHandle (const QPointF &center)
 
void drawGradientHandle (const QPointF &center, qreal radius)
 
void drawHandleCircle (const QPointF &center)
 
void drawHandleCircle (const QPointF &center, qreal radius)
 
void drawHandleLine (const QLineF &line, qreal width=1.0, QVector< qreal > dashPattern={}, qreal dashOffset=0.0)
 
void drawHandleRect (const QPointF &center)
 
void drawHandleRect (const QPointF &center, qreal radius)
 
void drawHandleRect (const QPointF &center, qreal radius, QPoint offset)
 
void drawHandleSmallCircle (const QPointF &center)
 
void drawPath (const QPainterPath &path)
 
void drawPixmap (const QPixmap &pixmap, QPointF position, int size, QRectF sourceRect)
 
void drawRubberLine (const QPolygonF &poly)
 
void fillHandleRect (const QPointF &center, qreal radius, QColor fillColor, QPoint offset)
 
 KisHandlePainterHelper (KisHandlePainterHelper &&rhs)
 
 KisHandlePainterHelper (KisHandlePainterHelper &rhs)=delete
 
 KisHandlePainterHelper (QPainter *_painter, const QTransform &originalPainterTransform, qreal handleRadius, int decorationThickness=1)
 
 KisHandlePainterHelper (QPainter *_painter, qreal handleRadius=0.0, int decorationThickness=1)
 
void setHandleStyle (const KisHandleStyle &style)
 
 ~KisHandlePainterHelper ()
 

Private Member Functions

void drawArrow (const QPointF &pos, const QPointF &from, qreal radius)
 
void init ()
 

Private Attributes

KisAlgebra2D::DecomposedMatrix m_decomposedMatrix
 
int m_decorationThickness
 
QPolygonF m_handlePolygon
 
qreal m_handleRadius
 
KisHandleStyle m_handleStyle
 
QTransform m_handleTransform
 
QTransform m_originalPainterTransform
 
QPainter * m_painter
 
QTransform m_painterTransform
 

Detailed Description

The KisHandlePainterHelper class is a special helper for painting handles around objects. It ensures the handles are painted with the same size and line width whatever transformation is setup in the painter. The handles will also be rotated/skewed if the object itself has these transformations.

On construction it resets QPainter transformation and on destruction recovers it back.

Please consider using KoShape::createHandlePainterHelper instead of direct construction of the helper. This factory method will also apply the transformations needed for a shape.

Definition at line 34 of file KisHandlePainterHelper.h.

Constructor & Destructor Documentation

◆ KisHandlePainterHelper() [1/4]

KisHandlePainterHelper::KisHandlePainterHelper ( QPainter * _painter,
qreal handleRadius = 0.0,
int decorationThickness = 1 )

Creates the helper, initializes all the internal transformations and resets the transformation of the painter.

Definition at line 16 of file KisHandlePainterHelper.cpp.

References init().

◆ KisHandlePainterHelper() [2/4]

KisHandlePainterHelper::KisHandlePainterHelper ( QPainter * _painter,
const QTransform & originalPainterTransform,
qreal handleRadius,
int decorationThickness = 1 )

Creates the helper, initializes all the internal transformations and resets the transformation of the painter. This override also adjusts the transformation of the painter into the coordinate system of the shape

Definition at line 27 of file KisHandlePainterHelper.cpp.

28 : m_painter(_painter),
29 m_originalPainterTransform(originalPainterTransform),
30 m_painterTransform(m_painter->transform()),
31 m_handleRadius(handleRadius),
32 m_decorationThickness(decorationThickness),
34{
35 init();
36}

References init().

◆ KisHandlePainterHelper() [3/4]

KisHandlePainterHelper::KisHandlePainterHelper ( KisHandlePainterHelper && rhs)

Move c-tor. Used to create and return the helper from functions by-value.

Definition at line 38 of file KisHandlePainterHelper.cpp.

◆ KisHandlePainterHelper() [4/4]

KisHandlePainterHelper::KisHandlePainterHelper ( KisHandlePainterHelper & rhs)
delete

◆ ~KisHandlePainterHelper()

KisHandlePainterHelper::~KisHandlePainterHelper ( )

Restores the transformation of the painter

Definition at line 66 of file KisHandlePainterHelper.cpp.

66 {
67 if (m_painter) {
69 }
70}

References m_originalPainterTransform, and m_painter.

Member Function Documentation

◆ drawArrow()

void KisHandlePainterHelper::drawArrow ( const QPointF & pos,
const QPointF & from,
qreal radius )
private

Draw a single arrow with the tip at position pos, directed from from, of size radius.

Definition at line 228 of file KisHandlePainterHelper.cpp.

229{
231
232 QPainterPath p;
233
234 QLineF line(pos, from);
235 line.setLength(radius);
236
237 QPointF norm = KisAlgebra2D::leftUnitNormal(pos - from);
238 norm *= 0.34 * radius;
239
240 p.moveTo(line.p2() + norm);
241 p.lineTo(line.p1());
242 p.lineTo(line.p2() - norm);
243
244 p.translate(-pos);
245
246 p = m_handleTransform.map(p).translated(m_painterTransform.map(pos));
247
249 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
250 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
251 m_painter->drawPath(p);
252 }
253}
const Params2D p
QVector< IterationStyle > handleIterations
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
T leftUnitNormal(const T &a)
qreal norm(const T &a)
QPair< QPen, QBrush > stylePair

References KisHandleStyle::handleIterations, KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, KisAlgebra2D::leftUnitNormal(), m_decorationThickness, m_handleStyle, m_handleTransform, m_painter, m_painterTransform, p, and KisHandleStyle::IterationStyle::stylePair.

◆ drawConnectionLine() [1/2]

void KisHandlePainterHelper::drawConnectionLine ( const QLineF & line)

Draw a line connecting two points

Definition at line 293 of file KisHandlePainterHelper.cpp.

294{
295 drawConnectionLine(line.p1(), line.p2());
296}
void drawConnectionLine(const QLineF &line)

References drawConnectionLine().

◆ drawConnectionLine() [2/2]

void KisHandlePainterHelper::drawConnectionLine ( const QPointF & p1,
const QPointF & p2 )

Draw a line connecting two points

Definition at line 298 of file KisHandlePainterHelper.cpp.

299{
301
302 QPointF realP1 = m_painterTransform.map(p1);
303 QPointF realP2 = m_painterTransform.map(p2);
304
306 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
307 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
308 m_painter->drawLine(realP1, realP2);
309 }
310}
QPointF p2
QPointF p1
QVector< IterationStyle > lineIterations

References KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, KisHandleStyle::lineIterations, m_decorationThickness, m_handleStyle, m_painter, m_painterTransform, p1, p2, and KisHandleStyle::IterationStyle::stylePair.

◆ drawGradientArrow()

void KisHandlePainterHelper::drawGradientArrow ( const QPointF & start,
const QPointF & end,
qreal radius )

Draw an arrow representing gradient position

Definition at line 255 of file KisHandlePainterHelper.cpp.

256{
258
259 QPainterPath p;
260 p.moveTo(start);
261 p.lineTo(end);
262 p = m_painterTransform.map(p);
263
265 it.stylePair.first.setWidthF(it.stylePair.first.widthF()*m_decorationThickness);
266 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
267 m_painter->drawPath(p);
268 }
269
270 const qreal length = kisDistance(start, end);
271 const QPointF diff = end - start;
272
273 if (length > 5 * radius) {
274 drawArrow(start + 0.33 * diff, start, radius);
275 drawArrow(start + 0.66 * diff, start, radius);
276 } else if (length > 3 * radius) {
277 drawArrow(start + 0.5 * diff, start, radius);
278 }
279}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
void drawArrow(const QPointF &pos, const QPointF &from, qreal radius)
qreal kisDistance(const QPointF &pt1, const QPointF &pt2)
Definition kis_global.h:190

References drawArrow(), KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, kisDistance(), length(), KisHandleStyle::lineIterations, m_decorationThickness, m_handleStyle, m_painter, m_painterTransform, p, and KisHandleStyle::IterationStyle::stylePair.

◆ drawGradientCrossHandle()

void KisHandlePainterHelper::drawGradientCrossHandle ( const QPointF & center,
qreal radius )

Draw a special handle representing the center of the gradient

Definition at line 188 of file KisHandlePainterHelper.cpp.

188 {
190
191 { // Draw a cross
192 QPainterPath p;
193 p.moveTo(-radius, -radius);
194 p.lineTo(radius, radius);
195 p.moveTo(radius, -radius);
196 p.lineTo(-radius, radius);
197
198 p = m_handleTransform.map(p);
199 p.translate(m_painterTransform.map(center));
200
202 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
203 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
204 m_painter->drawPath(p);
205 }
206 }
207
208 { // Draw a square
209 const qreal halfRadius = 0.5 * radius;
210
211 QPolygonF handlePolygon;
212 handlePolygon << QPointF(-halfRadius, 0);
213 handlePolygon << QPointF(0, halfRadius);
214 handlePolygon << QPointF(halfRadius, 0);
215 handlePolygon << QPointF(0, -halfRadius);
216
217 handlePolygon = m_handleTransform.map(handlePolygon);
218 handlePolygon.translate(m_painterTransform.map(center));
219
221 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
222 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
223 m_painter->drawPolygon(handlePolygon);
224 }
225 }
226}

References KisHandleStyle::handleIterations, KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, m_decorationThickness, m_handleStyle, m_handleTransform, m_painter, m_painterTransform, p, and KisHandleStyle::IterationStyle::stylePair.

◆ drawGradientHandle() [1/2]

void KisHandlePainterHelper::drawGradientHandle ( const QPointF & center)

Draw a rotated handle representing the gradient handle

Definition at line 183 of file KisHandlePainterHelper.cpp.

184{
185 drawGradientHandle(center, 1.41 * m_handleRadius);
186}
void drawGradientHandle(const QPointF &center, qreal radius)

References drawGradientHandle(), and m_handleRadius.

◆ drawGradientHandle() [2/2]

void KisHandlePainterHelper::drawGradientHandle ( const QPointF & center,
qreal radius )

Draw a rotated handle representing the gradient handle

Definition at line 163 of file KisHandlePainterHelper.cpp.

163 {
165
166 QPolygonF handlePolygon;
167
168 handlePolygon << QPointF(-radius, 0);
169 handlePolygon << QPointF(0, radius);
170 handlePolygon << QPointF(radius, 0);
171 handlePolygon << QPointF(0, -radius);
172
173 handlePolygon = m_handleTransform.map(handlePolygon);
174 handlePolygon.translate(m_painterTransform.map(center));
175
177 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
178 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
179 m_painter->drawPolygon(handlePolygon);
180 }
181}

References KisHandleStyle::handleIterations, KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, m_decorationThickness, m_handleStyle, m_handleTransform, m_painter, m_painterTransform, and KisHandleStyle::IterationStyle::stylePair.

◆ drawHandleCircle() [1/2]

void KisHandlePainterHelper::drawHandleCircle ( const QPointF & center)

Optimized version of the drawing method for drawing handles of predefined size

Definition at line 117 of file KisHandlePainterHelper.cpp.

118{
120}
void drawHandleCircle(const QPointF &center, qreal radius)

References drawHandleCircle(), and m_handleRadius.

◆ drawHandleCircle() [2/2]

void KisHandlePainterHelper::drawHandleCircle ( const QPointF & center,
qreal radius )

Draws a handle circle with a custom radius at position center

Definition at line 104 of file KisHandlePainterHelper.cpp.

104 {
106
107 QRectF handleRect(-radius, -radius, 2 * radius, 2 * radius);
108 handleRect.translate(m_painterTransform.map(center));
109
111 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
112 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
113 m_painter->drawEllipse(handleRect);
114 }
115}

References KisHandleStyle::handleIterations, KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, m_decorationThickness, m_handleStyle, m_painter, m_painterTransform, and KisHandleStyle::IterationStyle::stylePair.

◆ drawHandleLine()

void KisHandlePainterHelper::drawHandleLine ( const QLineF & line,
qreal width = 1.0,
QVector< qreal > dashPattern = {},
qreal dashOffset = 0.0 )

Draws a line in the style of a handle with width indicating the thickness.

Definition at line 127 of file KisHandlePainterHelper.cpp.

128{
130
131 QPainterPath p;
132 p.moveTo(m_painterTransform.map(line.p1()));
133 p.lineTo(m_painterTransform.map(line.p2()));
134 QPainterPathStroker s;
135 s.setWidth(width);
136 if (!dashPattern.isEmpty()) {
137 s.setDashPattern(dashPattern);
138 s.setDashOffset(dashOffset);
139 }
140 s.setCapStyle(Qt::RoundCap);
141 s.setJoinStyle(Qt::RoundJoin);
142 p = s.createStroke(p);
143
145 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
146 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
147 m_painter->strokePath(p, m_painter->pen());
148 m_painter->fillPath(p, m_painter->brush());
149 }
150}

References KisHandleStyle::handleIterations, KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, m_decorationThickness, m_handleStyle, m_painter, m_painterTransform, p, and KisHandleStyle::IterationStyle::stylePair.

◆ drawHandleRect() [1/3]

void KisHandlePainterHelper::drawHandleRect ( const QPointF & center)

Optimized version of the drawing method for drawing handles of predefined size

Definition at line 152 of file KisHandlePainterHelper.cpp.

152 {
154 QPolygonF paintingPolygon = m_handlePolygon.translated(m_painterTransform.map(center));
155
157 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
158 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
159 m_painter->drawPolygon(paintingPolygon);
160 }
161}

References KisHandleStyle::handleIterations, KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, m_decorationThickness, m_handlePolygon, m_handleStyle, m_painter, m_painterTransform, and KisHandleStyle::IterationStyle::stylePair.

◆ drawHandleRect() [2/3]

void KisHandlePainterHelper::drawHandleRect ( const QPointF & center,
qreal radius )

Draws a handle rect with a custom radius at position center

◆ drawHandleRect() [3/3]

void KisHandlePainterHelper::drawHandleRect ( const QPointF & center,
qreal radius,
QPoint offset = QPoint(0,0) )

Definition at line 77 of file KisHandlePainterHelper.cpp.

78{
80
81 QRectF handleRect(-radius, -radius, 2 * radius, 2 * radius);
82 QPolygonF handlePolygon = m_handleTransform.map(QPolygonF(handleRect));
83 handlePolygon.translate(m_painterTransform.map(center));
84
85 handlePolygon.translate(offset);
86
87 const QPen originalPen = m_painter->pen();
88
89 // temporarily set the pen width to 2 to avoid pixel shifting dropping pixels the border
90 QPen customPen = m_painter->pen();
91 customPen.setCosmetic(true);
92 customPen.setWidth(4);
93 m_painter->setPen(customPen);
94
96 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
97 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
98 m_painter->drawPolygon(handlePolygon);
99 }
100
101 m_painter->setPen(originalPen);
102}

References KisHandleStyle::handleIterations, KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, m_decorationThickness, m_handleStyle, m_handleTransform, m_painter, m_painterTransform, and KisHandleStyle::IterationStyle::stylePair.

◆ drawHandleSmallCircle()

void KisHandlePainterHelper::drawHandleSmallCircle ( const QPointF & center)

Optimized version of the drawing method for drawing handles of predefined size

Definition at line 122 of file KisHandlePainterHelper.cpp.

123{
124 drawHandleCircle(center, 0.7 * m_handleRadius);
125}

References drawHandleCircle(), and m_handleRadius.

◆ drawPath()

void KisHandlePainterHelper::drawPath ( const QPainterPath & path)

Draw an arbitrary path

Definition at line 312 of file KisHandlePainterHelper.cpp.

313{
314 const QPainterPath realPath = m_painterTransform.map(path);
315
317 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
318 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
319 m_painter->drawPath(realPath);
320 }
321}

References KisHandleStyle::IterationStyle::isValid, KisHandleStyle::lineIterations, m_decorationThickness, m_handleStyle, m_painter, m_painterTransform, and KisHandleStyle::IterationStyle::stylePair.

◆ drawPixmap()

void KisHandlePainterHelper::drawPixmap ( const QPixmap & pixmap,
QPointF position,
int size,
QRectF sourceRect )

Draw an a given pixmap on the UI

Definition at line 323 of file KisHandlePainterHelper.cpp.

324{
325 QPointF handlePolygon = m_painterTransform.map(position);
326
327 QPoint offsetPosition(0, 40);
328 handlePolygon += offsetPosition;
329
330 handlePolygon -= QPointF(size*0.5,size*0.5);
331
332 m_painter->drawPixmap(QRect(handlePolygon.x(), handlePolygon.y(),
333 size, size),
334 pixmap,
335 sourceRect);
336}

References m_painter, and m_painterTransform.

◆ drawRubberLine()

void KisHandlePainterHelper::drawRubberLine ( const QPolygonF & poly)

Draw a line showing the bounding box of the selection

Definition at line 281 of file KisHandlePainterHelper.cpp.

281 {
283
284 QPolygonF paintingPolygon = m_painterTransform.map(poly);
285
287 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
288 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
289 m_painter->drawPolygon(paintingPolygon);
290 }
291}

References KisHandleStyle::IterationStyle::isValid, KIS_SAFE_ASSERT_RECOVER_RETURN, KisHandleStyle::lineIterations, m_decorationThickness, m_handleStyle, m_painter, m_painterTransform, and KisHandleStyle::IterationStyle::stylePair.

◆ fillHandleRect()

void KisHandlePainterHelper::fillHandleRect ( const QPointF & center,
qreal radius,
QColor fillColor,
QPoint offset = QPoint(0,0) )

Definition at line 338 of file KisHandlePainterHelper.cpp.

339{
341
342 QRectF handleRect(-radius, -radius, 2 * radius, 2 * radius);
343 QPolygonF handlePolygon = m_handleTransform.map(QPolygonF(handleRect));
344 handlePolygon.translate(m_painterTransform.map(center));
345
346 QPainterPath painterPath;
347 painterPath.addPolygon(handlePolygon);
348
349 // offset that happens after zoom transform. This means the offset will be the same, no matter the zoom level
350 // this is good for UI elements that need to be below the bounding box
351 painterPath.translate(offset);
352
353 const QPainterPath pathToSend = painterPath;
354 const QBrush brushStyle(fillColor);
355 m_painter->fillPath(pathToSend, brushStyle);
356}

References KIS_SAFE_ASSERT_RECOVER_RETURN, m_handleTransform, m_painter, and m_painterTransform.

◆ init()

◆ setHandleStyle()

void KisHandlePainterHelper::setHandleStyle ( const KisHandleStyle & style)

Sets style used for painting the handles. Please use static methods of KisHandleStyle to select predefined styles.

Definition at line 72 of file KisHandlePainterHelper.cpp.

73{
74 m_handleStyle = style;
75}

References m_handleStyle.

Member Data Documentation

◆ m_decomposedMatrix

KisAlgebra2D::DecomposedMatrix KisHandlePainterHelper::m_decomposedMatrix
private

Definition at line 164 of file KisHandlePainterHelper.h.

◆ m_decorationThickness

int KisHandlePainterHelper::m_decorationThickness
private

Definition at line 163 of file KisHandlePainterHelper.h.

◆ m_handlePolygon

QPolygonF KisHandlePainterHelper::m_handlePolygon
private

Definition at line 166 of file KisHandlePainterHelper.h.

◆ m_handleRadius

qreal KisHandlePainterHelper::m_handleRadius
private

Definition at line 162 of file KisHandlePainterHelper.h.

◆ m_handleStyle

KisHandleStyle KisHandlePainterHelper::m_handleStyle
private

Definition at line 167 of file KisHandlePainterHelper.h.

◆ m_handleTransform

QTransform KisHandlePainterHelper::m_handleTransform
private

Definition at line 165 of file KisHandlePainterHelper.h.

◆ m_originalPainterTransform

QTransform KisHandlePainterHelper::m_originalPainterTransform
private

Definition at line 160 of file KisHandlePainterHelper.h.

◆ m_painter

QPainter* KisHandlePainterHelper::m_painter
private

Definition at line 159 of file KisHandlePainterHelper.h.

◆ m_painterTransform

QTransform KisHandlePainterHelper::m_painterTransform
private

Definition at line 161 of file KisHandlePainterHelper.h.


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