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