support all tiling options of Image

This commit is contained in:
Marco Martin 2011-11-14 16:34:46 +01:00
parent a347b2ec04
commit b5f6d6ff33

View File

@ -96,36 +96,36 @@ void QPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
if (m_pixmap.isNull()) { if (m_pixmap.isNull()) {
return; return;
} }
//do without painter save, faster and the support can be compiled out painter->save();
const bool wasAntiAlias = painter->testRenderHint(QPainter::Antialiasing);
const bool wasSmoothTransform = painter->testRenderHint(QPainter::SmoothPixmapTransform);
painter->setRenderHint(QPainter::Antialiasing, m_smooth); painter->setRenderHint(QPainter::Antialiasing, m_smooth);
painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth); painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth);
QRect destRect; QRect destRect;
switch (m_fillMode) { switch (m_fillMode) {
case PreserveAspectFit: { case PreserveAspectFit: {
QRect minimumRect = m_pixmap.rect().intersected(boundingRect().toRect()); QSize scaled = m_pixmap.size();
const qreal scale = qMin(m_pixmap.width()/minimumRect.width(), m_pixmap.height()/minimumRect.height());
destRect = QRect(QPoint(0, 0), QSize(m_pixmap.width()*scale, m_pixmap.height()*scale)); scaled.scale(boundingRect().size().toSize(), Qt::KeepAspectRatio);
destRect = QRect(QPoint(0, 0), scaled);
break; break;
} }
case PreserveAspectCrop: { case PreserveAspectCrop: {
QRect minimumRect = m_pixmap.rect().intersected(boundingRect().toRect()); painter->setClipRect(boundingRect(), Qt::IntersectClip);
const qreal scale = qMax(m_pixmap.width()/minimumRect.width(), m_pixmap.height()/minimumRect.height()); QSize scaled = m_pixmap.size();
destRect = QRect(QPoint(0, 0), QSize(m_pixmap.width()*scale, m_pixmap.height()*scale)); scaled.scale(boundingRect().size().toSize(), Qt::KeepAspectRatioByExpanding);
destRect = QRect(QPoint(0, 0), scaled);
break; break;
} }
case TileVertically: { case TileVertically: {
QRect minimumRect = m_pixmap.rect().intersected(boundingRect().toRect()); painter->scale(width()/(qreal)m_pixmap.width(), 1);
const qreal scale = m_pixmap.width()/minimumRect.width(); destRect = boundingRect().toRect();
destRect = QRect(QPoint(0, 0), QSize(m_pixmap.width()*scale, m_pixmap.height())); destRect.setWidth(destRect.width() / (width()/(qreal)m_pixmap.width()));
break; break;
} }
case TileHorizontally: { case TileHorizontally: {
QRect minimumRect = m_pixmap.rect().intersected(boundingRect().toRect()); painter->scale(1, height()/(qreal)m_pixmap.height());
const qreal scale = m_pixmap.height()/minimumRect.height(); destRect = boundingRect().toRect();
destRect = QRect(QPoint(0, 0), QSize(m_pixmap.width(), m_pixmap.height()*scale)); destRect.setHeight(destRect.height() / (height()/(qreal)m_pixmap.height()));
break; break;
} }
case Stretch: case Stretch:
@ -140,8 +140,7 @@ void QPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
painter->drawPixmap(destRect, m_pixmap, m_pixmap.rect()); painter->drawPixmap(destRect, m_pixmap, m_pixmap.rect());
} }
painter->setRenderHint(QPainter::Antialiasing, wasAntiAlias); painter->restore();
painter->setRenderHint(QPainter::SmoothPixmapTransform, wasSmoothTransform);
} }