From 6ae0b924102384d088c9a7f9b43170cf0d2668bd Mon Sep 17 00:00:00 2001 From: Joona Petrell Date: Wed, 14 Apr 2010 17:15:22 +1000 Subject: Image with PreserveAspect enabled with either width or height defined should implicitly update the undefined axis to follow the aspect ratio Task-number: Reviewed-by: Martin Jones --- .../graphicsitems/qdeclarativeimage.cpp | 6 ++++++ .../qdeclarativeimage/data/aspectratio.qml | 6 ++++++ .../qdeclarativeimage/tst_qdeclarativeimage.cpp | 25 +++++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/auto/declarative/qdeclarativeimage/data/aspectratio.qml diff --git a/src/declarative/graphicsitems/qdeclarativeimage.cpp b/src/declarative/graphicsitems/qdeclarativeimage.cpp index e4f3464..ca86637 100644 --- a/src/declarative/graphicsitems/qdeclarativeimage.cpp +++ b/src/declarative/graphicsitems/qdeclarativeimage.cpp @@ -311,6 +311,12 @@ void QDeclarativeImage::updatePaintedGeometry() d->paintedWidth = heightScale * qreal(d->pix.width()); d->paintedHeight = height(); } + if (widthValid() && !heightValid()) { + setImplicitHeight(d->paintedHeight); + } + if (heightValid() && !widthValid()) { + setImplicitWidth(d->paintedWidth); + } } else { d->paintedWidth = width(); d->paintedHeight = height(); diff --git a/tests/auto/declarative/qdeclarativeimage/data/aspectratio.qml b/tests/auto/declarative/qdeclarativeimage/data/aspectratio.qml new file mode 100644 index 0000000..402d33e --- /dev/null +++ b/tests/auto/declarative/qdeclarativeimage/data/aspectratio.qml @@ -0,0 +1,6 @@ +import Qt 4.7 + +Image { + source: "heart.png" + fillMode: Image.PreserveAspectFit; +} diff --git a/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp b/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp index 52d7c16..53c208e 100644 --- a/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp +++ b/tests/auto/declarative/qdeclarativeimage/tst_qdeclarativeimage.cpp @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -80,6 +81,7 @@ private slots: void imageSource_data(); void clearSource(); void resized(); + void preserveAspectRatio(); void smooth(); void pixmap(); void svg(); @@ -209,10 +211,31 @@ void tst_qdeclarativeimage::resized() QCOMPARE(obj->width(), 300.); QCOMPARE(obj->height(), 300.); QCOMPARE(obj->fillMode(), QDeclarativeImage::Stretch); - delete obj; } + +void tst_qdeclarativeimage::preserveAspectRatio() +{ + QDeclarativeView *canvas = new QDeclarativeView(0); + canvas->show(); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/aspectratio.qml")); + QDeclarativeImage *image = qobject_cast(canvas->rootObject()); + QVERIFY(image != 0); + image->setWidth(80.0); + QCOMPARE(image->width(), 80.); + QCOMPARE(image->height(), 80.); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/aspectratio.qml")); + image = qobject_cast(canvas->rootObject()); + image->setHeight(60.0); + QVERIFY(image != 0); + QCOMPARE(image->height(), 60.); + QCOMPARE(image->width(), 60.); + delete canvas; +} + void tst_qdeclarativeimage::smooth() { QString componentStr = "import Qt 4.7\nImage { source: \"" SRCDIR "/data/colors.png\"; smooth: true; width: 300; height: 300 }"; -- cgit v0.12