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

A pattern shape background. More...

#include <KoPatternBackground.h>

+ Inheritance diagram for KoPatternBackground:

Classes

class  Private
 

Public Types

enum  PatternRepeat { Original , Tiled , Stretched }
 Pattern rendering style. More...
 
enum  ReferencePoint {
  TopLeft , Top , TopRight , Left ,
  Center , Right , BottomLeft , Bottom ,
  BottomRight
}
 Pattern reference point. More...
 

Public Member Functions

bool compareTo (const KoShapeBackground *other) const override
 
 KoPatternBackground ()
 
 KoPatternBackground (const KoPatternBackground &)
 
KoPatternBackgroundoperator= (const KoPatternBackground &)
 
void paint (QPainter &painter, const QPainterPath &fillPath) const override
 reimplemented from KoShapeBackground
 
QImage pattern () const
 Returns the pattern.
 
QSizeF patternDisplaySize () const
 Returns the pattern display size.
 
QSizeF patternOriginalSize () const
 Returns the original image size.
 
QRectF patternRectFromFillSize (const QSizeF &size)
 Returns the bounding rect of the pattern image based on the given fill size.
 
ReferencePoint referencePoint () const
 Returns the pattern reference point identifier.
 
QPointF referencePointOffset () const
 Returns reference point offset in percent of the pattern display size.
 
PatternRepeat repeat () const
 Returns the pattern repeat.
 
void setPattern (const QImage &pattern)
 Sets a new pattern.
 
void setPatternDisplaySize (const QSizeF &size)
 Sets pattern display size.
 
void setReferencePoint (ReferencePoint referencePoint)
 Sets the pattern reference point.
 
void setReferencePointOffset (const QPointF &offset)
 Sets the reference point offset in percent of the pattern display size.
 
void setRepeat (PatternRepeat repeat)
 Sets the pattern repeatgfl.
 
void setTileRepeatOffset (const QPointF &offset)
 Sets the tile repeat offset in percent of the pattern display size.
 
void setTransform (const QTransform &matrix)
 Sets the transform matrix.
 
QPointF tileRepeatOffset () const
 Returns tile repeat offset in percent of the pattern display size.
 
QTransform transform () const
 Returns the transform matrix.
 
 ~KoPatternBackground () 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 pattern shape background.

Definition at line 23 of file KoPatternBackground.h.

Member Enumeration Documentation

◆ PatternRepeat

Pattern rendering style.

Enumerator
Original 
Tiled 
Stretched 

Definition at line 27 of file KoPatternBackground.h.

◆ ReferencePoint

Pattern reference point.

Enumerator
TopLeft 
Top 
TopRight 
Left 
Center 
Right 
BottomLeft 
Bottom 
BottomRight 

Definition at line 33 of file KoPatternBackground.h.

Constructor & Destructor Documentation

◆ KoPatternBackground() [1/2]

KoPatternBackground::KoPatternBackground ( )
explicit

Definition at line 110 of file KoPatternBackground.cpp.

112 , d(new Private)
113{
114}
QSharedDataPointer< Private > d

◆ ~KoPatternBackground()

KoPatternBackground::~KoPatternBackground ( )
override

Definition at line 116 of file KoPatternBackground.cpp.

117{
118}

◆ KoPatternBackground() [2/2]

KoPatternBackground::KoPatternBackground ( const KoPatternBackground & rhs)

Definition at line 120 of file KoPatternBackground.cpp.

121 : d(new Private(*rhs.d))
122{
123}

Member Function Documentation

◆ compareTo()

bool KoPatternBackground::compareTo ( const KoShapeBackground * other) const
overridevirtual

Implements KoShapeBackground.

Definition at line 131 of file KoPatternBackground.cpp.

132{
133 Q_UNUSED(other);
134 return false;
135}

◆ operator=()

KoPatternBackground & KoPatternBackground::operator= ( const KoPatternBackground & rhs)

Definition at line 125 of file KoPatternBackground.cpp.

