changeset 899:a8e3d686b43e

Merge
author Alain Mazy <alain@mazy.be>
date Tue, 16 Jul 2019 12:41:18 +0200
parents a7042ad79f08 (current diff) f557b18d287f (diff)
children f9ae731fdc25 240359ab1651
files
diffstat 11 files changed, 101 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/OpenGL/WebAssemblyOpenGLContext.cpp	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/OpenGL/WebAssemblyOpenGLContext.cpp	Tue Jul 16 12:41:18 2019 +0200
@@ -49,8 +49,10 @@
         context_ = emscripten_webgl_create_context(canvas.c_str(), &attr);
         if (context_ < 0)
         {
-          throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
-                                          "Cannot create an OpenGL context for canvas: " + canvas);
+          std::string message("Cannot create an OpenGL context for canvas: ");
+          message += canvas;
+          LOG(ERROR) << message;
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, message);
         }
 
         UpdateSize();
--- a/Framework/Scene2DViewport/AngleMeasureTool.cpp	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/Scene2DViewport/AngleMeasureTool.cpp	Tue Jul 16 12:41:18 2019 +0200
@@ -298,7 +298,7 @@
 
 #if 0
           // TODO:make it togglable
-          bool enableInfoDisplay = false;
+          bool enableInfoDisplay = true;
           if (enableInfoDisplay)
           {
             TrackerSample_SetInfoDisplayMessage("center_.GetX()",
--- a/Framework/Scene2DViewport/MeasureTool.h	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/Scene2DViewport/MeasureTool.h	Tue Jul 16 12:41:18 2019 +0200
@@ -144,5 +144,5 @@
 
 }
 
-extern void TrackerSample_SetInfoDisplayMessage(
-  std::string key, std::string value);
+ //extern void TrackerSample_SetInfoDisplayMessage(
+ //  std::string key, std::string value);
--- a/Framework/Scene2DViewport/MeasureToolsToolbox.cpp	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/Scene2DViewport/MeasureToolsToolbox.cpp	Tue Jul 16 12:41:18 2019 +0200
@@ -60,17 +60,17 @@
   }
 
   void AddSquare(PolylineSceneLayer::Chain& chain,
-    boost::shared_ptr<const Scene2D>     scene,
+    const Scene2D&      scene,
     const ScenePoint2D& centerS,
     const double&       sideLengthS)
   {
     // get the scaling factor 
     const double sceneToCanvas = 
-      scene->GetSceneToCanvasTransform().ComputeZoom();
+      scene.GetSceneToCanvasTransform().ComputeZoom();
 
     chain.clear();
     chain.reserve(4);
-    ScenePoint2D centerC = centerS.Apply(scene->GetSceneToCanvasTransform());
+    ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform());
     //TODO: take DPI into account 
     double handleLX = centerC.GetX() - sideLengthS * sceneToCanvas * 0.5;
     double handleTY = centerC.GetY() - sideLengthS * sceneToCanvas * 0.5;
@@ -81,10 +81,10 @@
     ScenePoint2D RBC(handleRX, handleBY);
     ScenePoint2D LBC(handleLX, handleBY);
 
-    ScenePoint2D startLT = LTC.Apply(scene->GetCanvasToSceneTransform());
-    ScenePoint2D startRT = RTC.Apply(scene->GetCanvasToSceneTransform());
-    ScenePoint2D startRB = RBC.Apply(scene->GetCanvasToSceneTransform());
-    ScenePoint2D startLB = LBC.Apply(scene->GetCanvasToSceneTransform());
+    ScenePoint2D startLT = LTC.Apply(scene.GetCanvasToSceneTransform());
+    ScenePoint2D startRT = RTC.Apply(scene.GetCanvasToSceneTransform());
+    ScenePoint2D startRB = RBC.Apply(scene.GetCanvasToSceneTransform());
+    ScenePoint2D startLB = LBC.Apply(scene.GetCanvasToSceneTransform());
 
     chain.push_back(startLT);
     chain.push_back(startRT);
