| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /*
|
|---|
| 9 | * Menu.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Dec 10, 2009
|
|---|
| 12 | * Author: crueger
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include <iostream>
|
|---|
| 23 |
|
|---|
| 24 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 25 | #include "Actions/Action.hpp"
|
|---|
| 26 | #include "Actions/ActionTrait.hpp"
|
|---|
| 27 | #include "Menu/Menu.hpp"
|
|---|
| 28 |
|
|---|
| 29 | using namespace MoleCuilder;
|
|---|
| 30 |
|
|---|
| 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()
|
|---|
| 45 | {
|
|---|
| 46 | DuplicatesList.clear();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /** Initialiser for class Menu.
|
|---|
| 50 | * Fills menus with items.
|
|---|
| 51 | */
|
|---|
| 52 | void Menu::init()
|
|---|
| 53 | {
|
|---|
| 54 | populate();
|
|---|
| 55 | populateActions();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 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;
|
|---|
| 71 | for(MenuDescription::const_iterator iter = MenuDescription::getInstance().getBeginIter();
|
|---|
| 72 | iter != MenuDescription::getInstance().getEndIter();
|
|---|
| 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;
|
|---|
| 80 | // std::cout << "MenuName is " << MenuName
|
|---|
| 81 | // << ", TopName is " << TopName
|
|---|
| 82 | // << " and Position is " << MenuPosition
|
|---|
| 83 | // << std::endl;
|
|---|
| 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 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 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 belonging 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 | {
|
|---|
| 129 | // std::cout << "Creating separator at position " << TopPosition << std::endl;
|
|---|
| 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 | {
|
|---|
| 139 | // std::cout << "Creating top-level menu " << MenuName
|
|---|
| 140 | // << " at position " << TopPosition << std::endl;
|
|---|
| 141 | ASSERT (!isPresent(MenuName),
|
|---|
| 142 | "Menu::addSubmenu() - trying to add menu "+MenuName+" with already present token!");
|
|---|
| 143 | addSubmenuItem(MenuName, MenuDescription::getInstance().getDescription(MenuName));
|
|---|
| 144 | DuplicatesList.insert(MenuName);
|
|---|
| 145 | LastItem = MenuItem;
|
|---|
| 146 | TopPosition = MenuPosition;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | bool Menu::isPresent(const std::string &token)
|
|---|
| 150 | {
|
|---|
| 151 | return (DuplicatesList.find(token) != DuplicatesList.end());
|
|---|
| 152 | }
|
|---|