ctkCmdLineModuleExplorerMainWindow.cpp 17 KB

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