1 // Commit: 501180c6fbed0857126da2bb0ff1f17ee35472c6
2 /****************************************************************************
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** No Commercial Usage
12 ** This file contains pre-release code and may not be distributed.
13 ** You may use this file in accordance with the terms and conditions
14 ** contained in the Technology Preview License Agreement accompanying
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
41 ****************************************************************************/
43 #include "qsgloader_p_p.h"
45 #include <QtDeclarative/qdeclarativeinfo.h>
47 #include <private/qdeclarativeengine_p.h>
48 #include <private/qdeclarativeglobal_p.h>
52 QSGLoaderPrivate::QSGLoaderPrivate()
53 : item(0), component(0), ownComponent(false), updatingSize(false),
54 itemWidthValid(false), itemHeightValid(false)
58 QSGLoaderPrivate::~QSGLoaderPrivate()
62 void QSGLoaderPrivate::itemGeometryChanged(QSGItem *resizeItem, const QRectF &newGeometry, const QRectF &oldGeometry)
64 if (resizeItem == item) {
65 if (!updatingSize && newGeometry.width() != oldGeometry.width())
66 itemWidthValid = true;
67 if (!updatingSize && newGeometry.height() != oldGeometry.height())
68 itemHeightValid = true;
71 QSGItemChangeListener::itemGeometryChanged(resizeItem, newGeometry, oldGeometry);
74 void QSGLoaderPrivate::clear()
77 component->deleteLater();
84 QSGItemPrivate *p = QSGItemPrivate::get(item);
85 p->removeItemChangeListener(this, QSGItemPrivate::Geometry);
87 // We can't delete immediately because our item may have triggered
88 // the Loader to load a different item.
89 item->setParentItem(0);
90 item->setVisible(false);
96 void QSGLoaderPrivate::initResize()
98 QSGItemPrivate *p = QSGItemPrivate::get(item);
99 p->addItemChangeListener(this, QSGItemPrivate::Geometry);
100 // We may override the item's size, so we need to remember
101 // whether the item provided its own valid size.
102 itemWidthValid = p->widthValid;
103 itemHeightValid = p->heightValid;
107 QSGLoader::QSGLoader(QSGItem *parent)
108 : QSGImplicitSizeItem(*(new QSGLoaderPrivate), parent)
110 setFlag(ItemIsFocusScope);
113 QSGLoader::~QSGLoader()
117 QSGItemPrivate *p = QSGItemPrivate::get(d->item);
118 p->removeItemChangeListener(d, QSGItemPrivate::Geometry);
122 QUrl QSGLoader::source() const
124 Q_D(const QSGLoader);
128 void QSGLoader::setSource(const QUrl &url)
131 if (d->source == url)
137 if (d->source.isEmpty()) {
138 emit sourceChanged();
139 emit statusChanged();
140 emit progressChanged();
145 d->component = new QDeclarativeComponent(qmlEngine(this), d->source, this);
146 d->ownComponent = true;
148 if (isComponentComplete())
152 QDeclarativeComponent *QSGLoader::sourceComponent() const
154 Q_D(const QSGLoader);
158 void QSGLoader::setSourceComponent(QDeclarativeComponent *comp)
161 if (comp == d->component)
167 d->ownComponent = false;
169 emit sourceChanged();
170 emit statusChanged();
171 emit progressChanged();
176 if (isComponentComplete())
180 void QSGLoader::resetSourceComponent()
182 setSourceComponent(0);
185 void QSGLoaderPrivate::load()
189 if (!q->isComponentComplete() || !component)
192 if (!component->isLoading()) {
195 QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
196 q, SLOT(_q_sourceLoaded()));
197 QObject::connect(component, SIGNAL(progressChanged(qreal)),
198 q, SIGNAL(progressChanged()));
199 emit q->statusChanged();
200 emit q->progressChanged();
201 emit q->sourceChanged();
202 emit q->itemChanged();
206 void QSGLoaderPrivate::_q_sourceLoaded()
211 if (!component->errors().isEmpty()) {
212 QDeclarativeEnginePrivate::warning(qmlEngine(q), component->errors());
213 emit q->sourceChanged();
214 emit q->statusChanged();
215 emit q->progressChanged();
219 QDeclarativeContext *creationContext = component->creationContext();
220 if (!creationContext) creationContext = qmlContext(q);
221 QDeclarativeContext *ctxt = new QDeclarativeContext(creationContext);
222 ctxt->setContextObject(q);
224 QDeclarativeGuard<QDeclarativeComponent> c = component;
225 QObject *obj = component->beginCreate(ctxt);
226 if (component != c) {
227 // component->create could trigger a change in source that causes
228 // component to be set to something else. In that case we just
237 item = qobject_cast<QSGItem *>(obj);
239 QDeclarative_setParent_noEvent(ctxt, obj);
240 QDeclarative_setParent_noEvent(item, q);
241 item->setParentItem(q);
242 // item->setFocus(true);
245 qmlInfo(q) << QSGLoader::tr("Loader does not support loading non-visual elements.");
250 if (!component->errors().isEmpty())
251 QDeclarativeEnginePrivate::warning(qmlEngine(q), component->errors());
256 component->completeCreate();
257 emit q->sourceChanged();
258 emit q->statusChanged();
259 emit q->progressChanged();
260 emit q->itemChanged();
265 QSGLoader::Status QSGLoader::status() const
267 Q_D(const QSGLoader);
270 return static_cast<QSGLoader::Status>(d->component->status());
275 return d->source.isEmpty() ? Null : Error;
278 void QSGLoader::componentComplete()
281 QSGItem::componentComplete();
285 qreal QSGLoader::progress() const
287 Q_D(const QSGLoader);
293 return d->component->progress();
298 void QSGLoaderPrivate::_q_updateSize(bool loaderGeometryChanged)
301 if (!item || updatingSize)
307 q->setImplicitWidth(item->implicitWidth());
309 q->setImplicitWidth(item->width());
310 if (loaderGeometryChanged && q->widthValid())
311 item->setWidth(q->width());
313 if (!itemHeightValid)
314 q->setImplicitHeight(item->implicitHeight());
316 q->setImplicitHeight(item->height());
317 if (loaderGeometryChanged && q->heightValid())
318 item->setHeight(q->height());
320 updatingSize = false;
323 QSGItem *QSGLoader::item() const
325 Q_D(const QSGLoader);
329 void QSGLoader::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
332 if (newGeometry != oldGeometry) {
335 QSGItem::geometryChanged(newGeometry, oldGeometry);
338 #include <moc_qsgloader_p.cpp>