16#include <QPainterPath>
47 QPointF
offsetFromRect(
const QRectF &fillRect,
const QSizeF &imageSize)
const {
51 offset = fillRect.topLeft();
54 offset.setX(fillRect.center().x() - 0.5 * imageSize.width());
55 offset.setY(fillRect.top());
58 offset.setX(fillRect.right() - imageSize.width());
59 offset.setY(fillRect.top());
62 offset.setX(fillRect.left());
63 offset.setY(fillRect.center().y() - 0.5 * imageSize.height());
66 offset.setX(fillRect.center().x() - 0.5 * imageSize.width());
67 offset.setY(fillRect.center().y() - 0.5 * imageSize.height());
70 offset.setX(fillRect.right() - imageSize.width());
71 offset.setY(fillRect.center().y() - 0.5 * imageSize.height());
74 offset.setX(fillRect.left());
75 offset.setY(fillRect.bottom() - imageSize.height());
78 offset.setX(fillRect.center().x() - 0.5 * imageSize.width());
79 offset.setY(fillRect.bottom() - imageSize.height());
82 offset.setX(fillRect.right() - imageSize.width());
83 offset.setY(fillRect.bottom() - imageSize.height());
179 return d->refPointOffsetPercent;
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()));
187 d->refPointOffsetPercent = QPointF(ox, oy);
192 return d->tileRepeatOffsetPercent;
197 d->tileRepeatOffsetPercent = offset;
202 return d->targetSize();
207 d->targetImageSizePercent = QSizeF();
208 d->targetImageSize = size;
213 return d->pattern.size();
218 if (
d->pattern.isNull()) {
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();
230 QRectF targetRect = fillPath.boundingRect();
232 targetRect.setWidth(targetRect.width() / scaleX);
233 targetRect.setHeight(targetRect.height() / scaleY);
236 QPointF offset =
d->offsetFromRect(targetRect, imageSize);
240 matrix.scale(scaleX, scaleY);
242 painter.setClipPath(fillPath);
243 painter.setWorldTransform(matrix,
true);
244 painter.drawTiledPixmap(targetRect, QPixmap::fromImage(
d->pattern), -offset);
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);
252 painter.setClipPath(fillPath);
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()));
269 rect.setTopLeft(
d->offsetFromRect(QRectF(QPointF(), size),
d->targetSize()));
270 rect.setSize(
d->targetSize());
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());
278 rect.setTopLeft(QPointF(0.0, 0.0));
QPointF tileRepeatOffsetPercent
QPointF refPointOffsetPercent
KoPatternBackground::ReferencePoint refPoint
QPointF offsetFromRect(const QRectF &fillRect, const QSizeF &imageSize) const
KoPatternBackground::PatternRepeat repeat
QSizeF targetImageSizePercent
QSizeF targetSize() const
A pattern shape background.
PatternRepeat repeat() const
Returns the pattern repeat.
void setRepeat(PatternRepeat repeat)
Sets the pattern repeatgfl.
void paint(QPainter &painter, const QPainterPath &fillPath) const override
reimplemented from KoShapeBackground
QRectF patternRectFromFillSize(const QSizeF &size)
Returns the bounding rect of the pattern image based on the given fill size.
QPointF referencePointOffset() const
Returns reference point offset in percent of the pattern display size.
KoPatternBackground & operator=(const KoPatternBackground &)
QSizeF patternOriginalSize() const
Returns the original image size.
QImage pattern() const
Returns the pattern.
QSharedDataPointer< Private > d
PatternRepeat
Pattern rendering style.
ReferencePoint
Pattern reference point.
QSizeF patternDisplaySize() const
Returns the pattern display size.
QPointF tileRepeatOffset() const
Returns tile repeat offset in percent of the pattern display size.
void setReferencePoint(ReferencePoint referencePoint)
Sets the pattern reference point.
void setPattern(const QImage &pattern)
Sets a new pattern.
bool compareTo(const KoShapeBackground *other) const override
QTransform transform() const
Returns the transform matrix.
~KoPatternBackground() override
ReferencePoint referencePoint() const
Returns the pattern reference point identifier.
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.
void setPatternDisplaySize(const QSizeF &size)
Sets pattern display size.
void setReferencePointOffset(const QPointF &offset)
Sets the reference point offset in percent of the pattern display size.