ctkCmdLineModuleExplorerMainWindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 "ctkCmdLineModuleExplorerGeneralModuleSettings.h"
  18. #include "ctkCmdLineModuleExplorerDirectorySettings.h"
  19. #include "ctkCmdLineModuleExplorerModulesSettings.h"
  20. #include "ctkCmdLineModuleExplorerTabList.h"
  21. #include "ctkCmdLineModuleExplorerProgressWidget.h"
  22. #include "ctkCmdLineModuleExplorerConstants.h"
  23. #include "ctkCmdLineModuleExplorerUtils.h"
  24. #include <ctkCmdLineModuleManager.h>
  25. #include <ctkCmdLineModuleConcurrentHelpers.h>
  26. #include <ctkCmdLineModuleDescription.h>
  27. #include <ctkCmdLineModuleFrontendFactoryQtGui.h>
  28. #include <ctkCmdLineModuleFrontendFactoryQtWebKit.h>
  29. #include <ctkCmdLineModuleBackendLocalProcess.h>
  30. #include <ctkCmdLineModuleBackendFunctionPointer.h>
  31. #include <ctkCmdLineModuleBackendXMLChecker.h>
  32. #include <ctkException.h>
  33. #include <ctkCmdLineModuleXmlException.h>
  34. #include <ctkSettingsDialog.h>
  35. #include <QtConcurrentMap>
  36. #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
  37. #include <QDesktopServices>
  38. #else
  39. #include <QStandardPaths>
  40. #endif
  41. #include <QMessageBox>
  42. #include <QFutureSynchronizer>
  43. #include <QCloseEvent>
  44. #include <QFileDialog>
  45. #include <QMessageBox>
  46. //-----------------------------------------------------------------------------
  47. ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
  48. QMainWindow(parent),
  49. ui(new Ui::ctkCmdLineModuleExplorerMainWindow),
  50. defaultModuleFrontendFactory(NULL),
  51. #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
  52. moduleManager(ctkCmdLineModuleManager::WEAK_VALIDATION, QDesktopServices::storageLocation(QDesktopServices::CacheLocation)),
  53. #else
  54. moduleManager(ctkCmdLineModuleManager::WEAK_VALIDATION, QStandardPaths::writableLocation(QStandardPaths::CacheLocation)),
  55. #endif
  56. directoryWatcher(&moduleManager),
  57. settingsDialog(NULL)
  58. {
  59. ui->setupUi(this);
  60. settings.restoreState(this->objectName(), *this);
  61. if (!settings.contains(ctkCmdLineModuleExplorerConstants::KEY_MAX_PARALLEL_MODULES))
  62. {
  63. settings.setValue(ctkCmdLineModuleExplorerConstants::KEY_MAX_PARALLEL_MODULES, QThread::idealThreadCount());
  64. }
  65. QThreadPool::globalInstance()->setMaxThreadCount(settings.value(ctkCmdLineModuleExplorerConstants::KEY_MAX_PARALLEL_MODULES,
  66. QThread::idealThreadCount()).toInt());
  67. // Frontends
  68. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtGui;
  69. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtWebKit;
  70. defaultModuleFrontendFactory = moduleFrontendFactories.front();
  71. ui->modulesTreeWidget->setModuleFrontendFactories(moduleFrontendFactories, moduleFrontendFactories.front());
  72. ui->modulesSearchBox->setClearIcon(QIcon(":/icons/clear.png"));
  73. connect(ui->modulesSearchBox, SIGNAL(textChanged(QString)), ui->modulesTreeWidget, SLOT(setFilter(QString)));
  74. // Backends
  75. ctkCmdLineModuleBackendFunctionPointer* backendFunctionPointer = new ctkCmdLineModuleBackendFunctionPointer;
  76. moduleBackends.push_back(backendFunctionPointer);
  77. xmlCheckerBackEnd = new ctkCmdLineModuleBackendXMLChecker;
  78. moduleBackends.push_back(xmlCheckerBackEnd);
  79. moduleBackends.push_back(new ctkCmdLineModuleBackendLocalProcess);
  80. for(int i = 0; i < moduleBackends.size(); ++i)
  81. {
  82. moduleManager.registerBackend(moduleBackends[i]);
  83. }
  84. tabList.reset(new ctkCmdLineModuleExplorerTabList(ui->mainTabWidget));
  85. // If a module is registered via the ModuleManager, add it to the tree
  86. connect(&moduleManager, SIGNAL(moduleRegistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(addModuleItem(ctkCmdLineModuleReference)));
  87. connect(&moduleManager, SIGNAL(moduleUnregistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(removeModuleItem(ctkCmdLineModuleReference)));
  88. // React to specific frontend creations
  89. connect(ui->modulesTreeWidget, SIGNAL(moduleFrontendCreated(ctkCmdLineModuleFrontend*)), tabList.data(), SLOT(addTab(ctkCmdLineModuleFrontend*)));
  90. // React to tab-changes
  91. connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), SLOT(moduleTabActivated(ctkCmdLineModuleFrontend*)));
  92. connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), ui->progressListWidget, SLOT(setCurrentProgressWidget(ctkCmdLineModuleFrontend*)));
  93. connect(tabList.data(), SIGNAL(tabClosed(ctkCmdLineModuleFrontend*)), ui->outputText, SLOT(frontendRemoved(ctkCmdLineModuleFrontend*)));
  94. connect(tabList.data(), SIGNAL(tabClosed(ctkCmdLineModuleFrontend*)), ui->progressListWidget, SLOT(removeProgressWidget(ctkCmdLineModuleFrontend*)));
  95. connect(ui->progressListWidget, SIGNAL(progressWidgetClicked(ctkCmdLineModuleFrontend*)), tabList.data(), SLOT(setActiveTab(ctkCmdLineModuleFrontend*)));
  96. connect(ui->ClearButton, SIGNAL(clicked()), ui->progressListWidget, SLOT(clearList()));
  97. connect(ui->m_CheckXMLButton, SIGNAL(pressed()), this, SLOT(checkXMLPressed()));
  98. // Listen to future events for the currently active tab
  99. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  100. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  101. // resuming the future, after the "resume" signal has been emitted... we use
  102. // a polling aproach instead.
  103. pollPauseTimer.setInterval(300);
  104. connect(&pollPauseTimer, SIGNAL(timeout()), SLOT(checkModulePaused()));
  105. connect(&currentFutureWatcher, SIGNAL(resumed()), SLOT(currentModuleResumed()));
  106. connect(&currentFutureWatcher, SIGNAL(canceled()), SLOT(currentModuleCanceled()));
  107. connect(&currentFutureWatcher, SIGNAL(finished()), SLOT(currentModuleFinished()));
  108. foreach(QUrl fpModule, backendFunctionPointer->registeredFunctionPointers())
  109. {
  110. moduleManager.registerModule(fpModule);
  111. }
  112. // Register persistent modules
  113. QFuture<void> future = QtConcurrent::mapped(settings.value(ctkCmdLineModuleExplorerConstants::KEY_REGISTERED_MODULES).toStringList(),
  114. ctkCmdLineModuleConcurrentRegister(&moduleManager, true));
  115. // Start watching directories
  116. directoryWatcher.setDebug(true);
  117. directoryWatcher.setDirectories(settings.value(ctkCmdLineModuleExplorerConstants::KEY_SEARCH_PATHS).toStringList());
  118. moduleTabActivated(NULL);
  119. pollPauseTimer.start();
  120. future.waitForFinished();
  121. }
  122. //-----------------------------------------------------------------------------
  123. ctkCLModuleExplorerMainWindow::~ctkCLModuleExplorerMainWindow()
  124. {
  125. qDeleteAll(moduleBackends);
  126. qDeleteAll(moduleFrontendFactories);
  127. settings.saveState(*this, this->objectName());
  128. if (settingsDialog)
  129. {
  130. settings.saveState(*settingsDialog, settingsDialog->objectName());
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. void ctkCLModuleExplorerMainWindow::addModule(const QUrl &location)
  135. {
  136. moduleManager.registerModule(location);
  137. }
  138. //-----------------------------------------------------------------------------
  139. void ctkCLModuleExplorerMainWindow::closeEvent(QCloseEvent *event)
  140. {
  141. QList<ctkCmdLineModuleFrontend*> runningFrontends;
  142. foreach (ctkCmdLineModuleFrontend* frontend, this->tabList->tabs())
  143. {
  144. if (frontend->isRunning())
  145. {
  146. runningFrontends << frontend;
  147. }
  148. }
  149. if (!runningFrontends.empty())
  150. {
  151. QMessageBox::StandardButton button =
  152. QMessageBox::warning(QApplication::activeWindow(),
  153. QString("Closing %1 running modules").arg(runningFrontends.size()),
  154. "Some modules are still running.\n"
  155. "Closing the application will cancel all current computations.",
  156. QMessageBox::Ok | QMessageBox::Cancel);
  157. if (button == QMessageBox::Ok)
  158. {
  159. QFutureSynchronizer<void> futureSync;
  160. futureSync.setCancelOnWait(true);
  161. foreach(ctkCmdLineModuleFrontend* frontend, runningFrontends)
  162. {
  163. if (frontend->future().canCancel())
  164. {
  165. futureSync.addFuture(frontend->future());
  166. }
  167. }
  168. futureSync.waitForFinished();
  169. event->accept();
  170. QMainWindow::closeEvent(event);
  171. return;
  172. }
  173. else
  174. {
  175. event->ignore();
  176. return;
  177. }
  178. }
  179. event->accept();
  180. }
  181. //-----------------------------------------------------------------------------
  182. void ctkCLModuleExplorerMainWindow::on_actionRun_triggered()
  183. {
  184. ctkCmdLineModuleFrontend* moduleFrontend = this->tabList->activeTab();
  185. Q_ASSERT(moduleFrontend);
  186. ui->actionRun->setEnabled(false);
  187. qobject_cast<QWidget*>(moduleFrontend->guiHandle())->setEnabled(false);
  188. ctkCmdLineModuleFuture future = moduleManager.run(moduleFrontend);
  189. ui->progressListWidget->addProgressWidget(moduleFrontend, future);
  190. ui->progressListWidget->setCurrentProgressWidget(moduleFrontend);
  191. ui->actionPause->setEnabled(future.canPause() && future.isRunning());
  192. ui->actionPause->setChecked(future.isPaused());
  193. ui->actionCancel->setEnabled(future.canCancel() && future.isRunning());
  194. this->currentFutureWatcher.setFuture(future);
  195. }
  196. //-----------------------------------------------------------------------------
  197. void ctkCLModuleExplorerMainWindow::on_actionPause_toggled(bool toggled)
  198. {
  199. this->currentFutureWatcher.setPaused(toggled);
  200. }
  201. //-----------------------------------------------------------------------------
  202. void ctkCLModuleExplorerMainWindow::on_actionCancel_triggered()
  203. {
  204. this->currentFutureWatcher.cancel();
  205. }
  206. //-----------------------------------------------------------------------------
  207. void ctkCLModuleExplorerMainWindow::on_actionOptions_triggered()
  208. {
  209. if (settingsDialog == NULL)
  210. {
  211. settingsDialog = new ctkSettingsDialog(this);
  212. settings.restoreState(settingsDialog->objectName(), *settingsDialog);
  213. settingsDialog->setSettings(&settings);
  214. ctkSettingsPanel* generalModulePanel = new ctkCmdLineModuleExplorerGeneralModuleSettings();
  215. settingsDialog->addPanel(generalModulePanel);
  216. settingsDialog->addPanel(new ctkCmdLineModuleExplorerDirectorySettings(&directoryWatcher), generalModulePanel);
  217. settingsDialog->addPanel(new ctkCmdLineModuleExplorerModulesSettings(&moduleManager), generalModulePanel);
  218. }
  219. settingsDialog->exec();
  220. }
  221. //-----------------------------------------------------------------------------
  222. void ctkCLModuleExplorerMainWindow::on_actionLoad_triggered()
  223. {
  224. QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Load modules..."));
  225. this->setCursor(Qt::BusyCursor);
  226. QFuture<ctkCmdLineModuleReference> future = QtConcurrent::mapped(fileNames, ctkCmdLineModuleConcurrentRegister(&this->moduleManager));
  227. future.waitForFinished();
  228. this->unsetCursor();
  229. ctkCmdLineModuleExplorerUtils::messageBoxModuleRegistration(fileNames, future.results(),
  230. this->moduleManager.validationMode());
  231. }
  232. //-----------------------------------------------------------------------------
  233. void ctkCLModuleExplorerMainWindow::on_actionQuit_triggered()
  234. {
  235. this->close();
  236. }
  237. //-----------------------------------------------------------------------------
  238. void ctkCLModuleExplorerMainWindow::on_actionReset_triggered()
  239. {
  240. this->tabList->activeTab()->resetValues();
  241. }
  242. //-----------------------------------------------------------------------------
  243. void ctkCLModuleExplorerMainWindow::on_actionClear_Cache_triggered()
  244. {
  245. moduleManager.clearCache();
  246. }
  247. //-----------------------------------------------------------------------------
  248. void ctkCLModuleExplorerMainWindow::on_actionReload_Modules_triggered()
  249. {
  250. moduleManager.reloadModules();
  251. }
  252. //-----------------------------------------------------------------------------
  253. void ctkCLModuleExplorerMainWindow::checkModulePaused()
  254. {
  255. if (this->currentFutureWatcher.future().isPaused())
  256. {
  257. if (!ui->actionPause->isChecked())
  258. {
  259. ui->actionPause->setChecked(true);
  260. }
  261. }
  262. else
  263. {
  264. if (ui->actionPause->isChecked())
  265. {
  266. ui->actionPause->setChecked(false);
  267. }
  268. }
  269. }
  270. //-----------------------------------------------------------------------------
  271. void ctkCLModuleExplorerMainWindow::currentModuleResumed()
  272. {
  273. ui->actionPause->setChecked(false);
  274. }
  275. //-----------------------------------------------------------------------------
  276. void ctkCLModuleExplorerMainWindow::currentModuleCanceled()
  277. {
  278. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  279. if (frontend)
  280. {
  281. ui->actionCancel->setEnabled(false);
  282. ui->actionPause->setEnabled(false);
  283. ui->actionRun->setEnabled(true);
  284. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  285. if (widget)
  286. {
  287. widget->setEnabled(true);
  288. }
  289. }
  290. }
  291. //-----------------------------------------------------------------------------
  292. void ctkCLModuleExplorerMainWindow::currentModuleFinished()
  293. {
  294. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  295. if (frontend)
  296. {
  297. ui->actionCancel->setEnabled(false);
  298. ui->actionPause->setEnabled(false);
  299. ui->actionRun->setEnabled(true);
  300. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  301. if (widget)
  302. {
  303. widget->setEnabled(true);
  304. }
  305. }
  306. }
  307. //-----------------------------------------------------------------------------
  308. void ctkCLModuleExplorerMainWindow::checkXMLPressed()
  309. {
  310. xmlCheckerBackEnd->setXML(ui->m_XMLToValidate->toPlainText());
  311. QUrl url(QString("xmlchecker://should call ctkCmdLineModuleBackendXMLChecker"));
  312. qDebug() << "ctkCLModuleExplorerMainWindow::checkXMLPressed validating:\n" << ui->m_XMLToValidate->toPlainText();
  313. ctkCmdLineModuleManager::ValidationMode previousMode = moduleManager.validationMode();
  314. try
  315. {
  316. ctkCmdLineModuleReference ref = moduleManager.moduleReference(url);
  317. if (ref)
  318. {
  319. moduleManager.unregisterModule(ref);
  320. }
  321. moduleManager.setValidationMode(ctkCmdLineModuleManager::STRICT_VALIDATION);
  322. moduleManager.registerModule(url);
  323. moduleManager.setValidationMode(previousMode);
  324. } catch (ctkException& except)
  325. {
  326. moduleManager.setValidationMode(previousMode);
  327. QWidget* widget = QApplication::activeModalWidget();
  328. if (widget == NULL) widget = QApplication::activeWindow();
  329. QMessageBox::critical(widget, QObject::tr("Failed while checking XML:"), except.message());
  330. }
  331. }
  332. //-----------------------------------------------------------------------------
  333. void ctkCLModuleExplorerMainWindow::moduleTabActivated(ctkCmdLineModuleFrontend *module)
  334. {
  335. if (module == NULL)
  336. {
  337. ui->actionRun->setEnabled(false);
  338. ui->actionPause->setEnabled(false);
  339. ui->actionCancel->setEnabled(false);
  340. ui->actionReset->setEnabled(false);
  341. ui->outputText->setActiveFrontend(NULL);
  342. currentFutureWatcher.setFuture(ctkCmdLineModuleFuture());
  343. }
  344. else
  345. {
  346. ui->actionRun->setEnabled(!module->isRunning());
  347. ui->actionPause->setEnabled(module->future().canPause() && module->isRunning());
  348. ui->actionPause->blockSignals(true);
  349. ui->actionPause->setChecked(module->isPaused());
  350. ui->actionPause->blockSignals(false);
  351. ui->actionCancel->setEnabled(module->future().canCancel() && module->isRunning());
  352. ui->actionReset->setEnabled(true);
  353. ui->outputText->setActiveFrontend(module);
  354. currentFutureWatcher.setFuture(module->future());
  355. }
  356. }