1 /*
2  *  Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "KoVectorPatternBackground.h"
20 
21 #include <QTransform>
22 #include <KoShape.h>
23 #include <KoShapePainter.h>
24 #include <KoBakedShapeRenderer.h>
25 
26 class KoVectorPatternBackground::Private : public QSharedData
27 {
28 public:
Private()29     Private()
30         : QSharedData()
31     {
32     }
33 
~Private()34     ~Private()
35     {
36         qDeleteAll(shapes);
37         shapes.clear();
38     }
39 
40     QList<KoShape*> shapes;
41     KoFlake::CoordinateSystem referenceCoordinates =
42             KoFlake::ObjectBoundingBox;
43     KoFlake::CoordinateSystem contentCoordinates =
44             KoFlake::UserSpaceOnUse;
45     QRectF referenceRect;
46     QTransform patternTransform;
47 };
48 
KoVectorPatternBackground()49 KoVectorPatternBackground::KoVectorPatternBackground()
50     : KoShapeBackground()
51     , d(new Private)
52 {
53 }
54 
~KoVectorPatternBackground()55 KoVectorPatternBackground::~KoVectorPatternBackground()
56 {
57 
58 }
59 
compareTo(const KoShapeBackground * other) const60 bool KoVectorPatternBackground::compareTo(const KoShapeBackground *other) const
61 {
62     Q_UNUSED(other);
63     return false;
64 }
65 
setReferenceCoordinates(KoFlake::CoordinateSystem value)66 void KoVectorPatternBackground::setReferenceCoordinates(KoFlake::CoordinateSystem value)
67 {
68     d->referenceCoordinates = value;
69 }
70 
referenceCoordinates() const71 KoFlake::CoordinateSystem KoVectorPatternBackground::referenceCoordinates() const
72 {
73     return d->referenceCoordinates;
74 }
75 
setContentCoordinates(KoFlake::CoordinateSystem value)76 void KoVectorPatternBackground::setContentCoordinates(KoFlake::CoordinateSystem value)
77 {
78     d->contentCoordinates = value;
79 }
80 
contentCoordinates() const81 KoFlake::CoordinateSystem KoVectorPatternBackground::contentCoordinates() const
82 {
83     return d->contentCoordinates;
84 }
85 
setReferenceRect(const QRectF & value)86 void KoVectorPatternBackground::setReferenceRect(const QRectF &value)
87 {
88     d->referenceRect = value;
89 }
90 
referenceRect() const91 QRectF KoVectorPatternBackground::referenceRect() const
92 {
93     return d->referenceRect;
94 }
95 
setPatternTransform(const QTransform & value)96 void KoVectorPatternBackground::setPatternTransform(const QTransform &value)
97 {
98     d->patternTransform = value;
99 }
100 
patternTransform() const101 QTransform KoVectorPatternBackground::patternTransform() const
102 {
103     return d->patternTransform;
104 }
105 
setShapes(const QList<KoShape * > value)106 void KoVectorPatternBackground::setShapes(const QList<KoShape*> value)
107 {
108     qDeleteAll(d->shapes);
109     d->shapes.clear();
110 
111     d->shapes = value;
112 }
113 
shapes() const114 QList<KoShape *> KoVectorPatternBackground::shapes() const
115 {
116     return d->shapes;
117 }
118 
paint(QPainter & painter,KoShapePaintingContext & context_Unused,const QPainterPath & fillPath) const119 void KoVectorPatternBackground::paint(QPainter &painter, KoShapePaintingContext &context_Unused, const QPainterPath &fillPath) const
120 {
121     Q_UNUSED(context_Unused);
122 
123     const QPainterPath dstShapeOutline = fillPath;
124     const QRectF dstShapeBoundingBox = dstShapeOutline.boundingRect();
125 
126     KoBakedShapeRenderer renderer(dstShapeOutline, QTransform(),
127                                   QTransform(),
128                                   d->referenceRect,
129                                   d->contentCoordinates != KoFlake::UserSpaceOnUse,
130                                   dstShapeBoundingBox,
131                                   d->referenceCoordinates != KoFlake::UserSpaceOnUse,
132                                   d->patternTransform);
133 
134     QPainter *patchPainter = renderer.bakeShapePainter();
135 
136     KoShapePainter p;
137     p.setShapes(d->shapes);
138     p.paint(*patchPainter);
139 
140     // uncomment for debug
141     // renderer.patchImage().save("dd_patch_image.png");
142 
143     painter.setPen(Qt::NoPen);
144     renderer.renderShape(painter);
145 }
146 
hasTransparency() const147 bool KoVectorPatternBackground::hasTransparency() const
148 {
149     return true;
150 }
151 
fillStyle(KoGenStyle &,KoShapeSavingContext &)152 void KoVectorPatternBackground::fillStyle(KoGenStyle &, KoShapeSavingContext &)
153 {
154     // noop
155 }
156 
loadStyle(KoOdfLoadingContext &,const QSizeF & Size)157 bool KoVectorPatternBackground::loadStyle(KoOdfLoadingContext &, const QSizeF &Size)
158 {
159     Q_UNUSED(Size);
160     return true;
161 }
162 
163