ctkCmdLineModuleExplorerMainWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "ctkCmdLineModuleExplorerModulesSettings.h"
  19. #include "ctkCmdLineModuleExplorerTabList.h"
  20. #include "ctkCmdLineModuleExplorerProgressWidget.h"
  21. #include "ctkCmdLineModuleExplorerConstants.h"
  22. #include <ctkCmdLineModuleManager.h>
  23. #include <ctkCmdLineModuleConcurrentHelpers.h>
  24. #include <ctkCmdLineModuleDescription.h>
  25. #include <ctkCmdLineModuleFrontendFactoryQtGui.h>
  26. #include <ctkCmdLineModuleFrontendFactoryQtWebKit.h>
  27. #include <ctkCmdLineModuleBackendLocalProcess.h>
  28. #include <ctkCmdLineModuleBackendFunctionPointer.h>
  29. #include <ctkException.h>
  30. #include <ctkSettingsDialog.h>
  31. #include <QtConcurrentMap>
  32. #include <QDesktopServices>
  33. #include <QMessageBox>
  34. #include <QFutureSynchronizer>
  35. #include <QCloseEvent>
  36. #include <QDebug>
  37. ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
  38. QMainWindow(parent),
  39. ui(new Ui::ctkCmdLineModuleExplorerMainWindow),
  40. defaultModuleFrontendFactory(NULL),
  41. moduleManager(ctkCmdLineModuleManager::STRICT_VALIDATION, QDesktopServices::storageLocation(QDesktopServices::CacheLocation)),
  42. directoryWatcher(&moduleManager)
  43. {
  44. ui->setupUi(this);
  45. settings.restoreState(this->objectName(), *this);
  46. // Frontends
  47. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtGui;
  48. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtWebKit;
  49. defaultModuleFrontendFactory = moduleFrontendFactories.front();
  50. ui->modulesTreeWidget->setModuleFrontendFactories(moduleFrontendFactories);
  51. // Backends
  52. ctkCmdLineModuleBackendFunctionPointer* backendFunctionPointer = new ctkCmdLineModuleBackendFunctionPointer;
  53. moduleBackends.push_back(new ctkCmdLineModuleBackendLocalProcess);
  54. moduleBackends.push_back(backendFunctionPointer);
  55. for(int i = 0; i < moduleBackends.size(); ++i)
  56. {
  57. moduleManager.registerBackend(moduleBackends[i]);
  58. }
  59. settingsDialog = new ctkSettingsDialog(this);
  60. settings.restoreState(settingsDialog->objectName(), *settingsDialog);
  61. settingsDialog->setSettings(&settings);
  62. settingsDialog->addPanel(new ctkCmdLineModuleExplorerDirectorySettings(&directoryWatcher));
  63. settingsDialog->addPanel(new ctkCmdLineModuleExplorerModulesSettings(&moduleManager));
  64. tabList.reset(new ctkCmdLineModuleExplorerTabList(ui->mainTabWidget));
  65. // If a module is registered via the ModuleManager, add it the tree
  66. connect(&moduleManager, SIGNAL(moduleRegistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(addModuleItem(ctkCmdLineModuleReference)));
  67. connect(&moduleManager, SIGNAL(moduleUnregistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(removeModuleItem(ctkCmdLineModuleReference)));
  68. // Double-clicking on an item in the tree creates a new tab with the default frontend
  69. connect(ui->modulesTreeWidget, SIGNAL(moduleDoubleClicked(ctkCmdLineModuleReference)), this, SLOT(addModuleTab(ctkCmdLineModuleReference)));
  70. // React to specific frontend creations
  71. connect(ui->modulesTreeWidget, SIGNAL(moduleFrontendCreated(ctkCmdLineModuleFrontend*)), tabList.data(), SLOT(addTab(ctkCmdLineModuleFrontend*)));
  72. // React to tab-changes
  73. connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), SLOT(moduleTabActivated(ctkCmdLineModuleFrontend*)));
  74. connect(tabList.data(), SIGNAL(tabClosed(ctkCmdLineModuleFrontend*)), ui->outputText, SLOT(frontendRemoved(ctkCmdLineModuleFrontend*)));
  75. // Listen to future events for the currently active tab
  76. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  77. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  78. // resuming the future, after the "resume" signal has been emitted... we use
  79. // a polling aproach instead.
  80. pollPauseTimer.setInterval(300);
  81. connect(&pollPauseTimer, SIGNAL(timeout()), SLOT(checkModulePaused()));
  82. connect(&currentFutureWatcher, SIGNAL(resumed()), SLOT(currentModuleResumed()));
  83. connect(&currentFutureWatcher, SIGNAL(canceled()), SLOT(currentModuleCanceled()));
  84. connect(&currentFutureWatcher, SIGNAL(finished()), SLOT(currentModuleFinished()));
  85. foreach(QUrl fpModule, backendFunctionPointer->registeredFunctionPointers())
  86. {
  87. moduleManager.registerModule(fpModule);
  88. }
  89. // Register persistent modules
  90. QtConcurrent::mapped(settings.value(ctkCmdLineModuleExplorerConstants::KEY_REGISTERED_MODULES).toStringList(),
  91. ctkCmdLineModuleConcurrentRegister(&moduleManager, true));
  92. // Start watching directories
  93. directoryWatcher.setDebug(true);
  94. directoryWatcher.setDirectories(settings.value(ctkCmdLineModuleExplorerConstants::KEY_SEARCH_PATHS).toStringList());
  95. moduleTabActivated(NULL);
  96. pollPauseTimer.start();
  97. }
  98. ctkCLModuleExplorerMainWindow::~ctkCLModuleExplorerMainWindow()
  99. {
  100. qDeleteAll(moduleBackends);
  101. qDeleteAll(moduleFrontendFactories);
  102. settings.saveState(*this, this->objectName());
  103. settings.saveState(*settingsDialog, settingsDialog->objectName());
  104. }
  105. void ctkCLModuleExplorerMainWindow::addModule(const QUrl &location)
  106. {
  107. moduleManager.registerModule(location);
  108. }
  109. void ctkCLModuleExplorerMainWindow::closeEvent(QCloseEvent *event)
  110. {
  111. QList<ctkCmdLineModuleFrontend*> runningFrontends;
  112. foreach (ctkCmdLineModuleFrontend* frontend, this->tabList->tabs())
  113. {
  114. if (frontend->isRunning())
  115. {
  116. runningFrontends << frontend;
  117. }
  118. }
  119. if (!runningFrontends.empty())
  120. {
  121. QMessageBox::StandardButton button =
  122. QMessageBox::warning(QApplication::topLevelWidgets().front(),
  123. QString("Closing %1 running modules").arg(runningFrontends.size()),
  124. "Some modules are still running.\n"
  125. "Closing the application will cancel all current computations.",
  126. QMessageBox::Ok | QMessageBox::Cancel);
  127. if (button == QMessageBox::Ok)
  128. {
  129. QFutureSynchronizer<void> futureSync;
  130. futureSync.setCancelOnWait(true);
  131. foreach(ctkCmdLineModuleFrontend* frontend, runningFrontends)
  132. {
  133. if (frontend->future().canCancel())
  134. {
  135. futureSync.addFuture(frontend->future());
  136. }
  137. }
  138. futureSync.waitForFinished();
  139. event->accept();
  140. QMainWindow::closeEvent(event);
  141. return;
  142. }
  143. else
  144. {
  145. event->ignore();
  146. return;
  147. }
  148. }
  149. event->accept();
  150. }
  151. void ctkCLModuleExplorerMainWindow::on_actionRun_triggered()
  152. {
  153. ctkCmdLineModuleFrontend* moduleFrontend = this->tabList->activeTab();
  154. Q_ASSERT(moduleFrontend);
  155. ctkCmdLineModuleExplorerProgressWidget* progressWidget = new ctkCmdLineModuleExplorerProgressWidget();
  156. this->ui->progressInfoWidget->layout()->addWidget(progressWidget);
  157. ui->actionRun->setEnabled(false);
  158. qobject_cast<QWidget*>(moduleFrontend->guiHandle())->setEnabled(false);
  159. ctkCmdLineModuleFuture future = moduleManager.run(moduleFrontend);
  160. ui->actionPause->setEnabled(future.canPause() && future.isRunning());
  161. ui->actionPause->setChecked(future.isPaused());
  162. ui->actionCancel->setEnabled(future.canCancel() && future.isRunning());
  163. progressWidget->setFuture(future);
  164. this->currentFutureWatcher.setFuture(future);
  165. }
  166. void ctkCLModuleExplorerMainWindow::on_actionPause_toggled(bool toggled)
  167. {
  168. this->currentFutureWatcher.setPaused(toggled);
  169. }
  170. void ctkCLModuleExplorerMainWindow::on_actionCancel_triggered()
  171. {
  172. this->currentFutureWatcher.cancel();
  173. }
  174. void ctkCLModuleExplorerMainWindow::on_actionOptions_triggered()
  175. {
  176. settingsDialog->exec();
  177. }
  178. void ctkCLModuleExplorerMainWindow::on_actionQuit_triggered()
  179. {
  180. this->close();
  181. }
  182. void ctkCLModuleExplorerMainWindow::on_actionReset_triggered()
  183. {
  184. this->tabList->activeTab()->resetValues();
  185. }
  186. void ctkCLModuleExplorerMainWindow::checkModulePaused()
  187. {
  188. if (this->currentFutureWatcher.future().isPaused())
  189. {
  190. if (!ui->actionPause->isChecked())
  191. {
  192. ui->actionPause->setChecked(true);
  193. }
  194. }
  195. else
  196. {
  197. if (ui->actionPause->isChecked())
  198. {
  199. ui->actionPause->setChecked(false);
  200. }
  201. }
  202. }
  203. void ctkCLModuleExplorerMainWindow::currentModuleResumed()
  204. {
  205. ui->actionPause->setChecked(false);
  206. }
  207. void ctkCLModuleExplorerMainWindow::currentModuleCanceled()
  208. {
  209. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  210. if (frontend)
  211. {
  212. ui->actionCancel->setEnabled(false);
  213. ui->actionPause->setEnabled(false);
  214. ui->actionRun->setEnabled(true);
  215. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  216. if (widget)
  217. {
  218. widget->setEnabled(true);
  219. }
  220. }
  221. }
  222. void ctkCLModuleExplorerMainWindow::currentModuleFinished()
  223. {
  224. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  225. if (frontend)
  226. {
  227. ui->actionCancel->setEnabled(false);
  228. ui->actionPause->setEnabled(false);
  229. ui->actionRun->setEnabled(true);
  230. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  231. if (widget)
  232. {
  233. widget->setEnabled(true);
  234. }
  235. }
  236. }
  237. void ctkCLModuleExplorerMainWindow::moduleTabActivated(ctkCmdLineModuleFrontend *module)
  238. {
  239. if (module == NULL)
  240. {
  241. ui->actionRun->setEnabled(false);
  242. ui->actionPause->setEnabled(false);
  243. ui->actionCancel->setEnabled(false);
  244. ui->actionReset->setEnabled(false);
  245. ui->outputText->setActiveFrontend(NULL);
  246. currentFutureWatcher.setFuture(ctkCmdLineModuleFuture());
  247. }
  248. else
  249. {
  250. ui->actionRun->setEnabled(!module->isRunning());
  251. ui->actionPause->setEnabled(module->future().canPause() && module->isRunning());
  252. ui->actionPause->setChecked(module->isPaused());
  253. ui->actionCancel->setEnabled(module->future().canCancel() && module->isRunning());
  254. ui->actionReset->setEnabled(true);
  255. ui->outputText->setActiveFrontend(module);
  256. currentFutureWatcher.setFuture(module->future());
  257. }
  258. }
  259. void ctkCLModuleExplorerMainWindow::addModuleTab(const ctkCmdLineModuleReference &moduleRef)
  260. {
  261. ctkCmdLineModuleFrontend* moduleFrontend = this->defaultModuleFrontendFactory->create(moduleRef);
  262. this->tabList->addTab(moduleFrontend);
  263. }