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("----------------------");
140 class MyQQuickView : public QQuickView
143 MyQQuickView() : QQuickView()
145 setResizeMode(QQuickView::SizeRootObjectToView);
153 , originalQmlRaster(false)
157 , versionDetection(true)
158 , slowAnimations(false)
164 bool originalQmlRaster;
167 bool scenegraphOnGraphicsview;
169 bool versionDetection;
173 #if defined(QMLSCENE_BUNDLE)
174 Q_DECLARE_METATYPE(QFileInfo);
175 QFileInfoList findQmlFiles(const QString &dirName)
181 QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
182 QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
184 foreach (QFileInfo fileInfo, fileInfos) {
185 if (fileInfo.isDir())
186 ret += findQmlFiles(fileInfo.filePath());
187 else if (fileInfo.fileName().length() > 0 && fileInfo.fileName().at(0).isLower())
188 ret.append(fileInfo);
195 static int displayOptionsDialog(Options *options)
199 QFormLayout *layout = new QFormLayout(&dialog);
201 QComboBox *qmlFileComboBox = new QComboBox(&dialog);
202 QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
204 foreach (QFileInfo fileInfo, fileInfos)
205 qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
207 QCheckBox *originalCheckBox = new QCheckBox(&dialog);
208 originalCheckBox->setText("Use original QML viewer");
209 originalCheckBox->setChecked(options->originalQml);
211 QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
212 fullscreenCheckBox->setText("Start fullscreen");
213 fullscreenCheckBox->setChecked(options->fullscreen);
215 QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
216 maximizedCheckBox->setText("Start maximized");
217 maximizedCheckBox->setChecked(options->maximized);
219 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
222 QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
223 QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
225 layout->addRow("Qml file:", qmlFileComboBox);
226 layout->addWidget(originalCheckBox);
227 layout->addWidget(maximizedCheckBox);
228 layout->addWidget(fullscreenCheckBox);
229 layout->addWidget(buttonBox);
231 int result = dialog.exec();
232 if (result == QDialog::Accepted) {
233 QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
234 QFileInfo fileInfo = variant.value<QFileInfo>();
236 if (fileInfo.canonicalFilePath().startsWith(":"))
237 options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
239 options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
240 options->originalQml = originalCheckBox->isChecked();
241 options->maximized = maximizedCheckBox->isChecked();
242 options->fullscreen = fullscreenCheckBox->isChecked();
248 static bool checkVersion(const QUrl &url)
250 if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty()) {
251 qWarning("QMLSCENE_IMPORT_NAME is no longer supported.");
254 QString fileName = url.toLocalFile();
255 if (fileName.isEmpty()) {
256 qWarning("qmlscene: filename required.");
261 if (!f.open(QFile::ReadOnly | QFile::Text)) {
262 qWarning("qmlscene: failed to check version of file '%s', could not open...",
263 qPrintable(fileName));
267 QRegExp quick1("^\\s*import +QtQuick +1\\.\\w*");
268 QRegExp qt47("^\\s*import +Qt +4\\.7");
270 QTextStream stream(&f);
271 bool codeFound= false;
273 QString line = stream.readLine();
274 if (line.contains("{")) {
278 if (quick1.indexIn(line) >= 0) {
279 import = quick1.cap(0).trimmed();
280 } else if (qt47.indexIn(line) >= 0) {
281 import = qt47.cap(0).trimmed();
284 if (!import.isNull()) {
285 qWarning("qmlscene: '%s' is no longer supported.\n"
286 "Use qmlviewer to load file '%s'.",
288 qPrintable(fileName));
297 static void displayFileDialog(Options *options)
299 #ifdef QT_WIDGETS_LIB
300 QString fileName = QFileDialog::getOpenFileName(0, "Open QML file", QString(), "QML Files (*.qml)");
301 if (!fileName.isEmpty()) {
302 QFileInfo fi(fileName);
303 options->file = QUrl::fromLocalFile(fi.canonicalFilePath());
307 qWarning("No filename specified...");
311 static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
313 QDir dir(directory+"/dummydata", "*.qml");
314 QStringList list = dir.entryList();
315 for (int i = 0; i < list.size(); ++i) {
316 QString qml = list.at(i);
317 QFile f(dir.filePath(qml));
318 f.open(QIODevice::ReadOnly);
319 QByteArray data = f.readAll();
320 QQmlComponent comp(&engine);
321 comp.setData(data, QUrl());
322 QObject *dummyData = comp.create();
325 QList<QQmlError> errors = comp.errors();
326 foreach (const QQmlError &error, errors) {
332 qWarning() << "Loaded dummy data:" << dir.filePath(qml);
333 qml.truncate(qml.length()-4);
334 engine.rootContext()->setContextProperty(qml, dummyData);
335 dummyData->setParent(&engine);
342 qWarning("Usage: qmlscene [options] <filename>");
344 qWarning(" options:");
345 qWarning(" --maximized ............................... run maximized");
346 qWarning(" --fullscreen .............................. run fullscreen");
347 qWarning(" --no-multisample .......................... Disable multisampling (anti-aliasing)");
348 qWarning(" --no-version-detection .................... Do not try to detect the version of the .qml file");
349 qWarning(" --slow-animations ......................... Run all animations in slow motion");
355 int main(int argc, char ** argv)
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("--clip"))
371 else if (lowerArgument == QLatin1String("--no-version-detection"))
372 options.versionDetection = false;
373 else if (lowerArgument == QLatin1String("--slow-animations"))
374 options.slowAnimations = true;
375 else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
376 imports.append(QString::fromLatin1(argv[++i]));
377 else if (lowerArgument == QLatin1String("--help")
378 || lowerArgument == QLatin1String("-help")
379 || lowerArgument == QLatin1String("--h")
380 || lowerArgument == QLatin1String("-h"))
385 #ifdef QT_WIDGETS_LIB
386 QApplication app(argc, argv);
388 QGuiApplication app(argc, argv);
390 app.setApplicationName("QtQmlViewer");
391 app.setOrganizationName("Nokia");
392 app.setOrganizationDomain("nokia.com");
394 QUnifiedTimer::instance()->setSlowModeEnabled(options.slowAnimations);
396 if (options.file.isEmpty())
397 #if defined(QMLSCENE_BUNDLE)
398 displayOptionsDialog(&options);
400 displayFileDialog(&options);
404 QQmlEngine *engine = 0;
408 if (!options.file.isEmpty()) {
409 if (!options.versionDetection || checkVersion(options.file)) {
410 QQuickView *qxView = new MyQQuickView();
411 engine = qxView->engine();
412 for (int i = 0; i < imports.size(); ++i)
413 engine->addImportPath(imports.at(i));
415 if (options.file.isLocalFile()) {
416 QFileInfo fi(options.file.toLocalFile());
417 loadDummyDataFiles(*engine, fi.path());
419 qxView->setSource(options.file);
421 QObject::connect(engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
423 window->setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
424 if (options.fullscreen)
425 window->showFullScreen();
426 else if (options.maximized)
427 window->showMaximized();
431 exitCode = app.exec();
435 #ifdef QML_RUNTIME_TESTING
436 RenderStatistics::printTotalStats();