1 #include <cpPlugins/Interface/Parameters.h>
2 #include <cpPlugins/Interface/ProcessObject.h>
3 #include <third_party/tinyxml/tinyxml.h>
5 // -------------------------------------------------------------------------
7 ProcessObject* cpPlugins::Interface::Parameters::
10 return( this->m_Process );
13 // -------------------------------------------------------------------------
14 const cpPlugins::Interface::
15 ProcessObject* cpPlugins::Interface::Parameters::
16 GetProcessObject( ) const
18 return( this->m_Process );
21 // -------------------------------------------------------------------------
22 void cpPlugins::Interface::Parameters::
23 SetProcessObject( ProcessObject* v )
25 if( this->m_Process != v )
33 // -------------------------------------------------------------------------
34 void cpPlugins::Interface::Parameters::
37 this->Superclass::Modified( );
38 if( this->m_Process != NULL )
39 this->m_Process->Modified( );
42 // -------------------------------------------------------------------------
43 void cpPlugins::Interface::Parameters::
46 this->m_Parameters.clear( );
50 // -------------------------------------------------------------------------
51 void cpPlugins::Interface::Parameters::
52 GetNames( std::vector< std::string >& container ) const
55 TParameters::const_iterator i = this->m_Parameters.begin( );
56 for( ; i != this->m_Parameters.end( ); ++i )
57 container.push_back( i->first );
60 // -------------------------------------------------------------------------
61 cpPlugins::Interface::Parameters::
62 Type cpPlugins::Interface::Parameters::
63 GetType( const std::string& name ) const
65 auto i = this->m_Parameters.find( name );
66 if( i != this->m_Parameters.end( ) )
67 return( i->second.first );
69 return( Self::NoType );
72 // -------------------------------------------------------------------------
73 #define cpPlugins_Parameters_TypeAsString( Y ) \
74 if( i->second.first == Self::Y ) \
77 std::string cpPlugins::Interface::Parameters::
78 GetTypeAsString( const std::string& name ) const
80 auto i = this->m_Parameters.find( name );
81 cpPlugins_Parameters_TypeAsString( String );
82 else cpPlugins_Parameters_TypeAsString( Bool );
83 else cpPlugins_Parameters_TypeAsString( Int );
84 else cpPlugins_Parameters_TypeAsString( Uint );
85 else cpPlugins_Parameters_TypeAsString( Real );
86 else cpPlugins_Parameters_TypeAsString( OpenFileName );
87 else cpPlugins_Parameters_TypeAsString( SaveFileName );
88 else cpPlugins_Parameters_TypeAsString( PathName );
89 else cpPlugins_Parameters_TypeAsString( StringList );
90 else cpPlugins_Parameters_TypeAsString( BoolList );
91 else cpPlugins_Parameters_TypeAsString( IntList );
92 else cpPlugins_Parameters_TypeAsString( UintList );
93 else cpPlugins_Parameters_TypeAsString( RealList );
94 else cpPlugins_Parameters_TypeAsString( OpenFileNameList );
95 else cpPlugins_Parameters_TypeAsString( SaveFileNameList );
96 else cpPlugins_Parameters_TypeAsString( PathNameList );
97 else cpPlugins_Parameters_TypeAsString( Choices );
98 else return( "NoType" );
101 // -------------------------------------------------------------------------
102 #define cpPlugins_Parameters_TypeFromString( Y, str ) \
103 if( str == std::string( #Y ) ) \
106 cpPlugins::Interface::Parameters::
107 Type cpPlugins::Interface::Parameters::
108 GetTypeFromString( const std::string& t )
110 cpPlugins_Parameters_TypeFromString( String, t );
111 else cpPlugins_Parameters_TypeFromString( Bool, t );
112 else cpPlugins_Parameters_TypeFromString( Int, t );
113 else cpPlugins_Parameters_TypeFromString( Uint, t );
114 else cpPlugins_Parameters_TypeFromString( Real, t );
115 else cpPlugins_Parameters_TypeFromString( OpenFileName, t );
116 else cpPlugins_Parameters_TypeFromString( SaveFileName, t );
117 else cpPlugins_Parameters_TypeFromString( PathName, t );
118 else cpPlugins_Parameters_TypeFromString( StringList, t );
119 else cpPlugins_Parameters_TypeFromString( BoolList, t );
120 else cpPlugins_Parameters_TypeFromString( IntList, t );
121 else cpPlugins_Parameters_TypeFromString( UintList, t );
122 else cpPlugins_Parameters_TypeFromString( RealList, t );
123 else cpPlugins_Parameters_TypeFromString( OpenFileNameList, t );
124 else cpPlugins_Parameters_TypeFromString( SaveFileNameList, t );
125 else cpPlugins_Parameters_TypeFromString( PathNameList, t );
126 else cpPlugins_Parameters_TypeFromString( Choices, t );
127 else return( Self::NoType );
130 // -------------------------------------------------------------------------
131 std::string cpPlugins::Interface::Parameters::
132 GetString( const std::string& name, bool force ) const
134 auto i = this->m_Parameters.find( name );
135 if( i != this->m_Parameters.end( ) )
137 if( i->second.first == Self::String || force )
138 return( i->second.second );
146 // -------------------------------------------------------------------------
147 void cpPlugins::Interface::Parameters::
148 SetString( const std::string& name, const std::string& v, bool force )
150 auto i = this->m_Parameters.find( name );
151 if( i != this->m_Parameters.end( ) )
153 if( i->second.first == Self::String || force )
155 if( i->second.second != v )
157 i->second.second = v;
167 // -------------------------------------------------------------------------
168 void cpPlugins::Interface::Parameters::
170 const std::string& name, const std::vector< std::string >& choices
173 // It is invalid not to give choices when configuring
174 if( choices.size( ) == 0 )
177 std::stringstream str_choices;
178 str_choices << choices[ 0 ];
179 for( unsigned int i = 1; i < choices.size( ); ++i )
180 str_choices << "#" << choices[ i ];
182 this->m_Parameters[ name ] =
183 TParameter( Self::Choices, str_choices.str( ) );
187 // -------------------------------------------------------------------------
188 std::vector< std::string > cpPlugins::Interface::Parameters::
189 GetChoices( const std::string& name ) const
191 std::vector< std::string > choices;
193 TParameters::const_iterator i = this->m_Parameters.find( name );
194 if( i != this->m_Parameters.end( ) )
196 if( i->second.first == Self::Choices )
198 std::istringstream str_choices( i->second.second );
199 std::string real_choices;
200 std::getline( str_choices, real_choices, '@' );
201 std::istringstream str( real_choices );
203 while( std::getline( str, token, '#' ) )
204 choices.push_back( token );
212 // -------------------------------------------------------------------------
213 std::string cpPlugins::Interface::Parameters::
214 GetSelectedChoice( const std::string& name ) const
216 auto i = this->m_Parameters.find( name );
217 if( i != this->m_Parameters.end( ) )
219 if( i->second.first == Self::Choices )
221 std::istringstream str_choices( i->second.second );
222 std::string real_choice;
223 std::getline( str_choices, real_choice, '@' );
224 std::getline( str_choices, real_choice, '@' );
225 return( real_choice );
234 // -------------------------------------------------------------------------
235 bool cpPlugins::Interface::Parameters::
236 SetSelectedChoice( const std::string& name, const std::string& choice )
238 auto i = this->m_Parameters.find( name );
239 if( i != this->m_Parameters.end( ) )
241 if( i->second.first == Self::Choices )
243 std::istringstream str_choices( i->second.second );
245 std::getline( str_choices, choices, '@' );
246 if( choices.find( choice ) != std::string::npos )
248 std::stringstream new_choices;
249 new_choices << choices << "@" << choice;
250 i->second.second = new_choices.str( );
264 // -------------------------------------------------------------------------
265 std::string cpPlugins::Interface::Parameters::
266 GetAcceptedFileExtensions( const std::string& name ) const
268 auto i = this->m_AcceptedFileExtensions.find( name );
269 if( i != this->m_AcceptedFileExtensions.end( ) )
275 // -------------------------------------------------------------------------
276 void cpPlugins::Interface::Parameters::
277 SetAcceptedFileExtensions(
278 const std::string& name, const std::string& extensions
281 auto i = this->m_Parameters.find( name );
282 if( i != this->m_Parameters.end( ) )
284 bool is_valid = ( i->second.first == Self::OpenFileName );
285 is_valid |= ( i->second.first == Self::SaveFileName );
286 is_valid |= ( i->second.first == Self::OpenFileNameList );
287 is_valid |= ( i->second.first == Self::SaveFileNameList );
289 this->m_AcceptedFileExtensions[ name ] = extensions;
294 // -------------------------------------------------------------------------
295 bool cpPlugins::Interface::Parameters::
296 ToXML( TiXmlElement* parent_elem ) const
298 if( parent_elem == NULL )
301 auto pIt = this->m_Parameters.begin( );
302 for( ; pIt != this->m_Parameters.end( ); ++pIt )
304 TiXmlElement* p = new TiXmlElement( "parameter" );
305 p->SetAttribute( "name", pIt->first.c_str( ) );
306 p->SetAttribute( "value", pIt->second.second.c_str( ) );
307 p->SetAttribute( "type", this->GetTypeAsString( pIt->first ).c_str( ) );
308 parent_elem->LinkEndChild( p );
314 // -------------------------------------------------------------------------
315 bool cpPlugins::Interface::Parameters::
316 FromXML( const TiXmlElement* filter_elem )
318 const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
320 while( param != NULL )
322 const char* param_name = param->Attribute( "name" );
323 const char* param_type = param->Attribute( "type" );
324 if( param_name != NULL && param_type != NULL )
327 value.second = param->Attribute( "value" );
328 value.first = Self::GetTypeFromString( param_type );
329 this->m_Parameters[ param_name ] = value;
332 param = param->NextSiblingElement( "parameter" );
340 // -------------------------------------------------------------------------
341 cpPlugins::Interface::Parameters::
349 // -------------------------------------------------------------------------
350 cpPlugins::Interface::Parameters::
355 // -------------------------------------------------------------------------
356 void cpPlugins::Interface::Parameters::
357 PrintSelf( std::ostream& os, itk::Indent indent ) const
359 TParameters::const_iterator i = this->m_Parameters.begin( );
360 for( ; i != this->m_Parameters.end( ); ++i )
363 << i->second.first << " | "
364 << i->second.second << ")"
368 // -------------------------------------------------------------------------
369 cpPlugins::Interface::Parameters::
370 TParameters& cpPlugins::Interface::Parameters::
373 return( this->m_Parameters );
376 // -------------------------------------------------------------------------
377 const cpPlugins::Interface::Parameters::
378 TParameters& cpPlugins::Interface::Parameters::
379 GetRawParameters( ) const
381 return( this->m_Parameters );