From 4f12b61d155b8843f62bbe198d3bccc70a15de48 Mon Sep 17 00:00:00 2001
From: Rostislav Khlebnikov <r.khlebnikov@gmail.com>
Date: Mon, 10 Nov 2014 16:25:36 +0000
Subject: [PATCH] Different behavior for single and multiple selection context
 menus fixed.

---
 Modules/QtWidgets/QmitkNodeDescriptorManager.cpp | 79 ++++++++++++++++++------
 Modules/QtWidgets/QmitkNodeDescriptorManager.h   | 16 +++++
 2 files changed, 75 insertions(+), 20 deletions(-)

diff --git a/Modules/QtWidgets/QmitkNodeDescriptorManager.cpp b/Modules/QtWidgets/QmitkNodeDescriptorManager.cpp
index 2969362..423f1a9 100644
--- a/Modules/QtWidgets/QmitkNodeDescriptorManager.cpp
+++ b/Modules/QtWidgets/QmitkNodeDescriptorManager.cpp
@@ -118,46 +118,85 @@ QmitkNodeDescriptor* QmitkNodeDescriptorManager::GetDescriptor( const QString& _
 
   return _Descriptor;
 }
-QList<QAction*> QmitkNodeDescriptorManager::GetActions( const mitk::DataNode* _Node ) const
+
+QList<QmitkNodeDescriptor*> QmitkNodeDescriptorManager::GetAllDescriptors(const mitk::DataNode* _Node) const
 {
-  QList<QAction*> actions = m_UnknownDataNodeDescriptor->GetBatchActions();
-  actions.append(m_UnknownDataNodeDescriptor->GetActions());
-  QmitkNodeDescriptor* lastDescriptor = m_UnknownDataNodeDescriptor;
+  QList<QmitkNodeDescriptor*> _Descriptors;
 
-  for(QList<QmitkNodeDescriptor*>::const_iterator it = m_NodeDescriptors.begin(); it != m_NodeDescriptors.end(); ++it)
+  for (QList<QmitkNodeDescriptor*>::const_iterator it = m_NodeDescriptors.begin(); it != m_NodeDescriptors.end(); ++it)
   {
-    if((*it)->CheckNode(_Node))
+    if ((*it)->CheckNode(_Node)) {
+      _Descriptors << *it;
+    }
+  }
+
+  return _Descriptors;
+}
+
+QList<QmitkNodeDescriptor*> QmitkNodeDescriptorManager::GetAllDescriptors(const QString& _ClassName) const
+{
+  QList<QmitkNodeDescriptor*> _Descriptors;
+
+  if (_ClassName == "Unknown")
+  {
+    _Descriptors << m_UnknownDataNodeDescriptor;
+  }
+  else
+  {
+    for (QList<QmitkNodeDescriptor*>::const_iterator it = m_NodeDescriptors.begin(); it != m_NodeDescriptors.end(); ++it)
     {
-      actions.append(lastDescriptor->GetSeparator());
-      lastDescriptor = *it;
-      actions.append(lastDescriptor->GetBatchActions());
-      actions.append(lastDescriptor->GetActions());
+      if ((*it)->GetClassName() == _ClassName)
+        _Descriptors << *it;
     }
   }
 
+  return _Descriptors;
+}
+
+
+QList<QAction*> QmitkNodeDescriptorManager::GetActions( const mitk::DataNode* _Node ) const
+{
+  QList<QmitkNodeDescriptor*> descriptors;
+  descriptors << m_UnknownDataNodeDescriptor;
+  descriptors << GetAllDescriptors(_Node);
+  QList<QAction*> actions;
+
+  for (QList<QmitkNodeDescriptor*>::const_iterator it = descriptors.begin(); it != descriptors.end(); ++it)
+  {
+    actions.append((*it)->GetBatchActions());
+    actions.append((*it)->GetActions());
+    actions.append((*it)->GetSeparator());
+  }
+  actions.removeLast();
+
   return actions;
 }
 
 QList<QAction*> QmitkNodeDescriptorManager::GetActions( const QList<mitk::DataNode::Pointer> &_Nodes ) const
 {
-  QList<QAction*> actions = m_UnknownDataNodeDescriptor->GetBatchActions();
-  QSet<QmitkNodeDescriptor*> nodeDescriptors;
-  QmitkNodeDescriptor* lastDescriptor;
+  QList<QAction*> actions;
+  QSet<QmitkNodeDescriptor*> nodeDescriptorsSet;
 
   // find all descriptors for the nodes (unique)
   foreach (mitk::DataNode::Pointer node, _Nodes)
   {
-    lastDescriptor = this->GetDescriptor(node);
-    nodeDescriptors.insert(lastDescriptor);
+    QList<QmitkNodeDescriptor*> currentNodeDescriptors = GetAllDescriptors(node);
+    foreach(QmitkNodeDescriptor* descriptor, currentNodeDescriptors) {
+      nodeDescriptorsSet.insert(descriptor);
+    }
   }
+
+  QList<QmitkNodeDescriptor*> nodeDescriptorsList;
+  nodeDescriptorsList << m_UnknownDataNodeDescriptor;
+  nodeDescriptorsList << nodeDescriptorsSet.toList();
+
   // add all actions for the found descriptors
-  lastDescriptor = m_UnknownDataNodeDescriptor;
-  foreach (QmitkNodeDescriptor* descr, nodeDescriptors)
+  foreach(QmitkNodeDescriptor* descr, nodeDescriptorsList)
   {
-    actions.append(lastDescriptor->GetSeparator());
-    lastDescriptor = descr;
-    actions.append(lastDescriptor->GetBatchActions());
+    actions.append(descr->GetBatchActions());
+    actions.append(descr->GetSeparator());
   }
+  actions.removeLast();
 
   return actions;
 }
diff --git a/Modules/QtWidgets/QmitkNodeDescriptorManager.h b/Modules/QtWidgets/QmitkNodeDescriptorManager.h
index ecaccff..cc59556 100644
--- a/Modules/QtWidgets/QmitkNodeDescriptorManager.h
+++ b/Modules/QtWidgets/QmitkNodeDescriptorManager.h
@@ -75,6 +75,22 @@ public:
   QmitkNodeDescriptor* GetDescriptor(const QString& _ClassName) const;
 
   ///
+  /// Get all descriptors in the descriptors list that match the given node.
+  ///
+  /// \return a list of QmitkNodeDescriptor for the given node or empty list if there is no QmitkNodeDescriptor for _Node
+  /// \sa AddDescriptor()
+  ///
+  QList<QmitkNodeDescriptor*> GetAllDescriptors(const mitk::DataNode* _Node) const;
+
+  ///
+  /// Get all QmitkNodeDescriptors for the given class name
+  ///
+  /// \return a list of QmitkNodeDescriptor for the given class name or empty list if there is no QmitkNodeDescriptor for _ClassName
+  ///
+  QList<QmitkNodeDescriptor*> GetAllDescriptors(const QString& _ClassName) const;
+
+
+  ///
   /// \return The UnknownDataNodeDescriptor, which is the default Descriptor for all Nodes.
   ///
   QmitkNodeDescriptor* GetUnknownDataNodeDescriptor() const;
-- 
1.8.4.msysgit.0

