| [b47bfc] | 1 | /* | 
|---|
| [4cf323d] | 2 | * QtMenu.hpp | 
|---|
| [b47bfc] | 3 | * | 
|---|
| [b59da6] | 4 | *  Created on: Nov 5, 2010 | 
|---|
|  | 5 | *      Author: heber | 
|---|
| [b47bfc] | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [b59da6] | 8 | #ifndef MENUINTERFACEQT_HPP_ | 
|---|
|  | 9 | #define MENUINTERFACEQT_HPP_ | 
|---|
| [b47bfc] | 10 |  | 
|---|
| [b59da6] | 11 | #include <Qt/qaction.h> | 
|---|
| [b47bfc] | 12 |  | 
|---|
| [b59da6] | 13 | #include <iostream> | 
|---|
|  | 14 | #include <list> | 
|---|
|  | 15 | #include <map> | 
|---|
|  | 16 | #include <string> | 
|---|
| [b47bfc] | 17 |  | 
|---|
|  | 18 | #include "Menu/Menu.hpp" | 
|---|
| [b59da6] | 19 | #include "Menu/MenuInterface.hpp" | 
|---|
|  | 20 | #include "Menu/Qt4/QtMenuPipe.hpp" | 
|---|
| [b47bfc] | 21 |  | 
|---|
| [b59da6] | 22 | /** QtMenu is a specialization of MenuInterface to Qt-like menus. | 
|---|
|  | 23 | * I.e. with this interface we can access QMenu and QMenuBar. | 
|---|
|  | 24 | * (The latter is the reason why we have to add this additional wrapping layer). | 
|---|
|  | 25 | */ | 
|---|
|  | 26 | template <class T> | 
|---|
|  | 27 | class QtMenu : virtual public MenuInterface, public Menu | 
|---|
| [b47bfc] | 28 | { | 
|---|
|  | 29 | public: | 
|---|
| [b59da6] | 30 | explicit QtMenu(const std::string &_token) : | 
|---|
|  | 31 | MenuInterface(_token), | 
|---|
|  | 32 | Menu(_token), | 
|---|
|  | 33 | MenuInstance(new T(QString(getNameWithAccelerator(_token).c_str()))), | 
|---|
|  | 34 | deleteMenu(true) | 
|---|
|  | 35 | {} | 
|---|
| [b47bfc] | 36 |  | 
|---|
| [b59da6] | 37 | QtMenu(T *_Menu, const std::string &_token) : | 
|---|
|  | 38 | MenuInterface(_token), | 
|---|
|  | 39 | Menu(_token), | 
|---|
|  | 40 | MenuInstance(_Menu), | 
|---|
|  | 41 | deleteMenu(false) | 
|---|
|  | 42 | {} | 
|---|
|  | 43 |  | 
|---|
|  | 44 | virtual ~QtMenu() | 
|---|
|  | 45 | { | 
|---|
|  | 46 | for(std::list<QtMenuPipe*>::iterator it=plumbing.begin(); it != plumbing.end(); it++) | 
|---|
|  | 47 | delete (*it); | 
|---|
|  | 48 |  | 
|---|
|  | 49 | if (deleteMenu) | 
|---|
|  | 50 | delete MenuInstance; | 
|---|
|  | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | T * const getMenuInstance() | 
|---|
|  | 54 | { | 
|---|
|  | 55 | return MenuInstance; | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
|  | 58 | protected: | 
|---|
|  | 59 | // We need to have a reference of the Menu, as Qt returns reference to added menu as well | 
|---|
|  | 60 | T *MenuInstance; | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /** Puts Qt's token, the ampersand, in front of the accelerator char in the menu name. | 
|---|
|  | 63 | * \param ActionName Action of menu | 
|---|
|  | 64 | * \return name with ampersand added at the right place | 
|---|
|  | 65 | */ | 
|---|
|  | 66 | std::string getNameWithAccelerator(const std::string &ActionName) | 
|---|
|  | 67 | { | 
|---|
|  | 68 | std::string newname; | 
|---|
|  | 69 | bool Inserted = false; | 
|---|
|  | 70 | std::pair < MenuShortcutMap::iterator, bool > Inserter; | 
|---|
|  | 71 | for (std::string::const_iterator CharRunner = ActionName.begin(); | 
|---|
|  | 72 | CharRunner != ActionName.end(); | 
|---|
|  | 73 | ++CharRunner) { | 
|---|
|  | 74 | std::cout << "Current char is " << *CharRunner << std::endl; | 
|---|
|  | 75 | if (!Inserted) { | 
|---|
|  | 76 | Inserter = ShortcutMap.insert( | 
|---|
|  | 77 | std::pair<char, std::string > (*CharRunner, ActionName) | 
|---|
|  | 78 | ); | 
|---|
|  | 79 | if (Inserter.second) { | 
|---|
|  | 80 | std::cout << "Accelerator is " << *CharRunner << std::endl; | 
|---|
|  | 81 | newname += '&'; | 
|---|
|  | 82 | Inserted = true; | 
|---|
|  | 83 | } | 
|---|
|  | 84 | } | 
|---|
|  | 85 | newname += *CharRunner; | 
|---|
|  | 86 | } | 
|---|
|  | 87 | return newname; | 
|---|
|  | 88 | } | 
|---|
| [b47bfc] | 89 |  | 
|---|
|  | 90 | private: | 
|---|
| [b59da6] | 91 | bool deleteMenu; | 
|---|
|  | 92 | std::list<QtMenuPipe*> plumbing; | 
|---|
|  | 93 |  | 
|---|
|  | 94 | typedef std::map <char, std::string> MenuShortcutMap; | 
|---|
|  | 95 | MenuShortcutMap ShortcutMap; | 
|---|
|  | 96 |  | 
|---|
|  | 97 | virtual void addActionItem(const std::string &token, const std::string &description) | 
|---|
|  | 98 | { | 
|---|
|  | 99 | QAction *action = MenuInstance->addAction(QString(getNameWithAccelerator(description).c_str())); | 
|---|
|  | 100 | QtMenuPipe *pipe = new QtMenuPipe(token,action); | 
|---|
|  | 101 | QObject::connect(action, SIGNAL(triggered()),pipe,SLOT(called())); | 
|---|
|  | 102 | plumbing.push_back(pipe); | 
|---|
|  | 103 | } | 
|---|
|  | 104 |  | 
|---|
|  | 105 | virtual void addSeparatorItem() | 
|---|
|  | 106 | { | 
|---|
|  | 107 | MenuInstance->addSeparator(); | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | virtual void addSubmenuItem(const std::string &token, const std::string &description) | 
|---|
|  | 111 | { | 
|---|
|  | 112 | QMenu *Menu = MenuInstance->addMenu(QString(token.c_str())); | 
|---|
|  | 113 | QtMenu<QMenu> *NewMenu = new QtMenu<QMenu>(Menu, token); | 
|---|
|  | 114 | NewMenu->init(); | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
| [b47bfc] | 117 | }; | 
|---|
|  | 118 |  | 
|---|
| [b59da6] | 119 | #endif /* MENUINTERFACEQT_HPP_ */ | 
|---|