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

#include <KoHatchBackground.h>

+ Inheritance diagram for KoHatchBackground:

Classes

class  Private
 

Public Types

enum  HatchStyle { Single , Double , Triple }
 

Public Member Functions

 KoHatchBackground ()
 
void paint (QPainter &painter, const QPainterPath &fillPath) const override
 Paints the background using the given fill path.
 
 ~KoHatchBackground () override
 
- Public Member Functions inherited from KoColorBackground
QBrush brush () const
 
QColor color () const
 Returns the background color.
 
bool compareTo (const KoShapeBackground *other) const override
 
 KoColorBackground ()
 
 KoColorBackground (const KoColorBackground &)
 
 KoColorBackground (const QColor &color, Qt::BrushStyle style=Qt::SolidPattern)
 Creates background from given color and style.
 
KoColorBackgroundoperator= (const KoColorBackground &)
 
void setColor (const QColor &color)
 Sets the background color.
 
Qt::BrushStyle style () const
 Returns the background style.
 
 ~KoColorBackground () override
 
- Public Member Functions inherited from KoShapeBackground
virtual bool hasTransparency () const
 Returns if the background has some transparency.
 
 KoShapeBackground ()
 
virtual operator bool () const
 
virtual ~KoShapeBackground ()
 

Private Attributes

QSharedDataPointer< Privated
 

Detailed Description

A hatch shape background

Definition at line 16 of file KoHatchBackground.h.

Member Enumeration Documentation

◆ HatchStyle

Enumerator
Single 
Double 
Triple 

Definition at line 19 of file KoHatchBackground.h.

Constructor & Destructor Documentation

◆ KoHatchBackground()

KoHatchBackground::KoHatchBackground ( )

Definition at line 40 of file KoHatchBackground.cpp.

42 , d(new Private)
43{
44}
QSharedDataPointer< Private > d

◆ ~KoHatchBackground()

KoHatchBackground::~KoHatchBackground ( )
override

Definition at line 46 of file KoHatchBackground.cpp.

47{
48}

Member Function Documentation

◆ paint()

void KoHatchBackground::paint ( QPainter & painter,
const QPainterPath & fillPath ) const
overridevirtual

Paints the background using the given fill path.

Reimplemented from KoColorBackground.

Definition at line 50 of file KoHatchBackground.cpp.

51{
52 if (color().isValid()) {
53 // paint background color if set by using the color background
54 KoColorBackground::paint(painter, fillPath);
55 }
56
57 const QRectF targetRect = fillPath.boundingRect();
58 painter.save();
59 painter.setClipPath(fillPath);
60 QPen pen(d->lineColor);
61 // we set the pen width to 0.5 pt for the hatch. This is not defined in the spec.
62 pen.setWidthF(0.5);
63 painter.setPen(pen);
64 QVector<QLineF> lines;
65
66 // The different styles are handled by painting the lines multiple times with a different
67 // angel offset as basically it just means we paint the lines also at a different angle.
68 // This are the angle offsets we need to apply to the different lines of a style.
69 // -90 is for single, 0 for the 2nd line in double and -45 for the 3th line in triple.
70 const int angleOffset[] = {-90, 0, -45 };
71 // The number of loops is defined by the style.
72 int loops = (d->style == Single) ? 1 : (d->style == Double) ? 2 : 3;
73
74 for (int i = 0; i < loops; ++i) {
75 int angle = d->angle - angleOffset[i];
76 qreal cosAngle = ::cos(angle/180.0*M_PI);
77 // if cos is nearly 0 the lines are horizontal. Use a special case for that
78 if (qAbs(cosAngle) > 0.00001) {
79 qreal xDiff = tan(angle/180.0*M_PI) * targetRect.height();
80 // calculate the distance we need to increase x when creating the lines so that the
81 // distance between the lines is also correct for rotated lines.
82 qreal xOffset = qAbs(d->distance / cosAngle);
83
84 // if the lines go to the right we need to start more to the left. Get the correct start.
85 qreal xStart = 0;
86 while (-xDiff < xStart) {
87 xStart -= xOffset;
88 }
89
90 // if the lines go to the left we need to stop more at the right. Get the correct end offset
91 qreal xEndOffset = 0;
92 if (xDiff < 0) {
93 while (xDiff < -xEndOffset) {
94 xEndOffset += xOffset;
95 }
96 }
97 // create line objects.
98 lines.reserve(lines.size() + int((targetRect.width() + xEndOffset - xStart) / xOffset) + 1);
99 for (qreal x = xStart; x < targetRect.width() + xEndOffset; x += xOffset) {
100 lines.append(QLineF(x, 0, x + xDiff, targetRect.height()));
101 }
102 }
103 else {
104 // horizontal lines
105 lines.reserve(lines.size() + int(targetRect.height()/d->distance) + 1);
106 for (qreal y = 0; y < targetRect.height(); y += d->distance) {
107 lines.append(QLineF(0, y, targetRect.width(), y));
108 }
109 }
110 }
111
112 painter.drawLines(lines);
113 painter.restore();
114}
void paint(QPainter &painter, const QPainterPath &fillPath) const override
Paints the background using the given fill path.
Qt::BrushStyle style() const
Returns the background style.
QColor color() const
Returns the background color.
#define M_PI
Definition kis_global.h:111

References KoColorBackground::color(), d, Double, M_PI, KoColorBackground::paint(), and Single.

Member Data Documentation

◆ d

QSharedDataPointer<Private> KoHatchBackground::d
private

Definition at line 33 of file KoHatchBackground.h.


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