Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UIElements/Menu/Menu.cpp

    rbcf653 rb59da6  
    2020#include "Helpers/MemDebug.hpp"
    2121
    22 #include "Menu.hpp"
     22#include <iostream>
    2323
    24 Menu::Menu()
     24#include "Actions/ActionRegistry.hpp"
     25#include "Actions/Action.hpp"
     26#include "Actions/ActionTraits.hpp"
     27#include "Menu/Menu.hpp"
     28
     29/** Constructor of class Menu.
     30 * \param &_name name of menu
     31 */
     32Menu::Menu(const std::string &_name) :
     33  MenuInterface(_name),
     34  name(_name),
     35  TopPosition(0),
     36  LastItem(NoItem)
     37{}
     38
     39/** Destructor of class Menu.
     40 *
     41 */
     42Menu::~Menu()
     43{}
     44
     45/** Initialiser for class Menu.
     46 * Fills menus with items.
     47 */
     48void Menu::init()
    2549{
    26   // TODO Auto-generated constructor stub
    27 
     50  populate();
     51  populateActions();
    2852}
    2953
    30 Menu::~Menu()
     54/** Initializing function.
     55 * Inserts Menus and Actions obtained from MenuDescription and
     56 * ActionRegistry.
     57 */
     58void Menu::populate()
    3159{
    32   // TODO Auto-generated destructor stub
     60  // go through all menus and create them
     61
     62  bool CompleteFlag = false;  // indicates whether all menus have been added
     63  bool PossibleMissingFlag = false; // indicates whether a separator is missing
     64  while (!CompleteFlag) {
     65    CompleteFlag = true;
     66    PossibleMissingFlag = false;
     67    for(MenuDescription::const_iterator iter = menudescriptions.getBeginIter();
     68        iter != menudescriptions.getEndIter();
     69        ++iter) {
     70      // skip when already present
     71      if (!isPresent(iter->first)) {
     72        // have some short refs to infos
     73        const std::string &MenuName = iter->first;
     74        const std::string &TopName = iter->second.first;
     75        const int &MenuPosition = iter->second.second;
     76        std::cout << "MenuName is " << MenuName
     77            << ", TopName is " << TopName
     78            << " and Position is " << MenuPosition
     79            << std::endl;
     80
     81        // does it belong to us?
     82        if (TopName == name) {
     83          if (MenuPosition-1 == TopPosition) {
     84            Menu::addSubmenu(MenuName, MenuPosition);
     85            CompleteFlag = false;
     86          }
     87          // is there a menuposition specified that we have not reached yet?
     88          if (MenuPosition-1 > TopPosition)
     89            PossibleMissingFlag = true;
     90        }
     91      }
     92    }
     93    if (PossibleMissingFlag && (CompleteFlag)) {
     94      Menu::addSeparator();
     95      CompleteFlag = false; // pass once more as there should be a menu to come
     96    }
     97  }
    3398}
     99
     100/** Fills this menu with all Actions that belong to it.
     101 */
     102void Menu::populateActions()
     103{
     104  // go through all actions and add those beloning to this menu
     105  ActionRegistry &AR = ActionRegistry::getInstance();
     106  for (ActionRegistry::const_iterator iter = AR.getBeginIter();
     107      iter != AR.getEndIter();
     108      ++iter) {
     109    const std::string &MenuName = iter->second->Traits.getMenuName();
     110    if (MenuName == name) {
     111      const std::string &ActionName = iter->first;
     112      Menu::addAction(ActionName);
     113    }
     114  }
     115}
     116
     117void Menu::addAction(const std::string &ActionName)
     118{
     119  LastItem = ActionItem;
     120  addActionItem(ActionName, ActionName);
     121}
     122
     123void Menu::addSeparator()
     124{
     125  std::cout << "Creating separator at position " << TopPosition << std::endl;
     126  ASSERT( LastItem != SeparatorItem,
     127      "Menu::populate() - adding another separator after a separator!");
     128  LastItem = SeparatorItem;
     129  addSeparatorItem();
     130  TopPosition++;
     131}
     132
     133void Menu::addSubmenu(const std::string &MenuName, const int MenuPosition)
     134{
     135  std::cout << "Creating top-level menu " << MenuName
     136      << " at position " << TopPosition << std::endl;
     137  ASSERT (!isPresent(MenuName),
     138      "Menu::addSubmenu() - trying to add menu "+MenuName+" with already present token!");
     139  addSubmenuItem(MenuName, menudescriptions.getDescription(MenuName));
     140  DuplicatesList.insert(MenuName);
     141  LastItem = MenuItem;
     142  TopPosition = MenuPosition;
     143}
     144
     145bool Menu::isPresent(const std::string &token)
     146{
     147  return (DuplicatesList.find(token) != DuplicatesList.end());
     148}
Note: See TracChangeset for help on using the changeset viewer.