Index: src/UIElements/Qt4/QtMainWindow.cpp
===================================================================
--- src/UIElements/Qt4/QtMainWindow.cpp	(revision e4afb4b989433224538ab4ba0f471e4573bbeaf2)
+++ src/UIElements/Qt4/QtMainWindow.cpp	(revision ffb9ad3ad26c892a8aa0013c675637237728b887)
@@ -43,4 +43,5 @@
 #include "Menu/Qt4/QtMenu.hpp"
 #include "Menu/ActionMenuItem.hpp"
+#include "Menu/SubMenuItem.hpp"
 #include "Views/Qt4/QtWorldView.hpp"
 #include "Views/Qt4/GLMoleculeView.hpp"
@@ -48,6 +49,4 @@
 #include "Views/Qt4/QtStatusBar.hpp"
 
-using namespace std;
-
 QtMainWindow::QtMainWindow(QApplication *_theApp) :
     theApp(_theApp)
@@ -63,20 +62,10 @@
   MenuBar = menuBar();
 
-  std::map <std::string, QtMenu *> NametoTextMenuMap;
-  // go through all menus and create them
-  QtMenu *Menu = NULL;
-  MenuDescription menudescriptions;
-  for(MenuDescription::const_iterator iter = menudescriptions.getBeginIter(); iter != menudescriptions.getEndIter(); ++iter) {
-    cout << "Creating menu " << *iter << endl;
-    Menu = new QtMenu(menudescriptions.getName(*iter).c_str());
-    MenuBar->addMenu(Menu);
-    NametoTextMenuMap.insert( pair <std::string, QtMenu *> (menudescriptions.getName(*iter), Menu) );
-    //new SubMenuItem(getSuitableShortForm(menudescriptions.getName(iter->first.c_str())),menudescriptions.getName(iter->first.c_str()),main_menu,Menu);
-  }
-
-  // go through all actions and add them to its menu
-  for (std::map <std::string, QtMenu *>::iterator MenuRunner = NametoTextMenuMap.begin(); MenuRunner != NametoTextMenuMap.end(); ++MenuRunner) {
-    cout << "Creating Action " << MenuRunner->first << " in menu " << MenuRunner->second << endl;
-    populateMenu(MenuRunner->second, MenuRunner->first);
+  // populate menus and add actions
+  {
+    QtMenuReferenceMap NametoTextMenuMap; // contains token to menu reference map
+    MenuShortcutMap ShortcutMap;
+    populateMenu(NametoTextMenuMap, ShortcutMap);
+    populateMenuWithActions(NametoTextMenuMap, ShortcutMap);
   }
 
@@ -97,5 +86,4 @@
 {
   menuBar()->clear();
-  delete editMoleculesMenu;
 }
 
@@ -105,30 +93,114 @@
 }
 
