ctkCmdLineModuleExplorerTreeWidget.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "ctkCmdLineModuleExplorerTreeWidget.h"
  16. #include <ctkCmdLineModuleFrontend.h>
  17. #include <ctkCmdLineModuleBackend.h>
  18. #include <ctkCmdLineModuleFrontendFactory.h>
  19. #include <ctkCmdLineModuleDescription.h>
  20. #include <QContextMenuEvent>
  21. #include <QMenu>
  22. #include <QDebug>
  23. QString ctkCmdLineModuleExplorerTreeWidget::CATEGORY_UNKNOWN = "Uncategorized";
  24. class ctkCmdLineModuleTreeWidgetItem : public QTreeWidgetItem
  25. {
  26. public:
  27. static const int CmdLineModuleType = 1001;
  28. ctkCmdLineModuleTreeWidgetItem(const ctkCmdLineModuleReference& moduleRef)
  29. : QTreeWidgetItem(CmdLineModuleType), ModuleRef(moduleRef)
  30. {
  31. init();
  32. }
  33. ctkCmdLineModuleTreeWidgetItem(QTreeWidget* parent, const ctkCmdLineModuleReference& moduleRef)
  34. : QTreeWidgetItem(parent, CmdLineModuleType), ModuleRef(moduleRef)
  35. {
  36. init();
  37. }
  38. ctkCmdLineModuleTreeWidgetItem(QTreeWidgetItem* parent, const ctkCmdLineModuleReference& moduleRef)
  39. : QTreeWidgetItem(parent, CmdLineModuleType), ModuleRef(moduleRef)
  40. {
  41. init();
  42. }
  43. ctkCmdLineModuleReference moduleReference() const
  44. {
  45. return ModuleRef;
  46. }
  47. private:
  48. void init()
  49. {
  50. this->setText(0, ModuleRef.description().title() + " [" + ModuleRef.backend()->name() + "]");
  51. this->setData(0, Qt::UserRole, QVariant::fromValue(ModuleRef));
  52. }
  53. ctkCmdLineModuleReference ModuleRef;
  54. };
  55. ctkCmdLineModuleExplorerTreeWidget::ctkCmdLineModuleExplorerTreeWidget(QWidget *parent)
  56. : QTreeWidget(parent)
  57. {
  58. this->ContextMenu = new QMenu(this);
  59. this->ShowFrontendMenu = this->ContextMenu->addMenu("Create Frontend");
  60. this->ContextMenu->addAction("Properties");
  61. connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(moduleDoubleClicked(QModelIndex)));
  62. }
  63. void ctkCmdLineModuleExplorerTreeWidget::setModuleFrontendFactories(const QList<ctkCmdLineModuleFrontendFactory *> &frontendFactories)
  64. {
  65. this->FrontendFactories = frontendFactories;
  66. this->ShowFrontendMenu->clear();
  67. foreach(ctkCmdLineModuleFrontendFactory* factory, this->FrontendFactories)
  68. {
  69. QAction* action = this->ShowFrontendMenu->addAction(factory->name(), this, SLOT(frontendFactoryActionTriggered()));
  70. this->ActionsToFrontendFactoryMap[action] = factory;
  71. action->setToolTip(factory->description());
  72. }
  73. }
  74. void ctkCmdLineModuleExplorerTreeWidget::addModuleItem(const ctkCmdLineModuleReference &moduleRef)
  75. {
  76. QString category = moduleRef.description().category();
  77. QTreeWidgetItem* rootItem = NULL;
  78. if (category.isEmpty())
  79. {
  80. category = CATEGORY_UNKNOWN;
  81. }
  82. rootItem = TreeWidgetCategories[category];
  83. if (rootItem == NULL)
  84. {
  85. // lazily create the root item for the category
  86. rootItem = new QTreeWidgetItem(this);
  87. rootItem->setText(0, category);
  88. TreeWidgetCategories[category] = rootItem;
  89. }
  90. TreeWidgetItems[moduleRef] = new ctkCmdLineModuleTreeWidgetItem(rootItem, moduleRef);
  91. }
  92. void ctkCmdLineModuleExplorerTreeWidget::removeModuleItem(const ctkCmdLineModuleReference &moduleRef)
  93. {
  94. QString category = moduleRef.description().category();
  95. if (category.isEmpty())
  96. {
  97. category = CATEGORY_UNKNOWN;
  98. }
  99. QTreeWidgetItem* treeWidgetItem = TreeWidgetItems.take(moduleRef);
  100. if (treeWidgetItem == NULL) return;
  101. this->removeItemWidget(treeWidgetItem, 0);
  102. delete treeWidgetItem;
  103. QTreeWidgetItem* rootItem = TreeWidgetCategories[category];
  104. if (rootItem && rootItem->childCount() == 0)
  105. {
  106. this->removeItemWidget(rootItem, 0);
  107. TreeWidgetCategories.remove(category);
  108. delete rootItem;
  109. }
  110. }
  111. void ctkCmdLineModuleExplorerTreeWidget::contextMenuEvent(QContextMenuEvent *event)
  112. {
  113. QTreeWidgetItem* item = this->itemAt(this->viewport()->mapFromGlobal(event->globalPos()));
  114. if (item != NULL && item->type() == ctkCmdLineModuleTreeWidgetItem::CmdLineModuleType)
  115. {
  116. this->ContextReference = item->data(0, Qt::UserRole).value<ctkCmdLineModuleReference>();
  117. this->ContextMenu->exec(event->globalPos());
  118. event->accept();
  119. }
  120. else
  121. {
  122. this->ContextReference = ctkCmdLineModuleReference();
  123. event->ignore();
  124. }
  125. }
  126. void ctkCmdLineModuleExplorerTreeWidget::moduleDoubleClicked(const QModelIndex &index)
  127. {
  128. QVariant data = index.data(Qt::UserRole);
  129. if (!data.isValid()) return;
  130. ctkCmdLineModuleReference moduleRef = data.value<ctkCmdLineModuleReference>();
  131. if (!moduleRef) return;
  132. emit moduleDoubleClicked(moduleRef);
  133. }
  134. void ctkCmdLineModuleExplorerTreeWidget::frontendFactoryActionTriggered()
  135. {
  136. ctkCmdLineModuleFrontendFactory* frontendFactory = this->ActionsToFrontendFactoryMap[static_cast<QAction*>(this->sender())];
  137. ctkCmdLineModuleFrontend* frontend = frontendFactory->create(this->ContextReference);
  138. emit moduleFrontendCreated(frontend);
  139. }