126{
127 d = rhs.d;
128 return *this;
129}

References d.

◆ paint()

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

reimplemented from KoShapeBackground

Implements KoShapeBackground.

Definition at line 216 of file KoPatternBackground.cpp.

217{
218 if (d->pattern.isNull()) {
219 return;
220 }
221
222 painter.save();
223 if (d->repeat == Tiled) {
224 // calculate scaling of pixmap
225 QSizeF targetSize = d->targetSize();
226 QSizeF imageSize = d->pattern.size();
227 qreal scaleX = targetSize.width() / imageSize.width();
228 qreal scaleY = targetSize.height() / imageSize.height();
229
230 QRectF targetRect = fillPath.boundingRect();
231 // undo scaling on target rectangle
232 targetRect.setWidth(targetRect.width() / scaleX);
233 targetRect.setHeight(targetRect.height() / scaleY);
234
235 // determine pattern offset
236 QPointF offset = d->offsetFromRect(targetRect, imageSize);
237
238 // create matrix for pixmap scaling
239 QTransform matrix;
240 matrix.scale(scaleX, scaleY);
241
242 painter.setClipPath(fillPath);
243 painter.setWorldTransform(matrix, true);
244 painter.drawTiledPixmap(targetRect, QPixmap::fromImage(d->pattern), -offset);
245 } else if (d->repeat == Original) {
246 QRectF sourceRect(QPointF(0, 0), d->pattern.size());
247 QRectF targetRect(QPoint(0, 0), d->targetSize());
248 targetRect.moveCenter(fillPath.boundingRect().center());
249 painter.setClipPath(fillPath);
250 painter.drawPixmap(targetRect, QPixmap::fromImage(d->pattern).scaled(sourceRect.size().toSize()), sourceRect);
251 } else if (d->repeat == Stretched) {
252 painter.setClipPath(fillPath);
253 // undo conversion of the scaling so that we can use a nicely scaled image of the correct size
254 qWarning() << "WARNING: stretched KoPatternBackground painting code is abandoned. The result might be not correct";
255 const QRectF targetRect = fillPath.boundingRect();
256 painter.drawPixmap(targetRect.topLeft(), QPixmap::fromImage(d->pattern).scaled(targetRect.size().toSize()));
257 }
258
259 painter.restore();
260}

References d, Original, Stretched, and Tiled.

◆ pattern()

QImage KoPatternBackground::pattern ( ) const

Returns the pattern.

Definition at line 152 of file KoPatternBackground.cpp.

153{
154 return d->pattern;
155}

References d.

◆ patternDisplaySize()

QSizeF KoPatternBackground::patternDisplaySize ( ) const

Returns the pattern display size.

Definition at line 200 of file KoPatternBackground.cpp.

201{
202 return d->targetSize();
203}

References d.

◆ patternOriginalSize()

QSizeF KoPatternBackground::patternOriginalSize ( ) const

Returns the original image size.

Definition at line 211 of file KoPatternBackground.cpp.

212{
213 return d->pattern.size();
214}

References d.

◆ patternRectFromFillSize()

QRectF KoPatternBackground::patternRectFromFillSize ( const QSizeF & size)

Returns the bounding rect of the pattern image based on the given fill size.

Definition at line 263 of file KoPatternBackground.cpp.

264{
265 QRectF rect;
266
267 switch (d->repeat) {
268 case Tiled:
269 rect.setTopLeft(d->offsetFromRect(QRectF(QPointF(), size), d->targetSize()));
270 rect.setSize(d->targetSize());
271 break;
272 case Original:
273 rect.setLeft(0.5 * (size.width() - d->targetSize().width()));
274 rect.setTop(0.5 * (size.height() - d->targetSize().height()));
275 rect.setSize(d->targetSize());
276 break;
277 case Stretched:
278 rect.setTopLeft(QPointF(0.0, 0.0));
279 rect.setSize(size);
280 break;
281 }
282
283 return rect;
284}
int size(const Forest< T > &forest)
Definition KisForest.h:1232

