| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 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"
 | 
|---|
| [3139b2] | 26 | #include "Actions/ActionTrait.hpp"
 | 
|---|
| [b59da6] | 27 | #include "Menu/Menu.hpp"
 | 
|---|
 | 28 | 
 | 
|---|
| [ce7fdc] | 29 | using namespace MoleCuilder;
 | 
|---|
 | 30 | 
 | 
|---|
| [b59da6] | 31 | /** Constructor of class Menu.
 | 
|---|
 | 32 |  * \param &_name name of menu
 | 
|---|
 | 33 |  */
 | 
|---|
 | 34 | Menu::Menu(const std::string &_name) :
 | 
|---|
 | 35 |   MenuInterface(_name),
 | 
|---|
 | 36 |   name(_name),
 | 
|---|
 | 37 |   TopPosition(0),
 | 
|---|
 | 38 |   LastItem(NoItem)
 | 
|---|
 | 39 | {}
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | /** Destructor of class Menu.
 | 
|---|
 | 42 |  *
 | 
|---|
 | 43 |  */
 | 
|---|
 | 44 | Menu::~Menu()
 | 
|---|
| [c82d0c] | 45 | {
 | 
|---|
 | 46 |   DuplicatesList.clear();
 | 
|---|
 | 47 | }
 | 
|---|
| [b59da6] | 48 | 
 | 
|---|
 | 49 | /** Initialiser for class Menu.
 | 
|---|
 | 50 |  * Fills menus with items.
 | 
|---|
 | 51 |  */
 | 
|---|
 | 52 | void Menu::init()
 | 
|---|
| [65b6e0] | 53 | {
 | 
|---|
| [b59da6] | 54 |   populate();
 | 
|---|
 | 55 |   populateActions();
 | 
|---|
 | 56 | }
 | 
|---|
| [65b6e0] | 57 | 
 | 
|---|
| [b59da6] | 58 | /** Initializing function.
 | 
|---|
 | 59 |  * Inserts Menus and Actions obtained from MenuDescription and
 | 
|---|
 | 60 |  * ActionRegistry.
 | 
|---|
 | 61 |  */
 | 
|---|
 | 62 | void Menu::populate()
 | 
