8#include "qpixmapitem.h"
11#include <QQuickWindow>
15 , m_fillMode(QPixmapItem::Stretch)
17 setFlag(ItemHasContents,
true);
20QPixmapItem::~QPixmapItem()
24void QPixmapItem::setPixmap(
const QPixmap &pixmap)
26 bool oldPixmapNull = m_pixmap.isNull();
30 Q_EMIT nativeWidthChanged();
31 Q_EMIT nativeHeightChanged();
33 if (oldPixmapNull != m_pixmap.isNull()) {
38QPixmap QPixmapItem::pixmap()
const
43void QPixmapItem::resetPixmap()
48int QPixmapItem::nativeWidth()
const
50 return m_pixmap.size().width() / m_pixmap.devicePixelRatio();
53int QPixmapItem::nativeHeight()
const
55 return m_pixmap.size().height() / m_pixmap.devicePixelRatio();
58QPixmapItem::FillMode QPixmapItem::fillMode()
const
63void QPixmapItem::setFillMode(QPixmapItem::FillMode mode)
65 if (mode == m_fillMode) {
75void QPixmapItem::paint(
QPainter *painter)
77 if (m_pixmap.isNull()) {
84 if (m_fillMode == TileVertically) {
85 painter->
scale(
width() / (qreal)m_pixmap.width(), 1);
88 if (m_fillMode == TileHorizontally) {
89 painter->
scale(1,
height() / (qreal)m_pixmap.height());
92 if (m_fillMode >= Tile) {
95 painter->
drawPixmap(m_paintedRect, m_pixmap, m_pixmap.rect());
101bool QPixmapItem::isNull()
const
103 return m_pixmap.isNull();
106int QPixmapItem::paintedWidth()
const
108 if (m_pixmap.isNull()) {
112 return m_paintedRect.width();
115int QPixmapItem::paintedHeight()
const
117 if (m_pixmap.isNull()) {
121 return m_paintedRect.height();
124void QPixmapItem::updatePaintedRect()
126 if (m_pixmap.isNull()) {
130 QRectF sourceRect = m_paintedRect;
135 auto posAdjusted = [
this](
const QPointF point) {
140 const auto effectiveDpr = w->effectiveDevicePixelRatio();
141 QPointF globalPixelPos =
mapToScene(point) * effectiveDpr;
142 QPointF posAdjust = QPointF(globalPixelPos.
x() - std::round(globalPixelPos.
x()), globalPixelPos.
y() - std::round(globalPixelPos.
y())) / effectiveDpr;
143 return (point - posAdjust);
146 switch (m_fillMode) {
147 case PreserveAspectFit: {
148 QSizeF scaled = m_pixmap.size();
150 destRect = QRectF(QPoint(0, 0), scaled);
154 case PreserveAspectCrop: {
155 QSizeF scaled = m_pixmap.size();
157 destRect = QRectF(QPoint(0, 0), scaled);
161 case TileVertically: {
162 destRect = bounds.
toRect();
166 case TileHorizontally: {
167 destRect = bounds.
toRect();
174 destRect = bounds.
toRect();
177 if (destRect != sourceRect) {
178 m_paintedRect = destRect.
toRect();
179 Q_EMIT paintedHeightChanged();
180 Q_EMIT paintedWidthChanged();
184void QPixmapItem::geometryChange(
const QRectF &newGeometry,
const QRectF &oldGeometry)
190#include "moc_qpixmapitem.cpp"
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap, const QPoint &position)
void scale(qreal sx, qreal sy)
void setRenderHint(RenderHint hint, bool on)
virtual QRectF boundingRect() const const
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
QPointF mapToScene(const QPointF &point) const const
QSizeF size() const const
QQuickWindow * window() const const
QPointF center() const const
qreal height() const const
void moveCenter(const QPointF &position)
void setHeight(qreal height)
void setWidth(qreal width)
QRect toRect() const const
qreal width() const const
void scale(const QSizeF &size, Qt::AspectRatioMode mode)