ctkCmdLineModuleExplorerMainWindow.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <QDebug>
  30. ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
  31. QMainWindow(parent),
  32. ui(new Ui::ctkCmdLineModuleExplorerMainWindow),
  33. defaultModuleFrontendFactory(NULL),
  34. moduleManager(ctkCmdLineModuleManager::STRICT_VALIDATION, QDesktopServices::storageLocation(QDesktopServices::CacheLocation)),
  35. directoryWatcher(&moduleManager)
  36. {
  37. ui->setupUi(this);
  38. // Frontends
  39. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtGui;
  40. moduleFrontendFactories << new ctkCmdLineModuleFrontendFactoryQtWebKit;
  41. defaultModuleFrontendFactory = moduleFrontendFactories.front();
  42. ui->modulesTreeWidget->setModuleFrontendFactories(moduleFrontendFactories);
  43. // Backends
  44. ctkCmdLineModuleBackendFunctionPointer* backendFunctionPointer = new ctkCmdLineModuleBackendFunctionPointer;
  45. moduleBackends.push_back(new ctkCmdLineModuleBackendLocalProcess);
  46. moduleBackends.push_back(backendFunctionPointer);
  47. for(int i = 0; i < moduleBackends.size(); ++i)
  48. {
  49. moduleManager.registerBackend(moduleBackends[i]);
  50. }
  51. settingsDialog = new ctkSettingsDialog(this);
  52. settingsDialog->addPanel(new ctkCmdLineModuleExplorerDirectorySettings());
  53. tabList.reset(new ctkCmdLineModuleExplorerTabList(ui->mainTabWidget));
  54. // If a module is registered via the ModuleManager, add it the tree
  55. connect(&moduleManager, SIGNAL(moduleRegistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(addModuleItem(ctkCmdLineModuleReference)));
  56. // Double-clicking on an item in the tree creates a new tab with the default frontend
  57. connect(ui->modulesTreeWidget, SIGNAL(moduleDoubleClicked(ctkCmdLineModuleReference)), this, SLOT(addModuleTab(ctkCmdLineModuleReference)));
  58. // React to specific frontend creations
  59. connect(ui->modulesTreeWidget, SIGNAL(moduleFrontendCreated(ctkCmdLineModuleFrontend*)), tabList.data(), SLOT(addTab(ctkCmdLineModuleFrontend*)));
  60. // React to tab-changes
  61. connect(tabList.data(), SIGNAL(tabActivated(ctkCmdLineModuleFrontend*)), SLOT(moduleTabActivated(ctkCmdLineModuleFrontend*)));
  62. // Listen to future events for the currently active tab
  63. // Due to Qt bug 12152, we cannot listen to the "paused" signal because it is
  64. // not emitted directly when the QFuture is paused. Instead, it is emitted after
  65. // resuming the future, after the "resume" signal has been emitted... we use
  66. // a polling aproach instead.
  67. pollPauseTimer.setInterval(300);
  68. connect(&pollPauseTimer, SIGNAL(timeout()), SLOT(checkModulePaused()));
  69. connect(&currentFutureWatcher, SIGNAL(resumed()), SLOT(currentModuleResumed()));
  70. connect(&currentFutureWatcher, SIGNAL(canceled()), SLOT(currentModuleCanceled()));
  71. connect(&currentFutureWatcher, SIGNAL(finished()), SLOT(currentModuleFinished()));
  72. foreach(QUrl fpModule, backendFunctionPointer->registeredFunctionPointers())
  73. {
  74. moduleManager.registerModule(fpModule);
  75. }
  76. directoryWatcher.setDebug(true);
  77. directoryWatcher.setDirectories(QStringList(QCoreApplication::applicationDirPath()));
  78. moduleTabActivated(NULL);
  79. pollPauseTimer.start();
  80. }
  81. ctkCLModuleExplorerMainWindow::~ctkCLModuleExplorerMainWindow()
  82. {
  83. qDeleteAll(moduleBackends);
  84. qDeleteAll(moduleFrontendFactories);
  85. }
  86. void ctkCLModuleExplorerMainWindow::addModule(const QUrl &location)
  87. {
  88. moduleManager.registerModule(location);
  89. }
  90. void ctkCLModuleExplorerMainWindow::on_actionRun_triggered()
  91. {
  92. ctkCmdLineModuleFrontend* moduleFrontend = this->tabList->activeTab();
  93. Q_ASSERT(moduleFrontend);
  94. ctkCmdLineModuleExplorerProgressWidget* progressWidget = new ctkCmdLineModuleExplorerProgressWidget();
  95. this->ui->progressInfoWidget->layout()->addWidget(progressWidget);
  96. ui->actionRun->setEnabled(false);
  97. qobject_cast<QWidget*>(moduleFrontend->guiHandle())->setEnabled(false);
  98. ctkCmdLineModuleFuture future = moduleManager.run(moduleFrontend);
  99. ui->actionPause->setEnabled(future.canPause() && future.isRunning());
  100. ui->actionPause->setChecked(future.isPaused());
  101. ui->actionCancel->setEnabled(future.canCancel() && future.isRunning());
  102. progressWidget->setFuture(future);
  103. this->currentFutureWatcher.setFuture(future);
  104. }
  105. void ctkCLModuleExplorerMainWindow::on_actionPause_toggled(bool toggled)
  106. {
  107. this->currentFutureWatcher.setPaused(toggled);
  108. }
  109. void ctkCLModuleExplorerMainWindow::on_actionCancel_triggered()
  110. {
  111. this->currentFutureWatcher.cancel();
  112. }
  113. void ctkCLModuleExplorerMainWindow::on_actionOptions_triggered()
  114. {
  115. settingsDialog->exec();
  116. }
  117. void ctkCLModuleExplorerMainWindow::checkModulePaused()
  118. {
  119. if (this->currentFutureWatcher.future().isPaused())
  120. {
  121. if (!ui->actionPause->isChecked())
  122. {
  123. ui->actionPause->setChecked(true);
  124. }
  125. }
  126. else
  127. {
  128. if (ui->actionPause->isChecked())
  129. {
  130. ui->actionPause->setChecked(false);
  131. }
  132. }
  133. }
  134. void ctkCLModuleExplorerMainWindow::currentModuleResumed()
  135. {
  136. ui->actionPause->setChecked(false);
  137. }
  138. void ctkCLModuleExplorerMainWindow::currentModuleCanceled()
  139. {
  140. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  141. if (frontend)
  142. {
  143. ui->actionCancel->setEnabled(false);
  144. ui->actionPause->setEnabled(false);
  145. ui->actionRun->setEnabled(true);
  146. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  147. if (widget)
  148. {
  149. widget->setEnabled(true);
  150. }
  151. }
  152. }
  153. void ctkCLModuleExplorerMainWindow::currentModuleFinished()
  154. {
  155. ctkCmdLineModuleFrontend* frontend = this->tabList->activeTab();
  156. if (frontend)
  157. {
  158. ui->actionCancel->setEnabled(false);
  159. ui->actionPause->setEnabled(false);
  160. ui->actionRun->setEnabled(true);
  161. QWidget* widget = qobject_cast<QWidget*>(frontend->guiHandle());
  162. if (widget)
  163. {
  164. widget->setEnabled(true);
  165. }
  166. }
  167. }
  168. void ctkCLModuleExplorerMainWindow::moduleTabActivated(ctkCmdLineModuleFrontend *module)
  169. {
  170. if (module == NULL)
  171. {
  172. ui->actionRun->setEnabled(false);
  173. ui->actionPause->setEnabled(false);
  174. ui->actionCancel->setEnabled(false);
  175. currentFutureWatcher.setFuture(ctkCmdLineModuleFuture());
  176. }
  177. else
  178. {
  179. ui->actionRun->setEnabled(!module->isRunning());
  180. ui->actionPause->setEnabled(module->future().canPause() && module->isRunning());
  181. ui->actionPause->setChecked(module->isPaused());
  182. ui->actionCancel->setEnabled(module->future().canCancel() && module->isRunning());
  183. currentFutureWatcher.setFuture(module->future());
  184. }
  185. }
  186. void ctkCLModuleExplorerMainWindow::addModuleTab(const ctkCmdLineModuleReference &moduleRef)
  187. {
  188. ctkCmdLineModuleFrontend* moduleFrontend = this->defaultModuleFrontendFactory->create(moduleRef);
  189. this->tabList->addTab(moduleFrontend);
  190. }