From 61cf2b0633e8af564d1368dc377da0b6869fb4c9 Mon Sep 17 00:00:00 2001
From: Shawn Rutledge <shawn.rutledge@digia.com>
Date: Mon, 18 Nov 2013 15:57:08 +0100
Subject: [PATCH] qml tool on OSX: wait for a timeout before exiting
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Double-clicking to open a QML file was not working because it would
exit if no files are given on the command line.  It needs to wait
a while for the QFileOpenEvent.

Task-number: QTBUG-34926
Change-Id: Icb585a777b0438db85120c62e7717f0f6eafffb1
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Reviewed-by: Alan Alpert <aalpert@blackberry.com>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
---
 tools/qml/main.cpp | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/tools/qml/main.cpp b/tools/qml/main.cpp
index ddcd259ae0..804241f023 100644
--- a/tools/qml/main.cpp
+++ b/tools/qml/main.cpp
@@ -72,8 +72,11 @@
 #define VERSION_MIN 0
 #define VERSION_STR "1.0"
 
+#define FILE_OPEN_EVENT_WAIT_TIME 3000 // ms
+
 static Config *conf = 0;
 static QQmlApplicationEngine *qae = 0;
+static int exitTimerId = -1;
 
 static void loadConf(const QString &override, bool quiet) // Terminates app on failure
 {
@@ -138,6 +141,8 @@ void contain(QObject *o, const QUrl &containPath)
 
 #ifdef QT_GUI_LIB
 
+void noFilesGiven();
+
 // Loads qml after receiving a QFileOpenEvent
 class LoaderApplication : public QGuiApplication
 {
@@ -146,12 +151,21 @@ public:
 
     bool event(QEvent *ev)
     {
-        if (ev->type() == QEvent::FileOpen)
+        if (ev->type() == QEvent::FileOpen) {
+            if (exitTimerId >= 0) {
+                killTimer(exitTimerId);
+                exitTimerId = -1;
+            }
             qae->load(static_cast<QFileOpenEvent *>(ev)->url());
+        }
         else
             return QGuiApplication::event(ev);
         return true;
     }
+
+    void timerEvent(QTimerEvent *) {
+        noFilesGiven();
+    }
 };
 
 #endif // QT_GUI_LIB
@@ -274,6 +288,13 @@ void printUsage()
     exit(0);
 }
 
+void noFilesGiven()
+{
+    if (!quietMode)
+        printf("qml: No files specified. Terminating.\n");
+    exit(1);
+}
+
 //Called before application initialization, removes arguments it uses
 void getAppFlags(int &argc, char **argv)
 {
@@ -462,9 +483,12 @@ int main(int argc, char *argv[])
         qInstallMessageHandler(quietMessageHandler);
 
     if (files.count() <= 0) {
-        if (!quietMode)
-            printf("qml: No files specified. Terminating.\n");
-        exit(1);
+#if defined(Q_OS_MAC)
+        if (applicationType == QmlApplicationTypeGui)
+            exitTimerId = static_cast<LoaderApplication *>(app)->startTimer(FILE_OPEN_EVENT_WAIT_TIME);
+        else
+#endif
+        noFilesGiven();
     }
 
     qae = &e;
-- 
GitLab