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 )
28 // -------------------------------------------------------------------------
29 void cpPlugins::Interface::Parameters::
32 this->Superclass::Modified( );
33 if( this->m_Process != NULL )
34 this->m_Process->Modified( );
37 // -------------------------------------------------------------------------
38 void cpPlugins::Interface::Parameters::
41 this->m_Parameters.clear( );
45 // -------------------------------------------------------------------------
46 void cpPlugins::Interface::Parameters::
47 GetNames( std::vector< std::string >& container ) const
50 TParameters::const_iterator i = this->m_Parameters.begin( );
51 for( ; i != this->m_Parameters.end( ); ++i )
52 container.push_back( i->first );
55 // -------------------------------------------------------------------------
56 cpPlugins::Interface::Parameters::
57 Type cpPlugins::Interface::Parameters::
58 GetType( const std::string& name ) const
60 auto i = this->m_Parameters.find( name );
61 if( i != this->m_Parameters.end( ) )
62 return( i->second.first );
64 return( Self::NoType );
67 // -------------------------------------------------------------------------
68 #define cpPlugins_Parameters_TypeAsString( Y ) \
69 if( i->second.first == Self::Y ) \
72 std::string cpPlugins::Interface::Parameters::
73 GetTypeAsString( const std::string& name ) const
75 auto i = this->m_Parameters.find( name );
76 cpPlugins_Parameters_TypeAsString( String );
77 else cpPlugins_Parameters_TypeAsString( Bool );
78 else cpPlugins_Parameters_TypeAsString( Int );
79 else cpPlugins_Parameters_TypeAsString( Uint );
80 else cpPlugins_Parameters_TypeAsString( Real );
81 else cpPlugins_Parameters_TypeAsString( OpenFileName );
82 else cpPlugins_Parameters_TypeAsString( SaveFileName );
83 else cpPlugins_Parameters_TypeAsString( PathName );
84 else cpPlugins_Parameters_TypeAsString( StringList );
85 else cpPlugins_Parameters_TypeAsString( BoolList );
86 else cpPlugins_Parameters_TypeAsString( IntList );
87 else cpPlugins_Parameters_TypeAsString( UintList );
88 else cpPlugins_Parameters_TypeAsString( RealList );
89 else cpPlugins_Parameters_TypeAsString( OpenFileNameList );
90 else cpPlugins_Parameters_TypeAsString( SaveFileNameList );
91 else cpPlugins_Parameters_TypeAsString( PathNameList );
92 else cpPlugins_Parameters_TypeAsString( Choices );
93 else return( "NoType" );
96 // -------------------------------------------------------------------------
97 #define cpPlugins_Parameters_TypeFromString( Y, str ) \
98 if( str == std::string( #Y ) ) \
101 cpPlugins::Interface::Parameters::
102 Type cpPlugins::Interface::Parameters::
103 GetTypeFromString( const std::string& t )
105 cpPlugins_Parameters_TypeFromString( String, t );
106 else cpPlugins_Parameters_TypeFromString( Bool, t );
107 else cpPlugins_Parameters_TypeFromString( Int, t );
108 else cpPlugins_Parameters_TypeFromString( Uint, t );
109 else cpPlugins_Parameters_TypeFromString( Real, t );
110 else cpPlugins_Parameters_TypeFromString( OpenFileName, t );
111 else cpPlugins_Parameters_TypeFromString( SaveFileName, t );
112 else cpPlugins_Parameters_TypeFromString( PathName, t );
113 else cpPlugins_Parameters_TypeFromString( StringList, t );
114 else cpPlugins_Parameters_TypeFromString( BoolList, t );
115 else cpPlugins_Parameters_TypeFromString( IntList, t );
116 else cpPlugins_Parameters_TypeFromString( UintList, t );
117 else cpPlugins_Parameters_TypeFromString( RealList, t );
118 else cpPlugins_Parameters_TypeFromString( OpenFileNameList, t );
119 else cpPlugins_Parameters_TypeFromString( SaveFileNameList, t );
120 else cpPlugins_Parameters_TypeFromString( PathNameList, t );
121 else cpPlugins_Parameters_TypeFromString( Choices, t );
122 else return( Self::NoType );
125 // -------------------------------------------------------------------------
126 std::string cpPlugins::Interface::Parameters::
127 GetString( const std::string& name, bool force ) const
129 auto i = this->m_Parameters.find( name );
130 if( i != this->m_Parameters.end( ) )
132 if( i->second.first == Self::String || force )
133 return( i->second.second );
141 // -------------------------------------------------------------------------
142 void cpPlugins::Interface::Parameters::
143 SetString( const std::string& name, const std::string& v, bool force )
145 auto i = this->m_Parameters.find( name );
146 if( i != this->m_Parameters.end( ) )
148 if( i->second.first == Self::String || force )
150 i->second.second = v;
158 // -------------------------------------------------------------------------
159 void cpPlugins::Interface::Parameters::
161 const std::string& name, const std::vector< std::string >& choices
164 // It is invalid not to give choices when configuring
165 if( choices.size( ) == 0 )
168 std::stringstream str_choices;
169 str_choices << choices[ 0 ];
170 for( unsigned int i = 1; i < choices.size( ); ++i )
171 str_choices << "#" << choices[ i ];
173 this->m_Parameters[ name ] =
174 TParameter( Self::Choices, str_choices.str( ) );
178 // -------------------------------------------------------------------------
179 std::vector< std::string > cpPlugins::Interface::Parameters::
180 GetChoices( const std::string& name ) const
182 std::vector< std::string > choices;
184 TParameters::const_iterator i = this->m_Parameters.find( name );
185 if( i != this->m_Parameters.end( ) )
187 if( i->second.first == Self::Choices )
189 std::istringstream str_choices( i->second.second );
190 std::string real_choices;
191 std::getline( str_choices, real_choices, '@' );
192 std::istringstream str( real_choices );
194 while( std::getline( str, token, '#' ) )
195 choices.push_back( token );
203 // -------------------------------------------------------------------------
204 std::string cpPlugins::Interface::Parameters::
205 GetSelectedChoice( const std::string& name ) const
207 auto i = this->m_Parameters.find( name );
208 if( i != this->m_Parameters.end( ) )
210 if( i->second.first == Self::Choices )
212 std::istringstream str_choices( i->second.second );
213 std::string real_choice;
214 std::getline( str_choices, real_choice, '@' );
215 std::getline( str_choices, real_choice, '@' );
216 return( real_choice );
225 // -------------------------------------------------------------------------
226 bool cpPlugins::Interface::Parameters::
227 SetSelectedChoice( const std::string& name, const std::string& choice )
229 auto i = this->m_Parameters.find( name );
230 if( i != this->m_Parameters.end( ) )
232 if( i->second.first == Self::Choices )
234 std::istringstream str_choices( i->second.second );
236 std::getline( str_choices, choices, '@' );
237 if( choices.find( choice ) != std::string::npos )
239 std::stringstream new_choices;
240 new_choices << choices << "@" << choice;
241 i->second.second = new_choices.str( );
254 // -------------------------------------------------------------------------
255 bool cpPlugins::Interface::Parameters::
256 ToXML( TiXmlElement* parent_elem ) const
258 if( parent_elem == NULL )
261 auto pIt = this->m_Parameters.begin( );
262 for( ; pIt != this->m_Parameters.end( ); ++pIt )
264 TiXmlElement* p = new TiXmlElement( "parameter" );
265 p->SetAttribute( "name", pIt->first.c_str( ) );
266 p->SetAttribute( "value", pIt->second.second.c_str( ) );
267 p->SetAttribute( "type", this->GetTypeAsString( pIt->first ).c_str( ) );
268 parent_elem->LinkEndChild( p );
274 // -------------------------------------------------------------------------
275 bool cpPlugins::Interface::Parameters::
276 FromXML( const TiXmlElement* filter_elem )
278 this->m_Parameters.clear( );
280 const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
282 while( param != NULL )
284 const char* param_name = param->Attribute( "name" );
285 const char* param_type = param->Attribute( "type" );
286 if( param_name != NULL && param_type != NULL )
289 value.second = param->Attribute( "value" );
290 value.first = Self::GetTypeFromString( param_type );
291 this->m_Parameters[ param_name ] = value;
294 param = param->NextSiblingElement( "parameter" );
301 // -------------------------------------------------------------------------
302 cpPlugins::Interface::Parameters::
310 // -------------------------------------------------------------------------
311 cpPlugins::Interface::Parameters::
316 // -------------------------------------------------------------------------
317 void cpPlugins::Interface::Parameters::
318 PrintSelf( std::ostream& os, itk::Indent indent ) const
320 TParameters::const_iterator i = this->m_Parameters.begin( );
321 for( ; i != this->m_Parameters.end( ); ++i )
324 << i->second.first << " | "
325 << i->second.second << ")"
329 // -------------------------------------------------------------------------
330 cpPlugins::Interface::Parameters::
331 TParameters& cpPlugins::Interface::Parameters::
334 return( this->m_Parameters );
337 // -------------------------------------------------------------------------
338 const cpPlugins::Interface::Parameters::
339 TParameters& cpPlugins::Interface::Parameters::
340 GetRawParameters( ) const
342 return( this->m_Parameters );