@@ -288,7 +288,7 @@
   for the actual text
   */
   void SetTextLayerOutlineProperties(
-    boost::shared_ptr<Scene2D> scene, boost::shared_ptr<LayerHolder> layerHolder, 
+    Scene2D& scene, boost::shared_ptr<LayerHolder> layerHolder, 
     const char* text, ScenePoint2D p)
   {
     double xoffsets[5] = { 2, 0, -2, 0, 0 };
@@ -296,7 +296,7 @@
 
     // get the scaling factor 
     const double pixelToScene =
-      scene->GetCanvasToSceneTransform().ComputeZoom();
+      scene.GetCanvasToSceneTransform().ComputeZoom();
 
     for (int i = 0; i < 5; ++i)
     {
--- a/Framework/Scene2DViewport/ViewportController.cpp	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/Scene2DViewport/ViewportController.cpp	Tue Jul 16 12:41:18 2019 +0200
@@ -36,7 +36,6 @@
     , undoStackW_(undoStackW)
     , canvasToSceneFactor_(0.0)
     , viewport_(viewport)
-    , scene_(viewport.GetScene())
   {
   }
 
--- a/Framework/Scene2DViewport/ViewportController.h	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/Scene2DViewport/ViewportController.h	Tue Jul 16 12:41:18 2019 +0200
@@ -181,7 +181,7 @@
 
     const Scene2D& GetScene() const
     {
-      return scene_;
+      return const_cast<IViewport&>(viewport_).GetScene();
     }
 
   private:
@@ -197,10 +197,8 @@
     
     // this is cached
     mutable double              canvasToSceneFactor_;
-
-
+    
     // Refactoring on 2019-07-10: Removing shared_ptr from scene
     IViewport&      viewport_;
-    const Scene2D&  scene_;  // As long as the viewport exists, its associated scene too   
   };
 }
--- a/Framework/Viewport/SdlViewport.cpp	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/Viewport/SdlViewport.cpp	Tue Jul 16 12:41:18 2019 +0200
@@ -28,9 +28,10 @@
 {
   SdlViewport::SdlViewport(const char* title,
                            unsigned int width,
-                           unsigned int height) :
+                           unsigned int height,
+                           bool allowDpiScaling) :
     ViewportBase(title),
-    context_(title, width, height),
+    context_(title, width, height, allowDpiScaling),
     compositor_(context_, GetScene())
   {
   }
@@ -38,9 +39,10 @@
   SdlViewport::SdlViewport(const char* title,
                            unsigned int width,
                            unsigned int height,
-                           boost::shared_ptr<Scene2D>& scene) :
+                           boost::shared_ptr<Scene2D>& scene,
+                           bool allowDpiScaling) :
     ViewportBase(title, scene),
-    context_(title, width, height),
+    context_(title, width, height, allowDpiScaling),
     compositor_(context_, GetScene())
   {
   }
--- a/Framework/Viewport/SdlViewport.h	Tue Jul 16 12:38:51 2019 +0200
+++ b/Framework/Viewport/SdlViewport.h	Tue Jul 16 12:41:18 2019 +0200
@@ -51,12 +51,14 @@
   public:
     SdlViewport(const char* title,
                 unsigned int width,
-                unsigned int height);
+                unsigned int height,
+                bool allowDpiScaling = true);
 
     SdlViewport(const char* title,
                 unsigned int width,
                 unsigned int height,
-                boost::shared_ptr<Scene2D>& scene);
+                boost::shared_ptr<Scene2D>& scene,
+                bool allowDpiScaling = true);
 
     virtual void Refresh()
     {
--- a/Samples/Sdl/FusionMprSdl.cpp	Tue Jul 16 12:38:51 2019 +0200
+++ b/Samples/Sdl/FusionMprSdl.cpp	Tue Jul 16 12:41:18 2019 +0200
@@ -82,12 +82,12 @@
     return descs[i];
   }
 
-  boost::shared_ptr<Scene2D> FusionMprSdlApp::GetScene()
+  Scene2D& FusionMprSdlApp::GetScene()
   {
     return controller_->GetScene();
   }
 
