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 "qdeclarativetester.h"
54 #include <private/qdeclarativedebughelper_p.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::tr("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 = "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 };
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 == "-frameless") {
340 opts.frameless = true;
341 } else if (arg == "-maximized") {
342 opts.maximized = true;
343 } else if (arg == "-fullscreen") {
344 opts.fullScreen = true;
345 } else if (arg == "-stayontop") {
346 opts.stayOnTop = true;
347 } else if (arg == "-netcache") {
348 if (lastArg) usage();
349 opts.cache = arguments.at(++i).toInt();
350 } else if (arg == "-recordrate") {
351 if (lastArg) usage();
352 opts.fps = arguments.at(++i).toDouble();
353 } else if (arg == "-recordfile") {
354 if (lastArg) usage();
355 opts.recordfile = arguments.at(++i);
356 } else if (arg == "-record") {
357 if (lastArg) usage();
358 opts.recordargs << arguments.at(++i);
359 } else if (arg == "-recorddither") {
360 if (lastArg) usage();
361 opts.dither = arguments.at(++i);
362 } else if (arg == "-autorecord") {
363 if (lastArg) usage();
364 QString range = arguments.at(++i);
365 int dash = range.indexOf('-');
367 opts.autorecord_from = range.left(dash).toInt();
368 opts.autorecord_to = range.mid(dash+1).toInt();
369 } else if (arg == "-devicekeys") {
371 } else if (arg == "-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 == "-translation") {
378 if (lastArg) usage();
379 opts.translationFile = arguments.at(++i);
380 } else if (arg == "-no-opengl") {
382 } else if (arg == "-opengl") {
384 } else if (arg == "-qmlbrowser") {
385 opts.useNativeFileBrowser = false;
386 } else if (arg == "-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 == "-I" || arg == "-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 == "-P") {
407 if (lastArg) usage();
408 opts.plugins << arguments.at(++i);
409 } else if (arg == "-script") {
410 if (lastArg) usage();
411 opts.script = arguments.at(++i);
412 } else if (arg == "-scriptopts") {
413 if (lastArg) usage();
414 opts.scriptopts = arguments.at(++i);
415 } else if (arg == "-savescript") {
416 if (lastArg) usage();
417 opts.script = arguments.at(++i);
418 opts.runScript = false;
419 } else if (arg == "-playscript") {
420 if (lastArg) usage();
421 opts.script = arguments.at(++i);
422 opts.runScript = true;
423 } else if (arg == "-sizeviewtorootobject") {
424 opts.sizeToView = false;
425 } else if (arg == "-sizerootobjecttoview") {
426 opts.sizeToView = true;
427 } else if (arg == "-experimentalgestures") {
428 opts.experimentalGestures = true;
429 } else if (!arg.startsWith('-')) {
430 fileNames.append(arg);
431 } else if (true || arg == "-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 QDeclarativeDebugHelper::enableDebugging();
526 systemMsgOutput = qInstallMsgHandler(myMessageOutput);
528 #if defined (Q_WS_X11) || defined (Q_WS_MAC)
529 //### default to using raster graphics backend for now
530 bool gsSpecified = false;
531 for (int i = 0; i < argc; ++i) {
532 QString arg = argv[i];
533 if (arg == "-graphicssystem") {
540 QApplication::setGraphicsSystem("raster");
543 QDeclarativeDebugHelper::enableDebugging();
545 Application app(argc, argv);
546 app.setApplicationName("QtQmlViewer");
547 app.setOrganizationName("Nokia");
548 app.setOrganizationDomain("nokia.com");
550 QDeclarativeViewer::registerTypes();
551 QDeclarativeTester::registerTypes();
553 parseCommandLineOptions(app.arguments());
555 QTranslator qmlTranslator;
556 if (!opts.translationFile.isEmpty()) {
557 if (qmlTranslator.load(opts.translationFile)) {
558 app.installTranslator(&qmlTranslator);
560 qWarning() << "Could not load the translation file" << opts.translationFile;
564 if (opts.fullScreen && opts.maximized)
565 qWarning() << "Both -fullscreen and -maximized specified. Using -fullscreen.";
567 if (fileNames.isEmpty()) {
568 QFile qmlapp(QLatin1String("qmlapp"));
569 if (qmlapp.exists() && qmlapp.open(QFile::ReadOnly)) {
570 QString content = QString::fromUtf8(qmlapp.readAll());
573 int newline = content.indexOf(QLatin1Char('\n'));
575 fileNames += content.left(newline);
577 fileNames += content;
581 globalViewer = createViewer();
583 if (fileNames.isEmpty()) {
584 // show the initial viewer delayed.
585 // This prevents an initial viewer popping up while there
586 // are FileOpen events coming through the event queue
587 QTimer::singleShot(1, &app, SLOT(showInitialViewer()));
589 foreach (const QString &fileName, fileNames)
593 QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));