Fix compilation for QT_NO_FILEDIALOG
[profile/ivi/qtdeclarative.git] / tools / qmlscene / main.cpp
index d351b27..4e6dd92 100644 (file)
@@ -1,34 +1,34 @@
 /****************************************************************************
 **
-** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
 **
 ** This file is part of the tools applications of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
-** No Commercial Usage
-** This file contains pre-release code and may not be distributed.
-** You may use this file in accordance with the terms and conditions
-** contained in the Technology Preview License Agreement accompanying
-** this package.
-**
 ** GNU Lesser General Public License Usage
-** 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.
+** 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
+** rights. These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
-** If you have questions regarding the use of this file, please contact
-** Nokia at qt-info@nokia.com.
-**
+** 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.
 **
 **
 **
 
 #include <QtCore/qdebug.h>
 #include <QtCore/qabstractanimation.h>
-#include <QtGui/qapplication.h>
-#include <QtDeclarative/qdeclarative.h>
-#include <QtDeclarative/qdeclarativeengine.h>
-#include <QtDeclarative/qdeclarativecomponent.h>
-#include <QtDeclarative/qdeclarativeview.h>
 #include <QtCore/qdir.h>
-#include <QtGui/QFormLayout>
-#include <QtGui/QComboBox>
-#include <QtGui/QCheckBox>
-#include <QtGui/QDialog>
-#include <QtGui/QDialogButtonBox>
-#include <QtGui/QFileDialog>
-#include <QtGui/QGraphicsView>
-
-#include <QtDeclarative/qdeclarativeitem.h>
-#include <QtDeclarative/qdeclarativecontext.h>
-#include <private/qdeclarativedebughelper_p.h>
-
-// ### This should be private API
-#include <qsgitem.h>
-#include <qsgview.h>
-
-#define QT_NO_SCENEGRAPHITEM
-
-#ifndef QT_NO_SCENEGRAPHITEM
-#include "scenegraphitem.h"
+#include <QtCore/qmath.h>
+#include <QtCore/qdatetime.h>
+
+#include <QtGui/QGuiApplication>
+
+#include <QtQml/qqml.h>
+#include <QtQml/qqmlengine.h>
+#include <QtQml/qqmlcomponent.h>
+#include <QtQml/qqmlcontext.h>
+
+#include <QtQuick/qquickitem.h>
+#include <QtQuick/qquickview.h>
+
+#include <private/qabstractanimation_p.h>
+
+#ifdef QT_WIDGETS_LIB
+#include <QtWidgets/QApplication>
+#include <QtWidgets/QFileDialog>
 #endif
 
-#include <QtCore/qmath.h>
 
 #ifdef QML_RUNTIME_TESTING
 class RenderStatistics
@@ -145,103 +137,6 @@ void RenderStatistics::printTotalStats()
 }
 #endif
 
-
-static QGLFormat getFormat()
-{
-    QGLFormat f = QGLFormat::defaultFormat();
-    f.setSampleBuffers(!qApp->arguments().contains("--no-multisample"));
-    f.setSwapInterval(qApp->arguments().contains("--nonblocking-swap") ? 0 : 1);
-    f.setStereo(qApp->arguments().contains("--stereo"));
-    return f;
-}
-
-class MyQSGView : public QSGView
-{
-public:
-    MyQSGView() : QSGView(getFormat())
-    {
-        setResizeMode(QSGView::SizeRootObjectToView);
-    }
-
-protected:
-    void paintEvent(QPaintEvent *e) {
-        QSGView::paintEvent(e);
-
-#ifdef QML_RUNTIME_TESTING
-//        RenderStatistics::updateStats();
-#endif
-
-        static bool continuousUpdate = qApp->arguments().contains("--continuous-update");
-        if (continuousUpdate)
-            update();
-    }
-};
-
-class MyDeclarativeView: public QDeclarativeView
-{
-public:
-    MyDeclarativeView(QWidget *parent = 0) : QDeclarativeView(parent)
-    {
-        setResizeMode(QDeclarativeView::SizeRootObjectToView);
-    }
-
-protected:
-    void paintEvent(QPaintEvent *event)
-    {
-        QDeclarativeView::paintEvent(event);
-
-#ifdef QML_RUNTIME_TESTING
-        RenderStatistics::updateStats();
-#endif
-
-        static bool continuousUpdate = qApp->arguments().contains("--continuous-update");
-        if (continuousUpdate)
-            scene()->update();
-    }
-};
-
-#ifndef QT_NO_SCENEGRAPHITEM
-class MyGraphicsView: public QGraphicsView
-{
-public:
-    MyGraphicsView(bool clip, QWidget *parent = 0) : QGraphicsView(parent)
-    {
-        setViewport(new QGLWidget(getFormat()));
-        setScene(&scene);
-        scene.addItem(&item);
-        item.setFlag(QGraphicsItem::ItemClipsToShape, clip);
-        QGraphicsTextItem *text;
-        text = scene.addText(QLatin1String("Scene graph on graphics view."), QFont(QLatin1String("Times"), 10));
-        text->setX(5);
-        text->setY(5);
-        text->setDefaultTextColor(Qt::black);
-        text = scene.addText(QLatin1String("Scene graph on graphics view."), QFont(QLatin1String("Times"), 10));
-        text->setX(4);
-        text->setY(4);
-        text->setDefaultTextColor(Qt::yellow);
-    }
-
-    SceneGraphItem *sceneGraphItem() { return &item; }
-
-protected:
-    void paintEvent(QPaintEvent *event)
-    {
-        QGraphicsView::paintEvent(event);
-
-#ifdef QML_RUNTIME_TESTING
-        RenderStatistics::updateStats();
-#endif
-
-        static bool continuousUpdate = qApp->arguments().contains("--continuous-update");
-        if (continuousUpdate)
-            QGraphicsView::scene()->update();
-    }
-
-    QGraphicsScene scene;
-    SceneGraphItem item;
-};
-#endif
-
 struct Options
 {
     Options()
@@ -249,10 +144,11 @@ struct Options
         , originalQmlRaster(false)
         , maximized(false)
         , fullscreen(false)
-        , scenegraphOnGraphicsview(false)
         , clip(false)
         , versionDetection(true)
-        , vsync(true)
+        , slowAnimations(false)
+        , quitImmediately(false)
+        , resizeViewToRootItem(false)
     {
     }
 
@@ -264,7 +160,9 @@ struct Options
     bool scenegraphOnGraphicsview;
     bool clip;
     bool versionDetection;
-    bool vsync;
+    bool slowAnimations;
+    bool quitImmediately;
+    bool resizeViewToRootItem;
 };
 
 #if defined(QMLSCENE_BUNDLE)
@@ -342,61 +240,70 @@ static int displayOptionsDialog(Options *options)
 }
 #endif
 
-static void checkAndAdaptVersion(const QUrl &url)
+static bool checkVersion(const QUrl &url)
 {
     if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
-        return;
+        qWarning("QMLSCENE_IMPORT_NAME is no longer supported.");
     }
 
     QString fileName = url.toLocalFile();
-    if (fileName.isEmpty())
-        return;
+    if (fileName.isEmpty()) {
+        qWarning("qmlscene: filename required.");
+        return false;
+    }
 
     QFile f(fileName);
     if (!f.open(QFile::ReadOnly | QFile::Text)) {
         qWarning("qmlscene: failed to check version of file '%s', could not open...",
                  qPrintable(fileName));
-        return;
+        return false;
     }
 
-    QRegExp quick1("import +QtQuick +1\\.");
-    QRegExp qt47("import +Qt +4\\.7");
-
-    QString envToWrite;
-    QString compat;
+    QRegExp quick1("^\\s*import +QtQuick +1\\.\\w*");
+    QRegExp qt47("^\\s*import +Qt +4\\.7");
 
     QTextStream stream(&f);
     bool codeFound= false;
-    while (!codeFound && envToWrite.isEmpty()) {
+    while (!codeFound) {
         QString line = stream.readLine();
-        if (line.contains("{"))
+        if (line.contains("{")) {
             codeFound = true;
-        if (quick1.indexIn(line) >= 0) {
-            envToWrite = QLatin1String("quick1");
-            compat = QLatin1String("QtQuick 1.0");
-        } else if (qt47.indexIn(line) >= 0) {
-            envToWrite = QLatin1String("qt");
-            compat = QLatin1String("Qt 4.7");
+        } else {
+            QString import;
+            if (quick1.indexIn(line) >= 0) {
+                import = quick1.cap(0).trimmed();
+            } else if (qt47.indexIn(line) >= 0) {
+                import = qt47.cap(0).trimmed();
+            }
+
+            if (!import.isNull()) {
+                qWarning("qmlscene: '%s' is no longer supported.\n"
+                         "Use qmlviewer to load file '%s'.",
+                         qPrintable(import),
+                         qPrintable(fileName));
+                return false;
+            }
         }
     }
 
-    if (!envToWrite.isEmpty()) {
-        qWarning("qmlscene: Autodetecting compatibility import \"%s\"...", qPrintable(compat));
-        if (qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
-            qputenv("QMLSCENE_IMPORT_NAME", envToWrite.toLatin1().constData());
-    }
+    return true;
 }
 
 static void displayFileDialog(Options *options)
 {
+#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);
         options->file = QUrl::fromLocalFile(fi.canonicalFilePath());
     }
+#else
+    Q_UNUSED(options);
+    qWarning("No filename specified...");
+#endif
 }
 
-static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& directory)
+static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
 {
     QDir dir(directory+"/dummydata", "*.qml");
     QStringList list = dir.entryList();
@@ -405,13 +312,13 @@ static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& direct
         QFile f(dir.filePath(qml));
         f.open(QIODevice::ReadOnly);
         QByteArray data = f.readAll();
-        QDeclarativeComponent comp(&engine);
+        QQmlComponent comp(&engine);
         comp.setData(data, QUrl());
         QObject *dummyData = comp.create();
 
         if(comp.isError()) {
-            QList<QDeclarativeError> errors = comp.errors();
-            foreach (const QDeclarativeError &error, errors) {
+            QList<QQmlError> errors = comp.errors();
+            foreach (const QQmlError &error, errors) {
                 qWarning() << error;
             }
         }
@@ -432,17 +339,13 @@ static void usage()
     qWarning(" options:");
     qWarning("  --maximized ............................... run maximized");
     qWarning("  --fullscreen .............................. run fullscreen");
-    qWarning("  --original-qml ............................ run using QGraphicsView instead of scenegraph (OpenGL engine)");
-    qWarning("  --original-qml-raster ..................... run using QGraphicsView instead of scenegraph (Raster engine)");
     qWarning("  --no-multisample .......................... Disable multisampling (anti-aliasing)");
-    qWarning("  --continuous-update ....................... Continuously render the scene");
-    qWarning("  --nonblocking-swap ........................ Do not wait for v-sync to swap buffers");
-    qWarning("  --stereo .................................. Enable stereo on the GL context");
-#ifndef QT_NO_SCENEGRAPHITEM
-    qWarning("  --sg-on-gv [--clip] ....................... Scenegraph on graphicsview (and clip to item)");
-#endif
     qWarning("  --no-version-detection .................... Do not try to detect the version of the .qml file");
-    qWarning("  --no-vsync-animations ..................... Do not use vsync based animations");
+    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(" ");
     exit(1);
@@ -450,49 +353,54 @@ static void usage()
 
 int main(int argc, char ** argv)
 {
-#ifdef Q_WS_X11
-    QApplication::setAttribute(Qt::AA_X11InitThreads);
-#endif
-
     Options options;
 
-    QDeclarativeDebugHelper::enableDebugging();
     QStringList imports;
+    QList<QPair<QString, QString> > bundles;
     for (int i = 1; i < argc; ++i) {
-        if (*argv[i] != '-' && QFileInfo(argv[i]).exists())
+        if (*argv[i] != '-' && QFileInfo(QFile::decodeName(argv[i])).exists()) {
             options.file = QUrl::fromLocalFile(argv[i]);
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--original-qml"))
-            options.originalQml = true;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--original-qml-raster"))
-            options.originalQmlRaster = true;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--maximized"))
-            options.maximized = true;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--fullscreen"))
-            options.fullscreen = true;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--sg-on-gv"))
-            options.scenegraphOnGraphicsview = true;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--clip"))
-            options.clip = true;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--no-version-detection"))
-            options.versionDetection = false;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("-i") && i + 1 < argc)
-            imports.append(QString::fromLatin1(argv[++i]));
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--no-vsync-animations"))
-            options.vsync = false;
-        else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--help")
-                 || QString::fromLatin1(argv[i]).toLower() == QLatin1String("-help")
-                 || QString::fromLatin1(argv[i]).toLower() == QLatin1String("--h")
-                 || QString::fromLatin1(argv[i]).toLower() == QLatin1String("-h"))
-            usage();
+        } else {
+            const QString lowerArgument = QString::fromLatin1(argv[i]).toLower();
+            if (lowerArgument == QLatin1String("--maximized"))
+                options.maximized = true;
+            else if (lowerArgument == QLatin1String("--fullscreen"))
+                options.fullscreen = 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("--resize-to-root"))
+                options.resizeViewToRootItem = true;
+            else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
+                imports.append(QString::fromLatin1(argv[++i]));
+            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"))
+                usage();
+        }
     }
 
-    QApplication::setGraphicsSystem("raster");
-
+#ifdef QT_WIDGETS_LIB
     QApplication app(argc, argv);
+#else
+    QGuiApplication app(argc, argv);
+#endif
     app.setApplicationName("QtQmlViewer");
     app.setOrganizationName("Nokia");
     app.setOrganizationDomain("nokia.com");
 
+    QUnifiedTimer::instance()->setSlowModeEnabled(options.slowAnimations);
+
     if (options.file.isEmpty())
 #if defined(QMLSCENE_BUNDLE)
         displayOptionsDialog(&options);
@@ -500,79 +408,53 @@ int main(int argc, char ** argv)
         displayFileDialog(&options);
 #endif
 
-    QWidget *view = 0;
-    QDeclarativeEngine *engine = 0;
+    QWindow *window = 0;
+    QQmlEngine *engine = 0;
 
     int exitCode = 0;
 
     if (!options.file.isEmpty()) {
-#ifndef QT_NO_SCENEGRAPHITEM
-        if (options.scenegraphOnGraphicsview) {
-            MyGraphicsView *gvView = new MyGraphicsView(options.clip);
-            SceneGraphItem *item = gvView->sceneGraphItem();
-            engine = item->engine();
-            for (int i = 0; i < imports.size(); ++i)
-                engine->addImportPath(imports.at(i));
-            view = gvView;
-            if (options.file.isLocalFile()) {
-                QFileInfo fi(options.file.toLocalFile());
-                loadDummyDataFiles(*engine, fi.path());
-            }
-            item->setSource(options.file);
-        } else
-#endif
-        if (!options.originalQml && !options.originalQmlRaster) {
-            if (options.versionDetection)
-                checkAndAdaptVersion(options.file);
-            QSGView *qxView = new MyQSGView();
-            qxView->setVSyncAnimations(options.vsync);
+        if (!options.versionDetection || checkVersion(options.file)) {
+            QQuickView *qxView = new QQuickView();
             engine = qxView->engine();
             for (int i = 0; i < imports.size(); ++i)
                 engine->addImportPath(imports.at(i));
-            view = qxView;
+            for (int i = 0; i < bundles.size(); ++i)
+                engine->addNamedBundle(bundles.at(i).first, bundles.at(i).second);
+            window = qxView;
             if (options.file.isLocalFile()) {
                 QFileInfo fi(options.file.toLocalFile());
                 loadDummyDataFiles(*engine, fi.path());
             }
             qxView->setSource(options.file);
 
-        } else {
-            MyDeclarativeView *gvView = new MyDeclarativeView();
-            engine = gvView->engine();
-            for (int i = 0; i < imports.size(); ++i)
-                engine->addImportPath(imports.at(i));
-            view = gvView;
-            if (options.file.isLocalFile()) {
-                QFileInfo fi(options.file.toLocalFile());
-                loadDummyDataFiles(*engine, fi.path());
-            }
-            gvView->setSource(options.file);
-            if (!options.originalQmlRaster) {
-                QGLWidget *viewport = new QGLWidget(getFormat());
-                gvView->setViewport(viewport);
-            }
-        }
+            QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
 
-        QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
+            if (options.resizeViewToRootItem)
+                qxView->setResizeMode(QQuickView::SizeViewToRootObject);
+            else
+                qxView->setResizeMode(QQuickView::SizeRootObjectToView);
 
-        if (options.fullscreen)
-            view->showFullScreen();
-        else if (options.maximized)
-            view->showMaximized();
-        else
-            view->show();
+            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();
 
-#ifdef Q_WS_MAC
-        view->raise();
-#endif
+            if (options.quitImmediately) {
+                QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
+            }
 
-        exitCode = app.exec();
+            exitCode = app.exec();
 
-        delete view;
+            delete window;
 
 #ifdef QML_RUNTIME_TESTING
-        RenderStatistics::printTotalStats();
+            RenderStatistics::printTotalStats();
 #endif
+        }
     }
 
     return exitCode;