1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the tools applications of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
48 #include <QtGui/QGuiApplication>
50 #include <QtQml/qqml.h>
51 #include <QtQml/qqmlengine.h>
52 #include <QtQml/qqmlcomponent.h>
53 #include <QtQml/qqmlcontext.h>
55 #include <QtQuick/qquickitem.h>
56 #include <QtQuick/qquickview.h>
58 #include <private/qabstractanimation_p.h>
61 #include <QtWidgets/QApplication>
62 #include <QtWidgets/QFileDialog>
66 #ifdef QML_RUNTIME_TESTING
67 class RenderStatistics
70 static void updateStats();
71 static void printTotalStats();
73 static QVector<qreal> timePerFrame;
74 static QVector<int> timesPerFrames;
77 QVector<qreal> RenderStatistics::timePerFrame;
78 QVector<int> RenderStatistics::timesPerFrames;
80 void RenderStatistics::updateStats()
89 int elapsed = time.elapsed();
90 timesPerFrames.append(elapsed - lastTime);
94 qreal avgtime = elapsed / (qreal) frames;
96 for (int i = 0; i < timesPerFrames.size(); ++i) {
97 qreal diff = timesPerFrames.at(i) - avgtime;
100 var /= timesPerFrames.size();
102 qDebug("Average time per frame: %f ms (%i fps), std.dev: %f ms", avgtime, qRound(1000. / avgtime), qSqrt(var));
104 timePerFrame.append(avgtime);
105 timesPerFrames.clear();
114 void RenderStatistics::printTotalStats()
116 int count = timePerFrame.count();
123 for (int i = 0; i < count; ++i) {
124 minTime = minTime == 0 ? timePerFrame.at(i) : qMin(minTime, timePerFrame.at(i));
125 maxTime = qMax(maxTime, timePerFrame.at(i));
126 avg += timePerFrame.at(i);
131 qDebug("----- Statistics -----");
132 qDebug("Average time per frame: %f ms (%i fps)", avg, qRound(1000. / avg));
133 qDebug("Best time per frame: %f ms (%i fps)", minTime, int(1000 / minTime));
134 qDebug("Worst time per frame: %f ms (%i fps)", maxTime, int(1000 / maxTime));
135 qDebug("----------------------");
144 , originalQmlRaster(false)
149 , versionDetection(true)
150 , slowAnimations(false)
151 , quitImmediately(false)
152 , resizeViewToRootItem(false)
158 bool originalQmlRaster;
162 bool scenegraphOnGraphicsview;
164 bool versionDetection;
166 bool quitImmediately;
167 bool resizeViewToRootItem;
170 #if defined(QMLSCENE_BUNDLE)
171 Q_DECLARE_METATYPE(QFileInfo);
172 QFileInfoList findQmlFiles(const QString &dirName)
178 QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
179 QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
181 foreach (QFileInfo fileInfo, fileInfos) {
182 if (fileInfo.isDir())
183 ret += findQmlFiles(fileInfo.filePath());
184 else if (fileInfo.fileName().length() > 0 && fileInfo.fileName().at(0).isLower())
185 ret.append(fileInfo);
192 static int displayOptionsDialog(Options *options)
196 QFormLayout *layout = new QFormLayout(&dialog);
198 QComboBox *qmlFileComboBox = new QComboBox(&dialog);
199 QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
201 foreach (QFileInfo fileInfo, fileInfos)
202 qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
204 QCheckBox *originalCheckBox = new QCheckBox(&dialog);
205 originalCheckBox->setText("Use original QML viewer");
206 originalCheckBox->setChecked(options->originalQml);
208 QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
209 fullscreenCheckBox->setText("Start fullscreen");
210 fullscreenCheckBox->setChecked(options->fullscreen);
212 QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
213 maximizedCheckBox->setText("Start maximized");
214 maximizedCheckBox->setChecked(options->maximized);
216 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
219 QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
220 QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
222 layout->addRow("Qml file:", qmlFileComboBox);
223 layout->addWidget(originalCheckBox);
224 layout->addWidget(maximizedCheckBox);
225 layout->addWidget(fullscreenCheckBox);
226 layout->addWidget(buttonBox);
228 int result = dialog.exec();
229 if (result == QDialog::Accepted) {
230 QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
231 QFileInfo fileInfo = variant.value<QFileInfo>();
233 if (fileInfo.canonicalFilePath().startsWith(":"))
234 options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
236 options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
237 options->originalQml = originalCheckBox->isChecked();
238 options->maximized = maximizedCheckBox->isChecked();
239 options->fullscreen = fullscreenCheckBox->isChecked();
245 static bool checkVersion(const QUrl &url)
247 if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
248 qWarning("QMLSCENE_IMPORT_NAME is no longer supported.");
250 QString fileName = url.toLocalFile();
251 if (fileName.isEmpty()) {
252 qWarning("qmlscene: filename required.");
257 if (!f.open(QFile::ReadOnly | QFile::Text)) {
258 qWarning("qmlscene: failed to check version of file '%s', could not open...",
259 qPrintable(fileName));
263 QRegExp quick1("^\\s*import +QtQuick +1\\.\\w*");
264 QRegExp qt47("^\\s*import +Qt +4\\.7");
266 QTextStream stream(&f);
267 bool codeFound= false;
269 QString line = stream.readLine();
270 if (line.contains("{")) {
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();
279 if (!import.isNull()) {
280 qWarning("qmlscene: '%s' is no longer supported.\n"
281 "Use qmlviewer to load file '%s'.",
283 qPrintable(fileName));
292 static void displayFileDialog(Options *options)
294 #if defined(QT_WIDGETS_LIB) && !defined(QT_NO_FILEDIALOG)
295 QString fileName = QFileDialog::getOpenFileName(0, "Open QML file", QString(), "QML Files (*.qml)");
296 if (!fileName.isEmpty()) {
297 QFileInfo fi(fileName);
298 options->file = QUrl::fromLocalFile(fi.canonicalFilePath());
302 qWarning("No filename specified...");
306 static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
308 QDir dir(directory+"/dummydata", "*.qml");
309 QStringList list = dir.entryList();
310 for (int i = 0; i < list.size(); ++i) {
311 QString qml = list.at(i);
312 QFile f(dir.filePath(qml));
313 f.open(QIODevice::ReadOnly);
314 QByteArray data = f.readAll();
315 QQmlComponent comp(&engine);
316 comp.setData(data, QUrl());
317 QObject *dummyData = comp.create();
320 QList<QQmlError> errors = comp.errors();
321 foreach (const QQmlError &error, errors)
326 qWarning() << "Loaded dummy data:" << dir.filePath(qml);
327 qml.truncate(qml.length()-4);
328 engine.rootContext()->setContextProperty(qml, dummyData);
329 dummyData->setParent(&engine);
336 qWarning("Usage: qmlscene [options] <filename>");
338 qWarning(" options:");
339 qWarning(" --maximized ............................... run maximized");
340 qWarning(" --fullscreen .............................. run fullscreen");
341 qWarning(" --transparent ............................. Make the window transparent");
342 qWarning(" --no-multisample .......................... Disable multisampling (anti-aliasing)");
343 qWarning(" --no-version-detection .................... Do not try to detect the version of the .qml file");
344 qWarning(" --slow-animations ......................... Run all animations in slow motion");
345 qWarning(" --resize-to-root .......................... Resize the window to the size of the root item");
346 qWarning(" --quit .................................... Quit immediately after starting");
347 qWarning(" -I <path> ................................. Add <path> to the list of import paths");
348 qWarning(" -B <name> <file> .......................... Add a named bundle");
354 int main(int argc, char ** argv)
359 QList<QPair<QString, QString> > bundles;
360 for (int i = 1; i < argc; ++i) {
361 if (*argv[i] != '-' && QFileInfo(QFile::decodeName(argv[i])).exists()) {
362 options.file = QUrl::fromLocalFile(argv[i]);
364 const QString lowerArgument = QString::fromLatin1(argv[i]).toLower();
365 if (lowerArgument == QLatin1String("--maximized"))
366 options.maximized = true;
367 else if (lowerArgument == QLatin1String("--fullscreen"))
368 options.fullscreen = true;
369 else if (lowerArgument == QLatin1String("--transparent"))
370 options.transparent = true;
371 else if (lowerArgument == QLatin1String("--clip"))
373 else if (lowerArgument == QLatin1String("--no-version-detection"))
374 options.versionDetection = false;
375 else if (lowerArgument == QLatin1String("--slow-animations"))
376 options.slowAnimations = true;
377 else if (lowerArgument == QLatin1String("--quit"))
378 options.quitImmediately = true;
379 else if (lowerArgument == QLatin1String("--resize-to-root"))
380 options.resizeViewToRootItem = true;
381 else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
382 imports.append(QString::fromLatin1(argv[++i]));
383 else if (lowerArgument == QLatin1String("-b") && i + 2 < argc) {
384 QString name = QString::fromLatin1(argv[++i]);
385 QString file = QString::fromLatin1(argv[++i]);
386 bundles.append(qMakePair(name, file));
387 } else if (lowerArgument == QLatin1String("--help")
388 || lowerArgument == QLatin1String("-help")
389 || lowerArgument == QLatin1String("--h")
390 || lowerArgument == QLatin1String("-h"))
395 #ifdef QT_WIDGETS_LIB
396 QApplication app(argc, argv);
398 QGuiApplication app(argc, argv);
400 app.setApplicationName("QtQmlViewer");
401 app.setOrganizationName("Nokia");
402 app.setOrganizationDomain("nokia.com");
404 QUnifiedTimer::instance()->setSlowModeEnabled(options.slowAnimations);
406 if (options.file.isEmpty())
407 #if defined(QMLSCENE_BUNDLE)
408 displayOptionsDialog(&options);
410 displayFileDialog(&options);
413 QQmlEngine *engine = 0;
417 if (!options.file.isEmpty()) {
418 if (!options.versionDetection || checkVersion(options.file)) {
420 engine = qxView.engine();
421 for (int i = 0; i < imports.size(); ++i)
422 engine->addImportPath(imports.at(i));
423 for (int i = 0; i < bundles.size(); ++i)
424 engine->addNamedBundle(bundles.at(i).first, bundles.at(i).second);
425 if (options.file.isLocalFile()) {
426 QFileInfo fi(options.file.toLocalFile());
427 loadDummyDataFiles(*engine, fi.path());
429 qxView.setSource(options.file);
431 QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
433 if (options.resizeViewToRootItem)
434 qxView.setResizeMode(QQuickView::SizeViewToRootObject);
436 qxView.setResizeMode(QQuickView::SizeRootObjectToView);
438 if (options.transparent) {
439 QSurfaceFormat surfaceFormat;
440 surfaceFormat.setAlphaBufferSize(8);
441 qxView.setFormat(surfaceFormat);
442 qxView.setClearBeforeRendering(true);
443 qxView.setClearColor(QColor(Qt::transparent));
444 qxView.setWindowFlags(Qt::FramelessWindowHint);
447 qxView.setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
449 if (options.fullscreen)
450 qxView.showFullScreen();
451 else if (options.maximized)
452 qxView.showMaximized();
456 if (options.quitImmediately)
457 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
459 exitCode = app.exec();
461 #ifdef QML_RUNTIME_TESTING
462 RenderStatistics::printTotalStats();