Cleaned up the qmlscene code a bit
[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 <QtCore/qdir.h>
45 #include <QtCore/qmath.h>
46 #include <QtCore/qdatetime.h>
47
48 #include <QtGui/QGuiApplication>
49
50 #include <QtDeclarative/qdeclarative.h>
51 #include <QtDeclarative/qdeclarativeengine.h>
52 #include <QtDeclarative/qdeclarativecomponent.h>
53 #include <QtDeclarative/qdeclarativecontext.h>
54
55 #include <QtQuick/qquickitem.h>
56 #include <QtQuick/qquickview.h>
57
58
59 #ifdef QML_RUNTIME_TESTING
60 class RenderStatistics
61 {
62 public:
63     static void updateStats();
64     static void printTotalStats();
65 private:
66     static QVector<qreal> timePerFrame;
67     static QVector<int> timesPerFrames;
68 };
69
70 QVector<qreal> RenderStatistics::timePerFrame;
71 QVector<int> RenderStatistics::timesPerFrames;
72
73 void RenderStatistics::updateStats()
74 {
75     static QTime time;
76     static int frames;
77     static int lastTime;
78
79     if (frames == 0) {
80         time.start();
81     } else {
82         int elapsed = time.elapsed();
83         timesPerFrames.append(elapsed - lastTime);
84         lastTime = elapsed;
85
86         if (elapsed > 5000) {
87             qreal avgtime = elapsed / (qreal) frames;
88             qreal var = 0;
89             for (int i = 0; i < timesPerFrames.size(); ++i) {
90                 qreal diff = timesPerFrames.at(i) - avgtime;
91                 var += diff * diff;
92             }
93             var /= timesPerFrames.size();
94
95             qDebug("Average time per frame: %f ms (%i fps), std.dev: %f ms", avgtime, qRound(1000. / avgtime), qSqrt(var));
96
97             timePerFrame.append(avgtime);
98             timesPerFrames.clear();
99             time.start();
100             lastTime = 0;
101             frames = 0;
102         }
103     }
104     ++frames;
105 }
106
107 void RenderStatistics::printTotalStats()
108 {
109     int count = timePerFrame.count();
110     if (count == 0)
111         return;
112
113     qreal minTime = 0;
114     qreal maxTime = 0;
115     qreal avg = 0;
116     for (int i = 0; i < count; ++i) {
117         minTime = minTime == 0 ? timePerFrame.at(i) : qMin(minTime, timePerFrame.at(i));
118         maxTime = qMax(maxTime, timePerFrame.at(i));
119         avg += timePerFrame.at(i);
120     }
121     avg /= count;
122
123     qDebug(" ");
124     qDebug("----- Statistics -----");
125     qDebug("Average time per frame: %f ms (%i fps)", avg, qRound(1000. / avg));
126     qDebug("Best time per frame: %f ms (%i fps)", minTime, int(1000 / minTime));
127     qDebug("Worst time per frame: %f ms (%i fps)", maxTime, int(1000 / maxTime));
128     qDebug("----------------------");
129     qDebug(" ");
130 }
131 #endif
132
133 class MyQQuickView : public QQuickView
134 {
135 public:
136     MyQQuickView() : QQuickView()
137     {
138         setResizeMode(QQuickView::SizeRootObjectToView);
139     }
140 };
141
142 struct Options
143 {
144     Options()
145         : originalQml(false)
146         , originalQmlRaster(false)
147         , maximized(false)
148         , fullscreen(false)
149         , clip(false)
150         , versionDetection(true)
151     {
152     }
153
154     QUrl file;
155     bool originalQml;
156     bool originalQmlRaster;
157     bool maximized;
158     bool fullscreen;
159     bool scenegraphOnGraphicsview;
160     bool clip;
161     bool versionDetection;
162     bool vsync;
163 };
164
165 #if defined(QMLSCENE_BUNDLE)
166 Q_DECLARE_METATYPE(QFileInfo);
167 QFileInfoList findQmlFiles(const QString &dirName)
168 {
169     QDir dir(dirName);
170
171     QFileInfoList ret;
172     if (dir.exists()) {
173         QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
174                                                     QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
175
176         foreach (QFileInfo fileInfo, fileInfos) {
177             if (fileInfo.isDir())
178                 ret += findQmlFiles(fileInfo.filePath());
179             else if (fileInfo.fileName().length() > 0 && fileInfo.fileName().at(0).isLower())
180                 ret.append(fileInfo);
181         }
182     }
183
184     return ret;
185 }
186
187 static int displayOptionsDialog(Options *options)
188 {
189     QDialog dialog;
190
191     QFormLayout *layout = new QFormLayout(&dialog);
192
193     QComboBox *qmlFileComboBox = new QComboBox(&dialog);
194     QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
195
196     foreach (QFileInfo fileInfo, fileInfos)
197         qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
198
199     QCheckBox *originalCheckBox = new QCheckBox(&dialog);
200     originalCheckBox->setText("Use original QML viewer");
201     originalCheckBox->setChecked(options->originalQml);
202
203     QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
204     fullscreenCheckBox->setText("Start fullscreen");
205     fullscreenCheckBox->setChecked(options->fullscreen);
206
207     QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
208     maximizedCheckBox->setText("Start maximized");
209     maximizedCheckBox->setChecked(options->maximized);
210
211     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
212                                                        Qt::Horizontal,
213                                                        &dialog);
214     QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
215     QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
216
217     layout->addRow("Qml file:", qmlFileComboBox);
218     layout->addWidget(originalCheckBox);
219     layout->addWidget(maximizedCheckBox);
220     layout->addWidget(fullscreenCheckBox);
221     layout->addWidget(buttonBox);
222
223     int result = dialog.exec();
224     if (result == QDialog::Accepted) {
225         QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
226         QFileInfo fileInfo = variant.value<QFileInfo>();
227
228         if (fileInfo.canonicalFilePath().startsWith(":"))
229             options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
230         else
231             options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
232         options->originalQml = originalCheckBox->isChecked();
233         options->maximized = maximizedCheckBox->isChecked();
234         options->fullscreen = fullscreenCheckBox->isChecked();
235     }
236     return result;
237 }
238 #endif
239
240 static void checkAndAdaptVersion(const QUrl &url)
241 {
242     if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
243         return;
244     }
245
246     QString fileName = url.toLocalFile();
247     if (fileName.isEmpty())
248         return;
249
250     QFile f(fileName);
251     if (!f.open(QFile::ReadOnly | QFile::Text)) {
252         qWarning("qmlscene: failed to check version of file '%s', could not open...",
253                  qPrintable(fileName));
254         return;
255     }
256
257     QRegExp quick1("^\\s*import +QtQuick +1\\.");
258     QRegExp quick2("^\\s*import +QtQuick +2\\.");
259     QRegExp qt47("^\\s*import +Qt +4\\.7");
260
261     QString envToWrite;
262     QString compat;
263
264     QTextStream stream(&f);
265     bool codeFound= false;
266     while (!codeFound) {
267         QString line = stream.readLine();
268         if (line.contains("{"))
269             codeFound = true;
270         if (envToWrite.isEmpty() && quick1.indexIn(line) >= 0) {
271             envToWrite = QLatin1String("quick1");
272             compat = QLatin1String("QtQuick 1.0");
273         } else if (envToWrite.isEmpty() && qt47.indexIn(line) >= 0) {
274             envToWrite = QLatin1String("qt");
275             compat = QLatin1String("Qt 4.7");
276         } else if (quick2.indexIn(line) >= 0) {
277             envToWrite.clear();
278             compat.clear();
279             break;
280         }
281     }
282
283     if (!envToWrite.isEmpty()) {
284         qWarning("qmlscene: Autodetecting compatibility import \"%s\"...", qPrintable(compat));
285         if (qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
286             qputenv("QMLSCENE_IMPORT_NAME", envToWrite.toLatin1().constData());
287     }
288 }
289
290 static void displayFileDialog(Options *options)
291 {
292 #ifdef QT_WIDGETS_LIB
293     QString fileName = QFileDialog::getOpenFileName(0, "Open QML file", QString(), "QML Files (*.qml)");
294     if (!fileName.isEmpty()) {
295         QFileInfo fi(fileName);
296         options->file = QUrl::fromLocalFile(fi.canonicalFilePath());
297     }
298 #else
299     Q_UNUSED(options);
300     qWarning("No filename specified...");
301 #endif
302 }
303
304 static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& directory)
305 {
306     QDir dir(directory+"/dummydata", "*.qml");
307     QStringList list = dir.entryList();
308     for (int i = 0; i < list.size(); ++i) {
309         QString qml = list.at(i);
310         QFile f(dir.filePath(qml));
311         f.open(QIODevice::ReadOnly);
312         QByteArray data = f.readAll();
313         QDeclarativeComponent comp(&engine);
314         comp.setData(data, QUrl());
315         QObject *dummyData = comp.create();
316
317         if(comp.isError()) {
318             QList<QDeclarativeError> errors = comp.errors();
319             foreach (const QDeclarativeError &error, errors) {
320                 qWarning() << error;
321             }
322         }
323
324         if (dummyData) {
325             qWarning() << "Loaded dummy data:" << dir.filePath(qml);
326             qml.truncate(qml.length()-4);
327             engine.rootContext()->setContextProperty(qml, dummyData);
328             dummyData->setParent(&engine);
329         }
330     }
331 }
332
333 static void usage()
334 {
335     qWarning("Usage: qmlscene [options] <filename>");
336     qWarning(" ");
337     qWarning(" options:");
338     qWarning("  --maximized ............................... run maximized");
339     qWarning("  --fullscreen .............................. run fullscreen");
340     qWarning("  --no-multisample .......................... Disable multisampling (anti-aliasing)");
341     qWarning("  --no-version-detection .................... Do not try to detect the version of the .qml file");
342
343     qWarning(" ");
344     exit(1);
345 }
346
347 int main(int argc, char ** argv)
348 {
349     Options options;
350
351     QStringList imports;
352     for (int i = 1; i < argc; ++i) {
353         if (*argv[i] != '-' && QFileInfo(QFile::decodeName(argv[i])).exists()) {
354             options.file = QUrl::fromLocalFile(argv[i]);
355         } else {
356             const QString lowerArgument = QString::fromLatin1(argv[i]).toLower();
357             if (lowerArgument == QLatin1String("--maximized"))
358                 options.maximized = true;
359             else if (lowerArgument == QLatin1String("--fullscreen"))
360                 options.fullscreen = true;
361             else if (lowerArgument == QLatin1String("--clip"))
362                 options.clip = true;
363             else if (lowerArgument == QLatin1String("--no-version-detection"))
364                 options.versionDetection = false;
365             else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
366                 imports.append(QString::fromLatin1(argv[++i]));
367             else if (lowerArgument == QLatin1String("--no-vsync-animations"))
368                 options.vsync = false;
369             else if (lowerArgument == QLatin1String("--help")
370                      || lowerArgument == QLatin1String("-help")
371                      || lowerArgument == QLatin1String("--h")
372                      || lowerArgument == QLatin1String("-h"))
373                 usage();
374         }
375     }
376
377     QGuiApplication app(argc, argv);
378     app.setApplicationName("QtQmlViewer");
379     app.setOrganizationName("Nokia");
380     app.setOrganizationDomain("nokia.com");
381
382     if (options.file.isEmpty())
383 #if defined(QMLSCENE_BUNDLE)
384         displayOptionsDialog(&options);
385 #else
386         displayFileDialog(&options);
387 #endif
388
389     QWindow *window = 0;
390     QDeclarativeEngine *engine = 0;
391
392     int exitCode = 0;
393
394     if (!options.file.isEmpty()) {
395         if (options.versionDetection)
396             checkAndAdaptVersion(options.file);
397         QQuickView *qxView = new MyQQuickView();
398         qxView->setVSyncAnimations(options.vsync);
399         engine = qxView->engine();
400         for (int i = 0; i < imports.size(); ++i)
401             engine->addImportPath(imports.at(i));
402         window = qxView;
403         if (options.file.isLocalFile()) {
404             QFileInfo fi(options.file.toLocalFile());
405             loadDummyDataFiles(*engine, fi.path());
406         }
407         qxView->setSource(options.file);
408
409         QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
410
411         window->setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
412         if (options.fullscreen)
413             window->showFullScreen();
414         else if (options.maximized)
415             window->showMaximized();
416         else
417             window->show();
418
419         exitCode = app.exec();
420
421         delete window;
422
423 #ifdef QML_RUNTIME_TESTING
424         RenderStatistics::printTotalStats();
425 #endif
426     }
427
428     return exitCode;
429 }
430