diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-07-30 23:39:53 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-07-30 23:39:53 (GMT) |
commit | 0ce2bedf9b5da9d480b994a9b812c37d1f5caa39 (patch) | |
tree | 7e8ef6069f8c669ff6e92e23cf8b46cfad7ed3ee /src/declarative/fx/qfxloader.cpp | |
parent | a70f8a2b0966420e1911554a117e066592b1822c (diff) | |
download | Qt-0ce2bedf9b5da9d480b994a9b812c37d1f5caa39.zip Qt-0ce2bedf9b5da9d480b994a9b812c37d1f5caa39.tar.gz Qt-0ce2bedf9b5da9d480b994a9b812c37d1f5caa39.tar.bz2 |
Introduce Loader item.
The Loader item takes the qml/qmlItem functionality from Item and
moves it to a specialized subclass.
Diffstat (limited to 'src/declarative/fx/qfxloader.cpp')
-rw-r--r-- | src/declarative/fx/qfxloader.cpp | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/src/declarative/fx/qfxloader.cpp b/src/declarative/fx/qfxloader.cpp new file mode 100644 index 0000000..7cf85af --- /dev/null +++ b/src/declarative/fx/qfxloader.cpp @@ -0,0 +1,188 @@ +/**************************************************************************** +** +** 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 "qfxloader_p.h" +#include <private/qmlengine_p.h> + +QT_BEGIN_NAMESPACE + +QFxLoaderPrivate::QFxLoaderPrivate() +: item(0), qmlcomp(0) +{ +} + +QFxLoaderPrivate::~QFxLoaderPrivate() +{ +} + +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. + */ + +/*! + \internal + \class QFxLoader + \qmlclass Loader + */ + +/*! + Create a new QFxLoader instance. + */ +QFxLoader::QFxLoader(QFxItem *parent) + : QFxItem(*(new QFxLoaderPrivate), parent) +{ +} + +/*! + Destroy the loader instance. + */ +QFxLoader::~QFxLoader() +{ +} + +/*! + \internal + \fn void QFxItem::sourceChanged() + This signal is emitted whenever the item's dynamic QML + source url changes. + */ + +/*! + \qmlproperty url Loader::source + This property holds the dynamic URL of the QML for the item. + + This property is used for dynamically loading QML into the + item. +*/ +QUrl QFxLoader::source() const +{ + Q_D(const QFxLoader); + return d->source; +} + +void QFxLoader::setSource(const QUrl &url) +{ + Q_D(QFxLoader); + if (d->source == url) + return; + + if (!d->source.isEmpty()) { + QHash<QString, QFxItem *>::Iterator iter = d->cachedChildren.find(d->source.toString()); + if (iter != d->cachedChildren.end()) + (*iter)->setOpacity(0.); + } + + d->source = url; + d->item = 0; + + if (d->source.isEmpty()) { + emit sourceChanged(); + return; + } + + QHash<QString, QFxItem *>::Iterator iter = d->cachedChildren.find(d->source.toString()); + if (iter != d->cachedChildren.end()) { + (*iter)->setOpacity(1.); + d->item = (*iter); + emit sourceChanged(); + } 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())); + } +} + +void QFxLoaderPrivate::_q_sourceLoaded() +{ + Q_Q(QFxLoader); + + if (qmlcomp) { + QmlContext *ctxt = new QmlContext(qmlContext(q)); + ctxt->addDefaultObject(q); + + if (!qmlcomp->errors().isEmpty()) { + qWarning() << qmlcomp->errors(); + delete qmlcomp; + qmlcomp = 0; + emit q->sourceChanged(); + return; + } + QObject *obj = qmlcomp->create(ctxt); + if (!qmlcomp->errors().isEmpty()) + qWarning() << qmlcomp->errors(); + QFxItem *qmlChild = qobject_cast<QFxItem *>(obj); + if (qmlChild) { + qmlChild->setParentItem(q); + cachedChildren.insert(source.toString(), qmlChild); + item = qmlChild; + } else { + delete qmlChild; + source = QUrl(); + } + delete qmlcomp; + qmlcomp = 0; + emit q->sourceChanged(); + } +} + +/*! + \qmlproperty url Loader::item + This property holds the top-level item created from source. +*/ +QFxItem *QFxLoader::item() const +{ + Q_D(const QFxLoader); + return d->item; +} + +QT_END_NAMESPACE + +#include "moc_qfxloader.cpp" |