Examples and fixes for QML Window properties
[profile/ivi/qtdeclarative.git] / tools / qmlscene / main.cpp
index 852fa1b..d41ade4 100644 (file)
@@ -1,38 +1,38 @@
 /****************************************************************************
 **
-** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
-** Contact: http://www.qt-project.org/
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the tools applications of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
 ** GNU Lesser General Public License Usage
-** This file may be used under the terms of the GNU Lesser General Public
-** License version 2.1 as published by the Free Software Foundation and
-** appearing in the file LICENSE.LGPL included in the packaging of this
-** file. Please review the following information to ensure the GNU Lesser
-** General Public License version 2.1 requirements will be met:
-** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
-** In addition, as a special exception, Nokia gives you certain additional
-** rights. These rights are described in the Nokia Qt LGPL Exception
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU General
-** Public License version 3.0 as published by the Free Software Foundation
-** and appearing in the file LICENSE.GPL included in the packaging of this
-** file. Please review the following information to ensure the GNU General
-** Public License version 3.0 requirements will be met:
-** http://www.gnu.org/copyleft/gpl.html.
-**
-** Other Usage
-** Alternatively, this file may be used in accordance with the terms and
-** conditions contained in a signed written agreement between you and Nokia.
-**
-**
-**
-**
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
 **
 **
 ** $QT_END_LICENSE$
@@ -308,7 +308,7 @@ static void displayFileDialog(Options *options)
 static void loadTranslationFile(QTranslator &translator, const QString& directory)
 {
     translator.load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n"));
-    QApplication::installTranslator(&translator);
+    QCoreApplication::installTranslator(&translator);
 }
 
 static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
@@ -409,8 +409,8 @@ int main(int argc, char ** argv)
     QGuiApplication app(argc, argv);
 #endif
     app.setApplicationName("QtQmlViewer");
-    app.setOrganizationName("Nokia");
-    app.setOrganizationDomain("nokia.com");
+    app.setOrganizationName("Qt Project");
+    app.setOrganizationDomain("qt-project.org");
 
     QTranslator translator;
     QTranslator qtTranslator;
@@ -442,59 +442,88 @@ int main(int argc, char ** argv)
         displayFileDialog(&options);
 #endif
 
-    QQmlEngine *engine = 0;
-
     int exitCode = 0;
 
     if (!options.file.isEmpty()) {
         if (!options.versionDetection || checkVersion(options.file)) {
             QTranslator translator;
-            QQuickView qxView;
-            engine = qxView.engine();
+
+            // TODO: as soon as the engine construction completes, the debug service is
+            // listening for connections.  But actually we aren't ready to debug anything.
+            QQmlEngine engine;
+            QQmlComponent *component = new QQmlComponent(&engine);
             for (int i = 0; i < imports.size(); ++i)
-                engine->addImportPath(imports.at(i));
+                engine.addImportPath(imports.at(i));
             for (int i = 0; i < bundles.size(); ++i)
-                engine->addNamedBundle(bundles.at(i).first, bundles.at(i).second);
+                engine.addNamedBundle(bundles.at(i).first, bundles.at(i).second);
             if (options.file.isLocalFile()) {
                 QFileInfo fi(options.file.toLocalFile());
                 loadTranslationFile(translator, fi.path());
-                loadDummyDataFiles(*engine, fi.path());
+                loadDummyDataFiles(engine, fi.path());
             }
-            qxView.setSource(options.file);
-
-            QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
-
-            if (options.resizeViewToRootItem)
-                qxView.setResizeMode(QQuickView::SizeViewToRootObject);
-            else
-                qxView.setResizeMode(QQuickView::SizeRootObjectToView);
-
-            if (options.transparent) {
-                QSurfaceFormat surfaceFormat;
-                surfaceFormat.setAlphaBufferSize(8);
-                qxView.setFormat(surfaceFormat);
-                qxView.setClearBeforeRendering(true);
-                qxView.setColor(QColor(Qt::transparent));
-                qxView.setWindowFlags(Qt::FramelessWindowHint);
+            QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
+            component->loadUrl(options.file);
+            if ( !component->isReady() ) {
+                qFatal(qPrintable(component->errorString()));
+                return -1;
             }
 
-            qxView.setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
+            QObject *topLevel = component->create();
+            QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
+            QQuickView* qxView = 0;
+            if (!window) {
+                QQuickItem *contentItem = qobject_cast<QQuickItem *>(topLevel);
+                if (contentItem) {
+                    qxView = new QQuickView(&engine, NULL);
+                    window = qxView;
+                    // Set window default properties; the qml can still override them
+                    QString oname = contentItem->objectName();
+                    window->setTitle(oname.isEmpty() ? QString::fromLatin1("qmlscene") : QString::fromLatin1("qmlscene: ") + oname);
+                    window->setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
+                    if (options.resizeViewToRootItem)
+                        qxView->setResizeMode(QQuickView::SizeViewToRootObject);
+                    else
+                        qxView->setResizeMode(QQuickView::SizeRootObjectToView);
+                    qxView->setContent(options.file, component, contentItem);
+                }
+            }
 
-            if (options.fullscreen)
-                qxView.showFullScreen();
-            else if (options.maximized)
-                qxView.showMaximized();
-            else
-                qxView.show();
+            if (window) {
+                if (options.transparent) {
+                    QSurfaceFormat surfaceFormat;
+                    surfaceFormat.setAlphaBufferSize(8);
+                    window->setFormat(surfaceFormat);
+                    window->setClearBeforeRendering(true);
+                    window->setColor(QColor(Qt::transparent));
+                    window->setWindowFlags(Qt::FramelessWindowHint);
+                }
+
+                if (options.fullscreen)
+                    window->showFullScreen();
+                else if (options.maximized)
+                    window->showMaximized();
+                else
+                    window->show();
+            }
 
             if (options.quitImmediately)
                 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
 
+            // Now would be a good time to inform the debug service to start listening.
+
             exitCode = app.exec();
 
 #ifdef QML_RUNTIME_TESTING
             RenderStatistics::printTotalStats();
 #endif
+            // Ready to exit.  If we created qxView, it owns the component;
+            // otherwise, the ownership is still right here.  Nobody deletes the engine
+            // (which is odd since the container constructor takes the engine pointer),
+            // but it's stack-allocated anyway.
+            if (qxView)
+                delete qxView;
+            else
+                delete component;
         }
     }