1 /****************************************************************************
3 ** Copyright (C) 2011 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 "qdeclarative.h"
43 #include "qmlruntime.h"
44 #include "qdeclarativeengine.h"
45 #include "loggerwidget.h"
48 #include <QApplication>
49 #include <QTranslator>
51 #include <QMessageBox>
53 #include <QLibraryInfo>
54 #include "qdeclarativetester.h"
58 QtMsgHandler systemMsgOutput = 0;
60 static QDeclarativeViewer *openFile(const QString &fileName);
61 static void showViewer(QDeclarativeViewer *viewer);
67 // Debugging output is not visible by default on Windows -
68 // therefore show modal dialog with errors instead.
69 if (!warnings.isEmpty()) {
70 QMessageBox::warning(0, QApplication::translate("QDeclarativeViewer", "Qt QML Viewer"), warnings);
76 QWeakPointer<LoggerWidget> logger;
77 static QAtomicInt recursiveLock(0);
79 #if defined (Q_OS_SYMBIAN)
81 #include <sys/types.h>
86 void myMessageOutput(QtMsgType type, const char *msg)
88 QString strMsg = QString::fromLatin1(msg);
90 if (!QCoreApplication::closingDown()) {
91 if (!logger.isNull()) {
92 if (recursiveLock.testAndSetOrdered(0, 1)) {
93 QMetaObject::invokeMethod(logger.data(), "append", Q_ARG(QString, strMsg));
98 warnings += QLatin1Char('\n');
101 #if defined (Q_OS_SYMBIAN)
104 fd = ::open("E:\\qml.log", O_WRONLY | O_CREAT);
106 ::write(fd, msg, strlen(msg));
107 ::write(fd, "\n", 1);
115 if (systemMsgOutput) {
116 systemMsgOutput(type, msg);
118 fprintf(stderr, "%s\n", msg);
123 static QDeclarativeViewer* globalViewer = 0;
125 // The qml file that is shown if the user didn't specify a QML file
126 QString initialFile = QLatin1String("qrc:/startup/startup.qml");
130 qWarning("Usage: qmlviewer [options] <filename>");
132 qWarning(" options:");
133 qWarning(" -v, -version ............................. display version");
134 qWarning(" -frameless ............................... run with no window frame");
135 qWarning(" -maximized................................ run maximized");
136 qWarning(" -fullscreen............................... run fullscreen");
137 qWarning(" -stayontop................................ keep viewer window on top");
138 qWarning(" -sizeviewtorootobject .................... the view resizes to the changes in the content");
139 qWarning(" -sizerootobjecttoview .................... the content resizes to the changes in the view (default)");
140 qWarning(" -qmlbrowser .............................. use a QML-based file browser");
141 qWarning(" -warnings [show|hide]..................... show warnings in a separate log window");
142 qWarning(" -recordfile <output> ..................... set video recording file");
143 qWarning(" - ImageMagick 'convert' for GIF)");
144 qWarning(" - png file for raw frames");
145 qWarning(" - 'ffmpeg' for other formats");
146 qWarning(" -recorddither ordered|threshold|floyd .... set GIF dither recording mode");
147 qWarning(" -recordrate <fps> ........................ set recording frame rate");
148 qWarning(" -record arg .............................. add a recording process argument");
149 qWarning(" -autorecord [from-]<tomilliseconds> ...... set recording to start and stop");
150 qWarning(" -devicekeys .............................. use numeric keys (see F1)");
151 qWarning(" -dragthreshold <size> .................... set mouse drag threshold size");
152 qWarning(" -netcache <size> ......................... set disk cache to size bytes");
153 qWarning(" -translation <translationfile> ........... set the language to run in");
154 qWarning(" -I <directory> ........................... prepend to the module import search path,");
155 qWarning(" display path if <directory> is empty");
156 qWarning(" -P <directory> ........................... prepend to the plugin search path");
157 #if defined(Q_WS_MAC)
158 qWarning(" -no-opengl ............................... don't use a QGLWidget for the viewport");
159 qWarning(" -opengl .................................. use a QGLWidget for the viewport (default)");
161 qWarning(" -no-opengl ............................... don't use a QGLWidget for the viewport (default)");
162 qWarning(" -opengl .................................. use a QGLWidget for the viewport");
164 qWarning(" -script <path> ........................... set the script to use");
165 qWarning(" -scriptopts <options>|help ............... set the script options to use");
168 qWarning(" Press F1 for interactive help");
173 void scriptOptsUsage()
175 qWarning("Usage: qmlviewer -scriptopts <option>[,<option>...] ...");
176 qWarning(" options:");
177 qWarning(" record ................................... record a new script");
178 qWarning(" play ..................................... playback an existing script");
179 qWarning(" testimages ............................... record images or compare images on playback");
180 qWarning(" testerror ................................ test 'error' property of root item on playback");
181 qWarning(" testskip ................................ test 'skip' property of root item on playback");
182 qWarning(" snapshot ................................. file being recorded is static,");
183 qWarning(" only one frame will be recorded or tested");
184 qWarning(" exitoncomplete ........................... cleanly exit the viewer on script completion");
185 qWarning(" exitonfailure ............................ immediately exit the viewer on script failure");
186 qWarning(" saveonexit ............................... save recording on viewer exit");
188 qWarning(" One of record, play or both must be specified.");
193 enum WarningsConfig { ShowWarnings, HideWarnings, DefaultWarnings };
202 dither(QLatin1String("none")),
210 useNativeFileBrowser(true),
211 experimentalGestures(false),
212 warningsConfig(DefaultWarnings),
215 #if defined(Q_OS_SYMBIAN)
217 useNativeFileBrowser = false;
220 #if defined(Q_WS_MAC)
231 QStringList recordargs;
239 QString translationFile;
244 bool useNativeFileBrowser;
245 bool experimentalGestures;
247 WarningsConfig warningsConfig;
250 QDeclarativeViewer::ScriptOptions scriptOptions;
253 static ViewerOptions opts;
254 static QStringList fileNames;
256 class Application : public QApplication
260 Application(int &argc, char **&argv)
261 : QApplication(argc, argv)
265 bool event(QEvent *ev)
267 if (ev->type() != QEvent::FileOpen)
268 return QApplication::event(ev);
270 QFileOpenEvent *fev = static_cast<QFileOpenEvent *>(ev);
272 globalViewer->open(fev->file());
273 if (!globalViewer->isVisible())
274 showViewer(globalViewer);
280 void showInitialViewer()
282 QApplication::processEvents();
284 QDeclarativeViewer *viewer = globalViewer;
287 if (viewer->currentFile().isEmpty()) {
288 if(opts.useNativeFileBrowser)
289 viewer->open(initialFile);
293 if (!viewer->isVisible())
298 static void parseScriptOptions()
300 QStringList options =
301 opts.scriptopts.split(QLatin1Char(','), QString::SkipEmptyParts);
303 QDeclarativeViewer::ScriptOptions scriptOptions = 0;
304 for (int i = 0; i < options.count(); ++i) {
305 const QString &option = options.at(i);
306 if (option == QLatin1String("help")) {
308 } else if (option == QLatin1String("play")) {
309 scriptOptions |= QDeclarativeViewer::Play;
310 } else if (option == QLatin1String("record")) {
311 scriptOptions |= QDeclarativeViewer::Record;
312 } else if (option == QLatin1String("testimages")) {
313 scriptOptions |= QDeclarativeViewer::TestImages;
314 } else if (option == QLatin1String("testerror")) {
315 scriptOptions |= QDeclarativeViewer::TestErrorProperty;
316 } else if (option == QLatin1String("testskip")) {
317 scriptOptions |= QDeclarativeViewer::TestSkipProperty;
318 } else if (option == QLatin1String("exitoncomplete")) {
319 scriptOptions |= QDeclarativeViewer::ExitOnComplete;
320 } else if (option == QLatin1String("exitonfailure")) {
321 scriptOptions |= QDeclarativeViewer::ExitOnFailure;
322 } else if (option == QLatin1String("saveonexit")) {
323 scriptOptions |= QDeclarativeViewer::SaveOnExit;
324 } else if (option == QLatin1String("snapshot")) {
325 scriptOptions |= QDeclarativeViewer::Snapshot;
331 opts.scriptOptions = scriptOptions;
334 static void parseCommandLineOptions(const QStringList &arguments)
336 for (int i = 1; i < arguments.count(); ++i) {
337 bool lastArg = (i == arguments.count() - 1);
338 QString arg = arguments.at(i);
339 if (arg == QLatin1String("-frameless")) {
340 opts.frameless = true;
341 } else if (arg == QLatin1String("-maximized")) {
342 opts.maximized = true;
343 } else if (arg == QLatin1String("-fullscreen")) {
344 opts.fullScreen = true;
345 } else if (arg == QLatin1String("-stayontop")) {
346 opts.stayOnTop = true;
347 } else if (arg == QLatin1String("-netcache")) {
348 if (lastArg) usage();
349 opts.cache = arguments.at(++i).toInt();
350 } else if (arg == QLatin1String("-recordrate")) {
351 if (lastArg) usage();
352 opts.fps = arguments.at(++i).toDouble();
353 } else if (arg == QLatin1String("-recordfile")) {
354 if (lastArg) usage();
355 opts.recordfile = arguments.at(++i);
356 } else if (arg == QLatin1String("-record")) {
357 if (lastArg) usage();
358 opts.recordargs << arguments.at(++i);
359 } else if (arg == QLatin1String("-recorddither")) {
360 if (lastArg) usage();
361 opts.dither = arguments.at(++i);
362 } else if (arg == QLatin1String("-autorecord")) {
363 if (lastArg) usage();
364 QString range = arguments.at(++i);
365 int dash = range.indexOf(QLatin1Char('-'));
367 opts.autorecord_from = range.left(dash).toInt();
368 opts.autorecord_to = range.mid(dash+1).toInt();
369 } else if (arg == QLatin1String("-devicekeys")) {
371 } else if (arg == QLatin1String("-dragthreshold")) {
372 if (lastArg) usage();
373 qApp->setStartDragDistance(arguments.at(++i).toInt());
374 } else if (arg == QLatin1String("-v") || arg == QLatin1String("-version")) {
375 qWarning("Qt QML Viewer version %s", QT_VERSION_STR);
377 } else if (arg == QLatin1String("-translation")) {
378 if (lastArg) usage();
379 opts.translationFile = arguments.at(++i);
380 } else if (arg == QLatin1String("-no-opengl")) {
382 } else if (arg == QLatin1String("-opengl")) {
384 } else if (arg == QLatin1String("-qmlbrowser")) {
385 opts.useNativeFileBrowser = false;
386 } else if (arg == QLatin1String("-warnings")) {
387 if (lastArg) usage();
388 QString warningsStr = arguments.at(++i);
389 if (warningsStr == QLatin1String("show")) {
390 opts.warningsConfig = ShowWarnings;
391 } else if (warningsStr == QLatin1String("hide")) {
392 opts.warningsConfig = HideWarnings;
396 } else if (arg == QLatin1String("-I") || arg == QLatin1String("-L")) {
397 if (arg == QLatin1String("-L"))
398 qWarning("-L option provided for compatibility only, use -I instead");
400 QDeclarativeEngine tmpEngine;
401 QString paths = tmpEngine.importPathList().join(QLatin1String(":"));
402 qWarning("Current search path: %s", paths.toLocal8Bit().constData());
405 opts.imports << arguments.at(++i);
406 } else if (arg == QLatin1String("-P")) {
407 if (lastArg) usage();
408 opts.plugins << arguments.at(++i);
409 } else if (arg == QLatin1String("-script")) {
410 if (lastArg) usage();
411 opts.script = arguments.at(++i);
412 } else if (arg == QLatin1String("-scriptopts")) {
413 if (lastArg) usage();
414 opts.scriptopts = arguments.at(++i);
415 } else if (arg == QLatin1String("-savescript")) {
416 if (lastArg) usage();
417 opts.script = arguments.at(++i);
418 opts.runScript = false;
419 } else if (arg == QLatin1String("-playscript")) {
420 if (lastArg) usage();
421 opts.script = arguments.at(++i);
422 opts.runScript = true;
423 } else if (arg == QLatin1String("-sizeviewtorootobject")) {
424 opts.sizeToView = false;
425 } else if (arg == QLatin1String("-sizerootobjecttoview")) {
426 opts.sizeToView = true;
427 } else if (arg == QLatin1String("-experimentalgestures")) {
428 opts.experimentalGestures = true;
429 } else if (!arg.startsWith(QLatin1Char('-'))) {
430 fileNames.append(arg);
431 } else if (true || arg == QLatin1String("-help")) {
436 if (!opts.scriptopts.isEmpty()) {
438 parseScriptOptions();
440 if (opts.script.isEmpty())
443 if (!(opts.scriptOptions & QDeclarativeViewer::Record) && !(opts.scriptOptions & QDeclarativeViewer::Play))
445 } else if (!opts.script.isEmpty()) {
451 static QDeclarativeViewer *createViewer()
453 Qt::WFlags wflags = (opts.frameless ? Qt::FramelessWindowHint : Qt::Widget);
455 wflags |= Qt::WindowStaysOnTopHint;
457 QDeclarativeViewer *viewer = new QDeclarativeViewer(0, wflags);
458 viewer->setAttribute(Qt::WA_DeleteOnClose, true);
459 viewer->setUseGL(opts.useGL);
461 if (!opts.scriptopts.isEmpty()) {
462 viewer->setScriptOptions(opts.scriptOptions);
463 viewer->setScript(opts.script);
466 logger = viewer->warningsWidget();
467 if (opts.warningsConfig == ShowWarnings) {
468 logger.data()->setDefaultVisibility(LoggerWidget::ShowWarnings);
469 logger.data()->show();
470 } else if (opts.warningsConfig == HideWarnings){
471 logger.data()->setDefaultVisibility(LoggerWidget::HideWarnings);
474 if (opts.experimentalGestures)
475 viewer->enableExperimentalGestures();
477 foreach (QString lib, opts.imports)
478 viewer->addLibraryPath(lib);
480 foreach (QString plugin, opts.plugins)
481 viewer->addPluginPath(plugin);
483 viewer->setNetworkCacheSize(opts.cache);
484 viewer->setRecordFile(opts.recordfile);
485 viewer->setSizeToView(opts.sizeToView);
487 viewer->setRecordRate(opts.fps);
488 if (opts.autorecord_to)
489 viewer->setAutoRecord(opts.autorecord_from, opts.autorecord_to);
491 viewer->setDeviceKeys(true);
492 viewer->setRecordDither(opts.dither);
493 if (opts.recordargs.count())
494 viewer->setRecordArgs(opts.recordargs);
496 viewer->setUseNativeFileBrowser(opts.useNativeFileBrowser);
501 void showViewer(QDeclarativeViewer *viewer)
504 viewer->showFullScreen();
505 else if (opts.maximized)
506 viewer->showMaximized();
512 QDeclarativeViewer *openFile(const QString &fileName)
514 QDeclarativeViewer *viewer = globalViewer;
516 viewer->open(fileName);
522 int main(int argc, char ** argv)
524 systemMsgOutput = qInstallMsgHandler(myMessageOutput);
526 #if defined (Q_WS_X11) || defined (Q_WS_MAC)
527 //### default to using raster graphics backend for now
528 bool gsSpecified = false;
529 for (int i = 0; i < argc; ++i) {
530 QString arg = QString::fromAscii(argv[i]);
531 if (arg == QLatin1String("-graphicssystem")) {
538 QApplication::setGraphicsSystem(QLatin1String("raster"));
541 Application app(argc, argv);
542 app.setApplicationName(QLatin1String("QtQmlViewer"));
543 app.setOrganizationName(QLatin1String("Nokia"));
544 app.setOrganizationDomain(QLatin1String("nokia.com"));
546 QDeclarativeViewer::registerTypes();
547 QDeclarativeTester::registerTypes();
549 parseCommandLineOptions(app.arguments());
551 QTranslator translator;
552 QTranslator qtTranslator;
553 QString sysLocale = QLocale::system().name();
554 if (translator.load(QLatin1String("qmlviewer_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
555 app.installTranslator(&translator);
556 if (qtTranslator.load(QLatin1String("qt_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
557 app.installTranslator(&qtTranslator);
559 app.removeTranslator(&translator);
563 QTranslator qmlTranslator;
564 if (!opts.translationFile.isEmpty()) {
565 if (qmlTranslator.load(opts.translationFile)) {
566 app.installTranslator(&qmlTranslator);
568 qWarning() << "Could not load the translation file" << opts.translationFile;
572 if (opts.fullScreen && opts.maximized)
573 qWarning() << "Both -fullscreen and -maximized specified. Using -fullscreen.";
575 if (fileNames.isEmpty()) {
576 QFile qmlapp(QLatin1String("qmlapp"));
577 if (qmlapp.exists() && qmlapp.open(QFile::ReadOnly)) {
578 QString content = QString::fromUtf8(qmlapp.readAll());
581 int newline = content.indexOf(QLatin1Char('\n'));
583 fileNames += content.left(newline);
585 fileNames += content;
589 globalViewer = createViewer();
591 if (fileNames.isEmpty()) {
592 // show the initial viewer delayed.
593 // This prevents an initial viewer popping up while there
594 // are FileOpen events coming through the event queue
595 QTimer::singleShot(1, &app, SLOT(showInitialViewer()));
597 foreach (const QString &fileName, fileNames)
601 QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));