ctkCmdLineModuleExplorerMainWindow.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleExplorerMainWindow.h"
  16. #include "ui_ctkCmdLineModuleExplorerMainWindow.h"
  17. #include "ctkCmdLineModuleExplorerDirectorySettings.h"
  18. #include "ctkCmdLineModuleExplorerTabList.h"
  19. #include "ctkCmdLineModuleExplorerProgressWidget.h"
  20. #include <ctkCmdLineModuleManager.h>
  21. #include <ctkCmdLineModuleDescription.h>
  22. #include <ctkCmdLineModuleFrontendFactoryQtGui.h>
  23. #include <ctkCmdLineModuleFrontendFactoryQtWebKit.h>
  24. #include <ctkCmdLineModuleBackendLocalProcess.h>
  25. #include <ctkCmdLineModuleBackendFunctionPointer.h>
  26. #include <ctkException.h>
  27. #include <ctkSettingsDialog.h>
  28. #include <QDesktopServices>
  29. #include <QMessageBox>
  30. #include <QFutureSynchronizer>
  31. #include <QCloseEvent>
  32. #include <QDebug>
  33. ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
  34. QMainWindow(parent),
  35. ui(new Ui::ctkCmdLineModuleExplorerMainWindow),
  36. defaultModuleFrontendFactory(NULL),
  37. moduleManager(ctkCmdLineModuleManager::STRICT_VALIDATION, QDesktopServices::storageLocation(QDesktopServices::CacheLocation)),
  38. directoryWatcher(&moduleManager)
  39. {
  40. ui->setupUi(this);
  41. // Frontends
  42. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtGui;
  43. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtWebKit;
  44. defaultModuleFrontendFactory = moduleFrontendFactories.front();
  45. ui->modulesTreeWidget->setModuleFrontendFactories(moduleFrontendFactories);
  46. // Backends
  47. ctkCmdLineModuleBackendFunctionPointer* backendFunctionPointer = new ctkCmdLineModuleBackendFunctionPointer;
  48. moduleBackends.push_back(new ctkCmdLineModuleBackendLocalProcess);
  49. moduleBackends.push_back(backendFunctionPointer);
  50. for(int i = 0; i < moduleBackends.size(); ++i)
  51. {
  52. moduleManager.registerBackend(moduleBackends[i]);
  53. }
  54. settingsDialog = new ctkSettingsDialog(this);
  55. settingsDialog->addPanel(new ctkCmdLineModuleExplorerDirectorySettings());
  56. tabList.reset(new ctkCmdLineModuleExplorerTabList(ui->mainTabWidget));
  57. // If a module is registered via the ModuleManager, add it the tree
  58. connect(&moduleManager, SIGNAL(moduleRegistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(addModuleItem(ctkCmdLineModuleReference)));
  59. // Double-clicking on an item in the tree creates a new tab with the default frontend
  60. connect(ui->modulesTreeWidget, SIGNAL(moduleDoubleClicked(ctkCmdLineModuleReference)), this, SLOT(addModuleTab(ctkCmdLineModuleReference)));
  61. // React to specific frontend creations
  62. connect(ui->modulesTreeWidget, SIGNAL(moduleFrontendCreated(ctkCmdLineModuleFrontend*)), tabList.data(), SLOT(addTab(ctkCmdLineModuleFrontend*)));
  63. // React to tab-changes
  64. connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), SLOT(moduleTabActivated(ctkCmdLineModuleFrontend*)));
  65. // Listen to future events for the currently active tab
  66. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  67. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  68. // resuming the future, after the "resume" signal has been emitted... we use
  69. // a polling aproach instead.
  70. pollPauseTimer.setInterval(300);
  71. connect(&pollPauseTimer, SIGNAL(timeout()), SLOT(checkModulePaused()));
  72. connect(&currentFutureWatcher, SIGNAL(resumed()), SLOT(currentModuleResumed()));
  73. connect(&currentFutureWatcher, SIGNAL(canceled()), SLOT(currentModuleCanceled()));
  74. connect(&currentFutureWatcher, SIGNAL(finished()), SLOT(currentModuleFinished()));
  75. foreach(QUrl fpModule, backendFunctionPointer->registeredFunctionPointers())
  76. {
  77. moduleManager.registerModule(fpModule);
  78. }
  79. directoryWatcher.setDebug(true);
  80. directoryWatcher.setDirectories(QStringList(QCoreApplication::applicationDirPath()));
  81. moduleTabActivated(NULL);
  82. pollPauseTimer.start();
  83. }
  84. ctkCLModuleExplorerMainWindow::~ctkCLModuleExplorerMainWindow()
  85. {
  86. qDeleteAll(moduleBackends);
  87. qDeleteAll(moduleFrontendFactories);
  88. }
  89. void ctkCLModuleExplorerMainWindow::addModule(const QUrl &location)
  90. {
  91. moduleManager.registerModule(location);
  92. }
  93. void ctkCLModuleExplorerMainWindow::closeEvent(QCloseEvent *event)
  94. {
  95. QList<ctkCmdLineModuleFrontend*> runningFrontends;
  96. foreach (ctkCmdLineModuleFrontend* frontend, this->tabList->tabs())
  97. {
  98. if (frontend->isRunning())
  99. {
  100. runningFrontends << frontend;
  101. }
  102. }
  103. if (!runningFrontends.empty())
  104. {
  105. QMessageBox::StandardButton button =
  106. QMessageBox::warning(QApplication::topLevelWidgets().front(),
  107. QString("Closing %1 running modules").arg(runningFrontends.size()),
  108. "Some modules are still running.\n"
  109. "Closing the application will cancel all current computations.",
  110. QMessageBox::Ok | QMessageBox::Cancel);
  111. if (button == QMessageBox::Ok)
  112. {
  113. QFutureSynchronizer<void> futureSync;
  114. futureSync.setCancelOnWait(true);
  115. foreach(ctkCmdLineModuleFrontend* frontend, runningFrontends)
  116. {
  117. if (frontend->future().canCancel())
  118. {
  119. futureSync.addFuture(frontend->future());
  120. }
  121. }
  122. futureSync.waitForFinished();
  123. event->accept();
  124. QMainWindow::closeEvent(event);
  125. return;
  126. }
  127. else
  128. {
  129. event->ignore();
  130. return;
  131. }
  132. }
  133. event->accept();
  134. }
  135. void ctkCLModuleExplorerMainWindow::on_actionRun_triggered()
  136. {
  137. ctkCmdLineModuleFrontend* moduleFrontend = this->tabList->activeTab();
  138. Q_ASSERT(moduleFrontend);
  139. ctkCmdLineModuleExplorerProgressWidget* progressWidget = new ctkCmdLineModuleExplorerProgressWidget();
  140. this->ui->progressInfoWidget->layout()->addWidget(progressWidget);
  141. ui->actionRun->setEnabled(false);
  142. qobject_cast<QWidget*>(moduleFrontend->guiHandle())->setEnabled(false);
  143. ctkCmdLineModuleFuture future = moduleManager.run(moduleFrontend);
  144. ui->actionPause->setEnabled(future.canPause() && future.isRunning());
  145. ui->actionPause->setChecked(future.isPaused());
  146. ui->actionCancel->setEnabled(future.canCancel() && future.isRunning());
  147. progressWidget->setFuture(future);
  148. this->currentFutureWatcher.setFuture(future);
  149. }
  150. void ctkCLModuleExplorerMainWindow::on_actionPause_toggled(bool toggled)
  151. {
  152. this->currentFutureWatcher.setPaused(toggled);
  153. }
  154. void ctkCLModuleExplorerMainWindow::on_actionCancel_triggered()
  155. {
  156. this->currentFutureWatcher.cancel();
  157. }
  158. void ctkCLModuleExplorerMainWindow::on_actionOptions_triggered()
  159. {
  160. settingsDialog->exec();
  161. }
  162. void ctkCLModuleExplorerMainWindow::on_actionQuit_triggered()
  163. {
  164. this->close();
  165. }
  166. void ctkCLModuleExplorerMainWindow::on_actionReset_triggered()
  167. {
  168. this->tabList->activeTab()->resetValues();
  169. }
  170. void ctkCLModuleExplorerMainWindow::checkModulePaused()
  171. {
  172. if (this->currentFutureWatcher.future().isPaused())
  173. {
  174. if (!ui->actionPause->isChecked())
  175. {
  176. ui->actionPause->setChecked(true);
  177. }
  178. }
  179. else
  180. {
  181. if (ui->actionPause->isChecked())
  182. {
  183. ui->actionPause->setChecked(false);
  184. }
  185. }
  186. }
  187. void ctkCLModuleExplorerMainWindow::currentModuleResumed()
  188. {
  189. ui->actionPause->setChecked(false);
  190. }
  191. void ctkCLModuleExplorerMainWindow::currentModuleCanceled()
  192. {
  193. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  194. if (frontend)
  195. {
  196. ui->actionCancel->setEnabled(false);
  197. ui->actionPause->setEnabled(false);
  198. ui->actionRun->setEnabled(true);
  199. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  200. if (widget)
  201. {
  202. widget->setEnabled(true);
  203. }
  204. }
  205. }
  206. void ctkCLModuleExplorerMainWindow::currentModuleFinished()
  207. {
  208. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  209. if (frontend)
  210. {
  211. ui->actionCancel->setEnabled(false);
  212. ui->actionPause->setEnabled(false);
  213. ui->actionRun->setEnabled(true);
  214. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  215. if (widget)
  216. {
  217. widget->setEnabled(true);
  218. }
  219. }
  220. }
  221. void ctkCLModuleExplorerMainWindow::moduleTabActivated(ctkCmdLineModuleFrontend *module)
  222. {
  223. if (module == NULL)
  224. {
  225. ui->actionRun->setEnabled(false);
  226. ui->actionPause->setEnabled(false);
  227. ui->actionCancel->setEnabled(false);
  228. ui->actionReset->setEnabled(false);
  229. currentFutureWatcher.setFuture(ctkCmdLineModuleFuture());
  230. }
  231. else
  232. {
  233. ui->actionRun->setEnabled(!module->isRunning());
  234. ui->actionPause->setEnabled(module->future().canPause() && module->isRunning());
  235. ui->actionPause->setChecked(module->isPaused());
  236. ui->actionCancel->setEnabled(module->future().canCancel() && module->isRunning());
  237. ui->actionReset->setEnabled(true);
  238. currentFutureWatcher.setFuture(module->future());
  239. }
  240. }
  241. void ctkCLModuleExplorerMainWindow::addModuleTab(const ctkCmdLineModuleReference &moduleRef)
  242. {
  243. ctkCmdLineModuleFrontend* moduleFrontend = this->defaultModuleFrontendFactory->create(moduleRef);
  244. this->tabList->addTab(moduleFrontend);
  245. }