fw4spl
SFilterSelectorDialog.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 "ioDicom/SFilterSelectorDialog.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/Composite.hpp>
16 #include <fwData/String.hpp>
17 
18 #include <fwDataTools/helper/Composite.hpp>
19 
20 #include <fwDicomIOFilter/IFilter.hpp>
21 
22 #include <fwGui/Cursor.hpp>
23 #include <fwGui/dialog/MessageDialog.hpp>
24 #include <fwGui/dialog/SelectorDialog.hpp>
25 
26 #include <fwRuntime/ConfigurationElement.hpp>
27 #include <fwRuntime/helper.hpp>
28 
29 #include <fwServices/macros.hpp>
30 
31 #include <boost/foreach.hpp>
32 
33 #include <sstream>
34 #include <string>
35 
36 namespace ioDicom
37 {
38 
39 //------------------------------------------------------------------------------
40 
42 
43 static const ::fwServices::IService::KeyType s_FILTER_INOUT = "filter";
44 
45 //------------------------------------------------------------------------------
46 
48  m_filtersAreExcluded( true )
49 {
51 }
52 
53 //------------------------------------------------------------------------------
54 
56 {
58 }
59 
60 //------------------------------------------------------------------------------
61 
63 {
65 
66  m_selectedFilters.clear();
67 
68  // Config Elem
69  // <selection mode="exclude">
70  // <addSelection filter="::fwDicomIOFilter::composite::CTImageStorageDefaultComposite" />
71  // <addSelection filter="::fwDicomIOFilter::composite::CTImageStorageDefaultComposite" />
72 
74  for(; iter != this->m_configuration->end(); ++iter )
75  {
76  SLM_INFO( "SFilterSelectorDialog " + (*iter)->getName());
77 
78  if( (*iter)->getName() == "selection" )
79  {
80  SLM_ASSERT( "The xml element <selection> must have the attribute 'mode'.", (*iter)->hasAttribute("mode"));
81  const std::string mode = (*iter)->getExistingAttributeValue("mode");
82  m_filtersAreExcluded = ( mode == "exclude" );
83  SLM_ASSERT( "The xml attribute <mode> must be either 'exclude' or 'include'.", mode == "exclude" ||
84  mode == "include" );
85  SLM_DEBUG( "mode => " + mode );
86  }
87 
88  if( (*iter)->getName() == "addSelection" )
89  {
90  SLM_ASSERT( "The xml element <addSelection> must have the attribute 'filter'.",
91  (*iter)->hasAttribute("filter"));
92  m_selectedFilters.push_back( (*iter)->getExistingAttributeValue("filter") );
93  SLM_DEBUG( "add selection => " + (*iter)->getExistingAttributeValue("filter") );
94  }
95 
96  }
97 
98 }
99 
100 //------------------------------------------------------------------------------
101 
103 {
104  SLM_TRACE_FUNC();
105 }
106 
107 //------------------------------------------------------------------------------
108 
110 {
111  SLM_TRACE_FUNC();
112 }
113 
114 //------------------------------------------------------------------------------
115 
117 {
118  SLM_TRACE_FUNC();
119 
120  // Retrieve available filters
121  std::vector< ::fwDicomIOFilter::IFilter::sptr > registredFilters;
122  for(std::string key: ::fwDicomIOFilter::registry::get()->getFactoryKeys())
123  {
124  ::fwDicomIOFilter::IFilter::sptr filter = ::fwDicomIOFilter::factory::New(key);
125  registredFilters.push_back(filter);
126  }
127 
128  // Filter available extensions and replace id by service description
129  std::map< std::string, ::fwDicomIOFilter::IFilter::sptr > availableFiltersMap;
130  std::vector< std::string > availableFilterNames;
131 
132  for( ::fwDicomIOFilter::IFilter::sptr filter: registredFilters )
133  {
134  const bool filterIsSelectedByUser = std::find( m_selectedFilters.begin(), m_selectedFilters.end(),
135  filter->getClassname() ) != m_selectedFilters.end();
136 
137  // Test if the filter is considered here as available by users
138  // excluded mode => add filters that are not selected by users
139  // included mode => add filters selected by users
140  if( (m_filtersAreExcluded && !filterIsSelectedByUser) ||
141  (!m_filtersAreExcluded && filterIsSelectedByUser) )
142  {
143  // Add this filter
144  std::string filterName = filter->getName();
145  filterName = (filterName.empty()) ? filter->getClassname() : filterName;
146  availableFiltersMap[filterName] = filter;
147  availableFilterNames.push_back( filterName );
148  }
149  }
150 
151  // Sort available services (lexical string sort)
152  std::sort( availableFilterNames.begin(), availableFilterNames.end() );
153 
154  // Test if we have an extension
155  if ( !availableFilterNames.empty() )
156  {
157  std::string filterName = *availableFilterNames.begin();
158  bool filterSelectionIsCanceled = false;
159 
160  // Selection of extension when availableFilterNames.size() > 1
161  if ( availableFilterNames.size() > 1 )
162  {
163  ::fwGui::dialog::SelectorDialog::sptr selector = ::fwGui::dialog::SelectorDialog::New();
164 
165  selector->setTitle("Filter to use");
166  selector->setSelections(availableFilterNames);
167  filterName = selector->show();
168  filterSelectionIsCanceled = filterName.empty();
169 
170  SLM_ASSERT("Unable to find the selected filter name in the filter map.",
171  filterSelectionIsCanceled || availableFiltersMap.find(filterName) != availableFiltersMap.end() );
172  }
173 
174  if ( !filterSelectionIsCanceled )
175  {
176 
177  ::fwDicomIOFilter::IFilter::sptr filter = availableFiltersMap[filterName];
178 
179  ::fwData::String::sptr obj = this->getInOut< ::fwData::String >(s_FILTER_INOUT);
180  if (!obj)
181  {
182  FW_DEPRECATED_KEY(s_FILTER_INOUT, "inout", "fw4spl_18.0");
183  obj = this->getObject< ::fwData::String >();
184  }
185  SLM_ASSERT("The filter selector service must work on a ::fwData::String object.", obj);
186  obj->setValue(filter->getClassname());
187 
188  auto sig
190  {
191  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
192  sig->asyncEmit();
193  }
194  }
195  }
196  else
197  {
198  SLM_WARN("SFilterSelectorDialog::load : availableFilters is empty.");
200  messageBox.setTitle("Filter not found");
201  messageBox.setMessage( "There is no available filter for this reader." );
202  messageBox.setIcon(::fwGui::dialog::IMessageDialog::WARNING);
203  messageBox.addButton(::fwGui::dialog::IMessageDialog::OK);
204  messageBox.show();
205  }
206 }
207 
208 //------------------------------------------------------------------------------
209 
210 void SFilterSelectorDialog::info( std::ostream& _sstream )
211 {
212  // Update message
213  _sstream << "SFilterSelectorDialog";
214 }
215 
216 //------------------------------------------------------------------------------
217 
218 } // namespace ioDicom
IODICOM_API SFilterSelectorDialog()
Constructor. Do nothing (Just initialize parameters).
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
IODICOM_API void starting() override
Starts the service. Do nothing.
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
Defines the generic message box for IHM. Use the Delegate design pattern.
IODICOM_API void updating() override
#define SLM_WARN(message)
Definition: spyLog.hpp:261
#define SLM_DEBUG(message)
Definition: spyLog.hpp:239
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
Display a dialog to select a dicom filter.
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
IODICOM_API void configuring() override
This method initializes class member parameters from configuration elements.
ioDicom contains services used to deal with the DICOM standard.
#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
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
IODICOM_API void stopping() override
Stops the service. Do nothing.
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
Defines the service interface managing the editor service which create their own container.
#define SLM_INFO(message)
Definition: spyLog.hpp:250
IODICOM_API void info(std::ostream &_sstream) override
Gives the name of the class. Do nothing.
Container::iterator Iterator
Defines the configuration element container type.
virtual IODICOM_API ~SFilterSelectorDialog() noexcept
Destructor. Do nothing.
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.
This class contains an std::string value.