Przeglądaj źródła

Exit hosted app when host is quitting

Ivo Wolf 13 lat temu
rodzic
commit
9c07673244

+ 10 - 0
Applications/ctkDICOMHost/ctkDICOMHostMainLogic.cpp

@@ -41,6 +41,8 @@ ctkDICOMHostMainLogic::ctkDICOMHostMainLogic(ctkHostedAppPlaceholderWidget* plac
     this, SLOT(onTreeSelectionChanged(const QItemSelection &, const QItemSelection &)));
 
   connect(this->Host, SIGNAL(dataAvailable()), this, SLOT(onDataAvailable()));
+
+  connect( qApp, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()) );
 }
 
 ctkDICOMHostMainLogic::~ctkDICOMHostMainLogic()
@@ -48,6 +50,14 @@ ctkDICOMHostMainLogic::~ctkDICOMHostMainLogic()
   delete Data;
 }
 
+void ctkDICOMHostMainLogic::aboutToQuit()
+{
+  this->Host->exitApplicationBlocking();
+
+  delete this->Host;
+  this->Host = 0;
+}
+
 void ctkDICOMHostMainLogic::configureHostedApp()
 {
   //qDebug() << "load button clicked";

+ 2 - 0
Applications/ctkDICOMHost/ctkDICOMHostMainLogic.h

@@ -3,6 +3,7 @@
 
 #include <QObject.h>
 #include <QStringList.h>
+
 #include "ctkDicomAppHostingTypes.h"
 
 class ctkHostedAppPlaceholderWidget;
@@ -28,6 +29,7 @@ protected slots:
   void onAppReady();
   void placeHolderResized();
   void onDataAvailable();
+  void aboutToQuit();
 signals:
   void TreeSelectionChanged(const QString &);
   void SelectionValid(bool);

+ 44 - 4
Plugins/org.commontk.dah.examplehost/ctkExampleDicomHost.cpp

@@ -95,10 +95,13 @@ void ctkExampleDicomHost::notifyStatus(const ctkDicomAppHosting::Status& status)
 //----------------------------------------------------------------------------
 ctkExampleDicomHost::~ctkExampleDicomHost()
 {
-  qDebug() << "Exiting host: trying to terminate app";
-  this->AppProcess.terminate();
-  qDebug() << "Exiting host: trying to kill app";
-  this->AppProcess.kill();
+  if(this->AppProcess.state()!=QProcess::NotRunning)
+  {
+    qDebug() << "Exiting host: trying to terminate app";
+    this->AppProcess.terminate();
+    qDebug() << "Exiting host: trying to kill app";
+    this->AppProcess.kill();
+  }
 }
 
 //----------------------------------------------------------------------------
@@ -172,6 +175,13 @@ void ctkExampleDicomHost::onReleaseAvailableResources()
 void ctkExampleDicomHost::exitApplication()
 {
   this->exitingApplication=true;
+  if(this->getApplicationState() == ctkDicomAppHosting::EXIT)
+    return;
+  if(this->getApplicationState() == ctkDicomAppHosting::IDLE)
+  {
+    getDicomAppService ()->setState (ctkDicomAppHosting::EXIT);
+    return;
+  }
   getDicomAppService ()->setState (ctkDicomAppHosting::CANCELED);
 }
 
@@ -199,3 +209,33 @@ QString ctkExampleDicomHost::generateUID()
   dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT);
   return uid;
 }
+
+//----------------------------------------------------------------------------
+void ctkExampleDicomHost::exitApplicationBlocking(int timeout)
+{
+  connect(&this->AppProcess,SIGNAL(stateChanged(QProcess::ProcessState)),SLOT(onBlockingExiting(QProcess::ProcessState)));
+
+  if(this->getApplicationState() != ctkDicomAppHosting::EXIT)
+  {
+    this->exitApplication();
+    QTimer::singleShot(timeout,this,SLOT(onBlockingExiting()));
+    BlockingLoopForExiting.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents);
+  }
+  if(this->AppProcess.state()!=QProcess::NotRunning)
+  {
+    this->AppProcess.kill();
+  }
+}
+
+//----------------------------------------------------------------------------
+void ctkExampleDicomHost::onBlockingExiting(QProcess::ProcessState)
+{
+  this->AppProcess.disconnect(SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(onBlockingExiting(QProcess::ProcessState)));
+  BlockingLoopForExiting.exit(0);
+}
+
+//----------------------------------------------------------------------------
+void ctkExampleDicomHost::onBlockingExiting()
+{
+  BlockingLoopForExiting.exit(0);
+}

+ 9 - 0
Plugins/org.commontk.dah.examplehost/ctkExampleDicomHost.h

@@ -77,6 +77,8 @@ public:
 
   QByteArray processReadAll(){return this->AppProcess.readAllStandardOutput ();}
 
+  void exitApplicationBlocking(int timeout = 2000);
+
 public slots:
   void onAppReady();
   void onReleaseAvailableResources();
@@ -93,6 +95,13 @@ signals:
   void giveAvailableScreen(QRect rect);
 
 protected:
+  QEventLoop BlockingLoopForExiting;
+
+protected slots:
+  void onBlockingExiting(QProcess::ProcessState);
+  void onBlockingExiting();
+
+protected:
 
   QProcess AppProcess;
   ctkHostedAppPlaceholderWidget* PlaceholderWidget;

+ 1 - 2
Plugins/org.commontk.dah.host/ctkDicomAbstractHost.cpp

@@ -129,8 +129,7 @@ void ctkDicomAbstractHost::notifyStateChanged(ctkDicomAppHosting::State newState
       d_ptr->AppState = ctkDicomAppHosting::IDLE;
       releaseAvailableResources();
     }
-    else if(d_ptr->AppState == ctkDicomAppHosting::EXIT
-            || d_ptr->AppState == ctkDicomAppHosting::IDLE
+    else if(d_ptr->AppState == ctkDicomAppHosting::IDLE
             || d_ptr->AppState == ctkDicomAppHosting::CANCELED)
     {
       d_ptr->AppState = ctkDicomAppHosting::IDLE;