Krita Source Code Documentation
Loading...
Searching...
No Matches
KoGradientBackground.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2008 Jan Hambrecht <jaham@gmx.net>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
8#include "KoFlake.h"
9#include <KoXmlNS.h>
11
12#include <FlakeDebug.h>
13
14#include <QBrush>
15#include <QPainter>
16#include <QSharedData>
17#include <QPainterPath>
18
19class KoGradientBackground::Private : public QSharedData
20{
21public:
23 : QSharedData()
24 , gradient(0)
25 {}
26
27 QGradient *gradient;
28 QTransform matrix;
29};
30
31KoGradientBackground::KoGradientBackground(QGradient * gradient, const QTransform &matrix)
33 , d(new Private)
34{
35 d->gradient = gradient;
36 d->matrix = matrix;
37 Q_ASSERT(d->gradient);
38}
39
40KoGradientBackground::KoGradientBackground(const QGradient & gradient, const QTransform &matrix)
42 , d(new Private)
43{
44 d->gradient = KoFlake::cloneGradient(&gradient);
45 d->matrix = matrix;
46 Q_ASSERT(d->gradient);
47}
48
50{
51 delete d->gradient;
52}
53
58
60{
61 d = rhs.d;
62 return *this;
63}
64
66{
67 const KoGradientBackground *otherGradient = dynamic_cast<const KoGradientBackground*>(other);
68
69 return otherGradient &&
70 d->matrix == otherGradient->d->matrix &&
71 *d->gradient == *otherGradient->d->gradient;
72}
73
74void KoGradientBackground::setTransform(const QTransform &matrix)
75{
76 d->matrix = matrix;
77}
78
80{
81 return d->matrix;
82}
83
84void KoGradientBackground::setGradient(const QGradient &gradient)
85{
86 delete d->gradient;
87
88 d->gradient = KoFlake::cloneGradient(&gradient);
89 Q_ASSERT(d->gradient);
90}
91
92const QGradient * KoGradientBackground::gradient() const
93{
94 return d->gradient;
95}
96
97void KoGradientBackground::paint(QPainter &painter, const QPainterPath &fillPath) const
98{
99 if (!d->gradient) return;
100
101 if (d->gradient->coordinateMode() == QGradient::ObjectBoundingMode) {
102
117 const QRectF boundingRect = fillPath.boundingRect();
118 QTransform gradientToUser(boundingRect.width(), 0, 0, boundingRect.height(),
119 boundingRect.x(), boundingRect.y());
120
121 // TODO: how about slicing the object?
122 QGradient g = *d->gradient;
123 g.setCoordinateMode(QGradient::LogicalMode);
124
125 QBrush b(g);
126 b.setTransform(d->matrix * gradientToUser);
127 painter.setBrush(b);
128 } else {
129 QBrush b(*d->gradient);
130 b.setTransform(d->matrix);
131 painter.setBrush(b);
132 }
133
134 painter.drawPath(fillPath);
135}
A gradient shape background.
const QGradient * gradient() const
Returns the gradient.
bool compareTo(const KoShapeBackground *other) const override
~KoGradientBackground() override
Destroys the background.
KoGradientBackground(QGradient *gradient, const QTransform &matrix=QTransform())
QTransform transform() const
Returns the transform matrix.
void setTransform(const QTransform &matrix)
Sets the transform matrix.
void setGradient(const QGradient &gradient)
KoGradientBackground & operator=(const KoGradientBackground &)
void paint(QPainter &painter, const QPainterPath &fillPath) const override
reimplemented from KoShapeBackground
QSharedDataPointer< Private > d
KRITAFLAKE_EXPORT QGradient * cloneGradient(const QGradient *gradient)
clones the given gradient
Definition KoFlake.cpp:17