Krita Source Code Documentation
Loading...
Searching...
No Matches
KoHatchBackground.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 *
3 * SPDX-FileCopyrightText: 2012 Thorsten Zachmann <zachmann@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "KoHatchBackground.h"
9
11
12#include <KoXmlNS.h>
13#include <KoUnit.h>
14
15
16#include <FlakeDebug.h>
17
18#include <QColor>
19#include <QString>
20#include <QPainter>
21#include <QPainterPath>
22
23class KoHatchBackground::Private : public QSharedData
24{
25public:
27 : QSharedData()
28 , angle(0.0)
29 , distance(1.0)
31 {}
32
33 QColor lineColor;
34 int angle;
35 qreal distance;
37 QString name;
38};
39
45
49
50void KoHatchBackground::paint(QPainter &painter, const QPainterPath &fillPath) const
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}
A simple solid color shape background.
void paint(QPainter &painter, const QPainterPath &fillPath) const override
Paints the background using the given fill path.
QColor color() const
Returns the background color.
KoHatchBackground::HatchStyle style
QSharedDataPointer< Private > d
void paint(QPainter &painter, const QPainterPath &fillPath) const override
Paints the background using the given fill path.
#define M_PI
Definition kis_global.h:111