| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [65b6e0] | 8 | /*
 | 
|---|
 | 9 |  * Menu.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Dec 10, 2009
 | 
|---|
 | 12 |  *      Author: crueger
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 21 | 
 | 
|---|
| [b59da6] | 22 | #include <iostream>
 | 
|---|
| [65b6e0] | 23 | 
 | 
|---|
| [b59da6] | 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 |  */
 | 
|---|
 | 32 | Menu::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 |  */
 | 
|---|
 | 42 | Menu::~Menu()
 | 
|---|
| [c82d0c] | 43 | {
 | 
|---|
 | 44 |   DuplicatesList.clear();
 | 
|---|
 | 45 | }
 | 
|---|
| [b59da6] | 46 | 
 | 
|---|
 | 47 | /** Initialiser for class Menu.
 | 
|---|
 | 48 |  * Fills menus with items.
 | 
|---|
 | 49 |  */
 | 
|---|
 | 50 | void Menu::init()
 | 
|---|
| [65b6e0] | 51 | {
 | 
|---|
| [b59da6] | 52 |   populate();
 | 
|---|
 | 53 |   populateActions();
 | 
|---|
 | 54 | }
 | 
|---|
| [65b6e0] | 55 | 
 | 
|---|
| [b59da6] | 56 | /** Initializing function.
 | 
|---|
 | 57 |  * Inserts Menus and Actions obtained from MenuDescription and
 | 
|---|
 | 58 |  * ActionRegistry.
 | 
|---|
 | 59 |  */
 | 
|---|
 | 60 | void Menu::populate()
 | 
|---|
 | 61 | {
 | 
|---|
 | 62 |   // go through all menus and create them
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |   bool CompleteFlag = false;  // indicates whether all menus have been added
 | 
|---|
 | 65 |   bool PossibleMissingFlag = false; // indicates whether a separator is missing
 | 
|---|
 | 66 |   while (!CompleteFlag) {
 | 
|---|
 | 67 |     CompleteFlag = true;
 | 
|---|
 | 68 |     PossibleMissingFlag = false;
 | 
|---|
| [c82d0c] | 69 |     for(MenuDescription::const_iterator iter = MenuDescription::getInstance().getBeginIter();
 | 
|---|
 | 70 |         iter != MenuDescription::getInstance().getEndIter();
 | 
|---|
| [b59da6] | 71 |         ++iter) {
 | 
|---|
 | 72 |       // skip when already present
 | 
|---|
 | 73 |       if (!isPresent(iter->first)) {
 | 
|---|
 | 74 |         // have some short refs to infos
 | 
|---|
 | 75 |         const std::string &MenuName = iter->first;
 | 
|---|
 | 76 |         const std::string &TopName = iter->second.first;
 | 
|---|
 | 77 |         const int &MenuPosition = iter->second.second;
 | 
|---|
| [ad7270] | 78 | //        std::cout << "MenuName is " << MenuName
 | 
|---|
 | 79 | //            << ", TopName is " << TopName
 | 
|---|
 | 80 | //            << " and Position is " << MenuPosition
 | 
|---|
 | 81 | //            << std::endl;
 | 
|---|
| [b59da6] | 82 | 
 | 
|---|
 | 83 |         // does it belong to us?
 | 
|---|
 | 84 |         if (TopName == name) {
 | 
|---|
 | 85 |           if (MenuPosition-1 == TopPosition) {
 | 
|---|
 | 86 |             Menu::addSubmenu(MenuName, MenuPosition);
 | 
|---|
 | 87 |             CompleteFlag = false;
 | 
|---|
 | 88 |           }
 | 
|---|
 | 89 |           // is there a menuposition specified that we have not reached yet?
 | 
|---|
 | 90 |           if (MenuPosition-1 > TopPosition)
 | 
|---|
 | 91 |             PossibleMissingFlag = true;
 | 
|---|
 | 92 |         }
 | 
|---|
 | 93 |       }
 | 
|---|
 | 94 |     }
 | 
|---|
 | 95 |     if (PossibleMissingFlag && (CompleteFlag)) {
 | 
|---|
 | 96 |       Menu::addSeparator();
 | 
|---|
 | 97 |       CompleteFlag = false; // pass once more as there should be a menu to come
 | 
|---|
 | 98 |     }
 | 
|---|
 | 99 |   }
 | 
|---|
| [65b6e0] | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
| [b59da6] | 102 | /** Fills this menu with all Actions that belong to it.
 | 
|---|
 | 103 |  */
 | 
|---|
 | 104 | void Menu::populateActions()
 | 
|---|
 | 105 | {
 | 
|---|
 | 106 |   // go through all actions and add those beloning to this menu
 | 
|---|
 | 107 |   ActionRegistry &AR = ActionRegistry::getInstance();
 | 
|---|
 | 108 |   for (ActionRegistry::const_iterator iter = AR.getBeginIter();
 | 
|---|
 | 109 |       iter != AR.getEndIter();
 | 
|---|
 | 110 |       ++iter) {
 | 
|---|
 | 111 |     const std::string &MenuName = iter->second->Traits.getMenuName();
 | 
|---|
 | 112 |     if (MenuName == name) {
 | 
|---|
 | 113 |       const std::string &ActionName = iter->first;
 | 
|---|
 | 114 |       Menu::addAction(ActionName);
 | 
|---|
 | 115 |     }
 | 
|---|
 | 116 |   }
 | 
|---|
 | 117 | }
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | void Menu::addAction(const std::string &ActionName)
 | 
|---|
 | 120 | {
 | 
|---|
 | 121 |   LastItem = ActionItem;
 | 
|---|
 | 122 |   addActionItem(ActionName, ActionName);
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | void Menu::addSeparator()
 | 
|---|
 | 126 | {
 | 
|---|
| [ad7270] | 127 | //  std::cout << "Creating separator at position " << TopPosition << std::endl;
 | 
|---|
| [b59da6] | 128 |   ASSERT( LastItem != SeparatorItem,
 | 
|---|
 | 129 |       "Menu::populate() - adding another separator after a separator!");
 | 
|---|
 | 130 |   LastItem = SeparatorItem;
 | 
|---|
 | 131 |   addSeparatorItem();
 | 
|---|
 | 132 |   TopPosition++;
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | void Menu::addSubmenu(const std::string &MenuName, const int MenuPosition)
 | 
|---|
 | 136 | {
 | 
|---|
| [ad7270] | 137 | //  std::cout << "Creating top-level menu " << MenuName
 | 
|---|
 | 138 | //      << " at position " << TopPosition << std::endl;
 | 
|---|
| [b59da6] | 139 |   ASSERT (!isPresent(MenuName),
 | 
|---|
 | 140 |       "Menu::addSubmenu() - trying to add menu "+MenuName+" with already present token!");
 | 
|---|
| [c82d0c] | 141 |   addSubmenuItem(MenuName, MenuDescription::getInstance().getDescription(MenuName));
 | 
|---|
| [b59da6] | 142 |   DuplicatesList.insert(MenuName);
 | 
|---|
 | 143 |   LastItem = MenuItem;
 | 
|---|
 | 144 |   TopPosition = MenuPosition;
 | 
|---|
 | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | bool Menu::isPresent(const std::string &token)
 | 
|---|
| [65b6e0] | 148 | {
 | 
|---|
| [b59da6] | 149 |   return (DuplicatesList.find(token) != DuplicatesList.end());
 | 
|---|
| [65b6e0] | 150 | }
 | 
|---|