changeset 1053:32b403a47b19

simplifying IViewport
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 10 Oct 2019 15:24:48 +0200
parents 0cc62db7e61b
children 3c9529edf5fd
files Framework/Scene2D/ICompositor.h Framework/Scene2DViewport/ViewportController.cpp Framework/Viewport/IViewport.h Framework/Viewport/SdlViewport.cpp Framework/Viewport/SdlViewport.h Framework/Viewport/ViewportBase.cpp Framework/Viewport/ViewportBase.h
diffstat 7 files changed, 81 insertions(+), 107 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Scene2D/ICompositor.h	Thu Oct 10 14:37:23 2019 +0200
+++ b/Framework/Scene2D/ICompositor.h	Thu Oct 10 15:24:48 2019 +0200
@@ -1,29 +1,29 @@
-#pragma once
-
-#include <boost/noncopyable.hpp>
-#include <EmbeddedResources.h>
-#include <Core/Enumerations.h>
-
-namespace OrthancStone
-{
-  class ICompositor : public boost::noncopyable
-  {
-
-  public:
-    virtual ~ICompositor() {}
-
-    virtual unsigned int GetCanvasWidth() const = 0;
-
-    virtual unsigned int GetCanvasHeight() const = 0;
-
-    virtual void Refresh() = 0;
-
-#if ORTHANC_ENABLE_LOCALE == 1
-    virtual void SetFont(size_t index,
-                         Orthanc::EmbeddedResources::FileResourceId resource,
-                         unsigned int fontSize,
-                         Orthanc::Encoding codepage) = 0;
-#endif
-
-  };
-}
+#pragma once
+
+#include <boost/noncopyable.hpp>
+#include <EmbeddedResources.h>
+#include <Core/Enumerations.h>
+
+namespace OrthancStone
+{
+  class ICompositor : public boost::noncopyable
+  {
+  public:
+    virtual ~ICompositor() 
+    {
+    }
+
+    virtual unsigned int GetCanvasWidth() const = 0;
+
+    virtual unsigned int GetCanvasHeight() const = 0;
+
+    virtual void Refresh() = 0;
+
+#if ORTHANC_ENABLE_LOCALE == 1
+    virtual void SetFont(size_t index,
+                         Orthanc::EmbeddedResources::FileResourceId resource,
+                         unsigned int fontSize,
+                         Orthanc::Encoding codepage) = 0;
+#endif
+  };
+}
--- a/Framework/Scene2DViewport/ViewportController.cpp	Thu Oct 10 14:37:23 2019 +0200
+++ b/Framework/Scene2DViewport/ViewportController.cpp	Thu Oct 10 15:24:48 2019 +0200
@@ -168,8 +168,12 @@
 
   void ViewportController::FitContent()
   {
-    viewport_.GetScene().FitContent(viewport_.GetCanvasWidth(), viewport_.GetCanvasHeight());
-    BroadcastMessage(SceneTransformChanged(*this));
+    if (viewport_.HasCompositor())
+    {
+      const ICompositor& compositor = viewport_.GetCompositor();
+      viewport_.GetScene().FitContent(compositor.GetCanvasWidth(), compositor.GetCanvasHeight());
+      BroadcastMessage(SceneTransformChanged(*this));
+    }
   }
 
   void ViewportController::AddMeasureTool(boost::shared_ptr<MeasureTool> measureTool)
@@ -219,4 +223,3 @@
     return TEXT_CENTER_DISTANCE_CANVAS_COORD * GetCanvasToSceneFactor();
   }
 }
-
--- a/Framework/Viewport/IViewport.h	Thu Oct 10 14:37:23 2019 +0200
+++ b/Framework/Viewport/IViewport.h	Thu Oct 10 15:24:48 2019 +0200
@@ -42,30 +42,13 @@
 
     virtual void Refresh() = 0;
 
-    virtual unsigned int GetCanvasWidth() const = 0;
-
-    virtual unsigned int GetCanvasHeight() const = 0;
-
     // TODO - Is this needed at this level (e.g. for SDL)?
     virtual const std::string& GetCanvasIdentifier() const = 0;
 
-    virtual ScenePoint2D GetPixelCenterCoordinates(int x, int y) const = 0;
-
-#if ORTHANC_ENABLE_LOCALE == 1
-    virtual void SetFont(size_t index,
-      Orthanc::EmbeddedResources::FileResourceId resource,
-      unsigned int fontSize,
-      Orthanc::Encoding codepage) = 0;
-#endif
+    virtual ScenePoint2D GetPixelCenterCoordinates(int x, int y) = 0;
 
-  protected:
-    // TODO - Replace this by "ICompositor& GetCompositor()"    
-    virtual ICompositor* GetCompositor() = 0;
+    virtual bool HasCompositor() const = 0;
 
-    virtual const ICompositor* GetCompositor() const
-    {
-      IViewport* self = const_cast<IViewport*>(this);
-      return self->GetCompositor();
-    }
+    virtual ICompositor& GetCompositor() = 0;
   };
 }
--- a/Framework/Viewport/SdlViewport.cpp	Thu Oct 10 14:37:23 2019 +0200
+++ b/Framework/Viewport/SdlViewport.cpp	Thu Oct 10 15:24:48 2019 +0200
@@ -49,10 +49,7 @@
 
   void SdlOpenGLViewport::Refresh()
   {
-    if (GetCompositor())
-    {
-      GetCompositor()->Refresh();
-    }
+    compositor_->Refresh();
   }
 
 
@@ -77,7 +74,7 @@
   
   void SdlCairoViewport::Refresh()
   {
-    GetCompositor()->Refresh();
+    compositor_.Refresh();
     window_.Render(sdlSurface_);
   }
 
