QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickview.cpp
index 7ffafeb..2a56c73 100644 (file)
 #include "qquickview.h"
 #include "qquickview_p.h"
 
-#include "qquickcanvas_p.h"
+#include "qquickwindow_p.h"
 #include "qquickitem_p.h"
 #include "qquickitemchangelistener_p.h"
 
 #include <private/qqmlprofilerservice_p.h>
 #include <private/qqmlinspectorservice_p.h>
+#include <private/qqmlmemoryprofiler_p.h>
 
 #include <QtQml/qqmlengine.h>
 #include <private/qqmlengine_p.h>
 #include <QtCore/qbasictimer.h>
 
-
 QT_BEGIN_NAMESPACE
 
-void QQuickViewPrivate::init()
+void QQuickViewPrivate::init(QQmlEngine* e)
 {
     Q_Q(QQuickView);
 
-    engine.setIncubationController(q->incubationController());
+    engine = e;
+
+    if (engine.isNull())
+        engine = new QQmlEngine(q);
+
+    if (!engine.data()->incubationController())
+        engine.data()->setIncubationController(q->incubationController());
 
     if (QQmlDebugService::isDebuggingEnabled())
         QQmlInspectorService::instance()->addView(q);
@@ -75,13 +81,16 @@ QQuickViewPrivate::~QQuickViewPrivate()
 {
     if (QQmlDebugService::isDebuggingEnabled())
         QQmlInspectorService::instance()->removeView(q_func());
-
-    delete root;
 }
 
 void QQuickViewPrivate::execute()
 {
     Q_Q(QQuickView);
+    if (!engine) {
+        qWarning() << "QQuickView: invalid qml engine.";
+        return;
+    }
+
     if (root) {
         delete root;
         root = 0;
@@ -91,7 +100,8 @@ void QQuickViewPrivate::execute()
         component = 0;
     }
     if (!source.isEmpty()) {
-        component = new QQmlComponent(&engine, source, q);
+        QML_MEMORY_SCOPE_URL(engine.data()->baseUrl().resolved(source));
+        component = new QQmlComponent(engine.data(), source, q);
         if (!component->isLoading()) {
             q->continueExecute();
         } else {
@@ -118,9 +128,9 @@ void QQuickViewPrivate::itemGeometryChanged(QQuickItem *resizeItem, const QRectF
 
     \inmodule QtQuick
 
-    This is a convenience subclass of QQuickCanvas which
+    This is a convenience subclass of QQuickWindow which
     will automatically load and display a QML scene when given the URL of the main source file. Alternatively,
-    you can instantiate your own objects using QQmlComponent and place them in a manually setup QQuickCanvas.
+    you can instantiate your own objects using QQmlComponent and place them in a manually setup QQuickWindow.
 
     Typical usage:
 
@@ -134,7 +144,12 @@ void QQuickViewPrivate::itemGeometryChanged(QQuickItem *resizeItem, const QRectF
     you can connect to the statusChanged() signal and monitor for QQuickView::Error.
     The errors are available via QQuickView::errors().
 
-    \sa {Using QML Bindings in C++ Applications}
+    QQuickView also manages sizing of the view and root object.  By default, the \l resizeMode
+    is SizeViewToRootObject, which will load the component and resize it to the
+    size of the view.  Alternatively the resizeMode may be set to SizeRootObjectToView which
+    will resize the view to the size of the root object.
+
+    \sa {Exposing C++ Data to QML}
 */
 
 
@@ -151,42 +166,66 @@ void QQuickViewPrivate::itemGeometryChanged(QQuickItem *resizeItem, const QRectF
 */
 
 /*!
-  \fn QQuickView::QQuickView(QWindow *parent)
-
   Constructs a QQuickView with the given \a parent.
+  The default value of \a parent is 0.
+
 */
-QQuickView::QQuickView(QWindow *parent, Qt::WindowFlags f)
-: QQuickCanvas(*(new QQuickViewPrivate), parent)
+QQuickView::QQuickView(QWindow *parent)
+: QQuickWindow(*(new QQuickViewPrivate), parent)
 {
-    setWindowFlags(f);
     d_func()->init();
 }
 
 /*!
-  \fn QQuickView::QQuickView(const QUrl &source, QWidget *parent)
-
   Constructs a QQuickView with the given QML \a source and \a parent.
+  The default value of \a parent is 0.
+
 */
-QQuickView::QQuickView(const QUrl &source, QWindow *parent, Qt::WindowFlags f)
-: QQuickCanvas(*(new QQuickViewPrivate), parent)
+QQuickView::QQuickView(const QUrl &source, QWindow *parent)
+: QQuickWindow(*(new QQuickViewPrivate), parent)
 {
-    setWindowFlags(f);
     d_func()->init();
     setSource(source);
 }
 
+/*!
+  Constructs a QQuickView with the given QML \a engine and \a parent.
+
+  Note: In this case, the QQuickView does not own the given \a engine object;
+  it is the caller's responsibility to destroy the engine. If the \a engine is deleted
+  before the view \a status() will return \a QQuickView::Error.
+
+  \sa Status, status(), errors()
+*/
+QQuickView::QQuickView(QQmlEngine* engine, QWindow *parent)
+    : QQuickWindow(*(new QQuickViewPrivate), parent)
+{
+    Q_ASSERT(engine);
+    d_func()->init(engine);
+}
+
+/*!
+  Destroys the QQuickView.
+*/
 QQuickView::~QQuickView()
 {
+    // Ensure that the component is destroyed before the engine; the engine may
+    // be a child of the QQuickViewPrivate, and will be destroyed by its dtor
+    Q_D(QQuickView);
+    delete d->root;
+    d->root = 0;
 }
 
-/*! \property QQuickView::source
+/*!
+  \property QQuickView::source
   \brief The URL of the source of the QML component.
 
-  Changing this property causes the QML component to be reloaded.
+  Ensure that the URL provided is full and correct, in particular, use
+  \l QUrl::fromLocalFile() when loading a file from the local filesystem.
 
-    Ensure that the URL provided is full and correct, in particular, use
-    \l QUrl::fromLocalFile() when loading a file from the local filesystem.
- */
+  Note that setting a source URL will result in the QML component being
+  instantiated, even if the URL is unchanged from the current value.
+*/
 
 /*!
     Sets the source to the \a url, loads the QML component and instantiates it.
@@ -194,8 +233,8 @@ QQuickView::~QQuickView()
     Ensure that the URL provided is full and correct, in particular, use
     \l QUrl::fromLocalFile() when loading a file from the local filesystem.
 
-    Calling this methods multiple times with the same url will result
-    in the QML being reloaded.
+    Calling this method multiple times with the same url will result
+    in the QML component being reinstantiated.
  */
 void QQuickView::setSource(const QUrl& url)
 {
@@ -222,7 +261,7 @@ QUrl QQuickView::source() const
 QQmlEngine* QQuickView::engine() const
 {
     Q_D(const QQuickView);
-    return const_cast<QQmlEngine *>(&d->engine);
+    return d->engine ? const_cast<QQmlEngine *>(d->engine.data()) : 0;
 }
 
 /*!
@@ -235,7 +274,7 @@ QQmlEngine* QQuickView::engine() const
 QQmlContext* QQuickView::rootContext() const
 {
     Q_D(const QQuickView);
-    return d->engine.rootContext();
+    return d->engine ? d->engine.data()->rootContext() : 0;
 }
 
 /*!
@@ -265,6 +304,9 @@ QQmlContext* QQuickView::rootContext() const
 QQuickView::Status QQuickView::status() const
 {
     Q_D(const QQuickView);
+    if (!d->engine)
+        return QQuickView::Error;
+
     if (!d->component)
         return QQuickView::Null;
 
@@ -278,24 +320,35 @@ QQuickView::Status QQuickView::status() const
 QList<QQmlError> QQuickView::errors() const
 {
     Q_D(const QQuickView);
+    QList<QQmlError> errs;
+
     if (d->component)
-        return d->component->errors();
-    return QList<QQmlError>();
+        errs = d->component->errors();
+
+    if (!d->engine) {
+        QQmlError error;
+        error.setDescription(QLatin1String("QQuickView: invalid qml engine."));
+        errs << error;
+    }
+
+    return errs;
 }
 
 /*!
     \property QQuickView::resizeMode
-    \brief whether the view should resize the canvas contents
+    \brief whether the view should resize the window contents
 
     If this property is set to SizeViewToRootObject (the default), the view
-    resizes with the root item in the QML.
+    resizes to the size of the root item in the QML.
 
     If this property is set to SizeRootObjectToView, the view will
-    automatically resize the root item.
+    automatically resize the root item to the size of the view.
 
     Regardless of this property, the sizeHint of the view
     is the initial size of the root item. Note though that
     since QML may load dynamically, that size may change.
+
+    \sa initialSize
 */
 
 void QQuickView::setResizeMode(ResizeMode mode)
@@ -416,7 +469,7 @@ void QQuickViewPrivate::setRootObject(QObject *obj)
         return;
     if (QQuickItem *sgItem = qobject_cast<QQuickItem *>(obj)) {
         root = sgItem;
-        sgItem->setParentItem(q->QQuickCanvas::rootItem());
+        sgItem->setParentItem(q->QQuickWindow::rootItem());
     } else {
         qWarning() << "QQuickView only supports loading of root objects that derive from QQuickItem." << endl
                    << endl
@@ -430,8 +483,8 @@ void QQuickViewPrivate::setRootObject(QObject *obj)
     }
     if (root) {
         initialSize = rootObjectSize();
-        if ((resizeMode == QQuickView::SizeViewToRootObject || !q->width() || !q->height())
-                && initialSize != q->size()) {
+        if ((resizeMode == QQuickView::SizeViewToRootObject || q->width() <= 1 || q->height() <= 1) &&
+            initialSize != q->size()) {
             q->resize(initialSize);
         }
         initResize();
@@ -468,7 +521,11 @@ QSize QQuickView::sizeHint() const
 }
 
 /*!
-  Returns the initial size of the root object
+  Returns the initial size of the root object.
+
+  If \l resizeMode is QQuickItem::SizeRootObjectToView the root object will be
+  resized to the size of the view.  initialSize contains the size of the
+  root object before it was resized.
 */
 QSize QQuickView::initialSize() const
 {
@@ -496,7 +553,7 @@ void QQuickView::resizeEvent(QResizeEvent *e)
     if (d->resizeMode == SizeRootObjectToView)
         d->updateSize();
 
-    QQuickCanvas::resizeEvent(e);
+    QQuickWindow::resizeEvent(e);
 }
 
 /*! \reimp */
@@ -504,7 +561,7 @@ void QQuickView::keyPressEvent(QKeyEvent *e)
 {
     QQmlProfilerService::addEvent(QQmlProfilerService::Key);
 
-    QQuickCanvas::keyPressEvent(e);
+    QQuickWindow::keyPressEvent(e);
 }
 
 /*! \reimp */
@@ -512,7 +569,7 @@ void QQuickView::keyReleaseEvent(QKeyEvent *e)
 {
     QQmlProfilerService::addEvent(QQmlProfilerService::Key);
 
-    QQuickCanvas::keyReleaseEvent(e);
+    QQuickWindow::keyReleaseEvent(e);
 }
 
 /*! \reimp */
@@ -520,7 +577,7 @@ void QQuickView::mouseMoveEvent(QMouseEvent *e)
 {
     QQmlProfilerService::addEvent(QQmlProfilerService::Mouse);
 
-    QQuickCanvas::mouseMoveEvent(e);
+    QQuickWindow::mouseMoveEvent(e);
 }
 
 /*! \reimp */
@@ -528,7 +585,7 @@ void QQuickView::mousePressEvent(QMouseEvent *e)
 {
     QQmlProfilerService::addEvent(QQmlProfilerService::Mouse);
 
-    QQuickCanvas::mousePressEvent(e);
+    QQuickWindow::mousePressEvent(e);
 }
 
 /*! \reimp */
@@ -536,7 +593,7 @@ void QQuickView::mouseReleaseEvent(QMouseEvent *e)
 {
     QQmlProfilerService::addEvent(QQmlProfilerService::Mouse);
 
-    QQuickCanvas::mouseReleaseEvent(e);
+    QQuickWindow::mouseReleaseEvent(e);
 }