Просмотр исходного кода

Properly close the module explorer app when modules are still running.

Sascha Zelzer лет назад: 12
Родитель
Сommit
5a873cd5d5

+ 52 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.cpp

@@ -37,6 +37,9 @@
 #include <ctkSettingsDialog.h>
 
 #include <QDesktopServices>
+#include <QMessageBox>
+#include <QFutureSynchronizer>
+#include <QCloseEvent>
 #include <QDebug>
 
 
@@ -116,6 +119,50 @@ void ctkCLModuleExplorerMainWindow::addModule(const QUrl &location)
   moduleManager.registerModule(location);
 }
 
+void ctkCLModuleExplorerMainWindow::closeEvent(QCloseEvent *event)
+{
+  QList<ctkCmdLineModuleFrontend*> runningFrontends;
+  foreach (ctkCmdLineModuleFrontend* frontend, this->tabList->tabs())
+  {
+    if (frontend->isRunning())
+    {
+      runningFrontends << frontend;
+    }
+  }
+
+  if (!runningFrontends.empty())
+  {
+    QMessageBox::StandardButton button =
+        QMessageBox::warning(QApplication::topLevelWidgets().front(),
+                             QString("Closing %1 running modules").arg(runningFrontends.size()),
+                             "Some modules are still running.\n"
+                             "Closing the application will cancel all current computations.",
+                             QMessageBox::Ok | QMessageBox::Cancel);
+    if (button == QMessageBox::Ok)
+    {
+      QFutureSynchronizer<void> futureSync;
+      futureSync.setCancelOnWait(true);
+      foreach(ctkCmdLineModuleFrontend* frontend, runningFrontends)
+      {
+        if (frontend->future().canCancel())
+        {
+          futureSync.addFuture(frontend->future());
+        }
+      }
+      futureSync.waitForFinished();
+      event->accept();
+      QMainWindow::closeEvent(event);
+      return;
+    }
+    else
+    {
+      event->ignore();
+      return;
+    }
+  }
+  event->accept();
+}
+
 void ctkCLModuleExplorerMainWindow::on_actionRun_triggered()
 {
   ctkCmdLineModuleFrontend* moduleFrontend = this->tabList->activeTab();
@@ -152,6 +199,11 @@ void ctkCLModuleExplorerMainWindow::on_actionOptions_triggered()
   settingsDialog->exec();
 }
 
+void ctkCLModuleExplorerMainWindow::on_actionQuit_triggered()
+{
+  this->close();
+}
+
 void ctkCLModuleExplorerMainWindow::checkModulePaused()
 {
   if (this->currentFutureWatcher.future().isPaused())

+ 5 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.h

@@ -50,12 +50,17 @@ public:
 
   void addModule(const QUrl &location);
 
+protected:
+
+  void closeEvent(QCloseEvent* event);
+
 protected Q_SLOTS:
 
   void on_actionRun_triggered();
   void on_actionPause_toggled(bool toggled);
   void on_actionCancel_triggered();
   void on_actionOptions_triggered();
+  void on_actionQuit_triggered();
 
   void checkModulePaused();
   void currentModuleResumed();

+ 5 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerTabList.cpp

@@ -55,6 +55,11 @@ ctkCmdLineModuleFrontend* ctkCmdLineModuleExplorerTabList::activeTab() const
   return this->TabIndexToFrontend[index];
 }
 
+QList<ctkCmdLineModuleFrontend *> ctkCmdLineModuleExplorerTabList::tabs() const
+{
+  return this->TabIndexToFrontend;
+}
+
 void ctkCmdLineModuleExplorerTabList::addTab(ctkCmdLineModuleFrontend* moduleFrontend)
 {
   QWidget* widget = qobject_cast<QWidget*>(moduleFrontend->guiHandle());

+ 2 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerTabList.h

@@ -42,6 +42,8 @@ public:
 
   ctkCmdLineModuleFrontend* activeTab() const;
 
+  QList<ctkCmdLineModuleFrontend*> tabs() const;
+
   Q_SLOT void addTab(ctkCmdLineModuleFrontend* frontend);
 
   Q_SIGNAL void tabActivated(ctkCmdLineModuleFrontend* module);