src/declarative/qml/qdeclarativecomponent.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
--- a/src/declarative/qml/qdeclarativecomponent.cpp	Fri Sep 17 08:34:18 2010 +0300
+++ b/src/declarative/qml/qdeclarativecomponent.cpp	Mon Oct 04 01:19:32 2010 +0300
@@ -44,7 +44,6 @@
 
 #include "private/qdeclarativecompiler_p.h"
 #include "private/qdeclarativecontext_p.h"
-#include "private/qdeclarativecompositetypedata_p.h"
 #include "private/qdeclarativeengine_p.h"
 #include "private/qdeclarativevme_p.h"
 #include "qdeclarative.h"
@@ -54,6 +53,7 @@
 #include "private/qdeclarativeglobal_p.h"
 #include "private/qdeclarativescriptparser_p.h"
 #include "private/qdeclarativedebugtrace_p.h"
+#include "private/qdeclarativeenginedebug_p.h"
 
 #include <QStack>
 #include <QStringList>
@@ -104,6 +104,7 @@
 
 /*!
     \qmlclass Component QDeclarativeComponent
+    \ingroup qml-utility-elements
     \since 4.7
     \brief The Component element encapsulates a QML component definition.
 
@@ -115,24 +116,10 @@
     a small component within a QML file, or for defining a component that logically 
     belongs with other QML components within a file.
 
-    For example, here is a component that is used by multiple \l Loader objects:
-
-    \qml
-    Item {
-        Component {
-            id: redSquare
+    For example, here is a component that is used by multiple \l Loader objects.
+    It contains a top level \l Rectangle item:
 
-            Rectangle {
-                color: "red"
-                width: 10
-                height: 10
-            }
-        }
-
-        Loader { sourceComponent: redSquare }
-        Loader { sourceComponent: redSquare; x: 20 }
-    }
-    \endqml
+    \snippet doc/src/snippets/declarative/component.qml 0
 
     Notice that while a \l Rectangle by itself would be automatically 
     rendered and displayed, this is not the case for the above rectangle
@@ -141,12 +128,16 @@
     file, and is not loaded until requested (in this case, by the
     two \l Loader objects).
 
+    A Component cannot contain anything other
+    than an \c id and a single top level item. While the \c id is optional,
+    the top level item is not; you cannot define an empty component.
+
     The Component element is commonly used to provide graphical components
     for views. For example, the ListView::delegate property requires a Component
     to specify how each list item is to be displayed.
 
-    Component objects can also be dynamically generated using
-    \l{Qt::createComponent}{Qt.createComponent()}.
+    Component objects can also be dynamically created using
+    \l{QML:Qt::createComponent()}{Qt.createComponent()}.
 */
 
 /*!
@@ -202,10 +193,10 @@
     \value Null This QDeclarativeComponent has no data.  Call loadUrl() or setData() to add QML content.
     \value Ready This QDeclarativeComponent is ready and create() may be called.
     \value Loading This QDeclarativeComponent is loading network data.
-    \value Error An error has occured.  Call errors() to retrieve a list of \{QDeclarativeError}{errors}.
+    \value Error An error has occurred.  Call errors() to retrieve a list of \{QDeclarativeError}{errors}.
 */
 
-void QDeclarativeComponentPrivate::typeDataReady()
+void QDeclarativeComponentPrivate::typeDataReady(QDeclarativeTypeData *)
 {
     Q_Q(QDeclarativeComponent);
 
@@ -217,28 +208,25 @@
     emit q->statusChanged(q->status());
 }
 
-void QDeclarativeComponentPrivate::updateProgress(qreal p)
+void QDeclarativeComponentPrivate::typeDataProgress(QDeclarativeTypeData *, qreal p)
 {
     Q_Q(QDeclarativeComponent);
 
     progress = p;
+
     emit q->progressChanged(p);
 }
 
