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()
65 QDeclarativeEnginePrivate::get(&engine)->sgContext = QSGCanvasPrivate::context;
67 QDeclarativeInspectorService::instance()->addView(q_func());
70 QSGViewPrivate::QSGViewPrivate()
71 : root(0), component(0), resizeMode(QSGView::SizeViewToRootObject), initialSize(0,0), resized(false)
75 QSGViewPrivate::~QSGViewPrivate()
77 QDeclarativeInspectorService::instance()->removeView(q_func());
82 void QSGViewPrivate::execute()
93 if (!source.isEmpty()) {
94 component = new QDeclarativeComponent(&engine, source, q);
95 if (!component->isLoading()) {
98 QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)),
99 q, SLOT(continueExecute()));
104 void QSGViewPrivate::itemGeometryChanged(QSGItem *resizeItem, const QRectF &newGeometry, const QRectF &oldGeometry)
107 if (resizeItem == root && resizeMode == QSGView::SizeViewToRootObject) {
108 // wait for both width and height to be changed
109 resizetimer.start(0,q);
111 QSGItemChangeListener::itemGeometryChanged(resizeItem, newGeometry, oldGeometry);
114 QSGView::QSGView(QWindow *parent, Qt::WindowFlags f)
115 : QSGCanvas(*(new QSGViewPrivate), parent)
121 QSGView::QSGView(const QUrl &source, QWindow *parent, Qt::WindowFlags f)
122 : QSGCanvas(*(new QSGViewPrivate), parent)
133 void QSGView::setSource(const QUrl& url)
140 QUrl QSGView::source() const
146 QDeclarativeEngine* QSGView::engine() const
149 return const_cast<QDeclarativeEngine *>(&d->engine);
152 QDeclarativeContext* QSGView::rootContext() const
155 return d->engine.rootContext();
158 QSGView::Status QSGView::status() const
162 return QSGView::Null;
164 return QSGView::Status(d->component->status());
167 QList<QDeclarativeError> QSGView::errors() const
171 return d->component->errors();
172 return QList<QDeclarativeError>();
175 void QSGView::setResizeMode(ResizeMode mode)
178 if (d->resizeMode == mode)
182 if (d->resizeMode == SizeViewToRootObject) {
183 QSGItemPrivate *p = QSGItemPrivate::get(d->root);
184 p->removeItemChangeListener(d, QSGItemPrivate::Geometry);
188 d->resizeMode = mode;
194 void QSGViewPrivate::initResize()
197 if (resizeMode == QSGView::SizeViewToRootObject) {
198 QSGItemPrivate *p = QSGItemPrivate::get(root);
199 p->addItemChangeListener(this, QSGItemPrivate::Geometry);
205 void QSGViewPrivate::updateSize()
211 if (resizeMode == QSGView::SizeViewToRootObject) {
212 QSize newSize = QSize(root->width(), root->height());
213 if (newSize.isValid() && newSize != q->size()) {
216 } else if (resizeMode == QSGView::SizeRootObjectToView) {
217 if (!qFuzzyCompare(q->width(), root->width()))
218 root->setWidth(q->width());
219 if (!qFuzzyCompare(q->height(), root->height()))
220 root->setHeight(q->height());
224 QSize QSGViewPrivate::rootObjectSize() const
226 QSize rootObjectSize(0,0);
227 int widthCandidate = -1;
228 int heightCandidate = -1;
230 widthCandidate = root->width();
231 heightCandidate = root->height();
233 if (widthCandidate > 0) {
234 rootObjectSize.setWidth(widthCandidate);
236 if (heightCandidate > 0) {
237 rootObjectSize.setHeight(heightCandidate);
239 return rootObjectSize;
242 QSGView::ResizeMode QSGView::resizeMode() const
245 return d->resizeMode;
251 void QSGView::continueExecute()
254 disconnect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(continueExecute()));
256 if (d->component->isError()) {
257 QList<QDeclarativeError> errorList = d->component->errors();
258 foreach (const QDeclarativeError &error, errorList) {
261 emit statusChanged(status());
265 QObject *obj = d->component->create();
267 if(d->component->isError()) {
268 QList<QDeclarativeError> errorList = d->component->errors();
269 foreach (const QDeclarativeError &error, errorList) {
272 emit statusChanged(status());
276 d->setRootObject(obj);
277 emit statusChanged(status());
284 void QSGViewPrivate::setRootObject(QObject *obj)
289 if (QSGItem *sgItem = qobject_cast<QSGItem *>(obj)) {
291 sgItem->setParentItem(q->QSGCanvas::rootItem());
293 qWarning() << "QSGView only supports loading of root objects that derive from QSGItem." << endl
295 << "If your example is using QML 2, (such as qmlscene) and the .qml file you" << endl
296 << "loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur." << endl
298 << "To load files with 'import QtQuick 1.0' with QML 2, specify:" << endl
299 << " QMLSCENE_IMPORT_NAME=quick1" << endl
300 << "on as an environment variable prior to launching the application." << endl
302 << "To load files with 'import Qt 4.7' with QML 2, specify:" << endl
303 << " QMLSCENE_IMPORT_NAME=qt" << endl
304 << "on as an environment variable prior to launching the application." << endl;
309 initialSize = rootObjectSize();
310 if ((resizeMode == QSGView::SizeViewToRootObject || !resized) // ### refactor: || !q->testAttribute(Qt::WA_Resized)
311 && initialSize != q->size()) {
313 q->resize(initialSize);
322 If the \l {QTimerEvent} {timer event} \a e is this
323 view's resize timer, sceneResized() is emitted.
325 void QSGView::timerEvent(QTimerEvent* e)
328 if (!e || e->timerId() == d->resizetimer.timerId()) {
330 d->resizetimer.stop();
336 Preferred size follows the root object geometry.
338 QSize QSGView::sizeHint() const
341 QSize rootObjectSize = d->rootObjectSize();
342 if (rootObjectSize.isEmpty()) {
345 return rootObjectSize;
349 QSize QSGView::initialSize() const
352 return d->initialSize;
355 QSGItem *QSGView::rootObject() const
363 This function handles the \l {QResizeEvent} {resize event}
366 void QSGView::resizeEvent(QResizeEvent *e)
369 if (d->resizeMode == SizeRootObjectToView)
372 QSGCanvas::resizeEvent(e);
375 void QSGView::keyPressEvent(QKeyEvent *e)
377 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
379 QSGCanvas::keyPressEvent(e);
382 void QSGView::keyReleaseEvent(QKeyEvent *e)
384 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
386 QSGCanvas::keyReleaseEvent(e);
389 void QSGView::mouseMoveEvent(QMouseEvent *e)
391 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
393 QSGCanvas::mouseMoveEvent(e);
396 void QSGView::mousePressEvent(QMouseEvent *e)
398 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
400 QSGCanvas::mousePressEvent(e);
403 void QSGView::mouseReleaseEvent(QMouseEvent *e)
405 QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
407 QSGCanvas::mouseReleaseEvent(e);