1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
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.
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.
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.
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.
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 <QtDeclarative/qdeclarative.h>
51 #include <QtDeclarative/qdeclarativeengine.h>
52 #include <QtDeclarative/qdeclarativecomponent.h>
53 #include <QtDeclarative/qdeclarativecontext.h>
55 #include <QtQuick/qquickitem.h>
56 #include <QtQuick/qquickview.h>
59 #include <QtWidgets/QApplication>
60 #include <QtWidgets/QFileDialog>
64 #ifdef QML_RUNTIME_TESTING
65 class RenderStatistics
68 static void updateStats();
69 static void printTotalStats();
71 static QVector<qreal> timePerFrame;
72 static QVector<int> timesPerFrames;
75 QVector<qreal> RenderStatistics::timePerFrame;
76 QVector<int> RenderStatistics::timesPerFrames;
78 void RenderStatistics::updateStats()
87 int elapsed = time.elapsed();
88 timesPerFrames.append(elapsed - lastTime);
92 qreal avgtime = elapsed / (qreal) frames;
94 for (int i = 0; i < timesPerFrames.size(); ++i) {
95 qreal diff = timesPerFrames.at(i) - avgtime;
98 var /= timesPerFrames.size();
100 qDebug("Average time per frame: %f ms (%i fps), std.dev: %f ms", avgtime, qRound(1000. / avgtime), qSqrt(var));
102 timePerFrame.append(avgtime);
103 timesPerFrames.clear();
112 void RenderStatistics::printTotalStats()
114 int count = timePerFrame.count();
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);
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("----------------------");
138 class MyQQuickView : public QQuickView
141 MyQQuickView() : QQuickView()
143 setResizeMode(QQuickView::SizeRootObjectToView);
151 , originalQmlRaster(false)
155 , versionDetection(true)
161 bool originalQmlRaster;
164 bool scenegraphOnGraphicsview;
166 bool versionDetection;
169 #if defined(QMLSCENE_BUNDLE)
170 Q_DECLARE_METATYPE(QFileInfo);
171 QFileInfoList findQmlFiles(const QString &dirName)
177 QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
178 QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
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);
191 static int displayOptionsDialog(Options *options)
195 QFormLayout *layout = new QFormLayout(&dialog);
197 QComboBox *qmlFileComboBox = new QComboBox(&dialog);
198 QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
200 foreach (QFileInfo fileInfo, fileInfos)
201 qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
203 QCheckBox *originalCheckBox = new QCheckBox(&dialog);
204 originalCheckBox->setText("Use original QML viewer");
205 originalCheckBox->setChecked(options->originalQml);
207 QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
208 fullscreenCheckBox->setText("Start fullscreen");
209 fullscreenCheckBox->setChecked(options->fullscreen);
211 QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
212 maximizedCheckBox->setText("Start maximized");
213 maximizedCheckBox->setChecked(options->maximized);
215 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
218 QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
219 QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
221 layout->addRow("Qml file:", qmlFileComboBox);
222 layout->addWidget(originalCheckBox);
223 layout->addWidget(maximizedCheckBox);
224 layout->addWidget(fullscreenCheckBox);
225 layout->addWidget(buttonBox);
227 int result = dialog.exec();
228 if (result == QDialog::Accepted) {
229 QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
230 QFileInfo fileInfo = variant.value<QFileInfo>();
232 if (fileInfo.canonicalFilePath().startsWith(":"))
233 options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
235 options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
236 options->originalQml = originalCheckBox->isChecked();
237 options->maximized = maximizedCheckBox->isChecked();
238 options->fullscreen = fullscreenCheckBox->isChecked();
244 static void checkAndAdaptVersion(const QUrl &url)
246 if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
250 QString fileName = url.toLocalFile();
251 if (fileName.isEmpty())
255 if (!f.open(QFile::ReadOnly | QFile::Text)) {
256 qWarning("qmlscene: failed to check version of file '%s', could not open...",
257 qPrintable(fileName));
261 QRegExp quick1("^\\s*import +QtQuick +1\\.");
262 QRegExp quick2("^\\s*import +QtQuick +2\\.");
263 QRegExp qt47("^\\s*import +Qt +4\\.7");
268 QTextStream stream(&f);
269 bool codeFound= false;
271 QString line = stream.readLine();
272 if (line.contains("{"))
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) {
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());
294 static void displayFileDialog(Options *options)
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());
304 qWarning("No filename specified...");
308 static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& directory)
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();
322 QList<QDeclarativeError> errors = comp.errors();
323 foreach (const QDeclarativeError &error, errors) {
329 qWarning() << "Loaded dummy data:" << dir.filePath(qml);
330 qml.truncate(qml.length()-4);
331 engine.rootContext()->setContextProperty(qml, dummyData);
332 dummyData->setParent(&engine);
339 qWarning("Usage: qmlscene [options] <filename>");
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");
351 int main(int argc, char ** argv)
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]);
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"))
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"))
379 #ifdef QT_WIDGETS_LIB
380 QApplication app(argc, argv);
382 QGuiApplication app(argc, argv);
384 app.setApplicationName("QtQmlViewer");
385 app.setOrganizationName("Nokia");
386 app.setOrganizationDomain("nokia.com");
388 if (options.file.isEmpty())
389 #if defined(QMLSCENE_BUNDLE)
390 displayOptionsDialog(&options);
392 displayFileDialog(&options);
396 QDeclarativeEngine *engine = 0;
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));
408 if (options.file.isLocalFile()) {
409 QFileInfo fi(options.file.toLocalFile());
410 loadDummyDataFiles(*engine, fi.path());
412 qxView->setSource(options.file);
414 QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
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();
424 exitCode = app.exec();
428 #ifdef QML_RUNTIME_TESTING
429 RenderStatistics::printTotalStats();