1 #include <cpPlugins/Interface/Parameters.h>
2 #include <cpPlugins/Interface/ProcessObject.h>
3 #include <third_party/tinyxml/tinyxml.h>
5 // -------------------------------------------------------------------------
6 const cpPlugins::Interface::Parameters::
7 TParameters& cpPlugins::Interface::Parameters::
8 GetRawParameters( ) const
10 return( this->m_Parameters );
13 // -------------------------------------------------------------------------
14 cpPlugins::Interface::
15 ProcessObject* cpPlugins::Interface::Parameters::
18 return( this->m_Process );
21 // -------------------------------------------------------------------------
22 const cpPlugins::Interface::
23 ProcessObject* cpPlugins::Interface::Parameters::
24 GetProcessObject( ) const
26 return( this->m_Process );
29 // -------------------------------------------------------------------------
30 void cpPlugins::Interface::Parameters::
31 SetProcessObject( ProcessObject* v )
36 // -------------------------------------------------------------------------
37 void cpPlugins::Interface::Parameters::
40 this->Superclass::Modified( );
41 if( this->m_Process != NULL )
42 this->m_Process->Modified( );
45 // -------------------------------------------------------------------------
46 void cpPlugins::Interface::Parameters::
49 this->m_Parameters.clear( );
53 // -------------------------------------------------------------------------
54 void cpPlugins::Interface::Parameters::
55 GetNames( std::vector< TString >& container ) const
58 TParameters::const_iterator i = this->m_Parameters.begin( );
59 for( ; i != this->m_Parameters.end( ); ++i )
60 container.push_back( i->first );
63 // -------------------------------------------------------------------------
64 cpPlugins::Interface::Parameters::
65 Type cpPlugins::Interface::Parameters::
66 GetType( const TString& name ) const
68 auto i = this->m_Parameters.find( name );
69 if( i != this->m_Parameters.end( ) )
70 return( i->second.first );
72 return( Self::NoType );
75 // -------------------------------------------------------------------------
76 #define cpPlugins_Parameters_TypeAsString( Y ) \
77 if( i->second.first == Self::Y ) \
80 std::string cpPlugins::Interface::Parameters::
81 GetTypeAsString( const TString& name ) const
83 auto i = this->m_Parameters.find( name );
84 cpPlugins_Parameters_TypeAsString( String );
85 else cpPlugins_Parameters_TypeAsString( Bool );
86 else cpPlugins_Parameters_TypeAsString( Int );
87 else cpPlugins_Parameters_TypeAsString( Uint );
88 else cpPlugins_Parameters_TypeAsString( Real );
89 else cpPlugins_Parameters_TypeAsString( Index );
90 else cpPlugins_Parameters_TypeAsString( Point );
91 else cpPlugins_Parameters_TypeAsString( Vector );
92 else cpPlugins_Parameters_TypeAsString( FileName );
93 else cpPlugins_Parameters_TypeAsString( PathName );
94 else cpPlugins_Parameters_TypeAsString( StringList );
95 else cpPlugins_Parameters_TypeAsString( BoolList );
96 else cpPlugins_Parameters_TypeAsString( IntList );
97 else cpPlugins_Parameters_TypeAsString( UintList );
98 else cpPlugins_Parameters_TypeAsString( RealList );
99 else cpPlugins_Parameters_TypeAsString( IndexList );
100 else cpPlugins_Parameters_TypeAsString( PointList );
101 else cpPlugins_Parameters_TypeAsString( VectorList );
102 else cpPlugins_Parameters_TypeAsString( FileNameList );
103 else cpPlugins_Parameters_TypeAsString( PathNameList );
104 else cpPlugins_Parameters_TypeAsString( Choices );
105 else return( "NoType" );
108 // -------------------------------------------------------------------------
109 #define cpPlugins_Parameters_TypeFromString( Y, str ) \
110 if( str == std::string( #Y ) ) \
113 cpPlugins::Interface::Parameters::
114 Type cpPlugins::Interface::Parameters::
115 GetTypeFromString( const std::string& t )
117 cpPlugins_Parameters_TypeFromString( String, t );
118 else cpPlugins_Parameters_TypeFromString( Bool, t );
119 else cpPlugins_Parameters_TypeFromString( Int, t );
120 else cpPlugins_Parameters_TypeFromString( Uint, t );
121 else cpPlugins_Parameters_TypeFromString( Real, t );
122 else cpPlugins_Parameters_TypeFromString( Index, t );
123 else cpPlugins_Parameters_TypeFromString( Point, t );
124 else cpPlugins_Parameters_TypeFromString( Vector, t );
125 else cpPlugins_Parameters_TypeFromString( FileName, t );
126 else cpPlugins_Parameters_TypeFromString( PathName, t );
127 else cpPlugins_Parameters_TypeFromString( StringList, t );
128 else cpPlugins_Parameters_TypeFromString( BoolList, t );
129 else cpPlugins_Parameters_TypeFromString( IntList, t );
130 else cpPlugins_Parameters_TypeFromString( UintList, t );
131 else cpPlugins_Parameters_TypeFromString( RealList, t );
132 else cpPlugins_Parameters_TypeFromString( IndexList, t );
133 else cpPlugins_Parameters_TypeFromString( PointList, t );
134 else cpPlugins_Parameters_TypeFromString( VectorList, t );
135 else cpPlugins_Parameters_TypeFromString( FileNameList, t );
136 else cpPlugins_Parameters_TypeFromString( PathNameList, t );
137 else cpPlugins_Parameters_TypeFromString( Choices, t );
138 else return( Self::NoType );
141 // -------------------------------------------------------------------------
142 std::string cpPlugins::Interface::Parameters::
143 GetString( const std::string& name, bool force ) const
145 auto i = this->m_Parameters.find( name );
146 if( i != this->m_Parameters.end( ) )
148 if( i->second.first == Self::String || force )
149 return( i->second.second );
157 // -------------------------------------------------------------------------
158 void cpPlugins::Interface::Parameters::
159 SetString( const std::string& name, const std::string& v, bool force )
161 auto i = this->m_Parameters.find( name );
162 if( i != this->m_Parameters.end( ) )
164 if( i->second.first == Self::String || force )
166 i->second.second = v;
174 // -------------------------------------------------------------------------
175 void cpPlugins::Interface::Parameters::
177 const std::string& name, const std::vector< std::string >& choices
180 // It is invalid not to give choices when configuring
181 if( choices.size( ) == 0 )
184 std::stringstream str_choices;
185 str_choices << choices[ 0 ];
186 for( unsigned int i = 1; i < choices.size( ); ++i )
187 str_choices << "#" << choices[ i ];
189 this->m_Parameters[ name ] =
190 TParameter( Self::Choices, str_choices.str( ) );
194 // -------------------------------------------------------------------------
195 std::vector< std::string > cpPlugins::Interface::Parameters::
196 GetChoices( const TString& name ) const
198 std::vector< std::string > choices;
200 TParameters::const_iterator i = this->m_Parameters.find( name );
201 if( i != this->m_Parameters.end( ) )
203 if( i->second.first == Self::Choices )
205 std::istringstream str_choices( i->second.second );
206 std::string real_choices;
207 std::getline( str_choices, real_choices, '@' );
208 std::istringstream str( real_choices );
210 while( std::getline( str, token, '#' ) )
211 choices.push_back( token );
219 // -------------------------------------------------------------------------
220 std::string cpPlugins::Interface::Parameters::
221 GetSelectedChoice( const std::string& name ) const
223 auto i = this->m_Parameters.find( name );
224 if( i != this->m_Parameters.end( ) )
226 if( i->second.first == Self::Choices )
228 std::istringstream str_choices( i->second.second );
229 std::string real_choice;
230 std::getline( str_choices, real_choice, '@' );
231 std::getline( str_choices, real_choice, '@' );
232 return( real_choice );
241 // -------------------------------------------------------------------------
242 bool cpPlugins::Interface::Parameters::
243 SetSelectedChoice( const TString& name, const TString& choice )
245 auto i = this->m_Parameters.find( name );
246 if( i != this->m_Parameters.end( ) )
248 if( i->second.first == Self::Choices )
250 std::vector< std::string > c = this->GetChoices( name );
251 if( std::find( c.begin( ), c.end( ), choice ) != c.end( ) )
253 std::istringstream str_choices( i->second.second );
255 std::getline( str_choices, choices, '@' );
256 std::stringstream new_choices;
257 new_choices << choices << "@" << choice;
258 i->second.second = new_choices.str( );
271 // -------------------------------------------------------------------------
272 #define cpPlugins_Parameters_ClearList( Y ) \
273 void cpPlugins::Interface::Parameters:: \
274 Clear##Y##List( const TString& name ) \
276 auto i = this->m_Parameters.find( name ); \
277 if( i == this->m_Parameters.end( ) ) \
279 if( i->second.first != Self::Y##List ) \
281 i->second.second = ""; \
285 cpPlugins_Parameters_ClearList( Index );
286 cpPlugins_Parameters_ClearList( Point );
287 cpPlugins_Parameters_ClearList( Vector );
289 // -------------------------------------------------------------------------
290 bool cpPlugins::Interface::Parameters::
291 ToXML( TiXmlElement* parent_elem ) const
293 if( parent_elem == NULL )
296 auto pIt = this->m_Parameters.begin( );
297 for( ; pIt != this->m_Parameters.end( ); ++pIt )
299 TiXmlElement* p = new TiXmlElement( "parameter" );
300 p->SetAttribute( "name", pIt->first.c_str( ) );
301 p->SetAttribute( "value", pIt->second.second.c_str( ) );
302 p->SetAttribute( "type", this->GetTypeAsString( pIt->first ).c_str( ) );
303 parent_elem->LinkEndChild( p );
309 // -------------------------------------------------------------------------
310 bool cpPlugins::Interface::Parameters::
311 FromXML( const TiXmlElement* filter_elem )
313 this->m_Parameters.clear( );
315 const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
317 while( param != NULL )
319 const char* param_name = param->Attribute( "name" );
320 const char* param_type = param->Attribute( "type" );
321 if( param_name != NULL && param_type != NULL )
324 value.second = param->Attribute( "value" );
325 value.first = Self::GetTypeFromString( param_type );
326 this->m_Parameters[ param_name ] = value;
329 param = param->NextSiblingElement( "parameter" );
336 // -------------------------------------------------------------------------
337 cpPlugins::Interface::Parameters::
345 // -------------------------------------------------------------------------
346 cpPlugins::Interface::Parameters::
351 // -------------------------------------------------------------------------
352 void cpPlugins::Interface::Parameters::
353 PrintSelf( std::ostream& os, itk::Indent indent ) const
355 TParameters::const_iterator i = this->m_Parameters.begin( );
356 for( ; i != this->m_Parameters.end( ); ++i )
359 << i->second.first << " | "
360 << i->second.second << ")"