changeset 70:a5c1be2ec26a

new configuration option: STL.3DHOP.CanvasStyle
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Oct 2024 17:12:49 +0200
parents c42bac260709
children f3bbafc067d0
files NEWS Resources/CMake/3dhop-4.3.patch Sources/Plugin.cpp
diffstat 3 files changed, 109 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Sun Jun 16 21:13:49 2024 +0200
+++ b/NEWS	Fri Oct 04 17:12:49 2024 +0200
@@ -1,6 +1,9 @@
 Pending changes in the mainline
 ===============================
 
+* Added configuration option "STL.3DHOP.CanvasStyle" to change the
+  background of the 3DHOP viewer
+
 
 Version 1.2 (2024-06-16)
 ========================
--- a/Resources/CMake/3dhop-4.3.patch	Sun Jun 16 21:13:49 2024 +0200
+++ b/Resources/CMake/3dhop-4.3.patch	Fri Oct 04 17:12:49 2024 +0200
@@ -1,7 +1,13 @@
 diff -urEb 3dhop.orig/3DHOP_all_tools.html 3dhop/3DHOP_all_tools.html
---- 3dhop.orig/3DHOP_all_tools.html	2024-06-15 15:43:28.329045772 +0200
-+++ 3dhop/3DHOP_all_tools.html	2024-06-15 15:50:04.629862005 +0200
-@@ -106,6 +106,25 @@
+--- 3dhop.orig/3DHOP_all_tools.html	2024-10-04 15:38:25.961879494 +0200
++++ 3dhop/3DHOP_all_tools.html	2024-10-04 15:39:42.273297957 +0200
+@@ -101,11 +101,30 @@
+  </div>
+ <!--SECTIONS-->
+ 
+- <canvas id="draw-canvas" style="background-image: url(skins/backgrounds/light.jpg)"/>
++ <canvas id="draw-canvas" style="${{CANVAS_STYLE}}"/>
+ </div>
  </body>
  
  <script type="text/javascript">
@@ -36,3 +42,4 @@
  		},
  		modelInstances : {
  			"model_1" : { 
+Only in 3dhop: 3DHOP_all_tools.html~
--- a/Sources/Plugin.cpp	Sun Jun 16 21:13:49 2024 +0200
+++ b/Sources/Plugin.cpp	Fri Oct 04 17:12:49 2024 +0200
@@ -119,12 +119,61 @@
  **/
 class ResourcesCache : public boost::noncopyable
 {
+public:
+  class IHandler : public boost::noncopyable
+  {
+  public:
+    virtual ~IHandler()
+    {
+    }
+
+    virtual void Apply(const std::string& resource) = 0;
+  };
+
 private:
   typedef std::map<std::string, std::string*>  Content;
   
   boost::shared_mutex  mutex_;
   Content              content_;
 
+  class RestOutputHandler : public IHandler
+  {
+  private:
+    OrthancPluginRestOutput* output_;
+    std::string              mime_;
+
+  public:
+    RestOutputHandler(OrthancPluginRestOutput* output,
+                      const std::string& mime) :
+      output_(output),
+      mime_(mime)
+    {
+    }
+
+    virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
+    {
+      OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_,
+                                resource.c_str(), resource.size(), mime_.c_str());
+    }
+  };
+
+  class StoreResourceIntoString : public IHandler
+  {
+  private:
+    std::string& target_;
+
+  public:
+    StoreResourceIntoString(std::string& target) :
+      target_(target)
+    {
+    }
+
+    virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
+    {
+      target_ = resource;
+    }
+  };
+
 public:
   ~ResourcesCache()
   {
@@ -135,11 +184,9 @@
     }
   }
 
-  void Answer(OrthancPluginRestOutput* output,
-              const std::string& path)
+  void Apply(IHandler& handler,
+             const std::string& path)
   {
-    const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
-
     {
       // Check whether the cache already contains the resource
       boost::shared_lock<boost::shared_mutex> lock(mutex_);
@@ -149,7 +196,7 @@
       if (found != content_.end())
       {
         assert(found->second != NULL);
-        OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, found->second->c_str(), found->second->size(), mime.c_str());
+        handler.Apply(*found->second);
         return;
       }
     }
@@ -158,7 +205,7 @@
 
     std::unique_ptr<std::string> item(new std::string);
     ReadStaticAsset(*item, path);
-    OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, item->c_str(), item->size(), mime.c_str());
+    handler.Apply(*item);
 
     {
       // Store the resource into the cache
@@ -170,6 +217,22 @@
       }
     }
   }
+
+  void Answer(OrthancPluginRestOutput* output,
+              const std::string& path)
+  {
+    const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
+
+    RestOutputHandler handler(output, mime);
+    Apply(handler, path);
+  }
+
+  void ReadResource(std::string& target,
+                   const std::string& path)
+  {
+    StoreResourceIntoString handler(target);
+    Apply(handler, path);
+  }
 };
 
 
@@ -981,6 +1044,9 @@
 
 #if ORTHANC_ENABLE_3DHOP == 1
 
+// This is the default background style of 3DHOP
+static std::string canvasStyle3DHOP_ = "background-image: url(skins/backgrounds/light.jpg)";
+
 void Serve3DHOPAssets(OrthancPluginRestOutput* output,
                       const char* url,
                       const OrthancPluginHttpRequest* request)
@@ -992,7 +1058,22 @@
   }
 
   const std::string file = request->groups[0];
-  cache_.Answer(output, "3dhop/" + file);
+  const std::string resourceId = "3dhop/" + file;
+
+  if (file == "3DHOP_all_tools.html")
+  {
+    std::string resource;
+    cache_.ReadResource(resource, resourceId);
+
+    boost::replace_all(resource, "${{CANVAS_STYLE}}", canvasStyle3DHOP_);
+
+    OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, resource.c_str(),
+                              resource.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Html));
+  }
+  else
+  {
+    cache_.Answer(output, resourceId);
+  }
 }
 
 #endif
@@ -1052,6 +1133,9 @@
     globalConfiguration.GetSection(configuration, "STL");
 
 #if ORTHANC_ENABLE_NEXUS == 1
+    OrthancPlugins::OrthancConfiguration configuration3DHOP;
+    configuration.GetSection(configuration3DHOP, "3DHOP");
+
     const bool enableNexus = configuration.GetBooleanValue("EnableNexus", false);
 
     if (enableNexus)
@@ -1072,6 +1156,11 @@
       OrthancPlugins::RegisterRestCallback<ExtractNexusModel>("/stl/3dhop-instances/([0-9a-f-]+).nxz", true);
 #endif
 
+      // New in release 1.3
+      canvasStyle3DHOP_ = configuration3DHOP.GetStringValue(
+        "CanvasStyle",
+        "background-image: url(skins/backgrounds/light.jpg)"); // This is the default background style of 3DHOP
+
       const bool hasCreateNexus_ = OrthancPlugins::CheckMinimalOrthancVersion(1, 9, 4);
 
       if (hasCreateNexus_)