Browse Source

setState() with state machine done in hosted app and example

Benoit Bleuze 14 years ago
parent
commit
7dbce3c45f

+ 23 - 30
Plugins/org.commontk.dah.app/ctkDicomAbstractApp.cpp

@@ -63,79 +63,66 @@ ctkDicomAbstractApp::~ctkDicomAbstractApp()
 {
 }
 
-/**
-  * Method triggered by the host. Changes the state of the hosted application.
-  *@return true if state received and not illegal in the transition diagram from the reference, false if illegal or not recognized.
-  */
+
+//----------------------------------------------------------------------------
 bool ctkDicomAbstractApp::setState(ctkDicomAppHosting::State newState)
 {
-  bool result = true;
+  bool result = false;
   //received a new state,
   switch (newState){
   case ctkDicomAppHosting::IDLE:
     if (d_ptr->currentState == ctkDicomAppHosting::COMPLETED)
     {
-
-    }
-    else
-    {
-      result = false;
+      emit releaseResources();
+      result = true;
     }
     break;
   case ctkDicomAppHosting::INPROGRESS:
     if (d_ptr->currentState == ctkDicomAppHosting::IDLE)
     {
       emit startProgress();
+      result = true;
     }
     else if(d_ptr->currentState == ctkDicomAppHosting::SUSPENDED)
     {
       emit resumeProgress();
-    }
-    else
-    {
-      result = false;
+      result = true;
     }
     break;
   case ctkDicomAppHosting::COMPLETED:
     qDebug() << "Hosting system shouldn't send completed";
-    result = false;
     break;
   case ctkDicomAppHosting::SUSPENDED:
     //suspend computation, release as much resource as possible with possible resuming of computation
     emit suspendProgress();
+    result = true;
     break;
   case ctkDicomAppHosting::CANCELED:
     //stop and release everything.
-    if (d_ptr->currentState != ctkDicomAppHosting::INPROGRESS
-        || d_ptr->currentState != ctkDicomAppHosting::SUSPENDED)
-    {
-      result = false;
-    }
-    else
+    if (d_ptr->currentState == ctkDicomAppHosting::INPROGRESS
+        || d_ptr->currentState == ctkDicomAppHosting::SUSPENDED)
     {
       //releasing resources
       emit cancelProgress();
       //special state, a transitional state, so we notify straight away the new state.
       getHostInterface()->notifyStateChanged(ctkDicomAppHosting::CANCELED);
       d_ptr->currentState = ctkDicomAppHosting::CANCELED;
+      result = true;
     }
     break;
   case ctkDicomAppHosting::EXIT:
     //check if current state is IDLE
-    if (d_ptr->currentState != ctkDicomAppHosting::IDLE)
+    if (d_ptr->currentState == ctkDicomAppHosting::IDLE)
     {
-      qDebug()<<"illegal transition to EXIT." <<
-                 "Current state is:" << d_ptr->currentState;
-      result = false;
+      //maybe not useful:
+      getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
+      emit exitHostedApp();
+      result = true;
     }
-    //maybe not useful:
-    getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
-    emit exitHostedApp();
     break;
   default:
     //should never happen
     qDebug() << "unexisting state Code, do nothing";
-    result = false;
   }
   if (!result)
   {
@@ -145,7 +132,7 @@ bool ctkDicomAbstractApp::setState(ctkDicomAppHosting::State newState)
   return result;
 }
 
-
+//----------------------------------------------------------------------------
 ctkDicomHostInterface* ctkDicomAbstractApp::getHostInterface() const
 {
   ctkDicomHostInterface* host = d_ptr->HostTracker.getService();
@@ -153,4 +140,10 @@ ctkDicomHostInterface* ctkDicomAbstractApp::getHostInterface() const
   return host;
 }
 
+//----------------------------------------------------------------------------
+ctkDicomAppHosting::State ctkDicomAbstractApp::getState()
+{
+  return d_ptr->currentState;
+}
+
 

+ 3 - 1
Plugins/org.commontk.dah.app/ctkDicomAbstractApp.h

@@ -40,13 +40,14 @@ class ctkPluginContext;
 class org_commontk_dah_app_EXPORT ctkDicomAbstractApp : public QObject, public ctkDicomAppInterface
 {
   Q_OBJECT
+  Q_INTERFACES(ctkDicomAppInterface)
 
 public:
 
   ctkDicomAbstractApp(ctkPluginContext* context);
   virtual ~ctkDicomAbstractApp();
   virtual bool setState(ctkDicomAppHosting::State newState);
-
+  virtual ctkDicomAppHosting::State getState();
 protected:
   virtual ctkDicomHostInterface* getHostInterface() const;
 
@@ -56,6 +57,7 @@ signals:
   void suspendProgress();
   void cancelProgress();
   void exitHostedApp();
+  void releaseResources();
 
 private:
   Q_DECLARE_PRIVATE(ctkDicomAbstractApp)

+ 4 - 0
Plugins/org.commontk.dah.core/ctkDicomAppInterface.h

@@ -30,6 +30,10 @@ struct ctkDicomAppInterface : public ctkDicomExchangeInterface
 
   // Application interface methods
   virtual ctkDicomAppHosting::State getState() = 0;
+  /**
+    * Method triggered by the host. Changes the state of the hosted application.
+    *@return true if state received and not illegal in the transition diagram from the reference, false if illegal or not recognized.
+    */
   virtual bool setState(ctkDicomAppHosting::State newState) = 0;
   virtual bool bringToFront(const QRect& requestedScreenArea) = 0;
 

+ 46 - 49
Plugins/org.commontk.dah.exampleapp/ctkExampleDicomAppLogic.cpp

@@ -41,8 +41,14 @@ ctkDicomAbstractApp(ctkExampleDicomAppPlugin::getPluginContext()), Button(0)
 {
 
 
-  connect(this, SIGNAL(stateChanged(int)), this, SLOT(changeState(int)), Qt::QueuedConnection);
-  emit stateChanged(ctkDicomAppHosting::IDLE);
+  connect(this, SIGNAL(startProgress()), this, SLOT(onStartProgress()), Qt::QueuedConnection);
+  connect(this, SIGNAL(resumeProgress()), this, SLOT(onResumeProgress()), Qt::QueuedConnection);
+  connect(this, SIGNAL(SuspendProgress()), this, SLOT(onSuspendProgress()), Qt::QueuedConnection);
+  connect(this, SIGNAL(cancelProgress()), this, SLOT(onCancelProgress()), Qt::QueuedConnection);
+  connect(this, SIGNAL(exitHostedApp()), this, SLOT(onExitHostedApp()), Qt::QueuedConnection);
+
+  //notify Host we are ready.
+  getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
 }
 
 //----------------------------------------------------------------------------
@@ -56,19 +62,7 @@ ctkExampleDicomAppLogic::~ctkExampleDicomAppLogic()
   }
 }
 
