Initial import from qtquick2.
[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 ** 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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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 "qdeclarativetester.h"
54 #include <private/qdeclarativedebughelper_p.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::tr("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 = "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("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 == "-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('-');
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 == "-devicekeys") {
370             opts.devkeys = true;
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);
376             exitApp(0);
377         } else if (arg == "-translation") {
378             if (lastArg) usage();
379             opts.translationFile = arguments.at(++i);
380         } else if (arg == "-no-opengl") {
381             opts.useGL = false;
382         } else if (arg == "-opengl") {
383             opts.useGL = true;
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;
393             } else {
394                 usage();
395             }
396         } else if (arg == "-I" || arg == "-L") {
397             if (arg == "-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 == "-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") {
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     QDeclarativeDebugHelper::enableDebugging();
525
526     systemMsgOutput = qInstallMsgHandler(myMessageOutput);
527
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") {
534             gsSpecified = true;
535             break;
536         }
537     }
538
539     if (!gsSpecified)
540         QApplication::setGraphicsSystem("raster");
541 #endif
542
543     QDeclarativeDebugHelper::enableDebugging();
544
545     Application app(argc, argv);
546     app.setApplicationName("QtQmlViewer");
547     app.setOrganizationName("Nokia");
548     app.setOrganizationDomain("nokia.com");
549
550     QDeclarativeViewer::registerTypes();
551     QDeclarativeTester::registerTypes();
552
553     parseCommandLineOptions(app.arguments());
554
555     QTranslator qmlTranslator;
556     if (!opts.translationFile.isEmpty()) {
557         if (qmlTranslator.load(opts.translationFile)) {
558             app.installTranslator(&qmlTranslator);
559         } else {
560             qWarning() << "Could not load the translation file" << opts.translationFile;
561         }
562     }
563
564     if (opts.fullScreen && opts.maximized)
565         qWarning() << "Both -fullscreen and -maximized specified. Using -fullscreen.";
566
567     if (fileNames.isEmpty()) {
568         QFile qmlapp(QLatin1String("qmlapp"));
569         if (qmlapp.exists() && qmlapp.open(QFile::ReadOnly)) {
570             QString content = QString::fromUtf8(qmlapp.readAll());
571             qmlapp.close();
572
573             int newline = content.indexOf(QLatin1Char('\n'));
574             if (newline >= 0)
575                 fileNames += content.left(newline);
576             else
577                 fileNames += content;
578         }
579     }
580
581     globalViewer = createViewer();
582
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()));
588     } else {
589         foreach (const QString &fileName, fileNames)
590             openFile(fileName);
591     }
592
593     QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
594
595     return app.exec();
596 }
597
598 #include "main.moc"