-  boost::shared_ptr<const Scene2D> FusionMprSdlApp::GetScene() const
+  const Scene2D& FusionMprSdlApp::GetScene() const
   {
     return controller_->GetScene();
   }
--- a/Samples/Sdl/TrackerSampleApp.cpp	Tue Jul 16 12:38:51 2019 +0200
+++ b/Samples/Sdl/TrackerSampleApp.cpp	Tue Jul 16 12:41:18 2019 +0200
@@ -79,9 +79,6 @@
   void TrackerSampleApp::DisplayInfoText()
   {
     // do not try to use stuff too early!
-    if (compositor_.get() == NULL)
-      return;
-
     std::stringstream msg;
 	
 	for (std::map<std::string, std::string>::const_iterator kv = infoTextMap_.begin();
@@ -111,8 +108,8 @@
     }
     // position the fixed info text in the upper right corner
     layerP->SetText(msgS.c_str());
-    double cX = compositor_->GetCanvasWidth() * (-0.5);
-    double cY = compositor_->GetCanvasHeight() * (-0.5);
+    double cX = GetCompositor().GetCanvasWidth() * (-0.5);
+    double cY = GetCompositor().GetCanvasHeight() * (-0.5);
     controller_->GetScene().GetCanvasToSceneTransform().Apply(cX,cY);
     layerP->SetPosition(cX, cY);
   }
