| [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 | 
 | 
|---|
| [56f73b] | 11 | // include config.h
 | 
|---|
| [9cdab3] | 12 | #include <cstddef>
 | 
|---|
| [56f73b] | 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 14 | #include <config.h>
 | 
|---|
 | 15 | #endif
 | 
|---|
 | 16 | 
 | 
|---|
 | 17 | 
 | 
|---|
| [b59da6] | 18 | #include <Qt/qaction.h>
 | 
|---|
| [9cdab3] | 19 | #include <Qt/qpoint.h>
 | 
|---|
| [b47bfc] | 20 | 
 | 
|---|
| [86c013] | 21 | #include "Menu/Qt4/QMenu_tooltip.hpp"
 | 
|---|
 | 22 | 
 | 
|---|
| [b59da6] | 23 | #include <iostream>
 | 
|---|
 | 24 | #include <list>
 | 
|---|
 | 25 | #include <map>
 | 
|---|
 | 26 | #include <string>
 | 
|---|
| [b47bfc] | 27 | 
 | 
|---|
 | 28 | #include "Menu/Menu.hpp"
 | 
|---|
| [b59da6] | 29 | #include "Menu/MenuInterface.hpp"
 | 
|---|
 | 30 | #include "Menu/Qt4/QtMenuPipe.hpp"
 | 
|---|
| [b47bfc] | 31 | 
 | 
|---|
| [b59da6] | 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
 | 
|---|
| [b47bfc] | 38 | {
 | 
|---|
 | 39 | public:
 | 
|---|
| [b59da6] | 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 |   {}
 | 
|---|
| [b47bfc] | 46 | 
 | 
|---|
| [b59da6] | 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 |   {
 | 
|---|
| [9cdab3] | 56 |     // delete all plumbed actions
 | 
|---|
| [b59da6] | 57 |     for(std::list<QtMenuPipe*>::iterator it=plumbing.begin(); it != plumbing.end(); it++)
 | 
|---|
 | 58 |       delete (*it);
 | 
|---|
 | 59 | 
 | 
|---|
| [9cdab3] | 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
 | 
|---|
| [b59da6] | 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) {
 | 
|---|
| [ad7270] | 90 | //      std::cout << "Current char is " << *CharRunner << std::endl;
 | 
|---|
| [b59da6] | 91 |       if (!Inserted) {
 | 
|---|
 | 92 |         Inserter = ShortcutMap.insert(
 | 
|---|
 | 93 |             std::pair<char, std::string > (*CharRunner, ActionName)
 | 
|---|
 | 94 |             );
 | 
|---|
 | 95 |         if (Inserter.second) {
 | 
|---|
| [ad7270] | 96 | //          std::cout << "Accelerator is " << *CharRunner << std::endl;
 | 
|---|
| [b59da6] | 97 |           newname += '&';
 | 
|---|
 | 98 |           Inserted = true;
 | 
|---|
 | 99 |         }
 | 
|---|
 | 100 |       }
 | 
|---|
 | 101 |       newname += *CharRunner;
 | 
|---|
 | 102 |     }
 | 
|---|
 | 103 |     return newname;
 | 
|---|
 | 104 |   }
 | 
|---|
| [b47bfc] | 105 | 
 | 
|---|
 | 106 | private:
 | 
|---|
| [b59da6] | 107 |   bool deleteMenu;
 | 
|---|
 | 108 |   std::list<QtMenuPipe*> plumbing;
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |   typedef std::map <char, std::string> MenuShortcutMap;
 | 
|---|
 | 111 |   MenuShortcutMap ShortcutMap;
 | 
|---|
 | 112 | 
 | 
|---|
| [9cdab3] | 113 |   typedef std::list< QtMenu<QMenu_tooltip>* > SubMenus_t;
 | 
|---|
 | 114 |   SubMenus_t submenus;
 | 
|---|
 | 115 | 
 | 
|---|
| [b59da6] | 116 |   virtual void addActionItem(const std::string &token, const std::string &description)
 | 
|---|
 | 117 |   {
 | 
|---|
| [163110] | 118 |     QAction *action = MenuInstance->addAction(QString(getNameWithAccelerator(token).c_str()));
 | 
|---|
| [86c013] | 119 |     action->setToolTip(QString(description.c_str()));
 | 
|---|
 | 120 |     action->setWhatsThis(QString(description.c_str()));
 | 
|---|
| [b59da6] | 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 |   {
 | 
|---|
| [86c013] | 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);
 | 
|---|
| [9cdab3] | 136 |     submenus.push_back(NewMenu);
 | 
|---|
| [b59da6] | 137 |     NewMenu->init();
 | 
|---|
 | 138 |   }
 | 
|---|
 | 139 | 
 | 
|---|
| [9cdab3] | 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 |   }
 | 
|---|
| [b47bfc] | 158 | };
 | 
|---|
 | 159 | 
 | 
|---|
| [b59da6] | 160 | #endif /* MENUINTERFACEQT_HPP_ */
 | 
|---|