[a56275] | 1 | /*
|
---|
| 2 | * TextWindow.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jan 7, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[112b09] | 8 | #include "Helpers/MemDebug.hpp"
|
---|
| 9 |
|
---|
[12b845] | 10 | #include <boost/bind.hpp>
|
---|
| 11 |
|
---|
| 12 | #include "Menu/Menu.hpp"
|
---|
| 13 | #include "Menu/TextMenu.hpp"
|
---|
| 14 | #include "Menu/ActionMenuItem.hpp"
|
---|
| 15 | #include "Menu/SeperatorItem.hpp"
|
---|
| 16 | #include "Menu/DisplayMenuItem.hpp"
|
---|
| 17 | #include "Menu/SubMenuItem.hpp"
|
---|
[5079a0] | 18 | #include "TextUI/TextStatusIndicator.hpp"
|
---|
[11428f] | 19 | #include "TextUI/TextWindow.hpp"
|
---|
[326bbe] | 20 | #include "Actions/MapOfActions.hpp"
|
---|
[12b845] | 21 | #include "Actions/MethodAction.hpp"
|
---|
| 22 | #include "Actions/ErrorAction.hpp"
|
---|
[f6bbc6] | 23 | #include "Actions/ActionRegistry.hpp"
|
---|
[4a611e] | 24 | #include "Parser/ChangeTracker.hpp"
|
---|
[12b845] | 25 | #include "Views/StreamStringView.hpp"
|
---|
| 26 | #include "Views/MethodStringView.hpp"
|
---|
| 27 |
|
---|
[326bbe] | 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 |
|
---|
[cc04b7] | 39 | #include <iostream>
|
---|
[326bbe] | 40 | #include <map>
|
---|
[cc04b7] | 41 |
|
---|
[12b845] | 42 | // TODO: see what code can be moved to a base class for Graphic and Text Windows
|
---|
[d893f79] | 43 | TextWindow::TextWindow()
|
---|
[a56275] | 44 | {
|
---|
[326bbe] | 45 | map <std::string, TextMenu *> NametoTextMenuMap;
|
---|
| 46 |
|
---|
| 47 | // populate all actions
|
---|
| 48 | MapOfActions::getInstance().populateActions();
|
---|
[12b845] | 49 |
|
---|
| 50 | // build the main menu
|
---|
| 51 | main_menu = new TextMenu(Log() << Verbose(0), "Main Menu");
|
---|
| 52 |
|
---|
[326bbe] | 53 | moleculeView = new StreamStringView(boost::bind(&MoleculeListClass::Enumerate,World::getInstance().getMolecules(),_1));
|
---|
[12b845] | 54 | new DisplayMenuItem(main_menu,moleculeView,"Molecule List");
|
---|
| 55 |
|
---|
| 56 | new SeperatorItem(main_menu);
|
---|
| 57 |
|
---|
[f6bbc6] | 58 | Action* undoAction = ActionRegistry::getInstance().getActionByName("Undo");
|
---|
| 59 | new ActionMenuItem('u',"Undo last operation",main_menu,undoAction);
|
---|
| 60 |
|
---|
| 61 | Action* redoAction = ActionRegistry::getInstance().getActionByName("Redo");
|
---|
| 62 | new ActionMenuItem('r',"Redo last operation",main_menu,redoAction);
|
---|
| 63 |
|
---|
| 64 | new SeperatorItem(main_menu);
|
---|
| 65 |
|
---|
[326bbe] | 66 | Action *setMoleculeAction = new MethodAction("setMoleculeAction",boost::bind(&MoleculeListClass::flipChosen,World::getInstance().getMolecules()));
|
---|
[12b845] | 67 | new ActionMenuItem('a',"set molecule (in)active",main_menu,setMoleculeAction);
|
---|
| 68 |
|
---|
[b2531f] | 69 | TextMenu *Menu = NULL;
|
---|
| 70 | std::set <char> ShortcutList;
|
---|
| 71 | for(map<std::string, pair<std::string,std::string> >::iterator iter = MapOfActions::getInstance().MenuDescription.begin(); iter != MapOfActions::getInstance().MenuDescription.end(); ++iter) {
|
---|
| 72 | Menu = new TextMenu(Log() << Verbose(0), iter->second.second);
|
---|
| 73 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> (iter->first, Menu) );
|
---|
| 74 | new SubMenuItem(getSuitableShortForm(ShortcutList,iter->first),iter->second.first.c_str(),main_menu,Menu);
|
---|
| 75 | }
|
---|
[12b845] | 76 |
|
---|
| 77 | new SeperatorItem(main_menu);
|
---|
| 78 |
|
---|
[4a611e] | 79 | Action *saveConfigAction = ActionRegistry::getInstance().getActionByName("output");
|
---|
| 80 | new ActionMenuItem('s',"save current setup to config files",main_menu,saveConfigAction);
|
---|
[12b845] | 81 |
|
---|
| 82 | quitAction = new MethodAction("quitAction",boost::bind(&TextMenu::doQuit,main_menu),false);
|
---|
| 83 | new ActionMenuItem('q',"quit",main_menu,quitAction);
|
---|
| 84 |
|
---|
[326bbe] | 85 | // go through all menus and create them
|
---|
| 86 | for (map <std::string, TextMenu *>::iterator MenuRunner = NametoTextMenuMap.begin(); MenuRunner != NametoTextMenuMap.end(); ++MenuRunner) {
|
---|
| 87 | cout << "Creating Menu " << MenuRunner->first << "." << endl;
|
---|
| 88 | populateMenu(MenuRunner->second, MenuRunner->first);
|
---|
| 89 | }
|
---|
[0188ea] | 90 |
|
---|
| 91 | // Add status indicators etc...
|
---|
| 92 |
|
---|
| 93 | statusIndicator = new TextStatusIndicator();
|
---|
[a56275] | 94 | }
|
---|
| 95 |
|
---|
| 96 | TextWindow::~TextWindow()
|
---|
| 97 | {
|
---|
[12b845] | 98 | delete quitAction;
|
---|
| 99 | delete moleculeView;
|
---|
[0188ea] | 100 | delete statusIndicator;
|
---|
[12b845] | 101 | delete main_menu;
|
---|
[a56275] | 102 | }
|
---|
| 103 |
|
---|
[12b845] | 104 | void TextWindow::display() {
|
---|
| 105 | main_menu->display();
|
---|
[a56275] | 106 | }
|
---|
[d893f79] | 107 |
|
---|
[11428f] | 108 | char TextWindow::getSuitableShortForm(std::set <char> &ShortcutList, const std::string name) const
|
---|
[d893f79] | 109 | {
|
---|
[326bbe] | 110 | for (std::string::const_iterator CharRunner = name.begin(); CharRunner != name.end(); ++CharRunner) {
|
---|
| 111 | if (ShortcutList.find(*CharRunner) == ShortcutList.end())
|
---|
| 112 | return *CharRunner;
|
---|
| 113 | }
|
---|
| 114 | DoeLog(1) && (eLog() << Verbose(1) << "Could not find a suitable shortform for TextWindow::getSuitableShortForm()." << endl);
|
---|
| 115 | return ((char)(ShortcutList.size() % 10) + '0');
|
---|
| 116 | }
|
---|
[d893f79] | 117 |
|
---|
[326bbe] | 118 | void TextWindow::populateMenu(TextMenu* Menu, const std::string &MenuName)
|
---|
| 119 | {
|
---|
| 120 | Action *ActionItem = NULL;
|
---|
| 121 | set <char> ShortcutList;
|
---|
| 122 | // through all actions for this menu
|
---|
| 123 | pair < multimap <std::string, std::string>::iterator, multimap <std::string, std::string>::iterator > MenuActions = MapOfActions::getInstance().MenuContainsActionMap.equal_range(MenuName);
|
---|
| 124 | for (multimap <std::string, std::string>::const_iterator MenuRunner = MenuActions.first; MenuRunner != MenuActions.second; ++MenuRunner) {
|
---|
| 125 | cout << " Adding " << MenuRunner->second << " to submenu " << MenuName << endl;
|
---|
| 126 | ActionItem = ActionRegistry::getInstance().getActionByName(MenuRunner->second);
|
---|
| 127 | new ActionMenuItem(getSuitableShortForm(ShortcutList, MenuRunner->second),MapOfActions::getInstance().getDescription(MenuRunner->second).c_str(),Menu,ActionItem);
|
---|
| 128 | }
|
---|
| 129 | // finally add default quit item
|
---|
| 130 | Action *returnFromAction = new TextMenu::LeaveAction(Menu);
|
---|
| 131 | MenuItem *returnFromItem = new ActionMenuItem('q',"return to Main menu",Menu,returnFromAction);
|
---|
| 132 | Menu->addDefault(returnFromItem);
|
---|
[d893f79] | 133 | }
|
---|