fw4spl
ILocationDialog.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/dialog/ILocationDialog.hpp"
8 
9 #include <fwData/location/Folder.hpp>
10 #include <fwData/location/SingleFile.hpp>
11 
12 #include <fwPreferences/helper.hpp>
13 
14 #include <fwServices/IService.hpp>
15 #include <fwServices/registry/ObjectService.hpp>
16 
17 namespace fwGui
18 {
19 namespace dialog
20 {
21 
22 const ILocationDialog::FactoryRegistryKeyType ILocationDialog::REGISTRY_KEY = "::fwGui::dialog::LocationDialog";
23 
24 const std::string ILocationDialog::SOFTWARE_UI = "SOFTWARE_UI";
25 const std::string ILocationDialog::DLG_DEFAULT_LOCATION = "DLG_DEFAULT_LOCATION";
26 
27 //-----------------------------------------------------------------------------
28 
29 ILocationDialog::ILocationDialog()
30 {
31 }
32 
33 //-----------------------------------------------------------------------------
34 
35 ILocationDialog::~ILocationDialog()
36 {
37 }
38 
39 //------------------------------------------------------------------------------
40 
41 void ILocationDialog::setTitle(const std::string& title)
42 {
43  m_title = title;
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 const std::string& ILocationDialog::getTitle()
49 {
50  return m_title;
51 }
52 
53 //------------------------------------------------------------------------------
54 
55 void ILocationDialog::setDefaultLocation( ::fwData::location::ILocation::sptr loc)
56 {
57  ::fwData::location::SingleFile::csptr singleFile = ::fwData::location::SingleFile::dynamicConstCast(loc);
58  ::fwData::location::Folder::csptr folder = ::fwData::location::Folder::dynamicConstCast(loc);
59  SLM_FATAL_IF( "Unsupported location", !singleFile && !folder );
60  m_defaultLocaction = loc;
61 }
62 
63 //------------------------------------------------------------------------------
64 
65 const ::boost::filesystem::path ILocationDialog::getDefaultLocation()
66 {
67  ::boost::filesystem::path defaultPath;
68  ::fwData::Composite::sptr prefUI = this->getPreferenceUI();
69  ::fwData::location::ILocation::sptr location;
70  if(prefUI)
71  {
72  if ( prefUI->find( ILocationDialog::DLG_DEFAULT_LOCATION ) != prefUI->end() )
73  {
74  location = ::fwData::location::ILocation::dynamicCast( (*prefUI)[ ILocationDialog::DLG_DEFAULT_LOCATION ] );
75  SLM_ASSERT("LOCATION not correct", location);
76  }
77  }
78 
79  if(!location)
80  {
81  location = m_defaultLocaction;
82  }
83 
84  ::fwData::location::SingleFile::csptr singleFile = ::fwData::location::SingleFile::dynamicConstCast(location);
85  ::fwData::location::Folder::csptr folder = ::fwData::location::Folder::dynamicConstCast(location);
86  if (singleFile)
87  {
88  defaultPath = singleFile->getPath();
89  }
90  else if (folder)
91  {
92  defaultPath = folder->getFolder();
93  }
94 
95  return defaultPath;
96 }
97 
98 //-----------------------------------------------------------------------------
99 
100 void ILocationDialog::saveDefaultLocation(::fwData::location::ILocation::sptr loc)
101 {
102  ::fwData::Composite::sptr prefUI = this->getPreferenceUI();
103  if(prefUI && loc)
104  {
105  (*prefUI)[ ILocationDialog::DLG_DEFAULT_LOCATION ] = loc;
106  }
107 }
108 
109 //-----------------------------------------------------------------------------
110 
111 ::fwData::Composite::sptr ILocationDialog::getPreferenceUI()
112 {
113  ::fwData::Composite::sptr prefUI;
114 
115  // Get preferences
116  ::fwData::Composite::sptr prefs = ::fwPreferences::getPreferences();
117  if(prefs)
118  {
119  ::fwData::Composite::sptr framesUI;
120  // Retrieves software UI pref
121  if ( prefs->find( ILocationDialog::SOFTWARE_UI ) != prefs->end() )
122  {
123  framesUI = ::fwData::Composite::dynamicCast( (*prefs)[ ILocationDialog::SOFTWARE_UI ]);
124  }
125  else
126  {
127  framesUI = ::fwData::Composite::New();
128  (*prefs)[ ILocationDialog::SOFTWARE_UI ] = framesUI;
129  }
130  // Retrieves associated dialog UI pref
131  if ( framesUI->find( this->getTitle() ) != framesUI->end() )
132  {
133  prefUI = ::fwData::Composite::dynamicCast( (*framesUI)[ this->getTitle() ] );
134  }
135  else
136  {
137  prefUI = ::fwData::Composite::New();
138  (*framesUI)[ this->getTitle() ] = prefUI;
139  }
140  }
141  return prefUI;
142 }
143 
144 //-----------------------------------------------------------------------------
145 
146 } //namespace dialog
147 } // namespace fwGui
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc)
set the initial location for the dialog
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
virtual FWGUI_APIconst::boost::filesystem::path getDefaultLocation()
get the default location for the dialog (from preferences or specified by user)
virtual FWGUI_API void setTitle(const std::string &title)
set the title for the dialog
#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 const FactoryRegistryKeyType REGISTRY_KEY
this unique key should be used for all factory for specific LocationDialog(qt,wx,...)
#define SLM_FATAL_IF(message, cond)
Definition: spyLog.hpp:287
virtual FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc)
save the specified default location for the dialog in preferences (if available)
virtual FWGUI_API const std::string & getTitle()
returns the title for the dialog