summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems/qdeclarativeloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativeloader.cpp')
-rw-r--r--src/declarative/graphicsitems/qdeclarativeloader.cpp148
1 files changed, 50 insertions, 98 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeloader.cpp b/src/declarative/graphicsitems/qdeclarativeloader.cpp
index bdd2c87..cbdfd87 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<QDeclarativeItem*>(item)) {
- if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) {
- QDeclarativeItemPrivate *p =
+ QDeclarativeItemPrivate *p =
static_cast<QDeclarativeItemPrivate *>(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<QDeclarativeItem*>(item)) {
- if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) {
- QDeclarativeItemPrivate *p =
+ QDeclarativeItemPrivate *p =
static_cast<QDeclarativeItemPrivate *>(QGraphicsItemPrivate::get(qmlItem));
- p->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
- }
+ p->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
} else if (item && item->isWidget()) {
QGraphicsWidget *widget = static_cast<QGraphicsWidget*>(item);
- if (resizeMode == QDeclarativeLoader::SizeLoaderToItem) {
- widget->installEventFilter(q);
- }
+ widget->installEventFilter(q);
}
_q_updateSize();
}
@@ -329,6 +321,7 @@ void QDeclarativeLoaderPrivate::_q_sourceLoaded()
emit q->statusChanged();
emit q->progressChanged();
emit q->itemChanged();
+ emit q->loaded();
}
}
@@ -337,10 +330,10 @@ void QDeclarativeLoaderPrivate::_q_sourceLoaded()
This property holds the status of QML loading. It can be one of:
\list
- \o Null - no QML source has been set
- \o Ready - the QML source has been loaded
- \o Loading - the QML source is currently being loaded
- \o Error - an error occurred while loading the QML source
+ \o Loader.Null - no QML source has been set
+ \o Loader.Ready - the QML source has been loaded
+ \o Loader.Loading - the QML source is currently being loaded
+ \o Loader.Error - an error occurred while loading the QML source
\endlist
Note that a change in the status property does not cause anything to happen
@@ -349,10 +342,13 @@ void QDeclarativeLoaderPrivate::_q_sourceLoaded()
of the following ways:
\list
\o Create a state, so that a state change occurs, e.g. State{name: 'loaded'; when: loader.status = Loader.Ready;}
- \o Do something inside the onStatusChanged signal handler, e.g. Loader{id: loader; onStatusChanged: if(loader.status == Loader.Ready) console.log('Loaded');}
+ \o Do something inside the onLoaded signal handler, e.g. Loader{id: loader; onLoaded: console.log('Loaded');}
\o Bind to the status variable somewhere, e.g. Text{text: if(loader.status!=Loader.Ready){'Not Loaded';}else{'Loaded';}}
\endlist
\sa progress
+
+ Note that if the source is a local file, the status will initially be Ready (or Error). While
+ there will be no onStatusChanged signal in that case, the onLoaded will still be invoked.
*/
QDeclarativeLoader::Status QDeclarativeLoader::status() const
@@ -368,6 +364,21 @@ QDeclarativeLoader::Status QDeclarativeLoader::status() const
return d->source.isEmpty() ? Null : Error;
}
+void QDeclarativeLoader::componentComplete()
+{
+ if (status() == Ready)
+ emit loaded();
+}
+
+
+/*!
+ \qmlsignal Loader::onLoaded()
+
+ This handler is called when the \l status becomes Loader.Ready, or on successful
+ initial load.
+*/
+
+
/*!
\qmlproperty real Loader::progress
@@ -390,83 +401,29 @@ 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<QDeclarativeItem*>(d->item)) {
- if (d->resizeMode == SizeLoaderToItem) {
- QDeclarativeItemPrivate *p =
- static_cast<QDeclarativeItemPrivate *>(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<QDeclarativeItem*>(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<QGraphicsWidget*>(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 +441,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 +451,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 +462,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);
}