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 #ifdef QML_RUNTIME_TESTING
60 class RenderStatistics
63 static void updateStats();
64 static void printTotalStats();
66 static QVector<qreal> timePerFrame;
67 static QVector<int> timesPerFrames;
70 QVector<qreal> RenderStatistics::timePerFrame;
71 QVector<int> RenderStatistics::timesPerFrames;
73 void RenderStatistics::updateStats()
82 int elapsed = time.elapsed();
83 timesPerFrames.append(elapsed - lastTime);
87 qreal avgtime = elapsed / (qreal) frames;
89 for (int i = 0; i < timesPerFrames.size(); ++i) {
90 qreal diff = timesPerFrames.at(i) - avgtime;
93 var /= timesPerFrames.size();
95 qDebug("Average time per frame: %f ms (%i fps), std.dev: %f ms", avgtime, qRound(1000. / avgtime), qSqrt(var));
97 timePerFrame.append(avgtime);
98 timesPerFrames.clear();
107 void RenderStatistics::printTotalStats()
109 int count = timePerFrame.count();
116 for (int i = 0; i < count; ++i) {
117 minTime = minTime == 0 ? timePerFrame.at(i) : qMin(minTime, timePerFrame.at(i));
118 maxTime = qMax(maxTime, timePerFrame.at(i));
119 avg += timePerFrame.at(i);
124 qDebug("----- Statistics -----");
125 qDebug("Average time per frame: %f ms (%i fps)", avg, qRound(1000. / avg));
126 qDebug("Best time per frame: %f ms (%i fps)", minTime, int(1000 / minTime));
127 qDebug("Worst time per frame: %f ms (%i fps)", maxTime, int(1000 / maxTime));
128 qDebug("----------------------");
133 class MyQQuickView : public QQuickView
136 MyQQuickView() : QQuickView()
138 setResizeMode(QQuickView::SizeRootObjectToView);
146 , originalQmlRaster(false)
150 , versionDetection(true)
156 bool originalQmlRaster;
159 bool scenegraphOnGraphicsview;
161 bool versionDetection;
165 #if defined(QMLSCENE_BUNDLE)
166 Q_DECLARE_METATYPE(QFileInfo);
167 QFileInfoList findQmlFiles(const QString &dirName)
173 QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
174 QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
176 foreach (QFileInfo fileInfo, fileInfos) {
177 if (fileInfo.isDir())
178 ret += findQmlFiles(fileInfo.filePath());
179 else if (fileInfo.fileName().length() > 0 && fileInfo.fileName().at(0).isLower())
180 ret.append(fileInfo);
187 static int displayOptionsDialog(Options *options)
191 QFormLayout *layout = new QFormLayout(&dialog);
193 QComboBox *qmlFileComboBox = new QComboBox(&dialog);
194 QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
196 foreach (QFileInfo fileInfo, fileInfos)
197 qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
199 QCheckBox *originalCheckBox = new QCheckBox(&dialog);
200 originalCheckBox->setText("Use original QML viewer");
201 originalCheckBox->setChecked(options->originalQml);
203 QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
204 fullscreenCheckBox->setText("Start fullscreen");
205 fullscreenCheckBox->setChecked(options->fullscreen);
207 QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
208 maximizedCheckBox->setText("Start maximized");
209 maximizedCheckBox->setChecked(options->maximized);
211 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
214 QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
215 QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
217 layout->addRow("Qml file:", qmlFileComboBox);
218 layout->addWidget(originalCheckBox);
219 layout->addWidget(maximizedCheckBox);
220 layout->addWidget(fullscreenCheckBox);
221 layout->addWidget(buttonBox);
223 int result = dialog.exec();
224 if (result == QDialog::Accepted) {
225 QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
226 QFileInfo fileInfo = variant.value<QFileInfo>();
228 if (fileInfo.canonicalFilePath().startsWith(":"))
229 options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
231 options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
232 options->originalQml = originalCheckBox->isChecked();
233 options->maximized = maximizedCheckBox->isChecked();
234 options->fullscreen = fullscreenCheckBox->isChecked();
240 static void checkAndAdaptVersion(const QUrl &url)
242 if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
246 QString fileName = url.toLocalFile();
247 if (fileName.isEmpty())
251 if (!f.open(QFile::ReadOnly | QFile::Text)) {
252 qWarning("qmlscene: failed to check version of file '%s', could not open...",
253 qPrintable(fileName));
257 QRegExp quick1("^\\s*import +QtQuick +1\\.");
258 QRegExp quick2("^\\s*import +QtQuick +2\\.");
259 QRegExp qt47("^\\s*import +Qt +4\\.7");
264 QTextStream stream(&f);
265 bool codeFound= false;
267 QString line = stream.readLine();
268 if (line.contains("{"))
270 if (envToWrite.isEmpty() && quick1.indexIn(line) >= 0) {
271 envToWrite = QLatin1String("quick1");
272 compat = QLatin1String("QtQuick 1.0");
273 } else if (envToWrite.isEmpty() && qt47.indexIn(line) >= 0) {
274 envToWrite = QLatin1String("qt");
275 compat = QLatin1String("Qt 4.7");
276 } else if (quick2.indexIn(line) >= 0) {
283 if (!envToWrite.isEmpty()) {
284 qWarning("qmlscene: Autodetecting compatibility import \"%s\"...", qPrintable(compat));
285 if (qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
286 qputenv("QMLSCENE_IMPORT_NAME", envToWrite.toLatin1().constData());
290 static void displayFileDialog(Options *options)
292 #ifdef QT_WIDGETS_LIB
293 QString fileName = QFileDialog::getOpenFileName(0, "Open QML file", QString(), "QML Files (*.qml)");
294 if (!fileName.isEmpty()) {
295 QFileInfo fi(fileName);
296 options->file = QUrl::fromLocalFile(fi.canonicalFilePath());
300 qWarning("No filename specified...");
304 static void loadDummyDataFiles(QDeclarativeEngine &engine, const QString& directory)
306 QDir dir(directory+"/dummydata", "*.qml");
307 QStringList list = dir.entryList();
308 for (int i = 0; i < list.size(); ++i) {
309 QString qml = list.at(i);
310 QFile f(dir.filePath(qml));
311 f.open(QIODevice::ReadOnly);
312 QByteArray data = f.readAll();
313 QDeclarativeComponent comp(&engine);
314 comp.setData(data, QUrl());
315 QObject *dummyData = comp.create();
318 QList<QDeclarativeError> errors = comp.errors();
319 foreach (const QDeclarativeError &error, errors) {
325 qWarning() << "Loaded dummy data:" << dir.filePath(qml);
326 qml.truncate(qml.length()-4);
327 engine.rootContext()->setContextProperty(qml, dummyData);
328 dummyData->setParent(&engine);
335 qWarning("Usage: qmlscene [options] <filename>");
337 qWarning(" options:");
338 qWarning(" --maximized ............................... run maximized");
339 qWarning(" --fullscreen .............................. run fullscreen");
340 qWarning(" --no-multisample .......................... Disable multisampling (anti-aliasing)");
341 qWarning(" --no-version-detection .................... Do not try to detect the version of the .qml file");
347 int main(int argc, char ** argv)
352 for (int i = 1; i < argc; ++i) {
353 if (*argv[i] != '-' && QFileInfo(QFile::decodeName(argv[i])).exists()) {
354 options.file = QUrl::fromLocalFile(argv[i]);
356 const QString lowerArgument = QString::fromLatin1(argv[i]).toLower();
357 if (lowerArgument == QLatin1String("--maximized"))
358 options.maximized = true;
359 else if (lowerArgument == QLatin1String("--fullscreen"))
360 options.fullscreen = true;
361 else if (lowerArgument == QLatin1String("--clip"))
363 else if (lowerArgument == QLatin1String("--no-version-detection"))
364 options.versionDetection = false;
365 else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
366 imports.append(QString::fromLatin1(argv[++i]));
367 else if (lowerArgument == QLatin1String("--no-vsync-animations"))
368 options.vsync = false;
369 else if (lowerArgument == QLatin1String("--help")
370 || lowerArgument == QLatin1String("-help")
371 || lowerArgument == QLatin1String("--h")
372 || lowerArgument == QLatin1String("-h"))
377 QGuiApplication app(argc, argv);
378 app.setApplicationName("QtQmlViewer");
379 app.setOrganizationName("Nokia");
380 app.setOrganizationDomain("nokia.com");
382 if (options.file.isEmpty())
383 #if defined(QMLSCENE_BUNDLE)
384 displayOptionsDialog(&options);
386 displayFileDialog(&options);
390 QDeclarativeEngine *engine = 0;
394 if (!options.file.isEmpty()) {
395 if (options.versionDetection)
396 checkAndAdaptVersion(options.file);
397 QQuickView *qxView = new MyQQuickView();
398 qxView->setVSyncAnimations(options.vsync);
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();