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