fw4spl
IFrameSrv.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 "fwGui/IFrameSrv.hpp"
8 
9 #include "fwGui/Application.hpp"
10 
11 #include <fwCom/Signal.hpp>
12 #include <fwCom/Signal.hxx>
13 #include <fwCom/Slot.hxx>
14 #include <fwCom/Slots.hxx>
15 
16 #include <fwServices/macros.hpp>
17 #include <fwServices/registry/ActiveWorkers.hpp>
18 
19 #include <fwThread/Worker.hpp>
20 #include <fwThread/Worker.hxx>
21 
22 #include <fwTools/fwID.hpp>
23 
24 #include <boost/foreach.hpp>
25 #include <boost/lambda/lambda.hpp>
26 
27 namespace fwGui
28 {
29 
30 const std::string IFrameSrv::CLOSE_POLICY_EXIT = "exit";
31 const std::string IFrameSrv::CLOSE_POLICY_NOTIFY = "notify";
32 const std::string IFrameSrv::CLOSE_POLICY_MESSAGE = "message";
33 
34 const ::fwCom::Slots::SlotKeyType IFrameSrv::s_SET_VISIBLE_SLOT = "setVisible";
35 const ::fwCom::Slots::SlotKeyType IFrameSrv::s_SHOW_SLOT = "show";
36 const ::fwCom::Slots::SlotKeyType IFrameSrv::s_HIDE_SLOT = "hide";
37 
38 const ::fwCom::Signals::SignalKeyType IFrameSrv::s_CLOSED_SIG = "closed";
39 
40 ::fwGui::container::fwContainer::wptr IFrameSrv::m_progressWidget =
41  std::weak_ptr< ::fwGui::container::fwContainer >();
42 
43 IFrameSrv::IFrameSrv() :
44  m_hasMenuBar(false),
45  m_hasToolBar(false),
46  m_closePolicy("exit")
47 {
48  m_sigClosed = newSignal< ClosedSignalType >(s_CLOSED_SIG);
49 
50  newSlot(s_SET_VISIBLE_SLOT, &IFrameSrv::setVisible, this);
51  newSlot(s_SHOW_SLOT, &IFrameSrv::show, this);
52  newSlot(s_HIDE_SLOT, &IFrameSrv::hide, this);
53 }
54 
55 //-----------------------------------------------------------------------------
56 
57 IFrameSrv::~IFrameSrv()
58 {
59 }
60 
61 //-----------------------------------------------------------------------------
62 
64 {
65  // find gui configuration
66  std::vector < ConfigurationType > vectGui = m_configuration->find("gui");
67  std::vector < ConfigurationType > vectWindow = m_configuration->find("window");
68 
69  if(!vectGui.empty())
70  {
71  // find LayoutManager configuration
72  std::vector < ConfigurationType > vectLayoutMng = vectGui.at(0)->find("frame");
73  SLM_ASSERT("<frame> xml element must exist", !vectLayoutMng.empty());
74  m_frameConfig = vectLayoutMng.at(0);
75  this->initializeLayoutManager(m_frameConfig);
76 
77  // find menuBarBuilder configuration
78  std::vector < ConfigurationType > vectMBBuilder = vectGui.at(0)->find("menuBar");
79  if(!vectMBBuilder.empty())
80  {
81  m_menuBarConfig = vectMBBuilder.at(0);
82  this->initializeMenuBarBuilder(m_menuBarConfig);
83 
84  m_hasMenuBar = true;
85  }
86 
87  // find toolBarBuilder configuration
88  std::vector < ConfigurationType > vectTBBuilder = vectGui.at(0)->find("toolBar");
89  if(!vectTBBuilder.empty())
90  {
91  m_toolBarConfig = vectTBBuilder.at(0);
92  this->initializeToolBarBuilder(m_toolBarConfig);
93 
94  m_hasToolBar = true;
95  }
96  }
97 
98  if(!vectWindow.empty())
99  {
100  ConfigurationType window = vectWindow.at(0);
101  std::string onclose = window->getAttributeValue("onclose");
102  if ( !onclose.empty() )
103  {
104  m_closePolicy = onclose;
105  }
106  SLM_ASSERT("Invalid onclose value : " << m_closePolicy << ". Should be 'exit', 'notify' or 'message'",
107  m_closePolicy == CLOSE_POLICY_NOTIFY || m_closePolicy == CLOSE_POLICY_EXIT
108  || m_closePolicy == CLOSE_POLICY_MESSAGE);
109  }
110 
111  m_viewRegistrar = ::fwGui::registrar::ViewRegistrar::New(this->getID());
112  // find ViewRegistryManager configuration
113  std::vector < ConfigurationType > vectRegistrar = m_configuration->find("registry");
114  if(!vectRegistrar.empty())
115  {
116  m_registrarConfig = vectRegistrar.at(0);
117  m_viewRegistrar->initialize(m_registrarConfig);
118  }
119 }
120 
121 //-----------------------------------------------------------------------------
122 
124 {
125  SLM_ASSERT("FrameLayoutManager must be initialized.", m_frameLayoutManager);
126 
127  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
128  {
129  m_frameLayoutManager->createFrame();
130  })).wait();
131 
132  ::fwGui::container::fwContainer::sptr frame = m_frameLayoutManager->getFrame();
133  if ( m_progressWidget.expired() )
134  {
135  m_progressWidget = frame;
136  }
137 
138  ::fwGui::container::fwContainer::sptr container = m_frameLayoutManager->getContainer();
139  std::vector< ::fwGui::container::fwContainer::sptr > subViews;
140  subViews.push_back(container);
141  m_viewRegistrar->manage(subViews);
142 
143  ::fwGui::layoutManager::IFrameLayoutManager::CloseCallback fct;
144 
145  if (m_closePolicy == CLOSE_POLICY_EXIT)
146  {
147  fct = std::bind( &::fwGui::IFrameSrv::onCloseExit, this);
148  }
149  else if (m_closePolicy == CLOSE_POLICY_NOTIFY)
150  {
151  fct = std::bind( &::fwGui::IFrameSrv::onCloseNotify, this);
152  }
153  else if(m_closePolicy == CLOSE_POLICY_MESSAGE)
154  {
155  fct = std::bind( &::fwGui::IFrameSrv::onCloseMessage, this);
156  auto app = ::fwGui::Application::New();
157  app->setConfirm(true);
158  }
159 
160  m_frameLayoutManager->setCloseCallback(fct);
161 
162  if (m_hasMenuBar)
163  {
164  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
165  {
166  m_menuBarBuilder->createMenuBar(frame);
167  })).wait();
168 
169  m_viewRegistrar->manageMenuBar(m_menuBarBuilder->getMenuBar());
170  }
171 
172  if (m_hasToolBar)
173  {
174  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
175  {
176  m_toolBarBuilder->createToolBar(frame);
177  })).wait();
178 
179  m_viewRegistrar->manageToolBar(m_toolBarBuilder->getToolBar());
180  }
181 }
182 
183 //-----------------------------------------------------------------------------
184 
186 {
187  SLM_ASSERT("ViewRegistrar must be initialized.", m_viewRegistrar);
188 
189  if (m_hasToolBar)
190  {
191  m_viewRegistrar->unmanageToolBar();
192  SLM_ASSERT("ToolBarBuilder must be initialized.", m_toolBarBuilder);
193 
194  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
195  {
196  m_toolBarBuilder->destroyToolBar();
197  })).wait();
198  }
199 
200  if (m_hasMenuBar)
201  {
202  m_viewRegistrar->unmanageMenuBar();
203  SLM_ASSERT("MenuBarBuilder must be initialized.", m_menuBarBuilder);
204 
205  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
206  {
207  m_menuBarBuilder->destroyMenuBar();
208  })).wait();
209  }
210 
211  m_viewRegistrar->unmanage();
212  SLM_ASSERT("FrameLayoutManager must be initialized.", m_frameLayoutManager);
213 
214  ::fwServices::registry::ActiveWorkers::getDefaultWorker()->postTask<void>(std::function< void() >([&]
215  {
216  m_frameLayoutManager->destroyFrame();
217  })).wait();
218 }
219 
220 //-----------------------------------------------------------------------------
221 
222 void IFrameSrv::initializeLayoutManager(ConfigurationType frameConfig)
223 {
224  OSLM_ASSERT("Bad configuration name "<<frameConfig->getName()<< ", must be frame",
225  frameConfig->getName() == "frame");
226  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(
227  ::fwGui::layoutManager::IFrameLayoutManager::REGISTRY_KEY);
228  m_frameLayoutManager = ::fwGui::layoutManager::IFrameLayoutManager::dynamicCast(guiObj);
229  OSLM_ASSERT("ClassFactoryRegistry failed for class "<< ::fwGui::layoutManager::IFrameLayoutManager::REGISTRY_KEY,
230  m_frameLayoutManager);
231 
232  m_frameLayoutManager->initialize(frameConfig);
233 }
234 
235 //-----------------------------------------------------------------------------
236 
237 void IFrameSrv::initializeMenuBarBuilder(ConfigurationType menuBarConfig)
238 {
239  OSLM_ASSERT("Bad configuration name "<<menuBarConfig->getName()<< ", must be menuBar",
240  menuBarConfig->getName() == "menuBar");
241 
242  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(::fwGui::builder::IMenuBarBuilder::REGISTRY_KEY);
243  m_menuBarBuilder = ::fwGui::builder::IMenuBarBuilder::dynamicCast(guiObj);
244  OSLM_ASSERT("ClassFactoryRegistry failed for class "<< ::fwGui::builder::IMenuBarBuilder::REGISTRY_KEY,
245  m_menuBarBuilder);
246 
247  m_menuBarBuilder->initialize(menuBarConfig);
248 }
249 
250 //-----------------------------------------------------------------------------
251 
252 void IFrameSrv::initializeToolBarBuilder(ConfigurationType toolBarConfig)
253 {
254  OSLM_ASSERT("Bad configuration name "<<toolBarConfig->getName()<< ", must be toolBar",
255  toolBarConfig->getName() == "toolBar");
256 
257  ::fwGui::GuiBaseObject::sptr guiObj = ::fwGui::factory::New(::fwGui::builder::IToolBarBuilder::REGISTRY_KEY);
258  m_toolBarBuilder = ::fwGui::builder::IToolBarBuilder::dynamicCast(guiObj);
259  OSLM_ASSERT("ClassFactoryRegistry failed for class "<< ::fwGui::builder::IToolBarBuilder::REGISTRY_KEY,
260  m_toolBarBuilder);
261 
262  m_toolBarBuilder->initialize(toolBarConfig);
263 }
264 
265 //-----------------------------------------------------------------------------
266 
267 void IFrameSrv::onCloseExit()
268 {
269  SLM_TRACE_FUNC();
270  ::fwGui::Application::New()->exit(0);
271 }
272 
273 //-----------------------------------------------------------------------------
274 
275 void IFrameSrv::onCloseNotify()
276 {
277  SLM_TRACE_FUNC();
278  m_sigClosed->asyncEmit();
279 }
280 
281 //-----------------------------------------------------------------------------
282 
283 void IFrameSrv::onCloseMessage()
284 {
285  SLM_TRACE_FUNC();
286  auto app = ::fwGui::Application::New();
287  app->exit(0);
288 }
289 
290 //-----------------------------------------------------------------------------
291 
292 ::fwGui::container::fwContainer::sptr IFrameSrv::getProgressWidget()
293 {
294  return m_progressWidget.lock();
295 }
296 
297 //-----------------------------------------------------------------------------
298 
299 void IFrameSrv::setVisible(bool isVisible)
300 {
301  ::fwGui::container::fwContainer::sptr container = m_frameLayoutManager->getFrame();
302  container->setVisible(isVisible);
303 }
304 
305 //-----------------------------------------------------------------------------
306 
307 void IFrameSrv::show()
308 {
309  this->setVisible(true);
310 }
311 
312 //-----------------------------------------------------------------------------
313 
314 void IFrameSrv::hide()
315 {
316  this->setVisible(false);
317 }
318 
319 //-----------------------------------------------------------------------------
320 
321 }
FWGUI_API void create()
Creates frame, sub-view, menubar and toolbar containers. Manages sub-view, menubar and toobar service...
Definition: IFrameSrv.cpp:123
#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
static const ::fwCom::Slots::SlotKeyType s_SHOW_SLOT
Slot to show the container.
Definition: IFrameSrv.hpp:98
FWGUI_API void destroy()
Stops sub-view, menubar and toobar services. Destroys frame, sub-view, menubar and toolbar containers...
Definition: IFrameSrv.cpp:185
FWGUI_API void initialize()
Initialize frame managers.
Definition: IFrameSrv.cpp:63
static const ::fwCom::Slots::SlotKeyType s_HIDE_SLOT
Slot to hide the container.
Definition: IFrameSrv.hpp:101
#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
static FWGUI_API::fwGui::container::fwContainer::sptr getProgressWidget()
Get widget defined for progress bar.
Definition: IFrameSrv.cpp:292
static const ::fwCom::Signals::SignalKeyType s_CLOSED_SIG
Signal emitted when frame is closed and onclose policy is notify.
Definition: IFrameSrv.hpp:83
static FWSERVICES_API::fwThread::Worker::sptr getDefaultWorker()
Get the default registered worker.
static const ::fwCom::Slots::SlotKeyType s_SET_VISIBLE_SLOT
Slot to show/hide the container.
Definition: IFrameSrv.hpp:95
static FWGUI_API::fwGui::container::fwContainer::wptr m_progressWidget
Static reference on a widget defined for progress bar installation.
Definition: IFrameSrv.hpp:132