-char QtMainWindow::getSuitableShortForm(set <char> &ShortcutList, const std::string name) const
-{
-  for (std::string::const_iterator CharRunner = name.begin(); CharRunner != name.end(); ++CharRunner) {
-    if (ShortcutList.find(*CharRunner) == ShortcutList.end())
-      return *CharRunner;
-  }
-  DoeLog(1) && (eLog() << Verbose(1) << "Could not find a suitable shortform for TextWindow::getSuitableShortForm()." << endl);
-  return ((char)(ShortcutList.size() % 10) + '0');
-}
-
-void QtMainWindow::populateMenu(QtMenu* Menu, const  std::string &MenuName)
-{
+/** Puts Qt's token, the ampersand, in front of the accelerator char in the menu name.
+ * \param ShortcutMap map to all already present accelerator keys
+ * \param MenuName Action of menu
+ * \param ActionName Action of menu
+ * \return name with ampersand added at the right place
+ */
+std::string QtMainWindow::getNameWithAccelerator(MenuShortcutMap &ShortcutMap, const std::string &MenuName, const std::string &ActionName) const
+{
+  std::string newname;
+  bool Inserted = false;
+  std::pair < MenuShortcutMap::iterator, bool > Inserter;
+  for (std::string::const_iterator CharRunner = ActionName.begin(); CharRunner != ActionName.end(); ++CharRunner) {
+    if (!Inserted) {
+      Inserter = ShortcutMap.insert( std::pair<std::string, char> (MenuName, *CharRunner) );
+      if (Inserter.second) {
+        newname += '&';
+        Inserted = true;
+      }
+    }
+    newname += *CharRunner;
+  }
+  return newname;
+}
+
+/** Instantiate all menus.
+ * \param NametoTextMenuMap lookup for token to menu reference
+ */
+void QtMainWindow::populateMenu(QtMenuReferenceMap &NametoTextMenuMap, MenuShortcutMap &ShortcutMap)
+{
+  // go through all menus and create them
+  std::map <std::string, int> TopPositions;
+  TopPositions.insert( std::pair<std::string, int> ("", 0) ); // contains which position was added last
+  QtMenu *Menu = NULL;
+  MenuDescription menudescriptions;
+  std::set <char> ShortcutList;
+  bool CompleteFlag = false;
+  while (!CompleteFlag) {
+    CompleteFlag = true;
+    for(MenuDescription::const_iterator iter = menudescriptions.getBeginIter(); iter != menudescriptions.getEndIter(); ++iter) {
+      // skip when already present
+      if (NametoTextMenuMap.find(iter->first) == NametoTextMenuMap.end()) {
+        // have some short refs to infos
+        const std::string &MenuName = iter->first;
+        const std::string &TopName = iter->second.first;
+        const int &MenuPosition = iter->second.second;
+        std::cout << "MenuName is " << MenuName << ", TopName is " << TopName << " and Position is " << MenuPosition << std::endl;
+
+        // is it top level?
+        if (TopName == "") {
+          ASSERT(TopPositions.find(TopName) != TopPositions.end(),
+              "QtMainWindow::QtMainWindow() - "+TopName+" not present in TopPositions.");
+          if (MenuPosition-1 == TopPositions[TopName]) {
+            std::cout << "Creating top-level menu " << MenuName << " at position " << MenuPosition << std::endl;
+            Menu = new QtMenu(getNameWithAccelerator(ShortcutMap, TopName, MenuName).c_str());
+            MenuBar->addMenu(Menu);
+            NametoTextMenuMap.insert( pair <std::string, QtMenu *> (MenuName, Menu) );
+            CompleteFlag = false;
+            TopPositions[TopName] = MenuPosition;
+            TopPositions[MenuName] = 0;
+          }
+        }
+        // is it a submenu and the top-level menu is present?
+        else if (NametoTextMenuMap.find(TopName) != NametoTextMenuMap.end()) {
+          ASSERT(TopPositions.find(TopName) != TopPositions.end(),
+              "QtMainWindow::QtMainWindow() - "+TopName+" not present in TopPositions.");
+          if (MenuPosition-1 == TopPositions[TopName]) {
+            QtMenu *& TopMenu = NametoTextMenuMap[TopName];
+            std::cout << "Creating submenu " << MenuName << " to menu " << TopName << " at position " << MenuPosition << std::endl;
+            Menu = new QtMenu(getNameWithAccelerator(ShortcutMap, TopName, MenuName).c_str());
+            TopMenu->addMenu(Menu);
+            NametoTextMenuMap.insert( pair <std::string, QtMenu *> (MenuName, Menu) );
+            CompleteFlag = false;
+            TopPositions[TopName] = MenuPosition;
+          }
+        }
+      }
+    }
+  }
+}
+
+/** Instantiate all actions within the menus.
+ * \param NametoTextMenuMap lookup for token to menu reference
+ * \param ShortcutMap map for all shortcuts within one menu
+ */
+void QtMainWindow::populateMenuWithActions(QtMenuReferenceMap &NametoTextMenuMap, MenuShortcutMap &ShortcutMap)
+{
+  typedef std::multimap <std::string, std::string> MenuMap;
+
+  // go through all menus
+  MenuDescription md;
+  MenuMap MenuItems = md.getMenuItemsMap();
+  QtMenu *Menu = NULL;
   Action *ActionItem = NULL;
-  set <char> ShortcutList;
-  // through all actions for this menu
-  MenuDescription md;
-  std::multimap <std::string, std::string> MenuItems = md.getMenuItemsMap();
-  std::pair < std::multimap <std::string, std::string>::iterator, std::multimap <std::string, std::string>::iterator > MenuActions = MenuItems.equal_range(MenuName);
-  for (std::multimap <std::string, std::string>::const_iterator MenuRunner = MenuActions.first; MenuRunner != MenuActions.second; ++MenuRunner) {
-    cout << " Adding " << MenuRunner->second << " to submenu " << MenuName << endl;
-    ActionItem = ActionRegistry::getInstance().getActionByName(MenuRunner->second);
-    new ActionMenuItem(getSuitableShortForm(ShortcutList, MenuRunner->second),ActionItem->Traits.getDescription().c_str(),Menu,ActionItem);
-  }
-  // finally add default quit item
-  //Action *returnFromAction = new TextMenu::LeaveAction(Menu);
-  //MenuItem *returnFromItem = new ActionMenuItem('q',"return to Main menu",Menu,returnFromAction);
-  //Menu->addDefault(returnFromItem);
-}
+  std::string OldMenuName;
+  for (MenuMap::const_iterator MenuRunner = MenuItems.begin(); MenuRunner != MenuItems.end(); ++MenuRunner) {
+    const std::string &ActionName = MenuRunner->second;
+    const std::string &MenuName = MenuRunner->first;
+    // add the actions to this menu
+    std::cout << " Adding " << ActionName << " to submenu " << MenuName << std::endl;
+    ActionItem = ActionRegistry::getInstance().getActionByName(ActionName);
+    ASSERT(NametoTextMenuMap.find(MenuName) != NametoTextMenuMap.end(),
+        "QtMainWindow::populateMenu() - cannot find reference for menu "+MenuName+" in NametoTextMenuMap.");
+    Menu = NametoTextMenuMap[MenuName];
+    //ASSERT(ShortcutMap.find(MenuName) != ShortcutMap.end(),
+      //  "QtMainWindow::populateMenuWithActions() - missing "+MenuName+" in ShortcutMap.");
+    new ActionMenuItem(
+        'a',
+        ActionItem->Traits.getDescription().c_str(),
+        Menu,
+        ActionItem);
+  }
+}
