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 |
|
---|
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 "Helpers/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <iostream>
|
---|
23 |
|
---|
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()
|
---|
43 | {}
|
---|
44 |
|
---|
45 | /** Initialiser for class Menu.
|
---|
46 | * Fills menus with items.
|
---|
47 | */
|
---|
48 | void Menu::init()
|
---|
49 | {
|
---|
50 | populate();
|
---|
51 | populateActions();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** Initializing function.
|
---|
55 | * Inserts Menus and Actions obtained from MenuDescription and
|
---|
56 | * ActionRegistry.
|
---|
57 | */
|
---|
58 | void Menu::populate()
|
---|
59 | {
|
---|
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 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Fills this menu with all Actions that belong to it.
|
---|
101 | */
|
---|
102 | void 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 |
|
---|
117 | void Menu::addAction(const std::string &ActionName)
|
---|
118 | {
|
---|
119 | LastItem = ActionItem;
|
---|
120 | addActionItem(ActionName, ActionName);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void 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 |
|
---|
133 | void 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 |
|
---|
145 | bool Menu::isPresent(const std::string &token)
|
---|
146 | {
|
---|
147 | return (DuplicatesList.find(token) != DuplicatesList.end());
|
---|
148 | }
|
---|