Krita Source Code Documentation
Loading...
Searching...
No Matches
KoMeshGradientBackground.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 */
4
7#include <KoMixColorsOp.h>
8#include <kis_algebra_2d.h>
9
10#include <QRegion>
11#include <QPainter>
12#include <QPainterPath>
13#include <QDebug>
14
16
17class KoMeshGradientBackground::Private : public QSharedData
18{
19public:
21 : QSharedData()
22 , gradient(0)
24 {}
25
26 Private(const Private& other)
27 : QSharedData()
28 , gradient(new SvgMeshGradient(*other.gradient))
29 , matrix(other.matrix)
31 {
32 }
33
35 delete renderer;
36 }
37
38 QScopedPointer<SvgMeshGradient> gradient;
39 QTransform matrix;
41};
42
45 , d(new Private)
46{
47 d->gradient.reset(new SvgMeshGradient(*gradient));
48 d->matrix = matrix;
49 Q_ASSERT(d->gradient);
50}
51
55
65
66void KoMeshGradientBackground::paint(QPainter &painter,
67 const QPainterPath &fillPath) const
68{
69 if (!d->gradient || !d->gradient->isValid()) return;
70 painter.save();
71
72 QScopedPointer<SvgMeshGradient> gradient(new SvgMeshGradient(*d->gradient));
73
74 QRectF meshBoundingRect = gradient->boundingRect();
75
77 const QTransform relativeToShape = KisAlgebra2D::mapToRect(fillPath.boundingRect());
78 gradient->setTransform(relativeToShape);
79 meshBoundingRect = gradient->boundingRect();
80 }
81
82 if (d->renderer->patchImage()->isNull()) {
83
84 d->renderer->configure(meshBoundingRect, painter.transform());
85 SvgMeshArray *mesharray = gradient->getMeshArray().data();
86
87 for (int row = 0; row < mesharray->numRows(); ++row) {
88 for (int col = 0; col < mesharray->numColumns(); ++col) {
89 SvgMeshPatch *patch = mesharray->getPatch(row, col);
90 d->renderer->fillPatch(patch, gradient->type(), mesharray, row, col);
91 }
92 }
93 // uncomment to debug
94 // d->renderer->patchImage()->save("mesh-patch.png");
95 }
96
97 painter.setClipPath(fillPath);
98
99 // patch is to be drawn wrt. to "user" coordinates
100 painter.drawImage(meshBoundingRect, *d->renderer->patchImage());
101
102 painter.restore();
103}
104
106{
107 return false;
108}
109
111{
112 return d->gradient.data();
113}
114
116{
117 return d->matrix;
118}
QScopedPointer< SvgMeshGradient > gradient
void paint(QPainter &painter, const QPainterPath &fillPath) const override
Paints the background using the given fill path.
QSharedDataPointer< Private > d
bool compareTo(const KoShapeBackground *other) const override
KoMeshGradientBackground(const SvgMeshGradient *gradient, const QTransform &matrix=QTransform())
KoMeshGradientBackground & operator=(const KoMeshGradientBackground &)
int numRows() const
int numColumns() const
SvgMeshPatch * getPatch(const int row, const int col) const
void setTransform(const QTransform &matrix)
SvgMeshGradient::Shading type() const
QRectF boundingRect() const
const QScopedPointer< SvgMeshArray > & getMeshArray() const
KoFlake::CoordinateSystem gradientUnits() const
QTransform mapToRect(const QRectF &rect)