ctkCmdLineModuleExplorerMainWindow.cpp 7.7 KB

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