ctkCmdLineModuleExplorerTreeWidget.cpp 5.7 KB

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