Ignore:
Timestamp:
Nov 9, 2010, 3:38:03 PM (14 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
5813ac
Parents:
e262c7
git-author:
Frederik Heber <heber@…> (11/01/10 20:29:58)
git-committer:
Frederik Heber <heber@…> (11/09/10 15:38:03)
Message:

FIX: Menu entries are again correctly added to QtGui part.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UIElements/Qt4/QtMainWindow.cpp

    re262c7 rffb9ad  
    4343#include "Menu/Qt4/QtMenu.hpp"
    4444#include "Menu/ActionMenuItem.hpp"
     45#include "Menu/SubMenuItem.hpp"
    4546#include "Views/Qt4/QtWorldView.hpp"
    4647#include "Views/Qt4/GLMoleculeView.hpp"
     
    4849#include "Views/Qt4/QtStatusBar.hpp"
    4950
    50 using namespace std;
    51 
    5251QtMainWindow::QtMainWindow(QApplication *_theApp) :
    5352    theApp(_theApp)
     
    6362  MenuBar = menuBar();
    6463
    65   std::map <std::string, QtMenu *> NametoTextMenuMap;
    66   // go through all menus and create them
    67   QtMenu *Menu = NULL;
    68   MenuDescription menudescriptions;
    69   for(MenuDescription::const_iterator iter = menudescriptions.getBeginIter(); iter != menudescriptions.getEndIter(); ++iter) {
    70     cout << "Creating menu " << *iter << endl;
    71     Menu = new QtMenu(menudescriptions.getName(*iter).c_str());
    72     MenuBar->addMenu(Menu);
    73     NametoTextMenuMap.insert( pair <std::string, QtMenu *> (menudescriptions.getName(*iter), Menu) );
    74     //new SubMenuItem(getSuitableShortForm(menudescriptions.getName(iter->first.c_str())),menudescriptions.getName(iter->first.c_str()),main_menu,Menu);
    75   }
    76 
    77   // go through all actions and add them to its menu
    78   for (std::map <std::string, QtMenu *>::iterator MenuRunner = NametoTextMenuMap.begin(); MenuRunner != NametoTextMenuMap.end(); ++MenuRunner) {
    79     cout << "Creating Action " << MenuRunner->first << " in menu " << MenuRunner->second << endl;
    80     populateMenu(MenuRunner->second, MenuRunner->first);
     64  // populate menus and add actions
     65  {
     66    QtMenuReferenceMap NametoTextMenuMap; // contains token to menu reference map
     67    MenuShortcutMap ShortcutMap;
     68    populateMenu(NametoTextMenuMap, ShortcutMap);
     69    populateMenuWithActions(NametoTextMenuMap, ShortcutMap);
    8170  }
    8271
     
    9786{
    9887  menuBar()->clear();
    99   delete editMoleculesMenu;
    10088}
    10189
     
    10593}
    10694
    107 char QtMainWindow::getSuitableShortForm(set <char> &ShortcutList, const std::string name) const
    108 {
    109   for (std::string::const_iterator CharRunner = name.begin(); CharRunner != name.end(); ++CharRunner) {
    110     if (ShortcutList.find(*CharRunner) == ShortcutList.end())
    111       return *CharRunner;
    112   }
    113   DoeLog(1) && (eLog() << Verbose(1) << "Could not find a suitable shortform for TextWindow::getSuitableShortForm()." << endl);
    114   return ((char)(ShortcutList.size() % 10) + '0');
    115 }
    116 
    117 void QtMainWindow::populateMenu(QtMenu* Menu, const  std::string &MenuName)
    118 {
     95/** Puts Qt's token, the ampersand, in front of the accelerator char in the menu name.
     96 * \param ShortcutMap map to all already present accelerator keys
     97 * \param MenuName Action of menu
     98 * \param ActionName Action of menu
     99 * \return name with ampersand added at the right place
     100 */
     101std::string QtMainWindow::getNameWithAccelerator(MenuShortcutMap &ShortcutMap, const std::string &MenuName, const std::string &ActionName) const
     102{
     103  std::string newname;
     104  bool Inserted = false;
     105  std::pair < MenuShortcutMap::iterator, bool > Inserter;
     106  for (std::string::const_iterator CharRunner = ActionName.begin(); CharRunner != ActionName.end(); ++CharRunner) {
     107    if (!Inserted) {
     108      Inserter = ShortcutMap.insert( std::pair<std::string, char> (MenuName, *CharRunner) );
     109      if (Inserter.second) {
     110        newname += '&';
     111        Inserted = true;
     112      }
     113    }
     114    newname += *CharRunner;
     115  }
     116  return newname;
     117}
     118
     119/** Instantiate all menus.
     120 * \param NametoTextMenuMap lookup for token to menu reference
     121 */
     122void QtMainWindow::populateMenu(QtMenuReferenceMap &NametoTextMenuMap, MenuShortcutMap &ShortcutMap)
     123{
     124  // go through all menus and create them
     125  std::map <std::string, int> TopPositions;
     126  TopPositions.insert( std::pair<std::string, int> ("", 0) ); // contains which position was added last
     127  QtMenu *Menu = NULL;
     128  MenuDescription menudescriptions;
     129  std::set <char> ShortcutList;
     130  bool CompleteFlag = false;
     131  while (!CompleteFlag) {
     132    CompleteFlag = true;
     133    for(MenuDescription::const_iterator iter = menudescriptions.getBeginIter(); iter != menudescriptions.getEndIter(); ++iter) {
     134      // skip when already present
     135      if (NametoTextMenuMap.find(iter->first) == NametoTextMenuMap.end()) {
     136        // have some short refs to infos
     137        const std::string &MenuName = iter->first;
     138        const std::string &TopName = iter->second.first;
     139        const int &MenuPosition = iter->second.second;
     140        std::cout << "MenuName is " << MenuName << ", TopName is " << TopName << " and Position is " << MenuPosition << std::endl;
     141
     142        // is it top level?
     143        if (TopName == "") {
     144          ASSERT(TopPositions.find(TopName) != TopPositions.end(),
     145              "QtMainWindow::QtMainWindow() - "+TopName+" not present in TopPositions.");
     146          if (MenuPosition-1 == TopPositions[TopName]) {
     147            std::cout << "Creating top-level menu " << MenuName << " at position " << MenuPosition << std::endl;
     148            Menu = new QtMenu(getNameWithAccelerator(ShortcutMap, TopName, MenuName).c_str());
     149            MenuBar->addMenu(Menu);
     150            NametoTextMenuMap.insert( pair <std::string, QtMenu *> (MenuName, Menu) );
     151            CompleteFlag = false;
     152            TopPositions[TopName] = MenuPosition;
     153            TopPositions[MenuName] = 0;
     154          }
     155        }
     156        // is it a submenu and the top-level menu is present?
     157        else if (NametoTextMenuMap.find(TopName) != NametoTextMenuMap.end()) {
     158          ASSERT(TopPositions.find(TopName) != TopPositions.end(),
     159              "QtMainWindow::QtMainWindow() - "+TopName+" not present in TopPositions.");
     160          if (MenuPosition-1 == TopPositions[TopName]) {
     161            QtMenu *& TopMenu = NametoTextMenuMap[TopName];
     162            std::cout << "Creating submenu " << MenuName << " to menu " << TopName << " at position " << MenuPosition << std::endl;
     163            Menu = new QtMenu(getNameWithAccelerator(ShortcutMap, TopName, MenuName).c_str());
     164            TopMenu->addMenu(Menu);
     165            NametoTextMenuMap.insert( pair <std::string, QtMenu *> (MenuName, Menu) );
     166            CompleteFlag = false;
     167            TopPositions[TopName] = MenuPosition;
     168          }
     169        }
     170      }
     171    }
     172  }
     173}
     174
     175/** Instantiate all actions within the menus.
     176 * \param NametoTextMenuMap lookup for token to menu reference
     177 * \param ShortcutMap map for all shortcuts within one menu
     178 */
     179void QtMainWindow::populateMenuWithActions(QtMenuReferenceMap &NametoTextMenuMap, MenuShortcutMap &ShortcutMap)
     180{
     181  typedef std::multimap <std::string, std::string> MenuMap;
     182
     183  // go through all menus
     184  MenuDescription md;
     185  MenuMap MenuItems = md.getMenuItemsMap();
     186  QtMenu *Menu = NULL;
    119187  Action *ActionItem = NULL;
    120   set <char> ShortcutList;
    121   // through all actions for this menu
    122   MenuDescription md;
    123   std::multimap <std::string, std::string> MenuItems = md.getMenuItemsMap();
    124   std::pair < std::multimap <std::string, std::string>::iterator, std::multimap <std::string, std::string>::iterator > MenuActions = MenuItems.equal_range(MenuName);
    125   for (std::multimap <std::string, std::string>::const_iterator MenuRunner = MenuActions.first; MenuRunner != MenuActions.second; ++MenuRunner) {
    126     cout << " Adding " << MenuRunner->second << " to submenu " << MenuName << endl;
    127     ActionItem = ActionRegistry::getInstance().getActionByName(MenuRunner->second);
    128     new ActionMenuItem(getSuitableShortForm(ShortcutList, MenuRunner->second),ActionItem->Traits.getDescription().c_str(),Menu,ActionItem);
    129   }
    130   // finally add default quit item
    131   //Action *returnFromAction = new TextMenu::LeaveAction(Menu);
    132   //MenuItem *returnFromItem = new ActionMenuItem('q',"return to Main menu",Menu,returnFromAction);
    133   //Menu->addDefault(returnFromItem);
    134 }
     188  std::string OldMenuName;
     189  for (MenuMap::const_iterator MenuRunner = MenuItems.begin(); MenuRunner != MenuItems.end(); ++MenuRunner) {
     190    const std::string &ActionName = MenuRunner->second;
     191    const std::string &MenuName = MenuRunner->first;
     192    // add the actions to this menu
     193    std::cout << " Adding " << ActionName << " to submenu " << MenuName << std::endl;
     194    ActionItem = ActionRegistry::getInstance().getActionByName(ActionName);
     195    ASSERT(NametoTextMenuMap.find(MenuName) != NametoTextMenuMap.end(),
     196        "QtMainWindow::populateMenu() - cannot find reference for menu "+MenuName+" in NametoTextMenuMap.");
     197    Menu = NametoTextMenuMap[MenuName];
     198    //ASSERT(ShortcutMap.find(MenuName) != ShortcutMap.end(),
     199      //  "QtMainWindow::populateMenuWithActions() - missing "+MenuName+" in ShortcutMap.");
     200    new ActionMenuItem(
     201        'a',
     202        ActionItem->Traits.getDescription().c_str(),
     203        Menu,
     204        ActionItem);
     205  }
     206}
Note: See TracChangeset for help on using the changeset viewer.