115dace0b62570c5bb655a3b3378379aeb3bfd93
[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 bool checkVersion(const QUrl &url)
245 {
246     if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
247         qWarning("QMLSCENE_IMPORT_NAME is no longer supported.");
248     }
249
250     QString fileName = url.toLocalFile();
251     if (fileName.isEmpty()) {
252         qWarning("qmlscene: filename required.");
253         return false;
254     }
255
256     QFile f(fileName);
257     if (!f.open(QFile::ReadOnly | QFile::Text)) {
258         qWarning("qmlscene: failed to check version of file '%s', could not open...",
259                  qPrintable(fileName));
260         return false;
261     }
262
263     QRegExp quick1("^\\s*import +QtQuick +1\\.\\w*");
264     QRegExp qt47("^\\s*import +Qt +4\\.7");
265
266     QTextStream stream(&f);
267     bool codeFound= false;
268     while (!codeFound) {
269         QString line = stream.readLine();
270         if (line.contains("{")) {
271             codeFound = true;
272         } else {
273             QString import;
274             if (quick1.indexIn(line) >= 0) {
275                 import = quick1.cap(0).trimmed();
276             } else if (qt47.indexIn(line) >= 0) {
277                 import = qt47.cap(0).trimmed();
278             }
279
280             if (!import.isNull()) {
281                 qWarning("qmlscene: '%s' is no longer supported.\n"
282                          "Use qmlviewer to load file '%s'.",
283                          qPrintable(import),
284                          qPrintable(fileName));
285                 return false;
286             }
287         }
288     }
289
290     return true;
291 }
292
293 static void displayFileDialog(Options *options)
294 {
295 #ifdef QT_WIDGETS_LIB
296     QString fileName = QFileDialog::getOpenFileName(0, "Open QML file", QString(), "QML Files (*.qml)");
297     if (!fileName.isEmpty()) {
298         QFileInfo fi(fileName);
299         options->file = QUrl::fromLocalFile(fi.canonicalFilePath());
300     }
301 #else
302     Q_UNUSED(options);
303     qWarning("No filename specified...");
304 #endif
305 }
306
307 static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& directory)
308 {
309     QDir dir(directory+"/dummydata", "*.qml");
310     QStringList list = dir.entryList();
311     for (int i = 0; i < list.size(); ++i) {
312         QString qml = list.at(i);
313         QFile f(dir.filePath(qml));
314         f.open(QIODevice::ReadOnly);
315         QByteArray data = f.readAll();
316         QDeclarativeComponent comp(&engine);
317         comp.setData(data, QUrl());
318         QObject *dummyData = comp.create();
319
320         if(comp.isError()) {
321             QList<QDeclarativeError> errors = comp.errors();
322             foreach (const QDeclarativeError &error, errors) {
323                 qWarning() << error;
324             }
325         }
326
327         if (dummyData) {
328             qWarning() << "Loaded dummy data:" << dir.filePath(qml);
329             qml.truncate(qml.length()-4);
330             engine.rootContext()->setContextProperty(qml, dummyData);
331             dummyData->setParent(&engine);
332         }
333     }
334 }
335
336 static void usage()
337 {
338     qWarning("Usage: qmlscene [options] <filename>");
339     qWarning(" ");
340     qWarning(" options:");
341     qWarning("  --maximized ............................... run maximized");
342     qWarning("  --fullscreen .............................. run fullscreen");
343     qWarning("  --no-multisample .......................... Disable multisampling (anti-aliasing)");
344     qWarning("  --no-version-detection .................... Do not try to detect the version of the .qml file");
345
346     qWarning(" ");
347     exit(1);
348 }
349
350 int main(int argc, char ** argv)
351 {
352     Options options;
353
354     QStringList imports;
355     for (int i = 1; i < argc; ++i) {
356         if (*argv[i] != '-' && QFileInfo(QFile::decodeName(argv[i])).exists()) {
357             options.file = QUrl::fromLocalFile(argv[i]);
358         } else {
359             const QString lowerArgument = QString::fromLatin1(argv[i]).toLower();
360             if (lowerArgument == QLatin1String("--maximized"))
361                 options.maximized = true;
362             else if (lowerArgument == QLatin1String("--fullscreen"))
363                 options.fullscreen = true;
364             else if (lowerArgument == QLatin1String("--clip"))
365                 options.clip = true;
366             else if (lowerArgument == QLatin1String("--no-version-detection"))
367                 options.versionDetection = false;
368             else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
369                 imports.append(QString::fromLatin1(argv[++i]));
370             else if (lowerArgument == QLatin1String("--help")
371                      || lowerArgument == QLatin1String("-help")
372                      || lowerArgument == QLatin1String("--h")
373                      || lowerArgument == QLatin1String("-h"))
374                 usage();
375         }
376     }
377
378 #ifdef QT_WIDGETS_LIB
379     QApplication app(argc, argv);
380 #else
381     QGuiApplication app(argc, argv);
382 #endif
383     app.setApplicationName("QtQmlViewer");
384     app.setOrganizationName("Nokia");
385     app.setOrganizationDomain("nokia.com");
386
387     if (options.file.isEmpty())
388 #if defined(QMLSCENE_BUNDLE)
389         displayOptionsDialog(&options);
390 #else
391         displayFileDialog(&options);
392 #endif
393
394     QWindow *window = 0;
395     QDeclarativeEngine *engine = 0;
396
397     int exitCode = 0;
398
399     if (!options.file.isEmpty()) {
400         if (!options.versionDetection || checkVersion(options.file)) {
401             QQuickView *qxView = new MyQQuickView();
402             engine = qxView->engine();
403             for (int i = 0; i < imports.size(); ++i)
404                 engine->addImportPath(imports.at(i));
405             window = qxView;
406             if (options.file.isLocalFile()) {
407                 QFileInfo fi(options.file.toLocalFile());
408                 loadDummyDataFiles(*engine, fi.path());
409             }
410             qxView->setSource(options.file);
411
412             QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
413
414             window->setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
415             if (options.fullscreen)
416                 window->showFullScreen();
417             else if (options.maximized)
418                 window->showMaximized();
419             else
420                 window->show();
421
422             exitCode = app.exec();
423
424             delete window;
425
426 #ifdef QML_RUNTIME_TESTING
427             RenderStatistics::printTotalStats();
428 #endif
429         }
430     }
431
432     return exitCode;
433 }
434