[844] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "qdeclarativeview.h"
|
---|
| 43 |
|
---|
| 44 | #include <qdeclarative.h>
|
---|
| 45 | #include <qdeclarativeitem.h>
|
---|
| 46 | #include <qdeclarativeengine.h>
|
---|
| 47 | #include <qdeclarativecontext.h>
|
---|
| 48 | #include <qdeclarativeglobal_p.h>
|
---|
| 49 | #include <qdeclarativeguard_p.h>
|
---|
| 50 |
|
---|
| 51 | #include <private/qdeclarativedebugtrace_p.h>
|
---|
| 52 |
|
---|
| 53 | #include <qscriptvalueiterator.h>
|
---|
| 54 | #include <qdebug.h>
|
---|
| 55 | #include <qtimer.h>
|
---|
| 56 | #include <qevent.h>
|
---|
| 57 | #include <qdir.h>
|
---|
| 58 | #include <qcoreapplication.h>
|
---|
| 59 | #include <qfontdatabase.h>
|
---|
| 60 | #include <qicon.h>
|
---|
| 61 | #include <qurl.h>
|
---|
| 62 | #include <qlayout.h>
|
---|
| 63 | #include <qwidget.h>
|
---|
| 64 | #include <qgraphicswidget.h>
|
---|
| 65 | #include <qbasictimer.h>
|
---|
| 66 | #include <QtCore/qabstractanimation.h>
|
---|
| 67 | #include <private/qgraphicsview_p.h>
|
---|
| 68 | #include <private/qdeclarativeitem_p.h>
|
---|
| 69 | #include <private/qabstractanimation_p.h>
|
---|
| 70 | #include <private/qdeclarativeitemchangelistener_p.h>
|
---|
| 71 |
|
---|
| 72 | QT_BEGIN_NAMESPACE
|
---|
| 73 |
|
---|
| 74 | DEFINE_BOOL_CONFIG_OPTION(frameRateDebug, QML_SHOW_FRAMERATE)
|
---|
| 75 | extern Q_GUI_EXPORT bool qt_applefontsmoothing_enabled;
|
---|
| 76 |
|
---|
| 77 | class QDeclarativeScene : public QGraphicsScene
|
---|
| 78 | {
|
---|
| 79 | public:
|
---|
| 80 | QDeclarativeScene();
|
---|
| 81 |
|
---|
| 82 | protected:
|
---|
| 83 | virtual void keyPressEvent(QKeyEvent *);
|
---|
| 84 | virtual void keyReleaseEvent(QKeyEvent *);
|
---|
| 85 |
|
---|
| 86 | virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *);
|
---|
| 87 | virtual void mousePressEvent(QGraphicsSceneMouseEvent *);
|
---|
| 88 | virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *);
|
---|
| 89 | };
|
---|
| 90 |
|
---|
| 91 | QDeclarativeScene::QDeclarativeScene()
|
---|
| 92 | {
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void QDeclarativeScene::keyPressEvent(QKeyEvent *e)
|
---|
| 96 | {
|
---|
| 97 | QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
|
---|
| 98 |
|
---|
| 99 | QGraphicsScene::keyPressEvent(e);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void QDeclarativeScene::keyReleaseEvent(QKeyEvent *e)
|
---|
| 103 | {
|
---|
| 104 | QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Key);
|
---|
| 105 |
|
---|
| 106 | QGraphicsScene::keyReleaseEvent(e);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void QDeclarativeScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
|
---|
| 110 | {
|
---|
| 111 | QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
|
---|
| 112 |
|
---|
| 113 | QGraphicsScene::mouseMoveEvent(e);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void QDeclarativeScene::mousePressEvent(QGraphicsSceneMouseEvent *e)
|
---|
| 117 | {
|
---|
| 118 | QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
|
---|
| 119 |
|
---|
| 120 | QGraphicsScene::mousePressEvent(e);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | void QDeclarativeScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
|
---|
| 124 | {
|
---|
| 125 | QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::Mouse);
|
---|
| 126 |
|
---|
| 127 | QGraphicsScene::mouseReleaseEvent(e);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | class QDeclarativeViewPrivate : public QGraphicsViewPrivate, public QDeclarativeItemChangeListener
|
---|
| 131 | {
|
---|
| 132 | Q_DECLARE_PUBLIC(QDeclarativeView)
|
---|
| 133 | public:
|
---|
| 134 | QDeclarativeViewPrivate()
|
---|
| 135 | : root(0), declarativeItemRoot(0), graphicsWidgetRoot(0), component(0), resizeMode(QDeclarativeView::SizeViewToRootObject), initialSize(0,0) {}
|
---|
| 136 | ~QDeclarativeViewPrivate() { delete root; delete engine; }
|
---|
| 137 | void execute();
|
---|
| 138 | void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry);
|
---|
| 139 | void initResize();
|
---|
| 140 | void updateSize();
|
---|
| 141 | inline QSize rootObjectSize() const;
|
---|
| 142 |
|
---|
| 143 | QDeclarativeGuard<QGraphicsObject> root;
|
---|
| 144 | QDeclarativeGuard<QDeclarativeItem> declarativeItemRoot;
|
---|
| 145 | QDeclarativeGuard<QGraphicsWidget> graphicsWidgetRoot;
|
---|
| 146 |
|
---|
| 147 | QUrl source;
|
---|
| 148 |
|
---|
| 149 | QDeclarativeEngine* engine;
|
---|
| 150 | QDeclarativeComponent *component;
|
---|
| 151 | QBasicTimer resizetimer;
|
---|
| 152 |
|
---|
| 153 | QDeclarativeView::ResizeMode resizeMode;
|
---|
| 154 | QSize initialSize;
|
---|
| 155 | QElapsedTimer frameTimer;
|
---|
| 156 |
|
---|
| 157 | void init();
|
---|
| 158 |
|
---|
| 159 | QDeclarativeScene scene;
|
---|
| 160 | };
|
---|
| 161 |
|
---|
| 162 | void QDeclarativeViewPrivate::execute()
|
---|
| 163 | {
|
---|
| 164 | Q_Q(QDeclarativeView);
|
---|
| 165 | if (root) {
|
---|
| 166 | delete root;
|
---|
| 167 | root = 0;
|
---|
| 168 | }
|
---|
| 169 | if (component) {
|
---|
| 170 | delete component;
|
---|
| 171 | component = 0;
|
---|
| 172 | }
|
---|
| 173 | if (!source.isEmpty()) {
|
---|
| 174 | component = new QDeclarativeComponent(engine, source, q);
|
---|
| 175 | if (!component->isLoading()) {
|
---|
| 176 | q->continueExecute();
|
---|
| 177 | } else {
|
---|
| 178 | QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(continueExecute()));
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void QDeclarativeViewPrivate::itemGeometryChanged(QDeclarativeItem *resizeItem, const QRectF &newGeometry, const QRectF &oldGeometry)
|
---|
| 184 | {
|
---|
| 185 | Q_Q(QDeclarativeView);
|
---|
| 186 | if (resizeItem == root && resizeMode == QDeclarativeView::SizeViewToRootObject) {
|
---|
| 187 | // wait for both width and height to be changed
|
---|
| 188 | resizetimer.start(0,q);
|
---|
| 189 | }
|
---|
| 190 | QDeclarativeItemChangeListener::itemGeometryChanged(resizeItem, newGeometry, oldGeometry);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /*!
|
---|
| 194 | \class QDeclarativeView
|
---|
| 195 | \since 4.7
|
---|
| 196 | \brief The QDeclarativeView class provides a widget for displaying a Qt Declarative user interface.
|
---|
| 197 |
|
---|
| 198 | QDeclarativeItem objects can be placed on a standard QGraphicsScene and
|
---|
| 199 | displayed with QGraphicsView. QDeclarativeView is a QGraphicsView subclass
|
---|
| 200 | provided as a convenience for displaying QML files, and connecting between
|
---|
| 201 | QML and C++ Qt objects.
|
---|
| 202 |
|
---|
| 203 | QDeclarativeView provides:
|
---|
| 204 |
|
---|
| 205 | \list
|
---|
| 206 | \o Management of QDeclarativeComponent loading and object creation
|
---|
| 207 | \o Initialization of QGraphicsView for optimal performance with QML using these settings:
|
---|
| 208 | \list
|
---|
| 209 | \o QGraphicsView::setOptimizationFlags(QGraphicsView::DontSavePainterState)
|
---|
| 210 | \o QGraphicsView::setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate)
|
---|
| 211 | \o QGraphicsScene::setItemIndexMethod(QGraphicsScene::NoIndex)
|
---|
| 212 | \endlist
|
---|
| 213 | \o Initialization of QGraphicsView for QML key handling using these settings:
|
---|
| 214 | \list
|
---|
| 215 | \o QGraphicsView::viewport()->setFocusPolicy(Qt::NoFocus)
|
---|
| 216 | \o QGraphicsView::setFocusPolicy(Qt::StrongFocus)
|
---|
| 217 | \o QGraphicsScene::setStickyFocus(true)
|
---|
| 218 | \endlist
|
---|
| 219 | \endlist
|
---|
| 220 |
|
---|
| 221 | Typical usage:
|
---|
| 222 |
|
---|
| 223 | \code
|
---|
| 224 | QDeclarativeView *view = new QDeclarativeView;
|
---|
| 225 | view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));
|
---|
| 226 | view->show();
|
---|
| 227 | \endcode
|
---|
| 228 |
|
---|
| 229 | Since QDeclarativeView is a QWidget-based class, it can be used to
|
---|
| 230 | display QML interfaces within QWidget-based GUI applications that do not
|
---|
| 231 | use the Graphics View framework.
|
---|
| 232 |
|
---|
| 233 | To receive errors related to loading and executing QML with QDeclarativeView,
|
---|
| 234 | you can connect to the statusChanged() signal and monitor for QDeclarativeView::Error.
|
---|
| 235 | The errors are available via QDeclarativeView::errors().
|
---|
| 236 |
|
---|
| 237 | \sa {Integrating QML with existing Qt UI code}, {Using QML in C++ Applications}
|
---|
| 238 | */
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | /*! \fn void QDeclarativeView::sceneResized(QSize size)
|
---|
| 242 | This signal is emitted when the view is resized to \a size.
|
---|
| 243 | */
|
---|
| 244 |
|
---|
| 245 | /*! \fn void QDeclarativeView::statusChanged(QDeclarativeView::Status status)
|
---|
| 246 | This signal is emitted when the component's current \a status changes.
|
---|
| 247 | */
|
---|
| 248 |
|
---|
| 249 | /*!
|
---|
| 250 | \fn QDeclarativeView::QDeclarativeView(QWidget *parent)
|
---|
| 251 |
|
---|
| 252 | Constructs a QDeclarativeView with the given \a parent.
|
---|
| 253 | */
|
---|
| 254 | QDeclarativeView::QDeclarativeView(QWidget *parent)
|
---|
| 255 | : QGraphicsView(*(new QDeclarativeViewPrivate), parent)
|
---|
| 256 | {
|
---|
| 257 | Q_D(QDeclarativeView);
|
---|
| 258 | setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
|
---|
| 259 | d->init();
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /*!
|
---|
| 263 | \fn QDeclarativeView::QDeclarativeView(const QUrl &source, QWidget *parent)
|
---|
| 264 |
|
---|
| 265 | Constructs a QDeclarativeView with the given QML \a source and \a parent.
|
---|
| 266 | */
|
---|
| 267 | QDeclarativeView::QDeclarativeView(const QUrl &source, QWidget *parent)
|
---|
| 268 | : QGraphicsView(*(new QDeclarativeViewPrivate), parent)
|
---|
| 269 | {
|
---|
| 270 | Q_D(QDeclarativeView);
|
---|
| 271 | setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
|
---|
| 272 | d->init();
|
---|
| 273 | setSource(source);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | void QDeclarativeViewPrivate::init()
|
---|
| 277 | {
|
---|
| 278 | Q_Q(QDeclarativeView);
|
---|
| 279 | engine = new QDeclarativeEngine();
|
---|
| 280 | q->setScene(&scene);
|
---|
| 281 |
|
---|
| 282 | q->setOptimizationFlags(QGraphicsView::DontSavePainterState);
|
---|
| 283 | q->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
| 284 | q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
| 285 | q->setFrameStyle(0);
|
---|
| 286 |
|
---|
| 287 | // These seem to give the best performance
|
---|
| 288 | q->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
|
---|
| 289 | scene.setItemIndexMethod(QGraphicsScene::NoIndex);
|
---|
| 290 | q->viewport()->setFocusPolicy(Qt::NoFocus);
|
---|
| 291 | q->setFocusPolicy(Qt::StrongFocus);
|
---|
| 292 |
|
---|
| 293 | scene.setStickyFocus(true); //### needed for correct focus handling
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | /*!
|
---|
| 297 | Destroys the view.
|
---|
| 298 | */
|
---|
| 299 | QDeclarativeView::~QDeclarativeView()
|
---|
| 300 | {
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | /*! \property QDeclarativeView::source
|
---|
| 304 | \brief The URL of the source of the QML component.
|
---|
| 305 |
|
---|
| 306 | Changing this property causes the QML component to be reloaded.
|
---|
| 307 |
|
---|
| 308 | Ensure that the URL provided is full and correct, in particular, use
|
---|
| 309 | \l QUrl::fromLocalFile() when loading a file from the local filesystem.
|
---|
| 310 | */
|
---|
| 311 |
|
---|
| 312 | /*!
|
---|
| 313 | Sets the source to the \a url, loads the QML component and instantiates it.
|
---|
| 314 |
|
---|
| 315 | Ensure that the URL provided is full and correct, in particular, use
|
---|
| 316 | \l QUrl::fromLocalFile() when loading a file from the local filesystem.
|
---|
| 317 |
|
---|
| 318 | Calling this methods multiple times with the same url will result
|
---|
| 319 | in the QML being reloaded.
|
---|
| 320 | */
|
---|
| 321 | void QDeclarativeView::setSource(const QUrl& url)
|
---|
| 322 | {
|
---|
| 323 | Q_D(QDeclarativeView);
|
---|
| 324 | d->source = url;
|
---|
| 325 | d->execute();
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /*!
|
---|
| 329 | Returns the source URL, if set.
|
---|
| 330 |
|
---|
| 331 | \sa setSource()
|
---|
| 332 | */
|
---|
| 333 | QUrl QDeclarativeView::source() const
|
---|
| 334 | {
|
---|
| 335 | Q_D(const QDeclarativeView);
|
---|
| 336 | return d->source;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | /*!
|
---|
| 340 | Returns a pointer to the QDeclarativeEngine used for instantiating
|
---|
| 341 | QML Components.
|
---|
| 342 | */
|
---|
| 343 | QDeclarativeEngine* QDeclarativeView::engine() const
|
---|
| 344 | {
|
---|
| 345 | Q_D(const QDeclarativeView);
|
---|
| 346 | return d->engine;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /*!
|
---|
| 350 | This function returns the root of the context hierarchy. Each QML
|
---|
| 351 | component is instantiated in a QDeclarativeContext. QDeclarativeContext's are
|
---|
| 352 | essential for passing data to QML components. In QML, contexts are
|
---|
| 353 | arranged hierarchically and this hierarchy is managed by the
|
---|
| 354 | QDeclarativeEngine.
|
---|
| 355 | */
|
---|
| 356 | QDeclarativeContext* QDeclarativeView::rootContext() const
|
---|
| 357 | {
|
---|
| 358 | Q_D(const QDeclarativeView);
|
---|
| 359 | return d->engine->rootContext();
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | /*!
|
---|
| 363 | \enum QDeclarativeView::Status
|
---|
| 364 | Specifies the loading status of the QDeclarativeView.
|
---|
| 365 |
|
---|
| 366 | \value Null This QDeclarativeView has no source set.
|
---|
| 367 | \value Ready This QDeclarativeView has loaded and created the QML component.
|
---|
| 368 | \value Loading This QDeclarativeView is loading network data.
|
---|
| 369 | \value Error One or more errors has occurred. Call errors() to retrieve a list
|
---|
| 370 | of errors.
|
---|
| 371 | */
|
---|
| 372 |
|
---|
| 373 | /*! \enum QDeclarativeView::ResizeMode
|
---|
| 374 |
|
---|
| 375 | This enum specifies how to resize the view.
|
---|
| 376 |
|
---|
| 377 | \value SizeViewToRootObject The view resizes with the root item in the QML.
|
---|
| 378 | \value SizeRootObjectToView The view will automatically resize the root item to the size of the view.
|
---|
| 379 | */
|
---|
| 380 |
|
---|
| 381 | /*!
|
---|
| 382 | \property QDeclarativeView::status
|
---|
| 383 | The component's current \l{QDeclarativeView::Status} {status}.
|
---|
| 384 | */
|
---|
| 385 |
|
---|
| 386 | QDeclarativeView::Status QDeclarativeView::status() const
|
---|
| 387 | {
|
---|
| 388 | Q_D(const QDeclarativeView);
|
---|
| 389 | if (!d->component)
|
---|
| 390 | return QDeclarativeView::Null;
|
---|
| 391 |
|
---|
| 392 | return QDeclarativeView::Status(d->component->status());
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | /*!
|
---|
| 396 | Return the list of errors that occurred during the last compile or create
|
---|
| 397 | operation. When the status is not Error, an empty list is returned.
|
---|
| 398 | */
|
---|
| 399 | QList<QDeclarativeError> QDeclarativeView::errors() const
|
---|
| 400 | {
|
---|
| 401 | Q_D(const QDeclarativeView);
|
---|
| 402 | if (d->component)
|
---|
| 403 | return d->component->errors();
|
---|
| 404 | return QList<QDeclarativeError>();
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | /*!
|
---|
| 408 | \property QDeclarativeView::resizeMode
|
---|
| 409 | \brief whether the view should resize the canvas contents
|
---|
| 410 |
|
---|
| 411 | If this property is set to SizeViewToRootObject (the default), the view
|
---|
| 412 | resizes with the root item in the QML.
|
---|
| 413 |
|
---|
| 414 | If this property is set to SizeRootObjectToView, the view will
|
---|
| 415 | automatically resize the root item.
|
---|
| 416 |
|
---|
| 417 | Regardless of this property, the sizeHint of the view
|
---|
| 418 | is the initial size of the root item. Note though that
|
---|
| 419 | since QML may load dynamically, that size may change.
|
---|
| 420 | */
|
---|
| 421 |
|
---|
| 422 | void QDeclarativeView::setResizeMode(ResizeMode mode)
|
---|
| 423 | {
|
---|
| 424 | Q_D(QDeclarativeView);
|
---|
| 425 | if (d->resizeMode == mode)
|
---|
| 426 | return;
|
---|
| 427 |
|
---|
| 428 | if (d->declarativeItemRoot) {
|
---|
| 429 | if (d->resizeMode == SizeViewToRootObject) {
|
---|
| 430 | QDeclarativeItemPrivate *p =
|
---|
| 431 | static_cast<QDeclarativeItemPrivate *>(QGraphicsItemPrivate::get(d->declarativeItemRoot));
|
---|
| 432 | p->removeItemChangeListener(d, QDeclarativeItemPrivate::Geometry);
|
---|
| 433 | }
|
---|
| 434 | } else if (d->graphicsWidgetRoot) {
|
---|
| 435 | if (d->resizeMode == QDeclarativeView::SizeViewToRootObject) {
|
---|
| 436 | d->graphicsWidgetRoot->removeEventFilter(this);
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | d->resizeMode = mode;
|
---|
| 441 | if (d->root) {
|
---|
| 442 | d->initResize();
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | void QDeclarativeViewPrivate::initResize()
|
---|
| 447 | {
|
---|
| 448 | Q_Q(QDeclarativeView);
|
---|
| 449 | if (declarativeItemRoot) {
|
---|
| 450 | if (resizeMode == QDeclarativeView::SizeViewToRootObject) {
|
---|
| 451 | QDeclarativeItemPrivate *p =
|
---|
| 452 | static_cast<QDeclarativeItemPrivate *>(QGraphicsItemPrivate::get(declarativeItemRoot));
|
---|
| 453 | p->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
|
---|
| 454 | }
|
---|
| 455 | } else if (graphicsWidgetRoot) {
|
---|
| 456 | if (resizeMode == QDeclarativeView::SizeViewToRootObject) {
|
---|
| 457 | graphicsWidgetRoot->installEventFilter(q);
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
| 460 | updateSize();
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | void QDeclarativeViewPrivate::updateSize()
|
---|
| 464 | {
|
---|
| 465 | Q_Q(QDeclarativeView);
|
---|
| 466 | if (!root)
|
---|
| 467 | return;
|
---|
| 468 | if (declarativeItemRoot) {
|
---|
| 469 | if (resizeMode == QDeclarativeView::SizeViewToRootObject) {
|
---|
| 470 | QSize newSize = QSize(declarativeItemRoot->width(), declarativeItemRoot->height());
|
---|
| 471 | if (newSize.isValid() && newSize != q->size()) {
|
---|
| 472 | q->resize(newSize);
|
---|
| 473 | }
|
---|
| 474 | } else if (resizeMode == QDeclarativeView::SizeRootObjectToView) {
|
---|
| 475 | if (!qFuzzyCompare(q->width(), declarativeItemRoot->width()))
|
---|
| 476 | declarativeItemRoot->setWidth(q->width());
|
---|
| 477 | if (!qFuzzyCompare(q->height(), declarativeItemRoot->height()))
|
---|
| 478 | declarativeItemRoot->setHeight(q->height());
|
---|
| 479 | }
|
---|
| 480 | } else if (graphicsWidgetRoot) {
|
---|
| 481 | if (resizeMode == QDeclarativeView::SizeViewToRootObject) {
|
---|
| 482 | QSize newSize = QSize(graphicsWidgetRoot->size().width(), graphicsWidgetRoot->size().height());
|
---|
| 483 | if (newSize.isValid() && newSize != q->size()) {
|
---|
| 484 | q->resize(newSize);
|
---|
| 485 | }
|
---|
| 486 | } else if (resizeMode == QDeclarativeView::SizeRootObjectToView) {
|
---|
| 487 | QSizeF newSize = QSize(q->size().width(), q->size().height());
|
---|
| 488 | if (newSize.isValid() && newSize != graphicsWidgetRoot->size()) {
|
---|
| 489 | graphicsWidgetRoot->resize(newSize);
|
---|
| 490 | }
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 | q->updateGeometry();
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | QSize QDeclarativeViewPrivate::rootObjectSize() const
|
---|
| 497 | {
|
---|
| 498 | QSize rootObjectSize(0,0);
|
---|
| 499 | int widthCandidate = -1;
|
---|
| 500 | int heightCandidate = -1;
|
---|
| 501 | if (root) {
|
---|
| 502 | QSizeF size = root->boundingRect().size();
|
---|
| 503 | widthCandidate = size.width();
|
---|
| 504 | heightCandidate = size.height();
|
---|
| 505 | }
|
---|
| 506 | if (widthCandidate > 0) {
|
---|
| 507 | rootObjectSize.setWidth(widthCandidate);
|
---|
| 508 | }
|
---|
| 509 | if (heightCandidate > 0) {
|
---|
| 510 | rootObjectSize.setHeight(heightCandidate);
|
---|
| 511 | }
|
---|
| 512 | return rootObjectSize;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | QDeclarativeView::ResizeMode QDeclarativeView::resizeMode() const
|
---|
| 516 | {
|
---|
| 517 | Q_D(const QDeclarativeView);
|
---|
| 518 | return d->resizeMode;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | /*!
|
---|
| 522 | \internal
|
---|
| 523 | */
|
---|
| 524 | void QDeclarativeView::continueExecute()
|
---|
| 525 | {
|
---|
| 526 | Q_D(QDeclarativeView);
|
---|
| 527 | disconnect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), this, SLOT(continueExecute()));
|
---|
| 528 |
|
---|
| 529 | if (d->component->isError()) {
|
---|
| 530 | QList<QDeclarativeError> errorList = d->component->errors();
|
---|
| 531 | foreach (const QDeclarativeError &error, errorList) {
|
---|
| 532 | qWarning() << error;
|
---|
| 533 | }
|
---|
| 534 | emit statusChanged(status());
|
---|
| 535 | return;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | QObject *obj = d->component->create();
|
---|
| 539 |
|
---|
| 540 | if(d->component->isError()) {
|
---|
| 541 | QList<QDeclarativeError> errorList = d->component->errors();
|
---|
| 542 | foreach (const QDeclarativeError &error, errorList) {
|
---|
| 543 | qWarning() << error;
|
---|
| 544 | }
|
---|
| 545 | emit statusChanged(status());
|
---|
| 546 | return;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | setRootObject(obj);
|
---|
| 550 | emit statusChanged(status());
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 |
|
---|
| 554 | /*!
|
---|
| 555 | \internal
|
---|
| 556 | */
|
---|
| 557 | void QDeclarativeView::setRootObject(QObject *obj)
|
---|
| 558 | {
|
---|
| 559 | Q_D(QDeclarativeView);
|
---|
| 560 | if (d->root == obj)
|
---|
| 561 | return;
|
---|
| 562 | if (QDeclarativeItem *declarativeItem = qobject_cast<QDeclarativeItem *>(obj)) {
|
---|
| 563 | d->scene.addItem(declarativeItem);
|
---|
| 564 | d->root = declarativeItem;
|
---|
| 565 | d->declarativeItemRoot = declarativeItem;
|
---|
| 566 | } else if (QGraphicsObject *graphicsObject = qobject_cast<QGraphicsObject *>(obj)) {
|
---|
| 567 | d->scene.addItem(graphicsObject);
|
---|
| 568 | d->root = graphicsObject;
|
---|
| 569 | if (graphicsObject->isWidget()) {
|
---|
| 570 | d->graphicsWidgetRoot = static_cast<QGraphicsWidget*>(graphicsObject);
|
---|
| 571 | } else {
|
---|
| 572 | qWarning() << "QDeclarativeView::resizeMode is not honored for components of type QGraphicsObject";
|
---|
| 573 | }
|
---|
| 574 | } else if (obj) {
|
---|
| 575 | qWarning() << "QDeclarativeView only supports loading of root objects that derive from QGraphicsObject";
|
---|
| 576 | if (QWidget* widget = qobject_cast<QWidget *>(obj)) {
|
---|
| 577 | window()->setAttribute(Qt::WA_OpaquePaintEvent, false);
|
---|
| 578 | window()->setAttribute(Qt::WA_NoSystemBackground, false);
|
---|
| 579 | if (layout() && layout()->count()) {
|
---|
| 580 | // Hide the QGraphicsView in GV mode.
|
---|
| 581 | QLayoutItem *item = layout()->itemAt(0);
|
---|
| 582 | if (item->widget())
|
---|
| 583 | item->widget()->hide();
|
---|
| 584 | }
|
---|
| 585 | widget->setParent(this);
|
---|
| 586 | if (isVisible()) {
|
---|
| 587 | widget->setVisible(true);
|
---|
| 588 | }
|
---|
| 589 | resize(widget->size());
|
---|
| 590 | }
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | if (d->root) {
|
---|
| 594 | d->initialSize = d->rootObjectSize();
|
---|
| 595 | if (d->initialSize != size()) {
|
---|
| 596 | if (!(parentWidget() && parentWidget()->layout())) {
|
---|
| 597 | resize(d->initialSize);
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 | d->initResize();
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | /*!
|
---|
| 605 | \internal
|
---|
| 606 | If the \l {QTimerEvent} {timer event} \a e is this
|
---|
| 607 | view's resize timer, sceneResized() is emitted.
|
---|
| 608 | */
|
---|
| 609 | void QDeclarativeView::timerEvent(QTimerEvent* e)
|
---|
| 610 | {
|
---|
| 611 | Q_D(QDeclarativeView);
|
---|
| 612 | if (!e || e->timerId() == d->resizetimer.timerId()) {
|
---|
| 613 | d->updateSize();
|
---|
| 614 | d->resizetimer.stop();
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | /*! \internal */
|
---|
| 619 | bool QDeclarativeView::eventFilter(QObject *watched, QEvent *e)
|
---|
| 620 | {
|
---|
| 621 | Q_D(QDeclarativeView);
|
---|
| 622 | if (watched == d->root && d->resizeMode == SizeViewToRootObject) {
|
---|
| 623 | if (d->graphicsWidgetRoot) {
|
---|
| 624 | if (e->type() == QEvent::GraphicsSceneResize) {
|
---|
| 625 | d->updateSize();
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 | }
|
---|
| 629 | return QGraphicsView::eventFilter(watched, e);
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | /*!
|
---|
| 633 | \internal
|
---|
| 634 | Preferred size follows the root object geometry.
|
---|
| 635 | */
|
---|
| 636 | QSize QDeclarativeView::sizeHint() const
|
---|
| 637 | {
|
---|
| 638 | Q_D(const QDeclarativeView);
|
---|
| 639 | QSize rootObjectSize = d->rootObjectSize();
|
---|
| 640 | if (rootObjectSize.isEmpty()) {
|
---|
| 641 | return size();
|
---|
| 642 | } else {
|
---|
| 643 | return rootObjectSize;
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | /*!
|
---|
| 648 | Returns the initial size of the root object
|
---|
| 649 | */
|
---|
| 650 | QSize QDeclarativeView::initialSize() const
|
---|
| 651 | {
|
---|
| 652 | Q_D(const QDeclarativeView);
|
---|
| 653 | return d->initialSize;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 | /*!
|
---|
| 657 | Returns the view's root \l {QGraphicsObject} {item}.
|
---|
| 658 | */
|
---|
| 659 | QGraphicsObject *QDeclarativeView::rootObject() const
|
---|
| 660 | {
|
---|
| 661 | Q_D(const QDeclarativeView);
|
---|
| 662 | return d->root;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | /*!
|
---|
| 666 | \internal
|
---|
| 667 | This function handles the \l {QResizeEvent} {resize event}
|
---|
| 668 | \a e.
|
---|
| 669 | */
|
---|
| 670 | void QDeclarativeView::resizeEvent(QResizeEvent *e)
|
---|
| 671 | {
|
---|
| 672 | Q_D(QDeclarativeView);
|
---|
| 673 | if (d->resizeMode == SizeRootObjectToView) {
|
---|
| 674 | d->updateSize();
|
---|
| 675 | }
|
---|
| 676 | if (d->declarativeItemRoot) {
|
---|
| 677 | setSceneRect(QRectF(0, 0, d->declarativeItemRoot->width(), d->declarativeItemRoot->height()));
|
---|
| 678 | } else if (d->root) {
|
---|
| 679 | setSceneRect(d->root->boundingRect());
|
---|
| 680 | } else {
|
---|
| 681 | setSceneRect(rect());
|
---|
| 682 | }
|
---|
| 683 | emit sceneResized(e->size());
|
---|
| 684 | QGraphicsView::resizeEvent(e);
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | /*!
|
---|
| 688 | \internal
|
---|
| 689 | */
|
---|
| 690 | void QDeclarativeView::paintEvent(QPaintEvent *event)
|
---|
| 691 | {
|
---|
| 692 | Q_D(QDeclarativeView);
|
---|
| 693 |
|
---|
| 694 | QDeclarativeDebugTrace::addEvent(QDeclarativeDebugTrace::FramePaint);
|
---|
| 695 | QDeclarativeDebugTrace::startRange(QDeclarativeDebugTrace::Painting);
|
---|
| 696 |
|
---|
| 697 | int time = 0;
|
---|
| 698 | if (frameRateDebug())
|
---|
| 699 | time = d->frameTimer.restart();
|
---|
| 700 |
|
---|
| 701 | #ifdef Q_WS_MAC
|
---|
| 702 | bool oldSmooth = qt_applefontsmoothing_enabled;
|
---|
| 703 | qt_applefontsmoothing_enabled = false;
|
---|
| 704 | #endif
|
---|
| 705 | QGraphicsView::paintEvent(event);
|
---|
| 706 | #ifdef Q_WS_MAC
|
---|
| 707 | qt_applefontsmoothing_enabled = oldSmooth;
|
---|
| 708 | #endif
|
---|
| 709 |
|
---|
| 710 | QDeclarativeDebugTrace::endRange(QDeclarativeDebugTrace::Painting);
|
---|
| 711 |
|
---|
| 712 | if (frameRateDebug())
|
---|
| 713 | qDebug() << "paintEvent:" << d->frameTimer.elapsed() << "time since last frame:" << time;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | QT_END_NAMESPACE
|
---|