From ac7d57a6c10c985c46c3f8f69c35eb6ef0c70d09 Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Tue, 20 Nov 2018 19:07:44 +0100
Subject: [PATCH] Clean creation on new OSG window

---
 include/gepetto/gui/mainwindow.hh |  4 ----
 src/gui/mainwindow.cc             | 27 ++++++++++-----------------
 src/gui/windows-manager.cc        |  8 +++++++-
 3 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/include/gepetto/gui/mainwindow.hh b/include/gepetto/gui/mainwindow.hh
index 618f6d4..26e5ce2 100644
--- a/include/gepetto/gui/mainwindow.hh
+++ b/include/gepetto/gui/mainwindow.hh
@@ -109,9 +109,6 @@ namespace gepetto {
 
 signals:
         void sendToBackground (WorkItem* item);
-        /// You should not need to call this function.
-        /// Use MainWindow::createView(const std::string&)
-        void createViewOnMainThread(const std::string& name);
         /// Triggered when an OSGWidget is created.
         void viewCreated (OSGWidget* widget);
         void refresh ();
@@ -268,7 +265,6 @@ signals:
 
         ActionSearchBar* actionSearchBar_;
 
-        QMutex delayedCreateView_;
         QStringList robotNames_;
         QStringList lastBodiesInCollision_;
 
diff --git a/src/gui/mainwindow.cc b/src/gui/mainwindow.cc
index aae4bae..c2fe6f2 100644
--- a/src/gui/mainwindow.cc
+++ b/src/gui/mainwindow.cc
@@ -76,8 +76,6 @@ namespace gepetto {
       osg()->createScene("hpp-gui");
 
       // Setup the main OSG widget
-      connect (this, SIGNAL (createViewOnMainThread(std::string)), SLOT (createView(std::string)));
-
       connect (ui_->actionRefresh, SIGNAL (triggered()), SLOT (requestRefresh()));
 
       connect (&backgroundQueue_, SIGNAL (done(int)), this, SLOT (handleWorkerDone(int)));
@@ -227,23 +225,18 @@ namespace gepetto {
     OSGWidget *MainWindow::createView(const std::string& name)
     {
       if (thread() != QThread::currentThread()) {
-        delayedCreateView_.lock();
-        emit createViewOnMainThread(name);
-        delayedCreateView_.lock();
-        delayedCreateView_.unlock();
-        return osgWindows_.last();
-      } else {
-        OSGWidget* osgWidget = new OSGWidget (osgViewerManagers_, name, this, 0
+        qDebug() << "createView must be called from the main thread.";
+        throw std::runtime_error("Cannot create a new window.");
+      }
+      OSGWidget* osgWidget = new OSGWidget (osgViewerManagers_, name, this, 0
 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
-            , osgViewer::Viewer::SingleThreaded
+          , osgViewer::Viewer::SingleThreaded
 #endif
-            );
-        osgWidget->setObjectName(name.c_str());
-        addOSGWidget (osgWidget);
-        emit viewCreated(osgWidget);
-        delayedCreateView_.unlock();
-        return osgWidget;
-      }
+          );
+      osgWidget->setObjectName(name.c_str());
+      addOSGWidget (osgWidget);
+      emit viewCreated(osgWidget);
+      return osgWidget;
     }
 
     void MainWindow::requestRefresh()
diff --git a/src/gui/windows-manager.cc b/src/gui/windows-manager.cc
index e8ab11f..e60f1f3 100644
--- a/src/gui/windows-manager.cc
+++ b/src/gui/windows-manager.cc
@@ -38,7 +38,13 @@ namespace gepetto {
 
     WindowsManager::WindowID WindowsManager::createWindow(const std::string& windowName)
     {
-      return MainWindow::instance()->createView(windowName)->windowID();
+      MainWindow* main = MainWindow::instance();
+      OSGWidget* widget;
+      QMetaObject::invokeMethod (main, "createView",
+          Qt::BlockingQueuedConnection,
+          Q_RETURN_ARG (OSGWidget*, widget),
+          Q_ARG (std::string, windowName));
+      return widget->windowID();
     }
 
     WindowsManager::WindowID WindowsManager::createWindow(const std::string& windowName,
-- 
GitLab