-//----------------------------------------------------------------------------
-ctkDicomAppHosting::State ctkExampleDicomAppLogic::getState()
-{
-  return ctkDicomAppHosting::IDLE;
-}
 
-//----------------------------------------------------------------------------
-bool ctkExampleDicomAppLogic::setState(ctkDicomAppHosting::State newState)
-{
-  qDebug() << "setState called";
-  emit stateChanged(newState);
-  return true;
-}
 
 //----------------------------------------------------------------------------
 bool ctkExampleDicomAppLogic::bringToFront(const QRect& /*requestedScreenArea*/)
@@ -99,49 +93,52 @@ void ctkExampleDicomAppLogic::do_something()
 }
 
 //----------------------------------------------------------------------------
-void ctkExampleDicomAppLogic::changeState(int anewstate)
+void ctkExampleDicomAppLogic::onStartProgress()
 {
-  ctkDicomAppHosting::State newstate = static_cast<ctkDicomAppHosting::State>(anewstate);
+  getHostInterface()->notifyStateChanged(ctkDicomAppHosting::INPROGRESS);
+  do_something();
+}
 
-  if (newstate == ctkDicomAppHosting::INPROGRESS)
-    {
-    do_something();
-    }
+//----------------------------------------------------------------------------
+void ctkExampleDicomAppLogic::onResumeProgress()
+{
+  //reclame all resources.
 
-  try
-    {
-    getHostInterface()->notifyStateChanged(newstate);
-    }
-  catch (const std::runtime_error& e)
-    {
-    qCritical() << e.what();
-    return;
-    }
+  //notify state changed
+  getHostInterface()->notifyStateChanged(ctkDicomAppHosting::INPROGRESS);
+  //we're rolling
+  //do something else normally, but this is an example
+  this->Button->setEnabled(true);
+}
 
-  if (newstate == ctkDicomAppHosting::CANCELED)
-    {
-    qDebug() << "  Received changeState(CANCELED) ... now releasing all resources and afterwards changing to state IDLE.";
-    qDebug() << "  Changing to state IDLE.";
-    try
-      {
-      getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
-      }
-    catch (const std::runtime_error& e)
-      {
-      qCritical() << e.what();
-      return;
-      }
-    }
+//----------------------------------------------------------------------------
+void ctkExampleDicomAppLogic::onSuspendProgress()
+{
+  //release resources it can reclame later to resume work
+  this->Button->setEnabled(false);
+  //notify state changed
+  getHostInterface()->notifyStateChanged(ctkDicomAppHosting::INPROGRESS);
+  //we're rolling
+  //do something else normally, but this is an example
+}
 
-  if (newstate == ctkDicomAppHosting::EXIT)
-    {
-    qDebug() << "  Received changeState(EXIT) ... exiting.";
-    this->getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
-    qApp->exit(0);
-    }
+//----------------------------------------------------------------------------
+void ctkExampleDicomAppLogic::onCancelProgress()
+{
+  //release all resources
+  onReleaseResources();
+  //update state
+  getHostInterface()->notifyStateChanged(ctkDicomAppHosting::IDLE);
 }
 
 //----------------------------------------------------------------------------
+void ctkExampleDicomAppLogic::onExitHostedApp()
+{
+  qApp->exit(0);
+}
+
+
+//----------------------------------------------------------------------------
 bool ctkExampleDicomAppLogic::notifyDataAvailable(ctkDicomAppHosting::AvailableData data, bool lastData)
 {
   Q_UNUSED(lastData)

+ 6 - 6
Plugins/org.commontk.dah.exampleapp/ctkExampleDicomAppLogic_p.h

@@ -43,8 +43,6 @@ public:
   virtual ~ctkExampleDicomAppLogic();
 
   // ctkDicomAppInterface
-  virtual ctkDicomAppHosting::State getState();
-  virtual bool setState(ctkDicomAppHosting::State newState);
   virtual bool bringToFront(const QRect& requestedScreenArea);
 
   // ctkDicomExchangeInterface
@@ -60,13 +58,15 @@ public:
   // some logic
   void do_something();
 
-signals:
-
-  void stateChanged(int);
 
 protected slots:
 
-  void changeState(int);
+  void onStartProgress();
+  void onResumeProgress();
+  void onSuspendProgress();
+  void onCancelProgress();
+  void onExitHostedApp();
+  void onReleaseResources();
 
   void buttonClicked();
 private: