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 QDeclarativeInspectorService::instance()->addView(q);
74 QSGViewPrivate::QSGViewPrivate()
75 : root(0), component(0), resizeMode(QSGView::SizeViewToRootObject), initialSize(0,0), resized(false)
79 QSGViewPrivate::~QSGViewPrivate()
81 QDeclarativeInspectorService::instance()->removeView(q_func());
86 void QSGViewPrivate::execute()
97 if (!source.isEmpty()) {
98 component = new QDeclarativeComponent(&engine, source, q);
99 if (!component->isLoading()) {
100 q->continueExecute();
102 QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
103 q, SLOT(continueExecute()));
108 void QSGViewPrivate::itemGeometryChanged(QSGItem *resizeItem, const QRectF &newGeometry, const QRectF &oldGeometry)
111 if (resizeItem == root && resizeMode == QSGView::SizeViewToRootObject) {
112 // wait for both width and height to be changed
113 resizetimer.start(0,q);
115 QSGItemChangeListener::itemGeometryChanged(resizeItem, newGeometry, oldGeometry);
118 QSGView::QSGView(QWindow *parent, Qt::WindowFlags f)
119 : QSGCanvas(*(new QSGViewPrivate), parent)
125 QSGView::QSGView(const QUrl &source, QWindow *parent, Qt::WindowFlags f)
126 : QSGCanvas(*(new QSGViewPrivate), parent)
137 void QSGView::setSource(const QUrl& url)
144 QUrl QSGView::source() const
150 QDeclarativeEngine* QSGView::engine() const
153 return const_cast<QDeclarativeEngine *>(&d->engine);
156 QDeclarativeContext* QSGView::rootContext() const
159 return d->engine.rootContext();
162 QSGView::Status QSGView::status() const
166 return QSGView::Null;
168 return QSGView::Status(d->component->status());
171 QList<QDeclarativeError> QSGView::errors() const
175 return d->component->errors();
176 return QList<QDeclarativeError>();
179 void QSGView::setResizeMode(ResizeMode mode)
182 if (d->resizeMode == mode)
186 if (d->resizeMode == SizeViewToRootObject) {
187 QSGItemPrivate *p = QSGItemPrivate::get(d->root);
188 p->removeItemChangeListener(d, QSGItemPrivate::Geometry);
192 d->resizeMode = mode;
198 void QSGViewPrivate::initResize()
201 if (resizeMode == QSGView::SizeViewToRootObject) {
202 QSGItemPrivate *p = QSGItemPrivate::get(root);
203 p->addItemChangeListener(this, QSGItemPrivate::Geometry);
209 void QSGViewPrivate::updateSize()
215 if (resizeMode == QSGView::SizeViewToRootObject) {
216 QSize newSize = QSize(root->width(), root->height());
217 if (newSize.isValid() && newSize != q->size()) {
220 } else if (resizeMode == QSGView::SizeRootObjectToView) {
221 if (!qFuzzyCompare(q->width(), root->width()))
222 root->setWidth(q->width());
223 if (!qFuzzyCompare(q->height(), root->height()))
224 root->setHeight(q->height());
228 QSize QSGViewPrivate::rootObjectSize() const
230 QSize rootObjectSize(0,0);
231 int widthCandidate = -1;
232 int heightCandidate = -1;
234 widthCandidate = root->width();
235 heightCandidate = root->height();
237 if (widthCandidate > 0) {
238 rootObjectSize.setWidth(widthCandidate);
240 if (heightCandidate > 0) {
241 rootObjectSize.setHeight(heightCandidate);
243 return rootObjectSize;
246 QSGView::ResizeMode QSGView::resizeMode() const
249 return d->resizeMode;
255 void QSGView::continueExecute()
258 disconnect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(continueExecute()));
260 if (d->component->isError()) {
261 QList<QDeclarativeError> errorList = d->component->errors();
262 foreach (const QDeclarativeError &error, errorList) {
265 emit statusChanged(status());
269 QObject *obj = d->component->create();
271 if (d->component->isError()) {
272 QList<QDeclarativeError> errorList = d->component->errors();
273 foreach (const QDeclarativeError &error, errorList) {
276 emit statusChanged(status());
280 d->setRootObject(obj);
281 emit statusChanged(status());
288 void QSGViewPrivate::setRootObject(QObject *obj)
293 if (QSGItem *sgItem = qobject_cast<QSGItem *>(obj)) {
295 sgItem->setParentItem(q->QSGCanvas::rootItem());
297 qWarning() << "QSGView only supports loading of root objects that derive from QSGItem." << endl
299 << "If your example is using QML 2, (such as qmlscene) and the .qml file you" << endl
300 << "loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur." << endl
302 << "To load files with 'import QtQuick 1.0' with QML 2, specify:" << endl
303 << " QMLSCENE_IMPORT_NAME=quick1" << endl
304 << "on as an environment variable prior to launching the application." << endl
306 << "To load files with 'import Qt 4.7' with QML 2, specify:" << endl
307 << " QMLSCENE_IMPORT_NAME=qt" << endl
308 << "on as an environment variable prior to launching the application." << endl;
313 initialSize = rootObjectSize();
314 if ((resizeMode == QSGView::SizeViewToRootObject || !resized) // ### refactor: || !q->testAttribute(Qt::WA_Resized)
315 && initialSize != q->size()) {
317 q->resize(initialSize);
326 If the \l {QTimerEvent} {timer event} \a e is this
327 view's resize timer, sceneResized() is emitted.
329 void QSGView::timerEvent(QTimerEvent* e)
332 if (!e || e->timerId() == d->resizetimer.timerId()) {
334 d->resizetimer.stop();
340 Preferred size follows the root object geometry.
342 QSize QSGView::sizeHint() const
345 QSize rootObjectSize = d->rootObjectSize();
346 if (rootObjectSize.isEmpty()) {
349 return rootObjectSize;
353 QSize QSGView::initialSize() const
356 return d->initialSize;
359 QSGItem *QSGView::rootObject() const
367 This function handles the \l {QResizeEvent} {resize event}
370 void QSGView::resizeEvent(QResizeEvent *e)
373 if (d->resizeMode == SizeRootObjectToView)
376 QSGCanvas::resizeEvent(e);
379 void QSGView::keyPressEvent(QKeyEvent *e)
381 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
383 QSGCanvas::keyPressEvent(e);
386 void QSGView::keyReleaseEvent(QKeyEvent *e)
388 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
390 QSGCanvas::keyReleaseEvent(e);
393 void QSGView::mouseMoveEvent(QMouseEvent *e)
395 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
397 QSGCanvas::mouseMoveEvent(e);
400 void QSGView::mousePressEvent(QMouseEvent *e)
402 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
404 QSGCanvas::mousePressEvent(e);
407 void QSGView::mouseReleaseEvent(QMouseEvent *e)
409 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
411 QSGCanvas::mouseReleaseEvent(e);