fw4spl
IFrameLayoutManager.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
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/layoutManager/IFrameLayoutManager.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwData/Integer.hpp>
12 #include <fwData/String.hpp>
13 
14 #include <fwPreferences/helper.hpp>
15 
16 #include <fwRuntime/operations.hpp>
17 
18 #include <fwServices/macros.hpp>
19 #include <fwServices/registry/ObjectService.hpp>
20 
21 #include <boost/filesystem/operations.hpp>
22 #include <boost/lexical_cast.hpp>
23 
24 namespace fwGui
25 {
26 
27 namespace layoutManager
28 {
29 //-----------------------------------------------------------------------------
30 
31 const IFrameLayoutManager::RegistryKeyType IFrameLayoutManager::REGISTRY_KEY = "::fwGui::FrameLayoutManager";
32 
33 const std::string IFrameLayoutManager::SOFTWARE_UI = "SOFTWARE_UI";
34 const std::string IFrameLayoutManager::FRAME_STATE_UI = "FRAME_STATE_UI";
35 const std::string IFrameLayoutManager::FRAME_SIZE_W_UI = "FRAME_SIZE_W_UI";
36 const std::string IFrameLayoutManager::FRAME_SIZE_H_UI = "FRAME_SIZE_H_UI";
37 const std::string IFrameLayoutManager::FRAME_POSITION_X_UI = "FRAME_POSITION_X_UI";
38 const std::string IFrameLayoutManager::FRAME_POSITION_Y_UI = "FRAME_POSITION_Y_UI";
39 
40 //-----------------------------------------------------------------------------
41 
43 {
44  CloseCallback fct = std::bind( &::fwGui::layoutManager::IFrameLayoutManager::defaultCloseCallback, this);
45  this->setCloseCallback(fct);
46 }
47 
48 //-----------------------------------------------------------------------------
49 
51 {
52 }
53 
54 //-----------------------------------------------------------------------------
55 
56 void IFrameLayoutManager::initialize( ConfigurationType configuration)
57 {
58  OSLM_ASSERT("Bad configuration name "<<configuration->getName()<< ", must be frame",
59  configuration->getName() == "frame");
60 
61  std::vector < ConfigurationType > name = configuration->find("name");
62  std::vector < ConfigurationType > icon = configuration->find("icon");
63  std::vector < ConfigurationType > minSize = configuration->find("minSize");
64  std::vector < ConfigurationType > styles = configuration->find("style");
65 
66  if(!name.empty())
67  {
68  m_frameInfo.m_name = name.at(0)->getValue();
69  }
70 
71  if(!icon.empty())
72  {
73  m_frameInfo.m_iconPath = ::fwRuntime::getBundleResourceFilePath(icon.at(0)->getValue());
74  OSLM_ASSERT("The icon "<< m_frameInfo.m_iconPath << " doesn't exist, please ensure that the path is correct",
75  ::boost::filesystem::exists(m_frameInfo.m_iconPath));
76  }
77 
78  if(!minSize.empty())
79  {
80  if(minSize.at(0)->hasAttribute("width"))
81  {
82  m_frameInfo.m_minSize.first =
83  ::boost::lexical_cast<int >(minSize.at(0)->getExistingAttributeValue("width"));
84  }
85  if(minSize.at(0)->hasAttribute("height"))
86  {
87  m_frameInfo.m_minSize.second = ::boost::lexical_cast<int >(minSize.at(0)->getExistingAttributeValue(
88  "height"));
89  }
90  }
91 
92  if(!styles.empty())
93  {
94  ::fwRuntime::ConfigurationElement::sptr stylesCfgElt = styles.at(0);
95  SLM_FATAL_IF("<style> node must contain mode attribute", !stylesCfgElt->hasAttribute("mode") );
96  const std::string style = stylesCfgElt->getExistingAttributeValue("mode");
97 
98  if (style == "DEFAULT")
99  {
100  m_frameInfo.m_style = DEFAULT;
101  }
102  else if (style == "STAY_ON_TOP")
103  {
104  m_frameInfo.m_style = STAY_ON_TOP;
105  }
106  else if (style == "MODAL")
107  {
108  m_frameInfo.m_style = MODAL;
109  }
110  else
111  {
112  OSLM_FATAL("The style "<<style<< " is unknown, it should be DEFAULT, STAY_ON_TOP or MODAL.");
113  }
114  }
115  this->readConfig();
116 }
117 
118 //-----------------------------------------------------------------------------
119 
120 void IFrameLayoutManager::setCloseCallback(CloseCallback fct)
121 {
122  this->m_closeCallback = fct;
123 }
124 
125 //-----------------------------------------------------------------------------
126 
127 void IFrameLayoutManager::defaultCloseCallback()
128 {
129  SLM_WARN("No specific close callback defined");
130 }
131 
132 //-----------------------------------------------------------------------------
133 
134 void IFrameLayoutManager::readConfig()
135 {
136  ::fwData::Composite::sptr prefUI = this->getPreferenceUI();
137  if(prefUI)
138  {
139  if ( prefUI->find( IFrameLayoutManager::FRAME_STATE_UI ) != prefUI->end() )
140  {
141  ::fwData::Integer::sptr state =
142  ::fwData::Integer::dynamicCast( (*prefUI)[ IFrameLayoutManager::FRAME_STATE_UI ] );
143  SLM_ASSERT("UI state not correct", state);
144  m_frameInfo.m_state = (FrameState) state->value();
145  }
146  if ( prefUI->find( IFrameLayoutManager::FRAME_SIZE_W_UI ) != prefUI->end() )
147  {
148  ::fwData::Integer::sptr sizew =
149  ::fwData::Integer::dynamicCast( (*prefUI)[ IFrameLayoutManager::FRAME_SIZE_W_UI ] );
150  SLM_ASSERT("UI sizeW not correct", sizew);
151  m_frameInfo.m_size.first = *sizew;
152  }
153  if ( prefUI->find( IFrameLayoutManager::FRAME_SIZE_H_UI ) != prefUI->end() )
154  {
155  ::fwData::Integer::sptr sizeh =
156  ::fwData::Integer::dynamicCast( (*prefUI)[ IFrameLayoutManager::FRAME_SIZE_H_UI ] );
157  SLM_ASSERT("UI sizeH not correct", sizeh);
158  m_frameInfo.m_size.second = *sizeh;
159  }
160  if ( prefUI->find( IFrameLayoutManager::FRAME_POSITION_X_UI ) != prefUI->end() )
161  {
162  ::fwData::Integer::sptr posx =
163  ::fwData::Integer::dynamicCast( (*prefUI)[ IFrameLayoutManager::FRAME_POSITION_X_UI ] );
164  SLM_ASSERT("UI posX not correct", posx);
165  m_frameInfo.m_position.first = *posx;
166  }
167  if ( prefUI->find( IFrameLayoutManager::FRAME_POSITION_Y_UI ) != prefUI->end() )
168  {
169  ::fwData::Integer::sptr posy =
170  ::fwData::Integer::dynamicCast( (*prefUI)[ IFrameLayoutManager::FRAME_POSITION_Y_UI ] );
171  SLM_ASSERT("UI posY not correct", posy);
172  m_frameInfo.m_position.second = *posy;
173  }
174  }
175 }
176 
177 //-----------------------------------------------------------------------------
178 
179 void IFrameLayoutManager::writeConfig()
180 {
181  ::fwData::Composite::sptr prefUI = this->getPreferenceUI();
182  if(prefUI)
183  {
184  if(m_frameInfo.m_state != ICONIZED)
185  {
186  ::fwData::Integer::sptr state = ::fwData::Integer::New(m_frameInfo.m_state);
187  (*prefUI)[ IFrameLayoutManager::FRAME_STATE_UI ] = state;
188  }
189 
190  ::fwData::Integer::sptr sizew = ::fwData::Integer::New(m_frameInfo.m_size.first);
191  (*prefUI)[ IFrameLayoutManager::FRAME_SIZE_W_UI ] = sizew;
192 
193  ::fwData::Integer::sptr sizeh = ::fwData::Integer::New(m_frameInfo.m_size.second);
194  (*prefUI)[ IFrameLayoutManager::FRAME_SIZE_H_UI ] = sizeh;
195 
196  ::fwData::Integer::sptr posx = ::fwData::Integer::New(m_frameInfo.m_position.first);
197  (*prefUI)[ IFrameLayoutManager::FRAME_POSITION_X_UI ] = posx;
198 
199  ::fwData::Integer::sptr posy = ::fwData::Integer::New(m_frameInfo.m_position.second);
200  (*prefUI)[ IFrameLayoutManager::FRAME_POSITION_Y_UI ] = posy;
201  }
202 }
203 
204 //-----------------------------------------------------------------------------
205 
206 ::fwData::Composite::sptr IFrameLayoutManager::getPreferenceUI()
207 {
208  ::fwData::Composite::sptr prefUI;
209  ::fwData::Composite::sptr prefs = ::fwPreferences::getPreferences();
210 
211  // Get preferences
212  if(prefs)
213  {
214  ::fwData::Composite::sptr framesUI;
215  // Retreives software UI pref
216  if ( prefs->find( IFrameLayoutManager::SOFTWARE_UI ) == prefs->end() )
217  {
218  framesUI = ::fwData::Composite::New();
219  (*prefs)[ IFrameLayoutManager::SOFTWARE_UI ] = framesUI;
220  }
221  else
222  {
223  framesUI = ::fwData::Composite::dynamicCast( (*prefs)[ IFrameLayoutManager::SOFTWARE_UI ]);
224  }
225  // Retreives frame UI pref
226  if ( framesUI->find( this->m_frameInfo.m_name ) != framesUI->end() )
227  {
228  prefUI = ::fwData::Composite::dynamicCast( (*framesUI)[ this->m_frameInfo.m_name ] );
229  }
230  else
231  {
232  prefUI = ::fwData::Composite::New();
233  (*framesUI)[ this->m_frameInfo.m_name ] = prefUI;
234  }
235  }
236  return prefUI;
237 }
238 
239 //-----------------------------------------------------------------------------
240 
241 } // namespace layoutManager
242 } // namespace fwGui
#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
FWGUI_API IFrameLayoutManager()
Constructor. Do nothing.
#define SLM_WARN(message)
Definition: spyLog.hpp:261
virtual FWGUI_API void initialize(ConfigurationType configuration)
Configure the layout before creation.
std::pair< int, int > m_minSize
Frame minimum size (min width and min height)
#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 FWGUI_API ~IFrameLayoutManager()
Destructor. Do nothing.
std::pair< int, int > m_position
Frame position.
#define SLM_FATAL_IF(message, cond)
Definition: spyLog.hpp:287
#define OSLM_FATAL(message)
Definition: spyLog.hpp:285
::boost::filesystem::path m_iconPath
Frame icon.
FrameState m_state
Frame state (maximize, minized, full screen)