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 void myMessageOutput(QtMsgType type, const char *msg)
81 QString strMsg = QString::fromLatin1(msg);
83 if (!QCoreApplication::closingDown()) {
84 if (!logger.isNull()) {
85 if (recursiveLock.testAndSetOrdered(0, 1)) {
86 QMetaObject::invokeMethod(logger.data(), "append", Q_ARG(QString, strMsg));
87 recursiveLock.store(0);
91 warnings += QLatin1Char('\n');
95 if (systemMsgOutput) {
96 systemMsgOutput(type, msg);
98 fprintf(stderr, "%s\n", msg);
103 static QDeclarativeViewer* globalViewer = 0;
105 // The qml file that is shown if the user didn't specify a QML file
106 QString initialFile = QLatin1String("qrc:/startup/startup.qml");
110 qWarning("Usage: qmlviewer [options] <filename>");
112 qWarning(" options:");
113 qWarning(" -v, -version ............................. display version");
114 qWarning(" -frameless ............................... run with no window frame");
115 qWarning(" -maximized................................ run maximized");
116 qWarning(" -fullscreen............................... run fullscreen");
117 qWarning(" -stayontop................................ keep viewer window on top");
118 qWarning(" -sizeviewtorootobject .................... the view resizes to the changes in the content");
119 qWarning(" -sizerootobjecttoview .................... the content resizes to the changes in the view (default)");
120 qWarning(" -qmlbrowser .............................. use a QML-based file browser");
121 qWarning(" -warnings [show|hide]..................... show warnings in a separate log window");
122 qWarning(" -recordfile <output> ..................... set video recording file");
123 qWarning(" - ImageMagick 'convert' for GIF)");
124 qWarning(" - png file for raw frames");
125 qWarning(" - 'ffmpeg' for other formats");
126 qWarning(" -recorddither ordered|threshold|floyd .... set GIF dither recording mode");
127 qWarning(" -recordrate <fps> ........................ set recording frame rate");
128 qWarning(" -record arg .............................. add a recording process argument");
129 qWarning(" -autorecord [from-]<tomilliseconds> ...... set recording to start and stop");
130 qWarning(" -devicekeys .............................. use numeric keys (see F1)");
131 qWarning(" -dragthreshold <size> .................... set mouse drag threshold size");
132 qWarning(" -netcache <size> ......................... set disk cache to size bytes");
133 qWarning(" -translation <translationfile> ........... set the language to run in");
134 qWarning(" -I <directory> ........................... prepend to the module import search path,");
135 qWarning(" display path if <directory> is empty");
136 qWarning(" -P <directory> ........................... prepend to the plugin search path");
137 #if defined(Q_OS_MAC)
138 qWarning(" -no-opengl ............................... don't use a QGLWidget for the viewport");
139 qWarning(" -opengl .................................. use a QGLWidget for the viewport (default)");
141 qWarning(" -no-opengl ............................... don't use a QGLWidget for the viewport (default)");
142 qWarning(" -opengl .................................. use a QGLWidget for the viewport");
144 qWarning(" -script <path> ........................... set the script to use");
145 qWarning(" -scriptopts <options>|help ............... set the script options to use");
148 qWarning(" Press F1 for interactive help");
153 void scriptOptsUsage()
155 qWarning("Usage: qmlviewer -scriptopts <option>[,<option>...] ...");
156 qWarning(" options:");
157 qWarning(" record ................................... record a new script");
158 qWarning(" play ..................................... playback an existing script");
159 qWarning(" testimages ............................... record images or compare images on playback");
160 qWarning(" testerror ................................ test 'error' property of root item on playback");
161 qWarning(" testskip ................................ test 'skip' property of root item on playback");
162 qWarning(" snapshot ................................. file being recorded is static,");
163 qWarning(" only one frame will be recorded or tested");
164 qWarning(" exitoncomplete ........................... cleanly exit the viewer on script completion");
165 qWarning(" exitonfailure ............................ immediately exit the viewer on script failure");
166 qWarning(" saveonexit ............................... save recording on viewer exit");
168 qWarning(" One of record, play or both must be specified.");
173 enum WarningsConfig { ShowWarnings, HideWarnings, DefaultWarnings };
182 dither(QLatin1String("none")),
190 useNativeFileBrowser(true),
191 experimentalGestures(false),
192 warningsConfig(DefaultWarnings),
196 #if defined(Q_OS_MAC)
207 QStringList recordargs;
215 QString translationFile;
220 bool useNativeFileBrowser;
221 bool experimentalGestures;
223 WarningsConfig warningsConfig;
226 QDeclarativeViewer::ScriptOptions scriptOptions;
229 static ViewerOptions opts;
230 static QStringList fileNames;
232 class Application : public QApplication
236 Application(int &argc, char **&argv)
237 : QApplication(argc, argv)
241 bool event(QEvent *ev)
243 if (ev->type() != QEvent::FileOpen)
244 return QApplication::event(ev);
246 QFileOpenEvent *fev = static_cast<QFileOpenEvent *>(ev);
248 globalViewer->open(fev->file());
249 if (!globalViewer->isVisible())
250 showViewer(globalViewer);
256 void showInitialViewer()
258 QApplication::processEvents();
260 QDeclarativeViewer *viewer = globalViewer;
263 if (viewer->currentFile().isEmpty()) {
264 if(opts.useNativeFileBrowser)
265 viewer->open(initialFile);
269 if (!viewer->isVisible())
274 static void parseScriptOptions()
276 QStringList options =
277 opts.scriptopts.split(QLatin1Char(','), QString::SkipEmptyParts);
279 QDeclarativeViewer::ScriptOptions scriptOptions = 0;
280 for (int i = 0; i < options.count(); ++i) {
281 const QString &option = options.at(i);
282 if (option == QLatin1String("help")) {
284 } else if (option == QLatin1String("play")) {
285 scriptOptions |= QDeclarativeViewer::Play;
286 } else if (option == QLatin1String("record")) {
287 scriptOptions |= QDeclarativeViewer::Record;
288 } else if (option == QLatin1String("testimages")) {
289 scriptOptions |= QDeclarativeViewer::TestImages;
290 } else if (option == QLatin1String("testerror")) {
291 scriptOptions |= QDeclarativeViewer::TestErrorProperty;
292 } else if (option == QLatin1String("testskip")) {
293 scriptOptions |= QDeclarativeViewer::TestSkipProperty;
294 } else if (option == QLatin1String("exitoncomplete")) {
295 scriptOptions |= QDeclarativeViewer::ExitOnComplete;
296 } else if (option == QLatin1String("exitonfailure")) {
297 scriptOptions |= QDeclarativeViewer::ExitOnFailure;
298 } else if (option == QLatin1String("saveonexit")) {
299 scriptOptions |= QDeclarativeViewer::SaveOnExit;
300 } else if (option == QLatin1String("snapshot")) {
301 scriptOptions |= QDeclarativeViewer::Snapshot;
307 opts.scriptOptions = scriptOptions;
310 static void parseCommandLineOptions(const QStringList &arguments)
312 for (int i = 1; i < arguments.count(); ++i) {
313 bool lastArg = (i == arguments.count() - 1);
314 QString arg = arguments.at(i);
315 if (arg == QLatin1String("-frameless")) {
316 opts.frameless = true;
317 } else if (arg == QLatin1String("-maximized")) {
318 opts.maximized = true;
319 } else if (arg == QLatin1String("-fullscreen")) {
320 opts.fullScreen = true;
321 } else if (arg == QLatin1String("-stayontop")) {
322 opts.stayOnTop = true;
323 } else if (arg == QLatin1String("-netcache")) {
324 if (lastArg) usage();
325 opts.cache = arguments.at(++i).toInt();
326 } else if (arg == QLatin1String("-recordrate")) {
327 if (lastArg) usage();
328 opts.fps = arguments.at(++i).toDouble();
329 } else if (arg == QLatin1String("-recordfile")) {
330 if (lastArg) usage();
331 opts.recordfile = arguments.at(++i);
332 } else if (arg == QLatin1String("-record")) {
333 if (lastArg) usage();
334 opts.recordargs << arguments.at(++i);
335 } else if (arg == QLatin1String("-recorddither")) {
336 if (lastArg) usage();
337 opts.dither = arguments.at(++i);
338 } else if (arg == QLatin1String("-autorecord")) {
339 if (lastArg) usage();
340 QString range = arguments.at(++i);
341 int dash = range.indexOf(QLatin1Char('-'));
343 opts.autorecord_from = range.left(dash).toInt();
344 opts.autorecord_to = range.mid(dash+1).toInt();
345 } else if (arg == QLatin1String("-devicekeys")) {
347 } else if (arg == QLatin1String("-dragthreshold")) {
348 if (lastArg) usage();
349 qApp->setStartDragDistance(arguments.at(++i).toInt());
350 } else if (arg == QLatin1String("-v") || arg == QLatin1String("-version")) {
351 qWarning("Qt QML Viewer version %s", QT_VERSION_STR);
353 } else if (arg == QLatin1String("-translation")) {
354 if (lastArg) usage();
355 opts.translationFile = arguments.at(++i);
356 } else if (arg == QLatin1String("-no-opengl")) {
358 } else if (arg == QLatin1String("-opengl")) {
360 } else if (arg == QLatin1String("-qmlbrowser")) {
361 opts.useNativeFileBrowser = false;
362 } else if (arg == QLatin1String("-warnings")) {
363 if (lastArg) usage();
364 QString warningsStr = arguments.at(++i);
365 if (warningsStr == QLatin1String("show")) {
366 opts.warningsConfig = ShowWarnings;
367 } else if (warningsStr == QLatin1String("hide")) {
368 opts.warningsConfig = HideWarnings;
372 } else if (arg == QLatin1String("-I") || arg == QLatin1String("-L")) {
373 if (arg == QLatin1String("-L"))
374 qWarning("-L option provided for compatibility only, use -I instead");
376 QDeclarativeEngine tmpEngine;
377 QString paths = tmpEngine.importPathList().join(QLatin1String(":"));
378 qWarning("Current search path: %s", paths.toLocal8Bit().constData());
381 opts.imports << arguments.at(++i);
382 } else if (arg == QLatin1String("-P")) {
383 if (lastArg) usage();
384 opts.plugins << arguments.at(++i);
385 } else if (arg == QLatin1String("-script")) {
386 if (lastArg) usage();
387 opts.script = arguments.at(++i);
388 } else if (arg == QLatin1String("-scriptopts")) {
389 if (lastArg) usage();
390 opts.scriptopts = arguments.at(++i);
391 } else if (arg == QLatin1String("-savescript")) {
392 if (lastArg) usage();
393 opts.script = arguments.at(++i);
394 opts.runScript = false;
395 } else if (arg == QLatin1String("-playscript")) {
396 if (lastArg) usage();
397 opts.script = arguments.at(++i);
398 opts.runScript = true;
399 } else if (arg == QLatin1String("-sizeviewtorootobject")) {
400 opts.sizeToView = false;
401 } else if (arg == QLatin1String("-sizerootobjecttoview")) {
402 opts.sizeToView = true;
403 } else if (arg == QLatin1String("-experimentalgestures")) {
404 opts.experimentalGestures = true;
405 } else if (!arg.startsWith(QLatin1Char('-'))) {
406 fileNames.append(arg);
407 } else if (true || arg == QLatin1String("-help")) {
412 if (!opts.scriptopts.isEmpty()) {
414 parseScriptOptions();
416 if (opts.script.isEmpty())
419 if (!(opts.scriptOptions & QDeclarativeViewer::Record) && !(opts.scriptOptions & QDeclarativeViewer::Play))
421 } else if (!opts.script.isEmpty()) {
427 static QDeclarativeViewer *createViewer()
429 Qt::WFlags wflags = (opts.frameless ? Qt::FramelessWindowHint : Qt::Widget);
431 wflags |= Qt::WindowStaysOnTopHint;
433 QDeclarativeViewer *viewer = new QDeclarativeViewer(0, wflags);
434 viewer->setAttribute(Qt::WA_DeleteOnClose, true);
435 viewer->setUseGL(opts.useGL);
437 if (!opts.scriptopts.isEmpty()) {
438 viewer->setScriptOptions(opts.scriptOptions);
439 viewer->setScript(opts.script);
442 logger = viewer->warningsWidget();
443 if (opts.warningsConfig == ShowWarnings) {
444 logger.data()->setDefaultVisibility(LoggerWidget::ShowWarnings);
445 logger.data()->show();
446 } else if (opts.warningsConfig == HideWarnings){
447 logger.data()->setDefaultVisibility(LoggerWidget::HideWarnings);
450 if (opts.experimentalGestures)
451 viewer->enableExperimentalGestures();
453 foreach (QString lib, opts.imports)
454 viewer->addLibraryPath(lib);
456 foreach (QString plugin, opts.plugins)
457 viewer->addPluginPath(plugin);
459 viewer->setNetworkCacheSize(opts.cache);
460 viewer->setRecordFile(opts.recordfile);
461 viewer->setSizeToView(opts.sizeToView);
463 viewer->setRecordRate(opts.fps);
464 if (opts.autorecord_to)
465 viewer->setAutoRecord(opts.autorecord_from, opts.autorecord_to);
467 viewer->setDeviceKeys(true);
468 viewer->setRecordDither(opts.dither);
469 if (opts.recordargs.count())
470 viewer->setRecordArgs(opts.recordargs);
472 viewer->setUseNativeFileBrowser(opts.useNativeFileBrowser);
477 void showViewer(QDeclarativeViewer *viewer)
480 viewer->showFullScreen();
481 else if (opts.maximized)
482 viewer->showMaximized();
488 QDeclarativeViewer *openFile(const QString &fileName)
490 QDeclarativeViewer *viewer = globalViewer;
492 viewer->open(fileName);
498 int main(int argc, char ** argv)
500 systemMsgOutput = qInstallMsgHandler(myMessageOutput);
501 Application app(argc, argv);
502 app.setApplicationName(QLatin1String("QtQmlViewer"));
503 app.setOrganizationName(QLatin1String("Nokia"));
504 app.setOrganizationDomain(QLatin1String("nokia.com"));
506 QDeclarativeViewer::registerTypes();
507 QDeclarativeTester::registerTypes();
509 parseCommandLineOptions(app.arguments());
511 QTranslator translator;
512 QTranslator qtTranslator;
513 QString sysLocale = QLocale::system().name();
514 if (translator.load(QLatin1String("qmlviewer_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
515 app.installTranslator(&translator);
516 if (qtTranslator.load(QLatin1String("qt_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
517 app.installTranslator(&qtTranslator);
519 app.removeTranslator(&translator);
523 QTranslator qmlTranslator;
524 if (!opts.translationFile.isEmpty()) {
525 if (qmlTranslator.load(opts.translationFile)) {
526 app.installTranslator(&qmlTranslator);
528 qWarning() << "Could not load the translation file" << opts.translationFile;
532 if (opts.fullScreen && opts.maximized)
533 qWarning() << "Both -fullscreen and -maximized specified. Using -fullscreen.";
535 if (fileNames.isEmpty()) {
536 QFile qmlapp(QLatin1String("qmlapp"));
537 if (qmlapp.exists() && qmlapp.open(QFile::ReadOnly)) {
538 QString content = QString::fromUtf8(qmlapp.readAll());
541 int newline = content.indexOf(QLatin1Char('\n'));
543 fileNames += content.left(newline);
545 fileNames += content;
549 globalViewer = createViewer();
551 if (fileNames.isEmpty()) {
552 // show the initial viewer delayed.
553 // This prevents an initial viewer popping up while there
554 // are FileOpen events coming through the event queue
555 QTimer::singleShot(1, &app, SLOT(showInitialViewer()));
557 foreach (const QString &fileName, fileNames)
561 QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));