Don't enable qmlscene compability mode for commented out imports.
[profile/ivi/qtdeclarative.git] / tools / qmlscene / main.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 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 tools applications of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtCore/qdebug.h>
43 #include <QtCore/qabstractanimation.h>
44 #include <QtGui/qapplication.h>
45 #include <QtDeclarative/qdeclarative.h>
46 #include <QtDeclarative/qdeclarativeengine.h>
47 #include <QtDeclarative/qdeclarativecomponent.h>
48 #include <QtQuick1/qdeclarativeview.h>
49 #include <QtCore/qdir.h>
50 #include <QtGui/QFormLayout>
51 #include <QtGui/QComboBox>
52 #include <QtGui/QCheckBox>
53 #include <QtGui/QDialog>
54 #include <QtGui/QDialogButtonBox>
55 #include <QtGui/QFileDialog>
56 #include <QtGui/QGraphicsView>
57
58 #include <QtDeclarative/qdeclarativecontext.h>
59 #include <private/qdeclarativedebughelper_p.h>
60
61 // ### This should be private API
62 #include <qsgitem.h>
63 #include <qsgview.h>
64
65 #define QT_NO_SCENEGRAPHITEM
66
67 #ifndef QT_NO_SCENEGRAPHITEM
68 #include "scenegraphitem.h"
69 #endif
70
71 #include <QtCore/qmath.h>
72
73 #ifdef QML_RUNTIME_TESTING
74 class RenderStatistics
75 {
76 public:
77     static void updateStats();
78     static void printTotalStats();
79 private:
80     static QVector<qreal> timePerFrame;
81     static QVector<int> timesPerFrames;
82 };
83
84 QVector<qreal> RenderStatistics::timePerFrame;
85 QVector<int> RenderStatistics::timesPerFrames;
86
87 void RenderStatistics::updateStats()
88 {
89     static QTime time;
90     static int frames;
91     static int lastTime;
92
93     if (frames == 0) {
94         time.start();
95     } else {
96         int elapsed = time.elapsed();
97         timesPerFrames.append(elapsed - lastTime);
98         lastTime = elapsed;
99
100         if (elapsed > 5000) {
101             qreal avgtime = elapsed / (qreal) frames;
102             qreal var = 0;
103             for (int i = 0; i < timesPerFrames.size(); ++i) {
104                 qreal diff = timesPerFrames.at(i) - avgtime;
105                 var += diff * diff;
106             }
107             var /= timesPerFrames.size();
108
109             qDebug("Average time per frame: %f ms (%i fps), std.dev: %f ms", avgtime, qRound(1000. / avgtime), qSqrt(var));
110
111             timePerFrame.append(avgtime);
112             timesPerFrames.clear();
113             time.start();
114             lastTime = 0;
115             frames = 0;
116         }
117     }
118     ++frames;
119 }
120
121 void RenderStatistics::printTotalStats()
122 {
123     int count = timePerFrame.count();
124     if (count == 0)
125         return;
126
127     qreal minTime = 0;
128     qreal maxTime = 0;
129     qreal avg = 0;
130     for (int i = 0; i < count; ++i) {
131         minTime = minTime == 0 ? timePerFrame.at(i) : qMin(minTime, timePerFrame.at(i));
132         maxTime = qMax(maxTime, timePerFrame.at(i));
133         avg += timePerFrame.at(i);
134     }
135     avg /= count;
136
137     qDebug(" ");
138     qDebug("----- Statistics -----");
139     qDebug("Average time per frame: %f ms (%i fps)", avg, qRound(1000. / avg));
140     qDebug("Best time per frame: %f ms (%i fps)", minTime, int(1000 / minTime));
141     qDebug("Worst time per frame: %f ms (%i fps)", maxTime, int(1000 / maxTime));
142     qDebug("----------------------");
143     qDebug(" ");
144 }
145 #endif
146
147
148 static QGLFormat getFormat()
149 {
150     QGLFormat f = QGLFormat::defaultFormat();
151     f.setSampleBuffers(!qApp->arguments().contains("--no-multisample"));
152     f.setSwapInterval(qApp->arguments().contains("--nonblocking-swap") ? 0 : 1);
153     f.setStereo(qApp->arguments().contains("--stereo"));
154     return f;
155 }
156
157 class MyQSGView : public QSGView
158 {
159 public:
160     MyQSGView() : QSGView(getFormat())
161     {
162         setResizeMode(QSGView::SizeRootObjectToView);
163     }
164
165 protected:
166     void paintEvent(QPaintEvent *e) {
167         QSGView::paintEvent(e);
168
169 #ifdef QML_RUNTIME_TESTING
170 //        RenderStatistics::updateStats();
171 #endif
172
173         static bool continuousUpdate = qApp->arguments().contains("--continuous-update");
174         if (continuousUpdate)
175             update();
176     }
177 };
178
179 class MyDeclarativeView: public QDeclarativeView
180 {
181 public:
182     MyDeclarativeView(QWidget *parent = 0) : QDeclarativeView(parent)
183     {
184         setResizeMode(QDeclarativeView::SizeRootObjectToView);
185     }
186
187 protected:
188     void paintEvent(QPaintEvent *event)
189     {
190         QDeclarativeView::paintEvent(event);
191
192 #ifdef QML_RUNTIME_TESTING
193         RenderStatistics::updateStats();
194 #endif
195
196         static bool continuousUpdate = qApp->arguments().contains("--continuous-update");
197         if (continuousUpdate)
198             scene()->update();
199     }
200 };
201
202 #ifndef QT_NO_SCENEGRAPHITEM
203 class MyGraphicsView: public QGraphicsView
204 {
205 public:
206     MyGraphicsView(bool clip, QWidget *parent = 0) : QGraphicsView(parent)
207     {
208         setViewport(new QGLWidget(getFormat()));
209         setScene(&scene);
210         scene.addItem(&item);
211         item.setFlag(QGraphicsItem::ItemClipsToShape, clip);
212         QGraphicsTextItem *text;
213         text = scene.addText(QLatin1String("Scene graph on graphics view."), QFont(QLatin1String("Times"), 10));
214         text->setX(5);
215         text->setY(5);
216         text->setDefaultTextColor(Qt::black);
217         text = scene.addText(QLatin1String("Scene graph on graphics view."), QFont(QLatin1String("Times"), 10));
218         text->setX(4);
219         text->setY(4);
220         text->setDefaultTextColor(Qt::yellow);
221     }
222
223     SceneGraphItem *sceneGraphItem() { return &item; }
224
225 protected:
226     void paintEvent(QPaintEvent *event)
227     {
228         QGraphicsView::paintEvent(event);
229
230 #ifdef QML_RUNTIME_TESTING
231         RenderStatistics::updateStats();
232 #endif
233
234         static bool continuousUpdate = qApp->arguments().contains("--continuous-update");
235         if (continuousUpdate)
236             QGraphicsView::scene()->update();
237     }
238
239     QGraphicsScene scene;
240     SceneGraphItem item;
241 };
242 #endif
243
244 struct Options
245 {
246     Options()
247         : originalQml(false)
248         , originalQmlRaster(false)
249         , maximized(false)
250         , fullscreen(false)
251         , scenegraphOnGraphicsview(false)
252         , clip(false)
253         , versionDetection(true)
254         , vsync(true)
255     {
256     }
257
258     QUrl file;
259     bool originalQml;
260     bool originalQmlRaster;
261     bool maximized;
262     bool fullscreen;
263     bool scenegraphOnGraphicsview;
264     bool clip;
265     bool versionDetection;
266     bool vsync;
267 };
268
269 #if defined(QMLSCENE_BUNDLE)
270 Q_DECLARE_METATYPE(QFileInfo);
271 QFileInfoList findQmlFiles(const QString &dirName)
272 {
273     QDir dir(dirName);
274
275     QFileInfoList ret;
276     if (dir.exists()) {
277         QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
278                                                     QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
279
280         foreach (QFileInfo fileInfo, fileInfos) {
281             if (fileInfo.isDir())
282                 ret += findQmlFiles(fileInfo.filePath());
283             else if (fileInfo.fileName().length() > 0 && fileInfo.fileName().at(0).isLower())
284                 ret.append(fileInfo);
285         }
286     }
287
288     return ret;
289 }
290
291 static int displayOptionsDialog(Options *options)
292 {
293     QDialog dialog;
294
295     QFormLayout *layout = new QFormLayout(&dialog);
296
297     QComboBox *qmlFileComboBox = new QComboBox(&dialog);
298     QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
299
300     foreach (QFileInfo fileInfo, fileInfos)
301         qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
302
303     QCheckBox *originalCheckBox = new QCheckBox(&dialog);
304     originalCheckBox->setText("Use original QML viewer");
305     originalCheckBox->setChecked(options->originalQml);
306
307     QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
308     fullscreenCheckBox->setText("Start fullscreen");
309     fullscreenCheckBox->setChecked(options->fullscreen);
310
311     QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
312     maximizedCheckBox->setText("Start maximized");
313     maximizedCheckBox->setChecked(options->maximized);
314
315     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
316                                                        Qt::Horizontal,
317                                                        &dialog);
318     QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
319     QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
320
321     layout->addRow("Qml file:", qmlFileComboBox);
322     layout->addWidget(originalCheckBox);
323     layout->addWidget(maximizedCheckBox);
324     layout->addWidget(fullscreenCheckBox);
325     layout->addWidget(buttonBox);
326
327     int result = dialog.exec();
328     if (result == QDialog::Accepted) {
329         QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
330         QFileInfo fileInfo = variant.value<QFileInfo>();
331
332         if (fileInfo.canonicalFilePath().startsWith(":"))
333             options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
334         else
335             options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
336         options->originalQml = originalCheckBox->isChecked();
337         options->maximized = maximizedCheckBox->isChecked();
338         options->fullscreen = fullscreenCheckBox->isChecked();
339     }
340     return result;
341 }
342 #endif
343
344 static void checkAndAdaptVersion(const QUrl &url)
345 {
346     if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
347         return;
348     }
349
350     QString fileName = url.toLocalFile();
351     if (fileName.isEmpty())
352         return;
353
354     QFile f(fileName);
355     if (!f.open(QFile::ReadOnly | QFile::Text)) {
356         qWarning("qmlscene: failed to check version of file '%s', could not open...",
357                  qPrintable(fileName));
358         return;
359     }
360
361     QRegExp quick1("^\\s*import +QtQuick +1\\.");
362     QRegExp qt47("^\\s*import +Qt +4\\.7");
363
364     QString envToWrite;
365     QString compat;
366
367     QTextStream stream(&f);
368     bool codeFound= false;
369     while (!codeFound && envToWrite.isEmpty()) {
370         QString line = stream.readLine();
371         if (line.contains("{"))
372             codeFound = true;
373         if (quick1.indexIn(line) >= 0) {
374             envToWrite = QLatin1String("quick1");
375             compat = QLatin1String("QtQuick 1.0");
376         } else if (qt47.indexIn(line) >= 0) {
377             envToWrite = QLatin1String("qt");
378             compat = QLatin1String("Qt 4.7");
379         }
380     }
381
382     if (!envToWrite.isEmpty()) {
383         qWarning("qmlscene: Autodetecting compatibility import \"%s\"...", qPrintable(compat));
384         if (qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
385             qputenv("QMLSCENE_IMPORT_NAME", envToWrite.toLatin1().constData());
386     }
387 }
388
389 static void displayFileDialog(Options *options)
390 {
391     QString fileName = QFileDialog::getOpenFileName(0, "Open QML file", QString(), "QML Files (*.qml)");
392     if (!fileName.isEmpty()) {
393         QFileInfo fi(fileName);
394         options->file = QUrl::fromLocalFile(fi.canonicalFilePath());
395     }
396 }
397
398 static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& directory)
399 {
400     QDir dir(directory+"/dummydata", "*.qml");
401     QStringList list = dir.entryList();
402     for (int i = 0; i < list.size(); ++i) {
403         QString qml = list.at(i);
404         QFile f(dir.filePath(qml));
405         f.open(QIODevice::ReadOnly);
406         QByteArray data = f.readAll();
407         QDeclarativeComponent comp(&engine);
408         comp.setData(data, QUrl());
409         QObject *dummyData = comp.create();
410
411         if(comp.isError()) {
412             QList<QDeclarativeError> errors = comp.errors();
413             foreach (const QDeclarativeError &error, errors) {
414                 qWarning() << error;
415             }
416         }
417
418         if (dummyData) {
419             qWarning() << "Loaded dummy data:" << dir.filePath(qml);
420             qml.truncate(qml.length()-4);
421             engine.rootContext()->setContextProperty(qml, dummyData);
422             dummyData->setParent(&engine);
423         }
424     }
425 }
426
427 static void usage()
428 {
429     qWarning("Usage: qmlscene [options] <filename>");
430     qWarning(" ");
431     qWarning(" options:");
432     qWarning("  --maximized ............................... run maximized");
433     qWarning("  --fullscreen .............................. run fullscreen");
434     qWarning("  --original-qml ............................ run using QGraphicsView instead of scenegraph (OpenGL engine)");
435     qWarning("  --original-qml-raster ..................... run using QGraphicsView instead of scenegraph (Raster engine)");
436     qWarning("  --no-multisample .......................... Disable multisampling (anti-aliasing)");
437     qWarning("  --continuous-update ....................... Continuously render the scene");
438     qWarning("  --nonblocking-swap ........................ Do not wait for v-sync to swap buffers");
439     qWarning("  --stereo .................................. Enable stereo on the GL context");
440 #ifndef QT_NO_SCENEGRAPHITEM
441     qWarning("  --sg-on-gv [--clip] ....................... Scenegraph on graphicsview (and clip to item)");
442 #endif
443     qWarning("  --no-version-detection .................... Do not try to detect the version of the .qml file");
444     qWarning("  --no-vsync-animations ..................... Do not use vsync based animations");
445
446     qWarning(" ");
447     exit(1);
448 }
449
450 int main(int argc, char ** argv)
451 {
452 #ifdef Q_WS_X11
453     QApplication::setAttribute(Qt::AA_X11InitThreads);
454 #endif
455
456     Options options;
457
458     QDeclarativeDebugHelper::enableDebugging();
459     QStringList imports;
460     for (int i = 1; i < argc; ++i) {
461         if (*argv[i] != '-' && QFileInfo(argv[i]).exists())
462             options.file = QUrl::fromLocalFile(argv[i]);
463         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--original-qml"))
464             options.originalQml = true;
465         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--original-qml-raster"))
466             options.originalQmlRaster = true;
467         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--maximized"))
468             options.maximized = true;
469         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--fullscreen"))
470             options.fullscreen = true;
471         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--sg-on-gv"))
472             options.scenegraphOnGraphicsview = true;
473         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--clip"))
474             options.clip = true;
475         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--no-version-detection"))
476             options.versionDetection = false;
477         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("-i") && i + 1 < argc)
478             imports.append(QString::fromLatin1(argv[++i]));
479         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--no-vsync-animations"))
480             options.vsync = false;
481         else if (QString::fromLatin1(argv[i]).toLower() == QLatin1String("--help")
482                  || QString::fromLatin1(argv[i]).toLower() == QLatin1String("-help")
483                  || QString::fromLatin1(argv[i]).toLower() == QLatin1String("--h")
484                  || QString::fromLatin1(argv[i]).toLower() == QLatin1String("-h"))
485             usage();
486     }
487
488     QApplication::setGraphicsSystem("raster");
489
490     QApplication app(argc, argv);
491     app.setApplicationName("QtQmlViewer");
492     app.setOrganizationName("Nokia");
493     app.setOrganizationDomain("nokia.com");
494
495     if (options.file.isEmpty())
496 #if defined(QMLSCENE_BUNDLE)
497         displayOptionsDialog(&options);
498 #else
499         displayFileDialog(&options);
500 #endif
501
502     QWidget *view = 0;
503     QDeclarativeEngine *engine = 0;
504
505     int exitCode = 0;
506
507     if (!options.file.isEmpty()) {
508 #ifndef QT_NO_SCENEGRAPHITEM
509         if (options.scenegraphOnGraphicsview) {
510             MyGraphicsView *gvView = new MyGraphicsView(options.clip);
511             SceneGraphItem *item = gvView->sceneGraphItem();
512             engine = item->engine();
513             for (int i = 0; i < imports.size(); ++i)
514                 engine->addImportPath(imports.at(i));
515             view = gvView;
516             if (options.file.isLocalFile()) {
517                 QFileInfo fi(options.file.toLocalFile());
518                 loadDummyDataFiles(*engine, fi.path());
519             }
520             item->setSource(options.file);
521         } else
522 #endif
523         if (!options.originalQml && !options.originalQmlRaster) {
524             if (options.versionDetection)
525                 checkAndAdaptVersion(options.file);
526             QSGView *qxView = new MyQSGView();
527             qxView->setVSyncAnimations(options.vsync);
528             engine = qxView->engine();
529             for (int i = 0; i < imports.size(); ++i)
530                 engine->addImportPath(imports.at(i));
531             view = qxView;
532             if (options.file.isLocalFile()) {
533                 QFileInfo fi(options.file.toLocalFile());
534                 loadDummyDataFiles(*engine, fi.path());
535             }
536             qxView->setSource(options.file);
537
538         } else {
539             MyDeclarativeView *gvView = new MyDeclarativeView();
540             engine = gvView->engine();
541             for (int i = 0; i < imports.size(); ++i)
542                 engine->addImportPath(imports.at(i));
543             view = gvView;
544             if (options.file.isLocalFile()) {
545                 QFileInfo fi(options.file.toLocalFile());
546                 loadDummyDataFiles(*engine, fi.path());
547             }
548             gvView->setSource(options.file);
549             if (!options.originalQmlRaster) {
550                 QGLWidget *viewport = new QGLWidget(getFormat());
551                 gvView->setViewport(viewport);
552             }
553         }
554
555         QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
556
557         if (options.fullscreen)
558             view->showFullScreen();
559         else if (options.maximized)
560             view->showMaximized();
561         else
562             view->show();
563
564 #ifdef Q_WS_MAC
565         view->raise();
566 #endif
567
568         exitCode = app.exec();
569
570         delete view;
571
572 #ifdef QML_RUNTIME_TESTING
573         RenderStatistics::printTotalStats();
574 #endif
575     }
576
577     return exitCode;
578 }
579