QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickview.cpp
index 36682a1..2a56c73 100644 (file)
@@ -1,10 +1,9 @@
 /****************************************************************************
 **
 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Contact: http://www.qt-project.org/
 **
-** This file is part of the QtDeclarative module of the Qt Toolkit.
+** This file is part of the QtQml module of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** GNU Lesser General Public License Usage
@@ -35,6 +34,7 @@
 **
 **
 **
+**
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/
 #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/qdeclarativedebugtrace_p.h>
-#include <private/qdeclarativeinspectorservice_p.h>
+#include <private/qqmlprofilerservice_p.h>
+#include <private/qqmlinspectorservice_p.h>
+#include <private/qqmlmemoryprofiler_p.h>
 
-#include <QtDeclarative/qdeclarativeengine.h>
-#include <private/qdeclarativeengine_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 (QDeclarativeDebugService::isDebuggingEnabled())
-        QDeclarativeInspectorService::instance()->addView(q);
+    if (!engine.data()->incubationController())
+        engine.data()->setIncubationController(q->incubationController());
+
+    if (QQmlDebugService::isDebuggingEnabled())
+        QQmlInspectorService::instance()->addView(q);
 }
 
 QQuickViewPrivate::QQuickViewPrivate()
@@ -73,15 +79,18 @@ QQuickViewPrivate::QQuickViewPrivate()
 
 QQuickViewPrivate::~QQuickViewPrivate()
 {
-    if (QDeclarativeDebugService::isDebuggingEnabled())
-        QDeclarativeInspectorService::instance()->removeView(q_func());
-
-    delete root;
+    if (QQmlDebugService::isDebuggingEnabled())
+        QQmlInspectorService::instance()->removeView(q_func());
 }
 
 void QQuickViewPrivate::execute()
 {
     Q_Q(QQuickView);
+    if (!engine) {
+        qWarning() << "QQuickView: invalid qml engine.";
+        return;
+    }
+
     if (root) {
         delete root;
         root = 0;
@@ -91,11 +100,12 @@ void QQuickViewPrivate::execute()
         component = 0;
     }
     if (!source.isEmpty()) {
-        component = new QDeclarativeComponent(&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 {
-            QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
+            QObject::connect(component, SIGNAL(statusChanged(QQmlComponent::Status)),
                              q, SLOT(continueExecute()));
         }
     }
@@ -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 QDeclarativeComponent 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)
 {
@@ -216,26 +255,26 @@ QUrl QQuickView::source() const
 }
 
 /*!
-  Returns a pointer to the QDeclarativeEngine used for instantiating
+  Returns a pointer to the QQmlEngine used for instantiating
   QML Components.
  */
-QDeclarativeEngine* QQuickView::engine() const
+QQmlEngine* QQuickView::engine() const
 {
     Q_D(const QQuickView);
-    return const_cast<QDeclarativeEngine *>(&d->engine);
+    return d->engine ? const_cast<QQmlEngine *>(d->engine.data()) : 0;
 }
 
 /*!
   This function returns the root of the context hierarchy.  Each QML
-  component is instantiated in a QDeclarativeContext.  QDeclarativeContext's are
+  component is instantiated in a QQmlContext.  QQmlContext's are
   essential for passing data to QML components.  In QML, contexts are
   arranged hierarchically and this hierarchy is managed by the
-  QDeclarativeEngine.
+  QQmlEngine.
  */
