Examples and fixes for QML Window properties
[profile/ivi/qtdeclarative.git] / tools / qmlscene / main.cpp
index 327dfc6..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$
@@ -62,6 +62,8 @@
 #include <QtWidgets/QFileDialog>
 #endif
 
+#include <QtCore/QTranslator>
+#include <QtCore/QLibraryInfo>
 
 #ifdef QML_RUNTIME_TESTING
 class RenderStatistics
@@ -137,15 +139,6 @@ void RenderStatistics::printTotalStats()
 }
 #endif
 
-class MyQQuickView : public QQuickView
-{
-public:
-    MyQQuickView() : QQuickView()
-    {
-        setResizeMode(QQuickView::SizeRootObjectToView);
-    }
-};
-
 struct Options
 {
     Options()
@@ -153,9 +146,12 @@ struct Options
         , originalQmlRaster(false)
         , maximized(false)
         , fullscreen(false)
+        , transparent(false)
         , clip(false)
         , versionDetection(true)
         , slowAnimations(false)
+        , quitImmediately(false)
+        , resizeViewToRootItem(false)
     {
     }
 
@@ -164,14 +160,17 @@ struct Options
     bool originalQmlRaster;
     bool maximized;
     bool fullscreen;
+    bool transparent;
     bool scenegraphOnGraphicsview;
     bool clip;
     bool versionDetection;
     bool slowAnimations;
+    bool quitImmediately;
+    bool resizeViewToRootItem;
+    QString translationFile;
 };
 
 #if defined(QMLSCENE_BUNDLE)
