fw4spl
ToolBarLayoutManager.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/ToolBarLayoutManager.hpp"
8 
9 #include "fwGuiQt/ActionCallback.hpp"
10 #include "fwGuiQt/container/QtContainer.hpp"
11 #include "fwGuiQt/container/QtMenuContainer.hpp"
12 #include "fwGuiQt/container/QtMenuItemContainer.hpp"
13 #include "fwGuiQt/container/QtToolBarContainer.hpp"
14 
15 #include <fwGui/registry/macros.hpp>
16 
17 #include <boost/assign/list_of.hpp>
18 #include <boost/lambda/lambda.hpp>
19 
20 #include <QAction>
21 #include <QActionGroup>
22 #include <QMenu>
23 #include <QToolBar>
24 #include <QToolButton>
25 
26 #include <functional>
27 
28 fwGuiRegisterMacro( ::fwGui::layoutManager::ToolBarLayoutManager,
29  ::fwGui::layoutManager::IToolBarLayoutManager::REGISTRY_KEY );
30 
31 namespace fwGui
32 {
33 namespace layoutManager
34 {
35 
36 //-----------------------------------------------------------------------------
37 
38 ToolBarLayoutManager::ToolBarLayoutManager(::fwGui::GuiBaseObject::Key key)
39 {
40 }
41 
42 //-----------------------------------------------------------------------------
43 
44 ToolBarLayoutManager::~ToolBarLayoutManager()
45 {
46 }
47 
48 //-----------------------------------------------------------------------------
49 
50 void ToolBarLayoutManager::createLayout( ::fwGui::container::fwToolBar::sptr parent )
51 {
53 
54  m_parent = ::fwGuiQt::container::QtToolBarContainer::dynamicCast(parent);
55  SLM_ASSERT("dynamicCast fwToolBar to QtToolBarContainer failed", m_parent);
56 
57  QToolBar* toolBar = m_parent->getQtToolBar();
58 
59  QActionGroup* actionGroup = 0;
60  unsigned int menuItemIndex = 0;
62  {
63  if (actionInfo.m_isSeparator)
64  {
65  if (actionInfo.m_size > 0)
66  {
67  QWidget* widget = new QWidget(toolBar);
68  widget->setMinimumWidth(actionInfo.m_size);
69  toolBar->addWidget(widget);
70  }
71  else
72  {
73  toolBar->addSeparator();
74  }
75  actionGroup = 0;
76  }
77  else if (actionInfo.m_isSpacer)
78  {
79  QWidget* spacer = new QWidget(toolBar);
80  spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
81  toolBar->addWidget(spacer);
82  actionGroup = 0;
83  }
84  else if (actionInfo.m_isMenu)
85  {
86  ::fwGuiQt::container::QtMenuContainer::sptr menu = ::fwGuiQt::container::QtMenuContainer::New();
87  QMenu* qtMenu = new QMenu(toolBar);
88  menu->setQtMenu(qtMenu);
89 
90  QToolButton* toolButton = new QToolButton(toolBar);
91  if (toolBar->orientation() == Qt::Horizontal)
92  {
93  toolButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
94  }
95  else
96  {
97  toolButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
98  }
99  toolButton->setMenu(qtMenu);
100  toolButton->setPopupMode(QToolButton::InstantPopup);
101  toolButton->setText(QString::fromStdString(actionInfo.m_name));
102  if (!actionInfo.m_icon.empty())
103  {
104  QIcon icon(QString::fromStdString(actionInfo.m_icon.string()));
105  toolButton->setIcon(icon);
106  toolButton->setToolTip(QString::fromStdString(actionInfo.m_name));
107  toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
108  }
109  toolBar->addWidget(toolButton);
110  m_menus.push_back(menu);
111  }
112  else if (actionInfo.m_isEditor)
113  {
114  ::fwGuiQt::container::QtContainer::sptr container = ::fwGuiQt::container::QtContainer::New();
115  QWidget* widget = new QWidget(toolBar);
116  container->setQtContainer(widget);
117 
118  if (toolBar->orientation() == Qt::Horizontal)
119  {
120  widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
121  }
122  else
123  {
124  widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
125  }
126  widget->adjustSize();
127  toolBar->addWidget(widget);
128 
129  m_containers.push_back(container);
130  }
131  else
132  {
133  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItem = ::fwGuiQt::container::QtMenuItemContainer::New();
134  QAction* action;
135  if (!actionInfo.m_icon.empty())
136  {
137  QIcon icon(QString::fromStdString(actionInfo.m_icon.string()));
138  if (!actionInfo.m_icon2.empty())
139  {
140  icon.addFile(QString::fromStdString(actionInfo.m_icon2.string()), QSize(), QIcon::Normal,
141  QIcon::On);
142  icon.addFile(QString::fromStdString(actionInfo.m_icon2.string()), QSize(), QIcon::Active,
143  QIcon::On);
144  }
145  action = toolBar->addAction( icon, QString::fromStdString(actionInfo.m_name) );
146  }
147  else
148  {
149  action = toolBar->addAction( QString::fromStdString(actionInfo.m_name) );
150  }
151 
152  action->setCheckable(actionInfo.m_isCheckable || actionInfo.m_isRadio);
153 
154  if (actionInfo.m_isRadio)
155  {
156  if (!actionGroup)
157  {
158  actionGroup = new QActionGroup(toolBar);
159  }
160  actionGroup->addAction(action);
161  }
162 
163  // create shortcut
164  if( !actionInfo.m_shortcut.empty() )
165  {
166  action->setShortcut(QKeySequence(QString::fromStdString(actionInfo.m_shortcut)));
167  }
168 
169  menuItem->setQtMenuItem(action);
170 
171  m_menuItems.push_back(menuItem);
172  OSLM_ASSERT("No callback found for menu" << actionInfo.m_name, menuItemIndex < m_callbacks.size());
173  ::fwGui::IMenuItemCallback::sptr callback = m_callbacks.at(menuItemIndex);
174 
175  ::fwGuiQt::ActionCallback::sptr qtCallback = ::fwGuiQt::ActionCallback::dynamicCast(callback);
176  SLM_ASSERT("dynamicCast IMenuItemCallback to ActionCallback failed", qtCallback);
177 
178  QObject::connect( action, SIGNAL(triggered(bool)), qtCallback.get(), SLOT(executeQt(bool)));
179  QObject::connect( action, SIGNAL(toggled(bool)), qtCallback.get(), SLOT(checkQt(bool)));
180  menuItemIndex++;
181  }
182  }
183 }
184 
185 //-----------------------------------------------------------------------------
186 
188 {
189  this->destroyActions();
190  m_parent->clean();
191  m_menuItems.clear();
192 }
193 
194 //-----------------------------------------------------------------------------
195 
196 void ToolBarLayoutManager::menuItemSetVisible(::fwGui::container::fwMenuItem::sptr fwMenuItem, bool isVisible)
197 {
198  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItemContainer =
199  ::fwGuiQt::container::QtMenuItemContainer::dynamicCast(fwMenuItem);
200  QAction* action = menuItemContainer->getQtMenuItem();
201  action->setVisible(isVisible);
202 }
203 
204 //-----------------------------------------------------------------------------
205 
206 void ToolBarLayoutManager::menuItemSetEnabled(::fwGui::container::fwMenuItem::sptr fwMenuItem, bool isEnabled)
207 {
208  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItemContainer =
209  ::fwGuiQt::container::QtMenuItemContainer::dynamicCast(fwMenuItem);
210  QAction* action = menuItemContainer->getQtMenuItem();
211  action->setEnabled(isEnabled);
212 }
213 
214 //-----------------------------------------------------------------------------
215 
216 void ToolBarLayoutManager::menuItemSetChecked(::fwGui::container::fwMenuItem::sptr fwMenuItem, bool isChecked)
217 {
218  ::fwGuiQt::container::QtMenuItemContainer::sptr menuItemContainer =
219  ::fwGuiQt::container::QtMenuItemContainer::dynamicCast(fwMenuItem);
220  QAction* action = menuItemContainer->getQtMenuItem();
221  action->setChecked(isChecked);
222 }
223 
224 //-----------------------------------------------------------------------------
225 
226 } // namespace layoutManager
227 } // namespace fwGui
228 
std::vector< ::fwGui::container::fwMenu::sptr > m_menus
All menus managed by this layout.
#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::fwMenuItem::sptr > m_menuItems
All actions managed by this layout.
Defines the toolBar layout manager for IHM.
virtual FWGUIQT_API void destroyLayout() override
Destroy local toolBars.
virtual FWGUIQT_API void menuItemSetVisible(::fwGui::container::fwMenuItem::sptr menuItem, bool isVisible) override
Set the action visibility.
virtual FWGUIQT_API void menuItemSetEnabled(::fwGui::container::fwMenuItem::sptr menuItem, bool isEnabled) override
Set the action enable or not.
#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
virtual FWGUIQT_API void menuItemSetChecked(::fwGui::container::fwMenuItem::sptr, bool isChecked) override
Set the action checked or not.
std::vector< ::fwGui::container::fwContainer::sptr > m_containers
All editors managed by this layout.
std::vector< ActionInfo > m_actionInfo
Save action informations from configuration.
virtual FWGUIQT_API void createLayout(::fwGui::container::fwToolBar::sptr parent) override
Instantiate actions with parent toolBar.
CallbacksType m_callbacks
Callbacks associate with toolBar items.
virtual FWGUI_API void destroyActions()
Helper to destroy local actions.