From 32bcf4921380ee424b6759f65ea29923a11f48c8 Mon Sep 17 00:00:00 2001
From: Mathias Paulin <mathias.paulin@irit.fr>
Date: Wed, 17 Mar 2021 09:52:36 +0100
Subject: [PATCH] Sync to Radium PR #717

---
 src/Mara/Gui/MainWindow.cpp | 48 ++++++++++++++++---------------------
 src/Mara/Gui/MainWindow.hpp |  9 +++++--
 2 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/src/Mara/Gui/MainWindow.cpp b/src/Mara/Gui/MainWindow.cpp
index 378d67e..bef5538 100644
--- a/src/Mara/Gui/MainWindow.cpp
+++ b/src/Mara/Gui/MainWindow.cpp
@@ -126,23 +126,10 @@ Viewer* MainWindow::getViewer() {
 
 void MainWindow::onFrameComplete() {}
 
-void MainWindow::postLoadFile( const std::string& filename ) {
-    m_viewer->makeCurrent();
-    m_viewer->getRenderer()->buildAllRenderTechniques();
-    m_viewer->doneCurrent();
-    m_selectionManager->clear();
-
-    auto romgr = Ra::Engine::RadiumEngine::getInstance()->getRenderObjectManager();
-
-    auto polyCount   = romgr->getNumFaces();
-    auto vertexCount = romgr->getNumVertices();
-    auto objectCount = romgr->getRenderObjectsCount();
-    auto sceneStats  = std::make_tuple( objectCount, polyCount, vertexCount );
-    m_controlWindow->setFileInfo( filename, sceneStats );
-
-    // TODO : find a better way to activate loaded camera
-    // If a camera is in the loaded scene, use it, else, use default
-    std::string loadedEntityName = Ra::Core::Utils::getBaseName( filename, false );
+
+void MainWindow::activateCamera( const std::string& sceneName ) {
+    // If a camera is in the given scene, use it, else, use default
+    std::string loadedEntityName = Ra::Core::Utils::getBaseName( sceneName, false );
     auto rootEntity =
         Ra::Engine::RadiumEngine::getInstance()->getEntityManager()->getEntity( loadedEntityName );
     if ( rootEntity != nullptr )
@@ -155,20 +142,18 @@ void MainWindow::postLoadFile( const std::string& filename ) {
         {
             LOG( Ra::Core::Utils::logINFO ) << "Activating camera " << ( *fc )->getName();
 
-            const auto systemEntity = SystemEntity::getInstance();
+            const auto systemEntity = Ra::Engine::Scene::SystemEntity::getInstance();
             systemEntity->removeComponent( "CAMERA_DEFAULT" );
 
-            m_initialCamera = static_cast<Camera*>( ( *fc ).get() );
-            auto c          = m_initialCamera->duplicate( systemEntity, "CAMERA_DEFAULT" );
-            c->resize( m_viewer->width(), m_viewer->height() );
-            m_viewer->getCameraManipulator()->setCamera( c );
-            emit frameUpdate();
+            auto camera = static_cast<Ra::Engine::Scene::Camera*>( ( *fc ).get() );
+            m_viewer->getCameraManipulator()->setCamera(
+                camera->duplicate( systemEntity, "CAMERA_DEFAULT" ) );
         }
-        else
-        { fitCamera(); }
     }
-    else
-    { fitCamera(); }
+}
+
+void MainWindow::prepareDisplay() {
+    if ( m_viewer->prepareDisplay() ) { emit frameUpdate(); }
 }
 
 void MainWindow::addControl( const QString& tabName, const QKeySequence& shortcut ) {
@@ -225,7 +210,16 @@ void MainWindow::loadFile() {
         {
             emit fileLoading( file );
         }
+
+        auto romgr = Ra::Engine::RadiumEngine::getInstance()->getRenderObjectManager();
+        auto polyCount   = romgr->getNumFaces();
+        auto vertexCount = romgr->getNumVertices();
+        auto objectCount = romgr->getRenderObjectsCount();
+        auto sceneStats  = std::make_tuple( objectCount, polyCount, vertexCount );
+        m_controlWindow->setFileInfo( pathList.first().toStdString(), sceneStats );
+        activateCamera( pathList.first().toStdString() );
     }
+
 }
 
 void MainWindow::setROVisible( Ra::Core::Utils::Index roIndex, bool visible ) {
diff --git a/src/Mara/Gui/MainWindow.hpp b/src/Mara/Gui/MainWindow.hpp
index e4a8420..c47fa85 100644
--- a/src/Mara/Gui/MainWindow.hpp
+++ b/src/Mara/Gui/MainWindow.hpp
@@ -82,8 +82,9 @@ class MainWindow : public Ra::Gui::MainWindowInterface, private Ui::MainWindow
     void timeFlow( bool state );
 
   public slots:
-    /// Call after loading a new file to let the window resetview for instance.
-    void postLoadFile( const std::string& filename ) override;
+    /// Called when a scene is ready to display to parameterize the application window and the
+    /// viewer.
+    void prepareDisplay() override;
     /// Cleanup resources.
     void cleanup() override;
 
@@ -136,6 +137,10 @@ class MainWindow : public Ra::Gui::MainWindowInterface, private Ui::MainWindow
     /// create the UI connections
     void createConnections();
 
+    /// After loading a file, set the first camera loaded (if any) as the active camera.
+    /// if multiple files are loaded, use the first camera of the first loaded file
+    void activateCamera( const std::string& sceneName );
+
     /// viewer widget
     std::unique_ptr<Ra::Gui::Viewer> m_viewer;
 
-- 
GitLab