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