fw4spl
ToolBarBuilder.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
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/builder/ToolBarBuilder.hpp"
8 #include "fwGuiQt/container/QtContainer.hpp"
9 #include "fwGuiQt/container/QtToolBarContainer.hpp"
10 
11 #include <fwGui/registry/macros.hpp>
12 
13 #include <QHBoxLayout>
14 #include <QMainWindow>
15 #include <QToolBar>
16 
17 
18 fwGuiRegisterMacro( ::fwGui::builder::ToolBarBuilder, ::fwGui::builder::IToolBarBuilder::REGISTRY_KEY);
19 
20 
21 namespace fwGui
22 {
23 namespace builder
24 {
25 
26 //-----------------------------------------------------------------------------
27 
28 ToolBarBuilder::ToolBarBuilder(::fwGui::GuiBaseObject::Key key)
29 {
30 }
31 
32 //-----------------------------------------------------------------------------
33 
34 ToolBarBuilder::~ToolBarBuilder()
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 void ToolBarBuilder::createToolBar( ::fwGui::container::fwContainer::sptr parent )
41 {
42  m_parent = ::fwGuiQt::container::QtContainer::dynamicCast(parent);
43  SLM_ASSERT("The parent container is not a QtContainer", m_parent);
44  QMainWindow *window = qobject_cast<QMainWindow*> ( m_parent->getQtContainer() );
45 
46  QToolBar *toolBar = new QToolBar(QObject::tr("ToolBar"));
47  toolBar->setIconSize( QSize(m_toolBitmapSize.first, m_toolBitmapSize.second) );
48  toolBar->setFloatable(false);
49 
50  ::fwGuiQt::container::QtToolBarContainer::sptr toolBarContainer = ::fwGuiQt::container::QtToolBarContainer::New();
51  if (window)
52  {
53  bool visible = window->isVisible();
54 
55  Qt::ToolBarArea area;
56  switch (m_aligment)
57  {
58  case TOP:
59  area = Qt::TopToolBarArea;
60  break;
61  case BOTTOM:
62  area = Qt::BottomToolBarArea;
63  break;
64  case RIGHT:
65  area = Qt::RightToolBarArea;
66  break;
67  case LEFT:
68  area = Qt::LeftToolBarArea;
69  break;
70  }
71  window->addToolBar( area, toolBar );
72 
73  //on Os X, the window is hidden (???)
74  window->setVisible(visible);
75  }
76  else // parent is not a QMainWindow
77  {
78  QWidget * widget = m_parent->getQtContainer();
79  SLM_ASSERT("Parent container must have a layout", widget->layout());
80  QBoxLayout * layout = qobject_cast<QBoxLayout*> ( widget->layout() );
81  switch (m_aligment)
82  {
83  case TOP:
84  layout->setDirection(QBoxLayout::TopToBottom);
85  toolBar->setOrientation(Qt::Horizontal);
86  break;
87  case BOTTOM:
88  layout->setDirection(QBoxLayout::BottomToTop);
89  toolBar->setOrientation(Qt::Horizontal);
90  break;
91  case RIGHT:
92  layout->setDirection(QBoxLayout::RightToLeft);
93  toolBar->setOrientation(Qt::Vertical);
94  break;
95  case LEFT:
96  layout->setDirection(QBoxLayout::LeftToRight);
97  toolBar->setOrientation(Qt::Vertical);
98  break;
99  }
100  SLM_ASSERT("Parent container layout must have be a QVBoxLayout", layout);
101  layout->insertWidget(0, toolBar, 0);
102  }
103 
104  toolBarContainer->setQtToolBar(toolBar);
105  m_toolBar = toolBarContainer;
106 
107 }
108 
109 //-----------------------------------------------------------------------------
110 
112 {
113  SLM_ASSERT("The ToolBar is not initialized", m_toolBar);
114  SLM_ASSERT("The parent's container is not a QtContainer", m_parent);
115  QMainWindow *window = qobject_cast<QMainWindow*> ( m_parent->getQtContainer() );
116 
117  if (window)
118  {
119  ::fwGuiQt::container::QtToolBarContainer::sptr qtToolBar =
120  ::fwGuiQt::container::QtToolBarContainer::dynamicCast(m_toolBar);
121  QToolBar * toolbar = qtToolBar->getQtToolBar();
122  window->removeToolBar( toolbar );
123  }
124  m_toolBar->destroyContainer();
125 }
126 
127 //-----------------------------------------------------------------------------
128 
129 
130 } // namespace builder
131 } // namespace fwGui
132 
133 
134 
virtual FWGUIQT_API void createToolBar(::fwGui::container::fwContainer::sptr parent) override
Instantiate layout with parent toolBar.
::fwGui::container::fwToolBar::sptr m_toolBar
ToolBar.
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
virtual FWGUIQT_API void destroyToolBar() override
Destroy local layout with sub containers.
Defines the generic layout manager for IHM.
#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