changeset 2394:9a58798409eb refactoring

ViewportController now owns IViewportInteractor
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Nov 2026 18:46:38 +0100
parents e597503dae43
children 3fa512add2e5
files Applications/Samples/Common/RtViewerApp.h Applications/Samples/Sdl/RtViewer/RtViewerSdl.cpp Applications/Samples/Sdl/SingleViewportSdlApplication.cpp Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyViewport.cpp OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyViewport.h OrthancStone/Sources/Scene2D/Internals/OpenGLTextureProgram.cpp OrthancStone/Sources/Scene2DViewport/ViewportController.cpp OrthancStone/Sources/Scene2DViewport/ViewportController.h
diffstat 10 files changed, 53 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/Samples/Common/RtViewerApp.h	Tue Nov 03 17:46:42 2026 +0100
+++ b/Applications/Samples/Common/RtViewerApp.h	Tue Nov 03 18:46:38 2026 +0100
@@ -25,14 +25,11 @@
 #include "../../../OrthancStone/Sources/Loaders/DicomStructureSetLoader.h"
 #include "../../../OrthancStone/Sources/Loaders/OrthancMultiframeVolumeLoader.h"
 #include "../../../OrthancStone/Sources/Loaders/OrthancSeriesVolumeProgressiveLoader.h"
-#include "../../../OrthancStone/Sources/Messages/IObserver.h"
 #include "../../../OrthancStone/Sources/Messages/ObserverBase.h"
-#include "../../../OrthancStone/Sources/Oracle/OracleCommandExceptionMessage.h"
 #include "../../../OrthancStone/Sources/Scene2DViewport/ViewportController.h"
 #include "../../../OrthancStone/Sources/StoneApplication.h"
-#include "../../../OrthancStone/Sources/Viewport/DefaultViewportInteractor.h"
-#include "../../../OrthancStone/Sources/Viewport/IViewport.h"
 #include "../../../OrthancStone/Sources/Volumes/DicomVolumeImage.h"
+#include "RtViewerView.h"
 
 #include <boost/enable_shared_from_this.hpp>
 #include <boost/noncopyable.hpp>
@@ -43,22 +40,9 @@
 
 namespace OrthancStone
 {
-  class OpenGLCompositor;
-  class IVolumeSlicer;
-  class ILayerStyleConfigurator;
-  class DicomStructureSetLoader;
-  class IOracle;
-  class ThreadedOracle;
-  class VolumeSceneLayerSource;
-  class SdlOpenGLViewport;
-  class RtViewerView;
-   
   static const unsigned int FONT_SIZE_0 = 32;
   static const unsigned int FONT_SIZE_1 = 24;
 
-  class Scene2D;
-  class UndoStack;
-
   /**
   This application subclasses IMessageEmitter to use a mutex before forwarding Oracle messages (that
   can be sent from multiple threads)
@@ -72,8 +56,7 @@
 #if ORTHANC_ENABLE_SDL
   public:
     void RunSdl();
-    void SdlRunLoop(const std::vector<boost::shared_ptr<RtViewerView> >& views,
-                    DefaultViewportInteractor& interactor);
+    void SdlRunLoop(const std::vector<boost::shared_ptr<RtViewerView> >& views);
   private:
     void HandleApplicationEvent(const SDL_Event& event);
 #elif ORTHANC_ENABLE_WASM
--- a/Applications/Samples/Sdl/RtViewer/RtViewerSdl.cpp	Tue Nov 03 17:46:42 2026 +0100
+++ b/Applications/Samples/Sdl/RtViewer/RtViewerSdl.cpp	Tue Nov 03 18:46:38 2026 +0100
@@ -32,6 +32,7 @@
 #include "../../../../OrthancStone/Sources/StoneApplication.h"
 #include "../../../../OrthancStone/Sources/StoneException.h"
 #include "../../../../OrthancStone/Sources/StoneInitialization.h"
+#include "../../../../OrthancStone/Sources/Viewport/DefaultViewportInteractor.h"
 
 // Orthanc (a.o. for screenshot capture)
 #include <Compatibility.h>  // For std::unique_ptr<>
@@ -155,18 +156,19 @@
     {
       views_[i]->PrepareViewport();
       views_[i]->EnableGLDebugOutput();
+
+      std::unique_ptr<DefaultViewportInteractor> interactor(new DefaultViewportInteractor);
+      interactor->SetWindowingLayer(views_[i]->GetCtLayerIndex());
+      views_[i]->GetController()->AcquireInteractor(interactor.release());
     }
 
-    DefaultViewportInteractor interactor;
-
     /**
     It is very important that the Oracle (responsible for network I/O) be started before creating and firing the 
     loaders, for any command scheduled by the loader before the oracle is started will be lost.
     */
     StartLoaders();
 
-
-    SdlRunLoop(views_, interactor);
+    SdlRunLoop(views_);
   }
 
   void RtViewerView::TakeScreenshot(const std::string& target,
@@ -210,8 +212,7 @@
     return boost::shared_ptr<RtViewerView>();
   }
 
