From d95178153a0f15991b2e6e91216dbcf5c0be2af3 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 14 Feb 2012 11:57:25 +1000 Subject: Preserve aspect ratio when setting Image.sourceSize Setting both sourceSize.width and sourceSize.height results in changing the image aspect ratio. This is never what you'd want. Fit the image to the provided sourceSize, maintaining the aspect ratio. Task-number: QTBUG-21161 Change-Id: I77e9aacb8d31475d5df0aef1de52c0edbd1e2fc9 Reviewed-by: Aaron Kennedy --- src/quick/util/qdeclarativepixmapcache.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/quick/util/qdeclarativepixmapcache.cpp') diff --git a/src/quick/util/qdeclarativepixmapcache.cpp b/src/quick/util/qdeclarativepixmapcache.cpp index 3670c58662..43ce3346cb 100644 --- a/src/quick/util/qdeclarativepixmapcache.cpp +++ b/src/quick/util/qdeclarativepixmapcache.cpp @@ -323,20 +323,22 @@ static bool readImage(const QUrl& url, QIODevice *dev, QImage *image, QString *e force_scale = true; } - bool scaled = false; if (requestSize.width() > 0 || requestSize.height() > 0) { QSize s = imgio.size(); + qreal ratio = 0.0; if (requestSize.width() && (force_scale || requestSize.width() < s.width())) { - if (requestSize.height() <= 0) - s.setHeight(s.height()*requestSize.width()/s.width()); - s.setWidth(requestSize.width()); scaled = true; + ratio = qreal(requestSize.width())/s.width(); } if (requestSize.height() && (force_scale || requestSize.height() < s.height())) { - if (requestSize.width() <= 0) - s.setWidth(s.width()*requestSize.height()/s.height()); - s.setHeight(requestSize.height()); scaled = true; + qreal hr = qreal(requestSize.height())/s.height(); + if (ratio == 0.0 || hr < ratio) + ratio = hr; + } + if (ratio > 0.0) { + s.setHeight(qRound(s.height() * ratio)); + s.setWidth(qRound(s.width() * ratio)); + imgio.setScaledSize(s); } - if (scaled) { imgio.setScaledSize(s); } } if (impsize) -- cgit v1.2.3