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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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"
55 #include <private/qdeclarativedebughelper_p.h>
59 QtMsgHandler systemMsgOutput = 0;
61 static QDeclarativeViewer *openFile(const QString &fileName);
62 static void showViewer(QDeclarativeViewer *viewer);
68 // Debugging output is not visible by default on Windows -
69 // therefore show modal dialog with errors instead.
70 if (!warnings.isEmpty()) {
71 QMessageBox::warning(0, QApplication::translate("QDeclarativeViewer", "Qt QML Viewer"), warnings);
77 QWeakPointer<LoggerWidget> logger;
78 static QAtomicInt recursiveLock(0);
80 #if defined (Q_OS_SYMBIAN)
82 #include <sys/types.h>
87 void myMessageOutput(QtMsgType type, const char *msg)
89 QString strMsg = QString::fromLatin1(msg);
91 if (!QCoreApplication::closingDown()) {
92 if (!logger.isNull()) {
93 if (recursiveLock.testAndSetOrdered(0, 1)) {
94 QMetaObject::invokeMethod(logger.data(), "append", Q_ARG(QString, strMsg));
99 warnings += QLatin1Char('\n');
102 #if defined (Q_OS_SYMBIAN)
105 fd = ::open("E:\\qml.log", O_WRONLY | O_CREAT);
107 ::write(fd, msg, strlen(msg));
108 ::write(fd, "\n", 1);
116 if (systemMsgOutput) {
117 systemMsgOutput(type, msg);
119 fprintf(stderr, "%s\n", msg);
124 static QDeclarativeViewer* globalViewer = 0;
126 // The qml file that is shown if the user didn't specify a QML file
127 QString initialFile = QLatin1String("qrc:/startup/startup.qml");
131 qWarning("Usage: qmlviewer [options] <filename>");
133 qWarning(" options:");
134 qWarning(" -v, -version ............................. display version");
135 qWarning(" -frameless ............................... run with no window frame");
136 qWarning(" -maximized................................ run maximized");
137 qWarning(" -fullscreen............................... run fullscreen");
138 qWarning(" -stayontop................................ keep viewer window on top");
139 qWarning(" -sizeviewtorootobject .................... the view resizes to the changes in the content");
140 qWarning(" -sizerootobjecttoview .................... the content resizes to the changes in the view (default)");
141 qWarning(" -qmlbrowser .............................. use a QML-based file browser");
142 qWarning(" -warnings [show|hide]..................... show warnings in a separate log window");
143 qWarning(" -recordfile <output> ..................... set video recording file");
144 qWarning(" - ImageMagick 'convert' for GIF)");
145 qWarning(" - png file for raw frames");
146 qWarning(" - 'ffmpeg' for other formats");
147 qWarning(" -recorddither ordered|threshold|floyd .... set GIF dither recording mode");
148 qWarning(" -recordrate <fps> ........................ set recording frame rate");
149 qWarning(" -record arg .............................. add a recording process argument");
150 qWarning(" -autorecord [from-]<tomilliseconds> ...... set recording to start and stop");
151 qWarning(" -devicekeys .............................. use numeric keys (see F1)");
152 qWarning(" -dragthreshold <size> .................... set mouse drag threshold size");
153 qWarning(" -netcache <size> ......................... set disk cache to size bytes");
154 qWarning(" -translation <translationfile> ........... set the language to run in");
155 qWarning(" -I <directory> ........................... prepend to the module import search path,");
156 qWarning(" display path if <directory> is empty");
157 qWarning(" -P <directory> ........................... prepend to the plugin search path");
158 #if defined(Q_WS_MAC)
159 qWarning(" -no-opengl ............................... don't use a QGLWidget for the viewport");
160 qWarning(" -opengl .................................. use a QGLWidget for the viewport (default)");
162 qWarning(" -no-opengl ............................... don't use a QGLWidget for the viewport (default)");
163 qWarning(" -opengl .................................. use a QGLWidget for the viewport");
165 qWarning(" -script <path> ........................... set the script to use");
166 qWarning(" -scriptopts <options>|help ............... set the script options to use");
169 qWarning(" Press F1 for interactive help");
174 void scriptOptsUsage()
176 qWarning("Usage: qmlviewer -scriptopts <option>[,<option>...] ...");
177 qWarning(" options:");
178 qWarning(" record ................................... record a new script");
179 qWarning(" play ..................................... playback an existing script");
180 qWarning(" testimages ............................... record images or compare images on playback");
181 qWarning(" testerror ................................ test 'error' property of root item on playback");
182 qWarning(" testskip ................................ test 'skip' property of root item on playback");
183 qWarning(" snapshot ................................. file being recorded is static,");
184 qWarning(" only one frame will be recorded or tested");
185 qWarning(" exitoncomplete ........................... cleanly exit the viewer on script completion");
186 qWarning(" exitonfailure ............................ immediately exit the viewer on script failure");
187 qWarning(" saveonexit ............................... save recording on viewer exit");
189 qWarning(" One of record, play or both must be specified.");
194 enum WarningsConfig { ShowWarnings, HideWarnings, DefaultWarnings };
203 dither(QLatin1String("none")),
211 useNativeFileBrowser(true),
212 experimentalGestures(false),
213 warningsConfig(DefaultWarnings),
216 #if defined(Q_OS_SYMBIAN)
218 useNativeFileBrowser = false;
221 #if defined(Q_WS_MAC)
232 QStringList recordargs;
240 QString translationFile;
245 bool useNativeFileBrowser;
246 bool experimentalGestures;
248 WarningsConfig warningsConfig;
251 QDeclarativeViewer::ScriptOptions scriptOptions;
254 static ViewerOptions opts;
255 static QStringList fileNames;
257 class Application : public QApplication
261 Application(int &argc, char **&argv)
262 : QApplication(argc, argv)
266 bool event(QEvent *ev)
268 if (ev->type() != QEvent::FileOpen)
269 return QApplication::event(ev);
271 QFileOpenEvent *fev = static_cast<QFileOpenEvent *>(ev);
273 globalViewer->open(fev->file());
274 if (!globalViewer->isVisible())
275 showViewer(globalViewer);
281 void showInitialViewer()
283 QApplication::processEvents();
285 QDeclarativeViewer *viewer = globalViewer;
288 if (viewer->currentFile().isEmpty()) {
289 if(opts.useNativeFileBrowser)
290 viewer->open(initialFile);
294 if (!viewer->isVisible())
299 static void parseScriptOptions()
301 QStringList options =
302 opts.scriptopts.split(QLatin1Char(','), QString::SkipEmptyParts);
304 QDeclarativeViewer::ScriptOptions scriptOptions = 0;
305 for (int i = 0; i < options.count(); ++i) {
306 const QString &option = options.at(i);
307 if (option == QLatin1String("help")) {
309 } else if (option == QLatin1String("play")) {
310 scriptOptions |= QDeclarativeViewer::Play;
311 } else if (option == QLatin1String("record")) {
312 scriptOptions |= QDeclarativeViewer::Record;
313 } else if (option == QLatin1String("testimages")) {
314 scriptOptions |= QDeclarativeViewer::TestImages;
315 } else if (option == QLatin1String("testerror")) {
316 scriptOptions |= QDeclarativeViewer::TestErrorProperty;
317 } else if (option == QLatin1String("testskip")) {
318 scriptOptions |= QDeclarativeViewer::TestSkipProperty;
319 } else if (option == QLatin1String("exitoncomplete")) {
320 scriptOptions |= QDeclarativeViewer::ExitOnComplete;
321 } else if (option == QLatin1String("exitonfailure")) {
322 scriptOptions |= QDeclarativeViewer::ExitOnFailure;
323 } else if (option == QLatin1String("saveonexit")) {
324 scriptOptions |= QDeclarativeViewer::SaveOnExit;
325 } else if (option == QLatin1String("snapshot")) {
326 scriptOptions |= QDeclarativeViewer::Snapshot;
332 opts.scriptOptions = scriptOptions;
335 static void parseCommandLineOptions(const QStringList &arguments)
337 for (int i = 1; i < arguments.count(); ++i) {
338 bool lastArg = (i == arguments.count() - 1);
339 QString arg = arguments.at(i);
340 if (arg == QLatin1String("-frameless")) {
341 opts.frameless = true;
342 } else if (arg == QLatin1String("-maximized")) {
343 opts.maximized = true;
344 } else if (arg == QLatin1String("-fullscreen")) {
345 opts.fullScreen = true;
346 } else if (arg == QLatin1String("-stayontop")) {
347 opts.stayOnTop = true;
348 } else if (arg == QLatin1String("-netcache")) {
349 if (lastArg) usage();
350 opts.cache = arguments.at(++i).toInt();
351 } else if (arg == QLatin1String("-recordrate")) {
352 if (lastArg) usage();
353 opts.fps = arguments.at(++i).toDouble();
354 } else if (arg == QLatin1String("-recordfile")) {
355 if (lastArg) usage();
356 opts.recordfile = arguments.at(++i);
357 } else if (arg == QLatin1String("-record")) {
358 if (lastArg) usage();
359 opts.recordargs << arguments.at(++i);
360 } else if (arg == QLatin1String("-recorddither")) {
361 if (lastArg) usage();
362 opts.dither = arguments.at(++i);
363 } else if (arg == QLatin1String("-autorecord")) {
364 if (lastArg) usage();
365 QString range = arguments.at(++i);
366 int dash = range.indexOf(QLatin1Char('-'));
368 opts.autorecord_from = range.left(dash).toInt();
369 opts.autorecord_to = range.mid(dash+1).toInt();
370 } else if (arg == QLatin1String("-devicekeys")) {
372 } else if (arg == QLatin1String("-dragthreshold")) {
373 if (lastArg) usage();
374 qApp->setStartDragDistance(arguments.at(++i).toInt());
375 } else if (arg == QLatin1String("-v") || arg == QLatin1String("-version")) {
376 qWarning("Qt QML Viewer version %s", QT_VERSION_STR);
378 } else if (arg == QLatin1String("-translation")) {
379 if (lastArg) usage();
380 opts.translationFile = arguments.at(++i);
381 } else if (arg == QLatin1String("-no-opengl")) {
383 } else if (arg == QLatin1String("-opengl")) {
385 } else if (arg == QLatin1String("-qmlbrowser")) {
386 opts.useNativeFileBrowser = false;
387 } else if (arg == QLatin1String("-warnings")) {
388 if (lastArg) usage();
389 QString warningsStr = arguments.at(++i);
390 if (warningsStr == QLatin1String("show")) {
391 opts.warningsConfig = ShowWarnings;
392 } else if (warningsStr == QLatin1String("hide")) {
393 opts.warningsConfig = HideWarnings;
397 } else if (arg == QLatin1String("-I") || arg == QLatin1String("-L")) {
398 if (arg == QLatin1String("-L"))
399 qWarning("-L option provided for compatibility only, use -I instead");
401 QDeclarativeEngine tmpEngine;
402 QString paths = tmpEngine.importPathList().join(QLatin1String(":"));
403 qWarning("Current search path: %s", paths.toLocal8Bit().constData());
406 opts.imports << arguments.at(++i);
407 } else if (arg == QLatin1String("-P")) {
408 if (lastArg) usage();
409 opts.plugins << arguments.at(++i);
410 } else if (arg == QLatin1String("-script")) {
411 if (lastArg) usage();
412 opts.script = arguments.at(++i);
413 } else if (arg == QLatin1String("-scriptopts")) {
414 if (lastArg) usage();
415 opts.scriptopts = arguments.at(++i);
416 } else if (arg == QLatin1String("-savescript")) {
417 if (lastArg) usage();
418 opts.script = arguments.at(++i);
419 opts.runScript = false;
420 } else if (arg == QLatin1String("-playscript")) {
421 if (lastArg) usage();
422 opts.script = arguments.at(++i);
423 opts.runScript = true;
424 } else if (arg == QLatin1String("-sizeviewtorootobject")) {
425 opts.sizeToView = false;
426 } else if (arg == QLatin1String("-sizerootobjecttoview")) {
427 opts.sizeToView = true;
428 } else if (arg == QLatin1String("-experimentalgestures")) {
429 opts.experimentalGestures = true;
430 } else if (!arg.startsWith(QLatin1Char('-'))) {
431 fileNames.append(arg);
432 } else if (true || arg == QLatin1String("-help")) {
437 if (!opts.scriptopts.isEmpty()) {
439 parseScriptOptions();
441 if (opts.script.isEmpty())
444 if (!(opts.scriptOptions & QDeclarativeViewer::Record) && !(opts.scriptOptions & QDeclarativeViewer::Play))
446 } else if (!opts.script.isEmpty()) {
452 static QDeclarativeViewer *createViewer()
454 Qt::WFlags wflags = (opts.frameless ? Qt::FramelessWindowHint : Qt::Widget);
456 wflags |= Qt::WindowStaysOnTopHint;
458 QDeclarativeViewer *viewer = new QDeclarativeViewer(0, wflags);
459 viewer->setAttribute(Qt::WA_DeleteOnClose, true);
460 viewer->setUseGL(opts.useGL);
462 if (!opts.scriptopts.isEmpty()) {
463 viewer->setScriptOptions(opts.scriptOptions);
464 viewer->setScript(opts.script);
467 logger = viewer->warningsWidget();
468 if (opts.warningsConfig == ShowWarnings) {
469 logger.data()->setDefaultVisibility(LoggerWidget::ShowWarnings);
470 logger.data()->show();
471 } else if (opts.warningsConfig == HideWarnings){
472 logger.data()->setDefaultVisibility(LoggerWidget::HideWarnings);
475 if (opts.experimentalGestures)
476 viewer->enableExperimentalGestures();
478 foreach (QString lib, opts.imports)
479 viewer->addLibraryPath(lib);
481 foreach (QString plugin, opts.plugins)
482 viewer->addPluginPath(plugin);
484 viewer->setNetworkCacheSize(opts.cache);
485 viewer->setRecordFile(opts.recordfile);
486 viewer->setSizeToView(opts.sizeToView);
488 viewer->setRecordRate(opts.fps);
489 if (opts.autorecord_to)
490 viewer->setAutoRecord(opts.autorecord_from, opts.autorecord_to);
492 viewer->setDeviceKeys(true);
493 viewer->setRecordDither(opts.dither);
494 if (opts.recordargs.count())
495 viewer->setRecordArgs(opts.recordargs);
497 viewer->setUseNativeFileBrowser(opts.useNativeFileBrowser);
502 void showViewer(QDeclarativeViewer *viewer)
505 viewer->showFullScreen();
506 else if (opts.maximized)
507 viewer->showMaximized();
513 QDeclarativeViewer *openFile(const QString &fileName)
515 QDeclarativeViewer *viewer = globalViewer;
517 viewer->open(fileName);
523 int main(int argc, char ** argv)
525 systemMsgOutput = qInstallMsgHandler(myMessageOutput);
527 #if defined (Q_WS_X11) || defined (Q_WS_MAC)
528 //### default to using raster graphics backend for now
529 bool gsSpecified = false;
530 for (int i = 0; i < argc; ++i) {
531 QString arg = QString::fromAscii(argv[i]);
532 if (arg == QLatin1String("-graphicssystem")) {
539 QApplication::setGraphicsSystem(QLatin1String("raster"));
542 QDeclarativeDebugHelper::enableDebugging();
544 Application app(argc, argv);
545 app.setApplicationName(QLatin1String("QtQmlViewer"));
546 app.setOrganizationName(QLatin1String("Nokia"));
547 app.setOrganizationDomain(QLatin1String("nokia.com"));
549 QDeclarativeViewer::registerTypes();
550 QDeclarativeTester::registerTypes();
552 parseCommandLineOptions(app.arguments());
554 QTranslator translator;
555 QTranslator qtTranslator;
556 QString sysLocale = QLocale::system().name();
557 if (translator.load(QLatin1String("qmlviewer_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
558 app.installTranslator(&translator);
559 if (qtTranslator.load(QLatin1String("qt_") + sysLocale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
560 app.installTranslator(&qtTranslator);
562 app.removeTranslator(&translator);
566 QTranslator qmlTranslator;
567 if (!opts.translationFile.isEmpty()) {
568 if (qmlTranslator.load(opts.translationFile)) {
569 app.installTranslator(&qmlTranslator);
571 qWarning() << "Could not load the translation file" << opts.translationFile;
575 if (opts.fullScreen && opts.maximized)
576 qWarning() << "Both -fullscreen and -maximized specified. Using -fullscreen.";
578 if (fileNames.isEmpty()) {
579 QFile qmlapp(QLatin1String("qmlapp"));
580 if (qmlapp.exists() && qmlapp.open(QFile::ReadOnly)) {
581 QString content = QString::fromUtf8(qmlapp.readAll());
584 int newline = content.indexOf(QLatin1Char('\n'));
586 fileNames += content.left(newline);
588 fileNames += content;
592 globalViewer = createViewer();
594 if (fileNames.isEmpty()) {
595 // show the initial viewer delayed.
596 // This prevents an initial viewer popping up while there
597 // are FileOpen events coming through the event queue
598 QTimer::singleShot(1, &app, SLOT(showInitialViewer()));
600 foreach (const QString &fileName, fileNames)
604 QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));