-void QDeclarativeComponentPrivate::fromTypeData(QDeclarativeCompositeTypeData *data)
+void QDeclarativeComponentPrivate::fromTypeData(QDeclarativeTypeData *data)
 {
-    url = data->imports.baseUrl();
-    QDeclarativeCompiledData *c = data->toCompiledComponent(engine);
+    url = data->finalUrl();
+    QDeclarativeCompiledData *c = data->compiledData();
 
     if (!c) {
-        Q_ASSERT(data->status == QDeclarativeCompositeTypeData::Error);
-
-        state.errors = data->errors;
-
+        Q_ASSERT(data->isError());
+        state.errors = data->errors();
     } else {
-
         cc = c;
-
     }
 
     data->release();
@@ -247,7 +235,7 @@
 void QDeclarativeComponentPrivate::clear()
 {
     if (typeData) {
-        typeData->remWaiter(this);
+        typeData->unregisterCallback(this);
         typeData->release();
         typeData = 0;
     }
@@ -279,7 +267,7 @@
     }
 
     if (d->typeData) {
-        d->typeData->remWaiter(d);
+        d->typeData->unregisterCallback(d);
         d->typeData->release();
     }
     if (d->cc)
@@ -451,19 +439,13 @@
 
     d->url = url;
 
-    QDeclarativeCompositeTypeData *typeData = 
-        QDeclarativeEnginePrivate::get(d->engine)->typeManager.getImmediate(data, url);
+    QDeclarativeTypeData *typeData = QDeclarativeEnginePrivate::get(d->engine)->typeLoader.get(data, url);
     
-    if (typeData->status == QDeclarativeCompositeTypeData::Waiting
-     || typeData->status == QDeclarativeCompositeTypeData::WaitingResources)
-    {
+    if (typeData->isCompleteOrError()) {
+        d->fromTypeData(typeData);
+    } else {
         d->typeData = typeData;
-        d->typeData->addWaiter(d);
-
-    } else {
-
-        d->fromTypeData(typeData);
-
+        d->typeData->registerCallback(d);
     }
 
     d->progress = 1.0;
@@ -509,18 +491,15 @@
         return;
     }
 
-    QDeclarativeCompositeTypeData *data = 
-        QDeclarativeEnginePrivate::get(d->engine)->typeManager.get(d->url);
+    QDeclarativeTypeData *data = QDeclarativeEnginePrivate::get(d->engine)->typeLoader.get(d->url);
 
-    if (data->status == QDeclarativeCompositeTypeData::Waiting
-     || data->status == QDeclarativeCompositeTypeData::WaitingResources)
-    {
-        d->typeData = data;
-        d->typeData->addWaiter(d);
-        d->progress = data->progress;
-    } else {
+    if (data->isCompleteOrError()) {
         d->fromTypeData(data);
         d->progress = 1.0;
+    } else {
+        d->typeData = data;
+        d->typeData->registerCallback(d);
+        d->progress = data->progress();
     }
 
     emit statusChanged(status());
@@ -528,7 +507,7 @@
 }
 
 /*!
-    Return the list of errors that occured during the last compile or create
+    Return the list of errors that occurred during the last compile or create
     operation.  An empty list is returned if isError() is not set.
 */
 QList<QDeclarativeError> QDeclarativeComponent::errors() const
@@ -607,6 +586,9 @@
     the \a parent value. Note that if the returned object is to be displayed, you 
     must provide a valid \a parent value or set the returned object's \l{Item::parent}{parent} 
     property, or else the object will not be visible.
+
+    Dynamically created instances can be deleted with the \c destroy() method.
+    See \l {Dynamic Object Management in QML} for more information.
 */
 
 /*!
@@ -625,10 +607,11 @@
         ctxt = d->engine->rootContext();
     if (!ctxt)
         return QScriptValue(QScriptValue::NullValue);
-    QObject* ret = create(ctxt);
-    if (!ret)
+    QObject* ret = beginCreate(ctxt);
+    if (!ret) {
+        completeCreate();
         return QScriptValue(QScriptValue::NullValue);
-
+    }
 
     if (parent) {
         ret->setParent(parent);
@@ -649,6 +632,7 @@
         if (needParent) 
             qWarning("QDeclarativeComponent: Created graphical object was not placed in the graphics scene.");
     }
+    completeCreate();
 
     QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(d->engine);
     QDeclarativeData::get(ret, true)->setImplicitDestructible();
@@ -771,8 +755,11 @@
 
     QObject *rv = begin(ctxt, ep, cc, start, count, &state, bindings);
 
-    if (rv && !context->isInternal && ep->isDebugging)
-        context->asQDeclarativeContextPrivate()->instances.append(rv);
+    if (ep->isDebugging && rv) {
+        if  (!context->isInternal)
+            context->asQDeclarativeContextPrivate()->instances.append(rv);
+        QDeclarativeEngineDebugServer::instance()->objectCreated(engine, rv);
+    }
 
     return rv;
 }