|---|
 | 63 | {
 | 
|---|
 | 64 |   // go through all menus and create them
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |   bool CompleteFlag = false;  // indicates whether all menus have been added
 | 
|---|
 | 67 |   bool PossibleMissingFlag = false; // indicates whether a separator is missing
 | 
|---|
 | 68 |   while (!CompleteFlag) {
 | 
|---|
 | 69 |     CompleteFlag = true;
 | 
|---|
 | 70 |     PossibleMissingFlag = false;
 | 
|---|
| [c82d0c] | 71 |     for(MenuDescription::const_iterator iter = MenuDescription::getInstance().getBeginIter();
 | 
|---|
 | 72 |         iter != MenuDescription::getInstance().getEndIter();
 | 
|---|
| [b59da6] | 73 |         ++iter) {
 | 
|---|
 | 74 |       // skip when already present
 | 
|---|
 | 75 |       if (!isPresent(iter->first)) {
 | 
|---|
 | 76 |         // have some short refs to infos
 | 
|---|
 | 77 |         const std::string &MenuName = iter->first;
 | 
|---|
 | 78 |         const std::string &TopName = iter->second.first;
 | 
|---|
 | 79 |         const int &MenuPosition = iter->second.second;
 | 
|---|
| [ad7270] | 80 | //        std::cout << "MenuName is " << MenuName
 | 
|---|
 | 81 | //            << ", TopName is " << TopName
 | 
|---|
 | 82 | //            << " and Position is " << MenuPosition
 | 
|---|
 | 83 | //            << std::endl;
 | 
|---|
| [b59da6] | 84 | 
 | 
|---|
 | 85 |         // does it belong to us?
 | 
|---|
 | 86 |         if (TopName == name) {
 | 
|---|
 | 87 |           if (MenuPosition-1 == TopPosition) {
 | 
|---|
 | 88 |             Menu::addSubmenu(MenuName, MenuPosition);
 | 
|---|
 | 89 |             CompleteFlag = false;
 | 
|---|
 | 90 |           }
 | 
|---|
 | 91 |           // is there a menuposition specified that we have not reached yet?
 | 
|---|
 | 92 |           if (MenuPosition-1 > TopPosition)
 | 
|---|
 | 93 |             PossibleMissingFlag = true;
 | 
|---|
 | 94 |         }
 | 
|---|
 | 95 |       }
 | 
|---|
 | 96 |     }
 | 
|---|
 | 97 |     if (PossibleMissingFlag && (CompleteFlag)) {
 | 
|---|
 | 98 |       Menu::addSeparator();
 | 
|---|
 | 99 |       CompleteFlag = false; // pass once more as there should be a menu to come
 | 
|---|
 | 100 |     }
 | 
|---|
 | 101 |   }
 | 
|---|
| [65b6e0] | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
| [b59da6] | 104 | /** Fills this menu with all Actions that belong to it.
 | 
|---|
 | 105 |  */
 | 
|---|
 | 106 | void Menu::populateActions()
 | 
|---|
 | 107 | {
 | 
|---|
 | 108 |   // go through all actions and add those beloning to this menu
 | 
|---|
 | 109 |   ActionRegistry &AR = ActionRegistry::getInstance();
 | 
|---|
 | 110 |   for (ActionRegistry::const_iterator iter = AR.getBeginIter();
 | 
|---|
 | 111 |       iter != AR.getEndIter();
 | 
|---|
 | 112 |       ++iter) {
 | 
|---|
 | 113 |     const std::string &MenuName = iter->second->Traits.getMenuName();
 | 
|---|
 | 114 |     if (MenuName == name) {
 | 
|---|
 | 115 |       const std::string &ActionName = iter->first;
 | 
|---|
 | 116 |       Menu::addAction(ActionName);
 | 
|---|
 | 117 |     }
 | 
|---|
 | 118 |   }
 | 
|---|
 | 119 | }
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | void Menu::addAction(const std::string &ActionName)
 | 
|---|
 | 122 | {
 | 
|---|
 | 123 |   LastItem = ActionItem;
 | 
|---|
 | 124 |   addActionItem(ActionName, ActionName);
 | 
|---|
 | 125 | }
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | void Menu::addSeparator()
 | 
|---|
 | 128 | {
 | 
|---|
| [ad7270] | 129 | //  std::cout << "Creating separator at position " << TopPosition << std::endl;
 | 
|---|
| [b59da6] | 130 |   ASSERT( LastItem != SeparatorItem,
 | 
|---|
 | 131 |       "Menu::populate() - adding another separator after a separator!");
 | 
|---|
 | 132 |   LastItem = SeparatorItem;
 | 
|---|
 | 133 |   addSeparatorItem();
 | 
|---|
 | 134 |   TopPosition++;
 | 
|---|
 | 135 | }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 | void Menu::addSubmenu(const std::string &MenuName, const int MenuPosition)
 | 
|---|
 | 138 | {
 | 
|---|
| [ad7270] | 139 | //  std::cout << "Creating top-level menu " << MenuName
 | 
|---|
 | 140 | //      << " at position " << TopPosition << std::endl;
 | 
|---|
| [b59da6] | 141 |   ASSERT (!isPresent(MenuName),
 | 
|---|
 | 142 |       "Menu::addSubmenu() - trying to add menu "+MenuName+" with already present token!");
 | 
|---|
| [c82d0c] | 143 |   addSubmenuItem(MenuName, MenuDescription::getInstance().getDescription(MenuName));
 | 
|---|
| [b59da6] | 144 |   DuplicatesList.insert(MenuName);
 | 
|---|
 | 145 |   LastItem = MenuItem;
 | 
|---|
 | 146 |   TopPosition = MenuPosition;
 | 
|---|
 | 147 | }
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 | bool Menu::isPresent(const std::string &token)
 | 
|---|
| [65b6e0] | 150 | {
 | 
|---|
| [b59da6] | 151 |   return (DuplicatesList.find(token) != DuplicatesList.end());
 | 
|---|
| [65b6e0] | 152 | }
 | 
|---|