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/items/qquickimage.cpp | 6 +++++- src/quick/util/qdeclarativepixmapcache.cpp | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/quick/items/qquickimage.cpp b/src/quick/items/qquickimage.cpp index 9af3a7e3ba..a71c666977 100644 --- a/src/quick/items/qquickimage.cpp +++ b/src/quick/items/qquickimage.cpp @@ -404,6 +404,10 @@ qreal QQuickImage::paintedHeight() const other dimension is set in proportion to preserve the source image's aspect ratio. (The \l fillMode is independent of this.) + If both the sourceSize.width and sourceSize.height are set the image will be scaled + down to fit within the specified size, maintaining the image's aspect ratio. The actual + size of the image after scaling is available via \l implicitWidth and \l implicitHeight. + If the source is an intrinsically scalable image (eg. SVG), this property determines the size of the loaded image regardless of intrinsic size. Avoid changing this property dynamically; rendering an SVG is \e slow compared @@ -413,7 +417,7 @@ qreal QQuickImage::paintedHeight() const be no greater than this property specifies. For some formats (currently only JPEG), the whole image will never actually be loaded into memory. - Since QtQuick 1.1 the sourceSize can be cleared to the natural size of the image + sourceSize can be cleared to the natural size of the image by setting sourceSize to \c undefined. \note \e {Changing this property dynamically causes the image source to be reloaded, 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