From 6878a0b7c507d21366cacee0a09bafeb81726d82 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 9 Sep 2009 15:14:21 +1000 Subject: Move ComponentInstance functionality into Loader --- demos/declarative/minehunt/minehunt.qml | 4 +- doc/src/declarative/elements.qdoc | 1 - doc/src/tutorials/declarative.qdoc | 2 +- .../tutorials/contacts/2_Reuse/1b/BlueRect.qml | 12 +- examples/declarative/webview/newwindows.qml | 2 +- src/declarative/QmlChanges.txt | 3 + src/declarative/fx/fx.pri | 3 - src/declarative/fx/qfxcomponentinstance.cpp | 149 ------------ src/declarative/fx/qfxcomponentinstance.h | 93 -------- src/declarative/fx/qfxcomponentinstance_p.h | 76 ------ src/declarative/fx/qfxloader.cpp | 258 ++++++++++++++++----- src/declarative/fx/qfxloader.h | 13 +- src/declarative/fx/qfxloader_p.h | 6 +- src/declarative/qml/qmlcomponent.cpp | 5 +- src/declarative/util/qfxperf.cpp | 1 - src/declarative/util/qfxperf_p.h | 1 - tests/auto/declarative/qfxloader/NoResize.qml | 7 + tests/auto/declarative/qfxloader/Rect120x60.qml | 6 + .../declarative/qfxloader/SetSourceComponent.qml | 6 + tests/auto/declarative/qfxloader/SizeToItem.qml | 6 + tests/auto/declarative/qfxloader/SizeToLoader.qml | 7 + tests/auto/declarative/qfxloader/qfxloader.pro | 5 + tests/auto/declarative/qfxloader/tst_qfxloader.cpp | 103 ++++++++ 23 files changed, 373 insertions(+), 396 deletions(-) delete mode 100644 src/declarative/fx/qfxcomponentinstance.cpp delete mode 100644 src/declarative/fx/qfxcomponentinstance.h delete mode 100644 src/declarative/fx/qfxcomponentinstance_p.h create mode 100644 tests/auto/declarative/qfxloader/NoResize.qml create mode 100644 tests/auto/declarative/qfxloader/Rect120x60.qml create mode 100644 tests/auto/declarative/qfxloader/SetSourceComponent.qml create mode 100644 tests/auto/declarative/qfxloader/SizeToItem.qml create mode 100644 tests/auto/declarative/qfxloader/SizeToLoader.qml create mode 100644 tests/auto/declarative/qfxloader/qfxloader.pro create mode 100644 tests/auto/declarative/qfxloader/tst_qfxloader.cpp diff --git a/demos/declarative/minehunt/minehunt.qml b/demos/declarative/minehunt/minehunt.qml index fa169aa..72431af 100644 --- a/demos/declarative/minehunt/minehunt.qml +++ b/demos/declarative/minehunt/minehunt.qml @@ -135,8 +135,8 @@ Item { x: 1 y: 1 Component { - ComponentInstance { - component: tile + Loader { + sourceComponent: tile x: (index - (Math.floor(index/9) * 9)) * 41 y: Math.floor(index/9) * 41 } diff --git a/doc/src/declarative/elements.qdoc b/doc/src/declarative/elements.qdoc index bfaf4ab..4eda947 100644 --- a/doc/src/declarative/elements.qdoc +++ b/doc/src/declarative/elements.qdoc @@ -100,7 +100,6 @@ The following table lists the QML elements provided by the Qt Declarative module \o \l Loader \o \l Repeater \o \l SystemPalette -\o \l ComponentInstance \o \l GraphicsObjectContainer \endlist diff --git a/doc/src/tutorials/declarative.qdoc b/doc/src/tutorials/declarative.qdoc index 7780988..48beabd 100644 --- a/doc/src/tutorials/declarative.qdoc +++ b/doc/src/tutorials/declarative.qdoc @@ -392,7 +392,7 @@ the contact field. There are also two other ways to reuse components in QML. A component - can be reused from within the same QML file using Component and ComponentInstance + can be reused from within the same QML file using Component and Loader elements. The next code snippet produces three red rounded rectangles within a large blue rectangle. diff --git a/examples/declarative/tutorials/contacts/2_Reuse/1b/BlueRect.qml b/examples/declarative/tutorials/contacts/2_Reuse/1b/BlueRect.qml index ec115fe..7bcdf50 100644 --- a/examples/declarative/tutorials/contacts/2_Reuse/1b/BlueRect.qml +++ b/examples/declarative/tutorials/contacts/2_Reuse/1b/BlueRect.qml @@ -16,18 +16,18 @@ Rectangle { } } ] - ComponentInstance { - component: redRectangle + Loader { + sourceComponent: redRectangle anchors.right: parent.right anchors.top: parent.top } - ComponentInstance { - component: redRectangle + Loader { + sourceComponent: redRectangle anchors.left: parent.left anchors.top: parent.top } - ComponentInstance { - component: redRectangle + Loader { + sourceComponent: redRectangle anchors.left: parent.left anchors.bottom: parent.bottom } diff --git a/examples/declarative/webview/newwindows.qml b/examples/declarative/webview/newwindows.qml index 9ff902e2..57cbf4e 100644 --- a/examples/declarative/webview/newwindows.qml +++ b/examples/declarative/webview/newwindows.qml @@ -24,5 +24,5 @@ Row { } ] width: 500 - ComponentInstance { component: WebViewPage } + Loader { sourceComponent: WebViewPage } } diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index 9bf4b10..42a8415 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -67,6 +67,8 @@ MouseRegion: add "pressedButtons" property Timer: add start() and stop() slots WebView: add newWindowComponent and newWindowParent properties Loader: add status() and progress() properties +Loader: add sourceComponent property +Loader: add resizeMode property Deletions: Column/VerticalPositioner: lost "margins" property @@ -74,6 +76,7 @@ Row/HorizontalPositioner: lost "margins" property Grid/Positioner/Layout: lost "margins" property WebView: lost "interactive" property (always true now) Flickable: removed "dragMode" property +ComponentInstance: removed. Replaced by Loader.sourceComponent Other Changes: Drag: axis becomes an enum with values "XAxis", "YAxis", "XandYAxis" diff --git a/src/declarative/fx/fx.pri b/src/declarative/fx/fx.pri index 80a6fdd..cfe78e1 100644 --- a/src/declarative/fx/fx.pri +++ b/src/declarative/fx/fx.pri @@ -1,8 +1,6 @@ HEADERS += \ fx/qfxanchors.h \ fx/qfxanchors_p.h \ - fx/qfxcomponentinstance.h \ - fx/qfxcomponentinstance_p.h \ fx/qfxevents_p.h \ fx/qfxflickable.h \ fx/qfxflickable_p.h \ @@ -49,7 +47,6 @@ HEADERS += \ SOURCES += \ fx/qfxanchors.cpp \ - fx/qfxcomponentinstance.cpp \ fx/qfxevents.cpp \ fx/qfxflickable.cpp \ fx/qfxflipable.cpp \ diff --git a/src/declarative/fx/qfxcomponentinstance.cpp b/src/declarative/fx/qfxcomponentinstance.cpp deleted file mode 100644 index 7a712aa..0000000 --- a/src/declarative/fx/qfxcomponentinstance.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qfxcomponentinstance.h" -#include "qfxcomponentinstance_p.h" -#include -#include - - -QT_BEGIN_NAMESPACE -QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,ComponentInstance,QFxComponentInstance) - -/*! - \internal - \class QFxComponentInstance ComponentInstance - - \brief The QFxComponentInstance class provides a way to instantiate an item from a component. - */ - -/*! - \qmlclass ComponentInstance QFxComponentInstance - \brief The ComponentInstance item allows you to instantiate a \l{Component}. - - \qml - Item { - Component { - id: RedSquare - Rectangle { color: "red"; width: 10; height: 10 } - } - - ComponentInstance { component: RedSquare } - } - \endqml -*/ -QFxComponentInstance::QFxComponentInstance(QFxItem *parent) - : QFxItem(*(new QFxComponentInstancePrivate), parent) -{ -} - -QFxComponentInstance::QFxComponentInstance(QFxComponentInstancePrivate &dd, QFxItem *parent) - : QFxItem(dd, parent) -{ -} - -/*! - \qmlproperty Component QFxComponentInstance::component - - This property holds the component to instantiate. -*/ -QmlComponent *QFxComponentInstance::component() const -{ - Q_D(const QFxComponentInstance); - return d->component; -} - -void QFxComponentInstance::setComponent(QmlComponent *c) -{ - Q_D(QFxComponentInstance); - if (d->component) { - qmlInfo(this) << "component is a write-once property."; - return; - } - d->component = c; - create(); -} - -void QFxComponentInstance::create() -{ - Q_D(QFxComponentInstance); - if (d->component) { - QObject *obj= d->component->create(qmlContext(this)); - if (obj) { - QFxItem *objitem = qobject_cast(obj); - if (objitem) { - d->instance = objitem; - objitem->setParentItem(this); - objitem->setFocus(true); - connect(objitem, SIGNAL(widthChanged()), this, SLOT(updateSize())); - connect(objitem, SIGNAL(heightChanged()), this, SLOT(updateSize())); - updateSize(); - emit instanceChanged(); - } else { - delete obj; - } - } - } -} - -void QFxComponentInstance::updateSize() -{ - QFxItem *i = instance(); - if (i) { - if (!widthValid()) - setImplicitWidth(i->width()); - if (!heightValid()) - setImplicitHeight(i->height()); - } -} - -/*! - \qmlproperty Item QFxComponentInstance::instance - - This property holds the instantiated component. -*/ -QFxItem *QFxComponentInstance::instance() const -{ - Q_D(const QFxComponentInstance); - return d->instance; -} - -QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxcomponentinstance.h b/src/declarative/fx/qfxcomponentinstance.h deleted file mode 100644 index b223ca2..0000000 --- a/src/declarative/fx/qfxcomponentinstance.h +++ /dev/null @@ -1,93 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QFXCOMPONENTINSTANCE_H -#define QFXCOMPONENTINSTANCE_H - -#include - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -QT_MODULE(Declarative) - -//### remove -//### add component property to Loader - -class QFxComponentInstancePrivate; -class Q_DECLARATIVE_EXPORT QFxComponentInstance : public QFxItem -{ - Q_OBJECT - Q_PROPERTY(QmlComponent *component READ component WRITE setComponent) - Q_PROPERTY(QFxItem *instance READ instance) - Q_CLASSINFO("DefaultProperty", "component") -public: - QFxComponentInstance(QFxItem *parent=0); - - QmlComponent *component() const; - void setComponent(QmlComponent *); - - QFxItem *instance() const; - -Q_SIGNALS: - void instanceChanged(); - -private Q_SLOTS: - void updateSize(); - -private: - void create(); - -protected: - QFxComponentInstance(QFxComponentInstancePrivate &dd, QFxItem *parent); - -private: - Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QFxComponentInstance) -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QFxComponentInstance) - -QT_END_HEADER - -#endif // QFXCOMPONENTINSTANCE_H diff --git a/src/declarative/fx/qfxcomponentinstance_p.h b/src/declarative/fx/qfxcomponentinstance_p.h deleted file mode 100644 index defeb74..0000000 --- a/src/declarative/fx/qfxcomponentinstance_p.h +++ /dev/null @@ -1,76 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** Contact: Qt Software Information (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the either Technology Preview License Agreement or the -** Beta Release License Agreement. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain -** additional rights. These rights are described in the Nokia Qt LGPL -** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this -** package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3.0 as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU General Public License version 3.0 requirements will be -** met: http://www.gnu.org/copyleft/gpl.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at qt-sales@nokia.com. -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QFXCOMPONENTINSTANCE_P_H -#define QFXCOMPONENTINSTANCE_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qfxitem_p.h" - - -QT_BEGIN_NAMESPACE -class QFxComponentInstancePrivate : public QFxItemPrivate -{ - Q_DECLARE_PUBLIC(QFxComponentInstance) - -public: - QFxComponentInstancePrivate() - : component(0), instance(0) - { - } - - QmlComponent *component; - QFxItem *instance; -}; - -QT_END_NAMESPACE - -#endif // QFXCOMPONENTINSTANCE_P_H diff --git a/src/declarative/fx/qfxloader.cpp b/src/declarative/fx/qfxloader.cpp index 869a5b0..783fd61 100644 --- a/src/declarative/fx/qfxloader.cpp +++ b/src/declarative/fx/qfxloader.cpp @@ -45,7 +45,7 @@ QT_BEGIN_NAMESPACE QFxLoaderPrivate::QFxLoaderPrivate() -: item(0), qmlcomp(0) +: item(0), component(0), ownComponent(false), resizeMode(QFxLoader::SizeLoaderToItem) { } @@ -59,9 +59,22 @@ QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Loader,QFxLoader) \qmlclass Loader \inherits Item - \brief The Loader item allows you to dynamically load an Item-based - subtree from a QML URL. - */ + \brief The Loader item allows dynamically loading an Item-based + subtree from a QML URL or Component. + + Loader instantiates an item from a component. The component to + instantiate may be specified directly by the \c sourceComponent + property, or loaded from a URL via the \c source property. + + It is also an effective means of delaying the creation of a component + until it is required: + \code + Loader { id: PageLoader } + Rectangle { + MouseRegion { anchors.fill: parent; onClicked: PageLoader.source = "Page1.qml" } + } + \endcode +*/ /*! \internal @@ -86,10 +99,10 @@ QFxLoader::~QFxLoader() /*! \qmlproperty url Loader::source - This property holds the dynamic URL of the QML for the item. + This property holds the URL of the QML component to + instantiate. - This property is used for dynamically loading QML into the - item. + \sa status, progress */ QUrl QFxLoader::source() const { @@ -103,44 +116,98 @@ void QFxLoader::setSource(const QUrl &url) if (d->source == url) return; - if (!d->source.isEmpty()) { - QHash::Iterator iter = d->cachedChildren.find(d->source.toString()); - if (iter != d->cachedChildren.end()) - (*iter)->setOpacity(0.); + if (d->ownComponent) { + delete d->component; + d->component = 0; } - - d->source = url; d->item = 0; - emit itemChanged(); + delete d->item; + d->source = url; if (d->source.isEmpty()) { emit sourceChanged(); emit statusChanged(); emit progressChanged(); + emit itemChanged(); + return; + } + + d->component = new QmlComponent(qmlEngine(this), d->source, this); + d->ownComponent = true; + if (!d->component->isLoading()) { + d->_q_sourceLoaded(); + } else { + connect(d->component, SIGNAL(statusChanged(QmlComponent::Status)), + this, SLOT(_q_sourceLoaded())); + connect(d->component, SIGNAL(progressChanged(qreal)), + this, SIGNAL(progressChanged())); + emit statusChanged(); + emit progressChanged(); + emit sourceChanged(); + emit itemChanged(); + } +} + +/*! + \qmlproperty Component Loader::sourceComponent + The sourceComponent property holds the \l{Component} to instantiate. + + \qml + Item { + Component { + id: RedSquare + Rectangle { color: "red"; width: 10; height: 10 } + } + + Loader { sourceComponent: RedSquare } + Loader { sourceComponent: RedSquare; x: 10 } + } + \endqml + + \sa source +*/ + +QmlComponent *QFxLoader::sourceComponent() const +{ + Q_D(const QFxLoader); + return d->component; +} + +void QFxLoader::setSourceComponent(QmlComponent *comp) +{ + Q_D(QFxLoader); + if (comp == d->component) return; + + d->source = QUrl(); + if (d->ownComponent) { + delete d->component; + d->component = 0; } + delete d->item; + d->item = 0; - QHash::Iterator iter = d->cachedChildren.find(d->source.toString()); - if (iter != d->cachedChildren.end()) { - (*iter)->setOpacity(1.); - d->item = (*iter); + d->component = comp; + d->ownComponent = false; + if (!d->component) { emit sourceChanged(); emit statusChanged(); emit progressChanged(); emit itemChanged(); + return; + } + + if (!d->component->isLoading()) { + d->_q_sourceLoaded(); } else { - d->qmlcomp = - new QmlComponent(qmlEngine(this), d->source, this); - if (!d->qmlcomp->isLoading()) { - d->_q_sourceLoaded(); - } else { - connect(d->qmlcomp, SIGNAL(statusChanged(QmlComponent::Status)), - this, SLOT(_q_sourceLoaded())); - connect(d->qmlcomp, SIGNAL(progressChanged(qreal)), - this, SIGNAL(progressChanged())); - emit statusChanged(); - emit progressChanged(); - } + connect(d->component, SIGNAL(statusChanged(QmlComponent::Status)), + this, SLOT(_q_sourceLoaded())); + connect(d->component, SIGNAL(progressChanged(qreal)), + this, SIGNAL(progressChanged())); + emit progressChanged(); + emit sourceChanged(); + emit statusChanged(); + emit itemChanged(); } } @@ -148,33 +215,39 @@ void QFxLoaderPrivate::_q_sourceLoaded() { Q_Q(QFxLoader); - if (qmlcomp) { + if (component) { QmlContext *ctxt = new QmlContext(qmlContext(q)); ctxt->addDefaultObject(q); - if (!qmlcomp->errors().isEmpty()) { - qWarning() << qmlcomp->errors(); - delete qmlcomp; - qmlcomp = 0; + if (!component->errors().isEmpty()) { + qWarning() << component->errors(); emit q->sourceChanged(); emit q->statusChanged(); emit q->progressChanged(); return; } - QObject *obj = qmlcomp->create(ctxt); - if (!qmlcomp->errors().isEmpty()) - qWarning() << qmlcomp->errors(); - QFxItem *qmlChild = qobject_cast(obj); - if (qmlChild) { - qmlChild->setParentItem(q); - cachedChildren.insert(source.toString(), qmlChild); - item = qmlChild; + + QObject *obj = component->create(ctxt); + if (obj) { + item = qobject_cast(obj); + if (item) { + item->setParentItem(q); +// item->setFocus(true); + QFxItem *resizeItem = 0; + if (resizeMode == QFxLoader::SizeLoaderToItem) + resizeItem = item; + else if (resizeMode == QFxLoader::SizeItemToLoader) + resizeItem = q; + if (resizeItem) { + QObject::connect(resizeItem, SIGNAL(widthChanged()), q, SLOT(_q_updateSize())); + QObject::connect(resizeItem, SIGNAL(heightChanged()), q, SLOT(_q_updateSize())); + } + _q_updateSize(); + } } else { - delete qmlChild; + delete obj; source = QUrl(); } - delete qmlcomp; - qmlcomp = 0; emit q->sourceChanged(); emit q->statusChanged(); emit q->progressChanged(); @@ -196,6 +269,19 @@ void QFxLoaderPrivate::_q_sourceLoaded() \sa progress */ +QFxLoader::Status QFxLoader::status() const +{ + Q_D(const QFxLoader); + + if (d->component) + return static_cast(d->component->status()); + + if (d->item) + return Ready; + + return d->source.isEmpty() ? Null : Error; +} + /*! \qmlproperty real Loader::progress @@ -204,27 +290,87 @@ void QFxLoaderPrivate::_q_sourceLoaded() \sa status */ -QFxLoader::Status QFxLoader::status() const +qreal QFxLoader::progress() const { Q_D(const QFxLoader); - if (d->qmlcomp) - return static_cast(d->qmlcomp->status()); - if (d->item) - return Ready; + return 1.0; - return d->source.isEmpty() ? Null : Error; + if (d->component) + return d->component->progress(); + + return 0.0; } -qreal QFxLoader::progress() const +/*! + \qmlproperty enum 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 +*/ +QFxLoader::ResizeMode QFxLoader::resizeMode() const { Q_D(const QFxLoader); + return d->resizeMode; +} + +void QFxLoader::setResizeMode(ResizeMode mode) +{ + Q_D(QFxLoader); + if (mode == d->resizeMode) + return; - if (d->qmlcomp) - return d->qmlcomp->progress(); + if (d->item) { + QFxItem *resizeItem = 0; + if (d->resizeMode == SizeLoaderToItem) + resizeItem = d->item; + else if (d->resizeMode == SizeItemToLoader) + resizeItem = this; + if (resizeItem) { + disconnect(resizeItem, SIGNAL(widthChanged()), this, SLOT(_q_updateSize())); + disconnect(resizeItem, SIGNAL(heightChanged()), this, SLOT(_q_updateSize())); + } + } + + d->resizeMode = mode; + + if (d->item) { + QFxItem *resizeItem = 0; + if (d->resizeMode == SizeLoaderToItem) + resizeItem = d->item; + else if (d->resizeMode == SizeItemToLoader) + resizeItem = this; + if (resizeItem) { + connect(resizeItem, SIGNAL(widthChanged()), this, SLOT(_q_updateSize())); + connect(resizeItem, SIGNAL(heightChanged()), this, SLOT(_q_updateSize())); + } + } +} - return d->item ? 1.0 : 0.0; +void QFxLoaderPrivate::_q_updateSize() +{ + Q_Q(QFxLoader); + if (!item) + return; + switch (resizeMode) { + case QFxLoader::SizeLoaderToItem: + if (!q->widthValid()) + q->setImplicitWidth(item->width()); + if (!q->heightValid()) + q->setImplicitHeight(item->height()); + break; + case QFxLoader::SizeItemToLoader: + item->setWidth(q->width()); + item->setHeight(q->height()); + break; + default: + break; + } } /*! diff --git a/src/declarative/fx/qfxloader.h b/src/declarative/fx/qfxloader.h index 132c8f4..b967465 100644 --- a/src/declarative/fx/qfxloader.h +++ b/src/declarative/fx/qfxloader.h @@ -55,14 +55,15 @@ class Q_DECLARATIVE_EXPORT QFxLoader : public QFxItem { Q_OBJECT Q_ENUMS(Status) + Q_ENUMS(ResizeMode) Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(QmlComponent *sourceComponent READ sourceComponent WRITE setSourceComponent NOTIFY sourceChanged) + Q_PROPERTY(ResizeMode resizeMode READ resizeMode WRITE setResizeMode) Q_PROPERTY(QFxItem *item READ item NOTIFY itemChanged) Q_PROPERTY(Status status READ status NOTIFY statusChanged) Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) //### sourceItem - //### sourceComponent - //### resizeMode { NoResize, SizeLoaderToItem (default), SizeItemToLoader } public: QFxLoader(QFxItem *parent=0); @@ -71,10 +72,17 @@ public: QUrl source() const; void setSource(const QUrl &); + QmlComponent *sourceComponent() const; + void setSourceComponent(QmlComponent *); + enum Status { Null, Ready, Loading, Error }; Status status() const; qreal progress() const; + enum ResizeMode { NoResize, SizeLoaderToItem, SizeItemToLoader }; + ResizeMode resizeMode() const; + void setResizeMode(ResizeMode mode); + QFxItem *item() const; Q_SIGNALS: @@ -87,6 +95,7 @@ private: Q_DISABLE_COPY(QFxLoader) Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QFxLoader) Q_PRIVATE_SLOT(d_func(), void _q_sourceLoaded()) + Q_PRIVATE_SLOT(d_func(), void _q_updateSize()) }; QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxloader_p.h b/src/declarative/fx/qfxloader_p.h index 13f3b53..0700fcd 100644 --- a/src/declarative/fx/qfxloader_p.h +++ b/src/declarative/fx/qfxloader_p.h @@ -69,10 +69,12 @@ public: QUrl source; QFxItem *item; - QmlComponent *qmlcomp; - QHash cachedChildren; + QmlComponent *component; + bool ownComponent; + QFxLoader::ResizeMode resizeMode; void _q_sourceLoaded(); + void _q_updateSize(); }; QT_END_NAMESPACE diff --git a/src/declarative/qml/qmlcomponent.cpp b/src/declarative/qml/qmlcomponent.cpp index f291ac0..1c35606 100644 --- a/src/declarative/qml/qmlcomponent.cpp +++ b/src/declarative/qml/qmlcomponent.cpp @@ -91,8 +91,8 @@ Item { height: 10 } } - ComponentInstance { component: RedSquare } - ComponentInstance { component: RedSquare; x: 20 } + Loader { sourceComponent: RedSquare } + Loader { sourceComponent: RedSquare; x: 20 } } \endqml */ @@ -328,6 +328,7 @@ QmlComponent::QmlComponent(QmlEngine *engine, QmlCompiledData *cc, int start, in d->start = start; d->count = count; d->url = cc->url; + d->progress = 1.0; } /*! diff --git a/src/declarative/util/qfxperf.cpp b/src/declarative/util/qfxperf.cpp index 90f639e..739e480 100644 --- a/src/declarative/util/qfxperf.cpp +++ b/src/declarative/util/qfxperf.cpp @@ -58,7 +58,6 @@ Q_DEFINE_PERFORMANCE_LOG(QFxPerf, "QFx") { Q_DEFINE_PERFORMANCE_METRIC(CreateParticle, " QFxParticles: Particle creation") Q_DEFINE_PERFORMANCE_METRIC(ItemComponentComplete, " QFxItem::componentComplete") Q_DEFINE_PERFORMANCE_METRIC(ImageComponentComplete, " QFxImage::componentComplete") - Q_DEFINE_PERFORMANCE_METRIC(ComponentInstanceComponentComplete, " QFxComponentInstance::componentComplete") Q_DEFINE_PERFORMANCE_METRIC(BaseLayoutComponentComplete, " QFxBasePositioner::componentComplete") Q_DEFINE_PERFORMANCE_METRIC(TextComponentComplete, " QFxText::componentComplete") Q_DEFINE_PERFORMANCE_METRIC(QFxText_setText, " QFxText::setText") diff --git a/src/declarative/util/qfxperf_p.h b/src/declarative/util/qfxperf_p.h index a1e38b7..cea7e80 100644 --- a/src/declarative/util/qfxperf_p.h +++ b/src/declarative/util/qfxperf_p.h @@ -77,7 +77,6 @@ Q_DECLARE_PERFORMANCE_LOG(QFxPerf) { Q_DECLARE_PERFORMANCE_METRIC(CreateParticle) Q_DECLARE_PERFORMANCE_METRIC(ItemComponentComplete) Q_DECLARE_PERFORMANCE_METRIC(ImageComponentComplete) - Q_DECLARE_PERFORMANCE_METRIC(ComponentInstanceComponentComplete) Q_DECLARE_PERFORMANCE_METRIC(BaseLayoutComponentComplete) Q_DECLARE_PERFORMANCE_METRIC(TextComponentComplete) Q_DECLARE_PERFORMANCE_METRIC(QFxText_setText) diff --git a/tests/auto/declarative/qfxloader/NoResize.qml b/tests/auto/declarative/qfxloader/NoResize.qml new file mode 100644 index 0000000..cfbb55a --- /dev/null +++ b/tests/auto/declarative/qfxloader/NoResize.qml @@ -0,0 +1,7 @@ +import Qt 4.6 + +Loader { + resizeMode: "NoResize" + width: 200; height: 80 + source: "Rect120x60.qml" +} diff --git a/tests/auto/declarative/qfxloader/Rect120x60.qml b/tests/auto/declarative/qfxloader/Rect120x60.qml new file mode 100644 index 0000000..aa4b0c2 --- /dev/null +++ b/tests/auto/declarative/qfxloader/Rect120x60.qml @@ -0,0 +1,6 @@ +import Qt 4.6 + +Rectangle { + width: 120 + height:60 +} diff --git a/tests/auto/declarative/qfxloader/SetSourceComponent.qml b/tests/auto/declarative/qfxloader/SetSourceComponent.qml new file mode 100644 index 0000000..c5dd7ff --- /dev/null +++ b/tests/auto/declarative/qfxloader/SetSourceComponent.qml @@ -0,0 +1,6 @@ +import Qt 4.6 + +Item { + Component { id: Comp; Rectangle { width: 120; height: 60 } } + Loader { sourceComponent: Comp } +} diff --git a/tests/auto/declarative/qfxloader/SizeToItem.qml b/tests/auto/declarative/qfxloader/SizeToItem.qml new file mode 100644 index 0000000..b52fa03 --- /dev/null +++ b/tests/auto/declarative/qfxloader/SizeToItem.qml @@ -0,0 +1,6 @@ +import Qt 4.6 + +Loader { + resizeMode: "SizeLoaderToItem" + source: "Rect120x60.qml" +} diff --git a/tests/auto/declarative/qfxloader/SizeToLoader.qml b/tests/auto/declarative/qfxloader/SizeToLoader.qml new file mode 100644 index 0000000..1a107e1 --- /dev/null +++ b/tests/auto/declarative/qfxloader/SizeToLoader.qml @@ -0,0 +1,7 @@ +import Qt 4.6 + +Loader { + resizeMode: "SizeItemToLoader" + width: 200; height: 80 + source: "Rect120x60.qml" +} diff --git a/tests/auto/declarative/qfxloader/qfxloader.pro b/tests/auto/declarative/qfxloader/qfxloader.pro new file mode 100644 index 0000000..643c18c --- /dev/null +++ b/tests/auto/declarative/qfxloader/qfxloader.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative gui +SOURCES += tst_qfxloader.cpp + +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qfxloader/tst_qfxloader.cpp b/tests/auto/declarative/qfxloader/tst_qfxloader.cpp new file mode 100644 index 0000000..2109898 --- /dev/null +++ b/tests/auto/declarative/qfxloader/tst_qfxloader.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include + +class tst_qfxloader : public QObject + +{ + Q_OBJECT +public: + tst_qfxloader(); + +private slots: + void url(); + void component(); + void sizeLoaderToItem(); + void sizeItemToLoader(); + void noResize(); + +private: + QmlEngine engine; +}; + +/* +inline QUrl TEST_FILE(const QString &filename) +{ + QFileInfo fileInfo(__FILE__); + return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename)); +} + +inline QUrl TEST_FILE(const char *filename) +{ + return TEST_FILE(QLatin1String(filename)); +} +*/ + +tst_qfxloader::tst_qfxloader() +{ +} + +void tst_qfxloader::url() +{ + QmlComponent component(&engine, QByteArray("import Qt 4.6\nLoader { source: \"Rect120x60.qml\" }"), QUrl("file://" SRCDIR "/")); + QFxLoader *loader = qobject_cast(component.create()); + QVERIFY(loader != 0); + QVERIFY(loader->item()); + QCOMPARE(loader->progress(), 1.0); + QCOMPARE(static_cast(loader)->children().count(), 1); +} + +void tst_qfxloader::component() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/SetSourceComponent.qml")); + QFxItem *item = qobject_cast(component.create()); + QVERIFY(item); + + QFxLoader *loader = qobject_cast(item->QGraphicsObject::children().at(1)); + QVERIFY(loader); + QVERIFY(loader->item()); + QCOMPARE(loader->progress(), 1.0); + QCOMPARE(static_cast(loader)->children().count(), 1); +} + +void tst_qfxloader::sizeLoaderToItem() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/SizeToItem.qml")); + QFxLoader *loader = qobject_cast(component.create()); + QVERIFY(loader != 0); + QCOMPARE(loader->width(), 120.0); + QCOMPARE(loader->height(), 60.0); +} + +void tst_qfxloader::sizeItemToLoader() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/SizeToLoader.qml")); + QFxLoader *loader = qobject_cast(component.create()); + QVERIFY(loader != 0); + QCOMPARE(loader->width(), 200.0); + QCOMPARE(loader->height(), 80.0); + + QFxItem *rect = loader->item(); + QVERIFY(rect); + QCOMPARE(rect->width(), 200.0); + QCOMPARE(rect->height(), 80.0); +} + +void tst_qfxloader::noResize() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/NoResize.qml")); + QFxLoader *loader = qobject_cast(component.create()); + QVERIFY(loader != 0); + QCOMPARE(loader->width(), 200.0); + QCOMPARE(loader->height(), 80.0); + + QFxItem *rect = loader->item(); + QVERIFY(rect); + QCOMPARE(rect->width(), 120.0); + QCOMPARE(rect->height(), 60.0); +} + +QTEST_MAIN(tst_qfxloader) + +#include "tst_qfxloader.moc" -- cgit v0.12