-Q_DECLARE_METATYPE(QFileInfo);
 QFileInfoList findQmlFiles(const QString &dirName)
 {
     QDir dir(dirName);
@@ -247,9 +246,8 @@ static int displayOptionsDialog(Options *options)
 
 static bool checkVersion(const QUrl &url)
 {
-    if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
+    if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
         qWarning("QMLSCENE_IMPORT_NAME is no longer supported.");
-    }
 
     QString fileName = url.toLocalFile();
     if (fileName.isEmpty()) {
@@ -275,11 +273,10 @@ static bool checkVersion(const QUrl &url)
             codeFound = true;
         } else {
             QString import;
-            if (quick1.indexIn(line) >= 0) {
+            if (quick1.indexIn(line) >= 0)
                 import = quick1.cap(0).trimmed();
-            } else if (qt47.indexIn(line) >= 0) {
+            else if (qt47.indexIn(line) >= 0)
                 import = qt47.cap(0).trimmed();
-            }
 
             if (!import.isNull()) {
                 qWarning("qmlscene: '%s' is no longer supported.\n"
@@ -296,7 +293,7 @@ static bool checkVersion(const QUrl &url)
 
 static void displayFileDialog(Options *options)
 {
-#ifdef QT_WIDGETS_LIB
+#if defined(QT_WIDGETS_LIB) && !defined(QT_NO_FILEDIALOG)
     QString fileName = QFileDialog::getOpenFileName(0, "Open QML file", QString(), "QML Files (*.qml)");
     if (!fileName.isEmpty()) {
         QFileInfo fi(fileName);
@@ -308,6 +305,12 @@ static void displayFileDialog(Options *options)
 #endif
 }
 
+static void loadTranslationFile(QTranslator &translator, const QString& directory)
+{
+    translator.load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n"));
+    QCoreApplication::installTranslator(&translator);
+}
+
 static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
 {
     QDir dir(directory+"/dummydata", "*.qml");
@@ -323,9 +326,8 @@ static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
 
         if(comp.isError()) {
             QList<QQmlError> errors = comp.errors();
-            foreach (const QQmlError &error, errors) {
+            foreach (const QQmlError &error, errors)
                 qWarning() << error;
-            }
         }
 
         if (dummyData) {
@@ -344,9 +346,15 @@ static void usage()
     qWarning(" options:");
     qWarning("  --maximized ............................... run maximized");
     qWarning("  --fullscreen .............................. run fullscreen");
+    qWarning("  --transparent ............................. Make the window transparent");
     qWarning("  --no-multisample .......................... Disable multisampling (anti-aliasing)");
     qWarning("  --no-version-detection .................... Do not try to detect the version of the .qml file");
     qWarning("  --slow-animations ......................... Run all animations in slow motion");
+    qWarning("  --resize-to-root .......................... Resize the window to the size of the root item");
+    qWarning("  --quit .................................... Quit immediately after starting");
+    qWarning("  -I <path> ................................. Add <path> to the list of import paths");
+    qWarning("  -B <name> <file> .......................... Add a named bundle");
+    qWarning("  -translation <translationfile> ........... set the language to run in");
 
     qWarning(" ");
     exit(1);
@@ -357,6 +365,7 @@ int main(int argc, char ** argv)
     Options options;
 
     QStringList imports;
+    QList<QPair<QString, QString> > bundles;
     for (int i = 1; i < argc; ++i) {
         if (*argv[i] != '-' && QFileInfo(QFile::decodeName(argv[i])).exists()) {
             options.file = QUrl::fromLocalFile(argv[i]);
@@ -366,15 +375,27 @@ int main(int argc, char ** argv)
                 options.maximized = true;
             else if (lowerArgument == QLatin1String("--fullscreen"))
                 options.fullscreen = true;
+            else if (lowerArgument == QLatin1String("--transparent"))
+                options.transparent = true;
             else if (lowerArgument == QLatin1String("--clip"))
                 options.clip = true;
             else if (lowerArgument == QLatin1String("--no-version-detection"))
                 options.versionDetection = false;
             else if (lowerArgument == QLatin1String("--slow-animations"))
                 options.slowAnimations = true;
+            else if (lowerArgument == QLatin1String("--quit"))
+                options.quitImmediately = true;
+           else if (lowerArgument == QLatin1String("-translation"))
+                options.translationFile = QLatin1String(argv[++i]);
+            else if (lowerArgument == QLatin1String("--resize-to-root"))
+                options.resizeViewToRootItem = true;
             else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
                 imports.append(QString::fromLatin1(argv[++i]));
-            else if (lowerArgument == QLatin1String("--help")
+            else if (lowerArgument == QLatin1String("-b") && i + 2 < argc) {
+                QString name = QString::fromLatin1(argv[++i]);
+                QString file = QString::fromLatin1(argv[++i]);
+                bundles.append(qMakePair(name, file));
+            } else if (lowerArgument == QLatin1String("--help")
                      || lowerArgument == QLatin1String("-help")
                      || lowerArgument == QLatin1String("--h")
                      || lowerArgument == QLatin1String("-h"))
@@ -388,8 +409,29 @@ 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;
+    QString sysLocale = QLocale::system().name();
+    if (translator.load(QLatin1String("qmlscene_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
+        app.installTranslator(&translator);
+        if (qtTranslator.load(QLatin1String("qt_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
+            app.installTranslator(&qtTranslator);
+        } else {
+            app.removeTranslator(&translator);
+        }
+    }
+
+    QTranslator qmlTranslator;
+    if (!options.translationFile.isEmpty()) {
+        if (qmlTranslator.load(options.translationFile)) {
+            app.installTranslator(&qmlTranslator);
+        } else {
+            qWarning() << "Could not load the translation file" << options.translationFile;
+        }
+    }
 
     QUnifiedTimer::instance()->setSlowModeEnabled(options.slowAnimations);
 
@@ -400,44 +442,90 @@ int main(int argc, char ** argv)
         displayFileDialog(&options);
 #endif
 
-    QWindow *window = 0;
-    QQmlEngine *engine = 0;
-
     int exitCode = 0;
 
     if (!options.file.isEmpty()) {
         if (!options.versionDetection || checkVersion(options.file)) {
-            QQuickView *qxView = new MyQQuickView();
-            engine = qxView->engine();
+            QTranslator translator;
+
+            // 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));
-            window = qxView;
+                engine.addImportPath(imports.at(i));
+            for (int i = 0; i < bundles.size(); ++i)
+                engine.addNamedBundle(bundles.at(i).first, bundles.at(i).second);
             if (options.file.isLocalFile()) {
                 QFileInfo fi(options.file.toLocalFile());
-                loadDummyDataFiles(*engine, fi.path());
+                loadTranslationFile(translator, fi.path());
+                loadDummyDataFiles(engine, fi.path());
+            }
+            QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
+            component->loadUrl(options.file);
+            if ( !component->isReady() ) {
+                qFatal(qPrintable(component->errorString()));
+                return -1;
             }
-            qxView->setSource(options.file);
 
-            QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
+            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);
+                }
+            }
 
-            window->setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
-            if (options.fullscreen)
-                window->showFullScreen();
-            else if (options.maximized)
-                window->showMaximized();
-            else
-                window->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();
+            }
 
-            exitCode = app.exec();
+            if (options.quitImmediately)
+                QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
 
-            delete window;
+            // 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;
         }
     }
 
     return exitCode;
 }
-