7 #include "uiIO/editor/SIOSelector.hpp" 9 #include <fwCom/Signal.hxx> 10 #include <fwCom/Slots.hpp> 11 #include <fwCom/Slots.hxx> 13 #include <fwCore/base.hpp> 15 #include <fwData/Composite.hpp> 17 #include <fwDataTools/helper/Composite.hpp> 19 #include <fwGui/Cursor.hpp> 20 #include <fwGui/dialog/MessageDialog.hpp> 21 #include <fwGui/dialog/SelectorDialog.hpp> 23 #include <fwIO/IReader.hpp> 24 #include <fwIO/IWriter.hpp> 26 #include <fwRuntime/ConfigurationElement.hpp> 27 #include <fwRuntime/helper.hpp> 29 #include <fwServices/macros.hpp> 30 #include <fwServices/op/Add.hpp> 31 #include <fwServices/registry/ServiceConfig.hpp> 32 #include <fwServices/registry/ServiceFactory.hpp> 34 #include <boost/foreach.hpp> 49 static const ::fwCom::Signals::SignalKeyType JOB_CREATED_SIGNAL =
"jobCreated";
50 static const ::fwCom::Slots::SlotKeyType FORWARD_JOB_SLOT =
"forwardJob";
55 m_mode( READER_MODE ),
56 m_servicesAreExcluded( true )
58 m_sigJobCreated = newSignal< JobCreatedSignalType >( JOB_CREATED_SIGNAL );
59 m_slotForwardJob = newSlot( FORWARD_JOB_SLOT, &SIOSelector::forwardJob,
this );
74 const std::string mode = srvConfig.get<std::string>(
"type.<xmlattr>.mode",
"reader");
75 SLM_ASSERT(
"The xml attribute <mode> must be 'reader' (to open file) or 'writer' (to write a new file).",
76 mode ==
"writer" || mode ==
"reader" );
80 m_dataClassname = srvConfig.get<std::string>(
"type.<xmlattr>.class",
"");
82 const std::string selectionMode = srvConfig.get<std::string>(
"selection.<xmlattr>.mode",
"exclude");
83 SLM_ASSERT(
"The xml attribute <mode> must be 'include' (to add the selection to selector list ) or " 84 "'exclude' (to exclude the selection of the selector list).",
85 selectionMode ==
"exclude" || selectionMode ==
"include" );
86 m_servicesAreExcluded = ( selectionMode ==
"exclude" );
87 SLM_DEBUG(
"selection mode => " + selectionMode );
89 const auto selectionCfg = srvConfig.equal_range(
"addSelection");
90 for (
auto itSelection = selectionCfg.first; itSelection != selectionCfg.second; ++itSelection)
92 const std::string service = itSelection->second.get<std::string>(
"<xmlattr>.service");
93 m_selectedServices.push_back(service);
94 SLM_DEBUG(
"add selection => " + service );
96 const std::string configId = itSelection->second.get<std::string>(
"<xmlattr>.config",
"");
99 m_serviceToConfig[service] = configId;
100 SLM_DEBUG(
"add config '" + configId +
"' for service '" + service +
"'");
104 const auto configCfg = srvConfig.equal_range(
"config");
105 for (
auto itCfg = configCfg.first; itCfg != configCfg.second; ++itCfg)
107 const std::string service = itCfg->second.get<std::string>(
"<xmlattr>.service");
108 const std::string configId = itCfg->second.get<std::string>(
"<xmlattr>.id");
110 m_serviceToConfig[service] = configId;
111 SLM_DEBUG(
"add config '" + configId +
"' for service '" + service +
"'");
131 bool createOutput =
false;
132 ::fwData::Object::sptr obj = this->getInOut< ::fwData::Object>(::fwIO::s_DATA_KEY);
135 std::vector< std::string > availableExtensionsId;
141 if (!obj && classname.empty())
148 SLM_WARN_IF(
"The 'class' attribute is defined, but the object is set as 'inout', only the object classname " 149 "is used", !classname.empty())
150 classname = obj->getClassname();
152 createOutput = (!obj && !m_dataClassname.empty());
153 availableExtensionsId =
155 classname,
"::fwIO::IReader");
165 availableExtensionsId =
167 obj->getClassname(),
"::fwIO::IWriter");
171 std::vector< std::pair < std::string, std::string > > availableExtensionsMap;
172 std::vector< std::string > availableExtensionsSelector;
174 for(
const std::string& serviceId : availableExtensionsId )
176 bool serviceIsSelectedByUser =
177 std::find( m_selectedServices.begin(), m_selectedServices.end(),
178 serviceId ) != m_selectedServices.end();
183 if( (m_servicesAreExcluded && !serviceIsSelectedByUser) ||
184 (!m_servicesAreExcluded && serviceIsSelectedByUser) )
187 std::string infoUser =
190 std::map< std::string, std::string >::const_iterator iter = m_serviceToConfig.find( serviceId );
191 if ( iter != m_serviceToConfig.end() )
198 availableExtensionsMap.push_back( std::pair < std::string, std::string > (serviceId, infoUser) );
199 availableExtensionsSelector.push_back( infoUser );
203 availableExtensionsMap.push_back( std::pair < std::string, std::string > (serviceId, serviceId) );
204 availableExtensionsSelector.push_back( serviceId );
210 std::sort( availableExtensionsSelector.begin(), availableExtensionsSelector.end() );
213 if ( !availableExtensionsMap.empty() )
215 std::string extensionId = availableExtensionsMap[0].first;
216 bool extensionSelectionIsCanceled =
false;
219 if ( availableExtensionsSelector.size() > 1 )
221 ::fwGui::dialog::SelectorDialog::sptr selector = ::fwGui::dialog::SelectorDialog::New();
225 selector->setTitle(
"Writer to use");
229 selector->setTitle(
"Reader to use");
231 selector->setSelections(availableExtensionsSelector);
232 std::string selection = selector->show();
233 if( !selection.empty() )
235 bool extensionIdFound =
false;
237 typedef std::pair < std::string, std::string > PairType;
238 for(PairType pair : availableExtensionsMap)
240 if (pair.second == selection )
242 extensionId = pair.first;
243 extensionIdFound =
true;
246 OSLM_ASSERT(
"Problem to find the selected string.", extensionIdFound );
250 extensionSelectionIsCanceled =
true;
254 if ( !extensionSelectionIsCanceled )
258 bool hasConfigForService =
false;
259 ::fwRuntime::ConfigurationElement::csptr srvCfg;
260 if ( m_serviceToConfig.find( extensionId ) != m_serviceToConfig.end() )
262 hasConfigForService =
true;
264 m_serviceToConfig[extensionId], extensionId );
266 "No service configuration of type ::fwServices::registry::ServiceConfig was found",
275 obj = ::fwData::factory::New(m_dataClassname);
276 SLM_ASSERT(
"Cannot create object with classname='" + m_dataClassname +
"'", obj);
279 ::fwIO::IReader::sptr reader = ::fwServices::add< ::fwIO::IReader >( extensionId );
280 reader->registerInOut(obj, ::fwIO::s_DATA_KEY);
283 if ( hasConfigForService )
285 reader->setConfiguration( ::fwRuntime::ConfigurationElement::constCast(srvCfg) );
289 auto jobCreatedSignal = reader->signal(
"jobCreated");
292 jobCreatedSignal->connect(m_slotForwardJob);
298 reader->configureWithIHM();
301 cursor.
setCursor(::fwGui::ICursor::BUSY);
306 ::fwServices::OSR::unregisterService(reader);
310 this->
setOutput(::fwIO::s_DATA_KEY, obj);
313 catch (std::exception& e)
315 std::string msg =
"Failed to read : \n" + std::string(e.what());
326 ::fwIO::IWriter::sptr writer =
327 ::fwIO::IWriter::dynamicCast(
factory->create(
"::fwIO::IWriter", extensionId));
328 ::fwServices::OSR::registerService(obj, ::fwIO::s_DATA_KEY,
329 ::fwServices::IService::AccessType::INPUT, writer);
333 if ( hasConfigForService )
335 writer->setConfiguration( ::fwRuntime::ConfigurationElement::constCast(srvCfg) );
339 auto jobCreatedSignal = writer->signal(
"jobCreated");
342 jobCreatedSignal->connect(m_slotForwardJob);
348 writer->configureWithIHM();
351 cursor.
setCursor(::fwGui::ICursor::BUSY);
356 ::fwServices::OSR::unregisterService(writer);
358 catch (std::exception& e)
360 std::string msg =
"Failed to write : \n" + std::string(e.what());
368 SLM_WARN(
"SIOSelector::load : availableExtensions is empty.");
372 messageBox.
setTitle(
"Reader not found");
373 messageBox.
setMessage(
"There are no available readers for this data type." );
374 messageBox.
setIcon(::fwGui::dialog::IMessageDialog::WARNING);
375 messageBox.
addButton(::fwGui::dialog::IMessageDialog::OK);
381 messageBox.
setTitle(
"Writer not found");
382 messageBox.
setMessage(
"There are no available writers for this data type." );
383 messageBox.
setIcon(::fwGui::dialog::IMessageDialog::WARNING);
384 messageBox.
addButton(::fwGui::dialog::IMessageDialog::OK);
395 _sstream <<
"SIOSelector";
409 m_sigJobCreated->emit(iJob);
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
virtual FWGUI_API void setCursor(::fwGui::ICursor::CursorType cursor) override
Set the cursor.
#define OSLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
virtual UIIO_API ~SIOSelector() noexcept
Destructor. Do nothing.
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.
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
UIIO_API void setIOMode(IOMode _mode)
This method allows to configure the service in reader or writer mode (set SIOSelector::m_mode).
IOMode
IOMode enum definition.
std::shared_ptr< ::fwJobs::IJob > sptr
Cancel request callback type.
#define SLM_WARN(message)
#define SLM_DEBUG(message)
Contains fwAtomsFilter::factory utilities.
FWSERVICES_API void setOutput(const ::fwServices::IService::KeyType &key, const ::fwData::Object::sptr &object, size_t index=0)
Register an output object at a given key in the OSR, replacing it if it already exists.
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
UIIO_API void starting() override
Starts the service. Do nothing.
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
UIIO_API void stopping() override
Stops the service. Do nothing.
UIIO_API SIOSelector()
Constructor. Do nothing (Just initialize parameters).
Base class for each data object.
static const std::string & classname()
return object's classname without its namespace, i.e. BaseObject
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
FWSERVICES_API::fwData::Object::sptr getObject()
Return the object associated to service.
UIIO_API void updating() override
Create a dialogue box to provide the user different available readers (writer) for the IOSelector ass...
Defines the generic cursor for IHM. Use the Delegate design pattern.
static FWSERVICES_API ServiceFactory::sptr getDefault()
Return the unique Instance, create it if required at first access.
std::shared_ptr< ::fwThread::Worker > m_associatedWorker
Associated worker.
Defines the service interface managing the editor service which create their own container.
static FWSERVICES_API ServiceConfig::sptr getDefault()
Return the default global instance of ServiceConfig.
UIIO_API void info(std::ostream &_sstream) override
Gives the name of the class. Do nothing.
This service displays a list of available readers or writers and lets you select one to load or save ...
UIIO_API void configuring() override
This method initializes class member parameters from configuration elements.
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.
The namespace uiIO contains a simple service to manipulate IO with IHM..
#define SLM_WARN_IF(message, cond)
virtual FWGUI_API void setDefaultCursor() override
Set the default cursor.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.