From e0c8fc977738ca4ac6d31e45bdd2aa1b32828f54 Mon Sep 17 00:00:00 2001 From: mae Date: Mon, 3 May 2010 19:44:52 +0200 Subject: Remove QDeclarativeLoader::resizeMode The explicit resizeMode is superfluous, all usecases could be achieved with the corrected default behaviour of the former ResizeLoaderToItem mode. The NoResize usecase is covered by wrapping the loader in an extra item. That means: A loader automatically gets its size from the item loaded unless the loader has an explicit size itself. Go-ahead-by: Michael Brasser --- examples/declarative/parallax/parallax.qml | 2 +- .../graphicsitems/qdeclarativeloader.cpp | 118 +++++---------------- .../graphicsitems/qdeclarativeloader_p.h | 7 -- .../graphicsitems/qdeclarativeloader_p_p.h | 3 +- .../qdeclarativeloader/data/NoResize.qml | 7 +- .../data/NoResizeGraphicsWidget.qml | 9 +- .../data/SizeGraphicsWidgetToLoader.qml | 1 - .../data/SizeLoaderToGraphicsWidget.qml | 1 - .../qdeclarativeloader/data/SizeToItem.qml | 1 - .../qdeclarativeloader/data/SizeToLoader.qml | 1 - .../qdeclarativeloader/tst_qdeclarativeloader.cpp | 67 ++++++------ 11 files changed, 68 insertions(+), 149 deletions(-) diff --git a/examples/declarative/parallax/parallax.qml b/examples/declarative/parallax/parallax.qml index cb0437d..ca00176 100644 --- a/examples/declarative/parallax/parallax.qml +++ b/examples/declarative/parallax/parallax.qml @@ -31,7 +31,7 @@ Rectangle { Loader { anchors { top: parent.top; topMargin: 10; horizontalCenter: parent.horizontalCenter } width: 300; height: 400 - clip: true; resizeMode: Loader.SizeItemToLoader + clip: true; source: "../../../demos/declarative/samegame/samegame.qml" } } diff --git a/src/declarative/graphicsitems/qdeclarativeloader.cpp b/src/declarative/graphicsitems/qdeclarativeloader.cpp index bdd2c87..62fa4db 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader.cpp +++ b/src/declarative/graphicsitems/qdeclarativeloader.cpp @@ -49,7 +49,6 @@ QT_BEGIN_NAMESPACE QDeclarativeLoaderPrivate::QDeclarativeLoaderPrivate() : item(0), component(0), ownComponent(false) - , resizeMode(QDeclarativeLoader::SizeLoaderToItem) { } @@ -59,9 +58,8 @@ QDeclarativeLoaderPrivate::~QDeclarativeLoaderPrivate() void QDeclarativeLoaderPrivate::itemGeometryChanged(QDeclarativeItem *resizeItem, const QRectF &newGeometry, const QRectF &oldGeometry) { - if (resizeItem == item && resizeMode == QDeclarativeLoader::SizeLoaderToItem) { - _q_updateSize(); - } + if (resizeItem == item) + _q_updateSize(false); QDeclarativeItemChangeListener::itemGeometryChanged(resizeItem, newGeometry, oldGeometry); } @@ -76,11 +74,9 @@ void QDeclarativeLoaderPrivate::clear() if (item) { if (QDeclarativeItem *qmlItem = qobject_cast(item)) { - if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) { - QDeclarativeItemPrivate *p = + QDeclarativeItemPrivate *p = static_cast(QGraphicsItemPrivate::get(qmlItem)); - p->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry); - } + p->removeItemChangeListener(this, QDeclarativeItemPrivate::Geometry); } // We can't delete immediately because our item may have triggered @@ -96,16 +92,12 @@ void QDeclarativeLoaderPrivate::initResize() { Q_Q(QDeclarativeLoader); if (QDeclarativeItem *qmlItem = qobject_cast(item)) { - if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) { - QDeclarativeItemPrivate *p = + QDeclarativeItemPrivate *p = static_cast(QGraphicsItemPrivate::get(qmlItem)); - p->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry); - } + p->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry); } else if (item && item->isWidget()) { QGraphicsWidget *widget = static_cast(item); - if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) { - widget->installEventFilter(q); - } + widget->installEventFilter(q); } _q_updateSize(); } @@ -390,83 +382,30 @@ qreal QDeclarativeLoader::progress() const return 0.0; } -/*! - \qmlproperty enumeration Loader::resizeMode - - This property determines how the Loader or item are resized: - \list - \o NoResize - no item will be resized - \o SizeLoaderToItem - the Loader will be sized to the size of the item, unless the size of the Loader has been otherwise specified. - \o SizeItemToLoader - the item will be sized to the size of the Loader. - \endlist - - Note that changing from SizeItemToLoader to SizeLoaderToItem - after the component is loaded will not return the item or Loader - to it's original size. This is due to the item size being adjusted - to the Loader size, thereby losing the original size of the item. - Future changes to the item's size will affect the loader, however. - - The default resizeMode is SizeLoaderToItem. -*/ -QDeclarativeLoader::ResizeMode QDeclarativeLoader::resizeMode() const -{ - Q_D(const QDeclarativeLoader); - return d->resizeMode; -} - -void QDeclarativeLoader::setResizeMode(ResizeMode mode) -{ - Q_D(QDeclarativeLoader); - if (mode == d->resizeMode) - return; - - if (QDeclarativeItem *qmlItem = qobject_cast(d->item)) { - if (d->resizeMode == SizeLoaderToItem) { - QDeclarativeItemPrivate *p = - static_cast(QGraphicsItemPrivate::get(qmlItem)); - p->removeItemChangeListener(d, QDeclarativeItemPrivate::Geometry); - } - } else if (d->item && d->item->isWidget()) { - if (d->resizeMode == SizeLoaderToItem) - d->item->removeEventFilter(this); - } - d->resizeMode = mode; - emit resizeModeChanged(); - d->initResize(); -} - -void QDeclarativeLoaderPrivate::_q_updateSize() +void QDeclarativeLoaderPrivate::_q_updateSize(bool loaderGeometryChanged) { Q_Q(QDeclarativeLoader); if (!item) return; if (QDeclarativeItem *qmlItem = qobject_cast(item)) { - if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) { - q->setWidth(qmlItem->width()); - q->setHeight(qmlItem->height()); - } else if (resizeMode == QDeclarativeLoader::SizeItemToLoader) { + q->setImplicitWidth(qmlItem->width()); + if (loaderGeometryChanged && q->widthValid()) qmlItem->setWidth(q->width()); + q->setImplicitHeight(qmlItem->height()); + if (loaderGeometryChanged && q->heightValid()) qmlItem->setHeight(q->height()); - } } else if (item && item->isWidget()) { QGraphicsWidget *widget = static_cast(item); - if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) { - QSizeF newSize = widget->size(); - if (newSize.isValid()) { - q->setWidth(newSize.width()); - q->setHeight(newSize.height()); - } - } else if (resizeMode == QDeclarativeLoader::SizeItemToLoader) { - QSizeF oldSize = widget->size(); - QSizeF newSize = oldSize; - if (q->heightValid()) - newSize.setHeight(q->height()); - if (q->widthValid()) - newSize.setWidth(q->width()); - if (oldSize != newSize) - widget->resize(newSize); - } + QSizeF widgetSize = widget->size(); + q->setImplicitWidth(widgetSize.width()); + if (loaderGeometryChanged && q->widthValid()) + widgetSize.setWidth(q->width()); + q->setImplicitHeight(widgetSize.height()); + if (loaderGeometryChanged && q->heightValid()) + widgetSize.setHeight(q->height()); + if (widget->size() != widgetSize) + widget->resize(widgetSize); } } @@ -484,9 +423,7 @@ void QDeclarativeLoader::geometryChanged(const QRectF &newGeometry, const QRectF { Q_D(QDeclarativeLoader); if (newGeometry != oldGeometry) { - if (d->resizeMode == SizeItemToLoader) { - d->_q_updateSize(); - } + d->_q_updateSize(); } QDeclarativeItem::geometryChanged(newGeometry, oldGeometry); } @@ -496,10 +433,8 @@ QVariant QDeclarativeLoader::itemChange(GraphicsItemChange change, const QVarian Q_D(QDeclarativeLoader); if (change == ItemSceneHasChanged) { if (d->item && d->item->isWidget()) { - if (d->resizeMode == SizeLoaderToItem) { - d->item->removeEventFilter(this); - d->item->installEventFilter(this); - } + d->item->removeEventFilter(this); + d->item->installEventFilter(this); } } return QDeclarativeItem::itemChange(change, value); @@ -509,9 +444,8 @@ bool QDeclarativeLoader::eventFilter(QObject *watched, QEvent *e) { Q_D(QDeclarativeLoader); if (watched == d->item && e->type() == QEvent::GraphicsSceneResize) { - if (d->item && d->item->isWidget() && d->resizeMode == SizeLoaderToItem) { - d->_q_updateSize(); - } + if (d->item && d->item->isWidget()) + d->_q_updateSize(false); } return QDeclarativeItem::eventFilter(watched, e); } diff --git a/src/declarative/graphicsitems/qdeclarativeloader_p.h b/src/declarative/graphicsitems/qdeclarativeloader_p.h index e9fd8e9..49dfa11 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader_p.h +++ b/src/declarative/graphicsitems/qdeclarativeloader_p.h @@ -55,11 +55,9 @@ class Q_DECLARATIVE_EXPORT QDeclarativeLoader : public QDeclarativeItem { Q_OBJECT Q_ENUMS(Status) - Q_ENUMS(ResizeMode) Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) Q_PROPERTY(QDeclarativeComponent *sourceComponent READ sourceComponent WRITE setSourceComponent RESET resetSourceComponent NOTIFY sourceChanged) - Q_PROPERTY(ResizeMode resizeMode READ resizeMode WRITE setResizeMode NOTIFY resizeModeChanged) Q_PROPERTY(QGraphicsObject *item READ item NOTIFY itemChanged) Q_PROPERTY(Status status READ status NOTIFY statusChanged) Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) @@ -79,10 +77,6 @@ public: Status status() const; qreal progress() const; - enum ResizeMode { NoResize, SizeLoaderToItem, SizeItemToLoader }; - ResizeMode resizeMode() const; - void setResizeMode(ResizeMode mode); - QGraphicsObject *item() const; Q_SIGNALS: @@ -90,7 +84,6 @@ Q_SIGNALS: void sourceChanged(); void statusChanged(); void progressChanged(); - void resizeModeChanged(); protected: void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); diff --git a/src/declarative/graphicsitems/qdeclarativeloader_p_p.h b/src/declarative/graphicsitems/qdeclarativeloader_p_p.h index 49069f9..0d4c4d0 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativeloader_p_p.h @@ -77,10 +77,9 @@ public: QGraphicsObject *item; QDeclarativeComponent *component; bool ownComponent : 1; - QDeclarativeLoader::ResizeMode resizeMode; void _q_sourceLoaded(); - void _q_updateSize(); + void _q_updateSize(bool loaderGeometryChanged = true); }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/qdeclarativeloader/data/NoResize.qml b/tests/auto/declarative/qdeclarativeloader/data/NoResize.qml index 6aa3d2f..72cd3b9 100644 --- a/tests/auto/declarative/qdeclarativeloader/data/NoResize.qml +++ b/tests/auto/declarative/qdeclarativeloader/data/NoResize.qml @@ -1,7 +1,8 @@ import Qt 4.7 -Loader { - resizeMode: "NoResize" +Item { width: 200; height: 80 - source: "Rect120x60.qml" + Loader { + source: "Rect120x60.qml" + } } diff --git a/tests/auto/declarative/qdeclarativeloader/data/NoResizeGraphicsWidget.qml b/tests/auto/declarative/qdeclarativeloader/data/NoResizeGraphicsWidget.qml index 9322141..0cff506 100644 --- a/tests/auto/declarative/qdeclarativeloader/data/NoResizeGraphicsWidget.qml +++ b/tests/auto/declarative/qdeclarativeloader/data/NoResizeGraphicsWidget.qml @@ -1,8 +1,9 @@ import Qt 4.7 -Loader { - resizeMode: Loader.NoResize - source: "GraphicsWidget250x250.qml" +Item { width: 200 - height: 80 + height: 80 + Loader { + source: "GraphicsWidget250x250.qml" + } } diff --git a/tests/auto/declarative/qdeclarativeloader/data/SizeGraphicsWidgetToLoader.qml b/tests/auto/declarative/qdeclarativeloader/data/SizeGraphicsWidgetToLoader.qml index 0cfb4df..81610ad 100644 --- a/tests/auto/declarative/qdeclarativeloader/data/SizeGraphicsWidgetToLoader.qml +++ b/tests/auto/declarative/qdeclarativeloader/data/SizeGraphicsWidgetToLoader.qml @@ -1,7 +1,6 @@ import Qt 4.7 Loader { - resizeMode: Loader.SizeItemToLoader width: 200 height: 80 source: "GraphicsWidget250x250.qml" diff --git a/tests/auto/declarative/qdeclarativeloader/data/SizeLoaderToGraphicsWidget.qml b/tests/auto/declarative/qdeclarativeloader/data/SizeLoaderToGraphicsWidget.qml index b588c9d..a801a42 100644 --- a/tests/auto/declarative/qdeclarativeloader/data/SizeLoaderToGraphicsWidget.qml +++ b/tests/auto/declarative/qdeclarativeloader/data/SizeLoaderToGraphicsWidget.qml @@ -1,6 +1,5 @@ import Qt 4.7 Loader { - resizeMode: Loader.SizeLoaderToItem source: "GraphicsWidget250x250.qml" } diff --git a/tests/auto/declarative/qdeclarativeloader/data/SizeToItem.qml b/tests/auto/declarative/qdeclarativeloader/data/SizeToItem.qml index 93be6f1..77aa8d9 100644 --- a/tests/auto/declarative/qdeclarativeloader/data/SizeToItem.qml +++ b/tests/auto/declarative/qdeclarativeloader/data/SizeToItem.qml @@ -1,6 +1,5 @@ import Qt 4.7 Loader { - resizeMode: "SizeLoaderToItem" source: "Rect120x60.qml" } diff --git a/tests/auto/declarative/qdeclarativeloader/data/SizeToLoader.qml b/tests/auto/declarative/qdeclarativeloader/data/SizeToLoader.qml index 04b46fb..0098927 100644 --- a/tests/auto/declarative/qdeclarativeloader/data/SizeToLoader.qml +++ b/tests/auto/declarative/qdeclarativeloader/data/SizeToLoader.qml @@ -1,7 +1,6 @@ import Qt 4.7 Loader { - resizeMode: "SizeItemToLoader" width: 200; height: 80 source: "Rect120x60.qml" } diff --git a/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp b/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp index 7cdadb4..b56ff13 100644 --- a/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp +++ b/tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp @@ -270,7 +270,6 @@ void tst_QDeclarativeLoader::sizeLoaderToItem() QDeclarativeComponent component(&engine, TEST_FILE("/SizeToItem.qml")); QDeclarativeLoader *loader = qobject_cast(component.create()); QVERIFY(loader != 0); - QVERIFY(loader->resizeMode() == QDeclarativeLoader::SizeLoaderToItem); QCOMPARE(loader->width(), 120.0); QCOMPARE(loader->height(), 60.0); @@ -282,20 +281,28 @@ void tst_QDeclarativeLoader::sizeLoaderToItem() QCOMPARE(loader->width(), 150.0); QCOMPARE(loader->height(), 45.0); + // Check explicit width + loader->setWidth(200.0); + QCOMPARE(loader->width(), 200.0); + QCOMPARE(rect->width(), 200.0); + rect->setWidth(100.0); // when rect changes ... + QCOMPARE(rect->width(), 100.0); // ... it changes + QCOMPARE(loader->width(), 200.0); // ... but loader stays the same + + // Check explicit height + loader->setHeight(200.0); + QCOMPARE(loader->height(), 200.0); + QCOMPARE(rect->height(), 200.0); + rect->setHeight(100.0); // when rect changes ... + QCOMPARE(rect->height(), 100.0); // ... it changes + QCOMPARE(loader->height(), 200.0); // ... but loader stays the same + // Switch mode - loader->setResizeMode(QDeclarativeLoader::SizeItemToLoader); loader->setWidth(180); loader->setHeight(30); QCOMPARE(rect->width(), 180.0); QCOMPARE(rect->height(), 30.0); - // notify - QSignalSpy spy(loader, SIGNAL(resizeModeChanged())); - loader->setResizeMode(QDeclarativeLoader::NoResize); - QCOMPARE(spy.count(),1); - loader->setResizeMode(QDeclarativeLoader::NoResize); - QCOMPARE(spy.count(),1); - delete loader; } @@ -304,7 +311,6 @@ void tst_QDeclarativeLoader::sizeItemToLoader() QDeclarativeComponent component(&engine, TEST_FILE("/SizeToLoader.qml")); QDeclarativeLoader *loader = qobject_cast(component.create()); QVERIFY(loader != 0); - QVERIFY(loader->resizeMode() == QDeclarativeLoader::SizeItemToLoader); QCOMPARE(loader->width(), 200.0); QCOMPARE(loader->height(), 80.0); @@ -320,7 +326,8 @@ void tst_QDeclarativeLoader::sizeItemToLoader() QCOMPARE(rect->height(), 30.0); // Switch mode - loader->setResizeMode(QDeclarativeLoader::SizeLoaderToItem); + loader->resetWidth(); // reset explicit size + loader->resetHeight(); rect->setWidth(160); rect->setHeight(45); QCOMPARE(loader->width(), 160.0); @@ -332,17 +339,12 @@ void tst_QDeclarativeLoader::sizeItemToLoader() void tst_QDeclarativeLoader::noResize() { QDeclarativeComponent component(&engine, TEST_FILE("/NoResize.qml")); - QDeclarativeLoader *loader = qobject_cast(component.create()); - QVERIFY(loader != 0); - QCOMPARE(loader->width(), 200.0); - QCOMPARE(loader->height(), 80.0); - - QDeclarativeItem *rect = qobject_cast(loader->item()); - QVERIFY(rect); - QCOMPARE(rect->width(), 120.0); - QCOMPARE(rect->height(), 60.0); + QDeclarativeItem* item = qobject_cast(component.create()); + QVERIFY(item != 0); + QCOMPARE(item->width(), 200.0); + QCOMPARE(item->height(), 80.0); - delete loader; + delete item; } void tst_QDeclarativeLoader::sizeLoaderToGraphicsWidget() @@ -353,7 +355,6 @@ void tst_QDeclarativeLoader::sizeLoaderToGraphicsWidget() scene.addItem(loader); QVERIFY(loader != 0); - QVERIFY(loader->resizeMode() == QDeclarativeLoader::SizeLoaderToItem); QCOMPARE(loader->width(), 250.0); QCOMPARE(loader->height(), 250.0); @@ -365,7 +366,6 @@ void tst_QDeclarativeLoader::sizeLoaderToGraphicsWidget() QCOMPARE(loader->height(), 45.0); // Switch mode - loader->setResizeMode(QDeclarativeLoader::SizeItemToLoader); loader->setWidth(180); loader->setHeight(30); QCOMPARE(widget->size().width(), 180.0); @@ -382,7 +382,6 @@ void tst_QDeclarativeLoader::sizeGraphicsWidgetToLoader() scene.addItem(loader); QVERIFY(loader != 0); - QVERIFY(loader->resizeMode() == QDeclarativeLoader::SizeItemToLoader); QCOMPARE(loader->width(), 200.0); QCOMPARE(loader->height(), 80.0); @@ -398,7 +397,8 @@ void tst_QDeclarativeLoader::sizeGraphicsWidgetToLoader() QCOMPARE(widget->size().height(), 30.0); // Switch mode - loader->setResizeMode(QDeclarativeLoader::SizeLoaderToItem); + loader->resetWidth(); // reset explicit size + loader->resetHeight(); widget->resize(QSizeF(160,45)); QCOMPARE(loader->width(), 160.0); QCOMPARE(loader->height(), 45.0); @@ -409,20 +409,15 @@ void tst_QDeclarativeLoader::sizeGraphicsWidgetToLoader() void tst_QDeclarativeLoader::noResizeGraphicsWidget() { QDeclarativeComponent component(&engine, TEST_FILE("/NoResizeGraphicsWidget.qml")); - QDeclarativeLoader *loader = qobject_cast(component.create()); + QDeclarativeItem *item = qobject_cast(component.create()); QGraphicsScene scene; - scene.addItem(loader); - - QVERIFY(loader != 0); - QCOMPARE(loader->width(), 200.0); - QCOMPARE(loader->height(), 80.0); + scene.addItem(item); - QGraphicsWidget *widget = qobject_cast(loader->item()); - QVERIFY(widget); - QCOMPARE(widget->size().width(), 250.0); - QCOMPARE(widget->size().height(), 250.0); + QVERIFY(item != 0); + QCOMPARE(item->width(), 200.0); + QCOMPARE(item->height(), 80.0); - delete loader; + delete item; } void tst_QDeclarativeLoader::networkRequestUrl() -- cgit v0.12