1/****************************************************************************
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
6** This file is part of the tools applications of the Qt Toolkit.
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
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>
59#include <QtWidgets/QApplication>
60#include <QtWidgets/QFileDialog>
63#include <QtCore/QTranslator>
64#include <QtCore/QLibraryInfo>
66#include <MouseTouchAdaptor.h>
68#ifdef QML_RUNTIME_TESTING
72 static void updateStats();
73 static void printTotalStats();
75 static QVector<qreal> timePerFrame;
76 static QVector<int> timesPerFrames;
79QVector<qreal> RenderStatistics::timePerFrame;
80QVector<int> RenderStatistics::timesPerFrames;
82void RenderStatistics::updateStats()
91 int elapsed = time.elapsed();
92 timesPerFrames.append(elapsed - lastTime);
96 qreal avgtime = elapsed / (qreal) frames;
98 for (int i = 0; i < timesPerFrames.size(); ++i) {
99 qreal diff = timesPerFrames.at(i) - avgtime;
102 var /= timesPerFrames.size();
104 qDebug("Average time per frame: %f ms (%i fps), std.dev: %f ms", avgtime, qRound(1000. / avgtime), qSqrt(var));
106 timePerFrame.append(avgtime);
107 timesPerFrames.clear();
116void RenderStatistics::printTotalStats()
118 int count = timePerFrame.count();
125 for (int i = 0; i < count; ++i) {
126 minTime = minTime == 0 ? timePerFrame.at(i) : qMin(minTime, timePerFrame.at(i));
127 maxTime = qMax(maxTime, timePerFrame.at(i));
128 avg += timePerFrame.at(i);
133 qDebug("----- Statistics -----");
134 qDebug("Average time per frame: %f ms (%i fps)", avg, qRound(1000. / avg));
135 qDebug("Best time per frame: %f ms (%i fps)", minTime, int(1000 / minTime));
136 qDebug("Worst time per frame: %f ms (%i fps)", maxTime, int(1000 / maxTime));
137 qDebug("----------------------");
146 , originalQmlRaster(false)
151 , versionDetection(true)
152 , quitImmediately(false)
153 , resizeViewToRootItem(false)
160 bool originalQmlRaster;
164 bool scenegraphOnGraphicsview;
166 bool versionDetection;
167 bool quitImmediately;
168 bool resizeViewToRootItem;
170 QString translationFile;
173#if defined(QMLSCENE_BUNDLE)
174QFileInfoList findQmlFiles(const QString &dirName)
180 QFileInfoList fileInfos = dir.entryInfoList(QStringList() << "*.qml",
181 QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
183 Q_FOREACH (QFileInfo fileInfo, fileInfos) {
184 if (fileInfo.isDir())
185 ret += findQmlFiles(fileInfo.filePath());
186 else if (fileInfo.fileName().length() > 0 && fileInfo.fileName().at(0).isLower())
187 ret.append(fileInfo);
194static int displayOptionsDialog(Options *options)
198 QFormLayout *layout = new QFormLayout(&dialog);
200 QComboBox *qmlFileComboBox = new QComboBox(&dialog);
201 QFileInfoList fileInfos = findQmlFiles(":/bundle") + findQmlFiles("./qmlscene-resources");
203 Q_FOREACH (QFileInfo fileInfo, fileInfos)
204 qmlFileComboBox->addItem(fileInfo.dir().dirName() + "/" + fileInfo.fileName(), QVariant::fromValue(fileInfo));
206 QCheckBox *originalCheckBox = new QCheckBox(&dialog);
207 originalCheckBox->setText("Use original QML viewer");
208 originalCheckBox->setChecked(options->originalQml);
210 QCheckBox *fullscreenCheckBox = new QCheckBox(&dialog);
211 fullscreenCheckBox->setText("Start fullscreen");
212 fullscreenCheckBox->setChecked(options->fullscreen);
214 QCheckBox *maximizedCheckBox = new QCheckBox(&dialog);
215 maximizedCheckBox->setText("Start maximized");
216 maximizedCheckBox->setChecked(options->maximized);
218 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
221 QObject::connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
222 QObject::connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
224 layout->addRow("Qml file:", qmlFileComboBox);
225 layout->addWidget(originalCheckBox);
226 layout->addWidget(maximizedCheckBox);
227 layout->addWidget(fullscreenCheckBox);
228 layout->addWidget(buttonBox);
230 int result = dialog.exec();
231 if (result == QDialog::Accepted) {
232 QVariant variant = qmlFileComboBox->itemData(qmlFileComboBox->currentIndex());
233 QFileInfo fileInfo = variant.value<QFileInfo>();
235 if (fileInfo.canonicalFilePath().startsWith(":"))
236 options->file = QUrl("qrc" + fileInfo.canonicalFilePath());
238 options->file = QUrl::fromLocalFile(fileInfo.canonicalFilePath());
239 options->originalQml = originalCheckBox->isChecked();
240 options->maximized = maximizedCheckBox->isChecked();
241 options->fullscreen = fullscreenCheckBox->isChecked();
247static bool checkVersion(const QUrl &url)
249 if (!qgetenv("QMLSCENE_IMPORT_NAME").isEmpty())
250 qWarning("QMLSCENE_IMPORT_NAME is no longer supported.");
252 QString fileName = url.toLocalFile();
253 if (fileName.isEmpty()) {
254 qWarning("qmlscene: filename required.");
259 if (!f.open(QFile::ReadOnly | QFile::Text)) {
260 qWarning("qmlscene: failed to check version of file '%s', could not open...",
261 qPrintable(fileName));
265 QRegExp quick1("^\\s*import +QtQuick +1\\.\\w*");
266 QRegExp qt47("^\\s*import +Qt +4\\.7");
268 QTextStream stream(&f);
269 bool codeFound= false;
271 QString line = stream.readLine();
272 if (line.contains("{")) {
276 if (quick1.indexIn(line) >= 0)
277 import = quick1.cap(0).trimmed();
278 else if (qt47.indexIn(line) >= 0)
279 import = qt47.cap(0).trimmed();
281 if (!import.isNull()) {
282 qWarning("qmlscene: '%s' is no longer supported.\n"
283 "Use qmlviewer to load file '%s'.",
285 qPrintable(fileName));
294static void displayFileDialog(Options *options)
296#if defined(QT_WIDGETS_LIB) && !defined(QT_NO_FILEDIALOG)
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#ifndef QT_NO_TRANSLATION
309static void loadTranslationFile(QTranslator &translator, const QString& directory)
311 translator.load(QLatin1String("qml_" )+QLocale::system().name(), directory + QLatin1String("/i18n"));
312 QCoreApplication::installTranslator(&translator);
316static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
318 QDir dir(directory+"/dummydata", "*.qml");
319 QStringList list = dir.entryList();
320 for (int i = 0; i < list.size(); ++i) {
321 QString qml = list.at(i);
322 QFile f(dir.filePath(qml));
323 f.open(QIODevice::ReadOnly);
324 QByteArray data = f.readAll();
325 QQmlComponent comp(&engine);
326 comp.setData(data, QUrl());
327 QObject *dummyData = comp.create();
330 QList<QQmlError> errors = comp.errors();
331 Q_FOREACH (const QQmlError &error, errors)
336 qWarning() << "Loaded dummy data:" << dir.filePath(qml);
337 qml.truncate(qml.length()-4);
338 engine.rootContext()->setContextProperty(qml, dummyData);
339 dummyData->setParent(&engine);
344static QObject *s_testRootObject = nullptr;
345static QObject *testRootObject(QQmlEngine *engine, QJSEngine *jsEngine)
348 if (!s_testRootObject) {
349 s_testRootObject = new QObject(engine);
351 return s_testRootObject;
356 qWarning("Usage: uqmlscene [options] <filename>");
358 qWarning(" Options:");
359 qWarning(" --maximized ............................... Run maximized");
360 qWarning(" --fullscreen .............................. Run fullscreen");
361 qWarning(" --transparent ............................. Make the window transparent");
362 qWarning(" --multisample ............................. Enable multisampling (OpenGL anti-aliasing)");
363 qWarning(" --no-version-detection .................... Do not try to detect the version of the .qml file");
364 qWarning(" --resize-to-root .......................... Resize the window to the size of the root item");
365 qWarning(" --quit .................................... Quit immediately after starting");
366 qWarning(" -I <path> ................................. Add <path> to the list of import paths");
367 qWarning(" -B <name> <file> .......................... Add a named bundle");
368 qWarning(" -translation <translationfile> ............ Set the language to run in");
374int main(int argc, char ** argv)
379 QList<QPair<QString, QString> > bundles;
380 for (int i = 1; i < argc; ++i) {
381 if (*argv[i] != '-' && QFileInfo(QFile::decodeName(argv[i])).exists()) {
382 options.file = QUrl::fromLocalFile(argv[i]);
384 const QString lowerArgument = QString::fromLatin1(argv[i]).toLower();
385 if (lowerArgument == QLatin1String("--maximized"))
386 options.maximized = true;
387 else if (lowerArgument == QLatin1String("--fullscreen"))
388 options.fullscreen = true;
389 else if (lowerArgument == QLatin1String("--transparent"))
390 options.transparent = true;
391 else if (lowerArgument == QLatin1String("--clip"))
393 else if (lowerArgument == QLatin1String("--no-version-detection"))
394 options.versionDetection = false;
395 else if (lowerArgument == QLatin1String("--quit"))
396 options.quitImmediately = true;
397 else if (lowerArgument == QLatin1String("-translation"))
398 options.translationFile = QLatin1String(argv[++i]);
399 else if (lowerArgument == QLatin1String("--resize-to-root"))
400 options.resizeViewToRootItem = true;
401 else if (lowerArgument == QLatin1String("--multisample"))
402 options.multisample = true;
403 else if (lowerArgument == QLatin1String("-i") && i + 1 < argc)
404 imports.append(QString::fromLatin1(argv[++i]));
405 else if (lowerArgument == QLatin1String("-b") && i + 2 < argc) {
406 QString name = QString::fromLatin1(argv[++i]);
407 QString file = QString::fromLatin1(argv[++i]);
408 bundles.append(qMakePair(name, file));
409 } else if (lowerArgument == QLatin1String("--help")
410 || lowerArgument == QLatin1String("-help")
411 || lowerArgument == QLatin1String("--h")
412 || lowerArgument == QLatin1String("-h"))
418 QApplication app(argc, argv);
420 QGuiApplication app(argc, argv);
422 app.setApplicationName("Unity8 QtQmlViewer");
423 app.setOrganizationName("Qt Project");
424 app.setOrganizationDomain("qt-project.org");
426 MouseTouchAdaptor mouseTouchAdaptor;
427 app.installNativeEventFilter(&mouseTouchAdaptor);
429#ifndef QT_NO_TRANSLATION
430 QTranslator translator;
431 QTranslator qtTranslator;
432 QString sysLocale = QLocale::system().name();
433 if (translator.load(QLatin1String("qmlscene_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
434 app.installTranslator(&translator);
435 if (qtTranslator.load(QLatin1String("qt_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
436 app.installTranslator(&qtTranslator);
438 app.removeTranslator(&translator);
442 QTranslator qmlTranslator;
443 if (!options.translationFile.isEmpty()) {
444 if (qmlTranslator.load(options.translationFile)) {
445 app.installTranslator(&qmlTranslator);
447 qWarning() << "Could not load the translation file" << options.translationFile;
452 if (options.file.isEmpty())
453#if defined(QMLSCENE_BUNDLE)
454 displayOptionsDialog(&options);
456 displayFileDialog(&options);
461 if (!options.file.isEmpty()) {
462 if (!options.versionDetection || checkVersion(options.file)) {
463#ifndef QT_NO_TRANSLATION
464 QTranslator translator;
467 // TODO: as soon as the engine construction completes, the debug service is
468 // listening for connections. But actually we aren't ready to debug anything.
470 QQmlComponent *component = new QQmlComponent(&engine);
471 for (int i = 0; i < imports.size(); ++i)
472 engine.addImportPath(imports.at(i));
473 for (int i = 0; i < bundles.size(); ++i)
474 engine.addNamedBundle(bundles.at(i).first, bundles.at(i).second);
475 if (options.file.isLocalFile()) {
476 QFileInfo fi(options.file.toLocalFile());
477#ifndef QT_NO_TRANSLATION
478 loadTranslationFile(translator, fi.path());
480 loadDummyDataFiles(engine, fi.path());
482 QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
484 qmlRegisterSingletonType<QObject>("Qt.test.qtestroot", 1, 0, "QTestRootObject", testRootObject);
486 component->loadUrl(options.file);
487 if ( !component->isReady() ) {
488 qWarning("%s", qPrintable(component->errorString()));
492 QObject *topLevel = component->create();
493 QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
494 QQuickView* qxView = 0;
496 QQuickItem *contentItem = qobject_cast<QQuickItem *>(topLevel);
498 qxView = new QQuickView(&engine, NULL);
500 // Set window default properties; the qml can still override them
501 QString oname = contentItem->objectName();
502 window->setTitle(oname.isEmpty() ? QString::fromLatin1("qmlscene") : QString::fromLatin1("qmlscene: ") + oname);
503 window->setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowFullscreenButtonHint);
504 if (options.resizeViewToRootItem)
505 qxView->setResizeMode(QQuickView::SizeViewToRootObject);
507 qxView->setResizeMode(QQuickView::SizeRootObjectToView);
508 qxView->setContent(options.file, component, contentItem);
513 QSurfaceFormat surfaceFormat = window->requestedFormat();
514 if (options.multisample)
515 surfaceFormat.setSamples(16);
516 if (options.transparent) {
517 surfaceFormat.setAlphaBufferSize(8);
518 window->setClearBeforeRendering(true);
519 window->setColor(QColor(Qt::transparent));
520 window->setFlags(Qt::FramelessWindowHint);
522 window->setFormat(surfaceFormat);
524 if (options.fullscreen)
525 window->showFullScreen();
526 else if (options.maximized)
527 window->showMaximized();
532 if (options.quitImmediately)
533 QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
535 // Now would be a good time to inform the debug service to start listening.
537 exitCode = app.exec();
539#ifdef QML_RUNTIME_TESTING
540 RenderStatistics::printTotalStats();
542 // Ready to exit. If we created qxView, it owns the component;
543 // otherwise, the ownership is still right here. Nobody deletes the engine
544 // (which is odd since the container constructor takes the engine pointer),
545 // but it's stack-allocated anyway.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list