@@ -104,5 +101,4 @@
       throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
     }
   }
-
 }
--- a/Framework/Viewport/SdlViewport.h	Thu Oct 10 14:37:23 2019 +0200
+++ b/Framework/Viewport/SdlViewport.h	Thu Oct 10 15:24:48 2019 +0200
@@ -48,21 +48,21 @@
   public:
     SdlViewport(const std::string& identifier)
       : ViewportBase(identifier)
-    {}
+    {
+    }
 
     SdlViewport(const std::string& identifier,
                 boost::shared_ptr<Scene2D>& scene)
       : ViewportBase(identifier, scene)
     {
-
     }
 
     virtual SdlWindow& GetWindow() = 0;
     
     virtual void UpdateSize(unsigned int width,
                             unsigned int height) = 0;
+  };
 
-  };
 
   class SdlOpenGLViewport : public SdlViewport
   {
@@ -87,16 +87,21 @@
       return context_.GetWindow();
     }
 
+    virtual void Refresh() ORTHANC_OVERRIDE;
+
     virtual void UpdateSize(unsigned int width, unsigned int height) ORTHANC_OVERRIDE
     {
       // nothing to do in OpenGL, the OpenGLCompositor::UpdateSize will be called automatically
     }
-    virtual void Refresh() ORTHANC_OVERRIDE;
 
-  protected:
-    virtual ICompositor* GetCompositor() ORTHANC_OVERRIDE
+    virtual bool HasCompositor() const ORTHANC_OVERRIDE
     {
-      return compositor_.get();
+      return true;
+    }
+
+    virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
+    {
+      return *compositor_.get();
     }
   };
 
@@ -108,6 +113,10 @@
     CairoCompositor   compositor_;
     SDL_Surface*      sdlSurface_;
 
+  private:
+    void UpdateSdlSurfaceSize(unsigned int width,
+                              unsigned int height);
+
   public:
     SdlCairoViewport(const char* title,
                      unsigned int width,
@@ -126,19 +135,20 @@
     {
       return window_;
     }
-
+    
     virtual void Refresh() ORTHANC_OVERRIDE;
 
     virtual void UpdateSize(unsigned int width,
                             unsigned int height) ORTHANC_OVERRIDE;
-  protected:
-    virtual ICompositor* GetCompositor() ORTHANC_OVERRIDE
+
+    virtual bool HasCompositor() const ORTHANC_OVERRIDE
     {
-      return &compositor_;
+      return true;
     }
 
-  private:
-    void UpdateSdlSurfaceSize(unsigned int width,
-                              unsigned int height);
+    virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
+    {
+      return compositor_;
+    }
   };
 }
--- a/Framework/Viewport/ViewportBase.cpp	Thu Oct 10 14:37:23 2019 +0200
+++ b/Framework/Viewport/ViewportBase.cpp	Thu Oct 10 15:24:48 2019 +0200
@@ -45,10 +45,18 @@
   }
   
 
-  ScenePoint2D ViewportBase::GetPixelCenterCoordinates(int x, int y) const
+  ScenePoint2D ViewportBase::GetPixelCenterCoordinates(int x, int y)
   {
-    return ScenePoint2D(
-      static_cast<double>(x) + 0.5 - static_cast<double>(GetCanvasWidth()) / 2.0,
-      static_cast<double>(y) + 0.5 - static_cast<double>(GetCanvasHeight()) / 2.0);
+    if (HasCompositor())
+    {
+      const ICompositor& compositor = GetCompositor();
+      return ScenePoint2D(
+        static_cast<double>(x) + 0.5 - static_cast<double>(compositor.GetCanvasWidth()) / 2.0,
+        static_cast<double>(y) + 0.5 - static_cast<double>(compositor.GetCanvasHeight()) / 2.0);
+    }
+    else
+    {
+      return ScenePoint2D(0, 0);
+    }
   }
 }
--- a/Framework/Viewport/ViewportBase.h	Thu Oct 10 14:37:23 2019 +0200
+++ b/Framework/Viewport/ViewportBase.h	Thu Oct 10 15:24:48 2019 +0200
@@ -28,6 +28,10 @@
 {
   class ViewportBase : public IViewport
   {
+  private:
+    std::string                 identifier_;
+    boost::shared_ptr<Scene2D>  scene_;
+
   public:
     ViewportBase(const std::string& identifier);
 
@@ -44,36 +48,6 @@
       return identifier_;
     }
 
-    virtual ScenePoint2D GetPixelCenterCoordinates(int x, int y) const ORTHANC_OVERRIDE;
-
-    virtual unsigned int GetCanvasWidth() const ORTHANC_OVERRIDE
-    {
-      if (GetCompositor())
-        return GetCompositor()->GetCanvasWidth();
-      else
-        return 0;
-    }
-
-    virtual unsigned int GetCanvasHeight() const ORTHANC_OVERRIDE
-    {
-      if (GetCompositor())
-        return GetCompositor()->GetCanvasHeight();
-      else
-        return 0;
-    }
-
-#if ORTHANC_ENABLE_LOCALE == 1
-    virtual void SetFont(size_t index,
-      Orthanc::EmbeddedResources::FileResourceId resource,
-      unsigned int fontSize,
-      Orthanc::Encoding codepage) ORTHANC_OVERRIDE
-    {
-      return GetCompositor()->SetFont(index, resource, fontSize, codepage);
-    }
-#endif
-
-  private:
-    std::string                 identifier_;
-    boost::shared_ptr<Scene2D>  scene_;
+    virtual ScenePoint2D GetPixelCenterCoordinates(int x, int y) ORTHANC_OVERRIDE;
   };
 }