diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-07-17 06:55:44 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-07-17 06:55:44 (GMT) |
commit | 76002f882a260082ca17b6f82130eb38db89adff (patch) | |
tree | 15cbc0f3f3bb2567e993508ff678eb8560740482 /src/declarative/fx/qfximage.cpp | |
parent | 4937b60008fcb0923f30f976556f427495695a91 (diff) | |
download | Qt-76002f882a260082ca17b6f82130eb38db89adff.zip Qt-76002f882a260082ca17b6f82130eb38db89adff.tar.gz Qt-76002f882a260082ca17b6f82130eb38db89adff.tar.bz2 |
Add a preserveAspect property to Image
Diffstat (limited to 'src/declarative/fx/qfximage.cpp')
-rw-r--r-- | src/declarative/fx/qfximage.cpp | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/declarative/fx/qfximage.cpp b/src/declarative/fx/qfximage.cpp index 148c269..34ff157 100644 --- a/src/declarative/fx/qfximage.cpp +++ b/src/declarative/fx/qfximage.cpp @@ -339,9 +339,22 @@ void QFxImage::paintContents(QPainter &p) p.restore(); } else if (!d->scaleGrid || d->scaleGrid->isNull()) { if (width() != pix.width() || height() != pix.height()) { + qreal widthScale = width() / qreal(pix.width()); + qreal heightScale = height() / qreal(pix.height()); + QTransform scale; - scale.scale(width() / qreal(pix.width()), - height() / qreal(pix.height())); + + if (d->preserveAspect) { + if (widthScale < heightScale) { + heightScale = widthScale; + scale.translate(0, (height() - heightScale * pix.height()) / 2); + } else if(heightScale < widthScale) { + widthScale = heightScale; + scale.translate((width() - widthScale * pix.width()) / 2, 0); + } + } + + scale.scale(widthScale, heightScale); QTransform old = p.transform(); p.setWorldTransform(scale * old); p.drawPixmap(0, 0, pix); @@ -486,6 +499,22 @@ QUrl QFxImage::source() const return d->url; } +bool QFxImage::preserveAspect() const +{ + Q_D(const QFxImage); + return d->preserveAspect; +} + +void QFxImage::setPreserveAspect(bool p) +{ + Q_D(QFxImage); + + if (p == d->preserveAspect) + return; + d->preserveAspect = p; + update(); +} + void QFxImage::setSource(const QUrl &url) { #ifdef Q_ENABLE_PERFORMANCE_LOG |