cf33c4e365c1a828c6816435191506defea498a0
[profile/ivi/qtdeclarative.git] / tools / qmlviewer / main.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the tools applications of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarative.h"
43 #include "qmlruntime.h"
44 #include "qdeclarativeengine.h"
45 #include "loggerwidget.h"
46 #include <QWidget>
47 #include <QDir>
48 #include <QApplication>
49 #include <QTranslator>
50 #include <QDebug>
51 #include <QMessageBox>
52 #include <QAtomicInt>
53 #include <QLibraryInfo>
54 #include "qdeclarativetester.h"
55
56 QT_USE_NAMESPACE
57
58 QtMsgHandler systemMsgOutput = 0;
59
60 static QDeclarativeViewer *openFile(const QString &fileName);
61 static void showViewer(QDeclarativeViewer *viewer);
62
63 QString warnings;
64 void exitApp(int i)
65 {
66 #ifdef Q_OS_WIN
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);
71     }
72 #endif
73     exit(i);
74 }
75
76 QWeakPointer<LoggerWidget> logger;
77 static QAtomicInt recursiveLock(0);
78
79 #if defined (Q_OS_SYMBIAN)
80 #include <unistd.h>
81 #include <sys/types.h>
82 #include <sys/stat.h>
83 #include <fcntl.h>
84 #endif
85
86 void myMessageOutput(QtMsgType type, const char *msg)
87 {
88     QString strMsg = QString::fromLatin1(msg);
89
90     if (!QCoreApplication::closingDown()) {
91         if (!logger.isNull()) {
92             if (recursiveLock.testAndSetOrdered(0, 1)) {
93                 QMetaObject::invokeMethod(logger.data(), "append", Q_ARG(QString, strMsg));
94                 recursiveLock = 0;
95             }
96         } else {
97             warnings += strMsg;
98             warnings += QLatin1Char('\n');
99         }
100     }
101 #if defined (Q_OS_SYMBIAN)
102     static int fd = -1;
103     if (fd == -1)
104         fd = ::open("E:\\qml.log", O_WRONLY | O_CREAT);
105
106     ::write(fd, msg, strlen(msg));
107     ::write(fd, "\n", 1);
108     ::fsync(fd);
109     switch (type) {
110     case QtFatalMsg:
111         abort();
112     }
113 #endif
114
115     if (systemMsgOutput) {
116         systemMsgOutput(type, msg);
117     } else { // Unix
118         fprintf(stderr, "%s\n", msg);
119         fflush(stderr);
120     }
121 }
122
123 static QDeclarativeViewer* globalViewer = 0;
124
125 // The qml file that is shown if the user didn't specify a QML file
126 QString initialFile = QLatin1String("qrc:/startup/startup.qml");
127
128 void usage()
129 {
130     qWarning("Usage: qmlviewer [options] <filename>");
131     qWarning(" ");
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)");
160 #else
161     qWarning("  -no-opengl ............................... don't use a QGLWidget for the viewport (default)");
162     qWarning("  -opengl .................................. use a QGLWidget for the viewport");
163 #endif
164     qWarning("  -script <path> ........................... set the script to use");
165     qWarning("  -scriptopts <options>|help ............... set the script options to use");
166
167     qWarning(" ");
168     qWarning(" Press F1 for interactive help");
169
170     exitApp(1);
171 }
172
173 void scriptOptsUsage()
174 {
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");
187     qWarning(" ");
188     qWarning(" One of record, play or both must be specified.");
189
190     exitApp(1);
191 }
192
193 enum WarningsConfig { ShowWarnings, HideWarnings, DefaultWarnings };
194
195 struct ViewerOptions
196 {
197     ViewerOptions()
198         : frameless(false),
199           fps(0.0),
200           autorecord_from(0),
201           autorecord_to(0),
202           dither(QLatin1String("none")),
203           runScript(false),
204           devkeys(false),
205           cache(0),
206           useGL(false),
207           fullScreen(false),
208           stayOnTop(false),
209           maximized(false),
210           useNativeFileBrowser(true),
211           experimentalGestures(false),
212           warningsConfig(DefaultWarnings),
213           sizeToView(true)
214     {
215 #if defined(Q_OS_SYMBIAN)
216         maximized = true;
217         useNativeFileBrowser = false;
218 #endif
219
220 #if defined(Q_WS_MAC)
221         useGL = true;
222 #endif
223     }
224
225     bool frameless;
226     double fps;
227     int autorecord_from;
228     int autorecord_to;
229     QString dither;
230     QString recordfile;
231     QStringList recordargs;
232     QStringList imports;
233     QStringList plugins;
234     QString script;
235     QString scriptopts;
236     bool runScript;
237     bool devkeys;
238     int cache;
239     QString translationFile;
240     bool useGL;
241     bool fullScreen;
242     bool stayOnTop;
243     bool maximized;
244     bool useNativeFileBrowser;
245     bool experimentalGestures;
246
247     WarningsConfig warningsConfig;
248     bool sizeToView;
249
250     QDeclarativeViewer::ScriptOptions scriptOptions;
251 };
252
253 static ViewerOptions opts;
254 static QStringList fileNames;
255
256 class Application : public QApplication
257 {
258     Q_OBJECT
259 public:
260     Application(int &argc, char **&argv)
261         : QApplication(argc, argv)
262     {}
263
264 protected:
265     bool event(QEvent *ev)
266     {
267         if (ev->type() != QEvent::FileOpen)
268             return QApplication::event(ev);
269
270         QFileOpenEvent *fev = static_cast<QFileOpenEvent *>(ev);
271
272         globalViewer->open(fev->file());
273         if (!globalViewer->isVisible())
274             showViewer(globalViewer);
275
276         return true;
277     }
278
279 private Q_SLOTS:
280     void showInitialViewer()
281     {
282         QApplication::processEvents();
283
284         QDeclarativeViewer *viewer = globalViewer;
285         if (!viewer)
286             return;
287         if (viewer->currentFile().isEmpty()) {
288             if(opts.useNativeFileBrowser)
289                 viewer->open(initialFile);
290             else
291                 viewer->openFile();
292         }
293         if (!viewer->isVisible())
294             showViewer(viewer);
295     }
296 };
297
298 static void parseScriptOptions()
299 {
300     QStringList options =
301         opts.scriptopts.split(QLatin1Char(','), QString::SkipEmptyParts);
302
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")) {
307             scriptOptsUsage();
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;
326         } else {
327             scriptOptsUsage();
328         }
329     }
330
331     opts.scriptOptions = scriptOptions;
332 }
333
334 static void parseCommandLineOptions(const QStringList &arguments)
335 {
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('-'));
366             if (dash > 0)
367                 opts.autorecord_from = range.left(dash).toInt();
368             opts.autorecord_to = range.mid(dash+1).toInt();
369         } else if (arg == QLatin1String("-devicekeys")) {
370             opts.devkeys = true;
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);
376             exitApp(0);
377         } else if (arg == QLatin1String("-translation")) {
378             if (lastArg) usage();
379             opts.translationFile = arguments.at(++i);
380         } else if (arg == QLatin1String("-no-opengl")) {
381             opts.useGL = false;
382         } else if (arg == QLatin1String("-opengl")) {
383             opts.useGL = true;
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;
393             } else {
394                 usage();
395             }
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");
399             if (lastArg) {
400                 QDeclarativeEngine tmpEngine;
401                 QString paths = tmpEngine.importPathList().join(QLatin1String(":"));
402                 qWarning("Current search path: %s", paths.toLocal8Bit().constData());
403                 exitApp(0);
404             }
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")) {
432             usage();
433         }
434     }
435
436     if (!opts.scriptopts.isEmpty()) {
437
438         parseScriptOptions();
439
440         if (opts.script.isEmpty())
441             usage();
442
443         if (!(opts.scriptOptions & QDeclarativeViewer::Record) && !(opts.scriptOptions & QDeclarativeViewer::Play))
444             scriptOptsUsage();
445     }  else if (!opts.script.isEmpty()) {
446         usage();
447     }
448
449 }
450
451 static QDeclarativeViewer *createViewer()
452 {
453     Qt::WFlags wflags = (opts.frameless ? Qt::FramelessWindowHint : Qt::Widget);
454     if (opts.stayOnTop)
455         wflags |= Qt::WindowStaysOnTopHint;
456
457     QDeclarativeViewer *viewer = new QDeclarativeViewer(0, wflags);
458     viewer->setAttribute(Qt::WA_DeleteOnClose, true);
459     viewer->setUseGL(opts.useGL);
460
461     if (!opts.scriptopts.isEmpty()) {
462         viewer->setScriptOptions(opts.scriptOptions);
463         viewer->setScript(opts.script);
464     }
465
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);
472     }
473
474     if (opts.experimentalGestures)
475         viewer->enableExperimentalGestures();
476
477     foreach (QString lib, opts.imports)
478         viewer->addLibraryPath(lib);
479
480     foreach (QString plugin, opts.plugins)
481         viewer->addPluginPath(plugin);
482
483     viewer->setNetworkCacheSize(opts.cache);
484     viewer->setRecordFile(opts.recordfile);
485     viewer->setSizeToView(opts.sizeToView);
486     if (opts.fps > 0)
487         viewer->setRecordRate(opts.fps);
488     if (opts.autorecord_to)
489         viewer->setAutoRecord(opts.autorecord_from, opts.autorecord_to);
490     if (opts.devkeys)
491         viewer->setDeviceKeys(true);
492     viewer->setRecordDither(opts.dither);
493     if (opts.recordargs.count())
494         viewer->setRecordArgs(opts.recordargs);
495
496     viewer->setUseNativeFileBrowser(opts.useNativeFileBrowser);
497
498     return viewer;
499 }
500
501 void showViewer(QDeclarativeViewer *viewer)
502 {
503     if (opts.fullScreen)
504         viewer->showFullScreen();
505     else if (opts.maximized)
506         viewer->showMaximized();
507     else
508         viewer->show();
509     viewer->raise();
510 }
511
512 QDeclarativeViewer *openFile(const QString &fileName)
513 {
514     QDeclarativeViewer *viewer = globalViewer;
515
516     viewer->open(fileName);
517     showViewer(viewer);
518
519     return viewer;
520 }
521
522 int main(int argc, char ** argv)
523 {
524     systemMsgOutput = qInstallMsgHandler(myMessageOutput);
525
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")) {
532             gsSpecified = true;
533             break;
534         }
535     }
536
537     if (!gsSpecified)
538         QApplication::setGraphicsSystem(QLatin1String("raster"));
539 #endif
540
541     Application app(argc, argv);
542     app.setApplicationName(QLatin1String("QtQmlViewer"));
543     app.setOrganizationName(QLatin1String("Nokia"));
544     app.setOrganizationDomain(QLatin1String("nokia.com"));
545
546     QDeclarativeViewer::registerTypes();
547     QDeclarativeTester::registerTypes();
548
549     parseCommandLineOptions(app.arguments());
550
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);
558         } else {
559             app.removeTranslator(&translator);
560         }
561     }
562
563     QTranslator qmlTranslator;
564     if (!opts.translationFile.isEmpty()) {
565         if (qmlTranslator.load(opts.translationFile)) {
566             app.installTranslator(&qmlTranslator);
567         } else {
568             qWarning() << "Could not load the translation file" << opts.translationFile;
569         }
570     }
571
572     if (opts.fullScreen && opts.maximized)
573         qWarning() << "Both -fullscreen and -maximized specified. Using -fullscreen.";
574
575     if (fileNames.isEmpty()) {
576         QFile qmlapp(QLatin1String("qmlapp"));
577         if (qmlapp.exists() && qmlapp.open(QFile::ReadOnly)) {
578             QString content = QString::fromUtf8(qmlapp.readAll());
579             qmlapp.close();
580
581             int newline = content.indexOf(QLatin1Char('\n'));
582             if (newline >= 0)
583                 fileNames += content.left(newline);
584             else
585                 fileNames += content;
586         }
587     }
588
589     globalViewer = createViewer();
590
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()));
596     } else {
597         foreach (const QString &fileName, fileNames)
598             openFile(fileName);
599     }
600
601     QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
602
603     return app.exec();
604 }
605
606 #include "main.moc"