1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include "qsgview_p.h"
45 #include "qsgcanvas_p.h"
46 #include "qsgitem_p.h"
47 #include "qsgitemchangelistener_p.h"
49 #include <private/qdeclarativedebugtrace_p.h>
50 #include <private/qdeclarativeinspectorservice_p.h>
52 #include <QtDeclarative/qdeclarativeengine.h>
53 #include <private/qdeclarativeengine_p.h>
54 #include <QtCore/qbasictimer.h>
57 // XXX todo - This whole class should probably be merged with QDeclarativeView for
58 // maximum seamlessness
61 DEFINE_BOOL_CONFIG_OPTION(frameRateDebug, QML_SHOW_FRAMERATE)
63 void QSGViewPrivate::init()
67 QDeclarativeEnginePrivate::get(&engine)->sgContext = QSGCanvasPrivate::context;
69 engine.setIncubationController(q->incubationController());
71 if (QDeclarativeDebugService::isDebuggingEnabled())
72 QDeclarativeInspectorService::instance()->addView(q);
75 QSGViewPrivate::QSGViewPrivate()
76 : root(0), component(0), resizeMode(QSGView::SizeViewToRootObject), initialSize(0,0), resized(false)
80 QSGViewPrivate::~QSGViewPrivate()
82 if (QDeclarativeDebugService::isDebuggingEnabled())
83 QDeclarativeInspectorService::instance()->removeView(q_func());
88 void QSGViewPrivate::execute()
99 if (!source.isEmpty()) {
100 component = new QDeclarativeComponent(&engine, source, q);
101 if (!component->isLoading()) {
102 q->continueExecute();
104 QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
105 q, SLOT(continueExecute()));
110 void QSGViewPrivate::itemGeometryChanged(QSGItem *resizeItem, const QRectF &newGeometry, const QRectF &oldGeometry)
113 if (resizeItem == root && resizeMode == QSGView::SizeViewToRootObject) {
114 // wait for both width and height to be changed
115 resizetimer.start(0,q);
117 QSGItemChangeListener::itemGeometryChanged(resizeItem, newGeometry, oldGeometry);
120 QSGView::QSGView(QWindow *parent, Qt::WindowFlags f)
121 : QSGCanvas(*(new QSGViewPrivate), parent)
127 QSGView::QSGView(const QUrl &source, QWindow *parent, Qt::WindowFlags f)
128 : QSGCanvas(*(new QSGViewPrivate), parent)
139 void QSGView::setSource(const QUrl& url)
146 QUrl QSGView::source() const
152 QDeclarativeEngine* QSGView::engine() const
155 return const_cast<QDeclarativeEngine *>(&d->engine);
158 QDeclarativeContext* QSGView::rootContext() const
161 return d->engine.rootContext();
164 QSGView::Status QSGView::status() const
168 return QSGView::Null;
170 return QSGView::Status(d->component->status());
173 QList<QDeclarativeError> QSGView::errors() const
177 return d->component->errors();
178 return QList<QDeclarativeError>();
181 void QSGView::setResizeMode(ResizeMode mode)
184 if (d->resizeMode == mode)
188 if (d->resizeMode == SizeViewToRootObject) {
189 QSGItemPrivate *p = QSGItemPrivate::get(d->root);
190 p->removeItemChangeListener(d, QSGItemPrivate::Geometry);
194 d->resizeMode = mode;
200 void QSGViewPrivate::initResize()
203 if (resizeMode == QSGView::SizeViewToRootObject) {
204 QSGItemPrivate *p = QSGItemPrivate::get(root);
205 p->addItemChangeListener(this, QSGItemPrivate::Geometry);
211 void QSGViewPrivate::updateSize()
217 if (resizeMode == QSGView::SizeViewToRootObject) {
218 QSize newSize = QSize(root->width(), root->height());
219 if (newSize.isValid() && newSize != q->size()) {
222 } else if (resizeMode == QSGView::SizeRootObjectToView) {
223 if (!qFuzzyCompare(q->width(), root->width()))
224 root->setWidth(q->width());
225 if (!qFuzzyCompare(q->height(), root->height()))
226 root->setHeight(q->height());
230 QSize QSGViewPrivate::rootObjectSize() const
232 QSize rootObjectSize(0,0);
233 int widthCandidate = -1;
234 int heightCandidate = -1;
236 widthCandidate = root->width();
237 heightCandidate = root->height();
239 if (widthCandidate > 0) {
240 rootObjectSize.setWidth(widthCandidate);
242 if (heightCandidate > 0) {
243 rootObjectSize.setHeight(heightCandidate);
245 return rootObjectSize;
248 QSGView::ResizeMode QSGView::resizeMode() const
251 return d->resizeMode;
257 void QSGView::continueExecute()
260 disconnect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(continueExecute()));
262 if (d->component->isError()) {
263 QList<QDeclarativeError> errorList = d->component->errors();
264 foreach (const QDeclarativeError &error, errorList) {
267 emit statusChanged(status());
271 QObject *obj = d->component->create();
273 if (d->component->isError()) {
274 QList<QDeclarativeError> errorList = d->component->errors();
275 foreach (const QDeclarativeError &error, errorList) {
278 emit statusChanged(status());
282 d->setRootObject(obj);
283 emit statusChanged(status());
290 void QSGViewPrivate::setRootObject(QObject *obj)
295 if (QSGItem *sgItem = qobject_cast<QSGItem *>(obj)) {
297 sgItem->setParentItem(q->QSGCanvas::rootItem());
299 qWarning() << "QSGView only supports loading of root objects that derive from QSGItem." << endl
301 << "If your example is using QML 2, (such as qmlscene) and the .qml file you" << endl
302 << "loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur." << endl
304 << "To load files with 'import QtQuick 1.0' with QML 2, specify:" << endl
305 << " QMLSCENE_IMPORT_NAME=quick1" << endl
306 << "on as an environment variable prior to launching the application." << endl
308 << "To load files with 'import Qt 4.7' with QML 2, specify:" << endl
309 << " QMLSCENE_IMPORT_NAME=qt" << endl
310 << "on as an environment variable prior to launching the application." << endl;
315 initialSize = rootObjectSize();
316 if ((resizeMode == QSGView::SizeViewToRootObject || !resized) // ### refactor: || !q->testAttribute(Qt::WA_Resized)
317 && initialSize != q->size()) {
319 q->resize(initialSize);
328 If the \l {QTimerEvent} {timer event} \a e is this
329 view's resize timer, sceneResized() is emitted.
331 void QSGView::timerEvent(QTimerEvent* e)
334 if (!e || e->timerId() == d->resizetimer.timerId()) {
336 d->resizetimer.stop();
342 Preferred size follows the root object geometry.
344 QSize QSGView::sizeHint() const
347 QSize rootObjectSize = d->rootObjectSize();
348 if (rootObjectSize.isEmpty()) {
351 return rootObjectSize;
355 QSize QSGView::initialSize() const
358 return d->initialSize;
361 QSGItem *QSGView::rootObject() const
369 This function handles the \l {QResizeEvent} {resize event}
372 void QSGView::resizeEvent(QResizeEvent *e)
375 if (d->resizeMode == SizeRootObjectToView)
378 QSGCanvas::resizeEvent(e);
381 void QSGView::keyPressEvent(QKeyEvent *e)
383 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
385 QSGCanvas::keyPressEvent(e);
388 void QSGView::keyReleaseEvent(QKeyEvent *e)
390 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
392 QSGCanvas::keyReleaseEvent(e);
395 void QSGView::mouseMoveEvent(QMouseEvent *e)
397 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
399 QSGCanvas::mouseMoveEvent(e);
402 void QSGView::mousePressEvent(QMouseEvent *e)
404 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
406 QSGCanvas::mousePressEvent(e);
409 void QSGView::mouseReleaseEvent(QMouseEvent *e)
411 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
413 QSGCanvas::mouseReleaseEvent(e);