From 757e4d1178f11d146648d3d208bf7c43bed65949 Mon Sep 17 00:00:00 2001
From: Mathias Paulin <mathias.paulin@irit.fr>
Date: Sat, 3 Apr 2021 10:04:05 +0200
Subject: [PATCH] Make the application handle Radium Picking results.

---
 src/Mara/RadiumPlayer.cpp | 33 +++++++++++++++++++++++++++++++++
 src/Mara/RadiumPlayer.hpp |  7 +++++++
 2 files changed, 40 insertions(+)

diff --git a/src/Mara/RadiumPlayer.cpp b/src/Mara/RadiumPlayer.cpp
index 6577cde..230ed0d 100644
--- a/src/Mara/RadiumPlayer.cpp
+++ b/src/Mara/RadiumPlayer.cpp
@@ -4,6 +4,7 @@
 #include <Engine/Rendering/ForwardRenderer.hpp>
 #include <Engine/Rendering/RenderObject.hpp>
 #include <Engine/Rendering/RenderObjectManager.hpp>
+#include <Gui/SelectionManager/SelectionManager.hpp>
 #include <Gui/Viewer/Viewer.hpp>
 
 #include <VolumeLoader/VolumeLoader.hpp>
@@ -74,6 +75,34 @@ void RadiumPlayer::parseCommandLine() {
     if ( parser.isSet( fileOpt ) ) { m_fileOption = parser.values( fileOpt ); }
 }
 
+void RadiumPlayer::toggleCirclePicking( bool on ) {
+    auto mainWindow = dynamic_cast<MainWindow*>( m_mainWindow.get() );
+    mainWindow->centralWidget()->setMouseTracking( on );
+}
+
+void RadiumPlayer::handlePicking(
+    const Ra::Engine::Rendering::Renderer::PickingResult& pickingResult ) {
+    Ra::Core::Utils::Index roIndex( pickingResult.m_roIdx );
+    Ra::Engine::RadiumEngine* engine = Ra::Engine::RadiumEngine::getInstance();
+    auto mainWindow                  = dynamic_cast<MainWindow*>( m_mainWindow.get() );
+    if ( roIndex.isValid() )
+    {
+        auto ro = engine->getRenderObjectManager()->getRenderObject( roIndex );
+        if ( ro->getType() != Ra::Engine::Rendering::RenderObjectType::UI )
+        {
+            Ra::Engine::Scene::Component* comp = ro->getComponent();
+            Ra::Engine::Scene::Entity* ent     = comp->getEntity();
+
+            // For now we don't enable group selection.
+            mainWindow->getSelectionManager()->setCurrentEntry(
+                Ra::Engine::Scene::ItemEntry( ent, comp, roIndex ),
+                QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Current );
+        }
+    }
+    else
+    { mainWindow->getSelectionManager()->clear(); }
+}
+
 void RadiumPlayer::addConnections() {
 
     auto mainWindow = dynamic_cast<MainWindow*>( m_mainWindow.get() );
@@ -89,6 +118,10 @@ void RadiumPlayer::addConnections() {
     connect(
         this, &Ra::Gui::BaseApplication::updateFrameStats, this, &RadiumPlayer::updateFrameStats );
     framesCountForStatsChanged( 25 );
+
+    connect(
+        m_viewer, &Ra::Gui::Viewer::toggleBrushPicking, this, &RadiumPlayer::toggleCirclePicking );
+    connect( m_viewer, &Ra::Gui::Viewer::rightClickPicking, this, &RadiumPlayer::handlePicking );
 }
 
 void RadiumPlayer::addPluginsDir() {
diff --git a/src/Mara/RadiumPlayer.hpp b/src/Mara/RadiumPlayer.hpp
index f562e67..baa27ab 100644
--- a/src/Mara/RadiumPlayer.hpp
+++ b/src/Mara/RadiumPlayer.hpp
@@ -42,5 +42,12 @@ class RadiumPlayer : public Ra::Gui::BaseApplication
 
     /// Current file. First indicate that the file (second) must be loaded at startup
     std::optional<QStringList> m_fileOption{std::nullopt};
+
+  private slots:
+    /// Allow to pick using a circle
+    void toggleCirclePicking( bool on );
+
+    /// Slot for the picking results from the viewer.
+    void handlePicking( const Ra::Engine::Rendering::Renderer::PickingResult& pickingResult );
 };
 } // namespace Mara
-- 
GitLab