Selaa lähdekoodia

Remove entries from the tree if modules are unregistered.

Sascha Zelzer 12 vuotta sitten
vanhempi
commit
2964e7806b

+ 1 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.cpp

@@ -76,6 +76,7 @@ ctkCLModuleExplorerMainWindow::ctkCLModuleExplorerMainWindow(QWidget *parent) :
 
   // If a module is registered via the ModuleManager, add it the tree
   connect(&moduleManager, SIGNAL(moduleRegistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(addModuleItem(ctkCmdLineModuleReference)));
+  connect(&moduleManager, SIGNAL(moduleUnregistered(ctkCmdLineModuleReference)), ui->modulesTreeWidget, SLOT(removeModuleItem(ctkCmdLineModuleReference)));
   // Double-clicking on an item in the tree creates a new tab with the default frontend
   connect(ui->modulesTreeWidget, SIGNAL(moduleDoubleClicked(ctkCmdLineModuleReference)), this, SLOT(addModuleTab(ctkCmdLineModuleReference)));
   // React to specific frontend creations

+ 25 - 1
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerTreeWidget.cpp

@@ -116,7 +116,31 @@ void ctkCmdLineModuleExplorerTreeWidget::addModuleItem(const ctkCmdLineModuleRef
     rootItem->setText(0, category);
     TreeWidgetCategories[category] = rootItem;
   }
-  new ctkCmdLineModuleTreeWidgetItem(rootItem, moduleRef);
+  TreeWidgetItems[moduleRef] = new ctkCmdLineModuleTreeWidgetItem(rootItem, moduleRef);
+}
+
+void ctkCmdLineModuleExplorerTreeWidget::removeModuleItem(const ctkCmdLineModuleReference &moduleRef)
+{
+  QString category = moduleRef.description().category();
+  if (category.isEmpty())
+  {
+    category = CATEGORY_UNKNOWN;
+  }
+
+
+  QTreeWidgetItem* treeWidgetItem = TreeWidgetItems.take(moduleRef);
+  if (treeWidgetItem == NULL) return;
+
+  this->removeItemWidget(treeWidgetItem, 0);
+  delete treeWidgetItem;
+
+  QTreeWidgetItem* rootItem = TreeWidgetCategories[category];
+  if (rootItem && rootItem->childCount() == 0)
+  {
+    this->removeItemWidget(rootItem, 0);
+    TreeWidgetCategories.remove(category);
+    delete rootItem;
+  }
 }
 
 void ctkCmdLineModuleExplorerTreeWidget::contextMenuEvent(QContextMenuEvent *event)

+ 2 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerTreeWidget.h

@@ -44,6 +44,7 @@ public:
   void setModuleFrontendFactories(const QList<ctkCmdLineModuleFrontendFactory*>& frontendFactories);
 
   Q_SLOT void addModuleItem(const ctkCmdLineModuleReference& moduleRef);
+  Q_SLOT void removeModuleItem(const ctkCmdLineModuleReference& moduleRef);
 
   Q_SIGNAL void moduleDoubleClicked(ctkCmdLineModuleReference moduleRef);
   Q_SIGNAL void moduleFrontendCreated(ctkCmdLineModuleFrontend* moduleFrontend);
@@ -65,6 +66,7 @@ private:
   ctkCmdLineModuleReference ContextReference;
   QList<ctkCmdLineModuleFrontendFactory*> FrontendFactories;
   QHash<QString, QTreeWidgetItem*> TreeWidgetCategories;
+  QHash<ctkCmdLineModuleReference, QTreeWidgetItem*> TreeWidgetItems;
   QHash<QAction*, ctkCmdLineModuleFrontendFactory*> ActionsToFrontendFactoryMap;
 };