-QDeclarativeContext* QQuickView::rootContext() 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 @@ QDeclarativeContext* QQuickView::rootContext() const
 QQuickView::Status QQuickView::status() const
 {
     Q_D(const QQuickView);
+    if (!d->engine)
+        return QQuickView::Error;
+
     if (!d->component)
         return QQuickView::Null;
 
@@ -275,27 +317,38 @@ QQuickView::Status QQuickView::status() const
     Return the list of errors that occurred during the last compile or create
     operation.  When the status is not Error, an empty list is returned.
 */
-QList<QDeclarativeError> QQuickView::errors() const
+QList<QQmlError> QQuickView::errors() const
 {
     Q_D(const QQuickView);
+    QList<QQmlError> errs;
+
     if (d->component)
-        return d->component->errors();
-    return QList<QDeclarativeError>();
+        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)
@@ -377,12 +430,13 @@ QQuickView::ResizeMode QQuickView::resizeMode() const
 void QQuickView::continueExecute()
 {
     Q_D(QQuickView);
-    disconnect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(continueExecute()));
+    disconnect(d->component, SIGNAL(statusChanged(QQmlComponent::Status)), this, SLOT(continueExecute()));
 
     if (d->component->isError()) {
-        QList<QDeclarativeError> errorList = d->component->errors();
-        foreach (const QDeclarativeError &error, errorList) {
-            qWarning() << error;
+        QList<QQmlError> errorList = d->component->errors();
+        foreach (const QQmlError &error, errorList) {
+            QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), 0).warning()
+                    << error;
         }
         emit statusChanged(status());
         return;
@@ -391,9 +445,10 @@ void QQuickView::continueExecute()
     QObject *obj = d->component->create();
 
     if (d->component->isError()) {
-        QList<QDeclarativeError> errorList = d->component->errors();
-        foreach (const QDeclarativeError &error, errorList) {
-            qWarning() << error;
+        QList<QQmlError> errorList = d->component->errors();
+        foreach (const QQmlError &error, errorList) {
+            QMessageLogger(error.url().toString().toLatin1().constData(), error.line(), 0).warning()
+                    << error;
         }
         emit statusChanged(status());
         return;
@@ -414,27 +469,22 @@ 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
                    << "If your example is using QML 2, (such as qmlscene) and the .qml file you" << endl
                    << "loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur." << endl
                    << endl
-                   << "To load files with 'import QtQuick 1.0' with QML 2, specify:" << endl
-                   << "  QMLSCENE_IMPORT_NAME=quick1" << endl
-                   << "on as an environment variable prior to launching the application." << endl
-                   << endl
-                   << "To load files with 'import Qt 4.7' with QML 2, specify:" << endl
-                   << "  QMLSCENE_IMPORT_NAME=qt" << endl
-                   << "on as an environment variable prior to launching the application." << endl;
+                   << "To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the" << endl
+                   << "QQuickView class in the qtquick1 module." << endl;
         delete obj;
         root = 0;
     }
     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();
@@ -471,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
 {
@@ -499,42 +553,47 @@ void QQuickView::resizeEvent(QResizeEvent *e)
     if (d->resizeMode == SizeRootObjectToView)
         d->updateSize();
 
-    QQuickCanvas::resizeEvent(e);
+    QQuickWindow::resizeEvent(e);
 }
 
+/*! \reimp */
 void QQuickView::keyPressEvent(QKeyEvent *e)
 {
-    QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
+    QQmlProfilerService::addEvent(QQmlProfilerService::Key);
 
-    QQuickCanvas::keyPressEvent(e);
+    QQuickWindow::keyPressEvent(e);
 }
 
+/*! \reimp */
 void QQuickView::keyReleaseEvent(QKeyEvent *e)
 {
-    QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
+    QQmlProfilerService::addEvent(QQmlProfilerService::Key);
 
-    QQuickCanvas::keyReleaseEvent(e);
+    QQuickWindow::keyReleaseEvent(e);
 }
 
+/*! \reimp */
 void QQuickView::mouseMoveEvent(QMouseEvent *e)
 {
-    QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
+    QQmlProfilerService::addEvent(QQmlProfilerService::Mouse);
 
-    QQuickCanvas::mouseMoveEvent(e);
+    QQuickWindow::mouseMoveEvent(e);
 }
 
+/*! \reimp */
 void QQuickView::mousePressEvent(QMouseEvent *e)
 {
-    QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
+    QQmlProfilerService::addEvent(QQmlProfilerService::Mouse);
 
-    QQuickCanvas::mousePressEvent(e);
+    QQuickWindow::mousePressEvent(e);
 }
 
+/*! \reimp */
 void QQuickView::mouseReleaseEvent(QMouseEvent *e)
 {
-    QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
+    QQmlProfilerService::addEvent(QQmlProfilerService::Mouse);
 
-    QQuickCanvas::mouseReleaseEvent(e);
+    QQuickWindow::mouseReleaseEvent(e);
 }