1 /****************************************************************************
3 ** Copyright (C) 2010 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/QFileDialog>
63 #ifdef QML_RUNTIME_TESTING
64 class RenderStatistics
67 static void updateStats();
68 static void printTotalStats();
70 static QVector<qreal> timePerFrame;
71 static QVector<int> timesPerFrames;
74 QVector<qreal> RenderStatistics::timePerFrame;
75 QVector<int> RenderStatistics::timesPerFrames;
77 void RenderStatistics::updateStats()
86 int elapsed = time.elapsed();
87 timesPerFrames.append(elapsed - lastTime);
91 qreal avgtime = elapsed / (qreal) frames;
93 for (int i = 0; i < timesPerFrames.size(); ++i) {
94 qreal diff = timesPerFrames.at(i) - avgtime;
97 var /= timesPerFrames.size();
99 qDebug("Average time per frame: %f ms (%i fps), std.dev: %f ms", avgtime, qRound(1000. / avgtime), qSqrt(var));
101 timePerFrame.append(avgtime);
102 timesPerFrames.clear();
111 void RenderStatistics::printTotalStats()
113 int count = timePerFrame.count();
120 for (int i = 0; i < count; ++i) {
121 minTime = minTime == 0 ? timePerFrame.at(i) : qMin(minTime, timePerFrame.at(i));
122 maxTime = qMax(maxTime, timePerFrame.at(i));
123 avg += timePerFrame.at(i);
128 qDebug("----- Statistics -----");
129 qDebug("Average time per frame: %f ms (%i fps)", avg, qRound(1000. / avg));
130 qDebug("Best time per frame: %f ms (%i fps)", minTime, int(1000 / minTime));
131 qDebug("Worst time per frame: %f ms (%i fps)", maxTime, int(1000 / maxTime));
132 qDebug("----------------------");
137 class MyQQuickView : public QQuickView
140 MyQQuickView() : QQuickView()
142 setResizeMode(QQuickView::SizeRootObjectToView);
150 , originalQmlRaster(false)
154 , versionDetection(true)
160 bool originalQmlRaster;
163 bool scenegraphOnGraphicsview;
165 bool versionDetection;
168 #if defined(QMLSCENE_BUNDLE)
169 Q_DECLARE_METATYPE(QFileInfo);
170 QFileInfoList findQmlFiles(const QString &dirName)
176 QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
177 QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
179 foreach (QFileInfo fileInfo, fileInfos) {
180 if (fileInfo.isDir())
181 ret += findQmlFiles(fileInfo.filePath());
182 else if (fileInfo.fileName().length() > 0 && fileInfo.fileName().at(0).isLower())
183 ret.append(fileInfo);
190 static int displayOptionsDialog(Options *options)
194 QFormLayout *layout = new QFormLayout(&dialog);
196 QComboBox *qmlFileComboBox = new QComboBox(&dialog);
197 QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
199 foreach (QFileInfo fileInfo, fileInfos)
200 qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
202 QCheckBox *originalCheckBox = new QCheckBox(&dialog);
203 originalCheckBox->setText("Use original QML viewer");
204 originalCheckBox->setChecked(options->originalQml);
206 QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
207 fullscreenCheckBox->setText("Start fullscreen");
208 fullscreenCheckBox->setChecked(options->fullscreen);
210 QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
211 maximizedCheckBox->setText("Start maximized");
212 maximizedCheckBox->setChecked(options->maximized);
214 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
217 QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
218 QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
220 layout->addRow("Qml file:", qmlFileComboBox);
221 layout->addWidget(originalCheckBox);
222 layout->addWidget(maximizedCheckBox);
223 layout->addWidget(fullscreenCheckBox);
224 layout->addWidget(buttonBox);
226 int result = dialog.exec();
227 if (result == QDialog::Accepted) {
228 QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
229 QFileInfo fileInfo = variant.value<QFileInfo>();
231 if (fileInfo.canonicalFilePath().startsWith(":"))
232 options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
234 options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
235 options->originalQml = originalCheckBox->isChecked();
236 options->maximized = maximizedCheckBox->isChecked();
237 options->fullscreen = fullscreenCheckBox->isChecked();
243 static void checkAndAdaptVersion(const QUrl &url)
245 if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
249 QString fileName = url.toLocalFile();
250 if (fileName.isEmpty())
254 if (!f.open(QFile::ReadOnly | QFile::Text)) {
255 qWarning("qmlscene: failed to check version of file '%s', could not open...",
256 qPrintable(fileName));
260 QRegExp quick1("^\\s*import +QtQuick +1\\.");
261 QRegExp quick2("^\\s*import +QtQuick +2\\.");
262 QRegExp qt47("^\\s*import +Qt +4\\.7");
267 QTextStream stream(&f);
268 bool codeFound= false;
270 QString line = stream.readLine();
271 if (line.contains("{"))
273 if (envToWrite.isEmpty() && quick1.indexIn(line) >= 0) {
274 envToWrite = QLatin1String("quick1");
275 compat = QLatin1String("QtQuick 1.0");
276 } else if (envToWrite.isEmpty() && qt47.indexIn(line) >= 0) {
277 envToWrite = QLatin1String("qt");
278 compat = QLatin1String("Qt 4.7");
279 } else if (quick2.indexIn(line) >= 0) {
286 if (!envToWrite.isEmpty()) {
287 qWarning("qmlscene: Autodetecting compatibility import \"%s\"...", qPrintable(compat));
288 if (qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
289 qputenv("QMLSCENE_IMPORT_NAME", envToWrite.toLatin1().constData());
293 static void displayFileDialog(Options *options)
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());
303 qWarning("No filename specified...");
307 static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& directory)
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();
321 QList<QDeclarativeError> errors = comp.errors();
322 foreach (const QDeclarativeError &error, errors) {
328 qWarning() << "Loaded dummy data:" << dir.filePath(qml);
329 qml.truncate(qml.length()-4);
330 engine.rootContext()->setContextProperty(qml, dummyData);
331 dummyData->setParent(&engine);
338 qWarning("Usage: qmlscene [options] <filename>");
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");
350 int main(int argc, char ** argv)
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]);
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"))
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"))
378 QGuiApplication app(argc, argv);
379 app.setApplicationName("QtQmlViewer");
380 app.setOrganizationName("Nokia");
381 app.setOrganizationDomain("nokia.com");
383 if (options.file.isEmpty())
384 #if defined(QMLSCENE_BUNDLE)
385 displayOptionsDialog(&options);
387 displayFileDialog(&options);
391 QDeclarativeEngine *engine = 0;
395 if (!options.file.isEmpty()) {
396 if (options.versionDetection)
397 checkAndAdaptVersion(options.file);
398 QQuickView *qxView = new MyQQuickView();
399 engine = qxView->engine();
400 for (int i = 0; i < imports.size(); ++i)
401 engine->addImportPath(imports.at(i));
403 if (options.file.isLocalFile()) {
404 QFileInfo fi(options.file.toLocalFile());
405 loadDummyDataFiles(*engine, fi.path());
407 qxView->setSource(options.file);
409 QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
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();
419 exitCode = app.exec();
423 #ifdef QML_RUNTIME_TESTING
424 RenderStatistics::printTotalStats();