| 1 | /* | 
|---|
| 2 | * QtMenu.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Nov 5, 2010 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef MENUINTERFACEQT_HPP_ | 
|---|
| 9 | #define MENUINTERFACEQT_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | // include config.h | 
|---|
| 12 | #include <cstddef> | 
|---|
| 13 | #ifdef HAVE_CONFIG_H | 
|---|
| 14 | #include <config.h> | 
|---|
| 15 | #endif | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | #include <Qt/qaction.h> | 
|---|
| 19 | #include <Qt/qpoint.h> | 
|---|
| 20 |  | 
|---|
| 21 | #include "Menu/Qt4/QMenu_tooltip.hpp" | 
|---|
| 22 |  | 
|---|
| 23 | #include <iostream> | 
|---|
| 24 | #include <list> | 
|---|
| 25 | #include <map> | 
|---|
| 26 | #include <string> | 
|---|
| 27 |  | 
|---|
| 28 | #include "Menu/Menu.hpp" | 
|---|
| 29 | #include "Menu/MenuInterface.hpp" | 
|---|
| 30 | #include "Menu/Qt4/QtMenuPipe.hpp" | 
|---|
| 31 |  | 
|---|
| 32 | /** QtMenu is a specialization of MenuInterface to Qt-like menus. | 
|---|
| 33 | * I.e. with this interface we can access QMenu and QMenuBar. | 
|---|
| 34 | * (The latter is the reason why we have to add this additional wrapping layer). | 
|---|
| 35 | */ | 
|---|
| 36 | template <class T> | 
|---|
| 37 | class QtMenu : virtual public MenuInterface, public Menu | 
|---|
| 38 | { | 
|---|
| 39 | public: | 
|---|
| 40 | explicit QtMenu(const std::string &_token) : | 
|---|
| 41 | MenuInterface(_token), | 
|---|
| 42 | Menu(_token), | 
|---|
| 43 | MenuInstance(new T(QString(getNameWithAccelerator(_token).c_str()))), | 
|---|
| 44 | deleteMenu(true) | 
|---|
| 45 | {} | 
|---|
| 46 |  | 
|---|
| 47 | QtMenu(T *_Menu, const std::string &_token) : | 
|---|
| 48 | MenuInterface(_token), | 
|---|
| 49 | Menu(_token), | 
|---|
| 50 | MenuInstance(_Menu), | 
|---|
| 51 | deleteMenu(false) | 
|---|
| 52 | {} | 
|---|
| 53 |  | 
|---|
| 54 | virtual ~QtMenu() | 
|---|
| 55 | { | 
|---|
| 56 | // delete all plumbed actions | 
|---|
| 57 | for(std::list<QtMenuPipe*>::iterator it=plumbing.begin(); it != plumbing.end(); it++) | 
|---|
| 58 | delete (*it); | 
|---|
| 59 |  | 
|---|
| 60 | // delete all submenus | 
|---|
| 61 | for(SubMenus_t::iterator iter = submenus.begin(); iter != submenus.end(); ++iter) | 
|---|
| 62 | delete(*iter); | 
|---|
| 63 |  | 
|---|
| 64 | // delete the wrapped instance if we took over ownership | 
|---|
| 65 | if (deleteMenu) | 
|---|
| 66 | delete MenuInstance; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | T * const getMenuInstance() | 
|---|
| 70 | { | 
|---|
| 71 | return MenuInstance; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | protected: | 
|---|
| 75 | // We need to have a reference of the Menu, as Qt returns reference to added menu as well | 
|---|
| 76 | T *MenuInstance; | 
|---|
| 77 |  | 
|---|
| 78 | /** Puts Qt's token, the ampersand, in front of the accelerator char in the menu name. | 
|---|
| 79 | * \param ActionName Action of menu | 
|---|
| 80 | * \return name with ampersand added at the right place | 
|---|
| 81 | */ | 
|---|
| 82 | std::string getNameWithAccelerator(const std::string &ActionName) | 
|---|
| 83 | { | 
|---|
| 84 | std::string newname; | 
|---|
| 85 | bool Inserted = false; | 
|---|
| 86 | std::pair < MenuShortcutMap::iterator, bool > Inserter; | 
|---|
| 87 | for (std::string::const_iterator CharRunner = ActionName.begin(); | 
|---|
| 88 | CharRunner != ActionName.end(); | 
|---|
| 89 | ++CharRunner) { | 
|---|
| 90 | //      std::cout << "Current char is " << *CharRunner << std::endl; | 
|---|
| 91 | if (!Inserted) { | 
|---|
| 92 | Inserter = ShortcutMap.insert( | 
|---|
| 93 | std::pair<char, std::string > (*CharRunner, ActionName) | 
|---|
| 94 | ); | 
|---|
| 95 | if (Inserter.second) { | 
|---|
| 96 | //          std::cout << "Accelerator is " << *CharRunner << std::endl; | 
|---|
| 97 | newname += '&'; | 
|---|
| 98 | Inserted = true; | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 | newname += *CharRunner; | 
|---|
| 102 | } | 
|---|
| 103 | return newname; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | private: | 
|---|
| 107 | bool deleteMenu; | 
|---|
| 108 | std::list<QtMenuPipe*> plumbing; | 
|---|
| 109 |  | 
|---|
| 110 | typedef std::map <char, std::string> MenuShortcutMap; | 
|---|
| 111 | MenuShortcutMap ShortcutMap; | 
|---|
| 112 |  | 
|---|
| 113 | typedef std::list< QtMenu<QMenu_tooltip>* > SubMenus_t; | 
|---|
| 114 | SubMenus_t submenus; | 
|---|
| 115 |  | 
|---|
| 116 | virtual void addActionItem(const std::string &token, const std::string &description) | 
|---|
| 117 | { | 
|---|
| 118 | QAction *action = MenuInstance->addAction(QString(getNameWithAccelerator(token).c_str())); | 
|---|
| 119 | action->setToolTip(QString(description.c_str())); | 
|---|
| 120 | action->setWhatsThis(QString(description.c_str())); | 
|---|
| 121 | QtMenuPipe *pipe = new QtMenuPipe(token,action); | 
|---|
| 122 | QObject::connect(action, SIGNAL(triggered()),pipe,SLOT(called())); | 
|---|
| 123 | plumbing.push_back(pipe); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | virtual void addSeparatorItem() | 
|---|
| 127 | { | 
|---|
| 128 | MenuInstance->addSeparator(); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | virtual void addSubmenuItem(const std::string &token, const std::string &description) | 
|---|
| 132 | { | 
|---|
| 133 | QMenu_tooltip *Menu = new QMenu_tooltip(QString(token.c_str())); | 
|---|
| 134 | MenuInstance->addMenu(Menu); | 
|---|
| 135 | QtMenu<QMenu_tooltip> *NewMenu = new QtMenu<QMenu_tooltip>(Menu, token); | 
|---|
| 136 | submenus.push_back(NewMenu); | 
|---|
| 137 | NewMenu->init(); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | public: | 
|---|
| 141 | QtMenu<QMenu_tooltip>* findSubmenu(const std::string &token) { | 
|---|
| 142 | for (SubMenus_t::iterator iter = submenus.begin(); iter != submenus.end(); ++iter) { | 
|---|
| 143 | QtMenu<QMenu_tooltip>* submenu = *(iter); | 
|---|
| 144 | if (submenu->getName() == token) | 
|---|
| 145 | return submenu; | 
|---|
| 146 | else { | 
|---|
| 147 | QtMenu<QMenu_tooltip>* found_submenu = submenu->findSubmenu(token); | 
|---|
| 148 | if (found_submenu != NULL) | 
|---|
| 149 | return found_submenu; | 
|---|
| 150 | } | 
|---|
| 151 | } | 
|---|
| 152 | return NULL; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | void popup(const QPoint &pos) { | 
|---|
| 156 | MenuInstance->popup(pos); | 
|---|
| 157 | } | 
|---|
| 158 | }; | 
|---|
| 159 |  | 
|---|
| 160 | #endif /* MENUINTERFACEQT_HPP_ */ | 
|---|