1 | /*
|
---|
2 | * TextWindow.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 7, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "Helpers/MemDebug.hpp"
|
---|
9 |
|
---|
10 | #include "TextUI/TextWindow.hpp"
|
---|
11 |
|
---|
12 | #include <boost/bind.hpp>
|
---|
13 |
|
---|
14 | #include "Menu/Menu.hpp"
|
---|
15 | #include "Menu/TextMenu.hpp"
|
---|
16 | #include "Menu/ActionMenuItem.hpp"
|
---|
17 | #include "Menu/SeperatorItem.hpp"
|
---|
18 | #include "Menu/DisplayMenuItem.hpp"
|
---|
19 | #include "Menu/SubMenuItem.hpp"
|
---|
20 | #include "TextUI/TextStatusIndicator.hpp"
|
---|
21 | #include "Actions/MapOfActions.hpp"
|
---|
22 | #include "Actions/MethodAction.hpp"
|
---|
23 | #include "Actions/ErrorAction.hpp"
|
---|
24 | #include "Actions/ActionRegistry.hpp"
|
---|
25 | #include "Views/StreamStringView.hpp"
|
---|
26 | #include "Views/MethodStringView.hpp"
|
---|
27 |
|
---|
28 | #include "defs.hpp"
|
---|
29 | #include "log.hpp"
|
---|
30 | #include "verbose.hpp"
|
---|
31 |
|
---|
32 | // all needed due to config::SaveAll()
|
---|
33 | #include "config.hpp"
|
---|
34 | #include "periodentafel.hpp"
|
---|
35 |
|
---|
36 | // config::SaveAll() and enumerate()
|
---|
37 | #include "molecule.hpp"
|
---|
38 |
|
---|
39 | #include <iostream>
|
---|
40 | #include <map>
|
---|
41 |
|
---|
42 | using namespace std;
|
---|
43 |
|
---|
44 | // TODO: see what code can be moved to a base class for Graphic and Text Windows
|
---|
45 | TextWindow::TextWindow()
|
---|
46 | {
|
---|
47 | char ConfigFileName[MAXSTRINGSIZE];
|
---|
48 | map <std::string, TextMenu *> NametoTextMenuMap;
|
---|
49 |
|
---|
50 | // populate all actions
|
---|
51 | MapOfActions::getInstance().populateActions();
|
---|
52 |
|
---|
53 | // build the main menu
|
---|
54 | main_menu = new TextMenu(Log() << Verbose(0), "Main Menu");
|
---|
55 |
|
---|
56 | moleculeView = new StreamStringView(boost::bind(&MoleculeListClass::Enumerate,World::getInstance().getMolecules(),_1));
|
---|
57 | new DisplayMenuItem(main_menu,moleculeView,"Molecule List");
|
---|
58 |
|
---|
59 | new SeperatorItem(main_menu);
|
---|
60 |
|
---|
61 | Action* undoAction = ActionRegistry::getInstance().getActionByName("Undo");
|
---|
62 | new ActionMenuItem('u',"Undo last operation",main_menu,undoAction);
|
---|
63 |
|
---|
64 | Action* redoAction = ActionRegistry::getInstance().getActionByName("Redo");
|
---|
65 | new ActionMenuItem('r',"Redo last operation",main_menu,redoAction);
|
---|
66 |
|
---|
67 | new SeperatorItem(main_menu);
|
---|
68 |
|
---|
69 | Action *setMoleculeAction = new MethodAction("setMoleculeAction",boost::bind(&MoleculeListClass::flipChosen,World::getInstance().getMolecules()));
|
---|
70 | new ActionMenuItem('a',"set molecule (in)active",main_menu,setMoleculeAction);
|
---|
71 |
|
---|
72 | TextMenu *AnalysisMenu = new TextMenu(Log() << Verbose(0), "Analysis");
|
---|
73 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("analysis", AnalysisMenu) );
|
---|
74 | new SubMenuItem('A',"Analysis (pair correlation, volume)",main_menu,AnalysisMenu);
|
---|
75 |
|
---|
76 | TextMenu *CommandMenu = new TextMenu(Log() << Verbose(0), "Configuration");
|
---|
77 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("command", CommandMenu) );
|
---|
78 | new SubMenuItem('c',"configuration",main_menu,CommandMenu);
|
---|
79 |
|
---|
80 | TextMenu *AtomMenu = new TextMenu(Log() << Verbose(0), "Atoms");
|
---|
81 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("atom", AtomMenu) );
|
---|
82 | new SubMenuItem('e',"edit atoms",main_menu,AtomMenu);
|
---|
83 |
|
---|
84 | TextMenu *FragmentationMenu = new TextMenu(Log() << Verbose(0), "Fragmentation");
|
---|
85 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("fragmentation", FragmentationMenu) );
|
---|
86 | new SubMenuItem('f',"fragmentation",main_menu,FragmentationMenu);
|
---|
87 |
|
---|
88 | TextMenu *ParserMenu = new TextMenu(Log() << Verbose(0), "Parse files");
|
---|
89 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("parser", ParserMenu) );
|
---|
90 | new SubMenuItem('p',"parse files into system",main_menu,ParserMenu);
|
---|
91 |
|
---|
92 | TextMenu *MoleculesMenu = new TextMenu(Log() << Verbose(0), "Edit Molecules");
|
---|
93 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("analysis", AnalysisMenu) );
|
---|
94 | new SubMenuItem('m',"edit molecules (load, parse, save)",main_menu,MoleculesMenu);
|
---|
95 |
|
---|
96 | TextMenu *TesselationMenu = new TextMenu(Log() << Verbose(0), "Tesselate Molecules");
|
---|
97 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("tesselation", TesselationMenu) );
|
---|
98 | new SubMenuItem('t',"tesselate molecules",main_menu,TesselationMenu);
|
---|
99 |
|
---|
100 | TextMenu *WorldMenu = new TextMenu(Log() << Verbose(0), "World");
|
---|
101 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> ("world", WorldMenu) );
|
---|
102 | new SubMenuItem('w',"edit world",main_menu,WorldMenu);
|
---|
103 |
|
---|
104 | new SeperatorItem(main_menu);
|
---|
105 |
|
---|
106 | Action *saveConfigAction = new MethodAction("saveConfigAction",boost::bind(&config::SaveAll,World::getInstance().getConfig(), ConfigFileName, World::getInstance().getPeriode(), World::getInstance().getMolecules()));
|
---|
107 | new ActionMenuItem('s',"save current setup to config file",main_menu,saveConfigAction);
|
---|
108 |
|
---|
109 | quitAction = new MethodAction("quitAction",boost::bind(&TextMenu::doQuit,main_menu),false);
|
---|
110 | new ActionMenuItem('q',"quit",main_menu,quitAction);
|
---|
111 |
|
---|
112 | // go through all menus and create them
|
---|
113 | for (map <std::string, TextMenu *>::iterator MenuRunner = NametoTextMenuMap.begin(); MenuRunner != NametoTextMenuMap.end(); ++MenuRunner) {
|
---|
114 | cout << "Creating Menu " << MenuRunner->first << "." << endl;
|
---|
115 | populateMenu(MenuRunner->second, MenuRunner->first);
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Add status indicators etc...
|
---|
119 |
|
---|
120 | statusIndicator = new TextStatusIndicator();
|
---|
121 | }
|
---|
122 |
|
---|
123 | TextWindow::~TextWindow()
|
---|
124 | {
|
---|
125 | delete quitAction;
|
---|
126 | delete moleculeView;
|
---|
127 | delete statusIndicator;
|
---|
128 | delete main_menu;
|
---|
129 | }
|
---|
130 |
|
---|
131 | void TextWindow::display() {
|
---|
132 | main_menu->display();
|
---|
133 | }
|
---|
134 |
|
---|
135 | char TextWindow::getSuitableShortForm(set <char> &ShortcutList, const std::string name) const
|
---|
136 | {
|
---|
137 | for (std::string::const_iterator CharRunner = name.begin(); CharRunner != name.end(); ++CharRunner) {
|
---|
138 | if (ShortcutList.find(*CharRunner) == ShortcutList.end())
|
---|
139 | return *CharRunner;
|
---|
140 | }
|
---|
141 | DoeLog(1) && (eLog() << Verbose(1) << "Could not find a suitable shortform for TextWindow::getSuitableShortForm()." << endl);
|
---|
142 | return ((char)(ShortcutList.size() % 10) + '0');
|
---|
143 | }
|
---|
144 |
|
---|
145 | void TextWindow::populateMenu(TextMenu* Menu, const std::string &MenuName)
|
---|
146 | {
|
---|
147 | Action *ActionItem = NULL;
|
---|
148 | set <char> ShortcutList;
|
---|
149 | // through all actions for this menu
|
---|
150 | pair < multimap <std::string, std::string>::iterator, multimap <std::string, std::string>::iterator > MenuActions = MapOfActions::getInstance().MenuContainsActionMap.equal_range(MenuName);
|
---|
151 | for (multimap <std::string, std::string>::const_iterator MenuRunner = MenuActions.first; MenuRunner != MenuActions.second; ++MenuRunner) {
|
---|
152 | cout << " Adding " << MenuRunner->second << " to submenu " << MenuName << endl;
|
---|
153 | ActionItem = ActionRegistry::getInstance().getActionByName(MenuRunner->second);
|
---|
154 | new ActionMenuItem(getSuitableShortForm(ShortcutList, MenuRunner->second),MapOfActions::getInstance().getDescription(MenuRunner->second).c_str(),Menu,ActionItem);
|
---|
155 | }
|
---|
156 | // finally add default quit item
|
---|
157 | Action *returnFromAction = new TextMenu::LeaveAction(Menu);
|
---|
158 | MenuItem *returnFromItem = new ActionMenuItem('q',"return to Main menu",Menu,returnFromAction);
|
---|
159 | Menu->addDefault(returnFromItem);
|
---|
160 | }
|
---|