| [b59da6] | 1 | /* | 
|---|
|  | 2 | * TextMenu.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Nov 5, 2010 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef TEXTMENU_HPP_ | 
|---|
|  | 9 | #define TEXTMENU_HPP_ | 
|---|
|  | 10 |  | 
|---|
| [56f73b] | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 |  | 
|---|
| [b59da6] | 17 | #include <map> | 
|---|
|  | 18 | #include <string> | 
|---|
|  | 19 |  | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [670cd1] | 21 |  | 
|---|
| [ad011c] | 22 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 23 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [b59da6] | 24 | #include "Menu/Menu.hpp" | 
|---|
|  | 25 | #include "Menu/MenuInterface.hpp" | 
|---|
|  | 26 | #include "Actions/Action.hpp" | 
|---|
|  | 27 | #include "Actions/ActionRegistry.hpp" | 
|---|
| [3139b2] | 28 | #include "Actions/ActionTrait.hpp" | 
|---|
| [f0f9a6] | 29 | #include "Menu/TextMenu/TxMenuLeaveAction.hpp" | 
|---|
| [b59da6] | 30 | #include "Menu/TextMenu/ActionMenuItem.hpp" | 
|---|
| [e9be39] | 31 | #include "Menu/TextMenu/SeparatorMenuItem.hpp" | 
|---|
| [b59da6] | 32 | #include "Menu/TextMenu/SubMenuItem.hpp" | 
|---|
|  | 33 |  | 
|---|
|  | 34 | /** TextMenu is a specialization of MenuInterface to access TxMenu-like menus. | 
|---|
| [8f3f40] | 35 | * | 
|---|
|  | 36 | * Basically, this class is to be used in the MainWindow class of the TextUI. | 
|---|
|  | 37 | * There, we simply instantiate the class and call init() in order to add all | 
|---|
|  | 38 | * MenuItem's from MenuDescriptions and Action's ActionRegistry. | 
|---|
|  | 39 | * | 
|---|
| [b59da6] | 40 | * \sa QtMenu. | 
|---|
|  | 41 | */ | 
|---|
|  | 42 | template <class T> | 
|---|
|  | 43 | class TextMenu : virtual public MenuInterface, public Menu | 
|---|
|  | 44 | { | 
|---|
|  | 45 | public: | 
|---|
| [8f3f40] | 46 | /** Constructor for class TextMenu. | 
|---|
|  | 47 | * Initializes outputter and token and takes note whether to delete the | 
|---|
|  | 48 | * MenuInstance or not. | 
|---|
|  | 49 | */ | 
|---|
| [b59da6] | 50 | TextMenu(std::ostream &_outputter, const std::string &_token) : | 
|---|
|  | 51 | MenuInterface(_token), | 
|---|
|  | 52 | Menu(_token), | 
|---|
|  | 53 | MenuInstance(new T(_outputter, _token)), | 
|---|
|  | 54 | outputter(_outputter), | 
|---|
|  | 55 | deleteMenu(true) | 
|---|
|  | 56 | {} | 
|---|
|  | 57 |  | 
|---|
|  | 58 | explicit TextMenu(T *_MenuInstance) : | 
|---|
|  | 59 | Menu(_MenuInstance->getTitle()), | 
|---|
|  | 60 | MenuInstance(_MenuInstance), | 
|---|
|  | 61 | outputter(_MenuInstance->getOutputter()), | 
|---|
|  | 62 | deleteMenu(false) | 
|---|
|  | 63 | {} | 
|---|
|  | 64 |  | 
|---|
| [8f3f40] | 65 | /** Destructor of MenuInstance. | 
|---|
|  | 66 | * | 
|---|
|  | 67 | */ | 
|---|
| [68c923] | 68 | virtual ~TextMenu() | 
|---|
| [b59da6] | 69 | { | 
|---|
| [0e08d5] | 70 | ShortcutMap.clear(); | 
|---|
| [b59da6] | 71 | if (deleteMenu) | 
|---|
|  | 72 | delete MenuInstance; | 
|---|
| [0e08d5] | 73 | for (typename SubmenuList::iterator iter = Submenus.begin(); !Submenus.empty(); iter = Submenus.begin()) { | 
|---|
|  | 74 | delete(*iter); | 
|---|
|  | 75 | Submenus.erase(iter); | 
|---|
|  | 76 | } | 
|---|
| [b59da6] | 77 | } | 
|---|
|  | 78 |  | 
|---|
| [8f3f40] | 79 | /** Display this MenuInstance. | 
|---|
|  | 80 | * | 
|---|
|  | 81 | */ | 
|---|
| [b59da6] | 82 | void display() | 
|---|
|  | 83 | { | 
|---|
|  | 84 | MenuInstance->display(); | 
|---|
|  | 85 | } | 
|---|
|  | 86 |  | 
|---|
| [8f3f40] | 87 | /** Returns a pointer to the contained/wrapped MenuInstance. | 
|---|
|  | 88 | * \return pointer to template class pointer | 
|---|
|  | 89 | */ | 
|---|
| [b59da6] | 90 | T * const getMenuInstance() | 
|---|
|  | 91 | { | 
|---|
|  | 92 | return MenuInstance; | 
|---|
|  | 93 | } | 
|---|
|  | 94 |  | 
|---|
| [8f3f40] | 95 | /** Reserves a specific trigger key such that it is not used during init(). | 
|---|
|  | 96 | * \param trigger trigger key | 
|---|
|  | 97 | * \param &name token given for reference. | 
|---|
|  | 98 | */ | 
|---|
| [b59da6] | 99 | void reserveShortcut(char trigger, const std::string &name) | 
|---|
|  | 100 | { | 
|---|
|  | 101 | ShortcutMap.insert( std::pair < char, std::string> (trigger, name) ); | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | protected: | 
|---|
|  | 105 | // We need to have a reference of the Menu, as Qt returns reference to added menu as well | 
|---|
|  | 106 | T *MenuInstance; | 
|---|
|  | 107 | std::ostream &outputter; | 
|---|
|  | 108 |  | 
|---|
|  | 109 | private: | 
|---|
|  | 110 | bool deleteMenu; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | typedef std::map <char, std::string> MenuShortcutMap; | 
|---|
|  | 113 | MenuShortcutMap ShortcutMap; | 
|---|
|  | 114 |  | 
|---|
| [0e08d5] | 115 | typedef std::list< TextMenu<T> *> SubmenuList; | 
|---|
|  | 116 | SubmenuList Submenus; | 
|---|
|  | 117 |  | 
|---|
| [8f3f40] | 118 | /** Adds an ActionItem by simply creating a new one. | 
|---|
|  | 119 | * \param &token token of Action (token in ActionRegistry) | 
|---|
|  | 120 | * \param &description descriptive text to be shown | 
|---|
|  | 121 | */ | 
|---|
| [b59da6] | 122 | virtual void addActionItem(const std::string &token, const std::string &description) | 
|---|
|  | 123 | { | 
|---|
|  | 124 | new ActionMenuItem(getSuitableShortForm(description), description, MenuInstance, token); | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
| [8f3f40] | 127 | /** Adds a (dead) separator item. | 
|---|
|  | 128 | * | 
|---|
|  | 129 | */ | 
|---|
| [b59da6] | 130 | virtual void addSeparatorItem() | 
|---|
|  | 131 | { | 
|---|
| [e9be39] | 132 | new SeparatorMenuItem(MenuInstance); | 
|---|
| [b59da6] | 133 | } | 
|---|
|  | 134 |  | 
|---|
| [8f3f40] | 135 | /** Adds a Menu to this current Menu. | 
|---|
|  | 136 | * We also create here a leave action for this submenu to be able to return | 
|---|
|  | 137 | * to the current one again | 
|---|
|  | 138 | * \param &token token of the menu | 
|---|
|  | 139 | * \param &description descriptive text | 
|---|
|  | 140 | */ | 
|---|
| [b59da6] | 141 | virtual void addSubmenuItem(const std::string &token, const std::string &description) | 
|---|
|  | 142 | { | 
|---|
|  | 143 | TextMenu<TxMenu> *NewMenu = new TextMenu<TxMenu>(outputter, token); | 
|---|
| [0e08d5] | 144 | Submenus.push_back(NewMenu); | 
|---|
| [b59da6] | 145 | new SubMenuItem(getSuitableShortForm(description), description, MenuInstance, NewMenu->getMenuInstance()); | 
|---|
|  | 146 | NewMenu->reserveShortcut('q',"leave"+token); | 
|---|
| [3139b2] | 147 | MoleCuilder::ActionTrait leaveTrait( | 
|---|
| [cb85f24] | 148 | MoleCuilder::OptionTrait("leave"+token, &typeid(void), "leave menu "+token), | 
|---|
| [b59da6] | 149 | token, | 
|---|
| [cb85f24] | 150 | MoleCuilder::ActionRegistry::getInstance().getLastPosition(token)+2); // have a separator in between | 
|---|
| [b59da6] | 151 | new TxMenu::LeaveAction(NewMenu->getMenuInstance(), leaveTrait); | 
|---|
|  | 152 | NewMenu->init(); | 
|---|
|  | 153 | new ActionMenuItem('q',"leave"+token,NewMenu->getMenuInstance(),"leave"+token); | 
|---|
|  | 154 | } | 
|---|
|  | 155 |  | 
|---|
| [8f3f40] | 156 | /** Return the next available trigger key suitable to this name. | 
|---|
|  | 157 | * This function is used internally to make sure that each key is unique to | 
|---|
|  | 158 | * each Action/Menu. The chosen trigger key is also stored in an internal ShortcutMap. | 
|---|
|  | 159 | * \param &name text shown in menu | 
|---|
|  | 160 | * \return trigger key | 
|---|
|  | 161 | */ | 
|---|
| [b59da6] | 162 | char getSuitableShortForm(const std::string &name) | 
|---|
|  | 163 | { | 
|---|
|  | 164 | for (std::string::const_iterator CharRunner = name.begin(); CharRunner != name.end(); ++CharRunner) { | 
|---|
|  | 165 | if (ShortcutMap.find(*CharRunner) == ShortcutMap.end()) { | 
|---|
|  | 166 | ShortcutMap.insert( std::pair < char, std::string> (*CharRunner, name) ); | 
|---|
|  | 167 | return *CharRunner; | 
|---|
|  | 168 | } | 
|---|
|  | 169 | } | 
|---|
|  | 170 | // if no letter matches, take digits | 
|---|
|  | 171 | int i=0; | 
|---|
|  | 172 | for (;i<10;++i) { | 
|---|
|  | 173 | if (ShortcutMap.find((char)i + '0') == ShortcutMap.end()) | 
|---|
|  | 174 | break; | 
|---|
|  | 175 | } | 
|---|
|  | 176 | if (i != 10) { | 
|---|
|  | 177 | ShortcutMap.insert( std::pair < char, std::string > ((char)i + '0', name)); | 
|---|
|  | 178 | return ((char)i + '0'); | 
|---|
|  | 179 | } else { | 
|---|
| [47d041] | 180 | ELOG(1, "Could not find a suitable shortform for " << name << "."); | 
|---|
| [b59da6] | 181 | return '#'; | 
|---|
|  | 182 | } | 
|---|
|  | 183 | } | 
|---|
|  | 184 | }; | 
|---|
|  | 185 |  | 
|---|
|  | 186 | #endif /* TEXTMENU_HPP_ */ | 
|---|