ctkCmdLineModuleExplorerTabList.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "ctkCmdLineModuleExplorerTabList.h"
  16. #include <ctkCmdLineModuleFrontend.h>
  17. #include <ctkCmdLineModuleFrontendFactory.h>
  18. #include <ctkCmdLineModuleDescription.h>
  19. #include <ctkCmdLineModuleManager.h>
  20. #include <ctkCmdLineModuleFuture.h>
  21. #include <QTabWidget>
  22. #include <QModelIndex>
  23. #include <QMessageBox>
  24. #include <QApplication>
  25. Q_DECLARE_METATYPE(ctkCmdLineModuleFrontendFactory*)
  26. ctkCmdLineModuleExplorerTabList::ctkCmdLineModuleExplorerTabList(QTabWidget *tabWidget)
  27. : TabWidget(tabWidget)
  28. {
  29. Q_ASSERT(TabWidget != NULL);
  30. connect(TabWidget, SIGNAL(currentChanged(int)), SLOT(tabIndexChanged(int)));
  31. connect(TabWidget, SIGNAL(tabCloseRequested(int)), SLOT(tabCloseRequested(int)));
  32. }
  33. ctkCmdLineModuleExplorerTabList::~ctkCmdLineModuleExplorerTabList()
  34. {
  35. qDeleteAll(this->TabIndexToFrontend);
  36. }
  37. ctkCmdLineModuleFrontend* ctkCmdLineModuleExplorerTabList::activeTab() const
  38. {
  39. int index = this->TabWidget->currentIndex();
  40. if (index < 0) return NULL;
  41. return this->TabIndexToFrontend[index];
  42. }
  43. QList<ctkCmdLineModuleFrontend *> ctkCmdLineModuleExplorerTabList::tabs() const
  44. {
  45. return this->TabIndexToFrontend;
  46. }
  47. void ctkCmdLineModuleExplorerTabList::setActiveTab(ctkCmdLineModuleFrontend *frontend)
  48. {
  49. this->TabWidget->setCurrentIndex(this->TabIndexToFrontend.indexOf(frontend));
  50. }
  51. void ctkCmdLineModuleExplorerTabList::addTab(ctkCmdLineModuleFrontend* moduleFrontend)
  52. {
  53. QWidget* widget = qobject_cast<QWidget*>(moduleFrontend->guiHandle());
  54. this->TabIndexToFrontend.push_back(moduleFrontend);
  55. int index = this->TabWidget->addTab(widget, moduleFrontend->moduleReference().description().title());
  56. this->TabWidget->setCurrentIndex(index);
  57. }
  58. void ctkCmdLineModuleExplorerTabList::tabIndexChanged(int index)
  59. {
  60. if (index < 0)
  61. {
  62. emit tabActivated(NULL);
  63. return;
  64. }
  65. emit tabActivated(this->TabIndexToFrontend[index]);
  66. }
  67. void ctkCmdLineModuleExplorerTabList::tabCloseRequested(int index)
  68. {
  69. ctkCmdLineModuleFrontend* frontend = this->TabIndexToFrontend[index];
  70. bool removeTab = false;
  71. if (frontend->isRunning())
  72. {
  73. if (frontend->future().canCancel())
  74. {
  75. QMessageBox::StandardButton button =
  76. QMessageBox::warning(QApplication::topLevelWidgets().front(),
  77. "Closing a running module",
  78. "The module '" + frontend->moduleReference().description().title() + "' is still running.\n"
  79. "Closing the tab will cancel the current computation.",
  80. QMessageBox::Ok | QMessageBox::Cancel);
  81. if (button == QMessageBox::Ok)
  82. {
  83. frontend->future().cancel();
  84. removeTab = true;
  85. }
  86. }
  87. else
  88. {
  89. QMessageBox::information(QApplication::topLevelWidgets().front(),
  90. "Closing not possible",
  91. "The module '" + frontend->moduleReference().description().title() + "' is still running "
  92. "and does not support being canceled.");
  93. }
  94. }
  95. else
  96. {
  97. removeTab = true;
  98. }
  99. if (removeTab)
  100. {
  101. this->TabIndexToFrontend.removeAt(index);
  102. this->TabWidget->removeTab(index);
  103. emit this->tabClosed(frontend);
  104. delete frontend;
  105. }
  106. }