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