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 230 of file KisHandlePainterHelper.cpp.

231{
233
234 QPainterPath p;
235
236 QLineF line(pos, from);
237 line.setLength(radius);
238
239 QPointF norm = KisAlgebra2D::leftUnitNormal(pos - from);
240 norm *= 0.34 * radius;
241
242 p.moveTo(line.p2() + norm);
243 p.lineTo(line.p1());
244 p.lineTo(line.p2() - norm);
245
246 p.translate(-pos);
247
248 p = m_handleTransform.map(p).translated(m_painterTransform.map(pos));
249
251 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
252 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
253 m_painter->drawPath(p);
254 }
255}
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 295 of file KisHandlePainterHelper.cpp.

296{
297 drawConnectionLine(line.p1(), line.p2());
298}
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 300 of file KisHandlePainterHelper.cpp.

301{
303
304 QPointF realP1 = m_painterTransform.map(p1);
305 QPointF realP2 = m_painterTransform.map(p2);
306
308 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
309 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
310 m_painter->drawLine(realP1, realP2);
311 }
312}
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 257 of file KisHandlePainterHelper.cpp.

258{
260
261 QPainterPath p;
262 p.moveTo(start);
263 p.lineTo(end);
264 p = m_painterTransform.map(p);
265
267 it.stylePair.first.setWidthF(it.stylePair.first.widthF()*m_decorationThickness);
268 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
269 m_painter->drawPath(p);
270 }
271
272 const qreal length = kisDistance(start, end);
273 const QPointF diff = end - start;
274
275 if (length > 5 * radius) {
276 drawArrow(start + 0.33 * diff, start, radius);
277 drawArrow(start + 0.66 * diff, start, radius);
278 } else if (length > 3 * radius) {
279 drawArrow(start + 0.5 * diff, start, radius);
280 }
281}
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 190 of file KisHandlePainterHelper.cpp.

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

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 185 of file KisHandlePainterHelper.cpp.

186{
187 drawGradientHandle(center, 1.41 * m_handleRadius);
188}
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 165 of file KisHandlePainterHelper.cpp.

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

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 119 of file KisHandlePainterHelper.cpp.

120{
122}
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 106 of file KisHandlePainterHelper.cpp.

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

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 129 of file KisHandlePainterHelper.cpp.

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

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 154 of file KisHandlePainterHelper.cpp.

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

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 *tempPen = new QPen(m_painter->pen());
91 tempPen->setCosmetic(true);
92 tempPen->setWidth(4 * m_decorationThickness);
93 const QPen customPen = *tempPen;
94 m_painter->setPen(customPen);
95
96
98 it.stylePair.first.setWidthF(it.stylePair.first.widthF() * m_decorationThickness);
99 PenBrushSaver saver(it.isValid ? m_painter : 0, it.stylePair, PenBrushSaver::allow_noop);
100 m_painter->drawPolygon(handlePolygon);
101 }
102
103 m_painter->setPen(originalPen);
104}

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 124 of file KisHandlePainterHelper.cpp.

125{
126 drawHandleCircle(center, 0.7 * m_handleRadius);
127}

References drawHandleCircle(), and m_handleRadius.

◆ drawPath()

void KisHandlePainterHelper::drawPath ( const QPainterPath & path)

Draw an arbitrary path

Definition at line 314 of file KisHandlePainterHelper.cpp.

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

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 325 of file KisHandlePainterHelper.cpp.

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

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 283 of file KisHandlePainterHelper.cpp.

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

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 340 of file KisHandlePainterHelper.cpp.

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

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: