changeset 535:79bb0a02d1cc bgo-commands-codegen

- Added ORTHANC_OVERRIDE to several methods (translates to "override" in C++ 11 compilers) - Last fixes to new style command handling (removed useless commands and switch command handling in simple samples to use XxxxSerializedMessageXxxx instead of XxxxCommandXxxx - Fixed hardcoded input instance in SingleFrameEditorApplication
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 19 Mar 2019 09:13:57 +0100
parents 7016c35d163c
children f65748b33bd1
files Applications/IStoneApplication.h Applications/Samples/SampleApplicationBase.h Applications/Samples/SimpleViewer/SimpleViewerApplication.cpp Applications/Samples/SimpleViewer/SimpleViewerApplication.h Applications/Samples/SimpleViewer/Wasm/simple-viewer.ts Applications/Samples/SimpleViewerApplicationSingleFile.h Applications/Samples/SingleFrameEditorApplication.h Applications/Samples/StoneSampleCommands_generated.hpp Applications/Samples/StoneSampleCommands_generated.ts Applications/Samples/Web/simple-viewer-single-file.ts Platforms/Wasm/Defaults.cpp Platforms/Wasm/WasmPlatformApplicationAdapter.cpp Platforms/Wasm/WasmPlatformApplicationAdapter.h Platforms/Wasm/wasm-application-runner.ts
diffstat 14 files changed, 58 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/IStoneApplication.h	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/IStoneApplication.h	Tue Mar 19 09:13:57 2019 +0100
@@ -49,6 +49,12 @@
     virtual void Initialize(StoneApplicationContext* context,
                             IStatusBar& statusBar,
                             const boost::program_options::variables_map& parameters) = 0;
+
+    /**
+      This method is meant to process messages received from the outside world (i.e. GUI)
+    */
+    virtual void HandleSerializedMessage(const char* data) = 0;
+
 #if ORTHANC_ENABLE_WASM==1
     virtual void InitializeWasm() {}  // specific initialization when the app is running in WebAssembly.  This is called after the other Initialize()
 #endif
--- a/Applications/Samples/SampleApplicationBase.h	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/SampleApplicationBase.h	Tue Mar 19 09:13:57 2019 +0100
@@ -41,27 +41,35 @@
     class SampleApplicationBase : public IStoneApplication
     {
     protected:
-      WorldSceneWidget*  mainWidget_;   // ownership is transfered to the application context
+      // ownership is transferred to the application context
+      WorldSceneWidget*  mainWidget_;
 
     public:
       virtual void Initialize(StoneApplicationContext* context,
                               IStatusBar& statusBar,
-                              const boost::program_options::variables_map& parameters)
+                              const boost::program_options::variables_map& parameters) ORTHANC_OVERRIDE
       {
       }
 
-      virtual std::string GetTitle() const
+      virtual std::string GetTitle() const ORTHANC_OVERRIDE
       {
         return "Stone of Orthanc - Sample";
       }
 
-      virtual void Finalize() {}
-      virtual IWidget* GetCentralWidget() {return mainWidget_;}
+      /**
+       * In the basic samples, the commands are handled by the platform adapter and NOT
+       * by the application handler
+      */
+      virtual void HandleSerializedMessage(const char* data) ORTHANC_OVERRIDE {};
+
+
+      virtual void Finalize() ORTHANC_OVERRIDE {}
+      virtual IWidget* GetCentralWidget() ORTHANC_OVERRIDE {return mainWidget_;}
 
 #if ORTHANC_ENABLE_WASM==1
       // default implementations for a single canvas named "canvas" in the HTML and an emtpy WasmApplicationAdapter
 
-      virtual void InitializeWasm()
+      virtual void InitializeWasm() ORTHANC_OVERRIDE
       {
         AttachWidgetToWasmViewport("canvas", mainWidget_);
       }
--- a/Applications/Samples/SimpleViewer/SimpleViewerApplication.cpp	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/SimpleViewer/SimpleViewerApplication.cpp	Tue Mar 19 09:13:57 2019 +0100
@@ -183,13 +183,13 @@
     smartLoader_->SetFrameInWidget(*mainWidget_, 0, instancesIdsPerSeriesId_[seriesId][0], 0);
   }
 
-  bool SimpleViewerApplication::Handle(const StoneSampleCommands::SelectTool& value) ORTHANC_OVERRIDE
+  bool SimpleViewerApplication::Handle(const StoneSampleCommands::SelectTool& value)
   {
     currentTool_ = value.tool;
     return true;
   }
 
-  bool SimpleViewerApplication::Handle(const StoneSampleCommands::Action& value) ORTHANC_OVERRIDE
+  bool SimpleViewerApplication::Handle(const StoneSampleCommands::Action& value)
   {
     switch (value.type)
     {
--- a/Applications/Samples/SimpleViewer/SimpleViewerApplication.h	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/SimpleViewer/SimpleViewerApplication.h	Tue Mar 19 09:13:57 2019 +0100
@@ -111,13 +111,13 @@
       font_.LoadFromResource(Orthanc::EmbeddedResources::FONT_UBUNTU_MONO_BOLD_16);
     }
 
-    virtual void Finalize() {}
-    virtual IWidget* GetCentralWidget() {return mainLayout_;}
+    virtual void Finalize() ORTHANC_OVERRIDE {}
+    virtual IWidget* GetCentralWidget() ORTHANC_OVERRIDE {return mainLayout_;}
 
-    virtual void DeclareStartupOptions(boost::program_options::options_description& options);
+    virtual void DeclareStartupOptions(boost::program_options::options_description& options) ORTHANC_OVERRIDE;
     virtual void Initialize(StoneApplicationContext* context,
                             IStatusBar& statusBar,
-                            const boost::program_options::variables_map& parameters);
+                            const boost::program_options::variables_map& parameters) ORTHANC_OVERRIDE;
 
     void OnStudyListReceived(const OrthancApiClient::JsonResponseReadyMessage& message);
 
@@ -155,10 +155,15 @@
       return StoneSampleCommands::StoneDispatchToHandler(cmdStr, this);
     }
 
-    virtual std::string GetTitle() const {return "SimpleViewer";}
+    virtual void HandleSerializedMessage(const char* data) ORTHANC_OVERRIDE
+    {
+      StoneSampleCommands::StoneDispatchToHandler(data, this);
+    }
+
+    virtual std::string GetTitle() const ORTHANC_OVERRIDE {return "SimpleViewer";}
 
 #if ORTHANC_ENABLE_WASM==1
-    virtual void InitializeWasm();
+    virtual void InitializeWasm() ORTHANC_OVERRIDE;
 #endif
 
 #if ORTHANC_ENABLE_QT==1
--- a/Applications/Samples/SimpleViewer/Wasm/simple-viewer.ts	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/SimpleViewer/Wasm/simple-viewer.ts	Tue Mar 19 09:13:57 2019 +0100
@@ -9,7 +9,7 @@
     args: {
     }                                                                                                                       
   };
-  wasmApplicationRunner.SendCommandToStoneApplication(JSON.stringify(command));
+  wasmApplicationRunner.SendSerializedMessageToStoneApplication(JSON.stringify(command));
 }
 
 function PerformAction(actionName: string) {
@@ -19,7 +19,7 @@
     args: {
     }
   };
-  wasmApplicationRunner.SendCommandToStoneApplication(JSON.stringify(command));
+  wasmApplicationRunner.SendSerializedMessageToStoneApplication(JSON.stringify(command));
 }
 
 class SimpleViewerUI {
--- a/Applications/Samples/SimpleViewerApplicationSingleFile.h	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/SimpleViewerApplicationSingleFile.h	Tue Mar 19 09:13:57 2019 +0100
@@ -207,11 +207,6 @@
 
         virtual void HandleSerializedMessageFromWeb(std::string& output, const std::string& input) 
         {
-          // the simple viewer does not use the serialized messages facilities
-        }
-
-        virtual void HandleCommandFromWeb(std::string& output, const std::string& input) 
-        {
           if (input == "select-tool:line-measure")
           {
             viewerApplication_.currentTool_ = Tool_LineMeasure;
--- a/Applications/Samples/SingleFrameEditorApplication.h	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/SingleFrameEditorApplication.h	Tue Mar 19 09:13:57 2019 +0100
@@ -490,8 +490,10 @@
         fontRegistry_.AddFromResource(Orthanc::EmbeddedResources::FONT_UBUNTU_MONO_BOLD_16);
         
         scene_.reset(new RadiographyScene(GetBroker()));
+        
+        RadiographyLayer& dicomLayer = scene_->LoadDicomFrame(context->GetOrthancApiClient(), instance, 0, false, NULL);
         //scene_->LoadDicomFrame(instance, frame, false); //.SetPan(200, 0);
-        RadiographyLayer& dicomLayer = scene_->LoadDicomFrame(context->GetOrthancApiClient(), "61f3143e-96f34791-ad6bbb8d-62559e75-45943e1b", 0, false, NULL);
+        // = scene_->LoadDicomFrame(context->GetOrthancApiClient(), "61f3143e-96f34791-ad6bbb8d-62559e75-45943e1b", 0, false, NULL);
 
 #if !defined(ORTHANC_ENABLE_WASM) || ORTHANC_ENABLE_WASM != 1
         Orthanc::HttpClient::ConfigureSsl(true, "/etc/ssl/certs/ca-certificates.crt");
--- a/Applications/Samples/StoneSampleCommands_generated.hpp	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/StoneSampleCommands_generated.hpp	Tue Mar 19 09:13:57 2019 +0100
@@ -2,7 +2,7 @@
          1         2         3         4         5         6         7
 12345678901234567890123456789012345678901234567890123456789012345678901234567890
 
-Generated on 2019-03-15 10:00:48.763392 by stonegentool
+Generated on 2019-03-18 12:07:42.696093 by stonegentool
 
 */
 #pragma once
@@ -86,7 +86,7 @@
     return result;
   }
 
-  inline std::string MakeIndent(int indent)
+  inline std::string MakeIndent(size_t indent)
   {
     char* txt = reinterpret_cast<char*>(malloc(indent+1)); // NO EXCEPTION BELOW!!!!!!!!!!!!
     for(size_t i = 0; i < indent; ++i)
@@ -99,14 +99,14 @@
 
   // generic dumper
   template<typename T>
-  std::ostream& StoneDumpValue(std::ostream& out, const T& value, int indent)
+  std::ostream& StoneDumpValue(std::ostream& out, const T& value, size_t indent)
   {
     out << MakeIndent(indent) << value;
     return out;
   }
 
   // string dumper
-  inline std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, int indent)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const std::string& value, size_t indent)
   {
     out << MakeIndent(indent) << "\"" << value  << "\"";
     return out;
@@ -148,7 +148,7 @@
   }
 
   template<typename T>
-  std::ostream& StoneDumpValue(std::ostream& out, const std::map<std::string,T>& value, int indent)
+  std::ostream& StoneDumpValue(std::ostream& out, const std::map<std::string,T>& value, size_t indent)
   {
     out << MakeIndent(indent) << "{\n";
     for (typename std::map<std::string, T>::const_iterator it = value.cbegin();
@@ -188,7 +188,7 @@
   }
 
   template<typename T>
-  std::ostream& StoneDumpValue(std::ostream& out, const std::vector<T>& value, int indent)
+  std::ostream& StoneDumpValue(std::ostream& out, const std::vector<T>& value, size_t indent)
   {
     out << MakeIndent(indent) << "[\n";
     for (size_t i = 0; i < value.size(); ++i)
@@ -372,7 +372,7 @@
     return Json::Value(strValue);
   }
 
-  inline std::ostream& StoneDumpValue(std::ostream& out, const Tool& value, int indent = 0)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const Tool& value, size_t indent = 0)
   {
     if( value == Tool_LineMeasure)
     {
@@ -485,7 +485,7 @@
     return Json::Value(strValue);
   }
 
-  inline std::ostream& StoneDumpValue(std::ostream& out, const ActionType& value, int indent = 0)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const ActionType& value, size_t indent = 0)
   {
     if( value == ActionType_UndoCrop)
     {
@@ -531,7 +531,7 @@
     return result;
   }
 
-  inline std::ostream& StoneDumpValue(std::ostream& out, const SelectTool& value, int indent = 0)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const SelectTool& value, size_t indent = 0)
   {
     out << MakeIndent(indent) << "{\n";
     out << MakeIndent(indent) << "tool:\n";
@@ -594,7 +594,7 @@
     return result;
   }
 
-  inline std::ostream& StoneDumpValue(std::ostream& out, const Action& value, int indent = 0)
+  inline std::ostream& StoneDumpValue(std::ostream& out, const Action& value, size_t indent = 0)
   {
     out << MakeIndent(indent) << "{\n";
     out << MakeIndent(indent) << "type:\n";
@@ -638,6 +638,7 @@
   {
   public:
     virtual bool Handle(const SelectTool& value) = 0;
+    virtual bool Handle(const Action& value) = 0;
   };
 
   /** Service function for StoneDispatchToHandler */
@@ -657,6 +658,12 @@
       _StoneDeserializeValue(value, jsonValue["value"]);
       return handler->Handle(value);
     }
+    else if (type == "StoneSampleCommands.Action")
+    {
+      Action value;
+      _StoneDeserializeValue(value, jsonValue["value"]);
+      return handler->Handle(value);
+    }
     else
     {
       return false;
--- a/Applications/Samples/StoneSampleCommands_generated.ts	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/StoneSampleCommands_generated.ts	Tue Mar 19 09:13:57 2019 +0100
@@ -2,7 +2,7 @@
          1         2         3         4         5         6         7
 12345678901234567890123456789012345678901234567890123456789012345678901234567890
 
-Generated on 2019-03-15 10:00:48.763392 by stonegentool
+Generated on 2019-03-18 12:07:42.696093 by stonegentool
 
 */
 
--- a/Applications/Samples/Web/simple-viewer-single-file.ts	Mon Mar 18 11:27:01 2019 +0100
+++ b/Applications/Samples/Web/simple-viewer-single-file.ts	Tue Mar 19 09:13:57 2019 +0100
@@ -9,7 +9,7 @@
             toolName: toolName
         }
     };
-    wasmApplicationRunner.SendCommandToStoneApplication(JSON.stringify(command));
+    wasmApplicationRunner.SendSerializedMessageToStoneApplication(JSON.stringify(command));
 
 }
 
@@ -19,7 +19,7 @@
         commandType: "simple",
         args: {}
     };
-    wasmApplicationRunner.SendCommandToStoneApplication(JSON.stringify(command));
+    wasmApplicationRunner.SendSerializedMessageToStoneApplication(JSON.stringify(command));
 }
 
 //initializes the buttons
--- a/Platforms/Wasm/Defaults.cpp	Mon Mar 18 11:27:01 2019 +0100
+++ b/Platforms/Wasm/Defaults.cpp	Tue Mar 19 09:13:57 2019 +0100
@@ -357,21 +357,6 @@
     return NULL;
   }
 
-  const char* EMSCRIPTEN_KEEPALIVE SendCommandToStoneApplication(const char* message) 
-  {
-    static std::string output; // we don't want the string to be deallocated when we return to JS code so we always use the same string (this is fine since JS is single-thread)
-
-    printf("SendCommandToStoneApplication\n");
-    printf("%s", message);
-
-    if (applicationWasmAdapter.get() != NULL) {
-      applicationWasmAdapter->HandleCommandFromWeb(output, std::string(message));
-      return output.c_str();
-    }
-    printf("This Stone application does not have a Web Adapter");
-    return NULL;
-  }
-
 #ifdef __cplusplus
 }
 #endif
--- a/Platforms/Wasm/WasmPlatformApplicationAdapter.cpp	Mon Mar 18 11:27:01 2019 +0100
+++ b/Platforms/Wasm/WasmPlatformApplicationAdapter.cpp	Tue Mar 19 09:13:57 2019 +0100
@@ -2,7 +2,6 @@
 
 #include "Framework/Toolbox/MessagingToolbox.h"
 #include "Framework/StoneException.h"
-#include <Applications/Commands/BaseCommandBuilder.h>
 #include <stdio.h>
 #include "Platforms/Wasm/Defaults.h"
 
@@ -14,35 +13,6 @@
   {
   }
 
-  void WasmPlatformApplicationAdapter::HandleCommandFromWeb(std::string& output, const std::string& input)
-  {
-    try
-    {
-      Json::Value inputJson;
-      // if the message is a command, build it and execute it
-      if (MessagingToolbox::ParseJson(inputJson, input.c_str(), input.size()))
-      {
-          std::unique_ptr<ICommand> command(application_.GetCommandBuilder().CreateFromJson(inputJson));
-          if (command.get() == NULL) 
-            printf("Could not parse command: '%s'\n", input.c_str());
-          else
-            application_.ExecuteCommand(*command);
-      } 
-    }
-    catch (StoneException& exc)
-    {
-      printf("Error while handling command from web (error code = %d):\n", exc.GetErrorCode());
-      printf("While interpreting input: '%s'\n", input.c_str());
-      output = std::string("ERROR : ");
-    }
-    catch (std::exception& exc)
-    {
-      printf("Error while handling message from web (error text = %s):\n", exc.what());
-      printf("While interpreting input: '%s'\n", input.c_str());
-      output = std::string("ERROR : ");
-    }
-  }
-
   void WasmPlatformApplicationAdapter::HandleSerializedMessageFromWeb(std::string& output, const std::string& input)
   {
     try
--- a/Platforms/Wasm/WasmPlatformApplicationAdapter.h	Mon Mar 18 11:27:01 2019 +0100
+++ b/Platforms/Wasm/WasmPlatformApplicationAdapter.h	Tue Mar 19 09:13:57 2019 +0100
@@ -13,7 +13,6 @@
       WasmPlatformApplicationAdapter(MessageBroker& broker, IStoneApplication& application);
 
       virtual void HandleSerializedMessageFromWeb(std::string& output, const std::string& input);
-      virtual void HandleCommandFromWeb(std::string& output, const std::string& input);
       virtual void NotifyStatusUpdateFromCppToWebWithString(const std::string& statusUpdateMessage);
       virtual void NotifyStatusUpdateFromCppToWebWithSerializedMessage(const std::string& statusUpdateMessage);
   };
--- a/Platforms/Wasm/wasm-application-runner.ts	Mon Mar 18 11:27:01 2019 +0100
+++ b/Platforms/Wasm/wasm-application-runner.ts	Tue Mar 19 09:13:57 2019 +0100
@@ -21,7 +21,6 @@
 var ReleaseCppViewport: Function = null;
 var StartWasmApplication: Function = null;
 export var SendSerializedMessageToStoneApplication: Function = null;
-export var SendCommandToStoneApplication: Function = null;
 
 function DoAnimationThread() {
   if (WasmDoAnimation != null) {
@@ -102,7 +101,6 @@
     WasmDoAnimation = (<any> window).StoneFrameworkModule.cwrap('WasmDoAnimation', null, []);
 
     SendSerializedMessageToStoneApplication = (<any> window).StoneFrameworkModule.cwrap('SendSerializedMessageToStoneApplication', 'string', ['string']);
-    SendCommandToStoneApplication = (<any> window).StoneFrameworkModule.cwrap('SendCommandToStoneApplication', 'string', ['string']);
 
     Logger.defaultLogger.debug("Connecting C++ methods to JS methods - done");