@@ -152,12 +149,12 @@
 
   ScenePoint2D TrackerSampleApp::GetRandomPointInScene() const
   {
-    unsigned int w = compositor_->GetCanvasWidth();
-    LOG(TRACE) << "compositor_->GetCanvasWidth() = " << 
-      compositor_->GetCanvasWidth();
-    unsigned int h = compositor_->GetCanvasHeight();
-    LOG(TRACE) << "compositor_->GetCanvasHeight() = " << 
-      compositor_->GetCanvasHeight();
+    unsigned int w = GetCompositor().GetCanvasWidth();
+    LOG(TRACE) << "GetCompositor().GetCanvasWidth() = " << 
+      GetCompositor().GetCanvasWidth();
+    unsigned int h = GetCompositor().GetCanvasHeight();
+    LOG(TRACE) << "GetCompositor().GetCanvasHeight() = " << 
+      GetCompositor().GetCanvasHeight();
 
     if ((w >= RAND_MAX) || (h >= RAND_MAX))
       LOG(WARNING) << "Canvas is too big : tools will not be randomly placed";
@@ -166,7 +163,7 @@
     int y = rand() % h;
     LOG(TRACE) << "random x = " << x << "random y = " << y;
 
-    ScenePoint2D p = compositor_->GetPixelCenterCoordinates(x, y);
+    ScenePoint2D p = controller_->GetViewport().GetPixelCenterCoordinates(x, y);
     LOG(TRACE) << "--> p.GetX() = " << p.GetX() << " p.GetY() = " << p.GetY();
 
     ScenePoint2D r = p.Apply(controller_->GetScene().GetCanvasToSceneTransform());
@@ -232,7 +229,7 @@
         // The "left-ctrl" key is down, while no tracker is present
         // Let's display the info text
         PointerEvent e;
-        e.AddPosition(compositor_->GetPixelCenterCoordinates(
+        e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(
           event.button.x, event.button.y));
 
         DisplayFloatingCtrlInfoText(e);
@@ -245,7 +242,7 @@
         {
           //LOG(TRACE) << "(activeTracker_.get() != NULL)";
           PointerEvent e;
-          e.AddPosition(compositor_->GetPixelCenterCoordinates(
+          e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(
             event.button.x, event.button.y));
           
           //LOG(TRACE) << "event.button.x = " << event.button.x << "     " <<
@@ -263,7 +260,7 @@
         HideInfoText();
 
         PointerEvent e;
-        e.AddPosition(compositor_->GetPixelCenterCoordinates(event.button.x, event.button.y));
+        e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(event.button.x, event.button.y));
 
         ScenePoint2D scenePos = e.GetMainPosition().Apply(
           controller_->GetScene().GetCanvasToSceneTransform());
@@ -290,7 +287,7 @@
       if (activeTracker_)
       {
         PointerEvent e;
-        e.AddPosition(compositor_->GetPixelCenterCoordinates(event.button.x, event.button.y));
+        e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(event.button.x, event.button.y));
         activeTracker_->PointerUp(e);
         if (!activeTracker_->IsAlive())
           activeTracker_.reset();
@@ -299,7 +296,7 @@
     else if (event.type == SDL_MOUSEBUTTONDOWN)
     {
       PointerEvent e;
-      e.AddPosition(compositor_->GetPixelCenterCoordinates(
+      e.AddPosition(controller_->GetViewport().GetPixelCenterCoordinates(
         event.button.x, event.button.y));
       if (activeTracker_)
       {
@@ -341,8 +338,8 @@
         CreateRandomMeasureTool();
         break;
       case SDLK_s:
-        controller_->FitContent(compositor_->GetCanvasWidth(),
-          compositor_->GetCanvasHeight());
+        controller_->FitContent(GetCompositor().GetCanvasWidth(),
+          GetCompositor().GetCanvasHeight());
         break;
 
       case SDLK_z:
@@ -380,8 +377,8 @@
       case SDLK_c:
         TakeScreenshot(
           "screenshot.png",
-          compositor_->GetCanvasWidth(),
-          compositor_->GetCanvasHeight());
+          GetCompositor().GetCanvasWidth(),
+          GetCompositor().GetCanvasHeight());
         break;
 
       default:
@@ -411,7 +408,7 @@
 
     case SDL_BUTTON_RIGHT:
       return boost::shared_ptr<IFlexiblePointerTracker>(new ZoomSceneTracker
-        (controller_, e, compositor_->GetCanvasHeight()));
+        (controller_, e, GetCompositor().GetCanvasHeight()));
 
     case SDL_BUTTON_LEFT:
     {
@@ -443,7 +440,7 @@
             controller_, e));
         case GuiTool_Zoom:
           return boost::shared_ptr<IFlexiblePointerTracker>(new ZoomSceneTracker(
-            controller_, e, compositor_->GetCanvasHeight()));
+            controller_, e, GetCompositor().GetCanvasHeight()));
         //case GuiTool_AngleMeasure:
         //  return new AngleMeasureTracker(GetScene(), e);
         //case GuiTool_CircleMeasure:
@@ -476,9 +473,10 @@
   TrackerSampleApp::TrackerSampleApp(MessageBroker& broker) : IObserver(broker)
     , currentTool_(GuiTool_Rotate)
     , undoStack_(new UndoStack)
+    , viewport_("Hello", 1024, 1024, false) // False means we do NOT let Windows treat this as a legacy application that needs to be scaled
   {
     controller_ = boost::shared_ptr<ViewportController>(
-      new ViewportController(undoStack_, broker));
+      new ViewportController(undoStack_, broker, viewport_));
 
     controller_->RegisterObserverCallback(
       new Callable<TrackerSampleApp, ViewportController::SceneTransformChanged>
@@ -594,7 +592,7 @@
     unsigned int canvasWidth,
     unsigned int canvasHeight)
   {
-    CairoCompositor compositor(*GetScene(), canvasWidth, canvasHeight);
+    CairoCompositor compositor(controller_->GetScene(), canvasWidth, canvasHeight);
     compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE_0, Orthanc::Encoding_Latin1);
     compositor.Refresh();
 
@@ -643,27 +641,50 @@
 
   static bool g_stopApplication = false;
   
+  OpenGLCompositor& TrackerSampleApp::GetCompositor()
+  {
+    using namespace Orthanc;
+    try
+    {
+      SdlViewport& viewport = dynamic_cast<SdlViewport&>(viewport_);
+      return viewport.GetCompositor();
+    }
+    catch (std::bad_cast e)
+    {
+      throw OrthancException(ErrorCode_InternalError, "Wrong viewport type!");
+     }
+  }
+
+  const OpenGLCompositor& TrackerSampleApp::GetCompositor() const
+  {
+    using namespace Orthanc;
+    try
+    {
+      SdlViewport& viewport = const_cast<SdlViewport&>(dynamic_cast<const SdlViewport&>(viewport_));
+      return viewport.GetCompositor();
+    }
+    catch (std::bad_cast e)
+    {
+      throw OrthancException(ErrorCode_InternalError, "Wrong viewport type!");
+    }
+  }
+
+
   void TrackerSampleApp::Run()
   {
-    // False means we do NOT let Windows treat this as a legacy application
-    // that needs to be scaled
-    SdlOpenGLContext window("Hello", 1024, 1024, false);
-
-    controller_->FitContent(window.GetCanvasWidth(), window.GetCanvasHeight());
+    controller_->FitContent(viewport_.GetCanvasWidth(), viewport_.GetCanvasHeight());
 
     glEnable(GL_DEBUG_OUTPUT);
     glDebugMessageCallback(OpenGLMessageCallback, 0);
 
-    compositor_.reset(new OpenGLCompositor(window, *GetScene()));
-
-    compositor_->SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
+    GetCompositor().SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
       FONT_SIZE_0, Orthanc::Encoding_Latin1);
-    compositor_->SetFont(1, Orthanc::EmbeddedResources::UBUNTU_FONT,
+    GetCompositor().SetFont(1, Orthanc::EmbeddedResources::UBUNTU_FONT,
       FONT_SIZE_1, Orthanc::Encoding_Latin1);
 
     while (!g_stopApplication)
     {
-      compositor_->Refresh();
+      GetCompositor().Refresh();
 
       SDL_Event event;
       while (!g_stopApplication && SDL_PollEvent(&event))
@@ -684,7 +705,7 @@
           switch (event.key.keysym.sym)
           {
           case SDLK_f:
-            window.GetWindow().ToggleMaximize();
+            viewport_.GetContext().GetWindow().ToggleMaximize();
             break;
 
           case SDLK_q:
@@ -698,10 +719,6 @@
       }
       SDL_Delay(1);
     }
-
-    // the following is paramount because the compositor holds a reference
-    // to the scene and we do not want this reference to become dangling
-    compositor_.reset(NULL);
   }
 
   void TrackerSampleApp::SetInfoDisplayMessage(
--- a/Samples/Sdl/TrackerSampleApp.h	Tue Jul 16 12:38:51 2019 +0200
+++ b/Samples/Sdl/TrackerSampleApp.h	Tue Jul 16 12:41:18 2019 +0200
@@ -25,6 +25,7 @@
 #include "../../Framework/Scene2DViewport/MeasureTool.h"
 #include "../../Framework/Scene2DViewport/PredeclaredTypes.h"
 #include "../../Framework/Scene2DViewport/ViewportController.h"
+#include "../../Framework/Viewport/SdlViewport.h"
 
 #include <SDL.h>
 
@@ -78,6 +79,18 @@
     void SelectNextTool();
     void CreateRandomMeasureTool();
 
+
+    /**
+    In the case of this app, the viewport is an SDL viewport and it has 
+    a OpenGLCompositor& GetCompositor() method
+    */
+    OpenGLCompositor& GetCompositor();
+
+    /**
+    See the other overload
+    */
+    const OpenGLCompositor& GetCompositor() const;
+
     /**
     This returns a random point in the canvas part of the scene, but in
     scene coordinates
@@ -108,7 +121,6 @@
     void HideInfoText();
 
   private:
-    std::auto_ptr<OpenGLCompositor> compositor_;
     /**
     WARNING: the measuring tools do store a reference to the scene, and it 
     paramount that the scene gets destroyed AFTER the measurement tools.
@@ -130,6 +142,7 @@
 
     GuiTool currentTool_;
     boost::shared_ptr<UndoStack> undoStack_;
+    SdlViewport viewport_;
   };
 
 }