ctkCmdLineModuleExplorerMainWindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <ctkCmdLineModuleXmlException.h>
  31. #include <ctkSettingsDialog.h>
  32. #include <QtConcurrentMap>
  33. #include <QDesktopServices>
  34. #include <QMessageBox>
  35. #include <QFutureSynchronizer>
  36. #include <QCloseEvent>
  37. #include <QFileDialog>
  38. ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
  39. QMainWindow(parent),
  40. ui(new Ui::ctkCmdLineModuleExplorerMainWindow),
  41. defaultModuleFrontendFactory(NULL),
  42. moduleManager(ctkCmdLineModuleManager::WEAK_VALIDATION, QDesktopServices::storageLocation(QDesktopServices::CacheLocation)),
  43. directoryWatcher(&moduleManager),
  44. settingsDialog(NULL)
  45. {
  46. ui->setupUi(this);
  47. settings.restoreState(this->objectName(), *this);
  48. // Frontends
  49. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtGui;
  50. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtWebKit;
  51. defaultModuleFrontendFactory = moduleFrontendFactories.front();
  52. ui->modulesTreeWidget->setModuleFrontendFactories(moduleFrontendFactories, moduleFrontendFactories.front());
  53. ui->modulesSearchBox->setClearIcon(QIcon(":/icons/clear.png"));
  54. connect(ui->modulesSearchBox, SIGNAL(textChanged(QString)), ui->modulesTreeWidget, SLOT(setFilter(QString)));
  55. // Backends
  56. ctkCmdLineModuleBackendFunctionPointer* backendFunctionPointer = new ctkCmdLineModuleBackendFunctionPointer;
  57. moduleBackends.push_back(new ctkCmdLineModuleBackendLocalProcess);
  58. moduleBackends.push_back(backendFunctionPointer);
  59. for(int i = 0; i < moduleBackends.size(); ++i)
  60. {
  61. moduleManager.registerBackend(moduleBackends[i]);
  62. }
  63. tabList.reset(new ctkCmdLineModuleExplorerTabList(ui->mainTabWidget));
  64. // If a module is registered via the ModuleManager, add it to the tree
  65. connect(&moduleManager, SIGNAL(moduleRegistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(addModuleItem(ctkCmdLineModuleReference)));
  66. connect(&moduleManager, SIGNAL(moduleUnregistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(removeModuleItem(ctkCmdLineModuleReference)));
  67. // React to specific frontend creations
  68. connect(ui->modulesTreeWidget, SIGNAL(moduleFrontendCreated(ctkCmdLineModuleFrontend*)), tabList.data(), SLOT(addTab(ctkCmdLineModuleFrontend*)));
  69. // React to tab-changes
  70. connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), SLOT(moduleTabActivated(ctkCmdLineModuleFrontend*)));
  71. connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), ui->progressListWidget, SLOT(setCurrentProgressWidget(ctkCmdLineModuleFrontend*)));
  72. connect(tabList.data(), SIGNAL(tabClosed(ctkCmdLineModuleFrontend*)), ui->outputText, SLOT(frontendRemoved(ctkCmdLineModuleFrontend*)));
  73. connect(tabList.data(), SIGNAL(tabClosed(ctkCmdLineModuleFrontend*)), ui->progressListWidget, SLOT(removeProgressWidget(ctkCmdLineModuleFrontend*)));
  74. connect(ui->ClearButton, SIGNAL(clicked()), ui->progressListWidget, SLOT(clearList()));
  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. QFuture<void> future = 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. future.waitForFinished();
  98. }
  99. ctkCLModuleExplorerMainWindow::~ctkCLModuleExplorerMainWindow()
  100. {
  101. qDeleteAll(moduleBackends);
  102. qDeleteAll(moduleFrontendFactories);
  103. settings.saveState(*this, this->objectName());
  104. settings.saveState(*settingsDialog, settingsDialog->objectName());
  105. }
  106. void ctkCLModuleExplorerMainWindow::addModule(const QUrl &location)
  107. {
  108. moduleManager.registerModule(location);
  109. }
  110. void ctkCLModuleExplorerMainWindow::closeEvent(QCloseEvent *event)
  111. {
  112. QList<ctkCmdLineModuleFrontend*> runningFrontends;
  113. foreach (ctkCmdLineModuleFrontend* frontend, this->tabList->tabs())
  114. {
  115. if (frontend->isRunning())
  116. {
  117. runningFrontends << frontend;
  118. }
  119. }
  120. if (!runningFrontends.empty())
  121. {
  122. QMessageBox::StandardButton button =
  123. QMessageBox::warning(QApplication::topLevelWidgets().front(),
  124. QString("Closing %1 running modules").arg(runningFrontends.size()),
  125. "Some modules are still running.\n"
  126. "Closing the application will cancel all current computations.",
  127. QMessageBox::Ok | QMessageBox::Cancel);
  128. if (button == QMessageBox::Ok)
  129. {
  130. QFutureSynchronizer<void> futureSync;
  131. futureSync.setCancelOnWait(true);
  132. foreach(ctkCmdLineModuleFrontend* frontend, runningFrontends)
  133. {
  134. if (frontend->future().canCancel())
  135. {
  136. futureSync.addFuture(frontend->future());
  137. }
  138. }
  139. futureSync.waitForFinished();
  140. event->accept();
  141. QMainWindow::closeEvent(event);
  142. return;
  143. }
  144. else
  145. {
  146. event->ignore();
  147. return;
  148. }
  149. }
  150. event->accept();
  151. }
  152. void ctkCLModuleExplorerMainWindow::on_actionRun_triggered()
  153. {
  154. ctkCmdLineModuleFrontend* moduleFrontend = this->tabList->activeTab();
  155. Q_ASSERT(moduleFrontend);
  156. ui->actionRun->setEnabled(false);
  157. qobject_cast<QWidget*>(moduleFrontend->guiHandle())->setEnabled(false);
  158. ctkCmdLineModuleFuture future = moduleManager.run(moduleFrontend);
  159. ui->progressListWidget->addProgressWidget(moduleFrontend, future);
  160. ui->progressListWidget->setCurrentProgressWidget(moduleFrontend);
  161. ui->actionPause->setEnabled(future.canPause() && future.isRunning());
  162. ui->actionPause->setChecked(future.isPaused());
  163. ui->actionCancel->setEnabled(future.canCancel() && future.isRunning());
  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. if (settingsDialog == NULL)
  177. {
  178. settingsDialog = new ctkSettingsDialog(this);
  179. settings.restoreState(settingsDialog->objectName(), *settingsDialog);
  180. settingsDialog->setSettings(&settings);
  181. settingsDialog->addPanel(new ctkCmdLineModuleExplorerDirectorySettings(&directoryWatcher));
  182. settingsDialog->addPanel(new ctkCmdLineModuleExplorerModulesSettings(&moduleManager));
  183. }
  184. settingsDialog->exec();
  185. }
  186. void ctkCLModuleExplorerMainWindow::on_actionLoad_triggered()
  187. {
  188. QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Load modules..."));
  189. this->setCursor(Qt::BusyCursor);
  190. QFuture<ctkCmdLineModuleReference> future = QtConcurrent::mapped(fileNames, ctkCmdLineModuleConcurrentRegister(&this->moduleManager));
  191. future.waitForFinished();
  192. QString errorMsg;
  193. QList<ctkCmdLineModuleReference> refs = future.results();
  194. for(int i = 0; i < fileNames.size(); ++i)
  195. {
  196. if (!refs.at(i))
  197. {
  198. errorMsg += tr("Failed to register ") + fileNames.at(i) + "\n\n";
  199. }
  200. else if (!refs.at(i).xmlValidationErrorString().isEmpty() &&
  201. this->moduleManager.validationMode() == ctkCmdLineModuleManager::STRICT_VALIDATION)
  202. {
  203. errorMsg += tr("Failed to register ") + fileNames.at(i) + ":\n" + refs.at(i).xmlValidationErrorString() + "\n\n";
  204. }
  205. }
  206. this->unsetCursor();
  207. if (!errorMsg.isEmpty())
  208. {
  209. QMessageBox::critical(this, "Failed to register modules", errorMsg);
  210. }
  211. }
  212. void ctkCLModuleExplorerMainWindow::on_actionQuit_triggered()
  213. {
  214. this->close();
  215. }
  216. void ctkCLModuleExplorerMainWindow::on_actionReset_triggered()
  217. {
  218. this->tabList->activeTab()->resetValues();
  219. }
  220. void ctkCLModuleExplorerMainWindow::checkModulePaused()
  221. {
  222. if (this->currentFutureWatcher.future().isPaused())
  223. {
  224. if (!ui->actionPause->isChecked())
  225. {
  226. ui->actionPause->setChecked(true);
  227. }
  228. }
  229. else
  230. {
  231. if (ui->actionPause->isChecked())
  232. {
  233. ui->actionPause->setChecked(false);
  234. }
  235. }
  236. }
  237. void ctkCLModuleExplorerMainWindow::currentModuleResumed()
  238. {
  239. ui->actionPause->setChecked(false);
  240. }
  241. void ctkCLModuleExplorerMainWindow::currentModuleCanceled()
  242. {
  243. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  244. if (frontend)
  245. {
  246. ui->actionCancel->setEnabled(false);
  247. ui->actionPause->setEnabled(false);
  248. ui->actionRun->setEnabled(true);
  249. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  250. if (widget)
  251. {
  252. widget->setEnabled(true);
  253. }
  254. }
  255. }
  256. void ctkCLModuleExplorerMainWindow::currentModuleFinished()
  257. {
  258. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  259. if (frontend)
  260. {
  261. ui->actionCancel->setEnabled(false);
  262. ui->actionPause->setEnabled(false);
  263. ui->actionRun->setEnabled(true);
  264. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  265. if (widget)
  266. {
  267. widget->setEnabled(true);
  268. }
  269. }
  270. }
  271. void ctkCLModuleExplorerMainWindow::moduleTabActivated(ctkCmdLineModuleFrontend *module)
  272. {
  273. if (module == NULL)
  274. {
  275. ui->actionRun->setEnabled(false);
  276. ui->actionPause->setEnabled(false);
  277. ui->actionCancel->setEnabled(false);
  278. ui->actionReset->setEnabled(false);
  279. ui->outputText->setActiveFrontend(NULL);
  280. currentFutureWatcher.setFuture(ctkCmdLineModuleFuture());
  281. }
  282. else
  283. {
  284. ui->actionRun->setEnabled(!module->isRunning());
  285. ui->actionPause->setEnabled(module->future().canPause() && module->isRunning());
  286. ui->actionPause->setChecked(module->isPaused());
  287. ui->actionCancel->setEnabled(module->future().canCancel() && module->isRunning());
  288. ui->actionReset->setEnabled(true);
  289. ui->outputText->setActiveFrontend(module);
  290. currentFutureWatcher.setFuture(module->future());
  291. }
  292. }