fw4spl
ViewRegistrar.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/registrar/ViewRegistrar.hpp"
8 
9 #include "fwGui/GuiRegistry.hpp"
10 #include "fwGui/container/fwContainer.hpp"
11 #include "fwGui/container/fwMenuBar.hpp"
12 #include "fwGui/container/fwToolBar.hpp"
13 
14 #include <fwServices/macros.hpp>
15 #include <fwServices/op/Get.hpp>
16 
17 #include <utility>
18 
19 namespace fwGui
20 {
21 namespace registrar
22 {
23 
24 //-----------------------------------------------------------------------------
25 
26 ViewRegistrar::ViewRegistrar(const std::string& sid) :
27  m_parentWid(""),
28  m_sid(sid)
29 {
30 }
31 
32 //-----------------------------------------------------------------------------
33 
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 ::fwGui::container::fwContainer::sptr ViewRegistrar::getParent()
41 {
42  ::fwGui::container::fwContainer::sptr parentContainer;
43  if(!m_parentWid.empty())
44  {
45  parentContainer = ::fwGui::GuiRegistry::getWIDContainer(m_parentWid);
46  OSLM_ASSERT("No fwContainer with the wid '"<< m_parentWid <<"' exists in the WID container map.",
47  parentContainer);
48  }
49  else
50  {
52  OSLM_ASSERT("No fwContainer with the sid '"<< m_sid <<"' exists in the SID container map.", parentContainer );
53  }
54  return parentContainer;
55 }
56 
57 //-----------------------------------------------------------------------------
58 
59 void ViewRegistrar::setParent(std::string wid)
60 {
61  OSLM_ASSERT("This method is available only if this container has a WID parent container", !m_parentWid.empty());
62  m_parentWid = wid;
63 }
64 
65 //-----------------------------------------------------------------------------
66 
67 void ViewRegistrar::initialize( ::fwRuntime::ConfigurationElement::sptr configuration)
68 {
69  OSLM_ASSERT("Bad configuration name "<<configuration->getName()<< ", must be viewRegistrar",
70  configuration->getName() == "registry");
71 
72  // find parent container
73  std::vector < ConfigurationType > vectParent = configuration->find("parent");
74  if(!vectParent.empty())
75  {
76  ConfigurationType parent = vectParent.at(0);
77  SLM_ASSERT("<parent> tag must have wid attribute", parent->hasAttribute("wid"));
78  m_parentWid = parent->getAttributeValue("wid");
79  }
80 
81  // index represents associated container with position in subViews vector
82  unsigned int index = 0;
83  // initialize m_sids and m_wids map with configuration
84  std::vector < ConfigurationType > vectViews = configuration->find("view");
85  std::vector < ConfigurationType > vectSlideViews = configuration->find("slideView");
86  std::copy(vectSlideViews.begin(), vectSlideViews.end(), std::back_inserter(vectViews));
87 
88  for( ConfigurationType view : vectViews)
89  {
90  SLM_ASSERT("<view> tag must have sid or wid attribute",
91  view->hasAttribute("sid") || view->hasAttribute("wid"));
92  if(view->hasAttribute("sid"))
93  {
94  bool start = false;
95  if(view->hasAttribute("start"))
96  {
97  std::string startValue = view->getAttributeValue("start");
98  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
99  startValue == "yes" || startValue == "no");
100  start = (startValue == "yes");
101  }
102  std::string sid = view->getAttributeValue("sid");
103  m_sids[sid] = SIDContainerMapType::mapped_type(index, start);
104  }
105  else if(view->hasAttribute("wid"))
106  {
107  std::string wid = view->getAttributeValue("wid");
108  m_wids[wid] = index;
109  }
110  index++;
111  }
112 
113  // find menuBar
114  std::vector < ConfigurationType > vectmenuBar = configuration->find("menuBar");
115  if(!vectmenuBar.empty())
116  {
117  ConfigurationType menuBarCfg = vectmenuBar.at(0);
118  if (menuBarCfg->hasAttribute("sid"))
119  {
120  bool start = false;
121  if (menuBarCfg->hasAttribute("start"))
122  {
123  std::string startValue = menuBarCfg->getAttributeValue("start");
124  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
125  startValue == "yes" || startValue == "no");
126  start = (startValue == "yes");
127  }
128  std::string sid = menuBarCfg->getAttributeValue("sid");
129  m_menuBarSid = std::make_pair( sid, start);
130  }
131  }
132 
133  // find toolBar
134  std::vector < ConfigurationType > vectToolBar = configuration->find("toolBar");
135  if(!vectToolBar.empty())
136  {
137  ConfigurationType toolBarCfg = vectToolBar.at(0);
138  if (toolBarCfg->hasAttribute("sid"))
139  {
140  bool start = false;
141  if (toolBarCfg->hasAttribute("start"))
142  {
143  std::string startValue = toolBarCfg->getAttributeValue("start");
144  SLM_ASSERT("Wrong value '"<< startValue <<"' for 'start' attribute (require yes or no)",
145  startValue == "yes" || startValue == "no");
146  start = (startValue == "yes");
147  }
148  std::string sid = toolBarCfg->getAttributeValue("sid");
149  m_toolBarSid = std::make_pair( sid, start);
150  }
151  }
152 }
153 
154 //-----------------------------------------------------------------------------
155 
156 void ViewRegistrar::manage(std::vector< ::fwGui::container::fwContainer::sptr > subViews )
157 {
158  ::fwGui::container::fwContainer::sptr container;
159  for( SIDContainerMapType::value_type sid : m_sids)
160  {
161  OSLM_ASSERT("Container index "<< sid.second.first <<" is bigger than subViews size!",
162  sid.second.first < subViews.size());
163  container = subViews.at( sid.second.first );
164  ::fwGui::GuiRegistry::registerSIDContainer(sid.first, container);
165  if(sid.second.second) //service is auto started?
166  {
167  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
168  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
169  OSLM_ASSERT("Service "<<sid.first <<" must be stopped.", service->isStopped() );
170  SLM_TRACE("Starting " + sid.first);
171  service->start();
172  }
173  }
174 
175  for( WIDContainerMapType::value_type wid : m_wids)
176  {
177  OSLM_ASSERT("Container index "<< wid.second <<" is bigger than subViews size!", wid.second < subViews.size());
178  container = subViews.at( wid.second );
179  ::fwGui::GuiRegistry::registerWIDContainer(wid.first, container);
180  }
181 }
182 
183 //-----------------------------------------------------------------------------
184 
185 void ViewRegistrar::manageMenuBar(::fwGui::container::fwMenuBar::sptr menuBar )
186 {
188  if(m_menuBarSid.second) //service is auto started?
189  {
190  OSLM_ASSERT("Service "<<m_menuBarSid.first <<" does not exist.", ::fwTools::fwID::exist(m_menuBarSid.first ) );
191  ::fwServices::IService::sptr service = ::fwServices::get( m_menuBarSid.first );
192  service->start();
193  }
194 }
195 
196 //-----------------------------------------------------------------------------
197 
198 void ViewRegistrar::manageToolBar(::fwGui::container::fwToolBar::sptr toolBar )
199 {
201  if(m_toolBarSid.second) //service is auto started?
202  {
203  OSLM_ASSERT("Service "<<m_toolBarSid.first <<" does not exist.", ::fwTools::fwID::exist(m_toolBarSid.first ) );
204  ::fwServices::IService::sptr service = ::fwServices::get( m_toolBarSid.first );
205  service->start();
206  }
207 }
208 
209 //-----------------------------------------------------------------------------
210 
212 {
213  for( SIDContainerMapType::value_type sid : m_sids)
214  {
215  if(sid.second.second) //service is auto started?
216  {
217  OSLM_ASSERT("Service "<<sid.first <<" does not exist.", ::fwTools::fwID::exist(sid.first ) );
218  ::fwServices::IService::sptr service = ::fwServices::get( sid.first );
219  SLM_TRACE("Stopping " + sid.first);
220  service->stop();
221  }
223  }
224 
225  for( WIDContainerMapType::value_type wid : m_wids)
226  {
228  }
229 }
230 
231 //-----------------------------------------------------------------------------
232 
234 {
235  if ( !m_toolBarSid.first.empty() )
236  {
237  if(m_toolBarSid.second) //service is auto started?
238  {
239  OSLM_ASSERT("Service "<<m_toolBarSid.first <<" does not exist.",
241  ::fwServices::IService::sptr service = ::fwServices::get( m_toolBarSid.first );
242  service->stop();
243  }
245  }
246 }
247 
248 //-----------------------------------------------------------------------------
249 
251 {
252  if ( !m_menuBarSid.first.empty() )
253  {
254  if(m_menuBarSid.second) //service is auto started?
255  {
256  OSLM_ASSERT("Service "<<m_menuBarSid.first <<" does not exist.",
258  ::fwServices::IService::sptr service = ::fwServices::get( m_menuBarSid.first );
259  service->stop();
260  }
262  }
263 }
264 
265 //-----------------------------------------------------------------------------
266 
267 } // namespace registrar
268 } //namespace fwGui
static FWGUI_API void registerSIDToolBar(std::string sid,::fwGui::container::fwToolBar::sptr toolBar)
Registers fwToolBar associate with service ID.
static FWGUI_API::fwGui::container::fwContainer::sptr getWIDContainer(std::string wid)
Returns fwContainer associate with window ID, null if not found.
static FWGUI_API void unregisterWIDContainer(std::string wid)
Unregisters container associate with window ID.
Definition: GuiRegistry.cpp:96
virtual FWGUI_API void unmanage()
Stopping view manager. All services managed in local subViews will be stopped.
#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
SIDToolBarPairType m_toolBarSid
Tool bar service ID associate with this ViewRegistrar.
std::string m_sid
Main service ID associate with this ViewRegistrar.
static FWTOOLS_API bool exist(IDType _id)
Definition: fwID.cpp:33
static FWGUI_API void unregisterSIDContainer(std::string sid)
Unregisters container associate with service ID.
Definition: GuiRegistry.cpp:48
static FWGUI_API void registerSIDContainer(std::string sid,::fwGui::container::fwContainer::sptr container)
Registers container associate with service ID.
Definition: GuiRegistry.cpp:39
static FWGUI_API void unregisterSIDToolBar(std::string sid)
Unregisters fwToolBar associate with service ID.
SIDContainerMapType m_sids
All services ID managed and associated with pair containing: subViews index vector and boolean descri...
static FWGUI_API::fwGui::container::fwContainer::sptr getSIDContainer(std::string sid)
Returns fwContainer associate with service ID, null if not found.
Definition: GuiRegistry.cpp:67
static FWGUI_API void registerSIDMenuBar(std::string sid,::fwGui::container::fwMenuBar::sptr menuBar)
Registers fwMenuBar associate with service ID.
virtual FWGUI_API void unmanageMenuBar()
Stopping view manager. MenuBar service will be stopped.
static FWGUI_API void registerWIDContainer(std::string wid,::fwGui::container::fwContainer::sptr container)
Registers container associate with window ID.
Definition: GuiRegistry.cpp:87
FWGUI_API ViewRegistrar(const std::string &sid)
Constructor.
virtual FWGUI_API void manageMenuBar(std::shared_ptr< ::fwGui::container::fwMenuBar > menuBar)
Register menu bar. If start="yes" in configuration the menu bar services will be started.
#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
WIDContainerMapType m_wids
All windows containers ID managed (and associated with subViews index vector).
static FWGUI_API void unregisterSIDMenuBar(std::string sid)
Unregisters fwMenuBar associate with service ID.
#define SLM_TRACE(message)
Definition: spyLog.hpp:228
virtual FWGUI_API ~ViewRegistrar()
Destructor. Do nothing.
SIDMenuBarPairType m_menuBarSid
Menu bar service ID associate with this ViewRegistrar.
virtual FWGUI_API void initialize(::fwRuntime::ConfigurationElement::sptr configuration)
Initialize managers.
virtual FWGUI_API void manage(std::vector< std::shared_ptr< ::fwGui::container::fwContainer > > subViews)
Starting view manager. All services managed in local subViews and with start="yes" in configuration w...
virtual FWGUI_API void unmanageToolBar()
Stopping view manager. ToolBar service will be stopped.
virtual FWGUI_API void manageToolBar(std::shared_ptr< ::fwGui::container::fwToolBar > toolBar)
Register tool bar. If start="yes" in configuration the tool bar services will be started.