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