-  void RtViewerApp::SdlRunLoop(const std::vector<boost::shared_ptr<RtViewerView> >& views,
-                               DefaultViewportInteractor& interactor)
+  void RtViewerApp::SdlRunLoop(const std::vector<boost::shared_ptr<RtViewerView> >& views)
   {
     using namespace OrthancStone;
 
@@ -348,8 +349,7 @@
               switch (sdlEvent.type)
               {
               case SDL_MOUSEBUTTONDOWN:
-                interactor.SetWindowingLayer(view->GetCtLayerIndex());
-                view->GetController()->HandleMousePress(interactor, p,
+                view->GetController()->HandleMousePress(p,
                                                         lock->GetCompositor().GetCanvasWidth(),
                                                         lock->GetCompositor().GetCanvasHeight());
                 break;
--- a/Applications/Samples/Sdl/SingleViewportSdlApplication.cpp	Tue Nov 03 17:46:42 2026 +0100
+++ b/Applications/Samples/Sdl/SingleViewportSdlApplication.cpp	Tue Nov 03 18:46:38 2026 +0100
@@ -37,11 +37,7 @@
     assert(core_.get() != NULL);
     core_->CreateComponents(context, viewport_, controller_);
 
-    std::unique_ptr<IViewportInteractor> interactor(core_->CreateMouseInteractor());
-    if (interactor.get() == NULL)
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
-    }
+    controller_->AcquireInteractor(core_->CreateMouseInteractor());
 
     if (startup_.get() != NULL)
     {
@@ -118,25 +114,13 @@
             switch (event.type)
             {
             case SDL_MOUSEBUTTONDOWN:
-            {
-              controller_->HandleMousePress(*interactor, p, viewportLock->GetCompositor().GetCanvasWidth(),
+              controller_->HandleMousePress(p, viewportLock->GetCompositor().GetCanvasWidth(),
                                             viewportLock->GetCompositor().GetCanvasHeight());
               break;
-            }
 
             case SDL_MOUSEMOTION:
-            {
-              if (controller_->HasActiveTracker())
-              {
-                controller_->HandleMouseMove(p);
-              }
-              else if (interactor->HasMouseHover())
-              {
-                interactor->HandleMouseHover(controller_->GetScene(), p);
-                viewportLock->Invalidate();
-              }
+              controller_->HandleMouseMove(p);
               break;
-            }
 
             case SDL_MOUSEBUTTONUP:
               controller_->HandleMouseRelease(p);
--- a/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp	Tue Nov 03 17:46:42 2026 +0100
+++ b/Applications/Samples/WebAssembly/SingleFrameViewer/SingleFrameViewer.cpp	Tue Nov 03 18:46:38 2026 +0100
@@ -143,7 +143,7 @@
 
         core_->CreateComponents(GetContext(), viewport_, viewport_->GetController());
 
-        viewport_->AcquireInteractor(core_->CreateMouseInteractor());
+        viewport_->GetController()->AcquireInteractor(core_->CreateMouseInteractor());
 
         emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, EM_FALSE, OnKeyDown);
       }
--- a/Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp	Tue Nov 03 17:46:42 2026 +0100
+++ b/Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp	Tue Nov 03 18:46:38 2026 +0100
@@ -4066,7 +4066,7 @@
                              WebViewerAction rightAction)
   {
     assert(viewport_ != NULL);
-    viewport_->AcquireInteractor(new Interactor(*this, leftAction, middleAction, rightAction));
+    viewport_->GetController()->AcquireInteractor(new Interactor(*this, leftAction, middleAction, rightAction));
   }
 
   void FitForPrint()
--- a/OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyViewport.cpp	Tue Nov 03 17:46:42 2026 +0100
+++ b/OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyViewport.cpp	Tue Nov 03 18:46:38 2026 +0100
@@ -25,14 +25,12 @@
 #  include "WebAssemblyViewport.h"
 #  include "../../Scene2DViewport/ViewportController.h"
 #  include "../../Toolbox/GenericToolbox.h"
-#  include "../../Viewport/DefaultViewportInteractor.h"
 #else
 // This is the case when using the WebAssembly side module, and this
 // source file must be compiled within the WebAssembly main module
 #  include <Viewport/WebAssemblyViewport.h>
 #  include <Toolbox/GenericToolbox.h>
 #  include <Scene2DViewport/ViewportController.h>
-#  include <Viewport/DefaultViewportInteractor.h>
 #endif
 
 
@@ -163,13 +161,12 @@
 
     LOG(TRACE) << "mouse down: " << that->GetCanvasCssSelector();      
 
-    if (that->compositor_.get() != NULL &&
-        that->interactor_.get() != NULL)
+    if (that->compositor_.get() != NULL)
     {
       PointerEvent pointer;
       ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
 
-      that->controller_->HandleMousePress(*that->interactor_, pointer,
+      that->controller_->HandleMousePress(pointer,
                                           that->compositor_->GetCanvasWidth(),
                                           that->compositor_->GetCanvasHeight());        
     }
@@ -185,27 +182,10 @@
 
     if (that->compositor_.get() != NULL)
     {
-      if (that->controller_->HasActiveTracker())
-      {
-        PointerEvent pointer;
-        ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
+      PointerEvent pointer;
+      ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
 
-        that->controller_->HandleMouseMove(pointer);
-      }
-      else if (that->interactor_.get() != NULL &&
-               that->interactor_->HasMouseHover())
-      {
-        // New in Stone Web viewer 2.0
-        PointerEvent pointer;
-        ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);      
-
-        that->interactor_->HandleMouseHover(that->controller_->GetScene(), pointer);
-
-        {
-          WebAssemblyViewport::WasmLock lock(*that);
-          lock.Invalidate();
-        }
-      }
+      that->controller_->HandleMouseMove(pointer);
     }
 
     LOG(TRACE) << "Exiting: " << __func__;
@@ -318,7 +298,6 @@
 #else
     canvasCssSelector_(canvasId),
 #endif
-    interactor_(new DefaultViewportInteractor),
     enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents),
     canvasWidth_(0),
     canvasHeight_(0)
@@ -331,8 +310,7 @@
   {
     WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
 
-    if (that->compositor_.get() != NULL &&
-        that->interactor_.get() != NULL)
+    if (that->compositor_.get() != NULL)
     {
       const ICompositor& compositor = *that->compositor_;
 
@@ -345,7 +323,7 @@
       switch (eventType)
       {
         case EMSCRIPTEN_EVENT_TOUCHSTART:
-          that->controller_->HandleMousePress(*that->interactor_, event,
+          that->controller_->HandleMousePress(event,
                                               that->compositor_->GetCanvasWidth(),
                                               that->compositor_->GetCanvasHeight());
           break;
@@ -472,18 +450,6 @@
     return new WasmLock(*this);
   }
   
-  void WebAssemblyViewport::AcquireInteractor(IViewportInteractor* interactor)
-  {
-    if (interactor == NULL)
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
-    }
-    else
-    {
-      interactor_.reset(interactor);
-    }
-  }
-
 
   void WebAssemblyViewport::RefreshCanvasSize()
   {
--- a/OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyViewport.h	Tue Nov 03 17:46:42 2026 +0100
+++ b/OrthancStone/Sources/Platforms/WebAssembly/WebAssemblyViewport.h	Tue Nov 03 18:46:38 2026 +0100
@@ -34,7 +34,6 @@
 #endif
 
 #include "../../Viewport/IViewport.h"
-#include "../../Viewport/IViewportInteractor.h"
 #include "../../Scene2DViewport/ViewportController.h"
 
 #include <Compatibility.h>
@@ -59,7 +58,6 @@
     std::string                           canvasCssSelector_;
     std::unique_ptr<ICompositor>          compositor_;
     boost::shared_ptr<ViewportController> controller_;
-    std::unique_ptr<IViewportInteractor>  interactor_;
     bool                                  enableEmscriptenMouseEvents_;
     unsigned int                          canvasWidth_;
     unsigned int                          canvasHeight_;
@@ -159,11 +157,6 @@
 
     ~WebAssemblyViewport();
 
-    /**
-    This method takes ownership
-    */
-    void AcquireInteractor(IViewportInteractor* interactor);
-
     const std::string& GetCanvasId() const
     {
       return canvasId_;
--- a/OrthancStone/Sources/Scene2D/Internals/OpenGLTextureProgram.cpp	Tue Nov 03 17:46:42 2026 +0100
+++ b/OrthancStone/Sources/Scene2D/Internals/OpenGLTextureProgram.cpp	Tue Nov 03 18:46:38 2026 +0100
@@ -24,6 +24,7 @@
 #include "OpenGLTextureProgram.h"
 #include "OpenGLShaderVersionDirective.h"
 
+#include <Logging.h>
 #include <OrthancException.h>
 
 static const unsigned int COMPONENTS = 2;
--- a/OrthancStone/Sources/Scene2DViewport/ViewportController.cpp	Tue Nov 03 17:46:42 2026 +0100
+++ b/OrthancStone/Sources/Scene2DViewport/ViewportController.cpp	Tue Nov 03 18:46:38 2026 +0100
@@ -191,8 +191,7 @@
   }
 
 
-  void ViewportController::HandleMousePress(IViewportInteractor&  interactor,
-                                            const PointerEvent&   event,
+  void ViewportController::HandleMousePress(const PointerEvent&   event,
                                             unsigned int          viewportWidth,
                                             unsigned int          viewportHeight)
   {
@@ -222,9 +221,10 @@
 
       // No measure tool, create new tracker from the interactor
       boost::shared_ptr<IViewport> sharedViewport(viewport_);
-      if (sharedViewport)
+      if (interactor_.get() != NULL &&
+          sharedViewport)
       {
-        activeTracker_.reset(interactor.CreateTracker(GetScene(), event, viewportWidth, viewportHeight));
+        activeTracker_.reset(interactor_->CreateTracker(GetScene(), event, viewportWidth, viewportHeight));
       }
       else
       {
@@ -243,6 +243,12 @@
     {
       InvalidateViewport();
     }
+    else if (interactor_.get() != NULL &&
+             interactor_->HasMouseHover())
+    {
+      interactor_->HandleMouseHover(GetScene(), event);
+      InvalidateViewport();
+    }
   }
 
 
@@ -324,4 +330,20 @@
     width = 0;
     height = 0;
   }
+
+
+  void ViewportController::AcquireInteractor(IViewportInteractor* interactor)
+  {
+    std::unique_ptr<IViewportInteractor> protection(interactor);
+
+    if (interactor == NULL)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
+    }
+    else
+    {
+      activeTracker_.reset();
+      interactor_.reset(protection.release());
+    }
+  }
 }
--- a/OrthancStone/Sources/Scene2DViewport/ViewportController.h	Tue Nov 03 17:46:42 2026 +0100
+++ b/OrthancStone/Sources/Scene2DViewport/ViewportController.h	Tue Nov 03 18:46:38 2026 +0100
@@ -140,8 +140,7 @@
 
 
     // Must be expressed in canvas coordinates
-    void HandleMousePress(IViewportInteractor& interactor,
-                          const PointerEvent& event,
+    void HandleMousePress(const PointerEvent& event,
                           unsigned int viewportWidth,
                           unsigned int viewportHeight);
 
@@ -198,6 +197,8 @@
     void GetCanvasSize(unsigned int& width,
                        unsigned int& height);
 
+    void AcquireInteractor(IViewportInteractor* interactor);
+
   private:
     // The scene can be used by the higher-level objects (including the
     // measuring tools), possibly in their destructor, and so it must be 
@@ -208,5 +209,6 @@
     boost::shared_ptr<UndoStack>                  undoStackW_;  // Global stack, possibly shared by all viewports
     std::vector<boost::shared_ptr<MeasureTool> >  measureTools_;  // TODO Refactoring - Make this a std::set?
     boost::shared_ptr<IFlexiblePointerTracker>    activeTracker_;  // TODO Refactoring - Couldn't this be a "std::unique_ptr"?
+    std::unique_ptr<IViewportInteractor>          interactor_;
   };
 }