fw4spl
MenuLayoutManager.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGuiQt/layoutManager/MenuLayoutManager.hpp"
8 
9 #include "fwGuiQt/ActionCallback.hpp"
10 #include "fwGuiQt/container/QtMenuContainer.hpp"
11 #include "fwGuiQt/container/QtMenuItemContainer.hpp"
12 
13 #include <fwGui/registry/macros.hpp>
14 
15 #include <boost/assign/list_of.hpp>
16 #include <boost/lambda/lambda.hpp>
17 
18 #include <QAction>
19 #include <QActionGroup>
20 #include <QMenu>
21 
22 #include <functional>
23 
24 fwGuiRegisterMacro( ::fwGui::layoutManager::MenuLayoutManager,
25  ::fwGui::layoutManager::IMenuLayoutManager::REGISTRY_KEY );
26 
27 namespace fwGui
28 {
29 namespace layoutManager
30 {
31 
32 //-----------------------------------------------------------------------------
33 
34 MenuLayoutManager::MenuLayoutManager(::fwGui::GuiBaseObject::Key key)
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 MenuLayoutManager::~MenuLayoutManager()
41 {
42 }
43 
44 //-----------------------------------------------------------------------------
45 
46 void MenuLayoutManager::createLayout( ::fwGui::container::fwMenu::sptr parent )
47 {
49 
50  m_parent = ::fwGuiQt::container::QtMenuContainer::dynamicCast(parent);
51  SLM_ASSERT("dynamicCast fwMenu to QtMenuContainer failed", m_parent);
52 
53  QMenu* menu = m_parent->getQtMenu();
54 
55  QActionGroup* actionGroup = 0;
56  unsigned int menuItemIndex = 0;
58  {
59  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItem = ::fwGuiQt::container::QtMenuItemContainer::New();
60 
61  QAction* action = menu->addAction( QString::fromStdString(actionInfo.m_name) );
62 
63  action->setSeparator(actionInfo.m_isSeparator);
64 
65  if (!actionInfo.m_icon.empty())
66  {
67  QIcon icon(QString::fromStdString(actionInfo.m_icon.string()));
68  action->setIcon(icon);
69  }
70  if (actionInfo.m_type == ::fwGui::layoutManager::IMenuLayoutManager::QUIT)
71  {
72  action->setMenuRole(QAction::QuitRole);
73  }
74  else if (actionInfo.m_type == ::fwGui::layoutManager::IMenuLayoutManager::ABOUT)
75  {
76  action->setMenuRole(QAction::AboutRole);
77  }
78  else
79  {
80  action->setMenuRole(QAction::NoRole);
81  }
82 
83  action->setCheckable(actionInfo.m_isCheckable || actionInfo.m_isRadio);
84 
85  if (actionInfo.m_isRadio)
86  {
87  if (!actionGroup)
88  {
89  actionGroup = new QActionGroup(menu);
90  }
91  actionGroup->addAction(action);
92  }
93 
94  // create shortcut
95  if( !actionInfo.m_shortcut.empty() )
96  {
97  action->setShortcut(QKeySequence(QString::fromStdString(actionInfo.m_shortcut)));
98  }
99 
100  if (actionInfo.m_isMenu)
101  {
102  ::fwGuiQt::container::QtMenuContainer::sptr menu = ::fwGuiQt::container::QtMenuContainer::New();
103  QMenu* qtMenu = new QMenu();
104  menu->setQtMenu(qtMenu);
105  action->setMenu(qtMenu);
106  m_menus.push_back(menu);
107  }
108 
109  menuItem->setQtMenuItem(action);
110 
111  if(!actionInfo.m_isSeparator && !actionInfo.m_isMenu )
112  {
113  m_menuItems.push_back(menuItem);
114  OSLM_ASSERT("No callback found for menu" << actionInfo.m_name, menuItemIndex < m_callbacks.size());
115  ::fwGui::IMenuItemCallback::sptr callback = m_callbacks.at(menuItemIndex);
116 
117  ::fwGuiQt::ActionCallback::sptr qtCallback = ::fwGuiQt::ActionCallback::dynamicCast(callback);
118  SLM_ASSERT("dynamicCast IMenuItemCallback to ActionCallback failed", qtCallback);
119 
120  QObject::connect( action, SIGNAL(triggered(bool)), qtCallback.get(), SLOT(executeQt(bool)));
121  QObject::connect( action, SIGNAL(toggled(bool)), qtCallback.get(), SLOT(checkQt(bool)));
122  menuItemIndex++;
123  }
124  else
125  {
126  actionGroup = 0;
127  }
128  }
129 }
130 
131 //-----------------------------------------------------------------------------
132 
134 {
135  this->destroyActions();
136  m_menuItems.clear();
137  m_parent->clean();
138 }
139 
140 //-----------------------------------------------------------------------------
141 
142 void MenuLayoutManager::menuItemSetVisible(::fwGui::container::fwMenuItem::sptr fwMenuItem, bool isVisible)
143 {
144  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItemContainer =
145  ::fwGuiQt::container::QtMenuItemContainer::dynamicCast(fwMenuItem);
146  QAction* action = menuItemContainer->getQtMenuItem();
147  action->setVisible(isVisible);
148 }
149 
150 //-----------------------------------------------------------------------------
151 
152 void MenuLayoutManager::menuItemSetEnabled(::fwGui::container::fwMenuItem::sptr fwMenuItem, bool isEnabled)
153 {
154  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItemContainer =
155  ::fwGuiQt::container::QtMenuItemContainer::dynamicCast(fwMenuItem);
156  QAction* action = menuItemContainer->getQtMenuItem();
157  action->setEnabled(isEnabled);
158 }
159 
160 //-----------------------------------------------------------------------------
161 
162 void MenuLayoutManager::menuItemSetChecked(::fwGui::container::fwMenuItem::sptr fwMenuItem, bool isChecked)
163 {
164  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItemContainer =
165  ::fwGuiQt::container::QtMenuItemContainer::dynamicCast(fwMenuItem);
166  QAction* action = menuItemContainer->getQtMenuItem();
167  action->setChecked(isChecked);
168 }
169 
170 //-----------------------------------------------------------------------------
171 
172 } // namespace layoutManager
173 } // namespace fwGui
174 
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
std::vector< ::fwGui::container::fwMenu::sptr > m_menus
All menus managed by this layout.
virtual FWGUIQT_API void menuItemSetChecked(::fwGui::container::fwMenuItem::sptr, bool isChecked) override
Set the action checked or not.
virtual FWGUIQT_API void createLayout(::fwGui::container::fwMenu::sptr parent) override
Instantiate actions with parent menu.
std::vector< ActionInfo > m_actionInfo
Save action informations from configuration.
virtual FWGUI_API void destroyActions()
Helper to destroy local actions.
std::vector< ::fwGui::container::fwMenuItem::sptr > m_menuItems
All actions managed by this layout.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
CallbacksType m_callbacks
Callbacks associate with menu items.
virtual FWGUIQT_API void destroyLayout() override
Destroy local menus.
virtual FWGUIQT_API void menuItemSetEnabled(::fwGui::container::fwMenuItem::sptr menuItem, bool isEnabled) override
Set the action enable or not.
Defines the menu layout manager for IHM.
virtual FWGUIQT_API void menuItemSetVisible(::fwGui::container::fwMenuItem::sptr menuItem, bool isVisible) override
Set the action visibility.