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