References d, Original, Stretched, and Tiled.

◆ referencePoint()

KoPatternBackground::ReferencePoint KoPatternBackground::referencePoint ( ) const

Returns the pattern reference point identifier.

Definition at line 167 of file KoPatternBackground.cpp.

168{
169 return d->refPoint;
170}

References d.

◆ referencePointOffset()

QPointF KoPatternBackground::referencePointOffset ( ) const

Returns reference point offset in percent of the pattern display size.

Definition at line 177 of file KoPatternBackground.cpp.

178{
179 return d->refPointOffsetPercent;
180}

References d.

◆ repeat()

KoPatternBackground::PatternRepeat KoPatternBackground::repeat ( ) const

Returns the pattern repeat.

Definition at line 162 of file KoPatternBackground.cpp.

163{
164 return d->repeat;
165}

References d.

◆ setPattern()

void KoPatternBackground::setPattern ( const QImage & pattern)

Sets a new pattern.

Definition at line 147 of file KoPatternBackground.cpp.

148{
149 d->pattern = pattern;
150}
QImage pattern() const
Returns the pattern.

References d, and pattern().

◆ setPatternDisplaySize()

void KoPatternBackground::setPatternDisplaySize ( const QSizeF & size)

Sets pattern display size.

Definition at line 205 of file KoPatternBackground.cpp.

206{
207 d->targetImageSizePercent = QSizeF();
208 d->targetImageSize = size;
209}

References d.

◆ setReferencePoint()

void KoPatternBackground::setReferencePoint ( ReferencePoint referencePoint)

Sets the pattern reference point.

Definition at line 172 of file KoPatternBackground.cpp.

173{
174 d->refPoint = referencePoint;
175}
ReferencePoint referencePoint() const
Returns the pattern reference point identifier.

References d, and referencePoint().

◆ setReferencePointOffset()

void KoPatternBackground::setReferencePointOffset ( const QPointF & offset)

Sets the reference point offset in percent of the pattern display size.

Definition at line 182 of file KoPatternBackground.cpp.

183{
184 qreal ox = qMax(qreal(0.0), qMin(qreal(100.0), offset.x()));
185 qreal oy = qMax(qreal(0.0), qMin(qreal(100.0), offset.y()));
186
187 d->refPointOffsetPercent = QPointF(ox, oy);
188}

References d.

◆ setRepeat()

void KoPatternBackground::setRepeat ( PatternRepeat repeat)

Sets the pattern repeatgfl.

Definition at line 157 of file KoPatternBackground.cpp.

158{
159 d->repeat = repeat;
160}
PatternRepeat repeat() const
Returns the pattern repeat.

References d, and repeat().

◆ setTileRepeatOffset()

void KoPatternBackground::setTileRepeatOffset ( const QPointF & offset)

Sets the tile repeat offset in percent of the pattern display size.

Definition at line 195 of file KoPatternBackground.cpp.

196{
197 d->tileRepeatOffsetPercent = offset;
198}

References d.

◆ setTransform()

void KoPatternBackground::setTransform ( const QTransform & matrix)

Sets the transform matrix.

Definition at line 137 of file KoPatternBackground.cpp.

138{
139 d->matrix = matrix;
140}

References d.

◆ tileRepeatOffset()

QPointF KoPatternBackground::tileRepeatOffset ( ) const

Returns tile repeat offset in percent of the pattern display size.

Definition at line 190 of file KoPatternBackground.cpp.

191{
192 return d->tileRepeatOffsetPercent;
193}

References d.

◆ transform()

QTransform KoPatternBackground::transform ( ) const

Returns the transform matrix.

Definition at line 142 of file KoPatternBackground.cpp.

143{
144 return d->matrix;
145}

References d.

Member Data Documentation

◆ d

QSharedDataPointer<Private> KoPatternBackground::d
private

Definition at line 107 of file KoPatternBackground.h.


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